Skip to content

Commit fa956f4

Browse files
authored
Merge pull request #235 from elysiajs/hana
Refactor Swagger to OpenAPI
2 parents 63ad74a + 3df25f4 commit fa956f4

25 files changed

+1448
-1154
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
# 1.3.1
1+
# 1.3.2 - 22 Aug 2025
2+
Feature:
3+
- add `withHeader` for adding custom headers to response schema
4+
- spread all possible path for optional params
5+
- provider can be `null` to disable provider
6+
- export `toOpenAPI` to generate spec programmatically
7+
8+
Breaking change:
9+
- rename `@elysiajs/swagger` to `@elysiajs/openapi`
10+
- map all `swagger`, and `scalar` prefix to respective `swagger` and `scalar` properties
11+
- rename `swaggerConfig`, and `scalarConfig` to `swagger` and `scalar` respectively
12+
- map `excludePaths`, `excludeMethods`, `excludeTags`, `excludeStaticFiles` to property of `excludes`
13+
14+
# 1.3.1 - 28 Jun 2025
215
Bug fix:
316
- Using relative path for specPath
417

README.md

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# @elysiajs/swagger
2-
Plugin for [elysia](https://github.com/elysiajs/elysia) to auto-generate Swagger page.
1+
# @elysiajs/openapi
2+
Plugin for [elysia](https://github.com/elysiajs/elysia) to auto-generate API documentation page.
33

44
## Installation
55
```bash
6-
bun add @elysiajs/swagger
6+
bun add @elysiajs/openapi
77
```
88

99
## Example
1010
```typescript
1111
import { Elysia, t } from 'elysia'
12-
import { swagger } from '@elysiajs/swagger'
12+
import { openapi } from '@elysiajs/openapi'
1313

1414
const app = new Elysia()
15-
.use(swagger())
15+
.use(openapi())
1616
.get('/', () => 'hi', { response: t.String({ description: 'sample description' }) })
1717
.post(
1818
'/json/:id',
@@ -43,34 +43,59 @@ const app = new Elysia()
4343
.listen(8080);
4444
```
4545

46-
Then go to `http://localhost:8080/swagger`.
46+
Then go to `http://localhost:8080/openapi`.
4747

4848
# config
4949

50-
## provider
51-
@default 'scalar'
52-
Choose between [Scalar](https://github.com/scalar/scalar) & [SwaggerUI](https://github.com/swagger-api/swagger-ui)
50+
## enabled
51+
@default true
52+
Enable/Disable the plugin
5353

54-
## scalar
55-
Customize scalarConfig, refers to [Scalar config](https://github.com/scalar/scalar/blob/main/documentation/configuration.md)
54+
## documentation
55+
OpenAPI documentation information
5656

57-
## swagger
58-
Customize Swagger config, refers to [Swagger 3.0.3 config](https://swagger.io/specification/v3)
57+
@see https://spec.openapis.org/oas/v3.0.3.html
5958

60-
## path
61-
@default '/swagger'
59+
## exclude
60+
Configuration to exclude paths or methods from documentation
6261

63-
The endpoint to expose Swagger
62+
## exclude.methods
63+
List of methods to exclude from documentation
6464

65-
## scalarCDN
66-
Self-host the scalar bundle or point to a different CDN.
65+
## exclude.paths
66+
List of paths to exclude from documentation
6767

68-
## excludeStaticFile
68+
## exclude.staticFile
6969
@default true
7070

71-
Determine if Swagger should exclude static files.
71+
Exclude static file routes from documentation
7272

73-
## exclude
74-
@default []
73+
## exclude.tags
74+
List of tags to exclude from documentation
75+
76+
## path
77+
@default '/openapi'
78+
79+
The endpoint to expose OpenAPI documentation frontend
7580

76-
Paths to exclude from the Swagger endpoint
81+
## provider
82+
@default 'scalar'
83+
84+
OpenAPI documentation frontend between:
85+
- [Scalar](https://github.com/scalar/scalar)
86+
- [SwaggerUI](https://github.com/openapi-api/openapi-ui)
87+
- null: disable frontend
88+
89+
## references
90+
Additional OpenAPI reference for each endpoint
91+
92+
## scalar
93+
Scalar configuration, refers to [Scalar config](https://github.com/scalar/scalar/blob/main/documentation/configuration.md)
94+
95+
## specPath
96+
@default '/${path}/json'
97+
98+
The endpoint to expose OpenAPI specification in JSON format
99+
100+
## swagger
101+
Swagger config, refers to [Swagger config](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/)

build.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ import { build, type Options } from 'tsup'
44
await $`rm -rf dist`
55

66
const tsupConfig: Options = {
7-
entry: ['src/**/*.ts'],
8-
splitting: false,
9-
sourcemap: false,
10-
clean: true,
11-
bundle: true
7+
entry: ['src/**/*.ts'],
8+
splitting: false,
9+
sourcemap: false,
10+
clean: true,
11+
bundle: true
1212
} satisfies Options
1313

1414
await Promise.all([
15-
// ? tsup esm
16-
build({
17-
outDir: 'dist',
18-
format: 'esm',
19-
target: 'node20',
20-
cjsInterop: false,
21-
...tsupConfig
22-
}),
23-
// ? tsup cjs
24-
build({
25-
outDir: 'dist/cjs',
26-
format: 'cjs',
27-
target: 'node20',
28-
// dts: true,
29-
...tsupConfig
30-
})
15+
// ? tsup esm
16+
build({
17+
outDir: 'dist',
18+
format: 'esm',
19+
target: 'node20',
20+
cjsInterop: false,
21+
...tsupConfig
22+
}),
23+
// ? tsup cjs
24+
build({
25+
outDir: 'dist/cjs',
26+
format: 'cjs',
27+
target: 'node20',
28+
// dts: true,
29+
...tsupConfig
30+
})
3131
])
3232

3333
await $`tsc --project tsconfig.dts.json`

bun.lock

Lines changed: 60 additions & 35 deletions
Large diffs are not rendered by default.

example/gen.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Elysia, t } from 'elysia'
2+
import { openapi } from '../src/index'
3+
import { fromTypes } from '../src/gen'
4+
5+
export const app = new Elysia()
6+
.use(
7+
openapi({
8+
references: fromTypes('example/gen.ts')
9+
})
10+
)
11+
.get('/', { test: 'hello' as const })
12+
.post(
13+
'/json',
14+
({ body, status }) => (Math.random() > 0.5 ? status(418) : body),
15+
{
16+
body: t.Object({
17+
hello: t.String()
18+
})
19+
}
20+
)
21+
.get('/id/:id/name/:name', ({ params }) => params)
22+
.listen(3000)

example/index.ts

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { Elysia, t } from 'elysia'
2-
import { swagger } from '../src/index'
2+
import { openapi, withHeaders } from '../src/index'
33

44
const schema = t.Object({
55
test: t.Literal('hello')
66
})
77

8-
const app = new Elysia({ prefix: '/api' })
8+
const schema2 = t.Object({
9+
test: t.Literal('world')
10+
})
11+
12+
const user = t.Object({
13+
name: t.String({
14+
example: 'saltyaom'
15+
})
16+
})
17+
18+
export const app = new Elysia()
919
.use(
10-
swagger({
20+
openapi({
1121
provider: 'scalar',
1222
documentation: {
1323
info: {
1424
title: 'Elysia Scalar',
15-
version: '0.8.1'
25+
version: '1.3.1a'
1626
},
1727
tags: [
1828
{
@@ -21,39 +31,55 @@ const app = new Elysia({ prefix: '/api' })
2131
}
2232
],
2333
components: {
24-
schemas: {
25-
User: {
26-
description: 'string'
27-
}
28-
},
2934
securitySchemes: {
30-
JwtAuth: {
35+
bearer: {
3136
type: 'http',
32-
scheme: 'bearer',
33-
bearerFormat: 'JWT',
34-
description: 'Enter JWT Bearer token **_only_**'
37+
scheme: 'bearer'
38+
},
39+
cookie: {
40+
type: 'apiKey',
41+
in: 'cookie',
42+
name: 'session_id'
3543
}
3644
}
3745
}
38-
},
39-
swaggerOptions: {
40-
persistAuthorization: true
4146
}
4247
})
4348
)
44-
.model({ schema })
49+
.model({ schema, schema2, user })
4550
.get(
4651
'/',
47-
() => {
48-
return { test: 'hello' as const }
49-
},
52+
{ test: 'hello' as const },
53+
{
54+
response: {
55+
200: t.Object({
56+
test: t.Literal('hello')
57+
}),
58+
204: withHeaders(
59+
t.Void({
60+
title: 'Thing',
61+
description: 'Void response'
62+
}),
63+
{
64+
'X-Custom-Header': t.Literal('Elysia')
65+
}
66+
)
67+
}
68+
}
69+
)
70+
.post(
71+
'/json',
72+
({ body }) => ({
73+
test: 'world'
74+
}),
5075
{
51-
response: 'schema'
76+
parse: ['json', 'formdata'],
77+
body: 'user',
78+
response: {
79+
200: 'schema',
80+
400: 'schema2'
81+
}
5282
}
5383
)
54-
.post('/json', ({ body }) => body, {
55-
parse: ['json', 'formdata'],
56-
body: 'schema',
57-
response: 'schema'
58-
})
84+
.get('/id/:id/name/:name', () => {})
5985
.listen(3000)

example/index2.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)