🎉 feat: user provided components

This commit is contained in:
saltyaom
2023-08-17 15:40:07 +07:00
4 changed files with 61 additions and 47 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -10,18 +10,31 @@ const app = new Elysia({
documentation: { documentation: {
info: { info: {
title: 'Elysia', title: 'Elysia',
version: '0.5.0' version: '0.6.10'
}, },
tags: [ tags: [
{ {
name: 'Test', name: 'Test',
description: 'Hello' description: 'Hello'
} }
] ],
components: {
schemas: {
User: {
description: 'string'
}
},
securitySchemes: {
JwtAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Enter JWT Bearer token **_only_**'
}
}
}
} }
}) })
) )
.use(plugin) .use(plugin)
.listen(8080) .listen(8080)
console.log(app.routes)

View File

@@ -1,6 +1,6 @@
{ {
"name": "@elysiajs/swagger", "name": "@elysiajs/swagger",
"version": "0.6.0", "version": "0.6.1",
"description": "Plugin for Elysia to auto-generate Swagger page", "description": "Plugin for Elysia to auto-generate Swagger page",
"author": { "author": {
"name": "saltyAom", "name": "saltyAom",
@@ -35,12 +35,13 @@
"release": "npm run build && npm run test && npm publish --access public" "release": "npm run build && npm run test && npm publish --access public"
}, },
"peerDependencies": { "peerDependencies": {
"elysia": ">= 0.6.0" "elysia": ">= 0.6.7"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"@types/node": "^20.1.4", "@types/node": "^20.1.4",
"bun-types": "^0.7.0", "bun-types": "^0.7.0",
"elysia": "0.6.8", "elysia": "^0.6.10",
"eslint": "^8.40.0", "eslint": "^8.40.0",
"rimraf": "4.3", "rimraf": "4.3",
"typescript": "^5.0.4" "typescript": "^5.0.4"

View File

@@ -74,50 +74,50 @@ export const swagger =
} }
} }
) )
}).route( }).route('GET', `${path}/json`, () => {
'GET', // eslint-disable-next-line @typescript-eslint/ban-ts-comment
`${path}/json`, // @ts-ignore
() => { const routes = app.routes as InternalRoute[]
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const routes = app.routes as InternalRoute[]
if (routes.length !== totalRoutes) { if (routes.length !== totalRoutes) {
totalRoutes = routes.length totalRoutes = routes.length
routes.forEach((route: InternalRoute<any>) => { routes.forEach((route: InternalRoute<any>) => {
registerSchemaPath({ registerSchemaPath({
schema, schema,
hook: route.hooks, hook: route.hooks,
method: route.method, method: route.method,
path: route.path, path: route.path,
models: app.meta.defs, models: app.meta.defs,
contentType: route.hooks.type contentType: route.hooks.type
})
}) })
} })
return {
openapi: '3.0.3',
...{
...documentation,
info: {
title: 'Elysia Documentation',
description: 'Developement documentation',
version: '0.0.0',
...documentation.info
}
},
paths: filterPaths(schema, {
excludeStaticFile,
exclude: Array.isArray(exclude) ? exclude : [exclude]
}),
components: {
schemas: app.meta.defs
}
} satisfies OpenAPIV3.Document
} }
)
return {
openapi: '3.0.3',
...{
...documentation,
info: {
title: 'Elysia Documentation',
description: 'Developement documentation',
version: '0.0.0',
...documentation.info
}
},
paths: filterPaths(schema, {
excludeStaticFile,
exclude: Array.isArray(exclude) ? exclude : [exclude]
}),
components: {
...documentation.components,
schemas: {
...app.meta.defs,
...documentation.components?.schemas
}
}
} satisfies OpenAPIV3.Document
})
// This is intentional to prevent deeply nested type // This is intentional to prevent deeply nested type
return app return app