@trojs/openapi-server 2.0.6 → 2.1.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/openapi-server",
3
3
  "description": "OpenAPI Server",
4
- "version": "2.0.6",
4
+ "version": "2.1.1",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
@@ -58,7 +58,7 @@
58
58
  "url": "https://github.com/trojs/openapi-server"
59
59
  },
60
60
  "engines": {
61
- "node": ">= 18.13"
61
+ "node": ">= 18.13 < 19 || >= 20 < 21 || >= 22 < 23"
62
62
  },
63
63
  "keywords": [
64
64
  "openapi",
@@ -82,7 +82,7 @@
82
82
  },
83
83
  "overrides": {
84
84
  "semver": "^7.5.3",
85
- "send": "^0.19.0",
85
+ "send@<=0.19.0": "^0.19.0",
86
86
  "cookie@<=0.7.0": "0.7.0"
87
87
  }
88
88
  }
@@ -16,10 +16,11 @@ import { parseParams } from './params.js';
16
16
  * @param {boolean=} params.errorDetails
17
17
  * @param {Logger=} params.logger
18
18
  * @param {object=} params.meta
19
+ * @param {boolean=} params.mock
19
20
  * @returns {Function}
20
21
  */
21
22
  export const makeExpressCallback =
22
- ({ controller, specification, errorDetails, logger, meta }) =>
23
+ ({ controller, specification, errorDetails, logger, meta, mock }) =>
23
24
  /**
24
25
  * Handle controller
25
26
  * @async
@@ -37,6 +38,7 @@ export const makeExpressCallback =
37
38
  const parameters = parseParams({
38
39
  query: allParameters,
39
40
  spec: context.operation.parameters,
41
+ mock,
40
42
  });
41
43
  const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;
42
44
 
package/src/params.js CHANGED
@@ -5,9 +5,10 @@ import { types } from './types.js';
5
5
  * @param {object} params
6
6
  * @param {object} params.query
7
7
  * @param {object} params.spec
8
+ * @param {boolean=} params.mock
8
9
  * @returns {object}
9
10
  */
10
- export const parseParams = ({ query, spec }) =>
11
+ export const parseParams = ({ query, spec, mock = false }) =>
11
12
  spec
12
13
  .map((parameter) => {
13
14
  const { name, schema } = parameter;
@@ -19,8 +20,16 @@ export const parseParams = ({ query, spec }) =>
19
20
  const Type = types[type];
20
21
  const paramName = query?.[name];
21
22
 
23
+ if (!paramName && defaultValue !== undefined) {
24
+ return { name, value: defaultValue };
25
+ }
26
+
27
+ if (!paramName && mock && exampleValue !== undefined) {
28
+ return { name, value: exampleValue };
29
+ }
30
+
22
31
  if (!paramName) {
23
- return { name, value: defaultValue ?? exampleValue };
32
+ return undefined;
24
33
  }
25
34
 
26
35
  if (Type === Boolean) {
@@ -33,6 +42,7 @@ export const parseParams = ({ query, spec }) =>
33
42
  const value = new Type(paramName).valueOf();
34
43
  return { name, value };
35
44
  })
45
+ .filter(Boolean)
36
46
  .reduce((acc, { name, value }) => {
37
47
  acc[name] = value;
38
48
  return acc;
package/src/router.js CHANGED
@@ -25,6 +25,7 @@ import { unauthorized } from './handlers/unauthorized.js';
25
25
  * @param {object=} params.meta
26
26
  * @param {SecurityHandler[]=} params.securityHandlers
27
27
  * @param {AjvOpts=} params.ajvOptions
28
+ * @param {boolean=} params.mock
28
29
  * @returns {{ api: OpenAPIBackend<any>, openAPISpecification: object }}
29
30
  */
30
31
  export const setupRouter = ({
@@ -37,6 +38,7 @@ export const setupRouter = ({
37
38
  meta,
38
39
  securityHandlers = [],
39
40
  ajvOptions = {},
41
+ mock,
40
42
  }) => {
41
43
  const api = new OpenAPIBackend({
42
44
  definition: openAPISpecification,
@@ -69,16 +71,16 @@ export const setupRouter = ({
69
71
  errorDetails,
70
72
  logger,
71
73
  meta,
74
+ mock,
72
75
  })
73
76
  );
74
77
  }
75
78
  );
76
79
 
77
80
  api.register('notImplemented', (context) => {
78
- const { mock } = context.api.mockResponseForOperation(
79
- context.operation.operationId
80
- );
81
- return mock;
81
+ const { mock: mockImplementation } =
82
+ context.api.mockResponseForOperation(context.operation.operationId);
83
+ return mockImplementation;
82
84
  });
83
85
 
84
86
  securityHandlers.forEach((securityHandler) => {