-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (57 loc) · 1.99 KB
/
index.ts
File metadata and controls
67 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { openapiSchemaToJsonSchema } from '@openapi-contrib/openapi-schema-to-json-schema'
import type { OpenAPI, OpenAPIV3_1 } from 'openapi-types'
import type { RspressPlugin } from 'rspress/core'
import { convertObj } from 'swagger2openapi'
import { generateRuntimeModule } from '../../utils/index.js'
import type { ApiPluginOptions, CustomResourceDefinition } from './types.js'
export type * from './types.js'
// @internal
declare module 'doom-@api-crdsMap' {
const crdsMap: Record<string, CustomResourceDefinition>
}
// @internal
declare module 'doom-@api-openapisMap' {
const openapisMap: Record<string, OpenAPIV3_1.Document>
}
// @internal
declare module 'doom-@api-virtual' {
const virtual: Pick<ApiPluginOptions, 'references' | 'pathPrefix'>
}
export const apiPlugin = ({
localBasePath,
}: ApiPluginOptions): RspressPlugin => {
return {
name: 'doom-api',
async addRuntimeModules(config, isProd) {
return {
'doom-@api-virtual': `export default ${JSON.stringify({ references: config.api?.references, pathPrefix: config.api?.pathPrefix }, null, isProd ? 0 : 2)}`,
...(await generateRuntimeModule(
config.api?.crds,
'api-crds',
config.root!,
localBasePath,
isProd,
)),
...(await generateRuntimeModule<OpenAPI.Document>(
config.api?.openapis,
'api-openapis',
config.root!,
localBasePath,
isProd,
async (doc) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
doc.info ??= { title: 'Unknown', version: '0.0.0' }
if ('swagger' in doc && doc.swagger === '2.0') {
doc = (await convertObj(doc, {})).openapi
}
if ('openapi' in doc && doc.openapi.startsWith('3.0.')) {
doc = openapiSchemaToJsonSchema(doc) as OpenAPIV3_1.Document
doc.openapi = '3.1.0'
}
return doc
},
)),
}
},
}
}