Skip to content

Commit 78aa870

Browse files
author
Daniil Zotov
authored
Merge branch 'main' into main
2 parents 1bc6491 + 18860ac commit 78aa870

File tree

6 files changed

+86
-15
lines changed

6 files changed

+86
-15
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.1.2 - 5 Sep 2024
2+
Feature:
3+
- add provenance publish
4+
5+
# 1.1.1 - 12 Aug 2024
6+
Feature:
7+
- add hide flag
18

29
# 1.1.0 - 16 Jul 2024
310
Change:
@@ -59,9 +66,9 @@ Change:
5966
Change:
6067
- Add support for Elysia 0.8
6168

62-
# 0.7.5
69+
# 0.7.5
6370
Improvement:
64-
- #[59](https://github.com/elysiajs/elysia-swagger/pull/59) use relative path to swagger json #59
71+
- #[59](https://github.com/elysiajs/elysia-swagger/pull/59) use relative path to swagger json #59
6572

6673
# 0.7.4 - 27 Oct 2023
6774
Improvement:
@@ -78,7 +85,7 @@ Bug fix:
7885
# 0.7.3 - 26 Sep 2023
7986
Feature:
8087
- [#19](https://github.com/elysiajs/elysia-swagger/pull/19) feat: handle nullish response types
81-
- [#18](https://github.com/elysiajs/elysia-swagger/pull/18) swagger ui options
88+
- [#18](https://github.com/elysiajs/elysia-swagger/pull/18) swagger ui options
8289

8390

8491
Improvement:

bun.lockb

24 Bytes
Binary file not shown.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/swagger",
3-
"version": "1.1.0",
3+
"version": "1.1.2",
44
"description": "Plugin for Elysia to auto-generate Swagger page",
55
"author": {
66
"name": "saltyAom",
@@ -74,6 +74,7 @@
7474
"typescript": "^5.5.3"
7575
},
7676
"dependencies": {
77-
"openapi-types": "^12.1.3"
77+
"openapi-types": "^12.1.3",
78+
"pathe": "^1.1.2"
7879
}
79-
}
80+
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const swagger = async <Path extends string = '/swagger'>(
6666

6767
app.get(path, function documentation() {
6868
const combinedSwaggerOptions = {
69-
url: `${relativePath}/json`,
69+
url: `/${relativePath}/json`,
7070
dom_id: '#swagger-ui',
7171
...swaggerOptions
7272
}
@@ -83,7 +83,7 @@ export const swagger = async <Path extends string = '/swagger'>(
8383
const scalarConfiguration: ReferenceConfiguration = {
8484
spec: {
8585
...scalarConfig.spec,
86-
url: `${relativePath}/json`
86+
url: `/${relativePath}/json`
8787
},
8888
...scalarConfig
8989
}
@@ -111,7 +111,7 @@ export const swagger = async <Path extends string = '/swagger'>(
111111
if (routes.length !== totalRoutes) {
112112
const ALLOWED_METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE']
113113
totalRoutes = routes.length
114-
114+
115115
routes.forEach((route: InternalRoute) => {
116116
if (route.hooks?.detail?.hide === true) return
117117
// TODO: route.hooks?.detail?.hide !== false add ability to hide: false to prevent excluding

src/swagger/index.ts

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
1-
import type { OpenAPIV3 } from 'openapi-types'
1+
import { OpenAPIV3 } from 'openapi-types';
2+
3+
type DateTimeSchema = {
4+
type: 'string';
5+
format: 'date-time';
6+
default?: string;
7+
};
8+
9+
type SchemaObject = OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject;
10+
11+
function isSchemaObject(schema: SchemaObject): schema is OpenAPIV3.SchemaObject {
12+
return 'type' in schema || 'properties' in schema || 'items' in schema;
13+
}
14+
15+
function isDateTimeProperty(key: string, schema: OpenAPIV3.SchemaObject): boolean {
16+
return (key === 'createdAt' || key === 'updatedAt') &&
17+
'anyOf' in schema &&
18+
Array.isArray(schema.anyOf);
19+
}
20+
21+
function transformDateProperties(schema: SchemaObject): SchemaObject {
22+
if (!isSchemaObject(schema) || typeof schema !== 'object' || schema === null) {
23+
return schema;
24+
}
25+
26+
const newSchema: OpenAPIV3.SchemaObject = { ...schema };
27+
28+
Object.entries(newSchema).forEach(([key, value]) => {
29+
if (isSchemaObject(value)) {
30+
if (isDateTimeProperty(key, value)) {
31+
const dateTimeFormat = value.anyOf?.find((item): item is OpenAPIV3.SchemaObject =>
32+
isSchemaObject(item) && item.format === 'date-time'
33+
);
34+
if (dateTimeFormat) {
35+
const dateTimeSchema: DateTimeSchema = {
36+
type: 'string',
37+
format: 'date-time',
38+
default: dateTimeFormat.default
39+
};
40+
(newSchema as Record<string, SchemaObject>)[key] = dateTimeSchema;
41+
}
42+
} else {
43+
(newSchema as Record<string, SchemaObject>)[key] = transformDateProperties(value);
44+
}
45+
}
46+
});
47+
48+
return newSchema;
49+
}
250

351
export const SwaggerUIRender = (
452
info: OpenAPIV3.InfoObject,
@@ -11,7 +59,21 @@ export const SwaggerUIRender = (
1159
},
1260
stringifiedSwaggerOptions: string,
1361
autoDarkMode?: boolean
14-
) => `<!DOCTYPE html>
62+
): string => {
63+
const swaggerOptions: OpenAPIV3.Document = JSON.parse(stringifiedSwaggerOptions);
64+
65+
if (swaggerOptions.components && swaggerOptions.components.schemas) {
66+
swaggerOptions.components.schemas = Object.fromEntries(
67+
Object.entries(swaggerOptions.components.schemas).map(([key, schema]) => [
68+
key,
69+
transformDateProperties(schema)
70+
])
71+
);
72+
}
73+
74+
const transformedStringifiedSwaggerOptions = JSON.stringify(swaggerOptions);
75+
76+
return `<!DOCTYPE html>
1577
<html lang="en">
1678
<head>
1779
<meta charset="utf-8" />
@@ -57,8 +119,9 @@ export const SwaggerUIRender = (
57119
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
58120
<script>
59121
window.onload = () => {
60-
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
122+
window.ui = SwaggerUIBundle(${transformedStringifiedSwaggerOptions});
61123
};
62124
</script>
63125
</body>
64-
</html>`
126+
</html>`;
127+
};

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
/* eslint-disable @typescript-eslint/no-unused-vars */
3-
import path from 'path'
3+
import { normalize } from 'pathe'
44
import type { HTTPMethod, LocalHook } from 'elysia'
55

66
import { Kind, type TSchema } from '@sinclair/typebox'
@@ -306,7 +306,7 @@ export const filterPaths = (
306306

307307
// exclude docs path and OpenAPI json path
308308
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
309-
path.normalize(p)
309+
normalize(p)
310310
)
311311

312312
for (const [key, value] of Object.entries(paths))

0 commit comments

Comments
 (0)