typedapi-client-helpers 0.1.7 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typedapi-client-helpers",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Reusable typed OpenAPI client helpers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,8 +18,9 @@
18
18
  },
19
19
  "homepage": "https://github.com/Stef-Buurman/TypedApi/tree/main/npm/typed-api-client#readme",
20
20
  "config": {
21
+ "swaggerFile": "swagger/TestSwagger.json",
21
22
  "apiOutput": "src/api",
22
- "typedApiDefaultFunctionsPath": "../../defaultApiFunctions",
23
+ "typedApiDefaultFunctionsPath": "src/defaultApiFunctions",
23
24
  "typedApiDefaultSuccessHandler": "handleGoodResult",
24
25
  "typedApiDefaultErrorHandler": "handleErrors"
25
26
  },
@@ -73,15 +73,17 @@ function getStringSetting(...values) {
73
73
  return undefined;
74
74
  }
75
75
 
76
- const fallbackDefaultFunctionsPath = "../../defaultApiFunctions";
76
+ const fallbackDefaultFunctionsPath = "defaultApiFunctions";
77
77
 
78
- const defaultFunctionsPath = getStringSetting(
78
+ const defaultFunctionsPathSetting = getStringSetting(
79
79
  process.env.TYPED_API_DEFAULT_FUNCTIONS_PATH,
80
80
  process.env.npm_config_typed_api_default_functions_path,
81
81
  config.typedApiDefaultFunctionsPath,
82
82
  fallbackDefaultFunctionsPath,
83
83
  );
84
84
 
85
+ const defaultFunctionsFilePath = path.resolve(cwd, defaultFunctionsPathSetting);
86
+
85
87
  const defaultSuccessHandlerName = getStringSetting(
86
88
  process.env.TYPED_API_DEFAULT_SUCCESS_HANDLER,
87
89
  process.env.npm_config_typed_api_default_success_handler,
@@ -194,23 +196,18 @@ function resolveImportFilePath(fromDirectory, importPath) {
194
196
  return candidates.find((candidate) => fs.existsSync(candidate));
195
197
  }
196
198
 
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`;
199
+ function createDefaultFunctionsFileIfMissing(filePath) {
200
+ const extension = path.extname(filePath);
201
+ const resolvedFilePath = extension ? filePath : `${filePath}.ts`;
205
202
 
206
- if (fs.existsSync(filePath)) {
207
- return filePath;
203
+ if (fs.existsSync(resolvedFilePath)) {
204
+ return resolvedFilePath;
208
205
  }
209
206
 
210
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
207
+ fs.mkdirSync(path.dirname(resolvedFilePath), { recursive: true });
211
208
 
212
209
  fs.writeFileSync(
213
- filePath,
210
+ resolvedFilePath,
214
211
  `
215
212
  ${createImportStatement({
216
213
  names: ["ApiErrorResult", "ApiSuccessResult"],
@@ -234,10 +231,22 @@ export function ${defaultErrorHandlerName}<T>(
234
231
  );
235
232
 
236
233
  console.log(
237
- `Created default API handler file: ${path.relative(process.cwd(), filePath)}`,
234
+ `Created default API handler file: ${path.relative(process.cwd(), resolvedFilePath)}`,
238
235
  );
239
236
 
240
- return filePath;
237
+ return resolvedFilePath;
238
+ }
239
+
240
+ function toRelativeImportPath(fromDirectory, filePath) {
241
+ let relativePath = path.relative(fromDirectory, filePath);
242
+
243
+ if (!relativePath.startsWith(".")) {
244
+ relativePath = `./${relativePath}`;
245
+ }
246
+
247
+ relativePath = relativePath.replaceAll(path.sep, "/");
248
+
249
+ return relativePath.replace(/\.(ts|tsx|js|jsx)$/, "");
241
250
  }
242
251
 
243
252
  function escapeRegExp(value) {
@@ -284,19 +293,8 @@ function fileExportsName(source, exportName) {
284
293
 
285
294
  function resolveDefaultHandlers(importFromDirectory) {
286
295
  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
- }
296
+ resolveImportFilePath(importFromDirectory, defaultFunctionsFilePath) ??
297
+ createDefaultFunctionsFileIfMissing(defaultFunctionsFilePath);
300
298
 
301
299
  const source = fs.readFileSync(defaultFunctionsFile, "utf8");
302
300
  const hasSuccessHandler = fileExportsName(source, defaultSuccessHandlerName);
@@ -319,7 +317,10 @@ function resolveDefaultHandlers(importFromDirectory) {
319
317
  }
320
318
 
321
319
  return {
322
- defaultFunctionsPath,
320
+ defaultFunctionsPath: toRelativeImportPath(
321
+ importFromDirectory,
322
+ defaultFunctionsFile,
323
+ ),
323
324
  defaultSuccessHandlerName,
324
325
  defaultErrorHandlerName,
325
326
  };