swgto-ts 3.0.0 → 3.0.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/dist/{chunk-OU5BJNJI.js → chunk-GTJIYNPK.js} +36 -13
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -4
|
@@ -338,6 +338,16 @@ ${[...exportSet].join("\n")}
|
|
|
338
338
|
}
|
|
339
339
|
|
|
340
340
|
// src/generators/genRequestJs.ts
|
|
341
|
+
function isSimpleTypeRef(name) {
|
|
342
|
+
return /^[\w.]+$/.test(name);
|
|
343
|
+
}
|
|
344
|
+
function buildParamTag(requestParamType, typeImportPath) {
|
|
345
|
+
if (requestParamType === "void") return "[params]";
|
|
346
|
+
if (isSimpleTypeRef(requestParamType)) {
|
|
347
|
+
return `{import(${JSON.stringify(typeImportPath)}).${requestParamType}} params`;
|
|
348
|
+
}
|
|
349
|
+
return `{${requestParamType}} params`;
|
|
350
|
+
}
|
|
341
351
|
function buildFunctionDoc(operation, typeImportPath, requestParamType) {
|
|
342
352
|
const lines = [];
|
|
343
353
|
if (operation.summary) {
|
|
@@ -349,7 +359,7 @@ function buildFunctionDoc(operation, typeImportPath, requestParamType) {
|
|
|
349
359
|
lines.push(`@path ${operation.path}`);
|
|
350
360
|
lines.push(`@requestPath ${operation.requestPath}`);
|
|
351
361
|
lines.push(`@method ${operation.method.toUpperCase()}`);
|
|
352
|
-
lines.push(`@param ${requestParamType
|
|
362
|
+
lines.push(`@param ${buildParamTag(requestParamType, typeImportPath)}`);
|
|
353
363
|
lines.push("@param [config]");
|
|
354
364
|
if (operation.responseTypeName) {
|
|
355
365
|
lines.push(`@returns {Promise<import(${JSON.stringify(typeImportPath)}).${operation.responseTypeName}>}`);
|
|
@@ -363,14 +373,19 @@ function renderFunction(operation, useGenericUrl, mergeParams) {
|
|
|
363
373
|
const queryLine = operation.queryParams.length ? ` params: ${mergeParams ? "params" : "params?.query"},
|
|
364
374
|
` : "";
|
|
365
375
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
366
|
-
const
|
|
376
|
+
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
377
|
+
const needsCleanData = mergeParams && hasBodySchema && operation.pathParams.length > 0;
|
|
378
|
+
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
379
|
+
` : "";
|
|
380
|
+
const dataValue = needsCleanData ? "requestData" : mergeParams ? "params" : bodyOnly ? "params" : "params?.body";
|
|
381
|
+
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
367
382
|
` : "";
|
|
368
383
|
const pathArg = mergeParams ? "params" : "params?.path";
|
|
369
384
|
const urlValue = useGenericUrl && operation.pathParams.length ? `buildUrl(${JSON.stringify(operation.requestPath)}, ${pathArg})` : operation.pathParams.length ? `buildUrl(${pathArg})` : JSON.stringify(operation.requestPath);
|
|
370
385
|
const pathLine = ` url: ${urlValue},
|
|
371
386
|
`;
|
|
372
387
|
return `export async function ${operation.functionName}(params, config) {
|
|
373
|
-
return request({
|
|
388
|
+
${destructureLine} return request({
|
|
374
389
|
${pathLine} method: ${JSON.stringify(operation.method)},
|
|
375
390
|
${queryLine}${bodyLine} ...config,
|
|
376
391
|
});
|
|
@@ -381,7 +396,12 @@ function generateJsRequestFile(operation, httpClientPath, typeImportPath, mergeP
|
|
|
381
396
|
const queryLine = operation.queryParams.length ? ` params: ${mergeParams ? "params" : "params?.query"},
|
|
382
397
|
` : "";
|
|
383
398
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
384
|
-
const
|
|
399
|
+
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
400
|
+
const needsCleanData = mergeParams && hasBodySchema && operation.pathParams.length > 0;
|
|
401
|
+
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
402
|
+
` : "";
|
|
403
|
+
const dataValue = needsCleanData ? "requestData" : mergeParams ? "params" : bodyOnly ? "params" : "params?.body";
|
|
404
|
+
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
385
405
|
` : "";
|
|
386
406
|
const pathArg = mergeParams ? "params" : "params?.path";
|
|
387
407
|
const pathLine = operation.pathParams.length ? ` url: buildUrl(${pathArg}),
|
|
@@ -399,7 +419,7 @@ ${buildUrlHelper}
|
|
|
399
419
|
|
|
400
420
|
${buildFunctionDoc(operation, typeImportPath, requestParamType)}
|
|
401
421
|
export async function ${operation.functionName}(params, config) {
|
|
402
|
-
return request({
|
|
422
|
+
${destructureLine} return request({
|
|
403
423
|
${pathLine} method: ${JSON.stringify(operation.method)},
|
|
404
424
|
${queryLine}${bodyLine} ...config,
|
|
405
425
|
});
|
|
@@ -445,7 +465,12 @@ function renderFunction2(operation, useGenericUrl, mergeParams) {
|
|
|
445
465
|
const requestArg = operation.requestTypeExpression ? `params: ${operation.requestTypeExpression}, config?: RequestConfig` : "params?: void, config?: RequestConfig";
|
|
446
466
|
const defaultResponseType = operation.responseTypeName ?? "unknown";
|
|
447
467
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
448
|
-
const
|
|
468
|
+
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
469
|
+
const needsCleanData = mergeParams && hasBodySchema && operation.pathParams.length > 0;
|
|
470
|
+
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
471
|
+
` : "";
|
|
472
|
+
const dataValue = needsCleanData ? "requestData" : mergeParams ? "params" : bodyOnly ? "params" : "params?.body";
|
|
473
|
+
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
449
474
|
` : "";
|
|
450
475
|
const queryLine = operation.queryParams.length ? ` params: ${mergeParams ? "params" : "params?.query"},
|
|
451
476
|
` : "";
|
|
@@ -454,22 +479,20 @@ function renderFunction2(operation, useGenericUrl, mergeParams) {
|
|
|
454
479
|
const pathLine = ` url: ${urlValue},
|
|
455
480
|
`;
|
|
456
481
|
return `${buildFunctionDoc2(operation)}export async function ${operation.functionName}<T = ${defaultResponseType}>(${requestArg}): Promise<T> {
|
|
457
|
-
return request<T>({
|
|
482
|
+
${destructureLine} return request<T>({
|
|
458
483
|
${pathLine} method: ${JSON.stringify(operation.method)},
|
|
459
484
|
${queryLine}${bodyLine} ...config,
|
|
460
485
|
});
|
|
461
486
|
}`;
|
|
462
487
|
}
|
|
463
488
|
function generateTsRequestFile(operation, httpClientPath, typeImportPath, mergeParams = false) {
|
|
464
|
-
const importTypes = [
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
"RequestConfig"
|
|
468
|
-
].filter((value, index, array) => Boolean(value) && array.indexOf(value) === index);
|
|
489
|
+
const importTypes = [...operation.requestImportTypes, operation.responseTypeName, "RequestConfig"].filter(
|
|
490
|
+
(value, index, array) => Boolean(value) && array.indexOf(value) === index
|
|
491
|
+
);
|
|
469
492
|
const importLine = importTypes.length ? `import type { ${importTypes.join(", ")} } from ${JSON.stringify(typeImportPath)};
|
|
470
493
|
` : "";
|
|
471
494
|
const buildUrlHelper = operation.pathParams.length ? `
|
|
472
|
-
function buildUrl(path?: Record<string,
|
|
495
|
+
function buildUrl(path?: Record<string, any>): string {
|
|
473
496
|
return ${JSON.stringify(operation.requestPath)}.replace(/\\{([^}]+)\\}/g, (_, key) => String(path?.[key] ?? ''));
|
|
474
497
|
}
|
|
475
498
|
` : "";
|
package/dist/cli.js
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swgto-ts",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Generate API request files from OpenAPI 3.x documents.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -25,9 +25,6 @@
|
|
|
25
25
|
"generator",
|
|
26
26
|
"cli"
|
|
27
27
|
],
|
|
28
|
-
"publishConfig": {
|
|
29
|
-
"registry": "https://registry.npmjs.org/"
|
|
30
|
-
},
|
|
31
28
|
"engines": {
|
|
32
29
|
"node": ">=18"
|
|
33
30
|
},
|