Skip to content

Commit

Permalink
Merge pull request #192 from ckb-cell/ref/migrate-to-openapi-3.1
Browse files Browse the repository at this point in the history
refactor: migrate swagger to use [email protected]
  • Loading branch information
ahonn authored Jul 12, 2024
2 parents 1887175 + 10d699e commit a857c78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/plugins/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ export const DOCS_ROUTE_PREFIX = '/docs';
export default fp(async (fastify) => {
fastify.register(swagger, {
hideUntagged: true,
swagger: {
openapi: {
openapi: '3.1.0',
info: {
title: 'Bitcoin/RGB++ Assets API',
version: pkg.version,
},
consumes: ['application/json'],
produces: ['application/json'],
security: [{ apiKey: [] }],
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
description: 'JWT token for authentication. Example: Bearer <token>',
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
description: 'JWT token for authentication. Example: Bearer <token>',
},
},
},
},
transform: jsonSchemaTransform,
transformObject: ({ swaggerObject }) => {
transformObject: ({ openapiObject }) => {
if (env.NODE_ENV === 'production') {
const { paths = {} } = swaggerObject;
const newPaths = Object.entries(paths).reduce((acc, [path, methods]) => {
const { paths = {} } = openapiObject;
openapiObject.paths = Object.entries(paths).reduce((acc, [path, methods]) => {
if (SWAGGER_PROD_IGNORE_URLS.some((ignorePath) => path.startsWith(ignorePath))) {
return acc;
}
return { ...acc, [path]: methods };
}, {});
swaggerObject.paths = newPaths;
}
return swaggerObject;
return openapiObject;
},
});
fastify.register(swaggerUI, {
Expand Down
2 changes: 1 addition & 1 deletion test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('`/docs/json` - 200', async () => {
const data = response.json();

expect(response.statusCode).toBe(200);
expect(data.swagger).toBe('2.0');
expect(data.openapi).toBe('3.1.0');
expect(Object.keys(data.paths)).toStrictEqual([
'/token/generate',
'/bitcoin/v1/info',
Expand Down

0 comments on commit a857c78

Please sign in to comment.