1 | <p align="center">
|
2 | <a href="https://npmjs.com/package/serverless-openapi-integration-helper">
|
3 | <img src="https://flat.badgen.net/npm/v/serverless-openapi-integration-helper?icon=npm&label=npm@latest"></a>
|
4 | <a href="https://www.npmjs.com/package/serverless-openapi-integration-helper">
|
5 | <img src="https://flat.badgen.net/npm/dt/serverless-openapi-integration-helper?icon=npm"></a>
|
6 | <a href="https://packagephobia.now.sh/result?p=serverless-openapi-integration-helper">
|
7 | <img src="https://flat.badgen.net/packagephobia/install/serverless-openapi-integration-helper"></a>
|
8 | <a href="https://www.npmjs.com/package/serverless-openapi-integration-helper">
|
9 | <img src="https://flat.badgen.net/npm/license/serverless-openapi-integration-helper"></a>
|
10 | <br/>
|
11 | </p>
|
12 |
|
13 | _Feedback is appreciated! If you have an idea for how this plugin/library can be improved (or even just a complaint/criticism) then please open an issue._
|
14 |
|
15 | # Serverless Plugin: AWS Api Gateway integration helper
|
16 |
|
17 | 1. [Overview](#overview)
|
18 | 1. [Installation & Setup](#installation--setup)
|
19 | 1. [Plugin configuration](#plugin-configuration)
|
20 | 1. [Usage](#usage)
|
21 | 1. [Command](#commands)
|
22 | 1. [CORS Generator](#cors-generator)
|
23 | 1. [AUTO-MOCK Generator](#auto-mock-generator)
|
24 | 1. [VALIDATION Generator](#validation-generator)
|
25 | 1. [Configuration Reference](#configuration-reference)
|
26 | 1. [Known Issues](#known-issues)
|
27 | 1. [Stage Deployment](#stage-deployment)
|
28 | 1. [Variable Resolving](#variable-resolving)
|
29 | 1. [Example](#example)
|
30 | 1. [Approach to a functional test of schema validation](#approach-to-a-functional-test-of-schema-validation)
|
31 |
|
32 | # Overview
|
33 | The plugin provides the functionality to merge [OpenApiSpecification files](https://swagger.io/specification/) (formerly known as swagger) with one or multiple YML files containing the the x-amazon-apigateway extensions.
|
34 | There are several use-cases to keep both information separated, e.g. it is needed to deploy different api gateway integrations depending on a stage environment.
|
35 |
|
36 | When dealing with functional tests you do not want to test the production environment, but only a mocking response.
|
37 |
|
38 | **The plugin supports YML based OpenApi3 specification files only**
|
39 |
|
40 | ## Features
|
41 | - deploy stage dependent x-amazon-apigateway integrations
|
42 | - separate infrastructure (aws) from openapi specification
|
43 | - use mock integrations for functional testing
|
44 | - auto-generating CORS methods, headers and api gateway mocking response
|
45 | - hook into package & deploy lifeCycle and generate combined openApi files on the fly during deployment
|
46 | - auto-inject generated openApi file into the Body property of specified API Gateway
|
47 | - **[NEW]:** generate mocking responses without specifying x-amazon-apigateway-integration objects
|
48 | - **[NEW]:** generate request-validation blocks
|
49 | - **[NEW]:** generate all required x-amazon-apigateway-integration objects automatically
|
50 |
|
51 | See the **examples** folder for a full working [example](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples)
|
52 |
|
53 | # Installation & Setup
|
54 |
|
55 | Run `npm install` in your Serverless project.
|
56 |
|
57 | `$ npm install --save-dev serverless-openapi-integration-helper`
|
58 |
|
59 | Add the plugin to your serverless.yml file
|
60 |
|
61 | ```yml
|
62 | plugins:
|
63 | - serverless-openapi-integration-helper
|
64 | ```
|
65 | # Plugin configuration
|
66 | You can configure the plugin under the key **openApiIntegration**. See
|
67 | See [Configuration Reference](#configuration-reference) for a list of available options
|
68 |
|
69 | The mapping array must be used to configure where the files containing the **x-amazon-apigateway-integration** blocks are located.
|
70 |
|
71 | ```yml
|
72 | openApiIntegration:
|
73 | package: true #New feature! Hook into the package & deploy process
|
74 | inputFile: schema.yml
|
75 | mapping:
|
76 | - stage: [dev, prod] #multiple stages
|
77 | path: integrations
|
78 | - stage: test #single stage
|
79 | path: mocks
|
80 | ```
|
81 |
|
82 | In the above example all YML files inside the _integrations_ directory will be processed and merged with the schema.yml file when deploying the dev stage
|
83 | ```shell
|
84 | serverless deploy --stage=dev
|
85 | ```
|
86 |
|
87 | To use a different x-amazon-apigateway to perform functional tests (with mocking responses e.g) the directory mock is processed and merged with the schema.yml file when deploying the test stage
|
88 | ```shell
|
89 | serverless deploy --stage=test
|
90 | ```
|
91 |
|
92 | # Usage
|
93 | You can setup a fully working API GATEWAY with any openApi 3.0 specification file
|
94 | First create the input file containing the [OpenApiSpecification](https://swagger.io/specification/)
|
95 | ```yml
|
96 | # ./schema.yml
|
97 | openapi: 3.0.0
|
98 | info:
|
99 | description: User Registration
|
100 | version: 1.0.0
|
101 | title: UserRegistration
|
102 | paths:
|
103 | /api/v1/user:
|
104 | post:
|
105 | summary: adds a user
|
106 | requestBody:
|
107 | content:
|
108 | application/json:
|
109 | schema:
|
110 | $ref: '#/components/schemas/Customer'
|
111 | responses:
|
112 | '201':
|
113 | description: user created
|
114 | components:
|
115 | schemas:
|
116 | Customer:
|
117 | type: object
|
118 | required:
|
119 | - email_address
|
120 | - password
|
121 | properties:
|
122 | email_address:
|
123 | type: string
|
124 | example: test@example.com
|
125 | password:
|
126 | type: string
|
127 | format: password
|
128 | example: someStrongPassword#
|
129 | ```
|
130 |
|
131 | The plugin will generate the **x-amazon-apigateway integrations** objects for all methods that do not have an integration.
|
132 |
|
133 | ```yml
|
134 | #generate a file containing a gateway mock integration in the directory /mocks
|
135 | serverless integration create --output mocks --type mock --stage=test
|
136 |
|
137 | #generate a file containing the production integration in the directory integrations/
|
138 | serverless integration create --output integrations --type http --stage=prod
|
139 | ```
|
140 |
|
141 | Supported types are
|
142 | - http_proxy
|
143 | - http
|
144 | - aws
|
145 | - aws_proxy
|
146 | - mock
|
147 |
|
148 |
|
149 | The plugin now generates a merged file during deployment that is automatically injected in your serverless resources
|
150 |
|
151 | ```shell
|
152 | #Create OpenApi File containing mocking responses (usable in functional tests) and deploy to ApiGateway
|
153 | serverless deploy --stage==test
|
154 | ```
|
155 |
|
156 | ```shell
|
157 | #Create OpenApi File containing the production integration and deploy to ApiGateway
|
158 | serverless deploy --stage=prod
|
159 | ```
|
160 |
|
161 | The generated output is automatically injected in the **resources.Resources.YOUR_API_GATEWAY.Properties.Body** property
|
162 | ```yml
|
163 | resources:
|
164 | Resources:
|
165 | ApiGatewayRestApi:
|
166 | Type: AWS::ApiGateway::RestApi
|
167 | Properties:
|
168 | ApiKeySourceType: HEADER
|
169 | Body: ~ #autogenerated by plugin
|
170 | Description: "Some Description"
|
171 | FailOnWarnings: false
|
172 | Name: ${opt:stage, self:provider.stage}-some-name
|
173 | EndpointConfiguration:
|
174 | Types:
|
175 | - REGIONAL
|
176 | ApiGatewayDeployment:
|
177 | Type: AWS::ApiGateway::Deployment
|
178 | Properties:
|
179 | RestApiId:
|
180 | Ref: ApiGatewayRestApi
|
181 | StageName: ${opt:stage, self:provider.stage}
|
182 | ```
|
183 |
|
184 | # Commands
|
185 |
|
186 | ## Manual merge
|
187 | The generate command can be used independently with
|
188 | ```yml
|
189 | serverless integration merge --stage=dev
|
190 | ```
|
191 | Of course then the API Gateway Body property has to be specified manually
|
192 |
|
193 | ```yml
|
194 | resources:
|
195 | Resources:
|
196 | ApiGatewayRestApi:
|
197 | Type: AWS::ApiGateway::RestApi
|
198 | Properties:
|
199 | ApiKeySourceType: HEADER
|
200 | Body: ${file(openapi-integration/api.yml)}
|
201 | ```
|
202 |
|
203 | # CORS generator
|
204 |
|
205 | The plugin can generate full CORS support out of the box.
|
206 | ```yml
|
207 | openApiIntegration:
|
208 | cors: true
|
209 | ...
|
210 | ```
|
211 |
|
212 | If enabled, the plugin generates all required OPTIONS methods as well as the required header informations and adds a mocking response to API Gateway.
|
213 | You can customize the CORS templates by placing your own files inside a directory **openapi-integration** (in your project root). The following files can be overwritten:
|
214 |
|
215 | | Filename | Description |
|
216 | | ------------- |:-------------:|
|
217 | | headers.yml | All headers required for CORS support |
|
218 | | integration.yml | Contains the x-amazon-apigateway-integration block |
|
219 | | path.yml| OpenApi specification for the OPTIONS method |
|
220 | | response-parameters.yml| The response Parameters of the x-amazon-apigateway-integration responses |
|
221 |
|
222 | See the [EXAMPLES](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples) directory for detailed instructions.
|
223 |
|
224 | # Auto Mock Generator
|
225 | If enabled, the plugin generates mocking responses for all methods that do not have an x-amazon-apigateway-integration block defined.
|
226 | It takes the first 2xx response defined in the openApi specification and generates a simple mocking response on the fly
|
227 | ```yml
|
228 | openApiIntegration:
|
229 | autoMock: true
|
230 | ...
|
231 | ```
|
232 |
|
233 | When using the autoMock feature, you do not need to specify inputPath mappings, since all endpoints are mocked automatically
|
234 |
|
235 | ```yml
|
236 | openApiIntegration:
|
237 | package: true
|
238 | inputFile: schema.yml
|
239 | mapping: ~
|
240 | ```
|
241 |
|
242 | # VALIDATION generator
|
243 |
|
244 | The plugin supports full request validation out of the box
|
245 | ```yml
|
246 | openApiIntegration:
|
247 | validation: true
|
248 | ...
|
249 | ```
|
250 |
|
251 | If enabled, the plugin generates the x-amazon-apigateway-request-validators blocks and adds a basic request validation to all methods.
|
252 | You can customize the VALIDATION template by placing your own files inside a directory **openapi-integration** (in your project root). The following files can be overwritten:
|
253 |
|
254 | | Filename | Description |
|
255 | | ------------- |:-------------:|
|
256 | | request-validator.yml | The x-amazon-apigateway-request-validators block |
|
257 |
|
258 | See the [EXAMPLES](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples) directory for detailed instructions.
|
259 |
|
260 | # Configuration Reference
|
261 |
|
262 | configure the plugin under the key **openApiIntegration**
|
263 |
|
264 | ```yml
|
265 | openApiIntegration:
|
266 | inputFile: schema.yml #required
|
267 | package: true #optionl defaults to false
|
268 | inputDirectory: ./ #optional, defaults to ./
|
269 | cors: true #optional, defaults to false
|
270 | autoMock: true #optional, defaults to false
|
271 | validation: true #optional, defaults to false
|
272 | mapping: #optional, can be completely blank if autoMock option is enabled
|
273 | - stage: [dev, prod] #multiple stages
|
274 | path: integrations
|
275 | - stage: test #single stage
|
276 | path: mocks/customer.yml
|
277 | outputFile: api.yml #optional, defaults to api.yml
|
278 | outputDirectory: openapi-integration #optional, defaults to ./openapi-integration
|
279 | ```
|
280 |
|
281 | # Known Issues
|
282 |
|
283 | ## Stage deployment
|
284 | When using serverless framework only to deploy your aws resources **without having any lambda functions or triggers**, the AWS Gateway deploymemt does not behave as expected.
|
285 | Any deployment to an existing stage will be ignored, since CloudFormation does not redeploy a stage if the DeploymentIdentifier has not changed.
|
286 |
|
287 | The plugin [serverless-random-gateway-deployment-id](https://www.npmjs.com/package/serverless-random-gateway-deployment-id) solves this problem by adding a random id to the deployment-name and all references to it on every deploy
|
288 |
|
289 | See the **examples** folder for a full working [example](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples)
|
290 |
|
291 | ## Variable Resolving
|
292 | Serverless variables inside the openapi integration files are not resolved correctly when using the package & deploy hooks. This problem can be solved by using the api gateway STAGE VARIABLES.
|
293 |
|
294 | See the **examples** folder for a full working [example](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples)
|
295 |
|
296 | # Example
|
297 | ```yml
|
298 | service:
|
299 | name: user-registration
|
300 |
|
301 | provider:
|
302 | name: aws
|
303 | stage: dev
|
304 | region: eu-central-1
|
305 |
|
306 | plugins:
|
307 | - serverless-openapi-integration-helper
|
308 |
|
309 | openApiIntegration:
|
310 | inputFile: schema.yml
|
311 | package: true
|
312 | mapping:
|
313 | - path: integrations
|
314 | stage: [dev, prod]
|
315 | - path: mocks/customer.yml
|
316 | stage: test
|
317 |
|
318 | functions:
|
319 |
|
320 | resources:
|
321 | Resources:
|
322 | ApiGatewayRestApi:
|
323 | Type: AWS::ApiGateway::RestApi
|
324 | Properties:
|
325 | ApiKeySourceType: HEADER
|
326 | Body: ~
|
327 | Description: "Some Description"
|
328 | FailOnWarnings: false
|
329 | Name: ${opt:stage, self:provider.stage}-some-name
|
330 | EndpointConfiguration:
|
331 | Types:
|
332 | - REGIONAL
|
333 | ApiGatewayDeployment:
|
334 | Type: AWS::ApiGateway::Deployment
|
335 | Properties:
|
336 | RestApiId:
|
337 | Ref: ApiGatewayRestApi
|
338 | StageName: ${opt:stage, self:provider.stage}
|
339 | ```
|
340 |
|
341 | ```
|
342 | serverless deploy --stage=test
|
343 | ```
|
344 |
|
345 | ```
|
346 | serverless deploy --stage=prod
|
347 | ```
|
348 |
|
349 | # Approach to a functional test of schema validation
|
350 | The plugin works well in combination with the [serverless-plugin-test-helper](https://www.npmjs.com/package/serverless-plugin-test-helper) to automate tests against the deployed api gateway
|
351 |
|
352 | ## Install The plugin test helper package
|
353 |
|
354 | ```shell
|
355 | npm install --save-dev serverless-plugin-test-helper
|
356 | ```
|
357 |
|
358 | add the plugin as a plugin dependency in your serverless configuration file and configure the plugin according to the [Readme](https://www.npmjs.com/package/serverless-plugin-test-helper)
|
359 | ```yml
|
360 | #./serveless.yml
|
361 | plugins:
|
362 | - serverless-plugin-test-helper
|
363 | - serverless-openapi-integration-helper
|
364 |
|
365 | [...]
|
366 |
|
367 | resources:
|
368 | Outputs:
|
369 | GatewayUrl: # This is the key that will be used in the generated outputs file
|
370 | Description: This is a helper for functional tests
|
371 | Value: !Join
|
372 | - ''
|
373 | - - 'https://'
|
374 | - !Ref ApiGatewayRestApi
|
375 | - '.execute-api.'
|
376 | - ${opt:region, self:provider.region}
|
377 | - '.amazonaws.com/'
|
378 | - ${opt:stage, self:provider.stage}
|
379 |
|
380 | Resources:
|
381 | ApiGatewayRestApi:
|
382 | Type: AWS::ApiGateway::RestApi
|
383 | Properties:
|
384 | ApiKeySourceType: HEADER
|
385 | Body: ~
|
386 | Description: User Registration (${opt:stage, self:provider.stage})
|
387 | FailOnWarnings: false
|
388 | Name: ${opt:stage, self:provider.stage}-gateway
|
389 | EndpointConfiguration:
|
390 | Types:
|
391 | - REGIONAL
|
392 | ApiGatewayDeployment:
|
393 | Type: AWS::ApiGateway::Deployment
|
394 | Properties:
|
395 | RestApiId:
|
396 | Ref: ApiGatewayRestApi
|
397 | StageName: ${opt:stage, self:provider.stage}
|
398 | ```
|
399 |
|
400 | ## Testing the schema validation
|
401 | Add a functional test (e.g. with jest)
|
402 |
|
403 | ```javascript
|
404 | //tests/registration.js
|
405 | import {getOutput} from 'serverless-plugin-test-helper';
|
406 | import axios from 'axios';
|
407 |
|
408 | axios.defaults.adapter = require('axios/lib/adapters/http'); //Todo
|
409 |
|
410 | const URL = getOutput('GatewayUrl');
|
411 | test('request validation on registration', async () => {
|
412 | expect.assertions(1);
|
413 | const {status} = await axios.post(URL + '/api/v1/user',
|
414 | {
|
415 | "email_address": "test@example.com",
|
416 | "password": "someStrongPassword#"
|
417 | },
|
418 | {
|
419 | headers: {
|
420 | 'Content-Type': 'application/json',
|
421 | }
|
422 | });
|
423 | expect(status).toEqual(201);
|
424 | });
|
425 |
|
426 | test('request validation on registration (invalid request)', async () => {
|
427 | expect.assertions(1);
|
428 | try {
|
429 | await axios.post(URL + '/api/v1/user',
|
430 | {
|
431 | "email": "test@example.com"
|
432 | },
|
433 | {
|
434 | headers: {
|
435 | 'Content-Type': 'application/json',
|
436 | }
|
437 | });
|
438 | } catch (e) {
|
439 | expect(e.response).toMatchObject({
|
440 | statusText: 'Bad Request',
|
441 | status: 400
|
442 | });
|
443 | }
|
444 | });
|
445 | ```
|
446 | Then perform the functional test
|
447 | ```shell
|
448 | serverless deploy --stage=test
|
449 | npm test
|
450 | serverless remove --stage=test
|
451 | ```
|
452 |
|
453 | The command will
|
454 | - merge the openapi specification file with the MOCK integration configured before
|
455 | - deploy to API Gateway in an isolated TEST infrastructure environment (your other environments will not be affected since we are deploying to a separated gateway)
|
456 | - perform the test and verify that schema validation is correct
|
457 | - remove all TEST resources if test succeeded
|
458 |
|
459 | See the **examples** folder for a full working [example](https://github.com/yndlingsfar/serverless-openapi-integration-helper/tree/main/examples)
|