From 37b9ddf6d1451f21fbca909605b9138df2b68693 Mon Sep 17 00:00:00 2001 From: saltyaom Date: Tue, 26 Sep 2023 18:02:38 +0700 Subject: [PATCH] :tada: feat: merge pr --- CHANGELOG.md | 13 ++++++++ bun.lockb | Bin 44950 -> 44951 bytes package.json | 4 +-- src/index.ts | 32 +++++++++---------- test/index.test.ts | 75 +++++++++++++++++++++++++++------------------ tsconfig.cjs.json | 2 +- tsconfig.esm.json | 2 +- 7 files changed, 78 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12525b2..9a0a908 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +# 0.7.3 - 26 Sep 2023 +Feature: +- [#19](https://github.com/elysiajs/elysia-swagger/pull/19) feat: handle nullish response types +- [#18](https://github.com/elysiajs/elysia-swagger/pull/18) swagger ui options + + +Improvement: +- [#23](https://github.com/elysiajs/elysia-swagger/pull/23) Add github action to run bun test +- remove `removeComment` from tsconfig to show JSDoc + +Bug fix: +- [#16](https://github.com/elysiajs/elysia-swagger/pull/16) fix: use global prefix + # 0.7.2 - 21 Sep 2023 Bug fix: - Paths is undefined diff --git a/bun.lockb b/bun.lockb index 16aea815b6b152d38e9abb8ce4bd65756c257621..891c47fa73f2a30cbe8004984c9d3d041fff27e0 100755 GIT binary patch delta 513 zcmbPspK1DirU`nAQ(EH7w%fXj)$Ff({f{*(?Oc3paLMJlR{8g|d#5~`?ZCd#eX(pP}AB#_nt(!4-g1}dJMpP!wX%D}+A*^uQq^W+K+0gbk%A5}Y8 z*%&4y%+6+HK!5-!|H|yBS&R*;7dHoR^hk4>0u2MHVW0d^LVNNA4i45JpwU*7=ki%I z_DqgcspotPHX1=;Nyo}tFe|H-*%1qYpku-$`_VUiRscMND?o5nvrg{c?h71g_ xXnA@=GyA^HG-05Op`M|h5d(wgyaJ7H3u*^!w3}&W6p3R3P3f_18ujgOAxsYw6|3+p* z4|xU#P6mbs3m`23q%Q+$Ng%BaqgO!cpg2Iw)Mz|;g!{z{v9%)V!pur$P_Q{FD+LI@6aIgjf zt+ARsm(QB9dvc^oJ?9%B-wCLSb@Du<7}fUpGG3m1x@Rrpzs;4sI`W&Bt@+3V E07iq24*&oF diff --git a/package.json b/package.json index 799f67e..65e82bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@elysiajs/swagger", - "version": "0.7.2", + "version": "0.7.3", "description": "Plugin for Elysia to auto-generate Swagger page", "author": { "name": "saltyAom", @@ -40,7 +40,7 @@ "devDependencies": { "@types/node": "^20.1.4", "bun-types": "^0.7.0", - "elysia": "0.7.5", + "elysia": "0.7.10", "eslint": "^8.40.0", "rimraf": "4.3", "typescript": "^5.0.4" diff --git a/src/index.ts b/src/index.ts index 7ebc155..5f53499 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,14 +18,14 @@ export const swagger = excludeStaticFile = true, path = '/swagger' as Path, exclude = [], - swaggerOptions = {}, + swaggerOptions = {} }: ElysiaSwaggerConfig = { documentation: {}, version: '4.18.2', excludeStaticFile: true, path: '/swagger' as Path, exclude: [], - swaggerOptions: {}, + swaggerOptions: {} } ) => (app: Elysia) => { @@ -34,26 +34,26 @@ export const swagger = const info = { title: 'Elysia Documentation', - description: 'Developement documentation', + description: 'Development documentation', version: '0.0.0', ...documentation.info } - const pathWithPrefix = `${app.config.prefix}${path}`; + const pathWithPrefix = `${app.config.prefix}${path}` app.get(path, () => { - const combinedSwaggerOptions = { - url: '${pathWithPrefix}/json', - dom_id: '#swagger-ui', - ...swaggerOptions + const combinedSwaggerOptions = { + url: `${pathWithPrefix}/json`, + dom_id: '#swagger-ui', + ...swaggerOptions } - const stringifiedSwaggerOptions = JSON.stringify(combinedSwaggerOptions, - (key,value) => { - if (typeof value == "function") { - return undefined; - } - else { - return value; + const stringifiedSwaggerOptions = JSON.stringify( + combinedSwaggerOptions, + (key, value) => { + if (typeof value == 'function') { + return undefined + } else { + return value } } ) @@ -92,8 +92,6 @@ export const swagger = } ) }).get(`${path}/json`, () => { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore const routes = app.routes as InternalRoute[] if (routes.length !== totalRoutes) { diff --git a/test/index.test.ts b/test/index.test.ts index d3bca67..4b0e3b0 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -64,6 +64,9 @@ describe('Swagger', () => { const res = await app.handle(req('/v2/swagger')) expect(res.status).toBe(200) + + const resJson = await app.handle(req('/v2/swagger/json')) + expect(resJson.status).toBe(200) }) it('Swagger UI options', async () => { @@ -75,56 +78,70 @@ describe('Swagger', () => { }) ) const res = await app.handle(req('/swagger')).then((x) => x.text()) - const expected = ` - window.onload = () => { - window.ui = SwaggerUIBundle({"url":"/swagger/json","dom_id":"#swagger-ui","persistAuthorization":true}); - }; - ` + const expected = `"persistAuthorization":true` + expect(res.trim().includes(expected.trim())).toBe(true) }) it('should not return content response when using Void type', async () => { - const app = new Elysia().use( - swagger()) - .get('/void', () => {}, { - response: { 204: t.Void({ + const app = new Elysia().use(swagger()).get('/void', () => {}, { + response: { + 204: t.Void({ description: 'Void response' - })}}); + }) + } + }) const res = await app.handle(req('/swagger/json')) expect(res.status).toBe(200) - const response = await res.json(); - expect(response.paths['/void'].get.responses['204'].description).toBe('Void response'); - expect(response.paths['/void'].get.responses['204'].content).toBeUndefined(); + const response = await res.json() + expect(response.paths['/void'].get.responses['204'].description).toBe( + 'Void response' + ) + expect( + response.paths['/void'].get.responses['204'].content + ).toBeUndefined() }) it('should not return content response when using Undefined type', async () => { - const app = new Elysia().use( - swagger()) + const app = new Elysia() + .use(swagger()) .get('/undefined', () => undefined, { - response: { 204: t.Undefined({ - description: 'Undefined response' - })}}); + response: { + 204: t.Undefined({ + description: 'Undefined response' + }) + } + }) const res = await app.handle(req('/swagger/json')) expect(res.status).toBe(200) - const response = await res.json(); - expect(response.paths['/undefined'].get.responses['204'].description).toBe('Undefined response'); - expect(response.paths['/undefined'].get.responses['204'].content).toBeUndefined(); + const response = await res.json() + expect( + response.paths['/undefined'].get.responses['204'].description + ).toBe('Undefined response') + expect( + response.paths['/undefined'].get.responses['204'].content + ).toBeUndefined() }) it('should not return content response when using Null type', async () => { - const app = new Elysia().use( - swagger()) - .get('/null', () => null, { - response: { 204: t.Null({ + const app = new Elysia().use(swagger()).get('/null', () => null, { + response: { + 204: t.Null({ description: 'Null response' - })}}); + }) + } + }) const res = await app.handle(req('/swagger/json')) expect(res.status).toBe(200) - const response = await res.json(); - expect(response.paths['/null'].get.responses['204'].description).toBe('Null response'); - expect(response.paths['/null'].get.responses['204'].content).toBeUndefined(); + const response = await res.json() + expect(response.paths['/null'].get.responses['204'].description).toBe( + 'Null response' + ) + expect( + response.paths['/null'].get.responses['204'].content + ).toBeUndefined() }) }) diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 361240e..a5f1b14 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -50,7 +50,7 @@ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ "outDir": "./dist/cjs", /* Specify an output folder for all emitted files. */ - "removeComments": true, /* Disable emitting comments. */ + // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 6c047bd..887c6e7 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -50,7 +50,7 @@ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ "outDir": "./dist", /* Specify an output folder for all emitted files. */ - "removeComments": true, /* Disable emitting comments. */ + // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */