typedapi-client-helpers 0.1.6 → 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 +9 -2
- package/scripts/generate-api-minimal.mjs +31 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedapi-client-helpers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Reusable typed OpenAPI client helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,9 +11,16 @@
|
|
|
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": {
|
|
21
|
+
"swaggerFile": "swagger/TestSwagger.json",
|
|
15
22
|
"apiOutput": "src/api",
|
|
16
|
-
"typedApiDefaultFunctionsPath": "
|
|
23
|
+
"typedApiDefaultFunctionsPath": "src/defaultApiFunctions",
|
|
17
24
|
"typedApiDefaultSuccessHandler": "handleGoodResult",
|
|
18
25
|
"typedApiDefaultErrorHandler": "handleErrors"
|
|
19
26
|
},
|
|
@@ -73,15 +73,17 @@ function getStringSetting(...values) {
|
|
|
73
73
|
return undefined;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
const fallbackDefaultFunctionsPath = "
|
|
76
|
+
const fallbackDefaultFunctionsPath = "defaultApiFunctions";
|
|
77
77
|
|
|
78
|
-
const
|
|
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(
|
|
198
|
-
|
|
199
|
-
|
|
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(
|
|
207
|
-
return
|
|
203
|
+
if (fs.existsSync(resolvedFilePath)) {
|
|
204
|
+
return resolvedFilePath;
|
|
208
205
|
}
|
|
209
206
|
|
|
210
|
-
fs.mkdirSync(path.dirname(
|
|
207
|
+
fs.mkdirSync(path.dirname(resolvedFilePath), { recursive: true });
|
|
211
208
|
|
|
212
209
|
fs.writeFileSync(
|
|
213
|
-
|
|
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(),
|
|
234
|
+
`Created default API handler file: ${path.relative(process.cwd(), resolvedFilePath)}`,
|
|
238
235
|
);
|
|
239
236
|
|
|
240
|
-
return
|
|
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,
|
|
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
|
};
|