@acrool/rtk-query-codegen-openapi 0.0.2-test.0 → 0.0.2-test.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/lib/bin/cli.mjs +1 -1
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +196 -217
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +202 -218
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/bin/cli.ts +1 -1
- package/src/codegen.ts +3 -5
- package/src/generate.ts +211 -256
package/lib/bin/cli.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{fileURLToPath as l}from"node:url";var p=()=>l(import.meta.url);var c=p();import{generateEndpoints as m,parseConfig as f}from"@
|
|
2
|
+
import{fileURLToPath as l}from"node:url";var p=()=>l(import.meta.url);var c=p();import{generateEndpoints as m,parseConfig as f}from"@rtk-query/codegen-openapi";import r from"commander";import{createRequire as u}from"node:module";import{dirname as g,resolve as d}from"node:path";var e=u(c),o=!1;try{e.resolve("esbuild")&&e.resolve("esbuild-runner")&&e("esbuild-runner/register"),o=!0}catch{}try{o||(e.resolve("typescript")&&e.resolve("ts-node")&&e("ts-node").register({transpileOnly:!0,compilerOptions:{target:"es6",module:"commonjs"}}),o=!0)}catch{}var h=e("../../package.json");r.version(h.version).usage("</path/to/config.js>").parse(process.argv);var t=r.args[0];r.args.length===0||!/\.([mc]?(jsx?|tsx?)|jsonc?)?$/.test(t)?r.help():(/\.[mc]?tsx?$/.test(t)&&!o&&(console.error("Encountered a TypeScript configfile, but neither esbuild-runner nor ts-node are installed."),process.exit(1)),v(d(process.cwd(),t)));async function v(s){process.chdir(g(s));let n=e(s);for(let i of f(n.default??n))try{console.log(`Generating ${i.outputFile}`),await m(i),console.log("Done")}catch(a){console.error(a),process.exit(1)}}
|
|
3
3
|
//# sourceMappingURL=cli.mjs.map
|
package/lib/bin/cli.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../node_modules/tsup/assets/esm_shims.js","../../src/bin/cli.ts"],"sourcesContent":["// Shim globals in esm bundle\nimport path from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst getFilename = () => fileURLToPath(import.meta.url)\nconst getDirname = () => path.dirname(getFilename())\n\nexport const __dirname = /* @__PURE__ */ getDirname()\nexport const __filename = /* @__PURE__ */ getFilename()\n","#!/usr/bin/env node\n\nimport { generateEndpoints, parseConfig } from '@
|
|
1
|
+
{"version":3,"sources":["../../node_modules/tsup/assets/esm_shims.js","../../src/bin/cli.ts"],"sourcesContent":["// Shim globals in esm bundle\nimport path from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst getFilename = () => fileURLToPath(import.meta.url)\nconst getDirname = () => path.dirname(getFilename())\n\nexport const __dirname = /* @__PURE__ */ getDirname()\nexport const __filename = /* @__PURE__ */ getFilename()\n","#!/usr/bin/env node\n\nimport { generateEndpoints, parseConfig } from '@rtk-query/codegen-openapi';\nimport program from 'commander';\nimport { createRequire } from 'node:module';\nimport { dirname, resolve } from 'node:path';\n\nconst require = createRequire(__filename);\n\nlet ts = false;\ntry {\n if (require.resolve('esbuild') && require.resolve('esbuild-runner')) {\n require('esbuild-runner/register');\n }\n ts = true;\n} catch {}\n\ntry {\n if (!ts) {\n if (require.resolve('typescript') && require.resolve('ts-node')) {\n (require('ts-node') as typeof import('ts-node')).register({\n transpileOnly: true,\n compilerOptions: {\n target: 'es6',\n module: 'commonjs',\n },\n });\n }\n\n ts = true;\n }\n} catch {}\n\n// tslint:disable-next-line\nconst meta = require('../../package.json');\n\nprogram.version(meta.version).usage('</path/to/config.js>').parse(process.argv);\n\nconst configFile = program.args[0];\n\nif (program.args.length === 0 || !/\\.([mc]?(jsx?|tsx?)|jsonc?)?$/.test(configFile)) {\n program.help();\n} else {\n if (/\\.[mc]?tsx?$/.test(configFile) && !ts) {\n console.error('Encountered a TypeScript configfile, but neither esbuild-runner nor ts-node are installed.');\n process.exit(1);\n }\n run(resolve(process.cwd(), configFile));\n}\n\nasync function run(configFile: string) {\n process.chdir(dirname(configFile));\n\n const unparsedConfig = require(configFile);\n\n for (const config of parseConfig(unparsedConfig.default ?? unparsedConfig)) {\n try {\n console.log(`Generating ${config.outputFile}`);\n await generateEndpoints(config);\n console.log(`Done`);\n } catch (err) {\n console.error(err);\n process.exit(1);\n }\n }\n}\n"],"mappings":";AAEA,OAAS,iBAAAA,MAAqB,WAE9B,IAAMC,EAAc,IAAMD,EAAc,YAAY,GAAG,EAIhD,IAAME,EAA6BC,EAAY,ECNtD,OAAS,qBAAAC,EAAmB,eAAAC,MAAmB,6BAC/C,OAAOC,MAAa,YACpB,OAAS,iBAAAC,MAAqB,cAC9B,OAAS,WAAAC,EAAS,WAAAC,MAAe,YAEjC,IAAMC,EAAUH,EAAcI,CAAU,EAEpCC,EAAK,GACT,GAAI,CACEF,EAAQ,QAAQ,SAAS,GAAKA,EAAQ,QAAQ,gBAAgB,GAChEA,EAAQ,yBAAyB,EAEnCE,EAAK,EACP,MAAQ,CAAC,CAET,GAAI,CACGA,IACCF,EAAQ,QAAQ,YAAY,GAAKA,EAAQ,QAAQ,SAAS,GAC3DA,EAAQ,SAAS,EAA+B,SAAS,CACxD,cAAe,GACf,gBAAiB,CACf,OAAQ,MACR,OAAQ,UACV,CACF,CAAC,EAGHE,EAAK,GAET,MAAQ,CAAC,CAGT,IAAMC,EAAOH,EAAQ,oBAAoB,EAEzCJ,EAAQ,QAAQO,EAAK,OAAO,EAAE,MAAM,sBAAsB,EAAE,MAAM,QAAQ,IAAI,EAE9E,IAAMC,EAAaR,EAAQ,KAAK,CAAC,EAE7BA,EAAQ,KAAK,SAAW,GAAK,CAAC,gCAAgC,KAAKQ,CAAU,EAC/ER,EAAQ,KAAK,GAET,eAAe,KAAKQ,CAAU,GAAK,CAACF,IACtC,QAAQ,MAAM,4FAA4F,EAC1G,QAAQ,KAAK,CAAC,GAEhBG,EAAIN,EAAQ,QAAQ,IAAI,EAAGK,CAAU,CAAC,GAGxC,eAAeC,EAAID,EAAoB,CACrC,QAAQ,MAAMN,EAAQM,CAAU,CAAC,EAEjC,IAAME,EAAiBN,EAAQI,CAAU,EAEzC,QAAWG,KAAUZ,EAAYW,EAAe,SAAWA,CAAc,EACvE,GAAI,CACF,QAAQ,IAAI,cAAcC,EAAO,UAAU,EAAE,EAC7C,MAAMb,EAAkBa,CAAM,EAC9B,QAAQ,IAAI,MAAM,CACpB,OAASC,EAAK,CACZ,QAAQ,MAAMA,CAAG,EACjB,QAAQ,KAAK,CAAC,CAChB,CAEJ","names":["fileURLToPath","getFilename","__filename","getFilename","generateEndpoints","parseConfig","program","createRequire","dirname","resolve","require","__filename","ts","meta","configFile","run","unparsedConfig","config","err"]}
|
package/lib/index.js
CHANGED
|
@@ -39,6 +39,7 @@ var import_node_module = require("node:module");
|
|
|
39
39
|
var import_node_path3 = __toESM(require("node:path"));
|
|
40
40
|
|
|
41
41
|
// src/generate.ts
|
|
42
|
+
var import_lodash = __toESM(require("lodash.camelcase"));
|
|
42
43
|
var import_node_path2 = __toESM(require("node:path"));
|
|
43
44
|
var import_generate3 = __toESM(require("oazapfts/generate"));
|
|
44
45
|
var import_typescript4 = __toESM(require("typescript"));
|
|
@@ -177,10 +178,7 @@ function generateEndpointDefinition({
|
|
|
177
178
|
objectProperties.push(
|
|
178
179
|
factory.createPropertyAssignment(
|
|
179
180
|
factory.createIdentifier(type === "query" ? "providesTags" : "invalidatesTags"),
|
|
180
|
-
factory.createArrayLiteralExpression(
|
|
181
|
-
tags.map((tag) => factory.createStringLiteral(tag)),
|
|
182
|
-
false
|
|
183
|
-
)
|
|
181
|
+
factory.createArrayLiteralExpression(tags.map((tag) => factory.createStringLiteral(tag), false))
|
|
184
182
|
)
|
|
185
183
|
);
|
|
186
184
|
}
|
|
@@ -382,10 +380,7 @@ var generateReactHooks = ({
|
|
|
382
380
|
// src/generate.ts
|
|
383
381
|
var generatedApiName = "injectedRtkApi";
|
|
384
382
|
var v3DocCache = {};
|
|
385
|
-
|
|
386
|
-
IUseFetcherArgs: "IUseFetcherArgs"
|
|
387
|
-
});
|
|
388
|
-
function defaultIsDataResponse(code, includeDefault, response, allResponses) {
|
|
383
|
+
function defaultIsDataResponse(code, includeDefault) {
|
|
389
384
|
if (includeDefault && code === "default") {
|
|
390
385
|
return true;
|
|
391
386
|
}
|
|
@@ -396,8 +391,7 @@ function getOperationName2({ verb, path: path4, operation }) {
|
|
|
396
391
|
return (0, import_generate3.getOperationName)(verb, path4, operation.operationId);
|
|
397
392
|
}
|
|
398
393
|
function getTags({ verb, pathItem }) {
|
|
399
|
-
|
|
400
|
-
return tags.map((tag) => tag.toString());
|
|
394
|
+
return verb ? pathItem[verb]?.tags || [] : [];
|
|
401
395
|
}
|
|
402
396
|
function patternMatches(pattern) {
|
|
403
397
|
const filters = Array.isArray(pattern) ? pattern : [pattern];
|
|
@@ -416,6 +410,26 @@ function operationMatches(pattern) {
|
|
|
416
410
|
return checkMatch(operationName, operationDefinition);
|
|
417
411
|
};
|
|
418
412
|
}
|
|
413
|
+
function argumentMatches(pattern) {
|
|
414
|
+
const checkMatch = typeof pattern === "function" ? pattern : patternMatches(pattern);
|
|
415
|
+
return function matcher(argumentDefinition) {
|
|
416
|
+
if (!pattern || argumentDefinition.in === "path") return true;
|
|
417
|
+
const argumentName = argumentDefinition.name;
|
|
418
|
+
return checkMatch(argumentName, argumentDefinition);
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
function withQueryComment(node, def, hasTrailingNewLine) {
|
|
422
|
+
const comment = def.origin === "param" ? def.param.description : def.body.description;
|
|
423
|
+
if (comment) {
|
|
424
|
+
return import_typescript4.default.addSyntheticLeadingComment(
|
|
425
|
+
node,
|
|
426
|
+
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
427
|
+
`* ${comment} `,
|
|
428
|
+
hasTrailingNewLine
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
return node;
|
|
432
|
+
}
|
|
419
433
|
function getOverrides(operation, endpointOverrides) {
|
|
420
434
|
return endpointOverrides?.find((override) => operationMatches(override.pattern)(operation));
|
|
421
435
|
}
|
|
@@ -534,109 +548,6 @@ async function generateApi(spec, {
|
|
|
534
548
|
}
|
|
535
549
|
return [...allTagTypes];
|
|
536
550
|
}
|
|
537
|
-
function generateQueryArgType(operationDefinition) {
|
|
538
|
-
const {
|
|
539
|
-
operation: { parameters = [], requestBody }
|
|
540
|
-
} = operationDefinition;
|
|
541
|
-
const properties = [];
|
|
542
|
-
if (requestBody && !(0, import_generate3.isReference)(requestBody)) {
|
|
543
|
-
const bodySchema = requestBody.content?.["application/json"]?.schema;
|
|
544
|
-
if (bodySchema) {
|
|
545
|
-
properties.push(
|
|
546
|
-
factory.createPropertySignature(
|
|
547
|
-
void 0,
|
|
548
|
-
factory.createIdentifier("body"),
|
|
549
|
-
void 0,
|
|
550
|
-
factory.createTypeReferenceNode(factory.createIdentifier("any"), void 0)
|
|
551
|
-
)
|
|
552
|
-
);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
const queryParams = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "query");
|
|
556
|
-
if (queryParams.length > 0) {
|
|
557
|
-
properties.push(
|
|
558
|
-
factory.createPropertySignature(
|
|
559
|
-
void 0,
|
|
560
|
-
factory.createIdentifier("params"),
|
|
561
|
-
void 0,
|
|
562
|
-
factory.createTypeLiteralNode(
|
|
563
|
-
queryParams.map(
|
|
564
|
-
(param) => factory.createPropertySignature(
|
|
565
|
-
void 0,
|
|
566
|
-
factory.createIdentifier(param.name),
|
|
567
|
-
factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
568
|
-
factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword)
|
|
569
|
-
)
|
|
570
|
-
)
|
|
571
|
-
)
|
|
572
|
-
)
|
|
573
|
-
);
|
|
574
|
-
}
|
|
575
|
-
const headerParams = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "header");
|
|
576
|
-
if (headerParams.length > 0) {
|
|
577
|
-
properties.push(
|
|
578
|
-
factory.createPropertySignature(
|
|
579
|
-
void 0,
|
|
580
|
-
factory.createIdentifier("headers"),
|
|
581
|
-
void 0,
|
|
582
|
-
factory.createTypeLiteralNode(
|
|
583
|
-
headerParams.map(
|
|
584
|
-
(param) => factory.createPropertySignature(
|
|
585
|
-
void 0,
|
|
586
|
-
factory.createIdentifier(param.name),
|
|
587
|
-
factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
588
|
-
factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword)
|
|
589
|
-
)
|
|
590
|
-
)
|
|
591
|
-
)
|
|
592
|
-
)
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
return factory.createTypeLiteralNode([
|
|
596
|
-
factory.createPropertySignature(
|
|
597
|
-
void 0,
|
|
598
|
-
factory.createIdentifier("variables"),
|
|
599
|
-
void 0,
|
|
600
|
-
factory.createTypeLiteralNode(properties)
|
|
601
|
-
),
|
|
602
|
-
factory.createPropertySignature(
|
|
603
|
-
void 0,
|
|
604
|
-
factory.createIdentifier("fetchOptions"),
|
|
605
|
-
factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
606
|
-
factory.createTypeReferenceNode(factory.createIdentifier("any"), void 0)
|
|
607
|
-
)
|
|
608
|
-
]);
|
|
609
|
-
}
|
|
610
|
-
function generateQueryArgDefinitions(operationDefinition) {
|
|
611
|
-
const {
|
|
612
|
-
operation: { parameters = [], requestBody }
|
|
613
|
-
} = operationDefinition;
|
|
614
|
-
const queryArg = {};
|
|
615
|
-
if (requestBody && !(0, import_generate3.isReference)(requestBody)) {
|
|
616
|
-
const bodySchema = requestBody.content?.["application/json"]?.schema;
|
|
617
|
-
if (bodySchema) {
|
|
618
|
-
queryArg["body"] = {
|
|
619
|
-
name: "body",
|
|
620
|
-
originalName: "body",
|
|
621
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.AnyKeyword),
|
|
622
|
-
required: true,
|
|
623
|
-
origin: "body",
|
|
624
|
-
body: requestBody
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
parameters.filter((p) => !(0, import_generate3.isReference)(p)).forEach((param) => {
|
|
629
|
-
queryArg[param.name] = {
|
|
630
|
-
name: param.name,
|
|
631
|
-
originalName: param.name,
|
|
632
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
633
|
-
required: param.required || false,
|
|
634
|
-
origin: "param",
|
|
635
|
-
param
|
|
636
|
-
};
|
|
637
|
-
});
|
|
638
|
-
return queryArg;
|
|
639
|
-
}
|
|
640
551
|
function generateEndpoint({
|
|
641
552
|
operationDefinition,
|
|
642
553
|
overrides
|
|
@@ -655,39 +566,138 @@ async function generateApi(spec, {
|
|
|
655
566
|
let ResponseType = factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UnknownKeyword);
|
|
656
567
|
if (returnsJson) {
|
|
657
568
|
const returnTypes = Object.entries(responses || {}).map(
|
|
658
|
-
([code, response]) =>
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
569
|
+
([code, response]) => [
|
|
570
|
+
code,
|
|
571
|
+
apiGen.resolve(response),
|
|
572
|
+
apiGen.getTypeFromResponse(response, "readOnly") || factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)
|
|
573
|
+
]
|
|
574
|
+
).filter(
|
|
575
|
+
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
576
|
+
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
577
|
+
([code, response, type]) => import_typescript4.default.addSyntheticLeadingComment(
|
|
578
|
+
{ ...type },
|
|
579
|
+
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
580
|
+
`* status ${code} ${response.description} `,
|
|
581
|
+
false
|
|
582
|
+
)
|
|
583
|
+
);
|
|
584
|
+
if (returnTypes.length > 0) {
|
|
663
585
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
664
586
|
}
|
|
665
587
|
}
|
|
666
|
-
const
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
588
|
+
const ResponseTypeName = factory.createTypeReferenceNode(
|
|
589
|
+
registerInterface(
|
|
590
|
+
factory.createTypeAliasDeclaration(
|
|
591
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
592
|
+
capitalize(operationName + operationNameSuffix + responseSuffix),
|
|
593
|
+
void 0,
|
|
594
|
+
ResponseType
|
|
595
|
+
)
|
|
596
|
+
).name
|
|
597
|
+
);
|
|
598
|
+
const operationParameters = apiGen.resolveArray(operation.parameters);
|
|
599
|
+
const pathItemParameters = apiGen.resolveArray(pathItem.parameters).filter((pp) => !operationParameters.some((op) => op.name === pp.name && op.in === pp.in));
|
|
600
|
+
const parameters = (0, import_generate3.supportDeepObjects)([...pathItemParameters, ...operationParameters]).filter(
|
|
601
|
+
argumentMatches(overrides?.parameterFilter)
|
|
602
|
+
);
|
|
603
|
+
const allNames = parameters.map((p) => p.name);
|
|
604
|
+
const queryArg = {};
|
|
605
|
+
function generateName(name, potentialPrefix) {
|
|
606
|
+
const isPureSnakeCase = /^[a-zA-Z][a-zA-Z0-9_]*$/.test(name);
|
|
607
|
+
const hasNamingConflict = allNames.filter((n) => n === name).length > 1;
|
|
608
|
+
if (hasNamingConflict) {
|
|
609
|
+
name = `${potentialPrefix}_${name}`;
|
|
610
|
+
}
|
|
611
|
+
const camelCaseName = (0, import_lodash.default)(name);
|
|
612
|
+
if (isPureSnakeCase && !allNames.includes(camelCaseName)) {
|
|
613
|
+
name = camelCaseName;
|
|
614
|
+
}
|
|
615
|
+
while (name in queryArg) {
|
|
616
|
+
name = `_${name}`;
|
|
617
|
+
}
|
|
618
|
+
return name;
|
|
619
|
+
}
|
|
620
|
+
for (const param of parameters) {
|
|
621
|
+
const name = generateName(param.name, param.in);
|
|
622
|
+
queryArg[name] = {
|
|
623
|
+
origin: "param",
|
|
624
|
+
name,
|
|
625
|
+
originalName: param.name,
|
|
626
|
+
type: apiGen.getTypeFromSchema((0, import_generate3.isReference)(param) ? param : param.schema, void 0, "writeOnly"),
|
|
627
|
+
required: param.required,
|
|
628
|
+
param
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
if (requestBody) {
|
|
632
|
+
const body = apiGen.resolve(requestBody);
|
|
633
|
+
const schema = apiGen.getSchemaFromContent(body.content);
|
|
634
|
+
const type = apiGen.getTypeFromSchema(schema);
|
|
635
|
+
const schemaName = (0, import_lodash.default)(
|
|
636
|
+
type.name || (0, import_generate3.getReferenceName)(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
637
|
+
);
|
|
638
|
+
const name = generateName(schemaName in queryArg ? "body" : schemaName, "body");
|
|
639
|
+
queryArg[name] = {
|
|
640
|
+
origin: "body",
|
|
641
|
+
name,
|
|
642
|
+
originalName: schemaName,
|
|
643
|
+
type: apiGen.getTypeFromSchema(schema, void 0, "writeOnly"),
|
|
644
|
+
required: true,
|
|
645
|
+
body
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
const propertyName = (name) => {
|
|
649
|
+
if (typeof name === "string") {
|
|
650
|
+
return (0, import_generate3.isValidIdentifier)(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
651
|
+
}
|
|
652
|
+
return name;
|
|
653
|
+
};
|
|
654
|
+
const queryArgValues = Object.values(queryArg);
|
|
655
|
+
const isFlatArg = flattenArg && queryArgValues.length === 1;
|
|
656
|
+
const QueryArg = factory.createTypeReferenceNode(
|
|
657
|
+
registerInterface(
|
|
658
|
+
factory.createTypeAliasDeclaration(
|
|
659
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
660
|
+
capitalize(operationName + operationNameSuffix + argSuffix),
|
|
661
|
+
void 0,
|
|
662
|
+
queryArgValues.length > 0 ? isFlatArg ? withQueryComment(
|
|
663
|
+
factory.createUnionTypeNode([
|
|
664
|
+
queryArgValues[0].type,
|
|
665
|
+
...!queryArgValues[0].required ? [factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)] : []
|
|
666
|
+
]),
|
|
667
|
+
queryArgValues[0],
|
|
668
|
+
false
|
|
669
|
+
) : factory.createTypeLiteralNode(
|
|
670
|
+
queryArgValues.map(
|
|
671
|
+
(def) => withQueryComment(
|
|
672
|
+
factory.createPropertySignature(
|
|
673
|
+
void 0,
|
|
674
|
+
propertyName(def.name),
|
|
675
|
+
(0, import_generate3.createQuestionToken)(!def.required),
|
|
676
|
+
def.type
|
|
677
|
+
),
|
|
678
|
+
def,
|
|
679
|
+
true
|
|
680
|
+
)
|
|
681
|
+
)
|
|
682
|
+
) : factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.VoidKeyword)
|
|
683
|
+
)
|
|
684
|
+
).name
|
|
672
685
|
);
|
|
673
|
-
const queryArgDefinitions = generateQueryArgDefinitions(operationDefinition);
|
|
674
|
-
const extraEndpointsProps = isQuery2 ? generateQueryEndpointProps({ operationDefinition }) : generateMutationEndpointProps({ operationDefinition });
|
|
675
686
|
return generateEndpointDefinition({
|
|
676
|
-
operationName,
|
|
687
|
+
operationName: operationNameSuffix ? capitalize(operationName + operationNameSuffix) : operationName,
|
|
677
688
|
type: isQuery2 ? "query" : "mutation",
|
|
678
|
-
Response,
|
|
679
|
-
QueryArg
|
|
689
|
+
Response: ResponseTypeName,
|
|
690
|
+
QueryArg,
|
|
680
691
|
queryFn: generateQueryFn({
|
|
681
692
|
operationDefinition,
|
|
682
|
-
queryArg
|
|
683
|
-
isFlatArg: flattenArg,
|
|
693
|
+
queryArg,
|
|
684
694
|
isQuery: isQuery2,
|
|
695
|
+
isFlatArg,
|
|
685
696
|
encodePathParams,
|
|
686
697
|
encodeQueryParams
|
|
687
698
|
}),
|
|
688
|
-
extraEndpointsProps,
|
|
689
|
-
tags
|
|
690
|
-
endpointBuilder
|
|
699
|
+
extraEndpointsProps: isQuery2 ? generateQueryEndpointProps({ operationDefinition }) : generateMutationEndpointProps({ operationDefinition }),
|
|
700
|
+
tags
|
|
691
701
|
});
|
|
692
702
|
}
|
|
693
703
|
function generateQueryFn({
|
|
@@ -698,91 +708,60 @@ async function generateApi(spec, {
|
|
|
698
708
|
encodePathParams: encodePathParams2,
|
|
699
709
|
encodeQueryParams: encodeQueryParams2
|
|
700
710
|
}) {
|
|
701
|
-
const {
|
|
702
|
-
|
|
703
|
-
path: path4,
|
|
704
|
-
verb
|
|
705
|
-
} = operationDefinition;
|
|
706
|
-
const bodyParameter = requestBody && !(0, import_generate3.isReference)(requestBody) ? requestBody.content?.["application/json"]?.schema : void 0;
|
|
707
|
-
const bodyArg = bodyParameter ? queryArg["body"] : void 0;
|
|
708
|
-
const pathParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "path").map((param) => ({
|
|
709
|
-
name: param.name,
|
|
710
|
-
originalName: param.name,
|
|
711
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
712
|
-
required: param.required,
|
|
713
|
-
param,
|
|
714
|
-
origin: "param"
|
|
715
|
-
}));
|
|
716
|
-
const queryParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "query").map((param) => ({
|
|
717
|
-
name: param.name,
|
|
718
|
-
originalName: param.name,
|
|
719
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
720
|
-
required: param.required,
|
|
721
|
-
param,
|
|
722
|
-
origin: "param"
|
|
723
|
-
}));
|
|
724
|
-
const headerParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "header").map((param) => ({
|
|
725
|
-
name: param.name.includes("-") ? `'${param.name}'` : param.name,
|
|
726
|
-
originalName: param.name,
|
|
727
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
728
|
-
required: param.required,
|
|
729
|
-
param,
|
|
730
|
-
origin: "param"
|
|
731
|
-
}));
|
|
711
|
+
const { path: path4, verb } = operationDefinition;
|
|
712
|
+
const bodyParameter = Object.values(queryArg).find((def) => def.origin === "body");
|
|
732
713
|
const rootObject = factory.createIdentifier("queryArg");
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
"url",
|
|
736
|
-
generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams2)
|
|
737
|
-
),
|
|
738
|
-
factory.createPropertyAssignment("method", factory.createStringLiteral(verb.toUpperCase()))
|
|
739
|
-
];
|
|
740
|
-
if (bodyArg) {
|
|
741
|
-
objectProperties.push(
|
|
742
|
-
factory.createPropertyAssignment(
|
|
743
|
-
"body",
|
|
744
|
-
factory.createPropertyAccessExpression(
|
|
745
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
|
|
746
|
-
factory.createIdentifier("body")
|
|
747
|
-
)
|
|
748
|
-
)
|
|
749
|
-
);
|
|
714
|
+
function pickParams(paramIn) {
|
|
715
|
+
return Object.values(queryArg).filter((def) => def.origin === "param" && def.param.in === paramIn);
|
|
750
716
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
)
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
)
|
|
770
|
-
)
|
|
717
|
+
function createObjectLiteralProperty(parameters, propertyName) {
|
|
718
|
+
if (parameters.length === 0) return void 0;
|
|
719
|
+
const properties = parameters.map((param) => {
|
|
720
|
+
const value = isFlatArg ? rootObject : accessProperty(rootObject, param.name);
|
|
721
|
+
const encodedValue = encodeQueryParams2 && param.param?.in === "query" ? factory.createConditionalExpression(
|
|
722
|
+
value,
|
|
723
|
+
void 0,
|
|
724
|
+
factory.createCallExpression(factory.createIdentifier("encodeURIComponent"), void 0, [
|
|
725
|
+
factory.createCallExpression(factory.createIdentifier("String"), void 0, [value])
|
|
726
|
+
]),
|
|
727
|
+
void 0,
|
|
728
|
+
factory.createIdentifier("undefined")
|
|
729
|
+
) : value;
|
|
730
|
+
return (0, import_generate3.createPropertyAssignment)(param.originalName, encodedValue);
|
|
731
|
+
});
|
|
732
|
+
return factory.createPropertyAssignment(
|
|
733
|
+
factory.createIdentifier(propertyName),
|
|
734
|
+
factory.createObjectLiteralExpression(properties, true)
|
|
771
735
|
);
|
|
772
736
|
}
|
|
773
|
-
objectProperties.push(
|
|
774
|
-
factory.createPropertyAssignment(
|
|
775
|
-
"fetchOptions",
|
|
776
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("fetchOptions"))
|
|
777
|
-
)
|
|
778
|
-
);
|
|
779
737
|
return factory.createArrowFunction(
|
|
780
738
|
void 0,
|
|
781
739
|
void 0,
|
|
782
|
-
[factory.createParameterDeclaration(void 0, void 0, rootObject)],
|
|
740
|
+
Object.keys(queryArg).length ? [factory.createParameterDeclaration(void 0, void 0, rootObject, void 0, void 0, void 0)] : [],
|
|
783
741
|
void 0,
|
|
784
742
|
factory.createToken(import_typescript4.default.SyntaxKind.EqualsGreaterThanToken),
|
|
785
|
-
factory.createParenthesizedExpression(
|
|
743
|
+
factory.createParenthesizedExpression(
|
|
744
|
+
factory.createObjectLiteralExpression(
|
|
745
|
+
[
|
|
746
|
+
factory.createPropertyAssignment(
|
|
747
|
+
factory.createIdentifier("url"),
|
|
748
|
+
generatePathExpression(path4, pickParams("path"), rootObject, isFlatArg, encodePathParams2)
|
|
749
|
+
),
|
|
750
|
+
isQuery2 && verb.toUpperCase() === "GET" ? void 0 : factory.createPropertyAssignment(
|
|
751
|
+
factory.createIdentifier("method"),
|
|
752
|
+
factory.createStringLiteral(verb.toUpperCase())
|
|
753
|
+
),
|
|
754
|
+
bodyParameter === void 0 ? void 0 : factory.createPropertyAssignment(
|
|
755
|
+
factory.createIdentifier("body"),
|
|
756
|
+
isFlatArg ? rootObject : factory.createPropertyAccessExpression(rootObject, factory.createIdentifier(bodyParameter.name))
|
|
757
|
+
),
|
|
758
|
+
createObjectLiteralProperty(pickParams("cookie"), "cookies"),
|
|
759
|
+
createObjectLiteralProperty(pickParams("header"), "headers"),
|
|
760
|
+
createObjectLiteralProperty(pickParams("query"), "params")
|
|
761
|
+
].filter(removeUndefined),
|
|
762
|
+
false
|
|
763
|
+
)
|
|
764
|
+
)
|
|
786
765
|
);
|
|
787
766
|
}
|
|
788
767
|
function generateQueryEndpointProps({}) {
|
|
@@ -792,6 +771,9 @@ async function generateApi(spec, {
|
|
|
792
771
|
return {};
|
|
793
772
|
}
|
|
794
773
|
}
|
|
774
|
+
function accessProperty(rootObject, propertyName) {
|
|
775
|
+
return (0, import_generate3.isValidIdentifier)(propertyName) ? factory.createPropertyAccessExpression(rootObject, factory.createIdentifier(propertyName)) : factory.createElementAccessExpression(rootObject, factory.createStringLiteral(propertyName));
|
|
776
|
+
}
|
|
795
777
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
796
778
|
const expressions = [];
|
|
797
779
|
const head = path4.replace(/\{(.*?)}(.*?)(?=\{|$)/g, (_, expression, literal) => {
|
|
@@ -803,21 +785,18 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
803
785
|
return "";
|
|
804
786
|
});
|
|
805
787
|
return expressions.length ? factory.createTemplateExpression(
|
|
806
|
-
factory.createTemplateHead(head
|
|
788
|
+
factory.createTemplateHead(head),
|
|
807
789
|
expressions.map(([prop, literal], index) => {
|
|
808
|
-
const value =
|
|
809
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
|
|
810
|
-
factory.createIdentifier(prop)
|
|
811
|
-
);
|
|
790
|
+
const value = isFlatArg ? rootObject : accessProperty(rootObject, prop);
|
|
812
791
|
const encodedValue = encodePathParams ? factory.createCallExpression(factory.createIdentifier("encodeURIComponent"), void 0, [
|
|
813
792
|
factory.createCallExpression(factory.createIdentifier("String"), void 0, [value])
|
|
814
793
|
]) : value;
|
|
815
794
|
return factory.createTemplateSpan(
|
|
816
795
|
encodedValue,
|
|
817
|
-
index === expressions.length - 1 ? factory.createTemplateTail(literal
|
|
796
|
+
index === expressions.length - 1 ? factory.createTemplateTail(literal) : factory.createTemplateMiddle(literal)
|
|
818
797
|
);
|
|
819
798
|
})
|
|
820
|
-
) : factory.
|
|
799
|
+
) : factory.createNoSubstitutionTemplateLiteral(head);
|
|
821
800
|
}
|
|
822
801
|
|
|
823
802
|
// src/index.ts
|