@moccona/apicodegen 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/npm/index.cjs +121 -45
- package/npm/index.cjs.map +1 -1
- package/npm/index.d.cts +2 -2
- package/npm/index.d.ts +2 -2
- package/npm/index.js +121 -45
- package/npm/index.js.map +1 -1
- package/npm/vite/index.d.ts +29 -0
- package/{bin/cli.cjs → npm/vite/index.js} +394 -283
- package/npm/vite/index.js.map +1 -0
- package/package.json +19 -16
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
#!/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
1
|
// src/core/base/Adaptor.ts
|
|
5
2
|
var Adapter = class {
|
|
6
3
|
};
|
|
@@ -105,7 +102,7 @@ var HttpMethods = /* @__PURE__ */ ((HttpMethods2) => {
|
|
|
105
102
|
})(HttpMethods || {});
|
|
106
103
|
|
|
107
104
|
// src/core/base/Base.ts
|
|
108
|
-
|
|
105
|
+
import { Agent, request } from "undici";
|
|
109
106
|
var Base = class _Base {
|
|
110
107
|
constructor() {
|
|
111
108
|
if (new.target === _Base) {
|
|
@@ -142,10 +139,10 @@ var Base = class _Base {
|
|
|
142
139
|
* @param [operationId] - Unique identifier for the operation.
|
|
143
140
|
* @returns - The generated function name.
|
|
144
141
|
*/
|
|
145
|
-
static pathToFnName(path, method,
|
|
146
|
-
const name = this.camelCase(this.normalize(path));
|
|
142
|
+
static pathToFnName(path, method, _operationId = "") {
|
|
143
|
+
const name = this.normalize(this.camelCase(this.normalize(path)));
|
|
147
144
|
const suffix = method ? this.capitalize(this.upperCamelCase(`using_${method}`)) : "";
|
|
148
|
-
return
|
|
145
|
+
return name + suffix;
|
|
149
146
|
}
|
|
150
147
|
/**
|
|
151
148
|
* Normalizes a string by replacing special characters and avoiding TypeScript keywords.
|
|
@@ -156,7 +153,7 @@ var Base = class _Base {
|
|
|
156
153
|
if (typescriptKeywords.has(text)) {
|
|
157
154
|
text += "_";
|
|
158
155
|
}
|
|
159
|
-
return text.replace(/[/\-_{}():\s
|
|
156
|
+
return text.replace(/[/\-_{}():\s`,*<>$#.]/gm, "_").replace(/^\d./gm, "").replaceAll("...", "");
|
|
160
157
|
}
|
|
161
158
|
/**
|
|
162
159
|
* Capitalizes the first character of a string.
|
|
@@ -191,13 +188,13 @@ var Base = class _Base {
|
|
|
191
188
|
* @returns - A promise resolving to the fetched documentation data.
|
|
192
189
|
*/
|
|
193
190
|
static async fetchDoc(url, requestInit = {}) {
|
|
194
|
-
const agent = new
|
|
191
|
+
const agent = new Agent({
|
|
195
192
|
connect: {
|
|
196
193
|
rejectUnauthorized: false
|
|
197
194
|
}
|
|
198
195
|
});
|
|
199
196
|
try {
|
|
200
|
-
const { body } = await
|
|
197
|
+
const { body } = await request(url, {
|
|
201
198
|
method: "GET",
|
|
202
199
|
dispatcher: agent,
|
|
203
200
|
...requestInit
|
|
@@ -330,9 +327,15 @@ var Provider = class {
|
|
|
330
327
|
};
|
|
331
328
|
|
|
332
329
|
// src/core/generator/index.ts
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
330
|
+
import { writeFile } from "fs/promises";
|
|
331
|
+
import { format } from "prettier";
|
|
332
|
+
import {
|
|
333
|
+
addSyntheticLeadingComment,
|
|
334
|
+
createPrinter,
|
|
335
|
+
factory as t,
|
|
336
|
+
NodeFlags,
|
|
337
|
+
SyntaxKind
|
|
338
|
+
} from "typescript";
|
|
336
339
|
var Generator = class _Generator {
|
|
337
340
|
/**
|
|
338
341
|
* Converts an array of TypeScript statements into a formatted string of code.
|
|
@@ -345,16 +348,16 @@ var Generator = class _Generator {
|
|
|
345
348
|
if (statements.length === 0) {
|
|
346
349
|
return "// No api declaration found.";
|
|
347
350
|
}
|
|
348
|
-
const sourceFile =
|
|
351
|
+
const sourceFile = t.createSourceFile(
|
|
349
352
|
statements,
|
|
350
|
-
|
|
351
|
-
|
|
353
|
+
t.createToken(SyntaxKind.EndOfFileToken),
|
|
354
|
+
NodeFlags.None
|
|
352
355
|
);
|
|
353
|
-
return
|
|
356
|
+
return createPrinter().printFile(sourceFile);
|
|
354
357
|
}
|
|
355
358
|
static async write(code, filepath) {
|
|
356
359
|
try {
|
|
357
|
-
await
|
|
360
|
+
await writeFile(filepath, code);
|
|
358
361
|
} catch (error) {
|
|
359
362
|
console.error(error);
|
|
360
363
|
}
|
|
@@ -366,7 +369,7 @@ var Generator = class _Generator {
|
|
|
366
369
|
* @param path - The base path string containing placeholders.
|
|
367
370
|
* @param parameters - Array of parameter objects defining the parameters.
|
|
368
371
|
* @param basePath - Optional base path to prepend (default: "").
|
|
369
|
-
* @returns A TypeScript template
|
|
372
|
+
* @returns A TypeScript template expressi
|
|
370
373
|
*/
|
|
371
374
|
static toUrlTemplate(path, parameters, basePath = "") {
|
|
372
375
|
const queryParameters = parameters.filter(
|
|
@@ -374,25 +377,25 @@ var Generator = class _Generator {
|
|
|
374
377
|
);
|
|
375
378
|
if (queryParameters.length > 0) {
|
|
376
379
|
const queryString = queryParameters.map(
|
|
377
|
-
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${qp.name}}`
|
|
380
|
+
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${Base.camelCase(Base.normalize(qp.name))}}`
|
|
378
381
|
).join("");
|
|
379
382
|
path += queryString;
|
|
380
383
|
}
|
|
381
384
|
const pathSegments = path.replaceAll("{", "${").split("$").filter(Boolean);
|
|
382
385
|
if (pathSegments.length === 1) {
|
|
383
|
-
return
|
|
386
|
+
return t.createNoSubstitutionTemplateLiteral(basePath + path);
|
|
384
387
|
}
|
|
385
|
-
return
|
|
386
|
-
|
|
388
|
+
return t.createTemplateExpression(
|
|
389
|
+
t.createTemplateHead(basePath + pathSegments[0]),
|
|
387
390
|
pathSegments.slice(1).map((segment, index) => {
|
|
388
391
|
const match = /^{(.+)}(.+)?/gm.exec(segment);
|
|
389
392
|
const isLastSegment = index === pathSegments.length - 2;
|
|
390
393
|
if (!match) {
|
|
391
394
|
throw new Error(`Invalid path segment: ${segment}`);
|
|
392
395
|
}
|
|
393
|
-
return
|
|
394
|
-
|
|
395
|
-
!isLastSegment ?
|
|
396
|
+
return t.createTemplateSpan(
|
|
397
|
+
t.createIdentifier(match[1]),
|
|
398
|
+
!isLastSegment ? t.createTemplateMiddle(match[2]) : t.createTemplateTail(match[2] || "")
|
|
396
399
|
);
|
|
397
400
|
})
|
|
398
401
|
);
|
|
@@ -410,9 +413,9 @@ var Generator = class _Generator {
|
|
|
410
413
|
return comment.tag ? ` @${comment.tag} ${comment.comment ?? ""}` : ` ${comment.comment}`;
|
|
411
414
|
};
|
|
412
415
|
const formattedComments = "*\n" + comments.map(formatComment).join("\n").trim() + "\n";
|
|
413
|
-
|
|
416
|
+
addSyntheticLeadingComment(
|
|
414
417
|
node,
|
|
415
|
-
|
|
418
|
+
SyntaxKind.MultiLineCommentTrivia,
|
|
416
419
|
formattedComments,
|
|
417
420
|
true
|
|
418
421
|
);
|
|
@@ -432,10 +435,10 @@ var Generator = class _Generator {
|
|
|
432
435
|
return nonArraySchema.format === "blob" /* blob */ || nonArraySchema.format === "binary" /* binary */ || nonArraySchema.type === "file" /* file */;
|
|
433
436
|
}
|
|
434
437
|
static toRequestBodyTypeNode(schema) {
|
|
435
|
-
return
|
|
438
|
+
return t.createParameterDeclaration(
|
|
436
439
|
void 0,
|
|
437
440
|
void 0,
|
|
438
|
-
|
|
441
|
+
t.createIdentifier("req"),
|
|
439
442
|
void 0,
|
|
440
443
|
this.toTypeNode(schema)
|
|
441
444
|
);
|
|
@@ -444,8 +447,8 @@ var Generator = class _Generator {
|
|
|
444
447
|
const { type, ref } = schema;
|
|
445
448
|
if (ref) {
|
|
446
449
|
const identify = Base.ref2name(ref);
|
|
447
|
-
return
|
|
448
|
-
|
|
450
|
+
return t.createTypeReferenceNode(
|
|
451
|
+
t.createIdentifier(
|
|
449
452
|
identify === "unknown" ? identify : Base.upperCamelCase(identify)
|
|
450
453
|
)
|
|
451
454
|
);
|
|
@@ -453,24 +456,24 @@ var Generator = class _Generator {
|
|
|
453
456
|
switch (type) {
|
|
454
457
|
case "array" /* array */:
|
|
455
458
|
const { items } = schema;
|
|
456
|
-
return
|
|
459
|
+
return t.createArrayTypeNode(this.toTypeNode(items));
|
|
457
460
|
case "object" /* object */:
|
|
458
461
|
const propsCount = Object.keys(schema.properties ?? {}).length;
|
|
459
462
|
if (!schema.properties || propsCount === 0) {
|
|
460
|
-
return
|
|
461
|
-
|
|
462
|
-
|
|
463
|
+
return t.createTypeReferenceNode(t.createIdentifier("Record"), [
|
|
464
|
+
t.createToken(SyntaxKind.StringKeyword),
|
|
465
|
+
t.createToken(SyntaxKind.UnknownKeyword)
|
|
463
466
|
]);
|
|
464
467
|
}
|
|
465
468
|
const props = Object.keys(schema.properties);
|
|
466
|
-
return
|
|
469
|
+
return t.createTypeLiteralNode(
|
|
467
470
|
props.map((propKey) => {
|
|
468
471
|
const propSchema = schema.properties[propKey];
|
|
469
|
-
return
|
|
472
|
+
return t.createPropertySignature(
|
|
470
473
|
void 0,
|
|
471
|
-
|
|
474
|
+
t.createStringLiteral(propKey),
|
|
472
475
|
// When field is required, a refrence or binary value, don't add question mark.
|
|
473
|
-
schema.required || schema.ref || this.isBinarySchema(schema) ? void 0 :
|
|
476
|
+
schema.required || schema.ref || this.isBinarySchema(schema) ? void 0 : t.createToken(SyntaxKind.QuestionToken),
|
|
474
477
|
this.toTypeNode(propSchema)
|
|
475
478
|
);
|
|
476
479
|
})
|
|
@@ -478,18 +481,18 @@ var Generator = class _Generator {
|
|
|
478
481
|
case "integer" /* integer */:
|
|
479
482
|
case "number" /* number */:
|
|
480
483
|
if (schema.enum) {
|
|
481
|
-
return
|
|
484
|
+
return t.createUnionTypeNode(
|
|
482
485
|
schema.enum.map(
|
|
483
|
-
(e) =>
|
|
486
|
+
(e) => t.createLiteralTypeNode(t.createNumericLiteral(e))
|
|
484
487
|
)
|
|
485
488
|
);
|
|
486
489
|
}
|
|
487
|
-
return
|
|
490
|
+
return t.createToken(SyntaxKind.NumberKeyword);
|
|
488
491
|
// case NonArraySchemaType.string:
|
|
489
492
|
case "boolean" /* boolean */:
|
|
490
|
-
return
|
|
493
|
+
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
491
494
|
case "file" /* file */:
|
|
492
|
-
return
|
|
495
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
493
496
|
default:
|
|
494
497
|
const {
|
|
495
498
|
format: format2,
|
|
@@ -501,63 +504,63 @@ var Generator = class _Generator {
|
|
|
501
504
|
} = schema;
|
|
502
505
|
switch (format2) {
|
|
503
506
|
case "number" /* number */:
|
|
504
|
-
return
|
|
507
|
+
return t.createToken(SyntaxKind.NumberKeyword);
|
|
505
508
|
case "string" /* string */:
|
|
506
|
-
return
|
|
509
|
+
return t.createToken(SyntaxKind.StringKeyword);
|
|
507
510
|
case "boolean" /* boolean */:
|
|
508
|
-
return
|
|
511
|
+
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
509
512
|
case "blob" /* blob */:
|
|
510
513
|
case "binary" /* binary */:
|
|
511
|
-
return
|
|
514
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
512
515
|
default:
|
|
513
516
|
}
|
|
514
517
|
if (enum_) {
|
|
515
|
-
return
|
|
518
|
+
return t.createUnionTypeNode(
|
|
516
519
|
enum_.map(
|
|
517
|
-
(e) =>
|
|
520
|
+
(e) => t.createLiteralTypeNode(t.createStringLiteral(e))
|
|
518
521
|
)
|
|
519
522
|
);
|
|
520
523
|
}
|
|
521
524
|
if (type2 === "string" /* string */) {
|
|
522
|
-
return
|
|
525
|
+
return t.createToken(SyntaxKind.StringKeyword);
|
|
523
526
|
}
|
|
524
527
|
if (oneOf) {
|
|
525
|
-
return
|
|
528
|
+
return t.createUnionTypeNode(
|
|
526
529
|
oneOf.map((schema2) => this.toTypeNode(schema2))
|
|
527
530
|
);
|
|
528
531
|
}
|
|
529
532
|
if (anyOf) {
|
|
530
|
-
return
|
|
533
|
+
return t.createUnionTypeNode(
|
|
531
534
|
anyOf.map((schema2) => this.toTypeNode(schema2))
|
|
532
535
|
);
|
|
533
536
|
}
|
|
534
537
|
if (allOf) {
|
|
535
|
-
return
|
|
538
|
+
return t.createIntersectionTypeNode(
|
|
536
539
|
allOf.map((schema2) => this.toTypeNode(schema2))
|
|
537
540
|
);
|
|
538
541
|
}
|
|
539
542
|
if (type2 && typeof type2 === "string") {
|
|
540
|
-
return
|
|
541
|
-
type2 !== "unknown" ?
|
|
543
|
+
return t.createTypeReferenceNode(
|
|
544
|
+
type2 !== "unknown" && type2 !== "null" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
|
|
542
545
|
);
|
|
543
546
|
}
|
|
544
547
|
}
|
|
545
|
-
return
|
|
548
|
+
return t.createToken(SyntaxKind.UnknownKeyword);
|
|
546
549
|
}
|
|
547
550
|
static toDeclarationNode(parameters) {
|
|
548
551
|
const objectElements = [];
|
|
549
552
|
const typeObjectElements = [];
|
|
550
553
|
for (const parameter of parameters) {
|
|
551
554
|
if (parameter.ref) {
|
|
552
|
-
|
|
555
|
+
t.createParameterDeclaration(
|
|
553
556
|
void 0,
|
|
554
557
|
void 0,
|
|
555
|
-
|
|
558
|
+
t.createIdentifier(
|
|
556
559
|
Base.camelCase(Base.normalize(Base.ref2name(parameter.ref)))
|
|
557
560
|
),
|
|
558
561
|
void 0,
|
|
559
|
-
|
|
560
|
-
|
|
562
|
+
t.createTypeReferenceNode(
|
|
563
|
+
t.createIdentifier(
|
|
561
564
|
Base.upperCamelCase(Base.normalize(Base.ref2name(parameter.ref)))
|
|
562
565
|
)
|
|
563
566
|
),
|
|
@@ -566,72 +569,67 @@ var Generator = class _Generator {
|
|
|
566
569
|
} else {
|
|
567
570
|
const { name, schema, required } = parameter;
|
|
568
571
|
objectElements.push(
|
|
569
|
-
|
|
572
|
+
t.createBindingElement(
|
|
570
573
|
void 0,
|
|
571
574
|
void 0,
|
|
572
|
-
|
|
575
|
+
t.createIdentifier(Base.camelCase(Base.normalize(name)))
|
|
573
576
|
)
|
|
574
577
|
);
|
|
575
578
|
typeObjectElements.push(
|
|
576
|
-
|
|
579
|
+
t.createPropertySignature(
|
|
577
580
|
[],
|
|
578
|
-
|
|
579
|
-
required ? void 0 :
|
|
580
|
-
!schema ?
|
|
581
|
+
t.createIdentifier(Base.camelCase(Base.normalize(name))),
|
|
582
|
+
required ? void 0 : t.createToken(SyntaxKind.QuestionToken),
|
|
583
|
+
!schema ? t.createToken(SyntaxKind.UnknownKeyword) : this.toTypeNode(schema)
|
|
581
584
|
)
|
|
582
585
|
);
|
|
583
586
|
}
|
|
584
587
|
}
|
|
585
|
-
return
|
|
588
|
+
return t.createParameterDeclaration(
|
|
586
589
|
void 0,
|
|
587
590
|
void 0,
|
|
588
|
-
|
|
591
|
+
t.createObjectBindingPattern(objectElements),
|
|
589
592
|
void 0,
|
|
590
|
-
|
|
593
|
+
t.createTypeLiteralNode(typeObjectElements),
|
|
591
594
|
void 0
|
|
592
595
|
);
|
|
593
596
|
}
|
|
594
597
|
static toFormDataStatement(parameters, requestBody) {
|
|
595
598
|
const statements = [];
|
|
596
|
-
const fdDeclaration =
|
|
599
|
+
const fdDeclaration = t.createVariableStatement(
|
|
597
600
|
void 0,
|
|
598
|
-
|
|
601
|
+
t.createVariableDeclarationList(
|
|
599
602
|
[
|
|
600
|
-
|
|
601
|
-
|
|
603
|
+
t.createVariableDeclaration(
|
|
604
|
+
t.createIdentifier("fd"),
|
|
602
605
|
void 0,
|
|
603
606
|
void 0,
|
|
604
|
-
|
|
605
|
-
|
|
607
|
+
t.createNewExpression(
|
|
608
|
+
t.createIdentifier("FormData"),
|
|
606
609
|
void 0,
|
|
607
610
|
[]
|
|
608
611
|
)
|
|
609
612
|
)
|
|
610
613
|
],
|
|
611
|
-
|
|
614
|
+
NodeFlags.Const
|
|
612
615
|
)
|
|
613
616
|
);
|
|
614
617
|
statements.push(fdDeclaration);
|
|
615
|
-
parameters.
|
|
616
|
-
(parameter) => parameter.ref !== void 0 && (parameter.in === "formData" /* formData */ || parameter.schema && this.isBinarySchema(parameter.schema))
|
|
617
|
-
).forEach((parameter) => {
|
|
618
|
+
parameters.forEach((parameter) => {
|
|
618
619
|
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")
|
|
620
|
+
t.createExpressionStatement(
|
|
621
|
+
t.createBinaryExpression(
|
|
622
|
+
t.createIdentifier(parameter.name),
|
|
623
|
+
t.createToken(SyntaxKind.AmpersandAmpersandToken),
|
|
624
|
+
t.createCallExpression(
|
|
625
|
+
t.createPropertyAccessExpression(
|
|
626
|
+
t.createIdentifier("fd"),
|
|
627
|
+
t.createIdentifier("append")
|
|
630
628
|
),
|
|
631
629
|
void 0,
|
|
632
630
|
[
|
|
633
|
-
|
|
634
|
-
|
|
631
|
+
t.createStringLiteral(parameter.name),
|
|
632
|
+
t.createIdentifier(parameter.name)
|
|
635
633
|
]
|
|
636
634
|
)
|
|
637
635
|
)
|
|
@@ -643,30 +641,36 @@ var Generator = class _Generator {
|
|
|
643
641
|
const schemaByKey = requestBody.properties[key];
|
|
644
642
|
if (schemaByKey.type === "array" /* array */ && this.isBinarySchema(schemaByKey)) {
|
|
645
643
|
statements.push(
|
|
646
|
-
|
|
644
|
+
t.createForOfStatement(
|
|
647
645
|
void 0,
|
|
648
|
-
|
|
649
|
-
[
|
|
650
|
-
|
|
646
|
+
t.createVariableDeclarationList(
|
|
647
|
+
[t.createVariableDeclaration("file")],
|
|
648
|
+
NodeFlags.Const
|
|
651
649
|
),
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
650
|
+
t.createElementAccessExpression(
|
|
651
|
+
t.createIdentifier("req"),
|
|
652
|
+
t.createStringLiteral(key)
|
|
655
653
|
),
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
654
|
+
t.createBlock([
|
|
655
|
+
t.createExpressionStatement(
|
|
656
|
+
t.createCallExpression(
|
|
657
|
+
t.createPropertyAccessExpression(
|
|
658
|
+
t.createIdentifier("fd"),
|
|
659
|
+
t.createIdentifier("append")
|
|
662
660
|
),
|
|
663
661
|
[],
|
|
664
662
|
[
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
663
|
+
t.createStringLiteral(key),
|
|
664
|
+
t.createIdentifier("file"),
|
|
665
|
+
t.createPropertyAccessExpression(
|
|
666
|
+
t.createAsExpression(
|
|
667
|
+
t.createIdentifier("file"),
|
|
668
|
+
t.createTypeReferenceNode(
|
|
669
|
+
t.createIdentifier("File"),
|
|
670
|
+
void 0
|
|
671
|
+
)
|
|
672
|
+
),
|
|
673
|
+
t.createIdentifier("name")
|
|
670
674
|
)
|
|
671
675
|
]
|
|
672
676
|
)
|
|
@@ -677,25 +681,25 @@ var Generator = class _Generator {
|
|
|
677
681
|
} else {
|
|
678
682
|
if (schemaByKey.required) {
|
|
679
683
|
statements.push(
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
684
|
+
t.createExpressionStatement(
|
|
685
|
+
t.createCallExpression(
|
|
686
|
+
t.createPropertyAccessExpression(
|
|
687
|
+
t.createIdentifier("fd"),
|
|
688
|
+
t.createIdentifier("append")
|
|
685
689
|
),
|
|
686
690
|
void 0,
|
|
687
691
|
[
|
|
688
|
-
|
|
689
|
-
schemaByKey.type === "string" ?
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
) :
|
|
693
|
-
|
|
692
|
+
t.createStringLiteral(key),
|
|
693
|
+
schemaByKey.type === "string" ? t.createElementAccessExpression(
|
|
694
|
+
t.createIdentifier("req"),
|
|
695
|
+
t.createStringLiteral(key)
|
|
696
|
+
) : t.createCallExpression(
|
|
697
|
+
t.createIdentifier("String"),
|
|
694
698
|
void 0,
|
|
695
699
|
[
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
700
|
+
t.createElementAccessExpression(
|
|
701
|
+
t.createIdentifier("req"),
|
|
702
|
+
t.createStringLiteral(key)
|
|
699
703
|
)
|
|
700
704
|
]
|
|
701
705
|
)
|
|
@@ -705,31 +709,31 @@ var Generator = class _Generator {
|
|
|
705
709
|
);
|
|
706
710
|
} else {
|
|
707
711
|
statements.push(
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
712
|
+
t.createExpressionStatement(
|
|
713
|
+
t.createBinaryExpression(
|
|
714
|
+
t.createElementAccessExpression(
|
|
715
|
+
t.createIdentifier("req"),
|
|
716
|
+
t.createStringLiteral(key)
|
|
713
717
|
),
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
718
|
+
t.createToken(SyntaxKind.AmpersandAmpersandToken),
|
|
719
|
+
t.createCallExpression(
|
|
720
|
+
t.createPropertyAccessExpression(
|
|
721
|
+
t.createIdentifier("fd"),
|
|
722
|
+
t.createIdentifier("append")
|
|
719
723
|
),
|
|
720
724
|
void 0,
|
|
721
725
|
[
|
|
722
|
-
|
|
723
|
-
schemaByKey.type === "string" ?
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
) :
|
|
727
|
-
|
|
726
|
+
t.createStringLiteral(key),
|
|
727
|
+
schemaByKey.type === "string" ? t.createElementAccessExpression(
|
|
728
|
+
t.createIdentifier("req"),
|
|
729
|
+
t.createStringLiteral(key)
|
|
730
|
+
) : t.createCallExpression(
|
|
731
|
+
t.createIdentifier("String"),
|
|
728
732
|
void 0,
|
|
729
733
|
[
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
734
|
+
t.createElementAccessExpression(
|
|
735
|
+
t.createIdentifier("req"),
|
|
736
|
+
t.createStringLiteral(key)
|
|
733
737
|
)
|
|
734
738
|
]
|
|
735
739
|
)
|
|
@@ -750,25 +754,28 @@ var Generator = class _Generator {
|
|
|
750
754
|
);
|
|
751
755
|
const shouldParseResponseToJSON = "application/json" === response?.type;
|
|
752
756
|
const isRequestBodyBinary = requestBody?.schema && requestBody.schema.type === "array" /* array */ && this.isBinarySchema(requestBody.schema);
|
|
753
|
-
const
|
|
754
|
-
(p) => p.in === "formData" /* formData */
|
|
757
|
+
const parametersShouldPutInFormData = parameters.filter(
|
|
758
|
+
(p) => p.in === "formData" /* formData */ || p.schema && this.isBinarySchema(p.schema)
|
|
755
759
|
);
|
|
756
|
-
const
|
|
757
|
-
(p) => p
|
|
760
|
+
const parametersShouldNotPutInFormData = parameters.filter(
|
|
761
|
+
(p) => !parametersShouldPutInFormData.includes(p)
|
|
758
762
|
);
|
|
759
|
-
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema
|
|
763
|
+
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some(
|
|
760
764
|
(p) => this.isBinarySchema(p)
|
|
761
765
|
);
|
|
762
766
|
const hasBinaryInParameters = parameters.some(
|
|
763
767
|
(p) => p?.schema && this.isBinarySchema(p.schema)
|
|
764
768
|
);
|
|
765
|
-
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary ||
|
|
766
|
-
return
|
|
767
|
-
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
769
|
+
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
|
|
770
|
+
return t.createBlock([
|
|
771
|
+
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
772
|
+
parametersShouldPutInFormData,
|
|
773
|
+
requestBody?.schema
|
|
774
|
+
) : [],
|
|
768
775
|
...adapter.client(
|
|
769
776
|
uri,
|
|
770
777
|
method,
|
|
771
|
-
|
|
778
|
+
parametersShouldNotPutInFormData,
|
|
772
779
|
requestBody,
|
|
773
780
|
response,
|
|
774
781
|
adapter,
|
|
@@ -784,15 +791,15 @@ var Generator = class _Generator {
|
|
|
784
791
|
for (const enumObject of enums) {
|
|
785
792
|
enumNames.push(Base.capitalize(enumObject.name));
|
|
786
793
|
statements.push(
|
|
787
|
-
|
|
788
|
-
[
|
|
789
|
-
|
|
794
|
+
t.createEnumDeclaration(
|
|
795
|
+
[t.createToken(SyntaxKind.ExportKeyword)],
|
|
796
|
+
t.createIdentifier(Base.upperCamelCase(enumObject.name)),
|
|
790
797
|
enumObject.enum.map((member) => {
|
|
791
|
-
return
|
|
792
|
-
|
|
798
|
+
return t.createEnumMember(
|
|
799
|
+
t.createStringLiteral(
|
|
793
800
|
typeof member === "string" ? member : `${member}_`
|
|
794
801
|
),
|
|
795
|
-
typeof member === "string" ?
|
|
802
|
+
typeof member === "string" ? t.createStringLiteral(member) : t.createNumericLiteral(member)
|
|
796
803
|
);
|
|
797
804
|
})
|
|
798
805
|
)
|
|
@@ -802,9 +809,9 @@ var Generator = class _Generator {
|
|
|
802
809
|
if (Object.hasOwnProperty.call(schemas, schemaKey) && !enumNames.includes(Base.upperCamelCase(schemaKey))) {
|
|
803
810
|
const schema = schemas[schemaKey];
|
|
804
811
|
statements.push(
|
|
805
|
-
|
|
806
|
-
[
|
|
807
|
-
|
|
812
|
+
t.createTypeAliasDeclaration(
|
|
813
|
+
[t.createModifier(SyntaxKind.ExportKeyword)],
|
|
814
|
+
t.createIdentifier(Base.upperCamelCase(schemaKey)),
|
|
808
815
|
void 0,
|
|
809
816
|
this.toTypeNode(schema)
|
|
810
817
|
)
|
|
@@ -830,10 +837,10 @@ var Generator = class _Generator {
|
|
|
830
837
|
}
|
|
831
838
|
const shouldAddExtraMethodNameSuffix = requestBody.length > 1;
|
|
832
839
|
for (const req of requestBody) {
|
|
833
|
-
const statement =
|
|
840
|
+
const statement = t.createFunctionDeclaration(
|
|
834
841
|
[
|
|
835
|
-
|
|
836
|
-
|
|
842
|
+
t.createModifier(SyntaxKind.ExportKeyword),
|
|
843
|
+
t.createModifier(SyntaxKind.AsyncKeyword)
|
|
837
844
|
],
|
|
838
845
|
void 0,
|
|
839
846
|
Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
|
|
@@ -873,7 +880,7 @@ var Generator = class _Generator {
|
|
|
873
880
|
return statements;
|
|
874
881
|
}
|
|
875
882
|
static async prettier(code) {
|
|
876
|
-
return await
|
|
883
|
+
return await format(code, {
|
|
877
884
|
parser: "typescript"
|
|
878
885
|
});
|
|
879
886
|
}
|
|
@@ -891,7 +898,7 @@ var Generator = class _Generator {
|
|
|
891
898
|
};
|
|
892
899
|
|
|
893
900
|
// src/core/client/axios.ts
|
|
894
|
-
|
|
901
|
+
import { factory as t2 } from "typescript";
|
|
895
902
|
var AxiosAdapter = class extends Adapter {
|
|
896
903
|
/**
|
|
897
904
|
* Name of the field used to specify the HTTP method in the request configuration.
|
|
@@ -925,30 +932,30 @@ var AxiosAdapter = class extends Adapter {
|
|
|
925
932
|
const inBody = parameters.filter((p) => !p.in || p.in === "body");
|
|
926
933
|
const inHeader = parameters.filter((p) => p.in === "header");
|
|
927
934
|
const toLiterlExpression = () => {
|
|
928
|
-
return
|
|
935
|
+
return t2.createObjectLiteralExpression(
|
|
929
936
|
[
|
|
930
937
|
// Set the HTTP method
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
938
|
+
t2.createPropertyAssignment(
|
|
939
|
+
t2.createIdentifier(adapter.methodFieldName),
|
|
940
|
+
t2.createStringLiteral(method.toUpperCase())
|
|
934
941
|
)
|
|
935
942
|
].concat(
|
|
936
943
|
// Add headers if there are any
|
|
937
|
-
inHeader.length > 0 ?
|
|
938
|
-
|
|
939
|
-
|
|
944
|
+
inHeader.length > 0 ? t2.createPropertyAssignment(
|
|
945
|
+
t2.createIdentifier(adapter.headersFieldName),
|
|
946
|
+
t2.createObjectLiteralExpression(
|
|
940
947
|
inHeader.map(
|
|
941
|
-
(p) =>
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
948
|
+
(p) => t2.createPropertyAssignment(
|
|
949
|
+
t2.createStringLiteral(p.name),
|
|
950
|
+
t2.createCallExpression(
|
|
951
|
+
t2.createIdentifier("encodeURIComponent"),
|
|
945
952
|
void 0,
|
|
946
953
|
[
|
|
947
|
-
|
|
948
|
-
|
|
954
|
+
t2.createCallExpression(
|
|
955
|
+
t2.createIdentifier("String"),
|
|
949
956
|
void 0,
|
|
950
957
|
[
|
|
951
|
-
|
|
958
|
+
t2.createIdentifier(
|
|
952
959
|
Base.camelCase(Base.normalize(p.name))
|
|
953
960
|
)
|
|
954
961
|
]
|
|
@@ -961,18 +968,18 @@ var AxiosAdapter = class extends Adapter {
|
|
|
961
968
|
) : []
|
|
962
969
|
).concat(
|
|
963
970
|
// Add body if needed
|
|
964
|
-
shouldUseFormData || inBody.length > 0 || requestBody?.schema ?
|
|
965
|
-
|
|
966
|
-
shouldUseFormData ?
|
|
971
|
+
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t2.createPropertyAssignment(
|
|
972
|
+
t2.createIdentifier(adapter.bodyFieldName),
|
|
973
|
+
shouldUseFormData ? t2.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t2.createIdentifier("req") : t2.createIdentifier("req")
|
|
967
974
|
) : []
|
|
968
975
|
),
|
|
969
976
|
true
|
|
970
977
|
);
|
|
971
978
|
};
|
|
972
979
|
statements.push(
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
980
|
+
t2.createReturnStatement(
|
|
981
|
+
t2.createCallExpression(
|
|
982
|
+
t2.createIdentifier(adapter.name),
|
|
976
983
|
response?.schema ? [
|
|
977
984
|
Generator.toTypeNode(
|
|
978
985
|
response.schema
|
|
@@ -987,7 +994,7 @@ var AxiosAdapter = class extends Adapter {
|
|
|
987
994
|
};
|
|
988
995
|
|
|
989
996
|
// src/core/client/fetch.ts
|
|
990
|
-
|
|
997
|
+
import { factory as t3, SyntaxKind as SyntaxKind2 } from "typescript";
|
|
991
998
|
var FetchAdapter = class extends Adapter {
|
|
992
999
|
methodFieldName = "method";
|
|
993
1000
|
bodyFieldName = "body";
|
|
@@ -1011,30 +1018,30 @@ var FetchAdapter = class extends Adapter {
|
|
|
1011
1018
|
const inBody = parameters.filter((p) => !p.in || p.in === "body");
|
|
1012
1019
|
const inHeader = parameters.filter((p) => p.in === "header");
|
|
1013
1020
|
const toLiterlExpression = () => {
|
|
1014
|
-
return
|
|
1021
|
+
return t3.createObjectLiteralExpression(
|
|
1015
1022
|
[
|
|
1016
1023
|
// Set the HTTP method
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1024
|
+
t3.createPropertyAssignment(
|
|
1025
|
+
t3.createIdentifier(adapter.methodFieldName),
|
|
1026
|
+
t3.createStringLiteral(method.toUpperCase())
|
|
1020
1027
|
)
|
|
1021
1028
|
].concat(
|
|
1022
1029
|
// Add headers if there are any
|
|
1023
|
-
inHeader.length > 0 ?
|
|
1024
|
-
|
|
1025
|
-
|
|
1030
|
+
inHeader.length > 0 ? t3.createPropertyAssignment(
|
|
1031
|
+
t3.createIdentifier(adapter.headersFieldName),
|
|
1032
|
+
t3.createObjectLiteralExpression(
|
|
1026
1033
|
inHeader.map(
|
|
1027
|
-
(p) =>
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1034
|
+
(p) => t3.createPropertyAssignment(
|
|
1035
|
+
t3.createStringLiteral(p.name),
|
|
1036
|
+
t3.createCallExpression(
|
|
1037
|
+
t3.createIdentifier("encodeURIComponent"),
|
|
1031
1038
|
void 0,
|
|
1032
1039
|
[
|
|
1033
|
-
|
|
1034
|
-
|
|
1040
|
+
t3.createCallExpression(
|
|
1041
|
+
t3.createIdentifier("String"),
|
|
1035
1042
|
void 0,
|
|
1036
1043
|
[
|
|
1037
|
-
|
|
1044
|
+
t3.createIdentifier(
|
|
1038
1045
|
Base.camelCase(Base.normalize(p.name))
|
|
1039
1046
|
)
|
|
1040
1047
|
]
|
|
@@ -1047,19 +1054,19 @@ var FetchAdapter = class extends Adapter {
|
|
|
1047
1054
|
) : []
|
|
1048
1055
|
).concat(
|
|
1049
1056
|
// Add body if needed
|
|
1050
|
-
shouldUseFormData || inBody.length > 0 || requestBody?.schema ?
|
|
1051
|
-
|
|
1052
|
-
shouldUseFormData ?
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1057
|
+
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t3.createPropertyAssignment(
|
|
1058
|
+
t3.createIdentifier(adapter.bodyFieldName),
|
|
1059
|
+
shouldUseFormData ? t3.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t3.createCallExpression(
|
|
1060
|
+
t3.createPropertyAccessExpression(
|
|
1061
|
+
t3.createIdentifier("JSON"),
|
|
1062
|
+
t3.createIdentifier("stringify")
|
|
1056
1063
|
),
|
|
1057
1064
|
[],
|
|
1058
1065
|
[
|
|
1059
|
-
requestBody ?
|
|
1066
|
+
requestBody ? t3.createIdentifier("req") : t3.createObjectLiteralExpression(
|
|
1060
1067
|
inBody.map(
|
|
1061
|
-
(b) =>
|
|
1062
|
-
|
|
1068
|
+
(b) => t3.createShorthandPropertyAssignment(
|
|
1069
|
+
t3.createIdentifier(b.name)
|
|
1063
1070
|
)
|
|
1064
1071
|
),
|
|
1065
1072
|
true
|
|
@@ -1067,7 +1074,7 @@ var FetchAdapter = class extends Adapter {
|
|
|
1067
1074
|
]
|
|
1068
1075
|
) : (
|
|
1069
1076
|
// One File parameter
|
|
1070
|
-
|
|
1077
|
+
t3.createIdentifier("req")
|
|
1071
1078
|
)
|
|
1072
1079
|
) : []
|
|
1073
1080
|
),
|
|
@@ -1075,57 +1082,68 @@ var FetchAdapter = class extends Adapter {
|
|
|
1075
1082
|
);
|
|
1076
1083
|
};
|
|
1077
1084
|
statements.push(
|
|
1078
|
-
|
|
1085
|
+
t3.createReturnStatement(
|
|
1079
1086
|
shouldUseJSONResponse ? (
|
|
1080
1087
|
// Handle JSON response with proper type checking
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1088
|
+
t3.createCallExpression(
|
|
1089
|
+
t3.createPropertyAccessExpression(
|
|
1090
|
+
t3.createCallExpression(
|
|
1091
|
+
t3.createIdentifier(adapter.name),
|
|
1085
1092
|
void 0,
|
|
1086
1093
|
[
|
|
1087
1094
|
Generator.toUrlTemplate(uri, parameters),
|
|
1088
1095
|
toLiterlExpression()
|
|
1089
1096
|
]
|
|
1090
1097
|
),
|
|
1091
|
-
|
|
1098
|
+
t3.createIdentifier("then")
|
|
1092
1099
|
),
|
|
1093
1100
|
void 0,
|
|
1094
1101
|
[
|
|
1095
|
-
|
|
1096
|
-
[
|
|
1102
|
+
t3.createArrowFunction(
|
|
1103
|
+
[t3.createModifier(SyntaxKind2.AsyncKeyword)],
|
|
1097
1104
|
[],
|
|
1098
1105
|
[
|
|
1099
|
-
|
|
1106
|
+
t3.createParameterDeclaration(
|
|
1100
1107
|
void 0,
|
|
1101
1108
|
void 0,
|
|
1102
|
-
|
|
1109
|
+
t3.createIdentifier("response")
|
|
1103
1110
|
)
|
|
1104
1111
|
],
|
|
1105
1112
|
void 0,
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1113
|
+
t3.createToken(SyntaxKind2.EqualsGreaterThanToken),
|
|
1114
|
+
response?.schema ? t3.createAsExpression(
|
|
1115
|
+
t3.createParenthesizedExpression(
|
|
1116
|
+
t3.createAwaitExpression(
|
|
1117
|
+
t3.createCallExpression(
|
|
1118
|
+
t3.createPropertyAccessExpression(
|
|
1119
|
+
t3.createIdentifier("response"),
|
|
1120
|
+
t3.createIdentifier("json")
|
|
1114
1121
|
),
|
|
1115
1122
|
void 0,
|
|
1116
1123
|
[]
|
|
1117
1124
|
)
|
|
1118
1125
|
)
|
|
1119
1126
|
),
|
|
1120
|
-
response?.schema ? Generator.toTypeNode(response.schema) :
|
|
1127
|
+
response?.schema ? Generator.toTypeNode(response.schema) : t3.createToken(SyntaxKind2.UnknownKeyword)
|
|
1128
|
+
) : t3.createParenthesizedExpression(
|
|
1129
|
+
t3.createAwaitExpression(
|
|
1130
|
+
t3.createCallExpression(
|
|
1131
|
+
t3.createPropertyAccessExpression(
|
|
1132
|
+
t3.createIdentifier("response"),
|
|
1133
|
+
t3.createIdentifier("json")
|
|
1134
|
+
),
|
|
1135
|
+
void 0,
|
|
1136
|
+
[]
|
|
1137
|
+
)
|
|
1138
|
+
)
|
|
1121
1139
|
)
|
|
1122
1140
|
)
|
|
1123
1141
|
]
|
|
1124
1142
|
)
|
|
1125
1143
|
) : (
|
|
1126
1144
|
// Simple fetch call without JSON parsing
|
|
1127
|
-
|
|
1128
|
-
|
|
1145
|
+
t3.createCallExpression(
|
|
1146
|
+
t3.createIdentifier(adapter.name),
|
|
1129
1147
|
void 0,
|
|
1130
1148
|
[Generator.toUrlTemplate(uri, parameters), toLiterlExpression()]
|
|
1131
1149
|
)
|
|
@@ -1137,7 +1155,7 @@ var FetchAdapter = class extends Adapter {
|
|
|
1137
1155
|
};
|
|
1138
1156
|
|
|
1139
1157
|
// src/openapi/index.ts
|
|
1140
|
-
|
|
1158
|
+
import { createScopedLogger } from "@moccona/logger";
|
|
1141
1159
|
|
|
1142
1160
|
// src/openapi/V2.ts
|
|
1143
1161
|
var V2 = class {
|
|
@@ -1228,13 +1246,25 @@ var V2 = class {
|
|
|
1228
1246
|
enum: enum_,
|
|
1229
1247
|
format: format2,
|
|
1230
1248
|
allOf: allOf?.map(
|
|
1231
|
-
(s) => Base.isRef(s) ? {
|
|
1249
|
+
(s) => Base.isRef(s) ? {
|
|
1250
|
+
...s,
|
|
1251
|
+
ref: s.$ref,
|
|
1252
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1253
|
+
} : this.toBaseSchema(s, enums)
|
|
1232
1254
|
),
|
|
1233
1255
|
anyOf: anyOf?.map(
|
|
1234
|
-
(s) => Base.isRef(s) ? {
|
|
1256
|
+
(s) => Base.isRef(s) ? {
|
|
1257
|
+
...s,
|
|
1258
|
+
ref: s.$ref,
|
|
1259
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1260
|
+
} : this.toBaseSchema(s, enums)
|
|
1235
1261
|
),
|
|
1236
1262
|
oneOf: oneOf?.map(
|
|
1237
|
-
(s) => Base.isRef(s) ? {
|
|
1263
|
+
(s) => Base.isRef(s) ? {
|
|
1264
|
+
...s,
|
|
1265
|
+
ref: s.$ref,
|
|
1266
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1267
|
+
} : this.toBaseSchema(s, enums)
|
|
1238
1268
|
),
|
|
1239
1269
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1240
1270
|
const propSchema = properties[p];
|
|
@@ -1264,7 +1294,8 @@ var V2 = class {
|
|
|
1264
1294
|
type,
|
|
1265
1295
|
items,
|
|
1266
1296
|
enum: enum_,
|
|
1267
|
-
properties
|
|
1297
|
+
properties,
|
|
1298
|
+
schema
|
|
1268
1299
|
} = parameter;
|
|
1269
1300
|
if (enum_) {
|
|
1270
1301
|
const type2 = Base.upperCamelCase(upLevelSchemaKey) + Base.upperCamelCase(name);
|
|
@@ -1286,17 +1317,36 @@ var V2 = class {
|
|
|
1286
1317
|
}
|
|
1287
1318
|
};
|
|
1288
1319
|
}
|
|
1320
|
+
if (items) {
|
|
1321
|
+
return {
|
|
1322
|
+
name,
|
|
1323
|
+
required,
|
|
1324
|
+
description,
|
|
1325
|
+
in: parameter.in,
|
|
1326
|
+
schema: {
|
|
1327
|
+
type,
|
|
1328
|
+
items
|
|
1329
|
+
}
|
|
1330
|
+
};
|
|
1331
|
+
}
|
|
1332
|
+
if (schema && Base.isRef(schema)) {
|
|
1333
|
+
return {
|
|
1334
|
+
name,
|
|
1335
|
+
required,
|
|
1336
|
+
description,
|
|
1337
|
+
in: parameter.in,
|
|
1338
|
+
schema: {
|
|
1339
|
+
type: Base.upperCamelCase(Base.ref2name(schema.$ref))
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
}
|
|
1289
1343
|
return {
|
|
1290
1344
|
name,
|
|
1291
1345
|
required,
|
|
1292
1346
|
description,
|
|
1293
1347
|
in: parameter.in,
|
|
1294
|
-
schema:
|
|
1295
|
-
type,
|
|
1296
|
-
items
|
|
1297
|
-
} : {
|
|
1348
|
+
schema: {
|
|
1298
1349
|
type,
|
|
1299
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
1300
1350
|
properties
|
|
1301
1351
|
}
|
|
1302
1352
|
};
|
|
@@ -1375,6 +1425,7 @@ var V2 = class {
|
|
|
1375
1425
|
if (code in responses2) {
|
|
1376
1426
|
const response = responses2[code];
|
|
1377
1427
|
const responseSchema = this.getResponseByRef(response);
|
|
1428
|
+
const inBodyOnlyHasBody = inBody && inBody.length === 1 && inBody[0].in === "body" && inBody[0].name === "body";
|
|
1378
1429
|
methodApis.push({
|
|
1379
1430
|
method,
|
|
1380
1431
|
operationId,
|
|
@@ -1383,7 +1434,12 @@ var V2 = class {
|
|
|
1383
1434
|
description: description_,
|
|
1384
1435
|
parameters: uniqueParameterName.map((name) => notInBody.find((p) => p.name === name)).filter(Boolean),
|
|
1385
1436
|
responses: responseSchema,
|
|
1386
|
-
requestBody: inBody.length > 0 ? [
|
|
1437
|
+
requestBody: inBody.length > 0 ? inBodyOnlyHasBody ? [
|
|
1438
|
+
{
|
|
1439
|
+
type: "application/json" /* JSON */,
|
|
1440
|
+
schema: inBody[0].schema
|
|
1441
|
+
}
|
|
1442
|
+
] : [
|
|
1387
1443
|
{
|
|
1388
1444
|
type: "application/json" /* JSON */,
|
|
1389
1445
|
schema: {
|
|
@@ -1591,13 +1647,25 @@ var V3 = class {
|
|
|
1591
1647
|
enum: enum_,
|
|
1592
1648
|
format: format2,
|
|
1593
1649
|
allOf: allOf?.map(
|
|
1594
|
-
(s) => Base.isRef(s) ? {
|
|
1650
|
+
(s) => Base.isRef(s) ? {
|
|
1651
|
+
...s,
|
|
1652
|
+
ref: s.$ref,
|
|
1653
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1654
|
+
} : this.toBaseSchema(s, enums)
|
|
1595
1655
|
),
|
|
1596
1656
|
anyOf: anyOf?.map(
|
|
1597
|
-
(s) => Base.isRef(s) ? {
|
|
1657
|
+
(s) => Base.isRef(s) ? {
|
|
1658
|
+
...s,
|
|
1659
|
+
ref: s.$ref,
|
|
1660
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1661
|
+
} : this.toBaseSchema(s, enums)
|
|
1598
1662
|
),
|
|
1599
1663
|
oneOf: oneOf?.map(
|
|
1600
|
-
(s) => Base.isRef(s) ? {
|
|
1664
|
+
(s) => Base.isRef(s) ? {
|
|
1665
|
+
...s,
|
|
1666
|
+
ref: s.$ref,
|
|
1667
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1668
|
+
} : this.toBaseSchema(s, enums)
|
|
1601
1669
|
),
|
|
1602
1670
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1603
1671
|
const propSchema = properties[p];
|
|
@@ -1894,13 +1962,25 @@ var V3_1 = class {
|
|
|
1894
1962
|
enum: enum_,
|
|
1895
1963
|
format: format2,
|
|
1896
1964
|
allOf: allOf?.map(
|
|
1897
|
-
(s) => Base.isRef(s) ? {
|
|
1965
|
+
(s) => Base.isRef(s) ? {
|
|
1966
|
+
...s,
|
|
1967
|
+
ref: s.$ref,
|
|
1968
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1969
|
+
} : this.toBaseSchema(s, enums)
|
|
1898
1970
|
),
|
|
1899
1971
|
anyOf: anyOf?.map(
|
|
1900
|
-
(s) => Base.isRef(s) ? {
|
|
1972
|
+
(s) => Base.isRef(s) ? {
|
|
1973
|
+
...s,
|
|
1974
|
+
ref: s.$ref,
|
|
1975
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1976
|
+
} : this.toBaseSchema(s, enums)
|
|
1901
1977
|
),
|
|
1902
1978
|
oneOf: oneOf?.map(
|
|
1903
|
-
(s) => Base.isRef(s) ? {
|
|
1979
|
+
(s) => Base.isRef(s) ? {
|
|
1980
|
+
...s,
|
|
1981
|
+
ref: s.$ref,
|
|
1982
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1983
|
+
} : this.toBaseSchema(s, enums)
|
|
1904
1984
|
),
|
|
1905
1985
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1906
1986
|
const propSchema = properties[p];
|
|
@@ -2030,10 +2110,10 @@ var V3_1 = class {
|
|
|
2030
2110
|
};
|
|
2031
2111
|
|
|
2032
2112
|
// src/openapi/index.ts
|
|
2033
|
-
var logger =
|
|
2113
|
+
var logger = createScopedLogger("OpenAPI");
|
|
2034
2114
|
function getDocVersion(doc) {
|
|
2035
|
-
const
|
|
2036
|
-
switch (
|
|
2115
|
+
const version = (doc.openapi || doc.swagger).slice(0, 3);
|
|
2116
|
+
switch (version) {
|
|
2037
2117
|
case "2.0":
|
|
2038
2118
|
return "v2" /* v2 */;
|
|
2039
2119
|
case "3.0":
|
|
@@ -2046,10 +2126,10 @@ function getDocVersion(doc) {
|
|
|
2046
2126
|
}
|
|
2047
2127
|
var OpenAPIProvider = class extends Provider {
|
|
2048
2128
|
parse(doc) {
|
|
2049
|
-
const
|
|
2050
|
-
logger.debug(`openapi version ${
|
|
2129
|
+
const version = getDocVersion(doc);
|
|
2130
|
+
logger.debug(`openapi version ${version}`);
|
|
2051
2131
|
let returnValue;
|
|
2052
|
-
switch (
|
|
2132
|
+
switch (version) {
|
|
2053
2133
|
case "v2" /* v2 */:
|
|
2054
2134
|
returnValue = new V2(doc).init();
|
|
2055
2135
|
break;
|
|
@@ -2060,7 +2140,7 @@ var OpenAPIProvider = class extends Provider {
|
|
|
2060
2140
|
returnValue = new V3_1(doc).init();
|
|
2061
2141
|
break;
|
|
2062
2142
|
default:
|
|
2063
|
-
logger.error(`Not a valid OpenAPI version ${
|
|
2143
|
+
logger.error(`Not a valid OpenAPI version ${version}`);
|
|
2064
2144
|
process.exit(1);
|
|
2065
2145
|
}
|
|
2066
2146
|
return returnValue;
|
|
@@ -2109,29 +2189,60 @@ async function codeGen(initOptions) {
|
|
|
2109
2189
|
return code;
|
|
2110
2190
|
}
|
|
2111
2191
|
|
|
2112
|
-
// src/
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
var
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2192
|
+
// src/vite-plugin/index.ts
|
|
2193
|
+
import { createScopedLogger as createScopedLogger2 } from "@moccona/logger";
|
|
2194
|
+
import { execaCommand } from "execa";
|
|
2195
|
+
import fs from "fs-extra";
|
|
2196
|
+
var PLUGIN_NAME = "apiCodeGen";
|
|
2197
|
+
var logger2 = createScopedLogger2("api-codegen-vite-plugin");
|
|
2198
|
+
var tsc = async () => {
|
|
2199
|
+
await execaCommand("npx tsc -b");
|
|
2200
|
+
};
|
|
2201
|
+
function apiCodeGenPlugin(options) {
|
|
2202
|
+
let firstRun = true;
|
|
2203
|
+
return {
|
|
2204
|
+
name: PLUGIN_NAME,
|
|
2205
|
+
async config(config) {
|
|
2206
|
+
if (!firstRun) return;
|
|
2207
|
+
firstRun = false;
|
|
2208
|
+
logger2.info("-------> api codegen start <--------");
|
|
2209
|
+
const proxies = await options.reduce(
|
|
2210
|
+
async (proxiesPromise_, option) => {
|
|
2211
|
+
const proxies_ = await proxiesPromise_;
|
|
2212
|
+
const { proxy = {}, name, ...codeGenInitOptions } = option;
|
|
2213
|
+
try {
|
|
2214
|
+
const code = await codeGen(codeGenInitOptions);
|
|
2215
|
+
await fs.createFile(codeGenInitOptions.output);
|
|
2216
|
+
await fs.writeFile(codeGenInitOptions.output, code);
|
|
2217
|
+
} catch (error) {
|
|
2218
|
+
logger2.error(`Failed to generate api ${name}`);
|
|
2219
|
+
console.error(error);
|
|
2220
|
+
}
|
|
2221
|
+
return {
|
|
2222
|
+
...proxies_,
|
|
2223
|
+
...proxy
|
|
2224
|
+
};
|
|
2225
|
+
},
|
|
2226
|
+
Promise.resolve({})
|
|
2227
|
+
);
|
|
2228
|
+
logger2.info("-------> api codegen finished <--------");
|
|
2229
|
+
try {
|
|
2230
|
+
await tsc();
|
|
2231
|
+
} catch (error) {
|
|
2232
|
+
logger2.error(error);
|
|
2233
|
+
}
|
|
2234
|
+
return {
|
|
2235
|
+
...config,
|
|
2236
|
+
server: {
|
|
2237
|
+
...config.server ?? {},
|
|
2238
|
+
proxy: Object.assign({}, config.server?.proxy ?? {}, proxies)
|
|
2239
|
+
}
|
|
2240
|
+
};
|
|
2134
2241
|
}
|
|
2135
|
-
}
|
|
2136
|
-
|
|
2137
|
-
|
|
2242
|
+
};
|
|
2243
|
+
}
|
|
2244
|
+
export {
|
|
2245
|
+
apiCodeGenPlugin,
|
|
2246
|
+
tsc
|
|
2247
|
+
};
|
|
2248
|
+
//# sourceMappingURL=index.js.map
|