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.
Files changed (54) hide show
  1. package/CHANGELOG.md +71 -68
  2. package/README.md +24 -15
  3. package/dist/cjs/index.js +0 -4
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/utils/handleApiResponse.js +19 -7
  6. package/dist/cjs/utils/handleApiResponse.js.map +1 -1
  7. package/dist/esm/index.js +0 -4
  8. package/dist/esm/index.js.map +1 -1
  9. package/dist/esm/utils/handleApiResponse.js +18 -7
  10. package/dist/esm/utils/handleApiResponse.js.map +1 -1
  11. package/dist/types/index.d.ts +0 -4
  12. package/dist/types/index.d.ts.map +1 -1
  13. package/dist/types/interfaces/ApiResult.d.ts +10 -1
  14. package/dist/types/interfaces/ApiResult.d.ts.map +1 -1
  15. package/dist/types/types/ApiMethodOptions.d.ts +2 -2
  16. package/dist/types/types/ApiMethodOptions.d.ts.map +1 -1
  17. package/dist/types/utils/handleApiResponse.d.ts +4 -2
  18. package/dist/types/utils/handleApiResponse.d.ts.map +1 -1
  19. package/package.json +2 -1
  20. package/scripts/generate-api-minimal.mjs +30 -1
  21. package/scripts/openapi-generator/index.mjs +7 -35
  22. package/scripts/openapi-generator/method-template.mjs +379 -397
  23. package/scripts/openapi-generator/openapi-utils.mjs +411 -411
  24. package/scripts/openapi-generator/schema.mjs +581 -735
  25. package/dist/cjs/types/ApiMethodArguments.js +0 -3
  26. package/dist/cjs/types/ApiMethodArguments.js.map +0 -1
  27. package/dist/cjs/types/HttpResponse.js +0 -3
  28. package/dist/cjs/types/HttpResponse.js.map +0 -1
  29. package/dist/cjs/utils/extractArgsCallbacksAndParams.js +0 -51
  30. package/dist/cjs/utils/extractArgsCallbacksAndParams.js.map +0 -1
  31. package/dist/cjs/utils/extractArgsToastsAndParams.js +0 -49
  32. package/dist/cjs/utils/extractArgsToastsAndParams.js.map +0 -1
  33. package/dist/cjs/utils/wireMapping.js +0 -123
  34. package/dist/cjs/utils/wireMapping.js.map +0 -1
  35. package/dist/esm/types/ApiMethodArguments.js +0 -2
  36. package/dist/esm/types/ApiMethodArguments.js.map +0 -1
  37. package/dist/esm/types/HttpResponse.js +0 -2
  38. package/dist/esm/types/HttpResponse.js.map +0 -1
  39. package/dist/esm/utils/extractArgsCallbacksAndParams.js +0 -48
  40. package/dist/esm/utils/extractArgsCallbacksAndParams.js.map +0 -1
  41. package/dist/esm/utils/extractArgsToastsAndParams.js +0 -46
  42. package/dist/esm/utils/extractArgsToastsAndParams.js.map +0 -1
  43. package/dist/esm/utils/wireMapping.js +0 -119
  44. package/dist/esm/utils/wireMapping.js.map +0 -1
  45. package/dist/types/types/ApiMethodArguments.d.ts +0 -6
  46. package/dist/types/types/ApiMethodArguments.d.ts.map +0 -1
  47. package/dist/types/types/HttpResponse.d.ts +0 -2
  48. package/dist/types/types/HttpResponse.d.ts.map +0 -1
  49. package/dist/types/utils/extractArgsCallbacksAndParams.d.ts +0 -22
  50. package/dist/types/utils/extractArgsCallbacksAndParams.d.ts.map +0 -1
  51. package/dist/types/utils/extractArgsToastsAndParams.d.ts +0 -20
  52. package/dist/types/utils/extractArgsToastsAndParams.d.ts.map +0 -1
  53. package/dist/types/utils/wireMapping.d.ts +0 -30
  54. 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", "typedapi.manifest.json"];
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);