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
351export 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+ } ;
0 commit comments