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
|
@@ -118,6 +118,13 @@ const defaultErrorHandlerName = getStringSetting(
|
|
|
118
118
|
"handleErrors",
|
|
119
119
|
);
|
|
120
120
|
|
|
121
|
+
const defaultErrorMessageName = getStringSetting(
|
|
122
|
+
process.env.TYPED_API_DEFAULT_ERROR_MESSAGE,
|
|
123
|
+
process.env.npm_config_typed_api_default_error_message,
|
|
124
|
+
config.typedApiDefaultErrorMessage,
|
|
125
|
+
"unknownErrorMessage",
|
|
126
|
+
);
|
|
127
|
+
|
|
121
128
|
const generatorOptions = {
|
|
122
129
|
cleanOutput: getBooleanSetting(
|
|
123
130
|
process.env.TYPED_API_CLEAN_OUTPUT,
|
|
@@ -408,6 +415,7 @@ function createDefaultFunctionsFileIfMissing(filePath) {
|
|
|
408
415
|
const resolvedFilePath = extension ? filePath : `${filePath}.ts`;
|
|
409
416
|
|
|
410
417
|
if (fs.existsSync(resolvedFilePath)) {
|
|
418
|
+
addDefaultErrorMessageIfMissing(resolvedFilePath);
|
|
411
419
|
return resolvedFilePath;
|
|
412
420
|
}
|
|
413
421
|
|
|
@@ -432,6 +440,8 @@ export function ${defaultErrorHandlerName}<T>(
|
|
|
432
440
|
): void | Promise<void> {
|
|
433
441
|
// Add your default error handling here.
|
|
434
442
|
}
|
|
443
|
+
|
|
444
|
+
export const ${defaultErrorMessageName} = "An unknown error occurred.";
|
|
435
445
|
`,
|
|
436
446
|
);
|
|
437
447
|
|
|
@@ -442,6 +452,22 @@ export function ${defaultErrorHandlerName}<T>(
|
|
|
442
452
|
return resolvedFilePath;
|
|
443
453
|
}
|
|
444
454
|
|
|
455
|
+
|
|
456
|
+
function addDefaultErrorMessageIfMissing(filePath) {
|
|
457
|
+
const source = fs.readFileSync(filePath, "utf8");
|
|
458
|
+
if (fileExportsName(source, defaultErrorMessageName)) return;
|
|
459
|
+
|
|
460
|
+
const separator = source.endsWith("\n") ? "\n" : "\n\n";
|
|
461
|
+
fs.appendFileSync(
|
|
462
|
+
filePath,
|
|
463
|
+
`${separator}export const ${defaultErrorMessageName} = "An unknown error occurred.";\n`,
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
console.log(
|
|
467
|
+
`Added default API error message to: ${path.relative(process.cwd(), filePath)}`,
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
445
471
|
function toRelativeImportPath(fromDirectory, filePath) {
|
|
446
472
|
let relativePath = path.relative(fromDirectory, filePath);
|
|
447
473
|
|
|
@@ -508,11 +534,13 @@ function resolveDefaultHandlers() {
|
|
|
508
534
|
const source = fs.readFileSync(defaultFunctionsFile, "utf8");
|
|
509
535
|
const hasSuccessHandler = fileExportsName(source, defaultSuccessHandlerName);
|
|
510
536
|
const hasErrorHandler = fileExportsName(source, defaultErrorHandlerName);
|
|
537
|
+
const hasErrorMessage = fileExportsName(source, defaultErrorMessageName);
|
|
511
538
|
|
|
512
|
-
if (!hasSuccessHandler || !hasErrorHandler) {
|
|
539
|
+
if (!hasSuccessHandler || !hasErrorHandler || !hasErrorMessage) {
|
|
513
540
|
const missingNames = [
|
|
514
541
|
...(!hasSuccessHandler ? [defaultSuccessHandlerName] : []),
|
|
515
542
|
...(!hasErrorHandler ? [defaultErrorHandlerName] : []),
|
|
543
|
+
...(!hasErrorMessage ? [defaultErrorMessageName] : []),
|
|
516
544
|
];
|
|
517
545
|
|
|
518
546
|
console.warn(
|
|
@@ -529,6 +557,7 @@ function resolveDefaultHandlers() {
|
|
|
529
557
|
path: toRelativeImportPath(methodsDir, defaultFunctionsFile),
|
|
530
558
|
success: defaultSuccessHandlerName,
|
|
531
559
|
error: defaultErrorHandlerName,
|
|
560
|
+
errorMessage: defaultErrorMessageName,
|
|
532
561
|
};
|
|
533
562
|
}
|
|
534
563
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
1
|
import fs from "node:fs";
|
|
3
2
|
import os from "node:os";
|
|
4
3
|
import path from "node:path";
|
|
@@ -11,7 +10,8 @@ import {
|
|
|
11
10
|
methodFileNameForController,
|
|
12
11
|
} from "./method-template.mjs";
|
|
13
12
|
|
|
14
|
-
const MANAGED_TOP_LEVEL = ["generated", "methods", "index.ts"
|
|
13
|
+
const MANAGED_TOP_LEVEL = ["generated", "methods", "index.ts"];
|
|
14
|
+
const LEGACY_MANAGED_TOP_LEVEL = ["typedapi.manifest.json"];
|
|
15
15
|
|
|
16
16
|
export async function generateApi(options) {
|
|
17
17
|
const inputPath = path.resolve(options.input);
|
|
@@ -73,34 +73,6 @@ export async function generateApi(options) {
|
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
const indexLines = [
|
|
77
|
-
'export * from "./generated/data-contracts";',
|
|
78
|
-
'export * from "./generated/http-client";',
|
|
79
|
-
...controllerNames.map(
|
|
80
|
-
(controllerName) => `export * from "./methods/${methodFileNameForController(controllerName).replace(/\.ts$/, "")}";`,
|
|
81
|
-
),
|
|
82
|
-
];
|
|
83
|
-
files.set("index.ts", `${indexLines.join("\n")}\n`);
|
|
84
|
-
|
|
85
|
-
const schemaHash = crypto.createHash("sha256").update(source).digest("hex");
|
|
86
|
-
files.set(
|
|
87
|
-
"typedapi.manifest.json",
|
|
88
|
-
`${JSON.stringify(
|
|
89
|
-
{
|
|
90
|
-
contractVersion: 1,
|
|
91
|
-
generator: options.runtimePackageName ?? "typedapi-client-helpers",
|
|
92
|
-
generatorVersion: options.generatorVersion ?? "unknown",
|
|
93
|
-
schemaSha256: schemaHash,
|
|
94
|
-
producer: openApi["x-typedapi"] ?? null,
|
|
95
|
-
operationCount: operations.length,
|
|
96
|
-
methodNameStyle: normalizedOptions.methodNameStyle,
|
|
97
|
-
prefixMethodNamesWithController:
|
|
98
|
-
normalizedOptions.prefixMethodNamesWithController,
|
|
99
|
-
},
|
|
100
|
-
null,
|
|
101
|
-
2,
|
|
102
|
-
)}\n`,
|
|
103
|
-
);
|
|
104
76
|
|
|
105
77
|
const changes = compareOutput(output, files);
|
|
106
78
|
if (options.check) {
|
|
@@ -144,7 +116,7 @@ function compareOutput(output, files) {
|
|
|
144
116
|
|
|
145
117
|
function listManagedFiles(output) {
|
|
146
118
|
const result = [];
|
|
147
|
-
for (const topLevel of MANAGED_TOP_LEVEL) {
|
|
119
|
+
for (const topLevel of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
148
120
|
const target = path.join(output, topLevel);
|
|
149
121
|
if (!fs.existsSync(target)) continue;
|
|
150
122
|
if (fs.statSync(target).isFile()) {
|
|
@@ -165,7 +137,7 @@ function walk(directory, callback) {
|
|
|
165
137
|
}
|
|
166
138
|
|
|
167
139
|
function removeManagedOutput(root) {
|
|
168
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
140
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
169
141
|
fs.rmSync(path.join(root, item), { recursive: true, force: true });
|
|
170
142
|
}
|
|
171
143
|
}
|
|
@@ -253,14 +225,14 @@ function writeManagedOutputInPlace(output, staging) {
|
|
|
253
225
|
const originallyPresent = new Set();
|
|
254
226
|
|
|
255
227
|
try {
|
|
256
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
228
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
257
229
|
const current = path.join(output, item);
|
|
258
230
|
if (!fs.existsSync(current)) continue;
|
|
259
231
|
originallyPresent.add(item);
|
|
260
232
|
syncPath(current, path.join(managedBackup, item));
|
|
261
233
|
}
|
|
262
234
|
|
|
263
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
235
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
264
236
|
const source = path.join(staging, item);
|
|
265
237
|
const target = path.join(output, item);
|
|
266
238
|
if (fs.existsSync(source)) syncPath(source, target);
|
|
@@ -268,7 +240,7 @@ function writeManagedOutputInPlace(output, staging) {
|
|
|
268
240
|
}
|
|
269
241
|
} catch (error) {
|
|
270
242
|
try {
|
|
271
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
243
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
272
244
|
const target = path.join(output, item);
|
|
273
245
|
const saved = path.join(managedBackup, item);
|
|
274
246
|
if (originallyPresent.has(item)) syncPath(saved, target);
|