typedapi-client-helpers 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -68
- package/README.md +24 -15
- package/dist/cjs/index.js +0 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/handleApiResponse.js +19 -7
- package/dist/cjs/utils/handleApiResponse.js.map +1 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/handleApiResponse.js +18 -7
- package/dist/esm/utils/handleApiResponse.js.map +1 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces/ApiResult.d.ts +10 -1
- package/dist/types/interfaces/ApiResult.d.ts.map +1 -1
- package/dist/types/types/ApiMethodOptions.d.ts +2 -2
- package/dist/types/types/ApiMethodOptions.d.ts.map +1 -1
- package/dist/types/utils/handleApiResponse.d.ts +4 -2
- package/dist/types/utils/handleApiResponse.d.ts.map +1 -1
- package/package.json +2 -1
- package/scripts/generate-api-minimal.mjs +30 -1
- package/scripts/openapi-generator/index.mjs +7 -35
- package/scripts/openapi-generator/method-template.mjs +379 -397
- package/scripts/openapi-generator/openapi-utils.mjs +411 -411
- package/scripts/openapi-generator/schema.mjs +581 -735
- package/dist/cjs/types/ApiMethodArguments.js +0 -3
- package/dist/cjs/types/ApiMethodArguments.js.map +0 -1
- package/dist/cjs/types/HttpResponse.js +0 -3
- package/dist/cjs/types/HttpResponse.js.map +0 -1
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js +0 -51
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/cjs/utils/extractArgsToastsAndParams.js +0 -49
- package/dist/cjs/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/cjs/utils/wireMapping.js +0 -123
- package/dist/cjs/utils/wireMapping.js.map +0 -1
- package/dist/esm/types/ApiMethodArguments.js +0 -2
- package/dist/esm/types/ApiMethodArguments.js.map +0 -1
- package/dist/esm/types/HttpResponse.js +0 -2
- package/dist/esm/types/HttpResponse.js.map +0 -1
- package/dist/esm/utils/extractArgsCallbacksAndParams.js +0 -48
- package/dist/esm/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/esm/utils/extractArgsToastsAndParams.js +0 -46
- package/dist/esm/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/esm/utils/wireMapping.js +0 -119
- package/dist/esm/utils/wireMapping.js.map +0 -1
- package/dist/types/types/ApiMethodArguments.d.ts +0 -6
- package/dist/types/types/ApiMethodArguments.d.ts.map +0 -1
- package/dist/types/types/HttpResponse.d.ts +0 -2
- package/dist/types/types/HttpResponse.d.ts.map +0 -1
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts +0 -22
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts.map +0 -1
- package/dist/types/utils/extractArgsToastsAndParams.d.ts +0 -20
- package/dist/types/utils/extractArgsToastsAndParams.d.ts.map +0 -1
- package/dist/types/utils/wireMapping.d.ts +0 -30
- package/dist/types/utils/wireMapping.d.ts.map +0 -1
|
@@ -1,397 +1,379 @@
|
|
|
1
|
-
import { getOperationTypes, generatedFileHeader } from "./schema.mjs";
|
|
2
|
-
import { pascalCase, typePropertyName } from "./names.mjs";
|
|
3
|
-
|
|
4
|
-
function unique(values) {
|
|
5
|
-
return [...new Set(values.filter(Boolean))];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function createImport(names, from, typeOnly = false) {
|
|
9
|
-
const cleanNames = unique(names).sort((a, b) => a.localeCompare(b));
|
|
10
|
-
if (cleanNames.length === 0) return "";
|
|
11
|
-
const keyword = typeOnly ? "import type" : "import";
|
|
12
|
-
if (cleanNames.length <= 3) return `${keyword} { ${cleanNames.join(", ")} } from ${JSON.stringify(from)};`;
|
|
13
|
-
return `${keyword} {\n${cleanNames.map((name) => ` ${name},`).join("\n")}\n} from ${JSON.stringify(from)};`;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function isPrimitiveType(type) {
|
|
17
|
-
return /^(void|any|unknown|string|number|boolean|Blob|File|Record<)/.test(type);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function extractTopLevelTypeNames(type) {
|
|
21
|
-
const withoutLiterals = String(type ?? "").replace(/"(?:\\.|[^"])*"|'(?:\\.|[^'])*'/g, "");
|
|
22
|
-
const matches = withoutLiterals.match(/\b[A-Z][A-Za-z0-9_]*\b/g) ?? [];
|
|
23
|
-
return matches.filter(
|
|
24
|
-
(name) => ![
|
|
25
|
-
"Array", "Record", "Promise", "HttpResponse", "Blob", "File", "ApiResult",
|
|
26
|
-
"RequestParams", "FormData", "ApiClientError",
|
|
27
|
-
].includes(name),
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function localPropertyAccess(baseExpression, wireName) {
|
|
32
|
-
return `${baseExpression}[${JSON.stringify(typePropertyName(wireName))}]`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function parameterValueExpression(sourceExpression, parameter) {
|
|
36
|
-
return localPropertyAccess(sourceExpression, parameter.name);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function parameterObjectExpression(sourceExpression, parameters) {
|
|
40
|
-
const properties = parameters.map(
|
|
41
|
-
(parameter) => `${JSON.stringify(parameter.name)}: ${parameterValueExpression(
|
|
42
|
-
sourceExpression,
|
|
43
|
-
parameter,
|
|
44
|
-
)}`,
|
|
45
|
-
);
|
|
46
|
-
return properties.length <= 2
|
|
47
|
-
? `{ ${properties.join(", ")} }`
|
|
48
|
-
: `{
|
|
49
|
-
${properties.map((property) => ` ${property},`).join("\n")}
|
|
50
|
-
}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function pathExpression(routePath, pathParams, inputName = "input") {
|
|
54
|
-
const parts = [];
|
|
55
|
-
let cursor = 0;
|
|
56
|
-
const regex = /\{([^}]+)\}/g;
|
|
57
|
-
let match;
|
|
58
|
-
|
|
59
|
-
while ((match = regex.exec(routePath))) {
|
|
60
|
-
const [placeholder, paramName] = match;
|
|
61
|
-
const before = routePath.slice(cursor, match.index);
|
|
62
|
-
if (before) parts.push(JSON.stringify(before));
|
|
63
|
-
const parameter = pathParams.find((item) => item.name === paramName);
|
|
64
|
-
const valueExpression = parameter
|
|
65
|
-
? parameterValueExpression(inputName, parameter)
|
|
66
|
-
: localPropertyAccess(inputName, paramName);
|
|
67
|
-
parts.push(`encodeURIComponent(String(${valueExpression}))`);
|
|
68
|
-
cursor = match.index + placeholder.length;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const after = routePath.slice(cursor);
|
|
72
|
-
if (after) parts.push(JSON.stringify(after));
|
|
73
|
-
if (parts.length === 0) return JSON.stringify(routePath);
|
|
74
|
-
|
|
75
|
-
return parts
|
|
76
|
-
.map((part) => /^"/.test(part) ? part.slice(1, -1).replace(/`/g, "\\`") : `\${${part}}`)
|
|
77
|
-
.join("")
|
|
78
|
-
.replace(/^/, "`")
|
|
79
|
-
.replace(/$/, "`");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function requestFormat(responseContentType, responseType) {
|
|
83
|
-
if (responseType === "void" || !responseContentType) return undefined;
|
|
84
|
-
if (responseContentType.includes("json")) return "json";
|
|
85
|
-
if (responseContentType.startsWith("text/")) return "text";
|
|
86
|
-
if (responseContentType === "application/octet-stream" || responseType === "Blob") return "blob";
|
|
87
|
-
return undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function contentTypeEnum(contentType) {
|
|
91
|
-
switch (contentType) {
|
|
92
|
-
case "application/json": return "ContentType.Json";
|
|
93
|
-
case "application/vnd.api+json": return "ContentType.JsonApi";
|
|
94
|
-
case "multipart/form-data": return "ContentType.FormData";
|
|
95
|
-
case "application/x-www-form-urlencoded": return "ContentType.UrlEncoded";
|
|
96
|
-
case "text/plain": return "ContentType.Text";
|
|
97
|
-
default: return contentType?.includes("json") ? "ContentType.Json" : undefined;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
`
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (operationTypes.bodyType) {
|
|
130
|
-
const bodyExpression = options.bodyValueName ?? "data";
|
|
131
|
-
items.push(`body: ${
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (operationTypes.hasHeaderParams || operationTypes.hasCookieParams) {
|
|
135
|
-
const headerSources = ["params.headers"];
|
|
136
|
-
if (operationTypes.hasHeaderParams) {
|
|
137
|
-
const headersExpression = parameterObjectExpression(
|
|
138
|
-
inputName,
|
|
139
|
-
operationTypes.parametersByLocation.header,
|
|
140
|
-
);
|
|
141
|
-
headerSources.push(`toRequestHeaders(${headersExpression})`);
|
|
142
|
-
}
|
|
143
|
-
if (operationTypes.hasCookieParams) {
|
|
144
|
-
const cookiesExpression = parameterObjectExpression(
|
|
145
|
-
inputName,
|
|
146
|
-
operationTypes.parametersByLocation.cookie,
|
|
147
|
-
);
|
|
148
|
-
const cookieCondition = operationTypes.parametersByLocation.cookie
|
|
149
|
-
.map((parameter) => `${parameterValueExpression(inputName, parameter)} !== undefined`)
|
|
150
|
-
.join(" || ") || "false";
|
|
151
|
-
headerSources.push(
|
|
152
|
-
`${cookieCondition} ? { Cookie: toCookieHeader(${cookiesExpression}) } : undefined`,
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
items.push(`headers: mergeHeaders(${headerSources.join(", ")})`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const type = contentTypeEnum(operationTypes.contentType);
|
|
159
|
-
if (type) items.push(`type: ${type}`);
|
|
160
|
-
const format = requestFormat(operationTypes.responseContentType, operationTypes.responseType);
|
|
161
|
-
if (format) items.push(`format: ${JSON.stringify(format)}`);
|
|
162
|
-
return items;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function callbackOptions(operationTypes, defaultHandlers) {
|
|
166
|
-
const items = [];
|
|
167
|
-
if (defaultHandlers) {
|
|
168
|
-
items.push("onSuccess: onSuccess ?? typedApiDefaultSuccessHandler");
|
|
169
|
-
items.push("onError: onError ?? typedApiDefaultErrorHandler");
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
items.push("
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
items.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function
|
|
196
|
-
return
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
)
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
args.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
operationTypes
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
if (operationTypes.
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
if (
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const useTypeOnlyImports = options.useTypeOnlyImports !== false;
|
|
382
|
-
const defaultHandlers = options.defaultHandlers;
|
|
383
|
-
const imports = [
|
|
384
|
-
generatedFileHeader(),
|
|
385
|
-
createImport(["request", ...(needsContentType ? ["ContentType"] : [])], "../generated/http-client"),
|
|
386
|
-
createImport(["RequestParams"], "../generated/http-client", useTypeOnlyImports),
|
|
387
|
-
createImport(dataContractValueImports, "../generated/data-contracts"),
|
|
388
|
-
createImport(dataContractImports, "../generated/data-contracts", useTypeOnlyImports),
|
|
389
|
-
createImport(helperValueImports, options.runtimePackageName ?? "typedapi-client-helpers"),
|
|
390
|
-
createImport(helperTypeImports, options.runtimePackageName ?? "typedapi-client-helpers", useTypeOnlyImports),
|
|
391
|
-
defaultHandlers
|
|
392
|
-
? `import { ${defaultHandlers.success} as typedApiDefaultSuccessHandler, ${defaultHandlers.error} as typedApiDefaultErrorHandler } from ${JSON.stringify(defaultHandlers.path)};`
|
|
393
|
-
: "",
|
|
394
|
-
].filter(Boolean);
|
|
395
|
-
|
|
396
|
-
return `${imports.join("\n")}\n\n${methods.join("\n\n")}\n`;
|
|
397
|
-
}
|
|
1
|
+
import { getOperationTypes, generatedFileHeader } from "./schema.mjs";
|
|
2
|
+
import { pascalCase, typePropertyName } from "./names.mjs";
|
|
3
|
+
|
|
4
|
+
function unique(values) {
|
|
5
|
+
return [...new Set(values.filter(Boolean))];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function createImport(names, from, typeOnly = false) {
|
|
9
|
+
const cleanNames = unique(names).sort((a, b) => a.localeCompare(b));
|
|
10
|
+
if (cleanNames.length === 0) return "";
|
|
11
|
+
const keyword = typeOnly ? "import type" : "import";
|
|
12
|
+
if (cleanNames.length <= 3) return `${keyword} { ${cleanNames.join(", ")} } from ${JSON.stringify(from)};`;
|
|
13
|
+
return `${keyword} {\n${cleanNames.map((name) => ` ${name},`).join("\n")}\n} from ${JSON.stringify(from)};`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isPrimitiveType(type) {
|
|
17
|
+
return /^(void|any|unknown|string|number|boolean|Blob|File|Record<)/.test(type);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function extractTopLevelTypeNames(type) {
|
|
21
|
+
const withoutLiterals = String(type ?? "").replace(/"(?:\\.|[^"])*"|'(?:\\.|[^'])*'/g, "");
|
|
22
|
+
const matches = withoutLiterals.match(/\b[A-Z][A-Za-z0-9_]*\b/g) ?? [];
|
|
23
|
+
return matches.filter(
|
|
24
|
+
(name) => ![
|
|
25
|
+
"Array", "Record", "Promise", "HttpResponse", "Blob", "File", "ApiResult",
|
|
26
|
+
"RequestParams", "FormData", "ApiClientError", "ApiHttpError",
|
|
27
|
+
].includes(name),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function localPropertyAccess(baseExpression, wireName) {
|
|
32
|
+
return `${baseExpression}[${JSON.stringify(typePropertyName(wireName))}]`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parameterValueExpression(sourceExpression, parameter) {
|
|
36
|
+
return localPropertyAccess(sourceExpression, parameter.name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parameterObjectExpression(sourceExpression, parameters) {
|
|
40
|
+
const properties = parameters.map(
|
|
41
|
+
(parameter) => `${JSON.stringify(parameter.name)}: ${parameterValueExpression(
|
|
42
|
+
sourceExpression,
|
|
43
|
+
parameter,
|
|
44
|
+
)}`,
|
|
45
|
+
);
|
|
46
|
+
return properties.length <= 2
|
|
47
|
+
? `{ ${properties.join(", ")} }`
|
|
48
|
+
: `{
|
|
49
|
+
${properties.map((property) => ` ${property},`).join("\n")}
|
|
50
|
+
}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pathExpression(routePath, pathParams, inputName = "input") {
|
|
54
|
+
const parts = [];
|
|
55
|
+
let cursor = 0;
|
|
56
|
+
const regex = /\{([^}]+)\}/g;
|
|
57
|
+
let match;
|
|
58
|
+
|
|
59
|
+
while ((match = regex.exec(routePath))) {
|
|
60
|
+
const [placeholder, paramName] = match;
|
|
61
|
+
const before = routePath.slice(cursor, match.index);
|
|
62
|
+
if (before) parts.push(JSON.stringify(before));
|
|
63
|
+
const parameter = pathParams.find((item) => item.name === paramName);
|
|
64
|
+
const valueExpression = parameter
|
|
65
|
+
? parameterValueExpression(inputName, parameter)
|
|
66
|
+
: localPropertyAccess(inputName, paramName);
|
|
67
|
+
parts.push(`encodeURIComponent(String(${valueExpression}))`);
|
|
68
|
+
cursor = match.index + placeholder.length;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const after = routePath.slice(cursor);
|
|
72
|
+
if (after) parts.push(JSON.stringify(after));
|
|
73
|
+
if (parts.length === 0) return JSON.stringify(routePath);
|
|
74
|
+
|
|
75
|
+
return parts
|
|
76
|
+
.map((part) => /^"/.test(part) ? part.slice(1, -1).replace(/`/g, "\\`") : `\${${part}}`)
|
|
77
|
+
.join("")
|
|
78
|
+
.replace(/^/, "`")
|
|
79
|
+
.replace(/$/, "`");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function requestFormat(responseContentType, responseType) {
|
|
83
|
+
if (responseType === "void" || !responseContentType) return undefined;
|
|
84
|
+
if (responseContentType.includes("json")) return "json";
|
|
85
|
+
if (responseContentType.startsWith("text/")) return "text";
|
|
86
|
+
if (responseContentType === "application/octet-stream" || responseType === "Blob") return "blob";
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function contentTypeEnum(contentType) {
|
|
91
|
+
switch (contentType) {
|
|
92
|
+
case "application/json": return "ContentType.Json";
|
|
93
|
+
case "application/vnd.api+json": return "ContentType.JsonApi";
|
|
94
|
+
case "multipart/form-data": return "ContentType.FormData";
|
|
95
|
+
case "application/x-www-form-urlencoded": return "ContentType.UrlEncoded";
|
|
96
|
+
case "text/plain": return "ContentType.Text";
|
|
97
|
+
default: return contentType?.includes("json") ? "ContentType.Json" : undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
function requestItems(operationInfo, operationTypes, options = {}) {
|
|
103
|
+
const inputName = options.inputName ?? "input";
|
|
104
|
+
const items = [
|
|
105
|
+
"...params",
|
|
106
|
+
`path: ${pathExpression(
|
|
107
|
+
operationInfo.routePath,
|
|
108
|
+
operationTypes.parametersByLocation.path,
|
|
109
|
+
inputName,
|
|
110
|
+
)}`,
|
|
111
|
+
`method: ${JSON.stringify(operationInfo.method)}`,
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
if (options.queryValueName) {
|
|
115
|
+
items.push(`query: ${options.queryValueName}`);
|
|
116
|
+
} else if (operationTypes.hasQueryParams) {
|
|
117
|
+
if (!operationTypes.hasPathParams && !operationTypes.hasHeaderParams && !operationTypes.hasCookieParams) {
|
|
118
|
+
items.push(`query: ${inputName}`);
|
|
119
|
+
} else {
|
|
120
|
+
items.push(
|
|
121
|
+
`query: ${parameterObjectExpression(
|
|
122
|
+
inputName,
|
|
123
|
+
operationTypes.parametersByLocation.query,
|
|
124
|
+
)}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (operationTypes.bodyType) {
|
|
130
|
+
const bodyExpression = options.bodyValueName ?? "data";
|
|
131
|
+
items.push(`body: ${bodyExpression}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (operationTypes.hasHeaderParams || operationTypes.hasCookieParams) {
|
|
135
|
+
const headerSources = ["params.headers"];
|
|
136
|
+
if (operationTypes.hasHeaderParams) {
|
|
137
|
+
const headersExpression = parameterObjectExpression(
|
|
138
|
+
inputName,
|
|
139
|
+
operationTypes.parametersByLocation.header,
|
|
140
|
+
);
|
|
141
|
+
headerSources.push(`toRequestHeaders(${headersExpression})`);
|
|
142
|
+
}
|
|
143
|
+
if (operationTypes.hasCookieParams) {
|
|
144
|
+
const cookiesExpression = parameterObjectExpression(
|
|
145
|
+
inputName,
|
|
146
|
+
operationTypes.parametersByLocation.cookie,
|
|
147
|
+
);
|
|
148
|
+
const cookieCondition = operationTypes.parametersByLocation.cookie
|
|
149
|
+
.map((parameter) => `${parameterValueExpression(inputName, parameter)} !== undefined`)
|
|
150
|
+
.join(" || ") || "false";
|
|
151
|
+
headerSources.push(
|
|
152
|
+
`${cookieCondition} ? { Cookie: toCookieHeader(${cookiesExpression}) } : undefined`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
items.push(`headers: mergeHeaders(${headerSources.join(", ")})`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const type = contentTypeEnum(operationTypes.contentType);
|
|
159
|
+
if (type) items.push(`type: ${type}`);
|
|
160
|
+
const format = requestFormat(operationTypes.responseContentType, operationTypes.responseType);
|
|
161
|
+
if (format) items.push(`format: ${JSON.stringify(format)}`);
|
|
162
|
+
return items;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function callbackOptions(operationTypes, defaultHandlers) {
|
|
166
|
+
const items = [];
|
|
167
|
+
if (defaultHandlers) {
|
|
168
|
+
items.push("onSuccess: onSuccess ?? typedApiDefaultSuccessHandler");
|
|
169
|
+
items.push("onError: onError ?? typedApiDefaultErrorHandler");
|
|
170
|
+
items.push("fallbackErrorMessage: typedApiDefaultErrorMessage");
|
|
171
|
+
} else {
|
|
172
|
+
items.push("onSuccess");
|
|
173
|
+
items.push("onError");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (operationTypes.usesHttpErrorFallback) {
|
|
177
|
+
items.push(
|
|
178
|
+
"transformError: (value, response) => createApiHttpError(response.status, value)",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return items.length <= 2
|
|
183
|
+
? `{ ${items.join(", ")} }`
|
|
184
|
+
: `{\n ${items.join(",\n ")},\n }`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function optionsType(operationTypes) {
|
|
188
|
+
return `ApiMethodOptions<${operationTypes.responseType}, ${operationTypes.errorType}, RequestParams>`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function promiseType(operationTypes) {
|
|
192
|
+
return `Promise<ApiResult<${operationTypes.responseType}, ${operationTypes.errorType}>>`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isPaginated(operationTypes) {
|
|
196
|
+
return Boolean(operationTypes.paginationMetadata) ||
|
|
197
|
+
/(?:Api)?Pagination(?:Sort)?Response\b|Paged(?:Response|Result)?\b|Paginated(?:Response|Result)?\b/.test(
|
|
198
|
+
operationTypes.responseType,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function methodDescription(operationInfo, operationTypes) {
|
|
203
|
+
const description = operationInfo.operation.description || operationInfo.operation.summary || "No description";
|
|
204
|
+
const tags = (operationInfo.operation.tags ?? []).join(", ");
|
|
205
|
+
return `/**\n * ${description.replace(/\n/g, "\n * ")}\n *\n * @tags ${tags}\n * @name ${operationTypes.methodNameSource}\n * @request ${operationInfo.method}:${operationInfo.routePath}\n */`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function createRequestCall(operationInfo, operationTypes, options = {}) {
|
|
209
|
+
const items = requestItems(operationInfo, operationTypes, options)
|
|
210
|
+
.map((item) => ` ${item},`)
|
|
211
|
+
.join("\n");
|
|
212
|
+
return `request<${operationTypes.responseType}, ${operationTypes.errorType}>({\n${items}\n })`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function createPaginatedMethod(operationInfo, operationTypes, defaultHandlers) {
|
|
216
|
+
const queryTypeName = operationTypes.parameterTypeNames.query;
|
|
217
|
+
const requestCall = createRequestCall(operationInfo, operationTypes, { queryValueName: "builtQuery" });
|
|
218
|
+
return `${methodDescription(operationInfo, operationTypes)}
|
|
219
|
+
export async function ${operationTypes.methodName}(
|
|
220
|
+
filters: FilterFormValues<${queryTypeName}>[] = [],
|
|
221
|
+
page = 1,
|
|
222
|
+
pageSize = 100,
|
|
223
|
+
sortBy: SortableKeys<${operationTypes.responseType}> | null = null,
|
|
224
|
+
sortDirection?: SortDirection,
|
|
225
|
+
options: ${optionsType(operationTypes)} = {},
|
|
226
|
+
): ${promiseType(operationTypes)} {
|
|
227
|
+
const { onSuccess, onError, params = {} } = options;
|
|
228
|
+
const builtQuery = buildQuery<
|
|
229
|
+
${queryTypeName},
|
|
230
|
+
UnwrapArray<ExtractDataIfPaginated<${operationTypes.responseType}>>
|
|
231
|
+
>(filters, page, pageSize, sortBy, sortDirection);
|
|
232
|
+
|
|
233
|
+
return handleApiResponse<${operationTypes.responseType}, ${operationTypes.errorType}>(
|
|
234
|
+
() => ${requestCall},
|
|
235
|
+
${callbackOptions(operationTypes, defaultHandlers)},
|
|
236
|
+
);
|
|
237
|
+
}`;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function regularParameterArgumentName(operationTypes) {
|
|
241
|
+
if (operationTypes.hasPathParams) return "pathParams";
|
|
242
|
+
if (
|
|
243
|
+
operationTypes.hasQueryParams &&
|
|
244
|
+
!operationTypes.hasHeaderParams &&
|
|
245
|
+
!operationTypes.hasCookieParams
|
|
246
|
+
) return "query";
|
|
247
|
+
return "requestParams";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function createRegularMethod(operationInfo, operationTypes, defaultHandlers) {
|
|
251
|
+
const args = [];
|
|
252
|
+
const parameterArgumentName = operationTypes.paramsTypeName
|
|
253
|
+
? regularParameterArgumentName(operationTypes)
|
|
254
|
+
: undefined;
|
|
255
|
+
|
|
256
|
+
if (operationTypes.paramsTypeName) {
|
|
257
|
+
const canOmitParams = operationTypes.allParametersOptional && !operationTypes.bodyType;
|
|
258
|
+
args.push(
|
|
259
|
+
`${parameterArgumentName}: ${operationTypes.paramsTypeName}${canOmitParams ? " = {}" : ""}`,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (operationTypes.bodyType) {
|
|
264
|
+
args.push(`data: ${operationTypes.bodyType}`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
args.push(`options: ${optionsType(operationTypes)} = {}`);
|
|
268
|
+
const requestCall = createRequestCall(operationInfo, operationTypes, {
|
|
269
|
+
inputName: parameterArgumentName ?? "undefined",
|
|
270
|
+
bodyValueName: operationTypes.bodyType ? "data" : undefined,
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
return `${methodDescription(operationInfo, operationTypes)}
|
|
274
|
+
export async function ${operationTypes.methodName}(
|
|
275
|
+
${args.join(",\n ")}
|
|
276
|
+
): ${promiseType(operationTypes)} {
|
|
277
|
+
const { onSuccess, onError, params = {} } = options;
|
|
278
|
+
|
|
279
|
+
return handleApiResponse<${operationTypes.responseType}, ${operationTypes.errorType}>(
|
|
280
|
+
() => ${requestCall},
|
|
281
|
+
${callbackOptions(operationTypes, defaultHandlers)},
|
|
282
|
+
);
|
|
283
|
+
}`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function createMethod(operationInfo, operationTypes, defaultHandlers, options) {
|
|
287
|
+
if (
|
|
288
|
+
operationTypes.hasQueryParams &&
|
|
289
|
+
!operationTypes.hasPathParams &&
|
|
290
|
+
!operationTypes.bodyType &&
|
|
291
|
+
!operationTypes.hasHeaderParams &&
|
|
292
|
+
!operationTypes.hasCookieParams &&
|
|
293
|
+
options.useFilterFormValues !== false &&
|
|
294
|
+
isPaginated(operationTypes)
|
|
295
|
+
) {
|
|
296
|
+
return createPaginatedMethod(operationInfo, operationTypes, defaultHandlers);
|
|
297
|
+
}
|
|
298
|
+
return createRegularMethod(operationInfo, operationTypes, defaultHandlers);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function methodFileNameForController(controllerName) {
|
|
302
|
+
return `${controllerName}.api.ts`;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function controllerNameForOperation(operationInfo, options = {}) {
|
|
306
|
+
if (options.moduleNameFirstTag !== false) {
|
|
307
|
+
const [tag] = operationInfo.operation.tags ?? [];
|
|
308
|
+
if (tag) return pascalCase(tag, "Default");
|
|
309
|
+
}
|
|
310
|
+
const parts = operationInfo.routePath.split("/").filter(Boolean);
|
|
311
|
+
return pascalCase(parts[1] ?? parts[0] ?? "Default", "Default");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function generateMethodFile(openApi, controllerName, operations, options = {}) {
|
|
315
|
+
const dataContractImports = [];
|
|
316
|
+
const helperTypeImports = ["ApiResult", "ApiMethodOptions"];
|
|
317
|
+
const helperValueImports = ["handleApiResponse"];
|
|
318
|
+
const methods = [];
|
|
319
|
+
let needsContentType = false;
|
|
320
|
+
let needsPaginationTypes = false;
|
|
321
|
+
let needsHeaderHelpers = false;
|
|
322
|
+
|
|
323
|
+
for (const operationInfo of operations) {
|
|
324
|
+
const operationTypes = getOperationTypes(openApi, operationInfo, options);
|
|
325
|
+
|
|
326
|
+
for (const typeName of Object.values(operationTypes.parameterTypeNames)) dataContractImports.push(typeName);
|
|
327
|
+
if (operationTypes.paramsTypeName) dataContractImports.push(operationTypes.paramsTypeName);
|
|
328
|
+
if (operationTypes.bodyType && !isPrimitiveType(operationTypes.bodyType)) {
|
|
329
|
+
dataContractImports.push(...extractTopLevelTypeNames(operationTypes.bodyType));
|
|
330
|
+
}
|
|
331
|
+
if (!isPrimitiveType(operationTypes.responseType)) {
|
|
332
|
+
dataContractImports.push(...extractTopLevelTypeNames(operationTypes.responseType));
|
|
333
|
+
}
|
|
334
|
+
if (!isPrimitiveType(operationTypes.errorType)) {
|
|
335
|
+
dataContractImports.push(...extractTopLevelTypeNames(operationTypes.errorType));
|
|
336
|
+
}
|
|
337
|
+
if (operationTypes.usesHttpErrorFallback) {
|
|
338
|
+
helperTypeImports.push("ApiHttpError");
|
|
339
|
+
helperValueImports.push("createApiHttpError");
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (contentTypeEnum(operationTypes.contentType)) needsContentType = true;
|
|
343
|
+
if (operationTypes.hasHeaderParams || operationTypes.hasCookieParams) needsHeaderHelpers = true;
|
|
344
|
+
if (
|
|
345
|
+
operationTypes.hasQueryParams &&
|
|
346
|
+
!operationTypes.hasPathParams && !operationTypes.bodyType &&
|
|
347
|
+
!operationTypes.hasHeaderParams && !operationTypes.hasCookieParams &&
|
|
348
|
+
options.useFilterFormValues !== false && isPaginated(operationTypes)
|
|
349
|
+
) {
|
|
350
|
+
needsPaginationTypes = true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
methods.push(createMethod(operationInfo, operationTypes, options.defaultHandlers, options));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (needsHeaderHelpers) helperValueImports.push("mergeHeaders", "toCookieHeader", "toRequestHeaders");
|
|
357
|
+
if (needsPaginationTypes) {
|
|
358
|
+
helperValueImports.push("buildQuery");
|
|
359
|
+
helperTypeImports.push(
|
|
360
|
+
"ExtractDataIfPaginated", "FilterFormValues", "SortableKeys", "SortDirection", "UnwrapArray",
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const useTypeOnlyImports = options.useTypeOnlyImports !== false;
|
|
365
|
+
const defaultHandlers = options.defaultHandlers;
|
|
366
|
+
const imports = [
|
|
367
|
+
generatedFileHeader(),
|
|
368
|
+
createImport(["request", ...(needsContentType ? ["ContentType"] : [])], "../generated/http-client"),
|
|
369
|
+
createImport(["RequestParams"], "../generated/http-client", useTypeOnlyImports),
|
|
370
|
+
createImport(dataContractImports, "../generated/data-contracts", useTypeOnlyImports),
|
|
371
|
+
createImport(helperValueImports, options.runtimePackageName ?? "typedapi-client-helpers"),
|
|
372
|
+
createImport(helperTypeImports, options.runtimePackageName ?? "typedapi-client-helpers", useTypeOnlyImports),
|
|
373
|
+
defaultHandlers
|
|
374
|
+
? `import { ${defaultHandlers.success} as typedApiDefaultSuccessHandler, ${defaultHandlers.error} as typedApiDefaultErrorHandler, ${defaultHandlers.errorMessage} as typedApiDefaultErrorMessage } from ${JSON.stringify(defaultHandlers.path)};`
|
|
375
|
+
: "",
|
|
376
|
+
].filter(Boolean);
|
|
377
|
+
|
|
378
|
+
return `${imports.join("\n")}\n\n${methods.join("\n\n")}\n`;
|
|
379
|
+
}
|