|
1
|
import { upperCaseFirst } from "@medusajs/utils" |
|
2
|
import { OpenAPIObject } from "openapi3-ts" |
|
3
|
|
|
4
|
export async function combineOAS( |
|
5
|
adminOAS: OpenAPIObject, |
|
6
|
storeOAS: OpenAPIObject |
|
7
|
): Promise<OpenAPIObject> { |
|
8
|
prepareOASForCombine(adminOAS, "admin") |
|
9
|
prepareOASForCombine(storeOAS, "store") |
|
10
|
|
|
11
|
const combinedOAS: OpenAPIObject = { |
|
12
|
openapi: "3.0.0", |
|
13
|
info: { title: "Medusa API", version: "1.0.0" }, |
|
14
|
servers: [], |
|
15
|
paths: {}, |
|
16
|
tags: [], |
|
17
|
components: { |
|
18
|
callbacks: {}, |
|
19
|
examples: {}, |
|
20
|
headers: {}, |
|
21
|
links: {}, |
|
22
|
parameters: {}, |
|
23
|
requestBodies: {}, |
|
24
|
responses: {}, |
|
25
|
schemas: {}, |
|
26
|
securitySchemes: {}, |
|
27
|
}, |
|
28
|
} |
|
29
|
|
|
30
|
for (const oas of [adminOAS, storeOAS]) { |
|
31
|
/** |
|
32
|
* Combine paths |
|
33
|
*/ |
|
34
|
Object.assign(combinedOAS.paths, oas.paths) |
|
35
|
/** |
|
36
|
* Combine tags |
|
37
|
*/ |
|
38
|
if (oas.tags) { |
|
39
|
combinedOAS.tags = [...combinedOAS.tags!, ...oas.tags] |
|
40
|
} |
|
41
|
/** |
|
42
|
* Combine components |
|
43
|
*/ |
|
44
|
if (oas.components) { |
|
45
|
for (const componentGroup of [ |
|
46
|
"callbacks", |
|
47
|
"examples", |
|
48
|
"headers", |
|
49
|
"links", |
|
50
|
"parameters", |
|
51
|
"requestBodies", |
|
52
|
"responses", |
|
53
|
"schemas", |
|
54
|
"securitySchemes", |
|
55
|
]) { |
|
56
|
if (Object.keys(oas.components).includes(componentGroup)) { |
|
57
|
Object.assign( |
|
58
|
combinedOAS.components![componentGroup], |
|
59
|
oas.components[componentGroup] |
|
60
|
) |
|
61
|
} |
|
62
|
} |
|
63
|
} |
|
64
|
} |
|
65
|
|
|
66
|
return combinedOAS |
|
67
|
} |
|
68
|
|
|
69
|
function prepareOASForCombine( |
|
70
|
oas: OpenAPIObject, |
|
71
|
apiType: ApiType |
|
72
|
): OpenAPIObject { |
|
73
|
console.log( |
|
74
|
`🔵 Prefixing ${apiType} tags and operationId with ${upperCaseFirst( |
|
75
|
apiType |
|
76
|
)}` |
|
77
|
) |
|
78
|
for (const pathKey in oas.paths) { |
|
79
|
for (const operationKey in oas.paths[pathKey]) { |
|
80
|
/** |
|
81
|
* Prefix tags declared on routes |
|
82
|
* e.g.: Admin Customer, Store Customer |
|
83
|
*/ |
|
84
|
if (oas.paths[pathKey][operationKey].tags) { |
|
85
|
oas.paths[pathKey][operationKey].tags = oas.paths[pathKey][ |
|
86
|
operationKey |
|
87
|
].tags.map((tag) => getPrefixedTagName(tag, apiType)) |
|
88
|
} |
|
89
|
/** |
|
90
|
* Prefix operationId |
|
91
|
* e.g.: AdminGetCustomers, StoreGetCustomers |
|
92
|
*/ |
|
93
|
if (oas.paths[pathKey][operationKey].operationId) { |
|
94
|
oas.paths[pathKey][operationKey].operationId = getPrefixedOperationId( |
|
95
|
oas.paths[pathKey][operationKey].operationId, |
|
96
|
apiType |
|
97
|
) |
|
98
|
} |
|
99
|
} |
|
100
|
} |
|
101
|
|
|
102
|
/** |
|
103
|
* Prefix tags globally |
|
104
|
* e.g.: Admin Customer, Store Customer |
|
105
|
*/ |
|
106
|
if (oas.tags) { |
|
107
|
for (const tag of oas.tags) { |
|
108
|
tag.name = getPrefixedTagName(tag.name, apiType) |
|
109
|
} |
|
110
|
} |
|
111
|
return oas |
|
112
|
} |
|
113
|
|
|
114
|
function getPrefixedTagName(tagName: string, apiType: ApiType): string { |
|
115
|
return `${upperCaseFirst(apiType)} ${tagName}` |
|
116
|
} |
|
117
|
|
|
118
|
function getPrefixedOperationId(operationId: string, apiType: ApiType): string { |
|
119
|
return `${upperCaseFirst(apiType)}${operationId}` |
|
120
|
} |