mirror of
https://github.com/zoriya/elysia-swagger.git
synced 2026-06-01 05:17:31 +00:00
🎉 feat: exp.51 just right slow
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
# 0.0.0-experimental.2 - 22 Nov 2022
|
||||
Change:
|
||||
- Support for KingWorld 0.0.0-experimental.51
|
||||
|
||||
# 0.0.0-experimental.1 - 12 Nov 2022
|
||||
Improvement:
|
||||
- Auto infers path params if schema is presented
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# @kingworldjs/swagger
|
||||
A plugin for [kingworld](https://github.com/saltyaom/kingworld) that auto-generate Swagger page.
|
||||
A plugin for [kingworld](https://github.com/saltyaom/kingworld) to auto-generate Swagger page.
|
||||
|
||||
## Installation
|
||||
```bash
|
||||
@@ -8,8 +8,8 @@ bun add @kingworldjs/swagger
|
||||
|
||||
## Example
|
||||
```typescript
|
||||
import KingWorld from 'kingworld'
|
||||
import swagger from '@kingworldjs/swagger'
|
||||
import { KingWorld } from 'kingworld'
|
||||
import { swagger } from '@kingworldjs/swagger'
|
||||
|
||||
const app = new KingWorld()
|
||||
.use(swagger)
|
||||
@@ -67,4 +67,4 @@ Determine if Swagger should exclude static files.
|
||||
## exclude
|
||||
@default []
|
||||
|
||||
Paths to exclude from Swagger endpoint
|
||||
Paths to exclude from the Swagger endpoint
|
||||
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@kingworldjs/swagger",
|
||||
"version": "0.0.0-experimental.1",
|
||||
"description": "A plugin for kingworld that auto-generate Swagger page",
|
||||
"version": "0.0.0-experimental.2",
|
||||
"description": "A plugin for kingworld to auto-generate Swagger page",
|
||||
"author": {
|
||||
"name": "saltyAom",
|
||||
"url": "https://github.com/SaltyAom",
|
||||
@@ -32,7 +32,7 @@
|
||||
"release": "npm run build && npm run test && npm publish --access public"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"kingworld": ">= 0.0.0-experimental.41"
|
||||
"kingworld": ">= 0.0.0-experimental.51"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@swc/cli": "^0.1.57",
|
||||
@@ -41,13 +41,13 @@
|
||||
"@types/swagger-ui-dist": "^3.30.1",
|
||||
"bun-types": "^0.2.2",
|
||||
"eslint": "^8.26.0",
|
||||
"kingworld": "^0.0.0-experimental.41",
|
||||
"kingworld": "^0.0.0-experimental.51",
|
||||
"nodemon": "^2.0.20",
|
||||
"openapi-types": "^12.0.2",
|
||||
"typescript": "^4.8.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@kingworldjs/static": "0.0.0-experimental.1",
|
||||
"@kingworldjs/static": "0.0.0-experimental.2",
|
||||
"swagger-ui-dist": "^4.15.5"
|
||||
}
|
||||
}
|
||||
|
||||
+77
-74
@@ -81,54 +81,55 @@ const filterPaths = (
|
||||
*
|
||||
* @see https://github.com/saltyaom/kingworld-swagger
|
||||
*/
|
||||
export const swagger = (
|
||||
app: KingWorld,
|
||||
{
|
||||
swagger = {},
|
||||
excludeStaticFile = true,
|
||||
path = '/swagger',
|
||||
exclude = []
|
||||
}: {
|
||||
/**
|
||||
* Customize Swagger config, refers to Swagger 2.0 config
|
||||
*
|
||||
* @see https://swagger.io/specification/v2/
|
||||
*/
|
||||
swagger?: Partial<OpenAPIV2.Document>
|
||||
/**
|
||||
* Determine if Swagger should exclude static files.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
excludeStaticFile?: boolean
|
||||
/**
|
||||
* The endpoint to expose Swagger
|
||||
*
|
||||
* @default '/swagger'
|
||||
*/
|
||||
path?: string
|
||||
/**
|
||||
* Paths to exclude from Swagger endpoint
|
||||
*
|
||||
* @default []
|
||||
*/
|
||||
exclude?: string | RegExp | (string | RegExp)[]
|
||||
} = {
|
||||
swagger: {},
|
||||
excludeStaticFile: true,
|
||||
path: '/swagger',
|
||||
exclude: []
|
||||
}
|
||||
) =>
|
||||
app
|
||||
.get(path, (context) => {
|
||||
context.set.redirect = `${path}/static/index.html`
|
||||
})
|
||||
.get(
|
||||
`${path}/static/swagger-initializer.js`,
|
||||
({ store }) =>
|
||||
new Response(
|
||||
`window.onload = function() {
|
||||
export const swagger =
|
||||
(
|
||||
{
|
||||
swagger = {},
|
||||
excludeStaticFile = true,
|
||||
path = '/swagger',
|
||||
exclude = []
|
||||
}: {
|
||||
/**
|
||||
* Customize Swagger config, refers to Swagger 2.0 config
|
||||
*
|
||||
* @see https://swagger.io/specification/v2/
|
||||
*/
|
||||
swagger?: Partial<OpenAPIV2.Document>
|
||||
/**
|
||||
* Determine if Swagger should exclude static files.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
excludeStaticFile?: boolean
|
||||
/**
|
||||
* The endpoint to expose Swagger
|
||||
*
|
||||
* @default '/swagger'
|
||||
*/
|
||||
path?: string
|
||||
/**
|
||||
* Paths to exclude from Swagger endpoint
|
||||
*
|
||||
* @default []
|
||||
*/
|
||||
exclude?: string | RegExp | (string | RegExp)[]
|
||||
} = {
|
||||
swagger: {},
|
||||
excludeStaticFile: true,
|
||||
path: '/swagger',
|
||||
exclude: []
|
||||
}
|
||||
) =>
|
||||
(app: KingWorld) =>
|
||||
app
|
||||
.get(path, (context) => {
|
||||
context.set.redirect = `${path}/static/index.html`
|
||||
})
|
||||
.get(
|
||||
`${path}/static/swagger-initializer.js`,
|
||||
({ store }) =>
|
||||
new Response(
|
||||
`window.onload = function() {
|
||||
window.ui = SwaggerUIBundle({
|
||||
url: "${path}/json",
|
||||
dom_id: '#swagger-ui',
|
||||
@@ -143,33 +144,35 @@ export const swagger = (
|
||||
layout: "StandaloneLayout"
|
||||
});
|
||||
};`,
|
||||
{
|
||||
headers: {
|
||||
'content-type': 'text/javascript;charset=utf-8'
|
||||
{
|
||||
headers: {
|
||||
'content-type': 'text/javascript;charset=utf-8'
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
.get(`${path}/json`, (context) => ({
|
||||
...{
|
||||
...defaultConfig,
|
||||
...swagger,
|
||||
info: {
|
||||
title: 'KingWorld Documentation',
|
||||
description: 'Developement documentation',
|
||||
version: '0.0.0',
|
||||
...swagger.info
|
||||
}
|
||||
)
|
||||
)
|
||||
.get(`${path}/json`, (context) => ({
|
||||
...{
|
||||
...defaultConfig,
|
||||
...swagger,
|
||||
info: {
|
||||
title: 'KingWorld Documentation',
|
||||
description: 'Developement documentation',
|
||||
version: '0.0.0',
|
||||
...swagger.info
|
||||
}
|
||||
},
|
||||
// @ts-ignore
|
||||
paths: filterPaths(context.store[SCHEMA], {
|
||||
excludeStaticFile,
|
||||
exclude: Array.isArray(exclude) ? exclude : [exclude]
|
||||
})
|
||||
}))
|
||||
.use(staticPlugin, {
|
||||
prefix: `${path}/static`,
|
||||
path: getAbsoluteFSPath()
|
||||
})
|
||||
},
|
||||
// @ts-ignore
|
||||
paths: filterPaths(context.store[SCHEMA], {
|
||||
excludeStaticFile,
|
||||
exclude: Array.isArray(exclude) ? exclude : [exclude]
|
||||
})
|
||||
}))
|
||||
.use(
|
||||
staticPlugin({
|
||||
prefix: `${path}/static`,
|
||||
path: getAbsoluteFSPath()
|
||||
})
|
||||
)
|
||||
|
||||
export default swagger
|
||||
|
||||
+6
-4
@@ -8,16 +8,18 @@ const req = (path: string) => new Request(path)
|
||||
|
||||
describe('Swagger', () => {
|
||||
it('redirect to Swagger page', async () => {
|
||||
const app = new KingWorld().use(swagger)
|
||||
const app = new KingWorld().use(swagger())
|
||||
|
||||
const res = await app.handle(req('/swagger'))
|
||||
expect(res.status).toBe(302)
|
||||
})
|
||||
|
||||
it('use custom path', async () => {
|
||||
const app = new KingWorld().use(swagger, {
|
||||
path: '/v2/swagger'
|
||||
})
|
||||
const app = new KingWorld().use(
|
||||
swagger({
|
||||
path: '/v2/swagger'
|
||||
})
|
||||
)
|
||||
|
||||
const res = await app.handle(req('/v2/swagger'))
|
||||
expect(res.status).toBe(302)
|
||||
|
||||
Reference in New Issue
Block a user