@acrool/rtk-query-codegen-openapi 0.0.2-test.1 → 0.0.2-test.3
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 +195 -219
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +201 -220
- 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 +210 -258
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
|
-
apiGen.getTypeFromSchema(bodySchema)
|
|
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
|
-
param.required ? void 0 : factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
568
|
-
apiGen.getTypeFromSchema(param.schema)
|
|
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.includes("-") ? `'${param.name}'` : param.name),
|
|
587
|
-
param.required ? void 0 : factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
588
|
-
apiGen.getTypeFromSchema(param.schema)
|
|
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,42 +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
|
-
|
|
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
|
|
670
597
|
);
|
|
671
|
-
const
|
|
672
|
-
const
|
|
673
|
-
|
|
674
|
-
|
|
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
|
|
675
685
|
);
|
|
676
|
-
const queryArgDefinitions = generateQueryArgDefinitions(operationDefinition);
|
|
677
|
-
const extraEndpointsProps = isQuery2 ? generateQueryEndpointProps({ operationDefinition }) : generateMutationEndpointProps({ operationDefinition });
|
|
678
686
|
return generateEndpointDefinition({
|
|
679
|
-
operationName,
|
|
687
|
+
operationName: operationNameSuffix ? capitalize(operationName + operationNameSuffix) : operationName,
|
|
680
688
|
type: isQuery2 ? "query" : "mutation",
|
|
681
|
-
Response,
|
|
682
|
-
QueryArg
|
|
689
|
+
Response: ResponseTypeName,
|
|
690
|
+
QueryArg,
|
|
683
691
|
queryFn: generateQueryFn({
|
|
684
692
|
operationDefinition,
|
|
685
|
-
queryArg
|
|
686
|
-
isFlatArg: flattenArg,
|
|
693
|
+
queryArg,
|
|
687
694
|
isQuery: isQuery2,
|
|
695
|
+
isFlatArg,
|
|
688
696
|
encodePathParams,
|
|
689
697
|
encodeQueryParams
|
|
690
698
|
}),
|
|
691
|
-
extraEndpointsProps,
|
|
692
|
-
tags
|
|
693
|
-
endpointBuilder
|
|
699
|
+
extraEndpointsProps: isQuery2 ? generateQueryEndpointProps({ operationDefinition }) : generateMutationEndpointProps({ operationDefinition }),
|
|
700
|
+
tags
|
|
694
701
|
});
|
|
695
702
|
}
|
|
696
703
|
function generateQueryFn({
|
|
@@ -701,91 +708,60 @@ async function generateApi(spec, {
|
|
|
701
708
|
encodePathParams: encodePathParams2,
|
|
702
709
|
encodeQueryParams: encodeQueryParams2
|
|
703
710
|
}) {
|
|
704
|
-
const {
|
|
705
|
-
|
|
706
|
-
path: path4,
|
|
707
|
-
verb
|
|
708
|
-
} = operationDefinition;
|
|
709
|
-
const bodyParameter = requestBody && !(0, import_generate3.isReference)(requestBody) ? requestBody.content?.["application/json"]?.schema : void 0;
|
|
710
|
-
const bodyArg = bodyParameter ? queryArg["body"] : void 0;
|
|
711
|
-
const pathParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "path").map((param) => ({
|
|
712
|
-
name: param.name,
|
|
713
|
-
originalName: param.name,
|
|
714
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
715
|
-
required: param.required,
|
|
716
|
-
param,
|
|
717
|
-
origin: "param"
|
|
718
|
-
}));
|
|
719
|
-
const queryParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "query").map((param) => ({
|
|
720
|
-
name: param.name,
|
|
721
|
-
originalName: param.name,
|
|
722
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
723
|
-
required: param.required,
|
|
724
|
-
param,
|
|
725
|
-
origin: "param"
|
|
726
|
-
}));
|
|
727
|
-
const headerParameters = parameters.filter((p) => !(0, import_generate3.isReference)(p) && p.in === "header").map((param) => ({
|
|
728
|
-
name: param.name.includes("-") ? `'${param.name}'` : param.name,
|
|
729
|
-
originalName: param.name,
|
|
730
|
-
type: factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.StringKeyword),
|
|
731
|
-
required: param.required,
|
|
732
|
-
param,
|
|
733
|
-
origin: "param"
|
|
734
|
-
}));
|
|
711
|
+
const { path: path4, verb } = operationDefinition;
|
|
712
|
+
const bodyParameter = Object.values(queryArg).find((def) => def.origin === "body");
|
|
735
713
|
const rootObject = factory.createIdentifier("queryArg");
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
"url",
|
|
739
|
-
generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams2)
|
|
740
|
-
),
|
|
741
|
-
factory.createPropertyAssignment("method", factory.createStringLiteral(verb.toUpperCase()))
|
|
742
|
-
];
|
|
743
|
-
if (bodyArg) {
|
|
744
|
-
objectProperties.push(
|
|
745
|
-
factory.createPropertyAssignment(
|
|
746
|
-
"body",
|
|
747
|
-
factory.createPropertyAccessExpression(
|
|
748
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
|
|
749
|
-
factory.createIdentifier("body")
|
|
750
|
-
)
|
|
751
|
-
)
|
|
752
|
-
);
|
|
714
|
+
function pickParams(paramIn) {
|
|
715
|
+
return Object.values(queryArg).filter((def) => def.origin === "param" && def.param.in === paramIn);
|
|
753
716
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
)
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
)
|
|
773
|
-
)
|
|
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)
|
|
774
735
|
);
|
|
775
736
|
}
|
|
776
|
-
objectProperties.push(
|
|
777
|
-
factory.createPropertyAssignment(
|
|
778
|
-
"fetchOptions",
|
|
779
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("fetchOptions"))
|
|
780
|
-
)
|
|
781
|
-
);
|
|
782
737
|
return factory.createArrowFunction(
|
|
783
738
|
void 0,
|
|
784
739
|
void 0,
|
|
785
|
-
[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)] : [],
|
|
786
741
|
void 0,
|
|
787
742
|
factory.createToken(import_typescript4.default.SyntaxKind.EqualsGreaterThanToken),
|
|
788
|
-
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
|
+
)
|
|
789
765
|
);
|
|
790
766
|
}
|
|
791
767
|
function generateQueryEndpointProps({}) {
|
|
@@ -795,6 +771,9 @@ async function generateApi(spec, {
|
|
|
795
771
|
return {};
|
|
796
772
|
}
|
|
797
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
|
+
}
|
|
798
777
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
799
778
|
const expressions = [];
|
|
800
779
|
const head = path4.replace(/\{(.*?)}(.*?)(?=\{|$)/g, (_, expression, literal) => {
|
|
@@ -806,21 +785,18 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
806
785
|
return "";
|
|
807
786
|
});
|
|
808
787
|
return expressions.length ? factory.createTemplateExpression(
|
|
809
|
-
factory.createTemplateHead(head
|
|
788
|
+
factory.createTemplateHead(head),
|
|
810
789
|
expressions.map(([prop, literal], index) => {
|
|
811
|
-
const value =
|
|
812
|
-
factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
|
|
813
|
-
factory.createIdentifier(prop)
|
|
814
|
-
);
|
|
790
|
+
const value = isFlatArg ? rootObject : accessProperty(rootObject, prop);
|
|
815
791
|
const encodedValue = encodePathParams ? factory.createCallExpression(factory.createIdentifier("encodeURIComponent"), void 0, [
|
|
816
792
|
factory.createCallExpression(factory.createIdentifier("String"), void 0, [value])
|
|
817
793
|
]) : value;
|
|
818
794
|
return factory.createTemplateSpan(
|
|
819
795
|
encodedValue,
|
|
820
|
-
index === expressions.length - 1 ? factory.createTemplateTail(literal
|
|
796
|
+
index === expressions.length - 1 ? factory.createTemplateTail(literal) : factory.createTemplateMiddle(literal)
|
|
821
797
|
);
|
|
822
798
|
})
|
|
823
|
-
) : factory.
|
|
799
|
+
) : factory.createNoSubstitutionTemplateLiteral(head);
|
|
824
800
|
}
|
|
825
801
|
|
|
826
802
|
// src/index.ts
|