Skip to content

Commit a857c78

Browse files
authored
Merge pull request #192 from ckb-cell/ref/migrate-to-openapi-3.1
refactor: migrate swagger to use [email protected]
2 parents 1887175 + 10d699e commit a857c78

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/plugins/swagger.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,36 @@ export const DOCS_ROUTE_PREFIX = '/docs';
1111
export default fp(async (fastify) => {
1212
fastify.register(swagger, {
1313
hideUntagged: true,
14-
swagger: {
14+
openapi: {
15+
openapi: '3.1.0',
1516
info: {
1617
title: 'Bitcoin/RGB++ Assets API',
1718
version: pkg.version,
1819
},
19-
consumes: ['application/json'],
20-
produces: ['application/json'],
2120
security: [{ apiKey: [] }],
22-
securityDefinitions: {
23-
apiKey: {
24-
type: 'apiKey',
25-
name: 'Authorization',
26-
in: 'header',
27-
description: 'JWT token for authentication. Example: Bearer <token>',
21+
components: {
22+
securitySchemes: {
23+
apiKey: {
24+
type: 'apiKey',
25+
name: 'Authorization',
26+
in: 'header',
27+
description: 'JWT token for authentication. Example: Bearer <token>',
28+
},
2829
},
2930
},
3031
},
3132
transform: jsonSchemaTransform,
32-
transformObject: ({ swaggerObject }) => {
33+
transformObject: ({ openapiObject }) => {
3334
if (env.NODE_ENV === 'production') {
34-
const { paths = {} } = swaggerObject;
35-
const newPaths = Object.entries(paths).reduce((acc, [path, methods]) => {
35+
const { paths = {} } = openapiObject;
36+
openapiObject.paths = Object.entries(paths).reduce((acc, [path, methods]) => {
3637
if (SWAGGER_PROD_IGNORE_URLS.some((ignorePath) => path.startsWith(ignorePath))) {
3738
return acc;
3839
}
3940
return { ...acc, [path]: methods };
4041
}, {});
41-
swaggerObject.paths = newPaths;
4242
}
43-
return swaggerObject;
43+
return openapiObject;
4444
},
4545
});
4646
fastify.register(swaggerUI, {

test/app.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('`/docs/json` - 200', async () => {
1212
const data = response.json();
1313

1414
expect(response.statusCode).toBe(200);
15-
expect(data.swagger).toBe('2.0');
15+
expect(data.openapi).toBe('3.1.0');
1616
expect(Object.keys(data.paths)).toStrictEqual([
1717
'/token/generate',
1818
'/bitcoin/v1/info',

0 commit comments

Comments
 (0)