@moccona/apicodegen 0.0.1 → 0.0.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/README.md +48 -0
- package/bin/{cli.cjs → cli.js} +411 -300
- package/npm/index.cjs +212 -108
- package/npm/index.cjs.map +1 -1
- package/npm/index.d.cts +10 -10
- package/npm/index.d.ts +10 -10
- package/npm/index.js +212 -108
- package/npm/index.js.map +1 -1
- package/npm/vite/index.d.ts +29 -0
- package/npm/vite/index.js +2276 -0
- package/npm/vite/index.js.map +1 -0
- package/package.json +27 -30
package/bin/{cli.cjs → cli.js}
RENAMED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#!/bin/env node
|
|
2
|
-
"use strict";
|
|
3
2
|
|
|
4
3
|
// src/core/base/Adaptor.ts
|
|
5
4
|
var Adapter = class {
|
|
@@ -105,7 +104,7 @@ var HttpMethods = /* @__PURE__ */ ((HttpMethods2) => {
|
|
|
105
104
|
})(HttpMethods || {});
|
|
106
105
|
|
|
107
106
|
// src/core/base/Base.ts
|
|
108
|
-
|
|
107
|
+
import { Agent, request } from "undici";
|
|
109
108
|
var Base = class _Base {
|
|
110
109
|
constructor() {
|
|
111
110
|
if (new.target === _Base) {
|
|
@@ -142,10 +141,10 @@ var Base = class _Base {
|
|
|
142
141
|
* @param [operationId] - Unique identifier for the operation.
|
|
143
142
|
* @returns - The generated function name.
|
|
144
143
|
*/
|
|
145
|
-
static pathToFnName(path, method,
|
|
146
|
-
const name = this.camelCase(this.normalize(path));
|
|
144
|
+
static pathToFnName(path, method, _operationId = "") {
|
|
145
|
+
const name = this.normalize(this.camelCase(this.normalize(path)));
|
|
147
146
|
const suffix = method ? this.capitalize(this.upperCamelCase(`using_${method}`)) : "";
|
|
148
|
-
return
|
|
147
|
+
return name + suffix;
|
|
149
148
|
}
|
|
150
149
|
/**
|
|
151
150
|
* Normalizes a string by replacing special characters and avoiding TypeScript keywords.
|
|
@@ -156,7 +155,7 @@ var Base = class _Base {
|
|
|
156
155
|
if (typescriptKeywords.has(text)) {
|
|
157
156
|
text += "_";
|
|
158
157
|
}
|
|
159
|
-
return text.replace(/[/\-_{}():\s
|
|
158
|
+
return text.replace(/[/\-_{}():\s`,*<>$#.]/gm, "_").replace(/^\d./gm, "").replaceAll("...", "");
|
|
160
159
|
}
|
|
161
160
|
/**
|
|
162
161
|
* Capitalizes the first character of a string.
|
|
@@ -191,20 +190,25 @@ var Base = class _Base {
|
|
|
191
190
|
* @returns - A promise resolving to the fetched documentation data.
|
|
192
191
|
*/
|
|
193
192
|
static async fetchDoc(url, requestInit = {}) {
|
|
194
|
-
const agent = new
|
|
195
|
-
connect: {
|
|
196
|
-
|
|
197
|
-
|
|
193
|
+
const agent = new Agent({
|
|
194
|
+
connect: { rejectUnauthorized: false }
|
|
195
|
+
});
|
|
196
|
+
const { body, statusCode } = await request(url, {
|
|
197
|
+
method: "GET",
|
|
198
|
+
dispatcher: agent,
|
|
199
|
+
...requestInit
|
|
198
200
|
});
|
|
201
|
+
if (statusCode >= 400) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`Failed to fetch OpenAPI documentation from ${url}: HTTP ${statusCode}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
199
206
|
try {
|
|
200
|
-
const { body } = await (0, import_undici.request)(url, {
|
|
201
|
-
method: "GET",
|
|
202
|
-
dispatcher: agent,
|
|
203
|
-
...requestInit
|
|
204
|
-
});
|
|
205
207
|
return body.json();
|
|
206
208
|
} catch (error) {
|
|
207
|
-
throw
|
|
209
|
+
throw new Error(
|
|
210
|
+
`Failed to parse JSON response from ${url}: ${error instanceof Error ? error.message : String(error)}`
|
|
211
|
+
);
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
214
|
/**
|
|
@@ -213,12 +217,11 @@ var Base = class _Base {
|
|
|
213
217
|
* @returns - The matched MediaTypes or null.
|
|
214
218
|
*/
|
|
215
219
|
static getMediaType(mediaType) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return;
|
|
220
|
+
const mediaTypeValues = Object.values(MediaTypes);
|
|
221
|
+
const found = mediaTypeValues.find(
|
|
222
|
+
(type) => mediaType.includes(type)
|
|
223
|
+
);
|
|
224
|
+
return found;
|
|
222
225
|
}
|
|
223
226
|
/**
|
|
224
227
|
* Checks if a schema is a valid enum type that isn't boolean.
|
|
@@ -253,17 +256,21 @@ var Base = class _Base {
|
|
|
253
256
|
* @returns - Array of unique enum schemas.
|
|
254
257
|
*/
|
|
255
258
|
static uniqueEnums(enums) {
|
|
256
|
-
const
|
|
257
|
-
for (const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
uniqueEnums_.push(enumObject);
|
|
259
|
+
const enumMap = /* @__PURE__ */ new Map();
|
|
260
|
+
for (const e of enums) {
|
|
261
|
+
const existing = enumMap.get(e.name);
|
|
262
|
+
if (existing) {
|
|
263
|
+
for (const value of e.enum) {
|
|
264
|
+
existing.add(value);
|
|
263
265
|
}
|
|
266
|
+
} else {
|
|
267
|
+
enumMap.set(e.name, new Set(e.enum));
|
|
264
268
|
}
|
|
265
269
|
}
|
|
266
|
-
return
|
|
270
|
+
return Array.from(enumMap.entries()).map(([name, values]) => ({
|
|
271
|
+
name,
|
|
272
|
+
enum: Array.from(values)
|
|
273
|
+
}));
|
|
267
274
|
}
|
|
268
275
|
/**
|
|
269
276
|
* Finds the first occurrence of a matching enum schema in an array.
|
|
@@ -280,7 +287,7 @@ var Base = class _Base {
|
|
|
280
287
|
* @returns - True if the object is a reference.
|
|
281
288
|
*/
|
|
282
289
|
static isRef(schema) {
|
|
283
|
-
return "$ref" in schema && typeof schema.$ref === "string";
|
|
290
|
+
return typeof schema === "object" && schema !== null && "$ref" in schema && typeof schema.$ref === "string";
|
|
284
291
|
}
|
|
285
292
|
};
|
|
286
293
|
|
|
@@ -330,9 +337,15 @@ var Provider = class {
|
|
|
330
337
|
};
|
|
331
338
|
|
|
332
339
|
// src/core/generator/index.ts
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
340
|
+
import { writeFile } from "fs/promises";
|
|
341
|
+
import { format } from "prettier";
|
|
342
|
+
import {
|
|
343
|
+
addSyntheticLeadingComment,
|
|
344
|
+
createPrinter,
|
|
345
|
+
factory as t,
|
|
346
|
+
NodeFlags,
|
|
347
|
+
SyntaxKind
|
|
348
|
+
} from "typescript";
|
|
336
349
|
var Generator = class _Generator {
|
|
337
350
|
/**
|
|
338
351
|
* Converts an array of TypeScript statements into a formatted string of code.
|
|
@@ -345,16 +358,16 @@ var Generator = class _Generator {
|
|
|
345
358
|
if (statements.length === 0) {
|
|
346
359
|
return "// No api declaration found.";
|
|
347
360
|
}
|
|
348
|
-
const sourceFile =
|
|
361
|
+
const sourceFile = t.createSourceFile(
|
|
349
362
|
statements,
|
|
350
|
-
|
|
351
|
-
|
|
363
|
+
t.createToken(SyntaxKind.EndOfFileToken),
|
|
364
|
+
NodeFlags.None
|
|
352
365
|
);
|
|
353
|
-
return
|
|
366
|
+
return createPrinter().printFile(sourceFile);
|
|
354
367
|
}
|
|
355
368
|
static async write(code, filepath) {
|
|
356
369
|
try {
|
|
357
|
-
await
|
|
370
|
+
await writeFile(filepath, code);
|
|
358
371
|
} catch (error) {
|
|
359
372
|
console.error(error);
|
|
360
373
|
}
|
|
@@ -366,7 +379,7 @@ var Generator = class _Generator {
|
|
|
366
379
|
* @param path - The base path string containing placeholders.
|
|
367
380
|
* @param parameters - Array of parameter objects defining the parameters.
|
|
368
381
|
* @param basePath - Optional base path to prepend (default: "").
|
|
369
|
-
* @returns A TypeScript template
|
|
382
|
+
* @returns A TypeScript template expressi
|
|
370
383
|
*/
|
|
371
384
|
static toUrlTemplate(path, parameters, basePath = "") {
|
|
372
385
|
const queryParameters = parameters.filter(
|
|
@@ -374,25 +387,25 @@ var Generator = class _Generator {
|
|
|
374
387
|
);
|
|
375
388
|
if (queryParameters.length > 0) {
|
|
376
389
|
const queryString = queryParameters.map(
|
|
377
|
-
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${qp.name}}`
|
|
390
|
+
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${Base.camelCase(Base.normalize(qp.name))}}`
|
|
378
391
|
).join("");
|
|
379
392
|
path += queryString;
|
|
380
393
|
}
|
|
381
394
|
const pathSegments = path.replaceAll("{", "${").split("$").filter(Boolean);
|
|
382
395
|
if (pathSegments.length === 1) {
|
|
383
|
-
return
|
|
396
|
+
return t.createNoSubstitutionTemplateLiteral(basePath + path);
|
|
384
397
|
}
|
|
385
|
-
return
|
|
386
|
-
|
|
398
|
+
return t.createTemplateExpression(
|
|
399
|
+
t.createTemplateHead(basePath + pathSegments[0]),
|
|
387
400
|
pathSegments.slice(1).map((segment, index) => {
|
|
388
401
|
const match = /^{(.+)}(.+)?/gm.exec(segment);
|
|
389
402
|
const isLastSegment = index === pathSegments.length - 2;
|
|
390
403
|
if (!match) {
|
|
391
404
|
throw new Error(`Invalid path segment: ${segment}`);
|
|
392
405
|
}
|
|
393
|
-
return
|
|
394
|
-
|
|
395
|
-
!isLastSegment ?
|
|
406
|
+
return t.createTemplateSpan(
|
|
407
|
+
t.createIdentifier(match[1]),
|
|
408
|
+
!isLastSegment ? t.createTemplateMiddle(match[2]) : t.createTemplateTail(match[2] || "")
|
|
396
409
|
);
|
|
397
410
|
})
|
|
398
411
|
);
|
|
@@ -410,9 +423,9 @@ var Generator = class _Generator {
|
|
|
410
423
|
return comment.tag ? ` @${comment.tag} ${comment.comment ?? ""}` : ` ${comment.comment}`;
|
|
411
424
|
};
|
|
412
425
|
const formattedComments = "*\n" + comments.map(formatComment).join("\n").trim() + "\n";
|
|
413
|
-
|
|
426
|
+
addSyntheticLeadingComment(
|
|
414
427
|
node,
|
|
415
|
-
|
|
428
|
+
SyntaxKind.MultiLineCommentTrivia,
|
|
416
429
|
formattedComments,
|
|
417
430
|
true
|
|
418
431
|
);
|
|
@@ -432,10 +445,10 @@ var Generator = class _Generator {
|
|
|
432
445
|
return nonArraySchema.format === "blob" /* blob */ || nonArraySchema.format === "binary" /* binary */ || nonArraySchema.type === "file" /* file */;
|
|
433
446
|
}
|
|
434
447
|
static toRequestBodyTypeNode(schema) {
|
|
435
|
-
return
|
|
448
|
+
return t.createParameterDeclaration(
|
|
436
449
|
void 0,
|
|
437
450
|
void 0,
|
|
438
|
-
|
|
451
|
+
t.createIdentifier("req"),
|
|
439
452
|
void 0,
|
|
440
453
|
this.toTypeNode(schema)
|
|
441
454
|
);
|
|
@@ -444,8 +457,8 @@ var Generator = class _Generator {
|
|
|
444
457
|
const { type, ref } = schema;
|
|
445
458
|
if (ref) {
|
|
446
459
|
const identify = Base.ref2name(ref);
|
|
447
|
-
return
|
|
448
|
-
|
|
460
|
+
return t.createTypeReferenceNode(
|
|
461
|
+
t.createIdentifier(
|
|
449
462
|
identify === "unknown" ? identify : Base.upperCamelCase(identify)
|
|
450
463
|
)
|
|
451
464
|
);
|
|
@@ -453,24 +466,24 @@ var Generator = class _Generator {
|
|
|
453
466
|
switch (type) {
|
|
454
467
|
case "array" /* array */:
|
|
455
468
|
const { items } = schema;
|
|
456
|
-
return
|
|
469
|
+
return t.createArrayTypeNode(this.toTypeNode(items));
|
|
457
470
|
case "object" /* object */:
|
|
458
471
|
const propsCount = Object.keys(schema.properties ?? {}).length;
|
|
459
472
|
if (!schema.properties || propsCount === 0) {
|
|
460
|
-
return
|
|
461
|
-
|
|
462
|
-
|
|
473
|
+
return t.createTypeReferenceNode(t.createIdentifier("Record"), [
|
|
474
|
+
t.createToken(SyntaxKind.StringKeyword),
|
|
475
|
+
t.createToken(SyntaxKind.UnknownKeyword)
|
|
463
476
|
]);
|
|
464
477
|
}
|
|
465
478
|
const props = Object.keys(schema.properties);
|
|
466
|
-
return
|
|
479
|
+
return t.createTypeLiteralNode(
|
|
467
480
|
props.map((propKey) => {
|
|
468
481
|
const propSchema = schema.properties[propKey];
|
|
469
|
-
return
|
|
482
|
+
return t.createPropertySignature(
|
|
470
483
|
void 0,
|
|
471
|
-
|
|
484
|
+
t.createStringLiteral(propKey),
|
|
472
485
|
// When field is required, a refrence or binary value, don't add question mark.
|
|
473
|
-
schema.required || schema.ref || this.isBinarySchema(schema) ? void 0 :
|
|
486
|
+
schema.required || schema.ref || this.isBinarySchema(schema) ? void 0 : t.createToken(SyntaxKind.QuestionToken),
|
|
474
487
|
this.toTypeNode(propSchema)
|
|
475
488
|
);
|
|
476
489
|
})
|
|
@@ -478,18 +491,18 @@ var Generator = class _Generator {
|
|
|
478
491
|
case "integer" /* integer */:
|
|
479
492
|
case "number" /* number */:
|
|
480
493
|
if (schema.enum) {
|
|
481
|
-
return
|
|
494
|
+
return t.createUnionTypeNode(
|
|
482
495
|
schema.enum.map(
|
|
483
|
-
(e) =>
|
|
496
|
+
(e) => t.createLiteralTypeNode(t.createNumericLiteral(e))
|
|
484
497
|
)
|
|
485
498
|
);
|
|
486
499
|
}
|
|
487
|
-
return
|
|
500
|
+
return t.createToken(SyntaxKind.NumberKeyword);
|
|
488
501
|
// case NonArraySchemaType.string:
|
|
489
502
|
case "boolean" /* boolean */:
|
|
490
|
-
return
|
|
503
|
+
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
491
504
|
case "file" /* file */:
|
|
492
|
-
return
|
|
505
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
493
506
|
default:
|
|
494
507
|
const {
|
|
495
508
|
format: format2,
|
|
@@ -501,137 +514,136 @@ var Generator = class _Generator {
|
|
|
501
514
|
} = schema;
|
|
502
515
|
switch (format2) {
|
|
503
516
|
case "number" /* number */:
|
|
504
|
-
return
|
|
517
|
+
return t.createToken(SyntaxKind.NumberKeyword);
|
|
505
518
|
case "string" /* string */:
|
|
506
|
-
return
|
|
519
|
+
return t.createToken(SyntaxKind.StringKeyword);
|
|
507
520
|
case "boolean" /* boolean */:
|
|
508
|
-
return
|
|
521
|
+
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
509
522
|
case "blob" /* blob */:
|
|
510
523
|
case "binary" /* binary */:
|
|
511
|
-
return
|
|
524
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
512
525
|
default:
|
|
513
526
|
}
|
|
514
527
|
if (enum_) {
|
|
515
|
-
return
|
|
528
|
+
return t.createUnionTypeNode(
|
|
516
529
|
enum_.map(
|
|
517
|
-
(e) =>
|
|
530
|
+
(e) => t.createLiteralTypeNode(t.createStringLiteral(e))
|
|
518
531
|
)
|
|
519
532
|
);
|
|
520
533
|
}
|
|
521
534
|
if (type2 === "string" /* string */) {
|
|
522
|
-
return
|
|
535
|
+
return t.createToken(SyntaxKind.StringKeyword);
|
|
523
536
|
}
|
|
524
537
|
if (oneOf) {
|
|
525
|
-
return
|
|
538
|
+
return t.createUnionTypeNode(
|
|
526
539
|
oneOf.map((schema2) => this.toTypeNode(schema2))
|
|
527
540
|
);
|
|
528
541
|
}
|
|
529
542
|
if (anyOf) {
|
|
530
|
-
return
|
|
543
|
+
return t.createUnionTypeNode(
|
|
531
544
|
anyOf.map((schema2) => this.toTypeNode(schema2))
|
|
532
545
|
);
|
|
533
546
|
}
|
|
534
547
|
if (allOf) {
|
|
535
|
-
return
|
|
548
|
+
return t.createIntersectionTypeNode(
|
|
536
549
|
allOf.map((schema2) => this.toTypeNode(schema2))
|
|
537
550
|
);
|
|
538
551
|
}
|
|
539
552
|
if (type2 && typeof type2 === "string") {
|
|
540
|
-
return
|
|
541
|
-
type2 !== "unknown" ?
|
|
553
|
+
return t.createTypeReferenceNode(
|
|
554
|
+
type2 !== "unknown" && type2 !== "null" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
|
|
542
555
|
);
|
|
543
556
|
}
|
|
544
557
|
}
|
|
545
|
-
return
|
|
558
|
+
return t.createToken(SyntaxKind.UnknownKeyword);
|
|
546
559
|
}
|
|
547
|
-
static
|
|
560
|
+
static toDeclarationNodes(parameters) {
|
|
548
561
|
const objectElements = [];
|
|
549
562
|
const typeObjectElements = [];
|
|
563
|
+
const refParameters = [];
|
|
550
564
|
for (const parameter of parameters) {
|
|
551
565
|
if (parameter.ref) {
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
)
|
|
564
|
-
void 0
|
|
566
|
+
const refName = Base.ref2name(parameter.ref);
|
|
567
|
+
refParameters.push(
|
|
568
|
+
t.createParameterDeclaration(
|
|
569
|
+
void 0,
|
|
570
|
+
void 0,
|
|
571
|
+
t.createIdentifier(Base.camelCase(Base.normalize(refName))),
|
|
572
|
+
void 0,
|
|
573
|
+
t.createTypeReferenceNode(
|
|
574
|
+
t.createIdentifier(Base.upperCamelCase(Base.normalize(refName)))
|
|
575
|
+
),
|
|
576
|
+
void 0
|
|
577
|
+
)
|
|
565
578
|
);
|
|
566
579
|
} else {
|
|
567
580
|
const { name, schema, required } = parameter;
|
|
568
581
|
objectElements.push(
|
|
569
|
-
|
|
582
|
+
t.createBindingElement(
|
|
570
583
|
void 0,
|
|
571
584
|
void 0,
|
|
572
|
-
|
|
585
|
+
t.createIdentifier(Base.camelCase(Base.normalize(name)))
|
|
573
586
|
)
|
|
574
587
|
);
|
|
575
588
|
typeObjectElements.push(
|
|
576
|
-
|
|
589
|
+
t.createPropertySignature(
|
|
577
590
|
[],
|
|
578
|
-
|
|
579
|
-
required ? void 0 :
|
|
580
|
-
!schema ?
|
|
591
|
+
t.createIdentifier(Base.camelCase(Base.normalize(name))),
|
|
592
|
+
required ? void 0 : t.createToken(SyntaxKind.QuestionToken),
|
|
593
|
+
!schema ? t.createToken(SyntaxKind.UnknownKeyword) : this.toTypeNode(schema)
|
|
581
594
|
)
|
|
582
595
|
);
|
|
583
596
|
}
|
|
584
597
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
598
|
+
if (objectElements.length > 0) {
|
|
599
|
+
const objectParam = t.createParameterDeclaration(
|
|
600
|
+
void 0,
|
|
601
|
+
void 0,
|
|
602
|
+
t.createObjectBindingPattern(objectElements),
|
|
603
|
+
void 0,
|
|
604
|
+
t.createTypeLiteralNode(typeObjectElements),
|
|
605
|
+
void 0
|
|
606
|
+
);
|
|
607
|
+
return [objectParam, ...refParameters];
|
|
608
|
+
}
|
|
609
|
+
return refParameters;
|
|
593
610
|
}
|
|
594
611
|
static toFormDataStatement(parameters, requestBody) {
|
|
595
612
|
const statements = [];
|
|
596
|
-
const fdDeclaration =
|
|
613
|
+
const fdDeclaration = t.createVariableStatement(
|
|
597
614
|
void 0,
|
|
598
|
-
|
|
615
|
+
t.createVariableDeclarationList(
|
|
599
616
|
[
|
|
600
|
-
|
|
601
|
-
|
|
617
|
+
t.createVariableDeclaration(
|
|
618
|
+
t.createIdentifier("fd"),
|
|
602
619
|
void 0,
|
|
603
620
|
void 0,
|
|
604
|
-
|
|
605
|
-
|
|
621
|
+
t.createNewExpression(
|
|
622
|
+
t.createIdentifier("FormData"),
|
|
606
623
|
void 0,
|
|
607
624
|
[]
|
|
608
625
|
)
|
|
609
626
|
)
|
|
610
627
|
],
|
|
611
|
-
|
|
628
|
+
NodeFlags.Const
|
|
612
629
|
)
|
|
613
630
|
);
|
|
614
631
|
statements.push(fdDeclaration);
|
|
615
|
-
parameters.
|
|
616
|
-
(parameter) => parameter.ref !== void 0 && (parameter.in === "formData" /* formData */ || parameter.schema && this.isBinarySchema(parameter.schema))
|
|
617
|
-
).forEach((parameter) => {
|
|
632
|
+
parameters.forEach((parameter) => {
|
|
618
633
|
statements.push(
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
import_typescript.factory.createPropertyAccessExpression(
|
|
628
|
-
import_typescript.factory.createIdentifier("fd"),
|
|
629
|
-
import_typescript.factory.createIdentifier("append")
|
|
634
|
+
t.createExpressionStatement(
|
|
635
|
+
t.createBinaryExpression(
|
|
636
|
+
t.createIdentifier(parameter.name),
|
|
637
|
+
t.createToken(SyntaxKind.AmpersandAmpersandToken),
|
|
638
|
+
t.createCallExpression(
|
|
639
|
+
t.createPropertyAccessExpression(
|
|
640
|
+
t.createIdentifier("fd"),
|
|
641
|
+
t.createIdentifier("append")
|
|
630
642
|
),
|
|
631
643
|
void 0,
|
|
632
644
|
[
|
|
633
|
-
|
|
634
|
-
|
|
645
|
+
t.createStringLiteral(parameter.name),
|
|
646
|
+
t.createIdentifier(parameter.name)
|
|
635
647
|
]
|
|
636
648
|
)
|
|
637
649
|
)
|
|
@@ -643,30 +655,36 @@ var Generator = class _Generator {
|
|
|
643
655
|
const schemaByKey = requestBody.properties[key];
|
|
644
656
|
if (schemaByKey.type === "array" /* array */ && this.isBinarySchema(schemaByKey)) {
|
|
645
657
|
statements.push(
|
|
646
|
-
|
|
658
|
+
t.createForOfStatement(
|
|
647
659
|
void 0,
|
|
648
|
-
|
|
649
|
-
[
|
|
650
|
-
|
|
660
|
+
t.createVariableDeclarationList(
|
|
661
|
+
[t.createVariableDeclaration("file")],
|
|
662
|
+
NodeFlags.Const
|
|
651
663
|
),
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
664
|
+
t.createElementAccessExpression(
|
|
665
|
+
t.createIdentifier("req"),
|
|
666
|
+
t.createStringLiteral(key)
|
|
655
667
|
),
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
668
|
+
t.createBlock([
|
|
669
|
+
t.createExpressionStatement(
|
|
670
|
+
t.createCallExpression(
|
|
671
|
+
t.createPropertyAccessExpression(
|
|
672
|
+
t.createIdentifier("fd"),
|
|
673
|
+
t.createIdentifier("append")
|
|
662
674
|
),
|
|
663
675
|
[],
|
|
664
676
|
[
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
677
|
+
t.createStringLiteral(key),
|
|
678
|
+
t.createIdentifier("file"),
|
|
679
|
+
t.createPropertyAccessExpression(
|
|
680
|
+
t.createAsExpression(
|
|
681
|
+
t.createIdentifier("file"),
|
|
682
|
+
t.createTypeReferenceNode(
|
|
683
|
+
t.createIdentifier("File"),
|
|
684
|
+
void 0
|
|
685
|
+
)
|
|
686
|
+
),
|
|
687
|
+
t.createIdentifier("name")
|
|
670
688
|
)
|
|
671
689
|
]
|
|
672
690
|
)
|
|
@@ -677,25 +695,25 @@ var Generator = class _Generator {
|
|
|
677
695
|
} else {
|
|
678
696
|
if (schemaByKey.required) {
|
|
679
697
|
statements.push(
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
698
|
+
t.createExpressionStatement(
|
|
699
|
+
t.createCallExpression(
|
|
700
|
+
t.createPropertyAccessExpression(
|
|
701
|
+
t.createIdentifier("fd"),
|
|
702
|
+
t.createIdentifier("append")
|
|
685
703
|
),
|
|
686
704
|
void 0,
|
|
687
705
|
[
|
|
688
|
-
|
|
689
|
-
schemaByKey.type === "string" ?
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
) :
|
|
693
|
-
|
|
706
|
+
t.createStringLiteral(key),
|
|
707
|
+
schemaByKey.type === "string" ? t.createElementAccessExpression(
|
|
708
|
+
t.createIdentifier("req"),
|
|
709
|
+
t.createStringLiteral(key)
|
|
710
|
+
) : t.createCallExpression(
|
|
711
|
+
t.createIdentifier("String"),
|
|
694
712
|
void 0,
|
|
695
713
|
[
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
714
|
+
t.createElementAccessExpression(
|
|
715
|
+
t.createIdentifier("req"),
|
|
716
|
+
t.createStringLiteral(key)
|
|
699
717
|
)
|
|
700
718
|
]
|
|
701
719
|
)
|
|
@@ -705,31 +723,31 @@ var Generator = class _Generator {
|
|
|
705
723
|
);
|
|
706
724
|
} else {
|
|
707
725
|
statements.push(
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
726
|
+
t.createExpressionStatement(
|
|
727
|
+
t.createBinaryExpression(
|
|
728
|
+
t.createElementAccessExpression(
|
|
729
|
+
t.createIdentifier("req"),
|
|
730
|
+
t.createStringLiteral(key)
|
|
713
731
|
),
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
732
|
+
t.createToken(SyntaxKind.AmpersandAmpersandToken),
|
|
733
|
+
t.createCallExpression(
|
|
734
|
+
t.createPropertyAccessExpression(
|
|
735
|
+
t.createIdentifier("fd"),
|
|
736
|
+
t.createIdentifier("append")
|
|
719
737
|
),
|
|
720
738
|
void 0,
|
|
721
739
|
[
|
|
722
|
-
|
|
723
|
-
schemaByKey.type === "string" ?
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
) :
|
|
727
|
-
|
|
740
|
+
t.createStringLiteral(key),
|
|
741
|
+
schemaByKey.type === "string" ? t.createElementAccessExpression(
|
|
742
|
+
t.createIdentifier("req"),
|
|
743
|
+
t.createStringLiteral(key)
|
|
744
|
+
) : t.createCallExpression(
|
|
745
|
+
t.createIdentifier("String"),
|
|
728
746
|
void 0,
|
|
729
747
|
[
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
748
|
+
t.createElementAccessExpression(
|
|
749
|
+
t.createIdentifier("req"),
|
|
750
|
+
t.createStringLiteral(key)
|
|
733
751
|
)
|
|
734
752
|
]
|
|
735
753
|
)
|
|
@@ -750,25 +768,28 @@ var Generator = class _Generator {
|
|
|
750
768
|
);
|
|
751
769
|
const shouldParseResponseToJSON = "application/json" === response?.type;
|
|
752
770
|
const isRequestBodyBinary = requestBody?.schema && requestBody.schema.type === "array" /* array */ && this.isBinarySchema(requestBody.schema);
|
|
753
|
-
const
|
|
754
|
-
(p) => p.in === "formData" /* formData */
|
|
771
|
+
const parametersShouldPutInFormData = parameters.filter(
|
|
772
|
+
(p) => p.in === "formData" /* formData */ || p.schema && this.isBinarySchema(p.schema)
|
|
755
773
|
);
|
|
756
|
-
const
|
|
757
|
-
(p) => p
|
|
774
|
+
const parametersShouldNotPutInFormData = parameters.filter(
|
|
775
|
+
(p) => !parametersShouldPutInFormData.includes(p)
|
|
758
776
|
);
|
|
759
|
-
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema
|
|
777
|
+
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some(
|
|
760
778
|
(p) => this.isBinarySchema(p)
|
|
761
779
|
);
|
|
762
780
|
const hasBinaryInParameters = parameters.some(
|
|
763
781
|
(p) => p?.schema && this.isBinarySchema(p.schema)
|
|
764
782
|
);
|
|
765
|
-
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary ||
|
|
766
|
-
return
|
|
767
|
-
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
783
|
+
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
|
|
784
|
+
return t.createBlock([
|
|
785
|
+
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
786
|
+
parametersShouldPutInFormData,
|
|
787
|
+
requestBody?.schema
|
|
788
|
+
) : [],
|
|
768
789
|
...adapter.client(
|
|
769
790
|
uri,
|
|
770
791
|
method,
|
|
771
|
-
|
|
792
|
+
parametersShouldNotPutInFormData,
|
|
772
793
|
requestBody,
|
|
773
794
|
response,
|
|
774
795
|
adapter,
|
|
@@ -784,15 +805,15 @@ var Generator = class _Generator {
|
|
|
784
805
|
for (const enumObject of enums) {
|
|
785
806
|
enumNames.push(Base.capitalize(enumObject.name));
|
|
786
807
|
statements.push(
|
|
787
|
-
|
|
788
|
-
[
|
|
789
|
-
|
|
808
|
+
t.createEnumDeclaration(
|
|
809
|
+
[t.createToken(SyntaxKind.ExportKeyword)],
|
|
810
|
+
t.createIdentifier(Base.upperCamelCase(enumObject.name)),
|
|
790
811
|
enumObject.enum.map((member) => {
|
|
791
|
-
return
|
|
792
|
-
|
|
812
|
+
return t.createEnumMember(
|
|
813
|
+
t.createStringLiteral(
|
|
793
814
|
typeof member === "string" ? member : `${member}_`
|
|
794
815
|
),
|
|
795
|
-
typeof member === "string" ?
|
|
816
|
+
typeof member === "string" ? t.createStringLiteral(member) : t.createNumericLiteral(member)
|
|
796
817
|
);
|
|
797
818
|
})
|
|
798
819
|
)
|
|
@@ -802,9 +823,9 @@ var Generator = class _Generator {
|
|
|
802
823
|
if (Object.hasOwnProperty.call(schemas, schemaKey) && !enumNames.includes(Base.upperCamelCase(schemaKey))) {
|
|
803
824
|
const schema = schemas[schemaKey];
|
|
804
825
|
statements.push(
|
|
805
|
-
|
|
806
|
-
[
|
|
807
|
-
|
|
826
|
+
t.createTypeAliasDeclaration(
|
|
827
|
+
[t.createModifier(SyntaxKind.ExportKeyword)],
|
|
828
|
+
t.createIdentifier(Base.upperCamelCase(schemaKey)),
|
|
808
829
|
void 0,
|
|
809
830
|
this.toTypeNode(schema)
|
|
810
831
|
)
|
|
@@ -830,17 +851,17 @@ var Generator = class _Generator {
|
|
|
830
851
|
}
|
|
831
852
|
const shouldAddExtraMethodNameSuffix = requestBody.length > 1;
|
|
832
853
|
for (const req of requestBody) {
|
|
833
|
-
const statement =
|
|
854
|
+
const statement = t.createFunctionDeclaration(
|
|
834
855
|
[
|
|
835
|
-
|
|
836
|
-
|
|
856
|
+
t.createModifier(SyntaxKind.ExportKeyword),
|
|
857
|
+
t.createModifier(SyntaxKind.AsyncKeyword)
|
|
837
858
|
],
|
|
838
859
|
void 0,
|
|
839
860
|
Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
|
|
840
861
|
void 0,
|
|
841
862
|
[
|
|
842
|
-
parameters.length > 0 ? _Generator.
|
|
843
|
-
req?.schema ? _Generator.toRequestBodyTypeNode(req.schema) :
|
|
863
|
+
...parameters.length > 0 ? _Generator.toDeclarationNodes(parameters) : [],
|
|
864
|
+
...req?.schema ? [_Generator.toRequestBodyTypeNode(req.schema)] : []
|
|
844
865
|
].filter(Boolean),
|
|
845
866
|
void 0,
|
|
846
867
|
this.bodyBlock(
|
|
@@ -873,7 +894,7 @@ var Generator = class _Generator {
|
|
|
873
894
|
return statements;
|
|
874
895
|
}
|
|
875
896
|
static async prettier(code) {
|
|
876
|
-
return await
|
|
897
|
+
return await format(code, {
|
|
877
898
|
parser: "typescript"
|
|
878
899
|
});
|
|
879
900
|
}
|
|
@@ -891,7 +912,7 @@ var Generator = class _Generator {
|
|
|
891
912
|
};
|
|
892
913
|
|
|
893
914
|
// src/core/client/axios.ts
|
|
894
|
-
|
|
915
|
+
import { factory as t2 } from "typescript";
|
|
895
916
|
var AxiosAdapter = class extends Adapter {
|
|
896
917
|
/**
|
|
897
918
|
* Name of the field used to specify the HTTP method in the request configuration.
|
|
@@ -925,30 +946,30 @@ var AxiosAdapter = class extends Adapter {
|
|
|
925
946
|
const inBody = parameters.filter((p) => !p.in || p.in === "body");
|
|
926
947
|
const inHeader = parameters.filter((p) => p.in === "header");
|
|
927
948
|
const toLiterlExpression = () => {
|
|
928
|
-
return
|
|
949
|
+
return t2.createObjectLiteralExpression(
|
|
929
950
|
[
|
|
930
951
|
// Set the HTTP method
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
952
|
+
t2.createPropertyAssignment(
|
|
953
|
+
t2.createIdentifier(adapter.methodFieldName),
|
|
954
|
+
t2.createStringLiteral(method.toUpperCase())
|
|
934
955
|
)
|
|
935
956
|
].concat(
|
|
936
957
|
// Add headers if there are any
|
|
937
|
-
inHeader.length > 0 ?
|
|
938
|
-
|
|
939
|
-
|
|
958
|
+
inHeader.length > 0 ? t2.createPropertyAssignment(
|
|
959
|
+
t2.createIdentifier(adapter.headersFieldName),
|
|
960
|
+
t2.createObjectLiteralExpression(
|
|
940
961
|
inHeader.map(
|
|
941
|
-
(p) =>
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
962
|
+
(p) => t2.createPropertyAssignment(
|
|
963
|
+
t2.createStringLiteral(p.name),
|
|
964
|
+
t2.createCallExpression(
|
|
965
|
+
t2.createIdentifier("encodeURIComponent"),
|
|
945
966
|
void 0,
|
|
946
967
|
[
|
|
947
|
-
|
|
948
|
-
|
|
968
|
+
t2.createCallExpression(
|
|
969
|
+
t2.createIdentifier("String"),
|
|
949
970
|
void 0,
|
|
950
971
|
[
|
|
951
|
-
|
|
972
|
+
t2.createIdentifier(
|
|
952
973
|
Base.camelCase(Base.normalize(p.name))
|
|
953
974
|
)
|
|
954
975
|
]
|
|
@@ -960,19 +981,18 @@ var AxiosAdapter = class extends Adapter {
|
|
|
960
981
|
)
|
|
961
982
|
) : []
|
|
962
983
|
).concat(
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
shouldUseFormData ? import_typescript2.factory.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? import_typescript2.factory.createIdentifier("req") : import_typescript2.factory.createIdentifier("req")
|
|
984
|
+
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t2.createPropertyAssignment(
|
|
985
|
+
t2.createIdentifier(adapter.bodyFieldName),
|
|
986
|
+
shouldUseFormData ? t2.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t2.createIdentifier("req") : t2.createIdentifier("req")
|
|
967
987
|
) : []
|
|
968
988
|
),
|
|
969
989
|
true
|
|
970
990
|
);
|
|
971
991
|
};
|
|
972
992
|
statements.push(
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
993
|
+
t2.createReturnStatement(
|
|
994
|
+
t2.createCallExpression(
|
|
995
|
+
t2.createIdentifier(adapter.name),
|
|
976
996
|
response?.schema ? [
|
|
977
997
|
Generator.toTypeNode(
|
|
978
998
|
response.schema
|
|
@@ -987,7 +1007,7 @@ var AxiosAdapter = class extends Adapter {
|
|
|
987
1007
|
};
|
|
988
1008
|
|
|
989
1009
|
// src/core/client/fetch.ts
|
|
990
|
-
|
|
1010
|
+
import { factory as t3, SyntaxKind as SyntaxKind2 } from "typescript";
|
|
991
1011
|
var FetchAdapter = class extends Adapter {
|
|
992
1012
|
methodFieldName = "method";
|
|
993
1013
|
bodyFieldName = "body";
|
|
@@ -1011,30 +1031,30 @@ var FetchAdapter = class extends Adapter {
|
|
|
1011
1031
|
const inBody = parameters.filter((p) => !p.in || p.in === "body");
|
|
1012
1032
|
const inHeader = parameters.filter((p) => p.in === "header");
|
|
1013
1033
|
const toLiterlExpression = () => {
|
|
1014
|
-
return
|
|
1034
|
+
return t3.createObjectLiteralExpression(
|
|
1015
1035
|
[
|
|
1016
1036
|
// Set the HTTP method
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1037
|
+
t3.createPropertyAssignment(
|
|
1038
|
+
t3.createIdentifier(adapter.methodFieldName),
|
|
1039
|
+
t3.createStringLiteral(method.toUpperCase())
|
|
1020
1040
|
)
|
|
1021
1041
|
].concat(
|
|
1022
1042
|
// Add headers if there are any
|
|
1023
|
-
inHeader.length > 0 ?
|
|
1024
|
-
|
|
1025
|
-
|
|
1043
|
+
inHeader.length > 0 ? t3.createPropertyAssignment(
|
|
1044
|
+
t3.createIdentifier(adapter.headersFieldName),
|
|
1045
|
+
t3.createObjectLiteralExpression(
|
|
1026
1046
|
inHeader.map(
|
|
1027
|
-
(p) =>
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1047
|
+
(p) => t3.createPropertyAssignment(
|
|
1048
|
+
t3.createStringLiteral(p.name),
|
|
1049
|
+
t3.createCallExpression(
|
|
1050
|
+
t3.createIdentifier("encodeURIComponent"),
|
|
1031
1051
|
void 0,
|
|
1032
1052
|
[
|
|
1033
|
-
|
|
1034
|
-
|
|
1053
|
+
t3.createCallExpression(
|
|
1054
|
+
t3.createIdentifier("String"),
|
|
1035
1055
|
void 0,
|
|
1036
1056
|
[
|
|
1037
|
-
|
|
1057
|
+
t3.createIdentifier(
|
|
1038
1058
|
Base.camelCase(Base.normalize(p.name))
|
|
1039
1059
|
)
|
|
1040
1060
|
]
|
|
@@ -1046,20 +1066,19 @@ var FetchAdapter = class extends Adapter {
|
|
|
1046
1066
|
)
|
|
1047
1067
|
) : []
|
|
1048
1068
|
).concat(
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
import_typescript3.factory.createIdentifier("stringify")
|
|
1069
|
+
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t3.createPropertyAssignment(
|
|
1070
|
+
t3.createIdentifier(adapter.bodyFieldName),
|
|
1071
|
+
shouldUseFormData ? t3.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t3.createCallExpression(
|
|
1072
|
+
t3.createPropertyAccessExpression(
|
|
1073
|
+
t3.createIdentifier("JSON"),
|
|
1074
|
+
t3.createIdentifier("stringify")
|
|
1056
1075
|
),
|
|
1057
1076
|
[],
|
|
1058
1077
|
[
|
|
1059
|
-
requestBody ?
|
|
1078
|
+
requestBody ? t3.createIdentifier("req") : t3.createObjectLiteralExpression(
|
|
1060
1079
|
inBody.map(
|
|
1061
|
-
(b) =>
|
|
1062
|
-
|
|
1080
|
+
(b) => t3.createShorthandPropertyAssignment(
|
|
1081
|
+
t3.createIdentifier(b.name)
|
|
1063
1082
|
)
|
|
1064
1083
|
),
|
|
1065
1084
|
true
|
|
@@ -1067,7 +1086,7 @@ var FetchAdapter = class extends Adapter {
|
|
|
1067
1086
|
]
|
|
1068
1087
|
) : (
|
|
1069
1088
|
// One File parameter
|
|
1070
|
-
|
|
1089
|
+
t3.createIdentifier("req")
|
|
1071
1090
|
)
|
|
1072
1091
|
) : []
|
|
1073
1092
|
),
|
|
@@ -1075,57 +1094,68 @@ var FetchAdapter = class extends Adapter {
|
|
|
1075
1094
|
);
|
|
1076
1095
|
};
|
|
1077
1096
|
statements.push(
|
|
1078
|
-
|
|
1097
|
+
t3.createReturnStatement(
|
|
1079
1098
|
shouldUseJSONResponse ? (
|
|
1080
1099
|
// Handle JSON response with proper type checking
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1100
|
+
t3.createCallExpression(
|
|
1101
|
+
t3.createPropertyAccessExpression(
|
|
1102
|
+
t3.createCallExpression(
|
|
1103
|
+
t3.createIdentifier(adapter.name),
|
|
1085
1104
|
void 0,
|
|
1086
1105
|
[
|
|
1087
1106
|
Generator.toUrlTemplate(uri, parameters),
|
|
1088
1107
|
toLiterlExpression()
|
|
1089
1108
|
]
|
|
1090
1109
|
),
|
|
1091
|
-
|
|
1110
|
+
t3.createIdentifier("then")
|
|
1092
1111
|
),
|
|
1093
1112
|
void 0,
|
|
1094
1113
|
[
|
|
1095
|
-
|
|
1096
|
-
[
|
|
1114
|
+
t3.createArrowFunction(
|
|
1115
|
+
[t3.createModifier(SyntaxKind2.AsyncKeyword)],
|
|
1097
1116
|
[],
|
|
1098
1117
|
[
|
|
1099
|
-
|
|
1118
|
+
t3.createParameterDeclaration(
|
|
1100
1119
|
void 0,
|
|
1101
1120
|
void 0,
|
|
1102
|
-
|
|
1121
|
+
t3.createIdentifier("response")
|
|
1103
1122
|
)
|
|
1104
1123
|
],
|
|
1105
1124
|
void 0,
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1125
|
+
t3.createToken(SyntaxKind2.EqualsGreaterThanToken),
|
|
1126
|
+
response?.schema ? t3.createAsExpression(
|
|
1127
|
+
t3.createParenthesizedExpression(
|
|
1128
|
+
t3.createAwaitExpression(
|
|
1129
|
+
t3.createCallExpression(
|
|
1130
|
+
t3.createPropertyAccessExpression(
|
|
1131
|
+
t3.createIdentifier("response"),
|
|
1132
|
+
t3.createIdentifier("json")
|
|
1114
1133
|
),
|
|
1115
1134
|
void 0,
|
|
1116
1135
|
[]
|
|
1117
1136
|
)
|
|
1118
1137
|
)
|
|
1119
1138
|
),
|
|
1120
|
-
response?.schema ? Generator.toTypeNode(response.schema) :
|
|
1139
|
+
response?.schema ? Generator.toTypeNode(response.schema) : t3.createToken(SyntaxKind2.UnknownKeyword)
|
|
1140
|
+
) : t3.createParenthesizedExpression(
|
|
1141
|
+
t3.createAwaitExpression(
|
|
1142
|
+
t3.createCallExpression(
|
|
1143
|
+
t3.createPropertyAccessExpression(
|
|
1144
|
+
t3.createIdentifier("response"),
|
|
1145
|
+
t3.createIdentifier("json")
|
|
1146
|
+
),
|
|
1147
|
+
void 0,
|
|
1148
|
+
[]
|
|
1149
|
+
)
|
|
1150
|
+
)
|
|
1121
1151
|
)
|
|
1122
1152
|
)
|
|
1123
1153
|
]
|
|
1124
1154
|
)
|
|
1125
1155
|
) : (
|
|
1126
1156
|
// Simple fetch call without JSON parsing
|
|
1127
|
-
|
|
1128
|
-
|
|
1157
|
+
t3.createCallExpression(
|
|
1158
|
+
t3.createIdentifier(adapter.name),
|
|
1129
1159
|
void 0,
|
|
1130
1160
|
[Generator.toUrlTemplate(uri, parameters), toLiterlExpression()]
|
|
1131
1161
|
)
|
|
@@ -1137,7 +1167,7 @@ var FetchAdapter = class extends Adapter {
|
|
|
1137
1167
|
};
|
|
1138
1168
|
|
|
1139
1169
|
// src/openapi/index.ts
|
|
1140
|
-
|
|
1170
|
+
import { createScopedLogger } from "@moccona/logger";
|
|
1141
1171
|
|
|
1142
1172
|
// src/openapi/V2.ts
|
|
1143
1173
|
var V2 = class {
|
|
@@ -1228,13 +1258,25 @@ var V2 = class {
|
|
|
1228
1258
|
enum: enum_,
|
|
1229
1259
|
format: format2,
|
|
1230
1260
|
allOf: allOf?.map(
|
|
1231
|
-
(s) => Base.isRef(s) ? {
|
|
1261
|
+
(s) => Base.isRef(s) ? {
|
|
1262
|
+
...s,
|
|
1263
|
+
ref: s.$ref,
|
|
1264
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1265
|
+
} : this.toBaseSchema(s, enums)
|
|
1232
1266
|
),
|
|
1233
1267
|
anyOf: anyOf?.map(
|
|
1234
|
-
(s) => Base.isRef(s) ? {
|
|
1268
|
+
(s) => Base.isRef(s) ? {
|
|
1269
|
+
...s,
|
|
1270
|
+
ref: s.$ref,
|
|
1271
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1272
|
+
} : this.toBaseSchema(s, enums)
|
|
1235
1273
|
),
|
|
1236
1274
|
oneOf: oneOf?.map(
|
|
1237
|
-
(s) => Base.isRef(s) ? {
|
|
1275
|
+
(s) => Base.isRef(s) ? {
|
|
1276
|
+
...s,
|
|
1277
|
+
ref: s.$ref,
|
|
1278
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1279
|
+
} : this.toBaseSchema(s, enums)
|
|
1238
1280
|
),
|
|
1239
1281
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1240
1282
|
const propSchema = properties[p];
|
|
@@ -1264,7 +1306,8 @@ var V2 = class {
|
|
|
1264
1306
|
type,
|
|
1265
1307
|
items,
|
|
1266
1308
|
enum: enum_,
|
|
1267
|
-
properties
|
|
1309
|
+
properties,
|
|
1310
|
+
schema
|
|
1268
1311
|
} = parameter;
|
|
1269
1312
|
if (enum_) {
|
|
1270
1313
|
const type2 = Base.upperCamelCase(upLevelSchemaKey) + Base.upperCamelCase(name);
|
|
@@ -1286,17 +1329,36 @@ var V2 = class {
|
|
|
1286
1329
|
}
|
|
1287
1330
|
};
|
|
1288
1331
|
}
|
|
1332
|
+
if (items) {
|
|
1333
|
+
return {
|
|
1334
|
+
name,
|
|
1335
|
+
required,
|
|
1336
|
+
description,
|
|
1337
|
+
in: parameter.in,
|
|
1338
|
+
schema: {
|
|
1339
|
+
type,
|
|
1340
|
+
items
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
if (schema && Base.isRef(schema)) {
|
|
1345
|
+
return {
|
|
1346
|
+
name,
|
|
1347
|
+
required,
|
|
1348
|
+
description,
|
|
1349
|
+
in: parameter.in,
|
|
1350
|
+
schema: {
|
|
1351
|
+
type: Base.upperCamelCase(Base.ref2name(schema.$ref))
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1289
1355
|
return {
|
|
1290
1356
|
name,
|
|
1291
1357
|
required,
|
|
1292
1358
|
description,
|
|
1293
1359
|
in: parameter.in,
|
|
1294
|
-
schema:
|
|
1360
|
+
schema: {
|
|
1295
1361
|
type,
|
|
1296
|
-
items
|
|
1297
|
-
} : {
|
|
1298
|
-
type,
|
|
1299
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
1300
1362
|
properties
|
|
1301
1363
|
}
|
|
1302
1364
|
};
|
|
@@ -1375,6 +1437,7 @@ var V2 = class {
|
|
|
1375
1437
|
if (code in responses2) {
|
|
1376
1438
|
const response = responses2[code];
|
|
1377
1439
|
const responseSchema = this.getResponseByRef(response);
|
|
1440
|
+
const inBodyOnlyHasBody = inBody && inBody.length === 1 && inBody[0].in === "body" && inBody[0].name === "body";
|
|
1378
1441
|
methodApis.push({
|
|
1379
1442
|
method,
|
|
1380
1443
|
operationId,
|
|
@@ -1383,7 +1446,12 @@ var V2 = class {
|
|
|
1383
1446
|
description: description_,
|
|
1384
1447
|
parameters: uniqueParameterName.map((name) => notInBody.find((p) => p.name === name)).filter(Boolean),
|
|
1385
1448
|
responses: responseSchema,
|
|
1386
|
-
requestBody: inBody.length > 0 ? [
|
|
1449
|
+
requestBody: inBody.length > 0 ? inBodyOnlyHasBody ? [
|
|
1450
|
+
{
|
|
1451
|
+
type: "application/json" /* JSON */,
|
|
1452
|
+
schema: inBody[0].schema
|
|
1453
|
+
}
|
|
1454
|
+
] : [
|
|
1387
1455
|
{
|
|
1388
1456
|
type: "application/json" /* JSON */,
|
|
1389
1457
|
schema: {
|
|
@@ -1451,7 +1519,11 @@ var V3 = class {
|
|
|
1451
1519
|
type: upLevelSchemaKey + refName
|
|
1452
1520
|
};
|
|
1453
1521
|
}
|
|
1454
|
-
|
|
1522
|
+
const resolvedSchema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1523
|
+
if (!resolvedSchema) {
|
|
1524
|
+
return { type: "unknown" };
|
|
1525
|
+
}
|
|
1526
|
+
schema = resolvedSchema;
|
|
1455
1527
|
}
|
|
1456
1528
|
return this.toBaseSchema(schema, enums, "", upLevelSchemaKey + refName);
|
|
1457
1529
|
}
|
|
@@ -1460,7 +1532,14 @@ var V3 = class {
|
|
|
1460
1532
|
*/
|
|
1461
1533
|
getParameterByRef(schema, enums = [], upLevelSchemaKey = "") {
|
|
1462
1534
|
if (Base.isRef(schema)) {
|
|
1463
|
-
|
|
1535
|
+
const resolvedSchema = this.doc.components?.parameters?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1536
|
+
if (!resolvedSchema) {
|
|
1537
|
+
return {
|
|
1538
|
+
name: "unknown",
|
|
1539
|
+
in: "query"
|
|
1540
|
+
};
|
|
1541
|
+
}
|
|
1542
|
+
schema = resolvedSchema;
|
|
1464
1543
|
}
|
|
1465
1544
|
const {
|
|
1466
1545
|
name,
|
|
@@ -1509,7 +1588,11 @@ var V3 = class {
|
|
|
1509
1588
|
*/
|
|
1510
1589
|
getResponseByRef(schema) {
|
|
1511
1590
|
if (Base.isRef(schema)) {
|
|
1512
|
-
|
|
1591
|
+
const resolvedSchema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1592
|
+
if (!resolvedSchema) {
|
|
1593
|
+
return [];
|
|
1594
|
+
}
|
|
1595
|
+
schema = resolvedSchema;
|
|
1513
1596
|
}
|
|
1514
1597
|
const { content = {} } = schema;
|
|
1515
1598
|
return Object.keys(content).map((c) => ({
|
|
@@ -1522,7 +1605,11 @@ var V3 = class {
|
|
|
1522
1605
|
*/
|
|
1523
1606
|
getRequestBodyByRef(schema, enums = []) {
|
|
1524
1607
|
if (Base.isRef(schema)) {
|
|
1525
|
-
|
|
1608
|
+
const resolvedSchema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1609
|
+
if (!resolvedSchema) {
|
|
1610
|
+
return [];
|
|
1611
|
+
}
|
|
1612
|
+
schema = resolvedSchema;
|
|
1526
1613
|
}
|
|
1527
1614
|
const { content = {} } = schema;
|
|
1528
1615
|
return Object.keys(content).map((c) => ({
|
|
@@ -1591,13 +1678,25 @@ var V3 = class {
|
|
|
1591
1678
|
enum: enum_,
|
|
1592
1679
|
format: format2,
|
|
1593
1680
|
allOf: allOf?.map(
|
|
1594
|
-
(s) => Base.isRef(s) ? {
|
|
1681
|
+
(s) => Base.isRef(s) ? {
|
|
1682
|
+
...s,
|
|
1683
|
+
ref: s.$ref,
|
|
1684
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1685
|
+
} : this.toBaseSchema(s, enums)
|
|
1595
1686
|
),
|
|
1596
1687
|
anyOf: anyOf?.map(
|
|
1597
|
-
(s) => Base.isRef(s) ? {
|
|
1688
|
+
(s) => Base.isRef(s) ? {
|
|
1689
|
+
...s,
|
|
1690
|
+
ref: s.$ref,
|
|
1691
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1692
|
+
} : this.toBaseSchema(s, enums)
|
|
1598
1693
|
),
|
|
1599
1694
|
oneOf: oneOf?.map(
|
|
1600
|
-
(s) => Base.isRef(s) ? {
|
|
1695
|
+
(s) => Base.isRef(s) ? {
|
|
1696
|
+
...s,
|
|
1697
|
+
ref: s.$ref,
|
|
1698
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1699
|
+
} : this.toBaseSchema(s, enums)
|
|
1601
1700
|
),
|
|
1602
1701
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1603
1702
|
const propSchema = properties[p];
|
|
@@ -1894,13 +1993,25 @@ var V3_1 = class {
|
|
|
1894
1993
|
enum: enum_,
|
|
1895
1994
|
format: format2,
|
|
1896
1995
|
allOf: allOf?.map(
|
|
1897
|
-
(s) => Base.isRef(s) ? {
|
|
1996
|
+
(s) => Base.isRef(s) ? {
|
|
1997
|
+
...s,
|
|
1998
|
+
ref: s.$ref,
|
|
1999
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2000
|
+
} : this.toBaseSchema(s, enums)
|
|
1898
2001
|
),
|
|
1899
2002
|
anyOf: anyOf?.map(
|
|
1900
|
-
(s) => Base.isRef(s) ? {
|
|
2003
|
+
(s) => Base.isRef(s) ? {
|
|
2004
|
+
...s,
|
|
2005
|
+
ref: s.$ref,
|
|
2006
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2007
|
+
} : this.toBaseSchema(s, enums)
|
|
1901
2008
|
),
|
|
1902
2009
|
oneOf: oneOf?.map(
|
|
1903
|
-
(s) => Base.isRef(s) ? {
|
|
2010
|
+
(s) => Base.isRef(s) ? {
|
|
2011
|
+
...s,
|
|
2012
|
+
ref: s.$ref,
|
|
2013
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2014
|
+
} : this.toBaseSchema(s, enums)
|
|
1904
2015
|
),
|
|
1905
2016
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1906
2017
|
const propSchema = properties[p];
|
|
@@ -2030,7 +2141,7 @@ var V3_1 = class {
|
|
|
2030
2141
|
};
|
|
2031
2142
|
|
|
2032
2143
|
// src/openapi/index.ts
|
|
2033
|
-
var logger =
|
|
2144
|
+
var logger = createScopedLogger("OpenAPI");
|
|
2034
2145
|
function getDocVersion(doc) {
|
|
2035
2146
|
const version2 = (doc.openapi || doc.swagger).slice(0, 3);
|
|
2036
2147
|
switch (version2) {
|
|
@@ -2110,14 +2221,14 @@ async function codeGen(initOptions) {
|
|
|
2110
2221
|
}
|
|
2111
2222
|
|
|
2112
2223
|
// src/cli.ts
|
|
2113
|
-
|
|
2224
|
+
import { createCommand } from "commander";
|
|
2114
2225
|
|
|
2115
2226
|
// package.json
|
|
2116
|
-
var version = "0.0.
|
|
2227
|
+
var version = "0.0.3";
|
|
2117
2228
|
|
|
2118
2229
|
// src/cli.ts
|
|
2119
|
-
var cli =
|
|
2120
|
-
cli.version(version).argument("<docURL>", "
|
|
2230
|
+
var cli = createCommand("apicodegen");
|
|
2231
|
+
cli.version(version).argument("<docURL>", "URL of the OpenAPI documentation").option("-o, --output <path>", "Output file path", "./output.ts").option("-a, --adaptor <type>", "HTTP client adaptor (fetch|axios)", "fetch").option("-b, --baseURL <url>", "Base URL for API endpoints").option("-v, --verbose", "Enable verbose logging", false).option("--importClientSource <path>", "Custom client import source path").action(
|
|
2121
2232
|
async (docURL, options) => {
|
|
2122
2233
|
try {
|
|
2123
2234
|
const code = await codeGen({
|