serverless-openapi-documenter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+ - uses: actions/setup-node@v3
16
+ with:
17
+ node-version: 16
18
+ - run: npm ci
19
+ # - run: npm test
20
+
21
+ publish-npm:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ steps:
25
+ - uses: actions/checkout@v3
26
+ - uses: actions/setup-node@v3
27
+ with:
28
+ node-version: 16
29
+ registry-url: https://registry.npmjs.org/
30
+ - run: npm ci
31
+ - run: npm publish
32
+ env:
33
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Jared Evans
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,337 @@
1
+ # OpenAPI Generator for serverless
2
+
3
+ This will generate an OpenAPI V3 (up to v3.0.3) file for you from your serverless file. It can optionally generate a Postman Collection V2 from the OpenAPI file for you too.
4
+
5
+ Originally based off of: https://github.com/temando/serverless-openapi-documentation
6
+
7
+ ## Adding documentation to serverless
8
+
9
+ To Run: `serverless openapi generate -o openapi.json -f json -a 3.0.3 -p postman.json`
10
+
11
+ Options:
12
+
13
+ | Option: | What It Does: |
14
+ |--------------|---------------|
15
+ | -o filename | What filename the OpenAPI documentation should output under |
16
+ | -f filetype | Whether to output the OpenAPI documentation as json or yaml |
17
+ | -a OpenAPI Version | The version of OpenAPI v3 to conform too |
18
+ | -p postman Collection filename | Whether to generate a postman collection from the OpenAPI v3 documentation and the filename to call it, json only |
19
+
20
+
21
+ ### OpenAPI Mapping
22
+
23
+ | OpenAPI field | Serverless field |
24
+ |--------------------|------------------------------------------------------------------------------------|
25
+ | info.title | service |
26
+ | info.description | custom.documentation.description || blank string |
27
+ | info.version | custom.documentation.version || random v4 uuid if not provided |
28
+ | path[path] | functions.functions.events.[http||httpApi].path |
29
+ | path[path].summary | functions.functions.summary |
30
+ | path[path].description | functions.functions.description |
31
+ | path[path].[operation] | functions.functions.[http||httpApi].method |
32
+ | path[path].[operation].summary | functions.functions.[http||httpApi].documentation.summary |
33
+ | path[path].[operation].description | functions.functions.[http||httpApi].documentation.description |
34
+ | path[path].[operation].operationId | functions.functions.[http||httpApi].documentation.operationId || functionName |
35
+ | path[path].[operation].deprecated | functions.functions.[http||httpApi].documentation.deprecated |
36
+ | path[path].[operation].parameters | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params |
37
+ | path[path].[operation].parameters.name | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.name |
38
+ | path[path].[operation].parameters.in | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params |
39
+ | path[path].[operation].parameters.description | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.description |
40
+ | path[path].[operation].parameters.required | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.required |
41
+ | path[path].[operation].parameters.deprecated | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.deprecated |
42
+ | path[path].[operation].parameters.allowEmptyValue | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.allowEmptyValue |
43
+ | path[path].[operation].parameters.style | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.style |
44
+ | path[path].[operation].parameters.explode | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.explode |
45
+ | path[path].[operation].parameters.allowReserved | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.allowReserved |
46
+ | path[path].[operation].parameters.schema | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.schema |
47
+ | path[path].[operation].parameters.example | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.example |
48
+ | path[path].[operation].parameters.examples | functions.functions.[http||httpApi].documentation.[path|query|cookie|header]Params.examples |
49
+ | path[path].[operation].requestBody | functions.functions.[http||httpApi].documentation.requestBody |
50
+ | path[path].[operation].requestBody.description | functions.functions.[http||httpApi].documentation.requestBody.description |
51
+ | path[path].[operation].requestBody.required | functions.functions.[http||httpApi].documentation.requestBody.required |
52
+ | path[path].[operation].requestBody.content | functions.functions.[http||httpApi].documentation.requestModels[contentType].name Links to custom.documentation.models.name |
53
+ | path[path].[operation].responses | functions.functions.[http||httpApi].documentation.methodResponses |
54
+ | path[path].[operation].requestBody.[statusCode] | functions.functions.[http||httpApi].documentation.methodResponses[statusCode] |
55
+ | path[path].[operation].requestBody.[statusCode].description | functions.functions.[http||httpApi].documentation.methodResponses[statusCode].responseBody.description |
56
+ | path[path].[operation].requestBody.[statusCode].content | functions.functions.[http||httpApi].documentation.methodResponses[statusCode].responseModels[contentType] Links to custom.documentation.models.name |
57
+
58
+
59
+ ### Configuration
60
+
61
+ To configure this plugin to generate valid OpenAPI documentation there are two places you'll need to modify in your `serverless.yml` file, the `custom` variables section and the `http` event section for each given function in your service.
62
+
63
+ The `custom` section of your `serverless.yml` can be configured as below:
64
+
65
+ ```yml
66
+ custom:
67
+ documentation:
68
+ version: '1'
69
+ title: 'My API'
70
+ description: 'This is my API'
71
+ models: {}
72
+ ```
73
+
74
+ These configurations can be quite verbose; you can separate it out into it's own file, such as `serverless.doc.yml` as below:
75
+
76
+ ```yml
77
+ custom:
78
+ documentation: ${file(serverless.doc.yml):documentation}
79
+
80
+ functions:
81
+ myFunc:
82
+ events:
83
+ - http:
84
+ path: getStuff
85
+ method: get
86
+ documentation: ${file(serverless.doc.yml):endpoints.myFunc}
87
+ ```
88
+
89
+ For more info on `serverless.yml` syntax, see their docs.
90
+
91
+ #### Models
92
+
93
+ Models contain additional information that you can use to define schemas for endpoints. You must define the *content type* for each schema that you provide in the models.
94
+
95
+ The *required* directives for the models section are as follow:
96
+
97
+ * `name`: the name of the schema
98
+ * `description`: a description of the schema
99
+ * `contentType`: the content type of the described request/response (ie. `application/json` or `application/xml`).
100
+ * `schema`: The JSON Schema ([website](http://json-schema.org/)) that describes the model. You can either use inline `YAML` to define these, or refer to an external schema file as below
101
+
102
+ ```yml
103
+ custom:
104
+ documentation:
105
+ models:
106
+ - name: "ErrorResponse"
107
+ description: "This is an error"
108
+ contentType: "application/json"
109
+ schema: ${file(models/ErrorResponse.json)}
110
+ - name: "PutDocumentResponse"
111
+ description: "PUT Document response model (external reference example)"
112
+ contentType: "application/json"
113
+ schema: ${file(models/PutDocumentResponse.json)}
114
+ - name: "PutDocumentRequest"
115
+ description: "PUT Document request model (inline example)"
116
+ contentType: "application/json"
117
+ schema:
118
+ $schema: "http://json-schema.org/draft-04/schema#"
119
+ properties:
120
+ SomeObject:
121
+ type: "object"
122
+ properties:
123
+ SomeAttribute:
124
+ type: "string"
125
+ ```
126
+
127
+ #### Functions
128
+
129
+ To define the documentation for a given function event, you need to create a `documentation` attribute for your http event in your `serverless.yml` file.
130
+
131
+ The `documentation` section of the event configuration can contain the following attributes:
132
+
133
+ * `summary`: a short description of the method
134
+ * `description`: a detailed description of the method
135
+ * `tags`: an array of tags for this event
136
+ * `deprecated`: boolean indicator that indicates clients should migrate away from this function
137
+ * `requestBody`: contains description of the request
138
+ * `description`: a description of the request body
139
+ * `requestModels`: a list of models to describe the request bodies (see [requestModels](#requestmodels) below)
140
+ * `queryParams`: a list of query parameters (see [queryParams](#queryparams) below)
141
+ * `pathParams`: a list of path parameters (see [pathParams](#pathparams) below)
142
+ * `cookieParams`: a list of cookie parameters (see [cookieParams](#cookieparams) below)
143
+ * `methodResponses`: an array of response models and applicable status codes
144
+ * `statusCode`: applicable http status code (ie. 200/404/500 etc.)
145
+ * `responseBody`: contains description of the response
146
+ * `description`: a description of the body response
147
+ * `responseHeaders`: a list of response headers (see [responseHeaders](#responseheaders) below)
148
+ * `responseModels`: a list of models to describe the request bodies (see [responseModels](#responsemodels) below) for each `Content-Type`
149
+
150
+ ```yml
151
+ functions:
152
+ createUser:
153
+ handler: "handler.create"
154
+ events:
155
+ - http:
156
+ path: "create"
157
+ method: "post"
158
+ documentation:
159
+ summary: "Create User"
160
+ description: "Creates a user and then sends a generated password email"
161
+ requestBody:
162
+ description: "A user information object"
163
+ requestModels:
164
+ application/json: "PutDocumentRequest"
165
+ pathParams:
166
+ - name: "username"
167
+ description: "The username for a user to create"
168
+ schema:
169
+ type: "string"
170
+ pattern: "^[-a-z0-9_]+$"
171
+ queryParams:
172
+ - name: "membershipType"
173
+ description: "The user's Membership Type"
174
+ schema:
175
+ type: "string"
176
+ enum:
177
+ - "premium"
178
+ - "standard"
179
+ cookieParams:
180
+ - name: "SessionId"
181
+ description: "A Session ID variable"
182
+ schema:
183
+ type: "string"
184
+ methodResponses:
185
+ - statusCode: 201
186
+ responseBody:
187
+ description: "A user object along with generated API Keys"
188
+ responseModels:
189
+ application/json: "PutDocumentResponse"
190
+ - statusCode: 500
191
+ responseBody:
192
+ description: "An error message when creating a new user"
193
+ responseModels:
194
+ application/json: "ErrorResponse"
195
+ ```
196
+
197
+ #### `queryParams`
198
+
199
+ Query parameters can be described as follow:
200
+
201
+ * `name`: the name of the query variable
202
+ * `description`: a description of the query variable
203
+ * `required`: whether the query parameter is mandatory (boolean)
204
+ * `schema`: JSON schema (inline or file)
205
+
206
+ ```yml
207
+ queryParams:
208
+ - name: "filter"
209
+ description: "The filter parameter"
210
+ required: true
211
+ schema:
212
+ type: "string"
213
+ ```
214
+
215
+ #### `pathParams`
216
+
217
+ Path parameters can be described as follow:
218
+
219
+ * `name`: the name of the query variable
220
+ * `description`: a description of the query variable
221
+ * `schema`: JSON schema (inline or file)
222
+
223
+ ```yml
224
+ pathParams:
225
+ - name: "usernameId"
226
+ description: "The usernameId parameter"
227
+ schema:
228
+ type: "string"
229
+ ```
230
+
231
+ #### `cookieParams`
232
+
233
+ Cookie parameters can be described as follow:
234
+
235
+ * `name`: the name of the query variable
236
+ * `description`: a description of the query variable
237
+ * `required`: whether the query parameter is mandatory (boolean)
238
+ * `schema`: JSON schema (inline or file)
239
+
240
+ ```yml
241
+ cookieParams:
242
+ - name: "sessionId"
243
+ description: "The sessionId parameter"
244
+ required: true
245
+ schema:
246
+ type: "string"
247
+ ```
248
+
249
+ #### `requestModels`
250
+
251
+ The `requestModels` property allows you to define models for the HTTP Request of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant request model named in the `models` section of your configuration (see [Defining Models](#models) section).
252
+
253
+ ```yml
254
+ requestModels:
255
+ application/json: "CreateRequest"
256
+ application/xml: "CreateRequestXML"
257
+ ```
258
+
259
+ #### `methodResponses`
260
+
261
+ You can define the response schemas by defining properties for your function event.
262
+
263
+ For an example of a `methodResponses` configuration for an event see below:
264
+
265
+ ```yml
266
+ methodResponse:
267
+ - statusCode: 200
268
+ responseHeaders:
269
+ - name: "Content-Type"
270
+ description: "Content Type header"
271
+ schema:
272
+ type: "string"
273
+ responseModels:
274
+ application/json: "CreateResponse"
275
+ application/xml: "CreateResponseXML"
276
+ ```
277
+
278
+ ##### `responseModels`
279
+
280
+ The `responseModels` property allows you to define models for the HTTP Response of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant response model named in the `models` section of your configuration (see [Defining Models](#models) section).
281
+
282
+ ```yml
283
+ responseModels:
284
+ application/json: "CreateResponse"
285
+ application/xml: "CreateResponseXML"
286
+ ```
287
+
288
+ ##### `responseHeaders` and `requestHeaders`
289
+
290
+ The `responseHeaders/requestHeaders` section of the configuration allows you to define the HTTP headers for the function event.
291
+
292
+ The attributes for a header are as follow:
293
+
294
+ * `name`: the name of the HTTP Header
295
+ * `description`: a description of the HTTP Header
296
+ * `schema`: JSON schema (inline or file)
297
+
298
+ ```yml
299
+ responseHeaders:
300
+ - name: "Content-Type"
301
+ description: "Content Type header"
302
+ schema:
303
+ type: "string"
304
+ requestHeaders:
305
+ - name: "Content-Type"
306
+ description: "Content Type header"
307
+ schema:
308
+ type: "string"
309
+ ```
310
+
311
+ ## Example configuration
312
+
313
+ Please view the example [serverless.yml](test/serverless 2/serverless.yml).
314
+
315
+ ## Install
316
+
317
+ This plugin works for Serverless 2.x and up.
318
+
319
+ To add this plugin to your package.json:
320
+
321
+ **Using npm:**
322
+ ```bash
323
+ npm install serverless-openapi-documenter --save-dev
324
+ ```
325
+
326
+ Next you need to add the plugin to the `plugins` section of your `serverless.yml` file.
327
+
328
+ ```yml
329
+ plugins:
330
+ - serverless-openapi-documenter
331
+ ```
332
+
333
+ > Note: Add this plugin _after_ `serverless-offline` to prevent issues with `String.replaceAll` being overridden incorrectly.
334
+
335
+ ## License
336
+
337
+ MIT
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const OpenAPIGenerator = require('./src/openAPIGenerator')
2
+
3
+ module.exports = OpenAPIGenerator
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "serverless-openapi-documenter",
3
+ "version": "0.0.1",
4
+ "description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC",
11
+ "devDependencies": {
12
+ "serverless": "^3.17.0"
13
+ },
14
+ "dependencies": {
15
+ "chalk": "^4.1.2",
16
+ "js-yaml": "^4.1.0",
17
+ "json-schema-for-openapi": "^0.1.0",
18
+ "oas-validator": "^5.0.8",
19
+ "openapi-to-postmanv2": "^3.2.0",
20
+ "swagger2openapi": "^7.0.8",
21
+ "uuid": "^8.3.2"
22
+ }
23
+ }