diff --git a/CHANGELOG.md b/CHANGELOG.md index a88774d..1de1ad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e4add1c..c276db9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bun.lockb b/bun.lockb index 0e255c5..0d9bdea 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5dbc5b5..9224c26 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.ts b/src/index.ts index e14fac0..5084d7a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 - /** - * 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 + /** + * 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 diff --git a/test/index.test.ts b/test/index.test.ts index ca10729..dc707be 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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)