@acrool/rtk-query-codegen-openapi 0.0.1

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/index.mjs ADDED
@@ -0,0 +1,850 @@
1
+ // node_modules/tsup/assets/esm_shims.js
2
+ import { fileURLToPath } from "node:url";
3
+ var getFilename = () => fileURLToPath(import.meta.url);
4
+ var __filename = /* @__PURE__ */ getFilename();
5
+
6
+ // src/index.ts
7
+ import fs from "node:fs";
8
+ import { createRequire } from "node:module";
9
+ import path3 from "node:path";
10
+
11
+ // src/generate.ts
12
+ import path2 from "node:path";
13
+ import ApiGenerator, {
14
+ getOperationName as _getOperationName,
15
+ isReference,
16
+ isValidIdentifier
17
+ } from "oazapfts/generate";
18
+ import ts4 from "typescript";
19
+
20
+ // src/utils/factory.ts
21
+ import ts from "typescript";
22
+ import semver from "semver";
23
+ var originalFactory = ts.factory;
24
+ function createImportSpecifier(propertyName, name) {
25
+ if (semver.satisfies(ts.version, ">= 4.5"))
26
+ return originalFactory.createImportSpecifier(false, propertyName, name);
27
+ return originalFactory.createImportSpecifier(propertyName, name);
28
+ }
29
+ function createExportSpecifier(propertyName, name) {
30
+ if (semver.satisfies(ts.version, ">= 4.5"))
31
+ return originalFactory.createExportSpecifier(false, propertyName, name);
32
+ return originalFactory.createExportSpecifier(propertyName, name);
33
+ }
34
+ var factory = {
35
+ ...originalFactory,
36
+ createImportSpecifier,
37
+ createExportSpecifier
38
+ };
39
+
40
+ // src/codegen.ts
41
+ import ts2 from "typescript";
42
+ var defaultEndpointBuilder = factory.createIdentifier("build");
43
+ function generateObjectProperties(obj) {
44
+ return Object.entries(obj).filter(([_, v]) => v).map(([k, v]) => factory.createPropertyAssignment(factory.createIdentifier(k), v));
45
+ }
46
+ function generateImportNode(pkg, namedImports, defaultImportName) {
47
+ return factory.createImportDeclaration(
48
+ void 0,
49
+ factory.createImportClause(
50
+ false,
51
+ defaultImportName !== void 0 ? factory.createIdentifier(defaultImportName) : void 0,
52
+ factory.createNamedImports(
53
+ Object.entries(namedImports).map(
54
+ ([propertyName, name]) => factory.createImportSpecifier(
55
+ name === propertyName ? void 0 : factory.createIdentifier(propertyName),
56
+ factory.createIdentifier(name)
57
+ )
58
+ )
59
+ )
60
+ ),
61
+ factory.createStringLiteral(pkg)
62
+ );
63
+ }
64
+ function generateCreateApiCall({
65
+ endpointBuilder = defaultEndpointBuilder,
66
+ endpointDefinitions,
67
+ tag
68
+ }) {
69
+ const injectEndpointsObjectLiteralExpression = factory.createObjectLiteralExpression(
70
+ generateObjectProperties({
71
+ endpoints: factory.createArrowFunction(
72
+ void 0,
73
+ void 0,
74
+ [factory.createParameterDeclaration(void 0, void 0, endpointBuilder, void 0, void 0, void 0)],
75
+ void 0,
76
+ factory.createToken(ts2.SyntaxKind.EqualsGreaterThanToken),
77
+ factory.createParenthesizedExpression(endpointDefinitions)
78
+ ),
79
+ overrideExisting: factory.createFalse()
80
+ }),
81
+ true
82
+ );
83
+ if (tag) {
84
+ const enhanceEndpointsObjectLiteralExpression = factory.createObjectLiteralExpression(
85
+ [factory.createShorthandPropertyAssignment(factory.createIdentifier("addTagTypes"), void 0)],
86
+ true
87
+ );
88
+ return factory.createVariableStatement(
89
+ void 0,
90
+ factory.createVariableDeclarationList(
91
+ [
92
+ factory.createVariableDeclaration(
93
+ factory.createIdentifier("injectedRtkApi"),
94
+ void 0,
95
+ void 0,
96
+ factory.createCallExpression(
97
+ factory.createPropertyAccessExpression(
98
+ factory.createCallExpression(
99
+ factory.createPropertyAccessExpression(
100
+ factory.createIdentifier("api"),
101
+ factory.createIdentifier("enhanceEndpoints")
102
+ ),
103
+ void 0,
104
+ [enhanceEndpointsObjectLiteralExpression]
105
+ ),
106
+ factory.createIdentifier("injectEndpoints")
107
+ ),
108
+ void 0,
109
+ [injectEndpointsObjectLiteralExpression]
110
+ )
111
+ )
112
+ ],
113
+ ts2.NodeFlags.Const
114
+ )
115
+ );
116
+ }
117
+ return factory.createVariableStatement(
118
+ void 0,
119
+ factory.createVariableDeclarationList(
120
+ [
121
+ factory.createVariableDeclaration(
122
+ factory.createIdentifier("injectedRtkApi"),
123
+ void 0,
124
+ void 0,
125
+ factory.createCallExpression(
126
+ factory.createPropertyAccessExpression(
127
+ factory.createIdentifier("api"),
128
+ factory.createIdentifier("injectEndpoints")
129
+ ),
130
+ void 0,
131
+ [injectEndpointsObjectLiteralExpression]
132
+ )
133
+ )
134
+ ],
135
+ ts2.NodeFlags.Const
136
+ )
137
+ );
138
+ }
139
+ function generateEndpointDefinition({
140
+ operationName,
141
+ type,
142
+ Response,
143
+ QueryArg,
144
+ queryFn,
145
+ endpointBuilder = defaultEndpointBuilder,
146
+ extraEndpointsProps,
147
+ tags
148
+ }) {
149
+ const objectProperties = generateObjectProperties({ query: queryFn, ...extraEndpointsProps });
150
+ if (tags.length > 0) {
151
+ objectProperties.push(
152
+ factory.createPropertyAssignment(
153
+ factory.createIdentifier(type === "query" ? "providesTags" : "invalidatesTags"),
154
+ factory.createArrayLiteralExpression(
155
+ tags.map((tag) => factory.createStringLiteral(tag)),
156
+ false
157
+ )
158
+ )
159
+ );
160
+ }
161
+ return factory.createPropertyAssignment(
162
+ factory.createIdentifier(operationName),
163
+ factory.createCallExpression(
164
+ factory.createPropertyAccessExpression(endpointBuilder, factory.createIdentifier(type)),
165
+ [Response, QueryArg],
166
+ [factory.createObjectLiteralExpression(objectProperties, true)]
167
+ )
168
+ );
169
+ }
170
+ function generateTagTypes({ addTagTypes }) {
171
+ return factory.createVariableStatement(
172
+ [factory.createModifier(ts2.SyntaxKind.ExportKeyword)],
173
+ factory.createVariableDeclarationList(
174
+ [
175
+ factory.createVariableDeclaration(
176
+ factory.createIdentifier("addTagTypes"),
177
+ void 0,
178
+ void 0,
179
+ factory.createAsExpression(
180
+ factory.createArrayLiteralExpression(
181
+ addTagTypes.map((tagType) => factory.createStringLiteral(tagType)),
182
+ true
183
+ ),
184
+ factory.createTypeReferenceNode(factory.createIdentifier("const"), void 0)
185
+ )
186
+ )
187
+ ],
188
+ ts2.NodeFlags.Const
189
+ )
190
+ );
191
+ }
192
+
193
+ // src/generators/react-hooks.ts
194
+ import ts3 from "typescript";
195
+ import { getOperationName } from "oazapfts/generate";
196
+
197
+ // src/utils/capitalize.ts
198
+ function capitalize(str) {
199
+ return str.replace(str[0], str[0].toUpperCase());
200
+ }
201
+
202
+ // src/types.ts
203
+ var operationKeys = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
204
+
205
+ // src/utils/getOperationDefinitions.ts
206
+ function getOperationDefinitions(v3Doc) {
207
+ return Object.entries(v3Doc.paths).flatMap(
208
+ ([path4, pathItem]) => !pathItem ? [] : Object.entries(pathItem).filter(
209
+ (arg) => operationKeys.includes(arg[0])
210
+ ).map(([verb, operation]) => ({
211
+ path: path4,
212
+ verb,
213
+ pathItem,
214
+ operation
215
+ }))
216
+ );
217
+ }
218
+
219
+ // src/utils/getV3Doc.ts
220
+ import SwaggerParser from "@apidevtools/swagger-parser";
221
+ import converter from "swagger2openapi";
222
+ async function getV3Doc(spec, httpResolverOptions) {
223
+ const doc = await SwaggerParser.bundle(spec, {
224
+ resolve: {
225
+ http: httpResolverOptions
226
+ }
227
+ });
228
+ const isOpenApiV3 = "openapi" in doc && doc.openapi.startsWith("3");
229
+ if (isOpenApiV3) {
230
+ return doc;
231
+ } else {
232
+ const result = await converter.convertObj(doc, {});
233
+ return result.openapi;
234
+ }
235
+ }
236
+
237
+ // src/utils/isQuery.ts
238
+ function isQuery(verb, overrides) {
239
+ if (overrides?.type) {
240
+ return overrides.type === "query";
241
+ }
242
+ return verb === "get";
243
+ }
244
+
245
+ // src/utils/isValidUrl.ts
246
+ function isValidUrl(string) {
247
+ try {
248
+ new URL(string);
249
+ } catch (_) {
250
+ return false;
251
+ }
252
+ return true;
253
+ }
254
+
255
+ // src/utils/prettier.ts
256
+ import path from "node:path";
257
+ import prettier from "prettier";
258
+ var EXTENSION_TO_PARSER = {
259
+ ts: "typescript",
260
+ tsx: "typescript",
261
+ js: "babel",
262
+ jsx: "babel",
263
+ "js.flow": "flow",
264
+ flow: "flow",
265
+ gql: "graphql",
266
+ graphql: "graphql",
267
+ css: "scss",
268
+ scss: "scss",
269
+ less: "scss",
270
+ stylus: "scss",
271
+ markdown: "markdown",
272
+ md: "markdown",
273
+ json: "json"
274
+ };
275
+ async function prettify(filePath, content, prettierConfigFile) {
276
+ let config = null;
277
+ let parser = "typescript";
278
+ if (filePath) {
279
+ const fileExtension = path.extname(filePath).slice(1);
280
+ parser = EXTENSION_TO_PARSER[fileExtension];
281
+ config = await prettier.resolveConfig(process.cwd(), {
282
+ useCache: true,
283
+ editorconfig: !prettierConfigFile,
284
+ config: prettierConfigFile
285
+ });
286
+ } else if (prettierConfigFile) {
287
+ config = await prettier.resolveConfig(process.cwd(), {
288
+ useCache: true,
289
+ config: prettierConfigFile
290
+ });
291
+ }
292
+ return prettier.format(content, {
293
+ parser,
294
+ ...config
295
+ });
296
+ }
297
+
298
+ // src/utils/removeUndefined.ts
299
+ function removeUndefined(t) {
300
+ return typeof t !== "undefined";
301
+ }
302
+
303
+ // src/generators/react-hooks.ts
304
+ var createBinding = ({
305
+ operationDefinition: { verb, path: path4, operation },
306
+ overrides,
307
+ isLazy = false
308
+ }) => factory.createBindingElement(
309
+ void 0,
310
+ void 0,
311
+ factory.createIdentifier(
312
+ `use${isLazy ? "Lazy" : ""}${capitalize(getOperationName(verb, path4, operation.operationId))}${isQuery(verb, overrides) ? "Query" : "Mutation"}`
313
+ ),
314
+ void 0
315
+ );
316
+ var getReactHookName = ({ operationDefinition, endpointOverrides, config }) => {
317
+ const overrides = getOverrides(operationDefinition, endpointOverrides);
318
+ const baseParams = {
319
+ operationDefinition,
320
+ overrides
321
+ };
322
+ const _isQuery = isQuery(operationDefinition.verb, overrides);
323
+ if (typeof config === "boolean") {
324
+ return createBinding(baseParams);
325
+ }
326
+ if (_isQuery) {
327
+ return [
328
+ ...config.queries ? [createBinding(baseParams)] : [],
329
+ ...config.lazyQueries ? [createBinding({ ...baseParams, isLazy: true })] : []
330
+ ];
331
+ }
332
+ return config.mutations ? createBinding(baseParams) : [];
333
+ };
334
+ var generateReactHooks = ({
335
+ exportName,
336
+ operationDefinitions,
337
+ endpointOverrides,
338
+ config
339
+ }) => factory.createVariableStatement(
340
+ [factory.createModifier(ts3.SyntaxKind.ExportKeyword)],
341
+ factory.createVariableDeclarationList(
342
+ [
343
+ factory.createVariableDeclaration(
344
+ factory.createObjectBindingPattern(
345
+ operationDefinitions.map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config })).flat()
346
+ ),
347
+ void 0,
348
+ void 0,
349
+ factory.createIdentifier(exportName)
350
+ )
351
+ ],
352
+ ts3.NodeFlags.Const
353
+ )
354
+ );
355
+
356
+ // src/generate.ts
357
+ var generatedApiName = "injectedRtkApi";
358
+ var v3DocCache = {};
359
+ var useFetcherImport = generateImportNode("@acrool/react-fetcher", {
360
+ IUseFetcherArgs: "IUseFetcherArgs"
361
+ });
362
+ function defaultIsDataResponse(code, includeDefault, response, allResponses) {
363
+ if (includeDefault && code === "default") {
364
+ return true;
365
+ }
366
+ const parsedCode = Number(code);
367
+ return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
368
+ }
369
+ function getOperationName2({ verb, path: path4, operation }) {
370
+ return _getOperationName(verb, path4, operation.operationId);
371
+ }
372
+ function getTags({ verb, pathItem }) {
373
+ const tags = verb && pathItem[verb]?.tags ? pathItem[verb].tags : [];
374
+ return tags.map((tag) => tag.toString());
375
+ }
376
+ function patternMatches(pattern) {
377
+ const filters = Array.isArray(pattern) ? pattern : [pattern];
378
+ return function matcher(operationName) {
379
+ if (!pattern) return true;
380
+ return filters.some(
381
+ (filter) => typeof filter === "string" ? filter === operationName : filter?.test(operationName)
382
+ );
383
+ };
384
+ }
385
+ function operationMatches(pattern) {
386
+ const checkMatch = typeof pattern === "function" ? pattern : patternMatches(pattern);
387
+ return function matcher(operationDefinition) {
388
+ if (!pattern) return true;
389
+ const operationName = getOperationName2(operationDefinition);
390
+ return checkMatch(operationName, operationDefinition);
391
+ };
392
+ }
393
+ function getOverrides(operation, endpointOverrides) {
394
+ return endpointOverrides?.find((override) => operationMatches(override.pattern)(operation));
395
+ }
396
+ async function generateApi(spec, {
397
+ apiFile,
398
+ apiImport = "api",
399
+ exportName = "enhancedApi",
400
+ argSuffix = "ApiArg",
401
+ responseSuffix = "ApiResponse",
402
+ operationNameSuffix = "",
403
+ hooks = false,
404
+ tag = false,
405
+ outputFile,
406
+ isDataResponse = defaultIsDataResponse,
407
+ filterEndpoints,
408
+ endpointOverrides,
409
+ unionUndefined,
410
+ encodePathParams = false,
411
+ encodeQueryParams = false,
412
+ flattenArg = false,
413
+ includeDefault = false,
414
+ useEnumType = false,
415
+ mergeReadWriteOnly = false,
416
+ httpResolverOptions
417
+ }) {
418
+ const v3Doc = v3DocCache[spec] ??= await getV3Doc(spec, httpResolverOptions);
419
+ const apiGen = new ApiGenerator(v3Doc, {
420
+ unionUndefined,
421
+ useEnumType,
422
+ mergeReadWriteOnly
423
+ });
424
+ if (apiGen.spec.components?.schemas) {
425
+ apiGen.preprocessComponents(apiGen.spec.components.schemas);
426
+ }
427
+ const operationDefinitions = getOperationDefinitions(v3Doc).filter(operationMatches(filterEndpoints));
428
+ const resultFile = ts4.createSourceFile(
429
+ "someFileName.ts",
430
+ "",
431
+ ts4.ScriptTarget.Latest,
432
+ /*setParentNodes*/
433
+ false,
434
+ ts4.ScriptKind.TS
435
+ );
436
+ const printer = ts4.createPrinter({ newLine: ts4.NewLineKind.LineFeed });
437
+ const interfaces = {};
438
+ function registerInterface(declaration) {
439
+ const name = declaration.name.escapedText.toString();
440
+ if (name in interfaces) {
441
+ throw new Error(`interface/type alias ${name} already registered`);
442
+ }
443
+ interfaces[name] = declaration;
444
+ return declaration;
445
+ }
446
+ if (outputFile) {
447
+ outputFile = path2.resolve(process.cwd(), outputFile);
448
+ if (apiFile.startsWith(".")) {
449
+ apiFile = path2.relative(path2.dirname(outputFile), apiFile);
450
+ apiFile = apiFile.replace(/\\/g, "/");
451
+ if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
452
+ }
453
+ }
454
+ apiFile = apiFile.replace(/\.[jt]sx?$/, "");
455
+ return printer.printNode(
456
+ ts4.EmitHint.Unspecified,
457
+ factory.createSourceFile(
458
+ [
459
+ generateImportNode(apiFile, { [apiImport]: "api" }),
460
+ ...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
461
+ generateCreateApiCall({
462
+ tag,
463
+ endpointDefinitions: factory.createObjectLiteralExpression(
464
+ operationDefinitions.map(
465
+ (operationDefinition) => generateEndpoint({
466
+ operationDefinition,
467
+ overrides: getOverrides(operationDefinition, endpointOverrides)
468
+ })
469
+ ),
470
+ true
471
+ )
472
+ }),
473
+ factory.createExportDeclaration(
474
+ void 0,
475
+ false,
476
+ factory.createNamedExports([
477
+ factory.createExportSpecifier(
478
+ factory.createIdentifier(generatedApiName),
479
+ factory.createIdentifier(exportName)
480
+ )
481
+ ]),
482
+ void 0
483
+ ),
484
+ ...Object.values(interfaces),
485
+ ...apiGen.aliases,
486
+ ...apiGen.enumAliases,
487
+ ...hooks ? [
488
+ generateReactHooks({
489
+ exportName: generatedApiName,
490
+ operationDefinitions,
491
+ endpointOverrides,
492
+ config: hooks
493
+ })
494
+ ] : []
495
+ ],
496
+ factory.createToken(ts4.SyntaxKind.EndOfFileToken),
497
+ ts4.NodeFlags.None
498
+ ),
499
+ resultFile
500
+ );
501
+ function extractAllTagTypes({ operationDefinitions: operationDefinitions2 }) {
502
+ const allTagTypes = /* @__PURE__ */ new Set();
503
+ for (const operationDefinition of operationDefinitions2) {
504
+ const { verb, pathItem } = operationDefinition;
505
+ for (const tag2 of getTags({ verb, pathItem })) {
506
+ allTagTypes.add(tag2);
507
+ }
508
+ }
509
+ return [...allTagTypes];
510
+ }
511
+ function generateQueryArgType(operationDefinition) {
512
+ const {
513
+ operation: { parameters = [], requestBody }
514
+ } = operationDefinition;
515
+ const properties = [];
516
+ if (requestBody && !isReference(requestBody)) {
517
+ const bodySchema = requestBody.content?.["application/json"]?.schema;
518
+ if (bodySchema) {
519
+ properties.push(
520
+ factory.createPropertySignature(
521
+ void 0,
522
+ factory.createIdentifier("body"),
523
+ void 0,
524
+ factory.createTypeReferenceNode(factory.createIdentifier("any"), void 0)
525
+ )
526
+ );
527
+ }
528
+ }
529
+ const queryParams = parameters.filter((p) => !isReference(p) && p.in === "query");
530
+ if (queryParams.length > 0) {
531
+ properties.push(
532
+ factory.createPropertySignature(
533
+ void 0,
534
+ factory.createIdentifier("params"),
535
+ void 0,
536
+ factory.createTypeLiteralNode(
537
+ queryParams.map(
538
+ (param) => factory.createPropertySignature(
539
+ void 0,
540
+ factory.createIdentifier(param.name),
541
+ factory.createToken(ts4.SyntaxKind.QuestionToken),
542
+ factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword)
543
+ )
544
+ )
545
+ )
546
+ )
547
+ );
548
+ }
549
+ const headerParams = parameters.filter((p) => !isReference(p) && p.in === "header");
550
+ if (headerParams.length > 0) {
551
+ properties.push(
552
+ factory.createPropertySignature(
553
+ void 0,
554
+ factory.createIdentifier("headers"),
555
+ void 0,
556
+ factory.createTypeLiteralNode(
557
+ headerParams.map(
558
+ (param) => factory.createPropertySignature(
559
+ void 0,
560
+ factory.createIdentifier(param.name),
561
+ factory.createToken(ts4.SyntaxKind.QuestionToken),
562
+ factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword)
563
+ )
564
+ )
565
+ )
566
+ )
567
+ );
568
+ }
569
+ return factory.createTypeLiteralNode([
570
+ factory.createPropertySignature(
571
+ void 0,
572
+ factory.createIdentifier("variables"),
573
+ void 0,
574
+ factory.createTypeLiteralNode(properties)
575
+ ),
576
+ factory.createPropertySignature(
577
+ void 0,
578
+ factory.createIdentifier("fetchOptions"),
579
+ factory.createToken(ts4.SyntaxKind.QuestionToken),
580
+ factory.createTypeReferenceNode(factory.createIdentifier("any"), void 0)
581
+ )
582
+ ]);
583
+ }
584
+ function generateQueryArgDefinitions(operationDefinition) {
585
+ const {
586
+ operation: { parameters = [], requestBody }
587
+ } = operationDefinition;
588
+ const queryArg = {};
589
+ if (requestBody && !isReference(requestBody)) {
590
+ const bodySchema = requestBody.content?.["application/json"]?.schema;
591
+ if (bodySchema) {
592
+ queryArg["body"] = {
593
+ name: "body",
594
+ originalName: "body",
595
+ type: factory.createKeywordTypeNode(ts4.SyntaxKind.AnyKeyword),
596
+ required: true,
597
+ origin: "body",
598
+ body: requestBody
599
+ };
600
+ }
601
+ }
602
+ parameters.filter((p) => !isReference(p)).forEach((param) => {
603
+ queryArg[param.name] = {
604
+ name: param.name,
605
+ originalName: param.name,
606
+ type: factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword),
607
+ required: param.required || false,
608
+ origin: "param",
609
+ param
610
+ };
611
+ });
612
+ return queryArg;
613
+ }
614
+ function generateEndpoint({
615
+ operationDefinition,
616
+ overrides
617
+ }) {
618
+ const {
619
+ verb,
620
+ path: path4,
621
+ pathItem,
622
+ operation,
623
+ operation: { responses, requestBody }
624
+ } = operationDefinition;
625
+ const operationName = getOperationName2({ verb, path: path4, operation });
626
+ const tags = tag ? getTags({ verb, pathItem }) : [];
627
+ const isQuery2 = isQuery(verb, overrides);
628
+ const returnsJson = apiGen.getResponseType(responses) === "json";
629
+ let ResponseType = factory.createKeywordTypeNode(ts4.SyntaxKind.UnknownKeyword);
630
+ if (returnsJson) {
631
+ const returnTypes = Object.entries(responses || {}).map(
632
+ ([code, response]) => isDataResponse(code, includeDefault, response, responses) && response ? apiGen.getTypeFromResponse(response) : void 0
633
+ ).filter(removeUndefined);
634
+ if (returnTypes.length === 1) {
635
+ ResponseType = returnTypes[0];
636
+ } else if (returnTypes.length > 1) {
637
+ ResponseType = factory.createUnionTypeNode(returnTypes);
638
+ }
639
+ }
640
+ const QueryArg = generateQueryArgType(operationDefinition);
641
+ const wrappedQueryArg = factory.createTypeReferenceNode(factory.createIdentifier("IUseFetcherArgs"), [QueryArg]);
642
+ const endpointBuilder = factory.createIdentifier("build");
643
+ const Response = factory.createTypeReferenceNode(
644
+ factory.createIdentifier(`${capitalize(operationName)}${responseSuffix}`),
645
+ void 0
646
+ );
647
+ const queryArgDefinitions = generateQueryArgDefinitions(operationDefinition);
648
+ const extraEndpointsProps = isQuery2 ? generateQueryEndpointProps({ operationDefinition }) : generateMutationEndpointProps({ operationDefinition });
649
+ return generateEndpointDefinition({
650
+ operationName,
651
+ type: isQuery2 ? "query" : "mutation",
652
+ Response,
653
+ QueryArg: wrappedQueryArg,
654
+ queryFn: generateQueryFn({
655
+ operationDefinition,
656
+ queryArg: queryArgDefinitions,
657
+ isFlatArg: flattenArg,
658
+ isQuery: isQuery2,
659
+ encodePathParams,
660
+ encodeQueryParams
661
+ }),
662
+ extraEndpointsProps,
663
+ tags,
664
+ endpointBuilder
665
+ });
666
+ }
667
+ function generateQueryFn({
668
+ operationDefinition,
669
+ queryArg,
670
+ isFlatArg,
671
+ isQuery: isQuery2,
672
+ encodePathParams: encodePathParams2,
673
+ encodeQueryParams: encodeQueryParams2
674
+ }) {
675
+ const {
676
+ verb,
677
+ path: path4,
678
+ operation: { parameters = [], requestBody }
679
+ } = operationDefinition;
680
+ const bodyParameter = requestBody && !isReference(requestBody) ? requestBody.content?.["application/json"]?.schema : void 0;
681
+ const bodyArg = bodyParameter ? queryArg["body"] : void 0;
682
+ const pathParameters = parameters.filter((p) => !isReference(p) && p.in === "path").map((param) => ({
683
+ name: param.name,
684
+ originalName: param.name,
685
+ type: factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword),
686
+ required: param.required,
687
+ param,
688
+ origin: "param"
689
+ }));
690
+ const queryParameters = parameters.filter((p) => !isReference(p) && p.in === "query").map((param) => ({
691
+ name: param.name,
692
+ originalName: param.name,
693
+ type: factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword),
694
+ required: param.required,
695
+ param,
696
+ origin: "param"
697
+ }));
698
+ const headerParameters = parameters.filter((p) => !isReference(p) && p.in === "header").map((param) => ({
699
+ name: param.name,
700
+ originalName: param.name,
701
+ type: factory.createKeywordTypeNode(ts4.SyntaxKind.StringKeyword),
702
+ required: param.required,
703
+ param,
704
+ origin: "param"
705
+ }));
706
+ const rootObject = factory.createIdentifier("queryArg");
707
+ const objectProperties = [
708
+ factory.createPropertyAssignment(
709
+ "url",
710
+ generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams2)
711
+ ),
712
+ factory.createPropertyAssignment("method", factory.createStringLiteral(verb.toUpperCase()))
713
+ ];
714
+ if (bodyArg) {
715
+ objectProperties.push(
716
+ factory.createPropertyAssignment(
717
+ "body",
718
+ factory.createPropertyAccessExpression(
719
+ factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
720
+ factory.createIdentifier("body")
721
+ )
722
+ )
723
+ );
724
+ }
725
+ if (queryParameters.length) {
726
+ objectProperties.push(
727
+ factory.createPropertyAssignment(
728
+ "params",
729
+ factory.createPropertyAccessExpression(
730
+ factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
731
+ factory.createIdentifier("params")
732
+ )
733
+ )
734
+ );
735
+ }
736
+ if (headerParameters.length) {
737
+ objectProperties.push(
738
+ factory.createPropertyAssignment(
739
+ "headers",
740
+ factory.createPropertyAccessExpression(
741
+ factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
742
+ factory.createIdentifier("headers")
743
+ )
744
+ )
745
+ );
746
+ }
747
+ objectProperties.push(
748
+ factory.createPropertyAssignment(
749
+ "fetchOptions",
750
+ factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("fetchOptions"))
751
+ )
752
+ );
753
+ return factory.createArrowFunction(
754
+ void 0,
755
+ void 0,
756
+ [factory.createParameterDeclaration(void 0, void 0, rootObject)],
757
+ void 0,
758
+ factory.createToken(ts4.SyntaxKind.EqualsGreaterThanToken),
759
+ factory.createParenthesizedExpression(factory.createObjectLiteralExpression(objectProperties, true))
760
+ );
761
+ }
762
+ function generateQueryEndpointProps({}) {
763
+ return {};
764
+ }
765
+ function generateMutationEndpointProps({}) {
766
+ return {};
767
+ }
768
+ }
769
+ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
770
+ const expressions = [];
771
+ const head = path4.replace(/\{(.*?)}(.*?)(?=\{|$)/g, (_, expression, literal) => {
772
+ const param = pathParameters.find((p) => p.originalName === expression);
773
+ if (!param) {
774
+ throw new Error(`path parameter ${expression} does not seem to be defined in '${path4}'!`);
775
+ }
776
+ expressions.push([param.name, literal]);
777
+ return "";
778
+ });
779
+ return expressions.length ? factory.createTemplateExpression(
780
+ factory.createTemplateHead(head, head),
781
+ expressions.map(([prop, literal], index) => {
782
+ const value = factory.createPropertyAccessExpression(
783
+ factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables")),
784
+ factory.createIdentifier(prop)
785
+ );
786
+ const encodedValue = encodePathParams ? factory.createCallExpression(factory.createIdentifier("encodeURIComponent"), void 0, [
787
+ factory.createCallExpression(factory.createIdentifier("String"), void 0, [value])
788
+ ]) : value;
789
+ return factory.createTemplateSpan(
790
+ encodedValue,
791
+ index === expressions.length - 1 ? factory.createTemplateTail(literal, literal) : factory.createTemplateMiddle(literal, literal)
792
+ );
793
+ })
794
+ ) : factory.createStringLiteral(head);
795
+ }
796
+
797
+ // src/index.ts
798
+ var require2 = createRequire(__filename);
799
+ async function generateEndpoints(options) {
800
+ const schemaLocation = options.schemaFile;
801
+ const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : path3.resolve(process.cwd(), schemaLocation);
802
+ const sourceCode = await enforceOazapftsTsVersion(async () => {
803
+ return generateApi(schemaAbsPath, options);
804
+ });
805
+ const { outputFile, prettierConfigFile } = options;
806
+ if (outputFile) {
807
+ fs.writeFileSync(
808
+ path3.resolve(process.cwd(), outputFile),
809
+ await prettify(outputFile, sourceCode, prettierConfigFile)
810
+ );
811
+ } else {
812
+ return await prettify(null, sourceCode, prettierConfigFile);
813
+ }
814
+ }
815
+ function parseConfig(fullConfig) {
816
+ const outFiles = [];
817
+ if ("outputFiles" in fullConfig) {
818
+ const { outputFiles, ...commonConfig } = fullConfig;
819
+ for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
820
+ outFiles.push({
821
+ ...commonConfig,
822
+ ...specificConfig,
823
+ outputFile
824
+ });
825
+ }
826
+ } else {
827
+ outFiles.push(fullConfig);
828
+ }
829
+ return outFiles;
830
+ }
831
+ function enforceOazapftsTsVersion(cb) {
832
+ const ozTsPath = require2.resolve("typescript", { paths: [require2.resolve("oazapfts")] });
833
+ const tsPath = require2.resolve("typescript");
834
+ const originalEntry = require2.cache[ozTsPath];
835
+ try {
836
+ require2.cache[ozTsPath] = require2.cache[tsPath];
837
+ return cb();
838
+ } finally {
839
+ if (originalEntry) {
840
+ require2.cache[ozTsPath] = originalEntry;
841
+ } else {
842
+ delete require2.cache[ozTsPath];
843
+ }
844
+ }
845
+ }
846
+ export {
847
+ generateEndpoints,
848
+ parseConfig
849
+ };
850
+ //# sourceMappingURL=index.mjs.map