serverless-openapi-documenter 0.0.30 → 0.0.32

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.
package/README.md CHANGED
@@ -59,6 +59,13 @@ Options:
59
59
  | info.title | custom.documentation.title OR service |
60
60
  | info.description | custom.documentation.description OR blank string |
61
61
  | info.version | custom.documentation.version OR random v4 uuid if not provided |
62
+ | info.termsOfService | custom.documentation.termsOfService |
63
+ | info.contact | custom.documentation.contact |
64
+ | info.contact.name | custom.documentation.contact.name OR blank string |
65
+ | info.contact.url | custom.documentation.contact.url if provided |
66
+ | info.license | custom.documentation.license |
67
+ | info.license.name | custom.documentation.license.name OR blank string |
68
+ | info.license.url | custom.documentation.license.url if provided |
62
69
  | externalDocs.description | custom.documentation.externalDocumentation.description |
63
70
  | externalDocs.url | custom.documentation.externalDocumentation.url |
64
71
  | servers[].description | custom.documentation.servers.description |
@@ -118,6 +125,7 @@ custom:
118
125
  version: '1'
119
126
  title: 'My API'
120
127
  description: 'This is my API'
128
+ termsOfService: https://google.com
121
129
  externalDocumentation:
122
130
  url: https://google.com
123
131
  description: A link to google
@@ -126,7 +134,7 @@ custom:
126
134
  description: The server
127
135
  variables:
128
136
  port:
129
- enum:
137
+ enum:
130
138
  - 4000
131
139
  - 3000
132
140
  default: 3000
@@ -142,6 +150,58 @@ custom:
142
150
 
143
151
  Mostly everything here is optional. A version from a UUID will be generated for you if you don't specify one, title will be the name of your service if you don't specify one.
144
152
 
153
+ #### termsOfService
154
+
155
+ Must be in the format of a url if included.
156
+
157
+ #### Contact
158
+
159
+ You can provide an optional contact object such as:
160
+
161
+ ```yml
162
+ custom:
163
+ documentation:
164
+ contact:
165
+ name: John
166
+ url: https://example.com
167
+ email: John@example.com
168
+ ```
169
+
170
+ These fields are optional, though `url` and `email` need to be in the format of an email address (ed: what that might be, i'm not 100% sure... go read the email RFC(s)) and a url.
171
+
172
+ #### License
173
+
174
+ You can provide an optional license object such as:
175
+
176
+ ```yml
177
+ custom:
178
+ documentation:
179
+ license:
180
+ name: Apache 2.0
181
+ url: https://www.apache.org/licenses/LICENSE-2.0.html
182
+ ```
183
+
184
+ Name is required but `url` is optional and must be in the format of a url.
185
+ #### Extended Fields
186
+
187
+ You can also add extended fields to the documentation object:
188
+
189
+ ```yml
190
+ custom:
191
+ documentation:
192
+ x-other-field: This is an extended field
193
+ ```
194
+
195
+ These fields must have `x-` before them, otherwise they will be ignored:
196
+
197
+ ```yml
198
+ custom:
199
+ documentation:
200
+ other-field: This is an extended field
201
+ ```
202
+
203
+ `other-field` here will not make it to the generated OpenAPI schema.
204
+
145
205
  These configurations can be quite verbose; you can separate it out into it's own file, such as `serverless.doc.yml` as below:
146
206
 
147
207
  ```yml
@@ -246,16 +306,16 @@ custom:
246
306
  schema: &ErrorItem
247
307
  type: object
248
308
  properties:
249
- message:
309
+ message:
250
310
  type: string
251
311
  code:
252
312
  type: integer
253
-
313
+
254
314
  - name: "PutDocumentResponse"
255
315
  description: "PUT Document response model (external reference example)"
256
316
  content:
257
317
  application/json:
258
- schema:
318
+ schema:
259
319
  type: array
260
320
  items: *ErrorItem
261
321
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-openapi-documenter",
3
- "version": "0.0.30",
3
+ "version": "0.0.32",
4
4
  "description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -31,7 +31,7 @@
31
31
  "@apidevtools/json-schema-ref-parser": "^9.1.0",
32
32
  "chalk": "^4.1.2",
33
33
  "js-yaml": "^4.1.0",
34
- "json-schema-for-openapi": "^0.3.0",
34
+ "json-schema-for-openapi": "^0.3.1",
35
35
  "oas-validator": "^5.0.8",
36
36
  "openapi-to-postmanv2": "^4.4.1",
37
37
  "swagger2openapi": "^7.0.8",
@@ -68,6 +68,37 @@ class DefinitionGenerator {
68
68
  description: documentation?.description || '',
69
69
  version: documentation?.version || uuid(),
70
70
  }
71
+
72
+ if (documentation.termsOfService)
73
+ info.termsOfService = documentation.termsOfService
74
+
75
+ if (documentation.contact) {
76
+ const contactObj = {}
77
+ contactObj.name = documentation.contact.name || ''
78
+
79
+ if (documentation.contact.url)
80
+ contactObj.url = documentation.contact.url
81
+
82
+ contactObj.email = documentation.contact.email || ''
83
+ Object.assign(info, {contact: contactObj})
84
+ }
85
+
86
+ if (documentation.license && documentation.license.name) {
87
+ const licenseObj = {}
88
+ licenseObj.name = documentation.license.name || ''
89
+
90
+ if (documentation.license.url)
91
+ licenseObj.url = documentation.license.url || ''
92
+
93
+ Object.assign(info, {license: licenseObj})
94
+ }
95
+
96
+ for (const key of Object.keys(documentation)) {
97
+ if (/^[x\-]/i.test(key)) {
98
+ Object.assign(info, {[key]: documentation[key]})
99
+ }
100
+ }
101
+
71
102
  Object.assign(this.openAPI, {info})
72
103
  }
73
104
 
@@ -174,6 +174,101 @@ describe('DefinitionGenerator', () => {
174
174
  expect(definitionGenerator.openAPI.info).to.be.an('object')
175
175
  expect(v4.test(definitionGenerator.openAPI.info.version)).to.be.true
176
176
  });
177
+
178
+ it('should assign a contact Object when a contact object is included', function() {
179
+ mockServerless.service.custom.documentation.contact = {
180
+ name: 'John',
181
+ url: 'http://example.com',
182
+ email: 'john@example.com'
183
+ }
184
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
185
+ definitionGenerator.createInfo()
186
+
187
+ expect(definitionGenerator.openAPI).to.be.an('object')
188
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
189
+ expect(definitionGenerator.openAPI.info).to.have.property('contact')
190
+ expect(definitionGenerator.openAPI.info.contact).to.be.an('object')
191
+ expect(definitionGenerator.openAPI.info.contact.name).to.be.an('string')
192
+ });
193
+
194
+ it('should only assign a contact url if one is provided', function() {
195
+ mockServerless.service.custom.documentation.contact = {
196
+ name: 'John',
197
+ email: 'john@example.com'
198
+ }
199
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
200
+ definitionGenerator.createInfo()
201
+
202
+ expect(definitionGenerator.openAPI).to.be.an('object')
203
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
204
+ expect(definitionGenerator.openAPI.info).to.have.property('contact')
205
+ expect(definitionGenerator.openAPI.info.contact).to.be.an('object')
206
+ expect(definitionGenerator.openAPI.info.contact.name).to.be.an('string')
207
+ expect(definitionGenerator.openAPI.info.contact).to.not.have.property('url')
208
+ });
209
+
210
+ it('should assign a license Object when a license object is included with a name', function() {
211
+ mockServerless.service.custom.documentation.license = {
212
+ name: 'Apache 2.0',
213
+ url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
214
+ }
215
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
216
+ definitionGenerator.createInfo()
217
+
218
+ expect(definitionGenerator.openAPI).to.be.an('object')
219
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
220
+ expect(definitionGenerator.openAPI.info).to.have.property('license')
221
+ expect(definitionGenerator.openAPI.info.license).to.be.an('object')
222
+ expect(definitionGenerator.openAPI.info.license.name).to.be.an('string')
223
+ });
224
+
225
+ it('should not assign a license Object when a license object is included without a name', function() {
226
+ mockServerless.service.custom.documentation.license = {
227
+ url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
228
+ }
229
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
230
+ definitionGenerator.createInfo()
231
+
232
+ expect(definitionGenerator.openAPI).to.be.an('object')
233
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
234
+ expect(definitionGenerator.openAPI.info).to.not.have.property('license')
235
+ });
236
+
237
+ it('should only assign a contact url if one is provided', function() {
238
+ mockServerless.service.custom.documentation.license = {
239
+ name: 'John',
240
+ }
241
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
242
+ definitionGenerator.createInfo()
243
+
244
+ expect(definitionGenerator.openAPI).to.be.an('object')
245
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
246
+ expect(definitionGenerator.openAPI.info).to.have.property('license')
247
+ expect(definitionGenerator.openAPI.info.license).to.be.an('object')
248
+ expect(definitionGenerator.openAPI.info.license.name).to.be.an('string')
249
+ expect(definitionGenerator.openAPI.info.license).to.not.have.property('url')
250
+ });
251
+
252
+ it('should assign specification extension fields when included', function() {
253
+ mockServerless.service.custom.documentation['x-field'] = 'john'
254
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
255
+ definitionGenerator.createInfo()
256
+
257
+ expect(definitionGenerator.openAPI).to.be.an('object')
258
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
259
+ expect(definitionGenerator.openAPI.info).to.have.property('x-field')
260
+ expect(definitionGenerator.openAPI.info['x-field']).to.be.equal('john')
261
+ });
262
+
263
+ it('should ignore fields that do not conform to specifiction extension', function() {
264
+ mockServerless.service.custom.documentation.otherField = 'john'
265
+ const definitionGenerator = new DefinitionGenerator(mockServerless)
266
+ definitionGenerator.createInfo()
267
+
268
+ expect(definitionGenerator.openAPI).to.be.an('object')
269
+ expect(definitionGenerator.openAPI.info).to.be.an('object')
270
+ expect(definitionGenerator.openAPI.info).to.not.have.property('otherField')
271
+ });
177
272
  });
178
273
 
179
274
  describe('createTags', () => {