Skip to content

Commit

Permalink
🎉 feat: merge pr
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Sep 26, 2023
1 parent 37e0697 commit 37b9ddf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 50 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
32 changes: 15 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export const swagger =
excludeStaticFile = true,
path = '/swagger' as Path,
exclude = [],
swaggerOptions = {},
swaggerOptions = {}
}: ElysiaSwaggerConfig<Path> = {
documentation: {},
version: '4.18.2',
excludeStaticFile: true,
path: '/swagger' as Path,
exclude: [],
swaggerOptions: {},
swaggerOptions: {}
}
) =>
(app: Elysia) => {
Expand All @@ -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
}
}
)
Expand Down Expand Up @@ -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) {
Expand Down
75 changes: 46 additions & 29 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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()
})
})
2 changes: 1 addition & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 37b9ddf

Please sign in to comment.