@uphold/fastify-openapi-router-plugin 1.0.1 → 1.1.0
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 +8 -8
- package/src/parser/spec.js +9 -3
- package/src/parser/spec.test.js +20 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uphold/fastify-openapi-router-plugin",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A plugin for Fastify to connect routes with a OpenAPI 3.x specification",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -34,24 +34,24 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@eslint/js": "^9.6.0",
|
|
37
|
-
"@vitest/coverage-v8": "^1.
|
|
37
|
+
"@vitest/coverage-v8": "^4.1.1",
|
|
38
38
|
"eslint": "^9.6.0",
|
|
39
39
|
"eslint-config-prettier": "^9.1.0",
|
|
40
40
|
"eslint-plugin-prettier": "^5.2.1",
|
|
41
41
|
"eslint-plugin-sort-destructure-keys": "^2.0.0",
|
|
42
42
|
"eslint-plugin-sort-imports-requires": "^1.0.2",
|
|
43
43
|
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
44
|
-
"fastify": "^
|
|
44
|
+
"fastify": "^5.8.4",
|
|
45
45
|
"globals": "^15.7.0",
|
|
46
46
|
"prettier": "^3.3.3",
|
|
47
|
-
"release-it": "^
|
|
48
|
-
"vitest": "^1.
|
|
47
|
+
"release-it": "^19.2.4",
|
|
48
|
+
"vitest": "^4.1.0"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@readme/openapi-parser": "^
|
|
51
|
+
"@readme/openapi-parser": "^6.0.0",
|
|
52
52
|
"fastify-plugin": "^4.5.1",
|
|
53
|
-
"lodash-es": "^4.17.
|
|
53
|
+
"lodash-es": "^4.17.23",
|
|
54
54
|
"openapi-types": "^12.1.3",
|
|
55
|
-
"p-props": "^6.
|
|
55
|
+
"p-props": "^6.1.0"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/parser/spec.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { parse, validate } from '@readme/openapi-parser';
|
|
2
2
|
|
|
3
3
|
export const validateSpec = async options => {
|
|
4
|
-
const
|
|
4
|
+
const validation = await validate(options.spec);
|
|
5
|
+
|
|
6
|
+
if (!validation.valid) {
|
|
7
|
+
throw Object.assign(new TypeError('Supplied schema is not a valid OpenAPI definition.'), validation);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const spec = await parse(options.spec);
|
|
5
11
|
|
|
6
12
|
const version = spec.openapi ?? spec.swagger;
|
|
7
13
|
const majorVersion = Number(version?.split?.('.')[0]);
|
|
8
14
|
|
|
9
15
|
if (isNaN(majorVersion) || majorVersion < 3 || majorVersion >= 4) {
|
|
10
|
-
throw new TypeError(`Unsupported OpenAPI version: ${version ?? 'unknown'}
|
|
16
|
+
throw new TypeError(`Unsupported OpenAPI version: '${version ?? 'unknown'}'.`);
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
return spec;
|
package/src/parser/spec.test.js
CHANGED
|
@@ -3,9 +3,9 @@ import { validateSpec } from './spec.js';
|
|
|
3
3
|
|
|
4
4
|
describe('validateSpec()', () => {
|
|
5
5
|
it('should throw for invalid options', async () => {
|
|
6
|
-
await expect(validateSpec({})).rejects.
|
|
7
|
-
await expect(validateSpec({ spec: { foo: 'bar' } })).rejects.
|
|
8
|
-
'Supplied schema is not a valid OpenAPI definition
|
|
6
|
+
await expect(validateSpec({})).rejects.toThrow('Expected a file path, URL, or object.');
|
|
7
|
+
await expect(validateSpec({ spec: { foo: 'bar' } })).rejects.toThrow(
|
|
8
|
+
'Supplied schema is not a valid OpenAPI definition'
|
|
9
9
|
);
|
|
10
10
|
});
|
|
11
11
|
|
|
@@ -18,20 +18,8 @@ describe('validateSpec()', () => {
|
|
|
18
18
|
paths: {},
|
|
19
19
|
swagger: '2.0'
|
|
20
20
|
};
|
|
21
|
-
const specOpenApi2 = {
|
|
22
|
-
info: {},
|
|
23
|
-
openapi: '2.0.0',
|
|
24
|
-
paths: {}
|
|
25
|
-
};
|
|
26
|
-
const specOpenApi4 = {
|
|
27
|
-
info: {},
|
|
28
|
-
openapi: '4.0.0',
|
|
29
|
-
paths: {}
|
|
30
|
-
};
|
|
31
21
|
|
|
32
|
-
await expect(validateSpec({ spec: specSwagger2 })).rejects.
|
|
33
|
-
await expect(validateSpec({ spec: specOpenApi2 })).rejects.toThrowError(/Unsupported OpenAPI version: 2\.0\.0/);
|
|
34
|
-
await expect(validateSpec({ spec: specOpenApi4 })).rejects.toThrowError(/Unsupported OpenAPI version: 4\.0\.0/);
|
|
22
|
+
await expect(validateSpec({ spec: specSwagger2 })).rejects.toThrow(/Unsupported OpenAPI version: '2\.0'\./);
|
|
35
23
|
});
|
|
36
24
|
|
|
37
25
|
it('should return a parsed spec', async () => {
|
|
@@ -41,7 +29,17 @@ describe('validateSpec()', () => {
|
|
|
41
29
|
version: '1'
|
|
42
30
|
},
|
|
43
31
|
openapi: '3.1.0',
|
|
44
|
-
paths: {
|
|
32
|
+
paths: {
|
|
33
|
+
'/pet': {
|
|
34
|
+
get: {
|
|
35
|
+
responses: {
|
|
36
|
+
200: {
|
|
37
|
+
description: 'successful operation'
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
45
43
|
};
|
|
46
44
|
|
|
47
45
|
expect(await validateSpec({ spec })).toMatchObject(spec);
|
|
@@ -49,6 +47,11 @@ describe('validateSpec()', () => {
|
|
|
49
47
|
|
|
50
48
|
it("should resolve '$ref' in spec", async () => {
|
|
51
49
|
const Pet = {
|
|
50
|
+
properties: {
|
|
51
|
+
name: {
|
|
52
|
+
type: 'string'
|
|
53
|
+
}
|
|
54
|
+
},
|
|
52
55
|
type: 'object'
|
|
53
56
|
};
|
|
54
57
|
const spec = {
|