serverless-openapi-documenter 0.0.112 → 0.0.113

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-openapi-documenter",
3
- "version": "0.0.112",
3
+ "version": "0.0.113",
4
4
  "description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -624,7 +624,7 @@ class DefinitionGenerator {
624
624
  continue;
625
625
  }
626
626
 
627
- const obj = JSON.parse(JSON.stringify(this.DEFAULT_CORS_HEADERS[key]));
627
+ const obj = structuredClone(this.DEFAULT_CORS_HEADERS[key]);
628
628
 
629
629
  if (key === "Access-Control-Allow-Origin") {
630
630
  if (
@@ -241,7 +241,7 @@ class OpenAPIGenerator {
241
241
  };
242
242
 
243
243
  PostmanGenerator.convert(
244
- { type: "json", data: JSON.parse(JSON.stringify(openAPI)) },
244
+ { type: "json", data: structuredClone(openAPI) },
245
245
  {},
246
246
  postmanGeneration
247
247
  );
@@ -71,26 +71,15 @@ class SchemaHandler {
71
71
  const modelName = model.name;
72
72
  const modelSchema = model.schema;
73
73
 
74
- this.logger.verbose(`dereferencing model: ${model.name}`);
75
- const dereferencedSchema = await this.__dereferenceSchema(
76
- modelSchema
74
+ const convertedSchemas = await this.__dereferenceAndConvert(
75
+ modelSchema,
76
+ modelName,
77
+ model
77
78
  ).catch((err) => {
78
- if (err.errors) {
79
- for (const error of err?.errors) {
80
- this.__HTTPError(error, model);
81
- }
82
- } else {
83
- this.__HTTPError(err, model);
84
- }
85
- return modelSchema;
79
+ if (err instanceof Error) throw err;
80
+ else return err;
86
81
  });
87
82
 
88
- this.logger.verbose(`convering model: ${model.name}`);
89
- const convertedSchemas = SchemaConvertor.convert(
90
- dereferencedSchema,
91
- modelName
92
- );
93
-
94
83
  if (
95
84
  typeof convertedSchemas.schemas === "object" &&
96
85
  !Array.isArray(convertedSchemas.schemas) &&
@@ -129,13 +118,12 @@ class SchemaHandler {
129
118
  return this.modelReferences[name];
130
119
  }
131
120
 
132
- const dereferencedSchema = await this.__dereferenceSchema(schema).catch(
133
- (err) => {
134
- throw err;
135
- }
136
- );
137
-
138
- const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, name);
121
+ const convertedSchemas = await this.__dereferenceAndConvert(schema, name, {
122
+ name,
123
+ schema,
124
+ }).catch((err) => {
125
+ throw err;
126
+ });
139
127
 
140
128
  for (const [schemaName, schemaValue] of Object.entries(
141
129
  convertedSchemas.schemas
@@ -157,6 +145,32 @@ class SchemaHandler {
157
145
  return `#/components/schemas/${finalName}`;
158
146
  }
159
147
 
148
+ async __dereferenceAndConvert(schema, name, model) {
149
+ this.logger.verbose(`dereferencing model: ${name}`);
150
+ const dereferencedSchema = await this.__dereferenceSchema(schema).catch(
151
+ (err) => {
152
+ this.__checkForHTTPErrorsAndThrow(err, model);
153
+
154
+ this.__checkForMissingPathAndThrow(err);
155
+
156
+ return schema;
157
+ }
158
+ );
159
+
160
+ this.logger.verbose(
161
+ `dereferenced model: ${JSON.stringify(dereferencedSchema)}`
162
+ );
163
+
164
+ this.logger.verbose(`converting model: ${name}`);
165
+ const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, name);
166
+
167
+ this.logger.verbose(
168
+ `converted schemas: ${JSON.stringify(convertedSchemas)}`
169
+ );
170
+
171
+ return convertedSchemas;
172
+ }
173
+
160
174
  async __dereferenceSchema(schema) {
161
175
  const bundledSchema = await $RefParser
162
176
  .bundle(schema, this.refParserOptions)
@@ -238,9 +252,23 @@ class SchemaHandler {
238
252
  }
239
253
  }
240
254
 
255
+ __checkForMissingPathAndThrow(error) {
256
+ if (error.message === "Expected a file path, URL, or object. Got undefined")
257
+ throw error;
258
+ }
259
+
260
+ __checkForHTTPErrorsAndThrow(error, model) {
261
+ if (error.errors) {
262
+ for (const err of error?.errors) {
263
+ this.__HTTPError(err, model);
264
+ }
265
+ } else {
266
+ this.__HTTPError(error, model);
267
+ }
268
+ }
269
+
241
270
  __HTTPError(error, model) {
242
271
  if (error.message.includes("HTTP ERROR")) {
243
- // throw err;
244
272
  throw new Error(
245
273
  `There was an error dereferencing ${
246
274
  model.name
@@ -23,7 +23,7 @@ describe("DefinitionGenerator", () => {
23
23
  );
24
24
 
25
25
  beforeEach(function () {
26
- mockServerless = JSON.parse(JSON.stringify(serverlessMock));
26
+ mockServerless = structuredClone(serverlessMock);
27
27
  Object.assign(mockServerless.service.custom.documentation, modelsDocument);
28
28
  });
29
29
 
@@ -39,9 +39,7 @@ describe("DefinitionGenerator", () => {
39
39
  });
40
40
 
41
41
  it("should default to version 3.0.0 of openAPI when openAPI version is not passed in", function () {
42
- const serverlessWithoutOpenAPIVersion = JSON.parse(
43
- JSON.stringify(mockServerless)
44
- );
42
+ const serverlessWithoutOpenAPIVersion = structuredClone(mockServerless);
45
43
  delete serverlessWithoutOpenAPIVersion.processedInput;
46
44
  let expected = new DefinitionGenerator(
47
45
  serverlessWithoutOpenAPIVersion,
@@ -108,9 +106,7 @@ describe("DefinitionGenerator", () => {
108
106
  });
109
107
 
110
108
  it("should respect the version of openAPI when passed in", function () {
111
- const serverlessWithOpenAPIVersion = JSON.parse(
112
- JSON.stringify(mockServerless)
113
- );
109
+ const serverlessWithOpenAPIVersion = structuredClone(mockServerless);
114
110
  serverlessWithOpenAPIVersion.processedInput.options.openApiVersion =
115
111
  "3.0.2";
116
112
  let expected = new DefinitionGenerator(
@@ -107,9 +107,7 @@ describe("OpenAPIGenerator", () => {
107
107
  const getAllFuncsStub = sinon
108
108
  .stub(sls.service, "getAllFunctions")
109
109
  .returns(["createUser"]);
110
- const basicInvalidFunction = JSON.parse(
111
- JSON.stringify(basicValidFunction)
112
- );
110
+ const basicInvalidFunction = structuredClone(basicValidFunction);
113
111
 
114
112
  delete basicInvalidFunction.createUser.events[0].http.documentation
115
113
  .methodResponses[0].responseModels;
@@ -145,9 +143,7 @@ describe("OpenAPIGenerator", () => {
145
143
  const getAllFuncsStub = sinon
146
144
  .stub(sls.service, "getAllFunctions")
147
145
  .returns(["createUser"]);
148
- const basicInvalidFunction = JSON.parse(
149
- JSON.stringify(basicValidFunction)
150
- );
146
+ const basicInvalidFunction = structuredClone(basicValidFunction);
151
147
 
152
148
  const getFuncStub = sinon
153
149
  .stub(sls.service, "getFunction")
@@ -182,9 +178,7 @@ describe("OpenAPIGenerator", () => {
182
178
  .stub(sls.service, "getAllFunctions")
183
179
  .returns(["createUser"]);
184
180
 
185
- const basicInvalidFunction = JSON.parse(
186
- JSON.stringify(basicValidFunction)
187
- );
181
+ const basicInvalidFunction = structuredClone(basicValidFunction);
188
182
 
189
183
  delete basicInvalidFunction.createUser.events[0].http.documentation
190
184
  .pathParams;
@@ -56,7 +56,7 @@ describe(`owasp`, function () {
56
56
  });
57
57
 
58
58
  it(`adds any properties contained in a new release`, async function () {
59
- const newOWASPJSONAdded = JSON.parse(JSON.stringify(newOWASPJSON));
59
+ const newOWASPJSONAdded = structuredClone(newOWASPJSON);
60
60
  newOWASPJSONAdded.headers.push({ name: "x-added", value: "true" });
61
61
 
62
62
  nock("https://owasp.org")
@@ -43,12 +43,12 @@ describe(`SchemaHandler`, function () {
43
43
  };
44
44
 
45
45
  beforeEach(function () {
46
- mockServerless = JSON.parse(JSON.stringify(serverlessMock));
47
- modelsDocument = JSON.parse(JSON.stringify(modelsDocumentOG));
48
- modelsAltDocument = JSON.parse(JSON.stringify(modelsAltDocumentOG));
49
- modelsListDocument = JSON.parse(JSON.stringify(modelsListDocumentOG));
50
- modelsListAltDocument = JSON.parse(JSON.stringify(modelsListAltDocumentOG));
51
- openAPI = JSON.parse(JSON.stringify(openAPISchema));
46
+ mockServerless = structuredClone(serverlessMock);
47
+ modelsDocument = structuredClone(modelsDocumentOG);
48
+ modelsAltDocument = structuredClone(modelsAltDocumentOG);
49
+ modelsListDocument = structuredClone(modelsListDocumentOG);
50
+ modelsListAltDocument = structuredClone(modelsListAltDocumentOG);
51
+ openAPI = structuredClone(openAPISchema);
52
52
  });
53
53
 
54
54
  describe(`constuctor`, function () {
@@ -135,7 +135,7 @@ describe(`SchemaHandler`, function () {
135
135
  });
136
136
 
137
137
  it(`should standardise mixed models syntax in to the correct format`, function () {
138
- const newModelsDocument = JSON.parse(JSON.stringify(modelsDocument));
138
+ const newModelsDocument = structuredClone(modelsDocument);
139
139
  Object.assign(
140
140
  mockServerless.service.custom.documentation,
141
141
  newModelsDocument
@@ -168,9 +168,7 @@ describe(`SchemaHandler`, function () {
168
168
  });
169
169
 
170
170
  it(`should standardise mixed modelsList syntax in to the correct format`, function () {
171
- const newModelsDocument = JSON.parse(
172
- JSON.stringify(modelsListDocument)
173
- );
171
+ const newModelsDocument = structuredClone(modelsListDocument);
174
172
  Object.assign(
175
173
  mockServerless.service.custom.documentation,
176
174
  newModelsDocument
@@ -203,9 +201,7 @@ describe(`SchemaHandler`, function () {
203
201
  });
204
202
 
205
203
  it(`should standardise mixed models and modelsList syntax in to the correct format`, function () {
206
- const newModelsDocument = JSON.parse(
207
- JSON.stringify(modelsListDocument)
208
- );
204
+ const newModelsDocument = structuredClone(modelsListDocument);
209
205
  Object.assign(
210
206
  mockServerless.service.custom.documentation,
211
207
  newModelsDocument