Skip to content

Commit 070919e

Browse files
committed
🎉 feat: add data-configuration for adding custom Scalar configuration
1 parent b9198bf commit 070919e

File tree

7 files changed

+260
-231
lines changed

7 files changed

+260
-231
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.4.11 - 25 Sep 2025
2+
Improvement:
3+
- add `embedSpec` option to embed spec into documentation page
4+
- add `data-configuration` for adding custom Scalar configuration
5+
16
# 1.4.10 - 22 Sep 2025
27
Improvement:
38
- [#267](https://github.com/elysiajs/elysia-openapi/pull/267) enum eupport for OpenAPI

example/c.ts

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

example/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { openapi, withHeaders } from '../src/index'
77
const app = new Elysia()
88
.use(
99
openapi({
10+
embedSchema: true,
1011
mapJsonSchema: {
1112
zod: z.toJSONSchema
1213
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/openapi",
3-
"version": "1.4.10",
3+
"version": "1.4.11",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/index.ts

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export const openapi = <
5050
swagger,
5151
scalar,
5252
references,
53-
mapJsonSchema
53+
mapJsonSchema,
54+
embedSpec
5455
}: ElysiaOpenAPIConfig<Enabled, Path> = {}) => {
5556
if (!enabled) return new Elysia({ name: '@elysiajs/openapi' })
5657

@@ -66,6 +67,38 @@ export const openapi = <
6667
let totalRoutes = 0
6768
let cachedSchema: OpenAPIV3.Document | undefined
6869

70+
const toFullSchema = ({
71+
paths,
72+
components: { schemas }
73+
}: ReturnType<typeof toOpenAPISchema>): OpenAPIV3.Document => {
74+
return (cachedSchema = {
75+
openapi: '3.0.3',
76+
...documentation,
77+
tags: !exclude?.tags
78+
? documentation.tags
79+
: documentation.tags?.filter(
80+
(tag) => !exclude.tags?.includes(tag.name)
81+
),
82+
info: {
83+
title: 'Elysia Documentation',
84+
description: 'Development documentation',
85+
version: '0.0.0',
86+
...documentation.info
87+
},
88+
paths: {
89+
...paths,
90+
...documentation.paths
91+
},
92+
components: {
93+
...documentation.components,
94+
schemas: {
95+
...schemas,
96+
...(documentation.components?.schemas as any)
97+
}
98+
}
99+
})
100+
}
101+
69102
const app = new Elysia({ name: '@elysiajs/openapi' })
70103
.use((app) => {
71104
if (provider === null) return app
@@ -80,64 +113,58 @@ export const openapi = <
80113
autoDarkMode: true,
81114
...swagger
82115
})
83-
: ScalarRender(info, {
84-
url: relativePath,
85-
version: 'latest',
86-
cdn: `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${scalar?.version ?? 'latest'}/dist/browser/standalone.min.js`,
87-
...(scalar as ApiReferenceConfiguration),
88-
_integration: 'elysiajs'
89-
}),
116+
: ScalarRender(
117+
info,
118+
{
119+
url: relativePath,
120+
version: 'latest',
121+
cdn: `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${scalar?.version ?? 'latest'}/dist/browser/standalone.min.js`,
122+
...(scalar as ApiReferenceConfiguration),
123+
_integration: 'elysiajs'
124+
},
125+
embedSpec
126+
? JSON.stringify(
127+
totalRoutes === app.routes.length
128+
? cachedSchema
129+
: toFullSchema(
130+
toOpenAPISchema(
131+
app,
132+
exclude,
133+
references,
134+
mapJsonSchema
135+
)
136+
)
137+
)
138+
: undefined
139+
),
90140
{
91141
headers: {
92142
'content-type': 'text/html; charset=utf8'
93143
}
94144
}
95145
)
96146

97-
return app.get(path, isCloudflareWorker() ? page : page(), {
98-
detail: {
99-
hide: true
147+
return app.get(
148+
path,
149+
embedSpec || isCloudflareWorker() ? page : page(),
150+
{
151+
detail: {
152+
hide: true
153+
}
100154
}
101-
})
155+
)
102156
})
103157
.get(
104158
specPath,
105-
function openAPISchema() {
106-
if (totalRoutes === app.routes.length) return cachedSchema
159+
function openAPISchema(): OpenAPIV3.Document {
160+
if (totalRoutes === app.routes.length && cachedSchema)
161+
return cachedSchema
107162

108163
totalRoutes = app.routes.length
109164

110-
const {
111-
paths,
112-
components: { schemas }
113-
} = toOpenAPISchema(app, exclude, references, mapJsonSchema)
114-
115-
return (cachedSchema = {
116-
openapi: '3.0.3',
117-
...documentation,
118-
tags: !exclude?.tags
119-
? documentation.tags
120-
: documentation.tags?.filter(
121-
(tag) => !exclude.tags?.includes(tag.name)
122-
),
123-
info: {
124-
title: 'Elysia Documentation',
125-
description: 'Development documentation',
126-
version: '0.0.0',
127-
...documentation.info
128-
},
129-
paths: {
130-
...paths,
131-
...documentation.paths
132-
},
133-
components: {
134-
...documentation.components,
135-
schemas: {
136-
...schemas,
137-
...(documentation.components?.schemas as any)
138-
}
139-
}
140-
} satisfies OpenAPIV3.Document)
165+
return toFullSchema(
166+
toOpenAPISchema(app, exclude, references, mapJsonSchema)
167+
)
141168
},
142169
{
143170
error({ error }) {

0 commit comments

Comments
 (0)