typedapi-client-helpers 0.1.5 → 0.1.7
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/README.md +314 -676
- package/dist/api/generated/Import.d.ts +22 -0
- package/dist/api/generated/Import.d.ts.map +1 -0
- package/dist/api/generated/Import.js +48 -0
- package/dist/api/generated/Import.js.map +1 -0
- package/dist/api/generated/ImportRoute.d.ts +30 -0
- package/dist/api/generated/ImportRoute.d.ts.map +1 -0
- package/dist/api/generated/ImportRoute.js +13 -0
- package/dist/api/generated/ImportRoute.js.map +1 -0
- package/dist/api/generated/Order.d.ts +38 -0
- package/dist/api/generated/Order.d.ts.map +1 -0
- package/dist/api/generated/Order.js +75 -0
- package/dist/api/generated/Order.js.map +1 -0
- package/dist/api/generated/OrderRoute.d.ts +83 -0
- package/dist/api/generated/OrderRoute.d.ts.map +1 -0
- package/dist/api/generated/OrderRoute.js +13 -0
- package/dist/api/generated/OrderRoute.js.map +1 -0
- package/dist/api/generated/Product.d.ts +62 -0
- package/dist/api/generated/Product.d.ts.map +1 -0
- package/dist/api/generated/Product.js +114 -0
- package/dist/api/generated/Product.js.map +1 -0
- package/dist/api/generated/ProductRoute.d.ts +140 -0
- package/dist/api/generated/ProductRoute.d.ts.map +1 -0
- package/dist/api/generated/ProductRoute.js +13 -0
- package/dist/api/generated/ProductRoute.js.map +1 -0
- package/dist/api/generated/Report.d.ts +14 -0
- package/dist/api/generated/Report.d.ts.map +1 -0
- package/dist/api/generated/Report.js +32 -0
- package/dist/api/generated/Report.js.map +1 -0
- package/dist/api/generated/ReportRoute.d.ts +23 -0
- package/dist/api/generated/ReportRoute.d.ts.map +1 -0
- package/dist/api/generated/ReportRoute.js +13 -0
- package/dist/api/generated/ReportRoute.js.map +1 -0
- package/dist/api/generated/Supplier.d.ts +46 -0
- package/dist/api/generated/Supplier.d.ts.map +1 -0
- package/dist/api/generated/Supplier.js +87 -0
- package/dist/api/generated/Supplier.js.map +1 -0
- package/dist/api/generated/SupplierRoute.d.ts +101 -0
- package/dist/api/generated/SupplierRoute.d.ts.map +1 -0
- package/dist/api/generated/SupplierRoute.js +13 -0
- package/dist/api/generated/SupplierRoute.js.map +1 -0
- package/dist/api/generated/data-contracts.d.ts +277 -0
- package/dist/api/generated/data-contracts.d.ts.map +1 -0
- package/dist/api/generated/data-contracts.js +26 -0
- package/dist/api/generated/data-contracts.js.map +1 -0
- package/dist/api/generated/http-client.d.ts +61 -0
- package/dist/api/generated/http-client.d.ts.map +1 -0
- package/dist/api/generated/http-client.js +171 -0
- package/dist/api/generated/http-client.js.map +1 -0
- package/dist/api/index.d.ts +8 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +8 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/methods/Import.api.d.ts +6 -0
- package/dist/api/methods/Import.api.d.ts.map +1 -0
- package/dist/api/methods/Import.api.js +27 -0
- package/dist/api/methods/Import.api.js.map +1 -0
- package/dist/api/methods/Order.api.d.ts +29 -0
- package/dist/api/methods/Order.api.d.ts.map +1 -0
- package/dist/api/methods/Order.api.js +52 -0
- package/dist/api/methods/Order.api.js.map +1 -0
- package/dist/api/methods/Product.api.d.ts +39 -0
- package/dist/api/methods/Product.api.d.ts.map +1 -0
- package/dist/api/methods/Product.api.js +69 -0
- package/dist/api/methods/Product.api.js.map +1 -0
- package/dist/api/methods/Report.api.d.ts +6 -0
- package/dist/api/methods/Report.api.d.ts.map +1 -0
- package/dist/api/methods/Report.api.js +19 -0
- package/dist/api/methods/Report.api.js.map +1 -0
- package/dist/api/methods/Supplier.api.d.ts +35 -0
- package/dist/api/methods/Supplier.api.d.ts.map +1 -0
- package/dist/api/methods/Supplier.api.js +63 -0
- package/dist/api/methods/Supplier.api.js.map +1 -0
- package/dist/index.cjs +47 -551
- package/dist/index.d.cts +108 -18
- package/dist/index.d.ts +108 -18
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -551
- package/dist/index.js.map +1 -0
- package/dist/interfaces/ApiResult.d.ts +17 -0
- package/dist/interfaces/ApiResult.d.ts.map +1 -0
- package/dist/interfaces/ApiResult.js +2 -0
- package/dist/interfaces/ApiResult.js.map +1 -0
- package/dist/interfaces/Filter.d.ts +31 -0
- package/dist/interfaces/Filter.d.ts.map +1 -0
- package/dist/interfaces/Filter.js +2 -0
- package/dist/interfaces/Filter.js.map +1 -0
- package/dist/interfaces/SortType.d.ts +7 -0
- package/dist/interfaces/SortType.d.ts.map +1 -0
- package/dist/interfaces/SortType.js +2 -0
- package/dist/interfaces/SortType.js.map +1 -0
- package/dist/types/ApiCallOptions.d.ts +9 -0
- package/dist/types/ApiCallOptions.d.ts.map +1 -0
- package/dist/types/ApiCallOptions.js +2 -0
- package/dist/types/ApiCallOptions.js.map +1 -0
- package/dist/types/ApiMethodTypes.d.ts +24 -0
- package/dist/types/ApiMethodTypes.d.ts.map +1 -0
- package/dist/types/ApiMethodTypes.js +2 -0
- package/dist/types/ApiMethodTypes.js.map +1 -0
- package/dist/types/HttpResponse.d.ts +16 -0
- package/dist/types/HttpResponse.d.ts.map +1 -0
- package/dist/types/HttpResponse.js +2 -0
- package/dist/types/HttpResponse.js.map +1 -0
- package/dist/utils/SortTypeFromSortDirection.d.ts +22 -0
- package/dist/utils/SortTypeFromSortDirection.d.ts.map +1 -0
- package/dist/utils/SortTypeFromSortDirection.js +45 -0
- package/dist/utils/SortTypeFromSortDirection.js.map +1 -0
- package/dist/utils/buildQuery.d.ts +11 -0
- package/dist/utils/buildQuery.d.ts.map +1 -0
- package/dist/utils/buildQuery.js +94 -0
- package/dist/utils/buildQuery.js.map +1 -0
- package/dist/utils/extractArgsCallbacksAndParams.d.ts +22 -0
- package/dist/utils/extractArgsCallbacksAndParams.d.ts.map +1 -0
- package/dist/utils/extractArgsCallbacksAndParams.js +44 -0
- package/dist/utils/extractArgsCallbacksAndParams.js.map +1 -0
- package/dist/utils/extractArgsToastsAndParams.d.ts +18 -0
- package/dist/utils/extractArgsToastsAndParams.d.ts.map +1 -0
- package/dist/utils/extractArgsToastsAndParams.js +40 -0
- package/dist/utils/extractArgsToastsAndParams.js.map +1 -0
- package/dist/utils/handleApiResponse.d.ts +9 -0
- package/dist/utils/handleApiResponse.d.ts.map +1 -0
- package/dist/utils/handleApiResponse.js +49 -0
- package/dist/utils/handleApiResponse.js.map +1 -0
- package/dist/utils/toFormData.d.ts +8 -0
- package/dist/utils/toFormData.d.ts.map +1 -0
- package/dist/utils/toFormData.js +68 -0
- package/dist/utils/toFormData.js.map +1 -0
- package/package.json +11 -3
- package/scripts/generate-api-minimal.mjs +263 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedapi-client-helpers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Reusable typed OpenAPI client helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,9 +11,17 @@
|
|
|
11
11
|
"bin",
|
|
12
12
|
"README.md"
|
|
13
13
|
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Stef-Buurman/TypedApi.git",
|
|
17
|
+
"directory": "npm/typed-api-client"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/Stef-Buurman/TypedApi/tree/main/npm/typed-api-client#readme",
|
|
14
20
|
"config": {
|
|
15
|
-
"
|
|
16
|
-
"
|
|
21
|
+
"apiOutput": "src/api",
|
|
22
|
+
"typedApiDefaultFunctionsPath": "../../defaultApiFunctions",
|
|
23
|
+
"typedApiDefaultSuccessHandler": "handleGoodResult",
|
|
24
|
+
"typedApiDefaultErrorHandler": "handleErrors"
|
|
17
25
|
},
|
|
18
26
|
"bin": {
|
|
19
27
|
"typedapi-generate": "bin/typedapi-generate.js"
|
|
@@ -63,6 +63,39 @@ const useFilterFormValues =
|
|
|
63
63
|
process.env.npm_config_typed_api_use_filter_form_values === "true" ||
|
|
64
64
|
config.typedApiUseFilterFormValues === true;
|
|
65
65
|
|
|
66
|
+
function getStringSetting(...values) {
|
|
67
|
+
for (const value of values) {
|
|
68
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
69
|
+
return value.trim();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const fallbackDefaultFunctionsPath = "../../defaultApiFunctions";
|
|
77
|
+
|
|
78
|
+
const defaultFunctionsPath = getStringSetting(
|
|
79
|
+
process.env.TYPED_API_DEFAULT_FUNCTIONS_PATH,
|
|
80
|
+
process.env.npm_config_typed_api_default_functions_path,
|
|
81
|
+
config.typedApiDefaultFunctionsPath,
|
|
82
|
+
fallbackDefaultFunctionsPath,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const defaultSuccessHandlerName = getStringSetting(
|
|
86
|
+
process.env.TYPED_API_DEFAULT_SUCCESS_HANDLER,
|
|
87
|
+
process.env.npm_config_typed_api_default_success_handler,
|
|
88
|
+
config.typedApiDefaultSuccessHandler,
|
|
89
|
+
"handleGoodResult",
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const defaultErrorHandlerName = getStringSetting(
|
|
93
|
+
process.env.TYPED_API_DEFAULT_ERROR_HANDLER,
|
|
94
|
+
process.env.npm_config_typed_api_default_error_handler,
|
|
95
|
+
config.typedApiDefaultErrorHandler,
|
|
96
|
+
"handleErrors",
|
|
97
|
+
);
|
|
98
|
+
|
|
66
99
|
function pascalCase(value) {
|
|
67
100
|
return value
|
|
68
101
|
.split(/[-_]/)
|
|
@@ -126,6 +159,186 @@ ${names.map((name) => ` ${name},`).join("\n")}
|
|
|
126
159
|
} from "${from}";`;
|
|
127
160
|
}
|
|
128
161
|
|
|
162
|
+
function createAliasedImportStatement({ imports, from }) {
|
|
163
|
+
if (!imports || imports.length === 0) {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return `import {
|
|
168
|
+
${imports
|
|
169
|
+
.map(({ name, alias }) =>
|
|
170
|
+
alias && alias !== name ? ` ${name} as ${alias},` : ` ${name},`,
|
|
171
|
+
)
|
|
172
|
+
.join("\n")}
|
|
173
|
+
} from "${from}";`;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function resolveImportFilePath(fromDirectory, importPath) {
|
|
177
|
+
if (!importPath.startsWith(".")) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const basePath = path.resolve(fromDirectory, importPath);
|
|
182
|
+
const candidates = [
|
|
183
|
+
basePath,
|
|
184
|
+
`${basePath}.ts`,
|
|
185
|
+
`${basePath}.tsx`,
|
|
186
|
+
`${basePath}.js`,
|
|
187
|
+
`${basePath}.jsx`,
|
|
188
|
+
path.join(basePath, "index.ts"),
|
|
189
|
+
path.join(basePath, "index.tsx"),
|
|
190
|
+
path.join(basePath, "index.js"),
|
|
191
|
+
path.join(basePath, "index.jsx"),
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
return candidates.find((candidate) => fs.existsSync(candidate));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function createDefaultFunctionsFileIfMissing(fromDirectory, importPath) {
|
|
198
|
+
if (!importPath.startsWith(".")) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const basePath = path.resolve(fromDirectory, importPath);
|
|
203
|
+
const extension = path.extname(basePath);
|
|
204
|
+
const filePath = extension ? basePath : `${basePath}.ts`;
|
|
205
|
+
|
|
206
|
+
if (fs.existsSync(filePath)) {
|
|
207
|
+
return filePath;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
211
|
+
|
|
212
|
+
fs.writeFileSync(
|
|
213
|
+
filePath,
|
|
214
|
+
`
|
|
215
|
+
${createImportStatement({
|
|
216
|
+
names: ["ApiErrorResult", "ApiSuccessResult"],
|
|
217
|
+
from: "typedapi-client-helpers",
|
|
218
|
+
typeOnly: true,
|
|
219
|
+
})}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
export function ${defaultSuccessHandlerName}<T>(
|
|
223
|
+
_response: ApiSuccessResult<T>,
|
|
224
|
+
): void | Promise<void> {
|
|
225
|
+
// Add your default success handling here.
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function ${defaultErrorHandlerName}<T>(
|
|
229
|
+
_error: ApiErrorResult<T>,
|
|
230
|
+
): void | Promise<void> {
|
|
231
|
+
// Add your default error handling here.
|
|
232
|
+
}
|
|
233
|
+
`,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
console.log(
|
|
237
|
+
`Created default API handler file: ${path.relative(process.cwd(), filePath)}`,
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return filePath;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function escapeRegExp(value) {
|
|
244
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function fileExportsName(source, exportName) {
|
|
248
|
+
const escapedName = escapeRegExp(exportName);
|
|
249
|
+
|
|
250
|
+
const directExportPattern = new RegExp(
|
|
251
|
+
`export\\s+(?:async\\s+)?(?:function|const|let|var|class)\\s+${escapedName}\\b`,
|
|
252
|
+
"m",
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
if (directExportPattern.test(source)) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const namedExportBlocks = source.matchAll(/export\s*\{([\s\S]*?)\}/g);
|
|
260
|
+
|
|
261
|
+
for (const match of namedExportBlocks) {
|
|
262
|
+
const exportedNames = match[1]
|
|
263
|
+
.split(",")
|
|
264
|
+
.map((part) => part.trim())
|
|
265
|
+
.filter(Boolean);
|
|
266
|
+
|
|
267
|
+
if (
|
|
268
|
+
exportedNames.some((part) => {
|
|
269
|
+
const [localName, aliasName] = part
|
|
270
|
+
.split(/\s+as\s+/)
|
|
271
|
+
.map((name) => name.trim());
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
aliasName === exportName || (!aliasName && localName === exportName)
|
|
275
|
+
);
|
|
276
|
+
})
|
|
277
|
+
) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function resolveDefaultHandlers(importFromDirectory) {
|
|
286
|
+
const defaultFunctionsFile =
|
|
287
|
+
resolveImportFilePath(importFromDirectory, defaultFunctionsPath) ??
|
|
288
|
+
createDefaultFunctionsFileIfMissing(
|
|
289
|
+
importFromDirectory,
|
|
290
|
+
defaultFunctionsPath,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
if (!defaultFunctionsFile) {
|
|
294
|
+
console.warn(
|
|
295
|
+
`Default API handlers were not imported because "${defaultFunctionsPath}" is not a relative path and could not be created automatically.`,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const source = fs.readFileSync(defaultFunctionsFile, "utf8");
|
|
302
|
+
const hasSuccessHandler = fileExportsName(source, defaultSuccessHandlerName);
|
|
303
|
+
const hasErrorHandler = fileExportsName(source, defaultErrorHandlerName);
|
|
304
|
+
|
|
305
|
+
if (!hasSuccessHandler || !hasErrorHandler) {
|
|
306
|
+
const missingNames = [
|
|
307
|
+
...(!hasSuccessHandler ? [defaultSuccessHandlerName] : []),
|
|
308
|
+
...(!hasErrorHandler ? [defaultErrorHandlerName] : []),
|
|
309
|
+
];
|
|
310
|
+
|
|
311
|
+
console.warn(
|
|
312
|
+
`Default API handlers were not imported because ${path.relative(
|
|
313
|
+
process.cwd(),
|
|
314
|
+
defaultFunctionsFile,
|
|
315
|
+
)} does not export: ${missingNames.join(", ")}.`,
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
defaultFunctionsPath,
|
|
323
|
+
defaultSuccessHandlerName,
|
|
324
|
+
defaultErrorHandlerName,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function callbackOptionsExpression(defaultHandlers) {
|
|
329
|
+
if (defaultHandlers) {
|
|
330
|
+
return `{
|
|
331
|
+
onSuccess: onSuccess ?? typedApiDefaultSuccessHandler,
|
|
332
|
+
onError: onError ?? typedApiDefaultErrorHandler
|
|
333
|
+
}`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return `{
|
|
337
|
+
onSuccess,
|
|
338
|
+
onError
|
|
339
|
+
}`;
|
|
340
|
+
}
|
|
341
|
+
|
|
129
342
|
function getAllTsFiles(dir) {
|
|
130
343
|
let results = [];
|
|
131
344
|
|
|
@@ -248,6 +461,8 @@ function getGeneratedMethods(source) {
|
|
|
248
461
|
}
|
|
249
462
|
|
|
250
463
|
async function createMethodFiles() {
|
|
464
|
+
const defaultHandlers = resolveDefaultHandlers(methodsDir);
|
|
465
|
+
|
|
251
466
|
const generatedFiles = fs
|
|
252
467
|
.readdirSync(generatedDir)
|
|
253
468
|
.filter(
|
|
@@ -354,7 +569,9 @@ export async function ${methodName}(
|
|
|
354
569
|
onSuccess?: ApiSuccessHandler<
|
|
355
570
|
ExtractResponse<ReturnType<${className}["${methodName}"]>>
|
|
356
571
|
>,
|
|
357
|
-
onError?: ApiErrorHandler
|
|
572
|
+
onError?: ApiErrorHandler<
|
|
573
|
+
ExtractError<ReturnType<${className}["${methodName}"]>>
|
|
574
|
+
>,
|
|
358
575
|
params?: RequestParams
|
|
359
576
|
): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
|
|
360
577
|
return handleApiResponse(
|
|
@@ -370,7 +587,7 @@ export async function ${methodName}(
|
|
|
370
587
|
>(filters, page, pageSize, sortBy, sortDirection),
|
|
371
588
|
params ?? {}
|
|
372
589
|
),
|
|
373
|
-
{
|
|
590
|
+
${callbackOptionsExpression(defaultHandlers)}
|
|
374
591
|
);
|
|
375
592
|
}
|
|
376
593
|
`.trim();
|
|
@@ -382,12 +599,14 @@ export async function ${methodName}(
|
|
|
382
599
|
onSuccess?: ApiSuccessHandler<
|
|
383
600
|
ExtractResponse<ReturnType<${className}["${methodName}"]>>
|
|
384
601
|
>,
|
|
385
|
-
onError?: ApiErrorHandler
|
|
602
|
+
onError?: ApiErrorHandler<
|
|
603
|
+
ExtractError<ReturnType<${className}["${methodName}"]>>
|
|
604
|
+
>,
|
|
386
605
|
params?: RequestParams
|
|
387
606
|
): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
|
|
388
607
|
return handleApiResponse(
|
|
389
608
|
() => ${instanceName}.${methodName}(query, params ?? {}),
|
|
390
|
-
{
|
|
609
|
+
${callbackOptionsExpression(defaultHandlers)}
|
|
391
610
|
);
|
|
392
611
|
}
|
|
393
612
|
`.trim();
|
|
@@ -402,7 +621,9 @@ export async function ${methodName}(
|
|
|
402
621
|
onSuccess?: ApiSuccessHandler<
|
|
403
622
|
ExtractResponse<ReturnType<${className}["${methodName}"]>>
|
|
404
623
|
>,
|
|
405
|
-
onError?: ApiErrorHandler
|
|
624
|
+
onError?: ApiErrorHandler<
|
|
625
|
+
ExtractError<ReturnType<${className}["${methodName}"]>>
|
|
626
|
+
>,
|
|
406
627
|
params?: RequestParams
|
|
407
628
|
): Promise<
|
|
408
629
|
ApiResult<
|
|
@@ -416,7 +637,7 @@ export async function ${methodName}(
|
|
|
416
637
|
return handleApiResponse(
|
|
417
638
|
() =>
|
|
418
639
|
${instanceName}.${methodName}(query, params ?? {}),
|
|
419
|
-
{
|
|
640
|
+
${callbackOptionsExpression(defaultHandlers)}
|
|
420
641
|
);
|
|
421
642
|
}
|
|
422
643
|
`.trim(),
|
|
@@ -434,7 +655,9 @@ export async function ${methodName}(
|
|
|
434
655
|
onSuccess?: ApiSuccessHandler<
|
|
435
656
|
ExtractResponse<ReturnType<${className}["${methodName}"]>>
|
|
436
657
|
>,
|
|
437
|
-
onError?: ApiErrorHandler
|
|
658
|
+
onError?: ApiErrorHandler<
|
|
659
|
+
ExtractError<ReturnType<${className}["${methodName}"]>>
|
|
660
|
+
>,
|
|
438
661
|
params?: RequestParams
|
|
439
662
|
): Promise<
|
|
440
663
|
ApiResult<
|
|
@@ -455,7 +678,7 @@ export async function ${methodName}(
|
|
|
455
678
|
>[0],
|
|
456
679
|
params ?? {}
|
|
457
680
|
),
|
|
458
|
-
{
|
|
681
|
+
${callbackOptionsExpression(defaultHandlers)}
|
|
459
682
|
);
|
|
460
683
|
}
|
|
461
684
|
`.trim();
|
|
@@ -474,7 +697,13 @@ export async function ${methodName}(
|
|
|
474
697
|
>
|
|
475
698
|
>
|
|
476
699
|
>?,
|
|
477
|
-
ApiErrorHandler
|
|
700
|
+
ApiErrorHandler<
|
|
701
|
+
ExtractError<
|
|
702
|
+
ReturnType<
|
|
703
|
+
${className}["${methodName}"]
|
|
704
|
+
>
|
|
705
|
+
>
|
|
706
|
+
>?,
|
|
478
707
|
RequestParams?
|
|
479
708
|
]
|
|
480
709
|
): Promise<
|
|
@@ -499,6 +728,11 @@ export async function ${methodName}(
|
|
|
499
728
|
ReturnType<
|
|
500
729
|
${className}["${methodName}"]
|
|
501
730
|
>
|
|
731
|
+
>,
|
|
732
|
+
ExtractError<
|
|
733
|
+
ReturnType<
|
|
734
|
+
${className}["${methodName}"]
|
|
735
|
+
>
|
|
502
736
|
>
|
|
503
737
|
>(argsWithCallbacks);
|
|
504
738
|
|
|
@@ -514,10 +748,7 @@ export async function ${methodName}(
|
|
|
514
748
|
${instanceName}.${methodName}(
|
|
515
749
|
...requestArgs
|
|
516
750
|
),
|
|
517
|
-
{
|
|
518
|
-
onSuccess,
|
|
519
|
-
onError
|
|
520
|
-
}
|
|
751
|
+
${callbackOptionsExpression(defaultHandlers)}
|
|
521
752
|
);
|
|
522
753
|
}
|
|
523
754
|
`.trim();
|
|
@@ -535,6 +766,7 @@ export async function ${methodName}(
|
|
|
535
766
|
"ApiSuccessHandler",
|
|
536
767
|
"ApiErrorHandler",
|
|
537
768
|
"ExtractResponse",
|
|
769
|
+
"ExtractError",
|
|
538
770
|
|
|
539
771
|
...(hasPaginatedMethods && useFilterFormValues
|
|
540
772
|
? [
|
|
@@ -552,6 +784,22 @@ export async function ${methodName}(
|
|
|
552
784
|
from: runtimePackageName,
|
|
553
785
|
});
|
|
554
786
|
|
|
787
|
+
const defaultFunctionImports = defaultHandlers
|
|
788
|
+
? createAliasedImportStatement({
|
|
789
|
+
imports: [
|
|
790
|
+
{
|
|
791
|
+
name: defaultHandlers.defaultSuccessHandlerName,
|
|
792
|
+
alias: "typedApiDefaultSuccessHandler",
|
|
793
|
+
},
|
|
794
|
+
{
|
|
795
|
+
name: defaultHandlers.defaultErrorHandlerName,
|
|
796
|
+
alias: "typedApiDefaultErrorHandler",
|
|
797
|
+
},
|
|
798
|
+
],
|
|
799
|
+
from: defaultHandlers.defaultFunctionsPath,
|
|
800
|
+
})
|
|
801
|
+
: "";
|
|
802
|
+
|
|
555
803
|
const runtimeTypeImports = createImportStatement({
|
|
556
804
|
names: runtimeTypeImportNames,
|
|
557
805
|
from: runtimePackageName,
|
|
@@ -589,6 +837,8 @@ export async function ${methodName}(
|
|
|
589
837
|
const content = `
|
|
590
838
|
${runtimeValueImports}
|
|
591
839
|
|
|
840
|
+
${defaultFunctionImports}
|
|
841
|
+
|
|
592
842
|
${runtimeTypeImports}
|
|
593
843
|
|
|
594
844
|
${generatedValueImports}
|
|
@@ -653,14 +903,6 @@ async function fixGeneratedMixedImports(apiDir) {
|
|
|
653
903
|
const originalContent = fs.readFileSync(filePath, "utf8");
|
|
654
904
|
let content = originalContent;
|
|
655
905
|
|
|
656
|
-
/*
|
|
657
|
-
* Converts:
|
|
658
|
-
*
|
|
659
|
-
* import { ContentType, HttpClient, RequestParams }
|
|
660
|
-
* from "./http-client";
|
|
661
|
-
*
|
|
662
|
-
* into separate runtime and type imports.
|
|
663
|
-
*/
|
|
664
906
|
content = content.replace(
|
|
665
907
|
/import\s*\{([\s\S]*?)\}\s*from\s*["']\.\/http-client["'];/g,
|
|
666
908
|
(_match, importsText) => {
|
|
@@ -699,10 +941,6 @@ async function fixGeneratedMixedImports(apiDir) {
|
|
|
699
941
|
},
|
|
700
942
|
);
|
|
701
943
|
|
|
702
|
-
/*
|
|
703
|
-
* Everything imported from data-contracts by generated API classes
|
|
704
|
-
* is normally used as a TypeScript type.
|
|
705
|
-
*/
|
|
706
944
|
if (useTypeOnlyImports) {
|
|
707
945
|
content = content.replace(
|
|
708
946
|
/import\s*\{([\s\S]*?)\}\s*from\s*["']\.\/data-contracts["'];/g,
|