diff --git a/CHANGELOG.md b/CHANGELOG.md index be1fdd7..409b59e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# 0.7.1 - 20 Sep 2023 +Bug fix: +- Add openapi-types as dependencies +- Fix `any` returned type + +# 0.7.0 - 20 Sep 2023 +- Add support for Elysia 0. + +# 0.7.0-beta.0 - 18 Sep 2023 +- Add support for Elysia 0.7 + # 0.6.2 - 11 Sep 2023 - Ship lodash.cloneDeep type diff --git a/bun.lockb b/bun.lockb index 823a438..16aea81 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/example/index.ts b/example/index.ts index 34640bc..843cec4 100644 --- a/example/index.ts +++ b/example/index.ts @@ -10,7 +10,7 @@ const app = new Elysia({ documentation: { info: { title: 'Elysia', - version: '0.6.10' + version: '0.7.0' }, tags: [ { diff --git a/example/plugin.ts b/example/plugin.ts index 32dd626..851abba 100644 --- a/example/plugin.ts +++ b/example/plugin.ts @@ -1,106 +1,103 @@ import { Elysia, t } from 'elysia' -export const plugin = (app: Elysia) => - app.group( - '/a', - (app) => - app - .model({ - sign: t.Object( - { - username: t.String(), - password: t.String() - }, - { - description: 'Models for handling authentication' - } - ), - number: t.Number() - }) - .get('/', ({ set }) => 'hi', { - detail: { - summary: 'Ping Pong', - description: 'Lorem Ipsum Dolar', - tags: ['Test'] - } - }) - .get('/unpath/:id', ({ params: { id } }) => id, { - params: t.Object({ - id: t.String({ - description: 'Extract value from path parameter' - }) - }), - detail: { - deprecated: true - } - }) - .post('/json', ({ body }) => body, { - type: 'json', - body: 'sign', - response: { - 200: 'sign' - }, - detail: { - summary: 'Using reference model' - } - }) - // .post( - // '/json/:id', - // ({ body, params: { id }, query: { name } }) => ({ - // ...body, - // id - // }), - // { - // transform({ params }) { - // params.id = +params.id - // }, - // schema: { - // body: 'sign', - // params: t.Object({ - // id: t.Number() - // }), - // response: { - // 200: t.Object( - // { - // id: t.Number(), - // username: t.String(), - // password: t.String() - // }, - // { - // title: 'User', - // description: - // "Contains user's confidential metadata" - // } - // ), - // 400: t.Object({ - // error: t.String() - // }) - // }, - // detail: { - // summary: 'Transform path parameter' - // } - // } - // } - // ) - .post('/file', ({ body: { file } }) => file, { - body: t.Object({ - file: t.File({ - type: ['image/jpeg', 'image/'], - minSize: '1k', - maxSize: '5m' - }) - }), - response: t.File() - }) - // .post('/files', ({ body: { files } }) => files[0], { - // schema: { - // body: t.Object({ - // files: t.Files({ - // type: 'image', - // maxSize: '5m' - // }) - // }), - // response: t.File() - // } - // }) - ) +export const plugin = new Elysia({ + prefix: '/a' +}) + .model({ + sign: t.Object( + { + username: t.String(), + password: t.String() + }, + { + description: 'Models for handling authentication' + } + ), + number: t.Number() + }) + .get('/', ({ set }) => 'hi', { + detail: { + summary: 'Ping Pong', + description: 'Lorem Ipsum Dolar', + tags: ['Test'] + } + }) + .get('/unpath/:id', ({ params: { id } }) => id, { + params: t.Object({ + id: t.String({ + description: 'Extract value from path parameter' + }) + }), + detail: { + deprecated: true + } + }) + .post('/json', ({ body }) => body, { + type: 'json', + body: 'sign', + response: { + 200: 'sign' + }, + detail: { + summary: 'Using reference model' + } + }) + // .post( + // '/json/:id', + // ({ body, params: { id }, query: { name } }) => ({ + // ...body, + // id + // }), + // { + // transform({ params }) { + // params.id = +params.id + // }, + // schema: { + // body: 'sign', + // params: t.Object({ + // id: t.Number() + // }), + // response: { + // 200: t.Object( + // { + // id: t.Number(), + // username: t.String(), + // password: t.String() + // }, + // { + // title: 'User', + // description: + // "Contains user's confidential metadata" + // } + // ), + // 400: t.Object({ + // error: t.String() + // }) + // }, + // detail: { + // summary: 'Transform path parameter' + // } + // } + // } + // ) + .post('/file', ({ body: { file } }) => file, { + body: t.Object({ + file: t.File({ + type: ['image/jpeg', 'image/'], + minSize: '1k', + maxSize: '5m' + }) + }), + response: t.File() + }) +// .post('/files', ({ body: { files } }) => files[0], { +// schema: { +// body: t.Object({ +// files: t.Files({ +// type: 'image', +// maxSize: '5m' +// }) +// }), +// response: t.File() +// } +// }) diff --git a/package.json b/package.json index c257a26..412f001 100644 --- a/package.json +++ b/package.json @@ -1,52 +1,53 @@ { - "name": "@elysiajs/swagger", - "version": "0.6.2", - "description": "Plugin for Elysia to auto-generate Swagger page", - "author": { - "name": "saltyAom", - "url": "https://github.com/SaltyAom", - "email": "saltyaom@gmail.com" - }, - "main": "./dist/index.js", - "exports": { - "bun": "./dist/index.js", - "node": "./dist/cjs/index.js", - "require": "./dist/cjs/index.js", - "import": "./dist/index.js", - "default": "./dist/index.js" - }, - "types": "./src/index.ts", - "keywords": [ - "elysia", - "swagger" - ], - "homepage": "https://github.com/elysiajs/elysia-swagger", - "repository": { - "type": "git", - "url": "https://github.com/elysiajs/elysia-swagger" - }, - "bugs": "https://github.com/elysiajs/elysia-swagger/issues", - "license": "MIT", - "scripts": { - "dev": "bun run --watch example/index.ts", - "test": "bun test && npm run test:node", - "test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js", - "build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json", - "release": "npm run build && npm run test && npm publish --access public" - }, - "peerDependencies": { - "elysia": ">= 0.6.7" - }, - "devDependencies": { - "@types/node": "^20.1.4", - "bun-types": "^0.7.0", - "elysia": "^0.6.20", - "eslint": "^8.40.0", - "rimraf": "4.3", - "typescript": "^5.0.4" - }, - "dependencies": { - "@types/lodash.clonedeep": "^4.5.7", - "lodash.clonedeep": "^4.5.0" - } + "name": "@elysiajs/swagger", + "version": "0.7.1", + "description": "Plugin for Elysia to auto-generate Swagger page", + "author": { + "name": "saltyAom", + "url": "https://github.com/SaltyAom", + "email": "saltyaom@gmail.com" + }, + "main": "./dist/index.js", + "exports": { + "bun": "./dist/index.js", + "node": "./dist/cjs/index.js", + "require": "./dist/cjs/index.js", + "import": "./dist/index.js", + "default": "./dist/index.js" + }, + "types": "./src/index.ts", + "keywords": [ + "elysia", + "swagger" + ], + "homepage": "https://github.com/elysiajs/elysia-swagger", + "repository": { + "type": "git", + "url": "https://github.com/elysiajs/elysia-swagger" + }, + "bugs": "https://github.com/elysiajs/elysia-swagger/issues", + "license": "MIT", + "scripts": { + "dev": "bun run --watch example/index.ts", + "test": "bun test && npm run test:node", + "test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js", + "build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json", + "release": "npm run build && npm run test && npm publish --access public" + }, + "peerDependencies": { + "elysia": ">= 0.7.0" + }, + "devDependencies": { + "@types/node": "^20.1.4", + "bun-types": "^0.7.0", + "elysia": "0.7.0", + "eslint": "^8.40.0", + "rimraf": "4.3", + "typescript": "^5.0.4" + }, + "dependencies": { + "@types/lodash.clonedeep": "^4.5.7", + "lodash.clonedeep": "^4.5.0", + "openapi-types": "^12.1.3" + } } diff --git a/src/index.ts b/src/index.ts index 37615aa..69afb08 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,7 +74,7 @@ export const swagger = } } ) - }).route('GET', `${path}/json`, () => { + }).get(`${path}/json`, () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const routes = app.routes as InternalRoute[] @@ -82,13 +82,14 @@ export const swagger = if (routes.length !== totalRoutes) { totalRoutes = routes.length - routes.forEach((route: InternalRoute) => { + routes.forEach((route: InternalRoute) => { registerSchemaPath({ schema, hook: route.hooks, method: route.method, path: route.path, - models: app.meta.defs, + // @ts-ignore + models: app.definitions, contentType: route.hooks.type }) }) @@ -112,7 +113,8 @@ export const swagger = components: { ...documentation.components, schemas: { - ...app.meta.defs, + // @ts-ignore + ...app.definitions, ...documentation.components?.schemas } } diff --git a/src/utils.ts b/src/utils.ts index 8707fa3..dbf2603 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -142,6 +142,8 @@ export const registerSchemaPath = ({ Object.entries(responseSchema as Record).forEach( ([key, value]) => { if (typeof value === 'string') { + if(!models[value]) return + // eslint-disable-next-line @typescript-eslint/no-unused-vars const { type, properties, required, ...rest } = models[ value