Skip to content

Commit

Permalink
Refactor to aws-serverless-swagger-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
sylwit committed Jan 5, 2021
1 parent 7db7b7e commit 1d443d5
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 330 deletions.
26 changes: 1 addition & 25 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,7 @@ on:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -30,18 +19,5 @@ jobs:
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
64 changes: 31 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
# Swagger UI AWS API Gateway
# AWS Serverless Swagger UI

This module is branched from [swagger-ui-express](https://www.npmjs.com/package/swagger-ui-express). It allows you to serve auto-generated [swagger-ui](https://swagger.io/tools/swagger-ui/) generated API docs from the AWS API Gateway. It uses Lambda functions to generate the API docs based on a `swagger.json` or `swagger.yaml` file. The result is living documentation for your API hosted on your AWS API Gateway.
This module allows you to serve auto-generated [swagger-ui](https://swagger.io/tools/swagger-ui/) API docs
from the AWS API Gateway.
It uses Lambda functions to generate the API docs based on a swagger/open-api yaml file.
The result is living documentation for your API hosted on your AWS API Gateway.

Swagger version is pulled from npm module swagger-ui-dist. Please use a lock file or specify the version of swagger-ui-dist you want to ensure it is consistent across environments.
Initially based upon [swagger-ui-aws-apigateway](https://github.com/klauslochmann/swagger-ui-aws-apigateway),
this version focus on :
* simplify the code base.
* speed up rendering
* update code with ES6 syntax including async handler.
* reduce the number of HTTP requests.
* accept any filename in `.yaml` for the definition, not only `interface.yaml`.
* allow the path to be whatever you want, not limited to `/api-docs` (see [Restrictions](#restrictions)).
* fix media files rendering in base64.

You may be also interested in:

* [swagger tools](https://github.com/swagger-api): Various tools, including swagger editor, swagger code gen etc.

## Usage

Install using npm:
## Install

```bash
$ npm install swagger-ui-aws-apigateway
npm install aws-serverless-swagger-ui
```

## Usage

Configure your API Gateway:
* Create a resource of type "proxy resource"
* Create a resource which will serve your sagger UI (ex: /docs)
* Create a resource of type "proxy resource" (ex: /docs/{proxy+})
* Create a method ANY
* Create a lambda function of type "LAMBDA_PROXY"

In your AWS lambda function, include this package as follows:

```javascript
const swaggerUi = require('swagger-ui-aws-apigateway');
const fs = require('fs');

// read your yaml file and initialize
const swaggerDocument = fs.readFileSync('./interface/service_interface.yaml');
var swaggerHandler = swaggerUi.setup(swaggerDocument);
const swaggerUi = require('aws-serverless-swagger-ui');
const swaggerHandler = swaggerUi.setup('swagger.yaml');

// call the swagger api doc in your handler

exports.handler = (event, context, callback) => {

if (event.path.includes("/api-docs")) {
console.log("Got request to the api docs.");
swaggerHandler(event, context, callback);
return;
}

// ... your other code ...

exports.handler = async (event, context, callback) => {
return (await swaggerHandler)(event, context, callback);
}
```

## Restrictions
Once deployed, access your swagger ui at : `https://your-api-gateway-endpoint/your-mount-path/index.html`


Currently the path in the url must be named "api-docs".
## Restrictions

## Requirements
Because API Gateway doesn't match the root folder with {proxy+} definition, your default URL should contain index.html.
We suggest creating a mock integration on your main folder to return a 301. (ex: /docs => 301 => /docs/index.html)

* Node v0.10.32 or above
## Logs

You can print some logs by setting the Environment Variable `DEBUG`
Loading

0 comments on commit 1d443d5

Please # to comment.