|
1 | | -# Swagger UI AWS API Gateway |
| 1 | +# AWS Serverless Swagger UI |
2 | 2 |
|
3 | | -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. |
| 3 | +This module allows you to serve auto-generated [swagger-ui](https://swagger.io/tools/swagger-ui/) API docs |
| 4 | +from the AWS API Gateway. |
| 5 | +It uses Lambda functions to generate the API docs based on a swagger/open-api yaml file. |
| 6 | +The result is living documentation for your API hosted on your AWS API Gateway. |
4 | 7 |
|
5 | | -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. |
| 8 | +Initially based upon [swagger-ui-aws-apigateway](https://github.com/klauslochmann/swagger-ui-aws-apigateway), |
| 9 | +this version focus on : |
| 10 | +* simplify the code base. |
| 11 | +* speed up rendering |
| 12 | +* update code with ES6 syntax including async handler. |
| 13 | +* reduce the number of HTTP requests. |
| 14 | +* accept any filename in `.yaml` for the definition, not only `interface.yaml`. |
| 15 | +* allow the path to be whatever you want, not limited to `/api-docs` (see [Restrictions](#restrictions)). |
| 16 | +* fix media files rendering in base64. |
6 | 17 |
|
7 | | -You may be also interested in: |
8 | | - |
9 | | -* [swagger tools](https://github.com/swagger-api): Various tools, including swagger editor, swagger code gen etc. |
10 | | - |
11 | | -## Usage |
12 | | - |
13 | | -Install using npm: |
| 18 | +## Install |
14 | 19 |
|
15 | 20 | ```bash |
16 | | -$ npm install swagger-ui-aws-apigateway |
| 21 | +npm install aws-serverless-swagger-ui |
17 | 22 | ``` |
18 | 23 |
|
| 24 | +## Usage |
| 25 | + |
19 | 26 | Configure your API Gateway: |
20 | | -* Create a resource of type "proxy resource" |
| 27 | +* Create a resource which will serve your sagger UI (ex: /docs) |
| 28 | +* Create a resource of type "proxy resource" (ex: /docs/{proxy+}) |
21 | 29 | * Create a method ANY |
22 | 30 | * Create a lambda function of type "LAMBDA_PROXY" |
23 | 31 |
|
24 | 32 | In your AWS lambda function, include this package as follows: |
25 | 33 |
|
26 | 34 | ```javascript |
27 | | -const swaggerUi = require('swagger-ui-aws-apigateway'); |
28 | | -const fs = require('fs'); |
29 | | - |
30 | | -// read your yaml file and initialize |
31 | | -const swaggerDocument = fs.readFileSync('./interface/service_interface.yaml'); |
32 | | -var swaggerHandler = swaggerUi.setup(swaggerDocument); |
| 35 | +const swaggerUi = require('aws-serverless-swagger-ui'); |
| 36 | +const swaggerHandler = swaggerUi.setup('swagger.yaml'); |
33 | 37 |
|
34 | | -// call the swagger api doc in your handler |
35 | | - |
36 | | -exports.handler = (event, context, callback) => { |
37 | | - |
38 | | - if (event.path.includes("/api-docs")) { |
39 | | - console.log("Got request to the api docs."); |
40 | | - swaggerHandler(event, context, callback); |
41 | | - return; |
42 | | - } |
43 | | - |
44 | | - // ... your other code ... |
45 | 38 |
|
| 39 | +exports.handler = async (event, context, callback) => { |
| 40 | + return (await swaggerHandler)(event, context, callback); |
46 | 41 | } |
47 | 42 | ``` |
48 | 43 |
|
49 | | -## Restrictions |
| 44 | +Once deployed, access your swagger ui at : `https://your-api-gateway-endpoint/your-mount-path/index.html` |
| 45 | + |
50 | 46 |
|
51 | | -Currently the path in the url must be named "api-docs". |
| 47 | +## Restrictions |
52 | 48 |
|
53 | | -## Requirements |
| 49 | +Because API Gateway doesn't match the root folder with {proxy+} definition, your default URL should contain index.html. |
| 50 | +We suggest creating a mock integration on your main folder to return a 301. (ex: /docs => 301 => /docs/index.html) |
54 | 51 |
|
55 | | -* Node v0.10.32 or above |
| 52 | +## Logs |
56 | 53 |
|
| 54 | +You can print some logs by setting the Environment Variable `DEBUG` |
0 commit comments