|
1
|
import { OpenAPIV3 } from "openapi-types" |
|
2
|
import { CodeSample } from "../../types/index.js" |
|
3
|
import { getFakeStrValue } from "utils" |
|
4
|
import { getRouteExamplesOutputBasePath } from "../../utils/get-output-base-paths.js" |
|
5
|
import { RouteExamples } from "types" |
|
6
|
import { readFileSync } from "fs" |
|
7
|
|
|
8
|
type CodeSampleData = Omit<CodeSample, "source"> |
|
9
|
|
|
10
|
const JS_SDK_PREFIX = { |
|
11
|
store: `import Medusa from "@medusajs/js-sdk" |
|
12
|
|
|
13
|
let MEDUSA_BACKEND_URL = "http://localhost:9000" |
|
14
|
|
|
15
|
if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) { |
|
16
|
MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL |
|
17
|
} |
|
18
|
|
|
19
|
export const sdk = new Medusa({ |
|
20
|
baseUrl: MEDUSA_BACKEND_URL, |
|
21
|
debug: process.env.NODE_ENV === "development", |
|
22
|
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY, |
|
23
|
}) |
|
24
|
|
|
25
|
`, |
|
26
|
admin: `import Medusa from "@medusajs/js-sdk" |
|
27
|
|
|
28
|
export const sdk = new Medusa({ |
|
29
|
baseUrl: import.meta.env.VITE_BACKEND_URL || "/", |
|
30
|
debug: import.meta.env.DEV, |
|
31
|
auth: { |
|
32
|
type: "session", |
|
33
|
}, |
|
34
|
}) |
|
35
|
|
|
36
|
`, |
|
37
|
} |
|
38
|
|
|
39
|
/** |
|
40
|
* This class generates examples for OAS. |
|
41
|
*/ |
|
42
|
class OasExamplesGenerator { |
|
43
|
static JSCLIENT_CODESAMPLE_DATA: CodeSampleData = { |
|
44
|
lang: "JavaScript", |
|
45
|
label: "JS SDK", |
|
46
|
} |
|
47
|
static CURL_CODESAMPLE_DATA: CodeSampleData = { |
|
48
|
lang: "Shell", |
|
49
|
label: "cURL", |
|
50
|
} |
|
51
|
|
|
52
|
private routeExamples: RouteExamples |
|
53
|
|
|
54
|
constructor() { |
|
55
|
// load route examples |
|
56
|
this.routeExamples = JSON.parse( |
|
57
|
readFileSync(getRouteExamplesOutputBasePath(), "utf8") |
|
58
|
) |
|
59
|
} |
|
60
|
|
|
61
|
generateJsSdkExanmple({ |
|
62
|
method, |
|
63
|
path, |
|
64
|
}: { |
|
65
|
method: string |
|
66
|
path: string |
|
67
|
}): string { |
|
68
|
const normalizedMethod = method.toUpperCase() |
|
69
|
// Try to match route by normalizing path parameters |
|
70
|
// path parameters may have different names, so we normalize them |
|
71
|
// to a generic `{param}` placeholder |
|
72
|
const normalizedPath = path.replaceAll(/\{[^}]+\}/g, "{param}") |
|
73
|
const targetRouteKey = `${normalizedMethod} ${normalizedPath}` |
|
74
|
const matchingRouteKey = Object.keys(this.routeExamples).find((key) => { |
|
75
|
const normalizedKey = key.replaceAll(/\{[^}]+\}/g, "{param}") |
|
76
|
return normalizedKey === targetRouteKey |
|
77
|
}) |
|
78
|
|
|
79
|
if (!matchingRouteKey || !this.routeExamples[matchingRouteKey]["js-sdk"]) { |
|
80
|
return "" |
|
81
|
} |
|
82
|
|
|
83
|
const area = path.startsWith("/store") ? "store" : "admin" |
|
84
|
return `${JS_SDK_PREFIX[area]}${this.routeExamples[matchingRouteKey]["js-sdk"]}` |
|
85
|
} |
|
86
|
|
|
87
|
/** |
|
88
|
* Generate cURL examples for an OAS operation. |
|
89
|
* |
|
90
|
* @param param0 - The operation's details. |
|
91
|
* @returns The cURL example. |
|
92
|
*/ |
|
93
|
generateCurlExample({ |
|
94
|
method, |
|
95
|
path, |
|
96
|
isAdminAuthenticated, |
|
97
|
isStoreAuthenticated, |
|
98
|
requestSchema, |
|
99
|
}: { |
|
100
|
/** |
|
101
|
* The HTTP method. |
|
102
|
*/ |
|
103
|
method: string |
|
104
|
/** |
|
105
|
* The API Route's path. |
|
106
|
*/ |
|
107
|
path: string |
|
108
|
/** |
|
109
|
* Whether the route requires admin authentication. |
|
110
|
*/ |
|
111
|
isAdminAuthenticated?: boolean |
|
112
|
/** |
|
113
|
* Whether the route requires customer authentication. |
|
114
|
*/ |
|
115
|
isStoreAuthenticated?: boolean |
|
116
|
/** |
|
117
|
* The schema of the request body, if any. |
|
118
|
*/ |
|
119
|
requestSchema?: OpenAPIV3.SchemaObject |
|
120
|
}): string { |
|
121
|
const exampleArr = [ |
|
122
|
`curl${ |
|
123
|
method.toLowerCase() !== "get" ? ` -X ${method.toUpperCase()}` : "" |
|
124
|
} '{backend_url}${path}'`, |
|
125
|
] |
|
126
|
|
|
127
|
if (isAdminAuthenticated) { |
|
128
|
exampleArr.push(`-H 'Authorization: Bearer {access_token}'`) |
|
129
|
} else if (isStoreAuthenticated) { |
|
130
|
exampleArr.push(`-H 'Authorization: Bearer {access_token}'`) |
|
131
|
} |
|
132
|
|
|
133
|
if (path.startsWith("/store")) { |
|
134
|
exampleArr.push(`-H 'x-publishable-api-key: {your_publishable_api_key}'`) |
|
135
|
} |
|
136
|
|
|
137
|
if (requestSchema) { |
|
138
|
const requestData = this.getSchemaRequiredData(requestSchema) |
|
139
|
|
|
140
|
if (Object.keys(requestData).length > 0) { |
|
141
|
exampleArr.push(`-H 'Content-Type: application/json'`) |
|
142
|
exampleArr.push( |
|
143
|
`--data-raw '${JSON.stringify(requestData, undefined, 2)}'` |
|
144
|
) |
|
145
|
} |
|
146
|
} |
|
147
|
|
|
148
|
return exampleArr.join(` \\\n`) |
|
149
|
} |
|
150
|
|
|
151
|
/** |
|
152
|
* Retrieves data object from a schema object. Only retrieves the required fields. |
|
153
|
* |
|
154
|
* @param schema - The schema to retrieve its required data object. |
|
155
|
* @returns An object of required data and their fake values. |
|
156
|
*/ |
|
157
|
getSchemaRequiredData( |
|
158
|
schema: OpenAPIV3.SchemaObject |
|
159
|
): Record<string, unknown> { |
|
160
|
const data: Record<string, unknown> = {} |
|
161
|
|
|
162
|
if (schema.required?.length && schema.properties) { |
|
163
|
schema.required.forEach((propertyName) => { |
|
164
|
// extract property and its type |
|
165
|
const property = schema.properties![ |
|
166
|
propertyName |
|
167
|
] as OpenAPIV3.SchemaObject |
|
168
|
let value: unknown |
|
169
|
if (property.type === "object") { |
|
170
|
const typedValue: Record<string, unknown> = {} |
|
171
|
// get the fake value of every property in the object |
|
172
|
if (property.properties) { |
|
173
|
Object.entries(property.properties).forEach( |
|
174
|
([childName, childProp]) => { |
|
175
|
const typedChildProp = childProp as OpenAPIV3.SchemaObject |
|
176
|
if (!typedChildProp.type) { |
|
177
|
return |
|
178
|
} |
|
179
|
// if the property is an object, get its data object |
|
180
|
// otherwise, get its fake value |
|
181
|
typedValue[childName] = |
|
182
|
typedChildProp.type === "object" |
|
183
|
? this.getSchemaRequiredData( |
|
184
|
typedChildProp as OpenAPIV3.SchemaObject |
|
185
|
) |
|
186
|
: getFakeStrValue({ |
|
187
|
name: childName, |
|
188
|
type: typedChildProp.type, |
|
189
|
format: typedChildProp.format, |
|
190
|
}) |
|
191
|
} |
|
192
|
) |
|
193
|
} |
|
194
|
|
|
195
|
value = typedValue |
|
196
|
} else if (property.type === "array") { |
|
197
|
// if the type of the array's items is an object, retrieve |
|
198
|
// its data object. Otherwise, retrieve its fake value. |
|
199
|
const propertyItems = property.items as OpenAPIV3.SchemaObject |
|
200
|
if (!propertyItems.type) { |
|
201
|
value = [] |
|
202
|
} else { |
|
203
|
value = [ |
|
204
|
propertyItems.type === "object" |
|
205
|
? this.getSchemaRequiredData( |
|
206
|
property.items as OpenAPIV3.SchemaObject |
|
207
|
) |
|
208
|
: getFakeStrValue({ |
|
209
|
name: propertyName, |
|
210
|
type: propertyItems.type, |
|
211
|
format: propertyItems.format, |
|
212
|
}), |
|
213
|
] |
|
214
|
} |
|
215
|
} else if (property.type) { |
|
216
|
// retrieve fake value for all other types |
|
217
|
value = getFakeStrValue({ |
|
218
|
name: propertyName, |
|
219
|
type: property.type, |
|
220
|
format: property.format, |
|
221
|
}) |
|
222
|
} |
|
223
|
|
|
224
|
if (value !== undefined) { |
|
225
|
data[propertyName] = value |
|
226
|
} |
|
227
|
}) |
|
228
|
} |
|
229
|
|
|
230
|
return data |
|
231
|
} |
|
232
|
} |
|
233
|
|
|
234
|
export default OasExamplesGenerator |