@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/npm/index.js
CHANGED
|
@@ -205,10 +205,10 @@ var Base = class _Base {
|
|
|
205
205
|
* @param [operationId] - Unique identifier for the operation.
|
|
206
206
|
* @returns - The generated function name.
|
|
207
207
|
*/
|
|
208
|
-
static pathToFnName(path, method,
|
|
209
|
-
const name = this.camelCase(this.normalize(path));
|
|
208
|
+
static pathToFnName(path, method, _operationId = "") {
|
|
209
|
+
const name = this.normalize(this.camelCase(this.normalize(path)));
|
|
210
210
|
const suffix = method ? this.capitalize(this.upperCamelCase(`using_${method}`)) : "";
|
|
211
|
-
return
|
|
211
|
+
return name + suffix;
|
|
212
212
|
}
|
|
213
213
|
/**
|
|
214
214
|
* Normalizes a string by replacing special characters and avoiding TypeScript keywords.
|
|
@@ -219,7 +219,7 @@ var Base = class _Base {
|
|
|
219
219
|
if (typescriptKeywords.has(text)) {
|
|
220
220
|
text += "_";
|
|
221
221
|
}
|
|
222
|
-
return text.replace(/[/\-_{}():\s
|
|
222
|
+
return text.replace(/[/\-_{}():\s`,*<>$#.]/gm, "_").replace(/^\d./gm, "").replaceAll("...", "");
|
|
223
223
|
}
|
|
224
224
|
/**
|
|
225
225
|
* Capitalizes the first character of a string.
|
|
@@ -255,19 +255,24 @@ var Base = class _Base {
|
|
|
255
255
|
*/
|
|
256
256
|
static async fetchDoc(url, requestInit = {}) {
|
|
257
257
|
const agent = new Agent({
|
|
258
|
-
connect: {
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
connect: { rejectUnauthorized: false }
|
|
259
|
+
});
|
|
260
|
+
const { body, statusCode } = await request(url, {
|
|
261
|
+
method: "GET",
|
|
262
|
+
dispatcher: agent,
|
|
263
|
+
...requestInit
|
|
261
264
|
});
|
|
265
|
+
if (statusCode >= 400) {
|
|
266
|
+
throw new Error(
|
|
267
|
+
`Failed to fetch OpenAPI documentation from ${url}: HTTP ${statusCode}`
|
|
268
|
+
);
|
|
269
|
+
}
|
|
262
270
|
try {
|
|
263
|
-
const { body } = await request(url, {
|
|
264
|
-
method: "GET",
|
|
265
|
-
dispatcher: agent,
|
|
266
|
-
...requestInit
|
|
267
|
-
});
|
|
268
271
|
return body.json();
|
|
269
272
|
} catch (error) {
|
|
270
|
-
throw
|
|
273
|
+
throw new Error(
|
|
274
|
+
`Failed to parse JSON response from ${url}: ${error instanceof Error ? error.message : String(error)}`
|
|
275
|
+
);
|
|
271
276
|
}
|
|
272
277
|
}
|
|
273
278
|
/**
|
|
@@ -276,12 +281,11 @@ var Base = class _Base {
|
|
|
276
281
|
* @returns - The matched MediaTypes or null.
|
|
277
282
|
*/
|
|
278
283
|
static getMediaType(mediaType) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return;
|
|
284
|
+
const mediaTypeValues = Object.values(MediaTypes);
|
|
285
|
+
const found = mediaTypeValues.find(
|
|
286
|
+
(type) => mediaType.includes(type)
|
|
287
|
+
);
|
|
288
|
+
return found;
|
|
285
289
|
}
|
|
286
290
|
/**
|
|
287
291
|
* Checks if a schema is a valid enum type that isn't boolean.
|
|
@@ -316,17 +320,21 @@ var Base = class _Base {
|
|
|
316
320
|
* @returns - Array of unique enum schemas.
|
|
317
321
|
*/
|
|
318
322
|
static uniqueEnums(enums) {
|
|
319
|
-
const
|
|
320
|
-
for (const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
uniqueEnums_.push(enumObject);
|
|
323
|
+
const enumMap = /* @__PURE__ */ new Map();
|
|
324
|
+
for (const e of enums) {
|
|
325
|
+
const existing = enumMap.get(e.name);
|
|
326
|
+
if (existing) {
|
|
327
|
+
for (const value of e.enum) {
|
|
328
|
+
existing.add(value);
|
|
326
329
|
}
|
|
330
|
+
} else {
|
|
331
|
+
enumMap.set(e.name, new Set(e.enum));
|
|
327
332
|
}
|
|
328
333
|
}
|
|
329
|
-
return
|
|
334
|
+
return Array.from(enumMap.entries()).map(([name, values]) => ({
|
|
335
|
+
name,
|
|
336
|
+
enum: Array.from(values)
|
|
337
|
+
}));
|
|
330
338
|
}
|
|
331
339
|
/**
|
|
332
340
|
* Finds the first occurrence of a matching enum schema in an array.
|
|
@@ -343,7 +351,7 @@ var Base = class _Base {
|
|
|
343
351
|
* @returns - True if the object is a reference.
|
|
344
352
|
*/
|
|
345
353
|
static isRef(schema) {
|
|
346
|
-
return "$ref" in schema && typeof schema.$ref === "string";
|
|
354
|
+
return typeof schema === "object" && schema !== null && "$ref" in schema && typeof schema.$ref === "string";
|
|
347
355
|
}
|
|
348
356
|
};
|
|
349
357
|
|
|
@@ -435,7 +443,7 @@ var Generator = class _Generator {
|
|
|
435
443
|
* @param path - The base path string containing placeholders.
|
|
436
444
|
* @param parameters - Array of parameter objects defining the parameters.
|
|
437
445
|
* @param basePath - Optional base path to prepend (default: "").
|
|
438
|
-
* @returns A TypeScript template
|
|
446
|
+
* @returns A TypeScript template expressi
|
|
439
447
|
*/
|
|
440
448
|
static toUrlTemplate(path, parameters, basePath = "") {
|
|
441
449
|
const queryParameters = parameters.filter(
|
|
@@ -443,7 +451,7 @@ var Generator = class _Generator {
|
|
|
443
451
|
);
|
|
444
452
|
if (queryParameters.length > 0) {
|
|
445
453
|
const queryString = queryParameters.map(
|
|
446
|
-
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${qp.name}}`
|
|
454
|
+
(qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${Base.camelCase(Base.normalize(qp.name))}}`
|
|
447
455
|
).join("");
|
|
448
456
|
path += queryString;
|
|
449
457
|
}
|
|
@@ -558,7 +566,7 @@ var Generator = class _Generator {
|
|
|
558
566
|
case "boolean" /* boolean */:
|
|
559
567
|
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
560
568
|
case "file" /* file */:
|
|
561
|
-
return t.createTypeReferenceNode(t.createIdentifier("
|
|
569
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
562
570
|
default:
|
|
563
571
|
const {
|
|
564
572
|
format: format2,
|
|
@@ -577,7 +585,7 @@ var Generator = class _Generator {
|
|
|
577
585
|
return t.createToken(SyntaxKind.BooleanKeyword);
|
|
578
586
|
case "blob" /* blob */:
|
|
579
587
|
case "binary" /* binary */:
|
|
580
|
-
return t.createTypeReferenceNode(t.createIdentifier("
|
|
588
|
+
return t.createTypeReferenceNode(t.createIdentifier("Blob"));
|
|
581
589
|
default:
|
|
582
590
|
}
|
|
583
591
|
if (enum_) {
|
|
@@ -601,36 +609,36 @@ var Generator = class _Generator {
|
|
|
601
609
|
);
|
|
602
610
|
}
|
|
603
611
|
if (allOf) {
|
|
604
|
-
return t.
|
|
612
|
+
return t.createIntersectionTypeNode(
|
|
605
613
|
allOf.map((schema2) => this.toTypeNode(schema2))
|
|
606
614
|
);
|
|
607
615
|
}
|
|
608
616
|
if (type2 && typeof type2 === "string") {
|
|
609
617
|
return t.createTypeReferenceNode(
|
|
610
|
-
type2 !== "unknown" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
|
|
618
|
+
type2 !== "unknown" && type2 !== "null" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
|
|
611
619
|
);
|
|
612
620
|
}
|
|
613
621
|
}
|
|
614
622
|
return t.createToken(SyntaxKind.UnknownKeyword);
|
|
615
623
|
}
|
|
616
|
-
static
|
|
624
|
+
static toDeclarationNodes(parameters) {
|
|
617
625
|
const objectElements = [];
|
|
618
626
|
const typeObjectElements = [];
|
|
627
|
+
const refParameters = [];
|
|
619
628
|
for (const parameter of parameters) {
|
|
620
629
|
if (parameter.ref) {
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
)
|
|
633
|
-
void 0
|
|
630
|
+
const refName = Base.ref2name(parameter.ref);
|
|
631
|
+
refParameters.push(
|
|
632
|
+
t.createParameterDeclaration(
|
|
633
|
+
void 0,
|
|
634
|
+
void 0,
|
|
635
|
+
t.createIdentifier(Base.camelCase(Base.normalize(refName))),
|
|
636
|
+
void 0,
|
|
637
|
+
t.createTypeReferenceNode(
|
|
638
|
+
t.createIdentifier(Base.upperCamelCase(Base.normalize(refName)))
|
|
639
|
+
),
|
|
640
|
+
void 0
|
|
641
|
+
)
|
|
634
642
|
);
|
|
635
643
|
} else {
|
|
636
644
|
const { name, schema, required } = parameter;
|
|
@@ -651,14 +659,18 @@ var Generator = class _Generator {
|
|
|
651
659
|
);
|
|
652
660
|
}
|
|
653
661
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
+
if (objectElements.length > 0) {
|
|
663
|
+
const objectParam = t.createParameterDeclaration(
|
|
664
|
+
void 0,
|
|
665
|
+
void 0,
|
|
666
|
+
t.createObjectBindingPattern(objectElements),
|
|
667
|
+
void 0,
|
|
668
|
+
t.createTypeLiteralNode(typeObjectElements),
|
|
669
|
+
void 0
|
|
670
|
+
);
|
|
671
|
+
return [objectParam, ...refParameters];
|
|
672
|
+
}
|
|
673
|
+
return refParameters;
|
|
662
674
|
}
|
|
663
675
|
static toFormDataStatement(parameters, requestBody) {
|
|
664
676
|
const statements = [];
|
|
@@ -681,16 +693,11 @@ var Generator = class _Generator {
|
|
|
681
693
|
)
|
|
682
694
|
);
|
|
683
695
|
statements.push(fdDeclaration);
|
|
684
|
-
parameters.
|
|
685
|
-
(parameter) => parameter.ref !== void 0 && (parameter.in === "formData" /* formData */ || parameter.schema && this.isBinarySchema(parameter.schema))
|
|
686
|
-
).forEach((parameter) => {
|
|
696
|
+
parameters.forEach((parameter) => {
|
|
687
697
|
statements.push(
|
|
688
698
|
t.createExpressionStatement(
|
|
689
699
|
t.createBinaryExpression(
|
|
690
|
-
t.
|
|
691
|
-
t.createIdentifier("req"),
|
|
692
|
-
t.createStringLiteral(parameter.name)
|
|
693
|
-
),
|
|
700
|
+
t.createIdentifier(parameter.name),
|
|
694
701
|
t.createToken(SyntaxKind.AmpersandAmpersandToken),
|
|
695
702
|
t.createCallExpression(
|
|
696
703
|
t.createPropertyAccessExpression(
|
|
@@ -734,7 +741,13 @@ var Generator = class _Generator {
|
|
|
734
741
|
t.createStringLiteral(key),
|
|
735
742
|
t.createIdentifier("file"),
|
|
736
743
|
t.createPropertyAccessExpression(
|
|
737
|
-
t.
|
|
744
|
+
t.createAsExpression(
|
|
745
|
+
t.createIdentifier("file"),
|
|
746
|
+
t.createTypeReferenceNode(
|
|
747
|
+
t.createIdentifier("File"),
|
|
748
|
+
void 0
|
|
749
|
+
)
|
|
750
|
+
),
|
|
738
751
|
t.createIdentifier("name")
|
|
739
752
|
)
|
|
740
753
|
]
|
|
@@ -819,25 +832,28 @@ var Generator = class _Generator {
|
|
|
819
832
|
);
|
|
820
833
|
const shouldParseResponseToJSON = "application/json" === response?.type;
|
|
821
834
|
const isRequestBodyBinary = requestBody?.schema && requestBody.schema.type === "array" /* array */ && this.isBinarySchema(requestBody.schema);
|
|
822
|
-
const
|
|
823
|
-
(p) => p.in === "formData" /* formData */
|
|
835
|
+
const parametersShouldPutInFormData = parameters.filter(
|
|
836
|
+
(p) => p.in === "formData" /* formData */ || p.schema && this.isBinarySchema(p.schema)
|
|
824
837
|
);
|
|
825
|
-
const
|
|
826
|
-
(p) => p
|
|
838
|
+
const parametersShouldNotPutInFormData = parameters.filter(
|
|
839
|
+
(p) => !parametersShouldPutInFormData.includes(p)
|
|
827
840
|
);
|
|
828
|
-
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema
|
|
841
|
+
const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some(
|
|
829
842
|
(p) => this.isBinarySchema(p)
|
|
830
843
|
);
|
|
831
844
|
const hasBinaryInParameters = parameters.some(
|
|
832
845
|
(p) => p?.schema && this.isBinarySchema(p.schema)
|
|
833
846
|
);
|
|
834
|
-
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary ||
|
|
847
|
+
const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
|
|
835
848
|
return t.createBlock([
|
|
836
|
-
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
849
|
+
...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
|
|
850
|
+
parametersShouldPutInFormData,
|
|
851
|
+
requestBody?.schema
|
|
852
|
+
) : [],
|
|
837
853
|
...adapter.client(
|
|
838
854
|
uri,
|
|
839
855
|
method,
|
|
840
|
-
|
|
856
|
+
parametersShouldNotPutInFormData,
|
|
841
857
|
requestBody,
|
|
842
858
|
response,
|
|
843
859
|
adapter,
|
|
@@ -908,8 +924,8 @@ var Generator = class _Generator {
|
|
|
908
924
|
Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
|
|
909
925
|
void 0,
|
|
910
926
|
[
|
|
911
|
-
parameters.length > 0 ? _Generator.
|
|
912
|
-
req?.schema ? _Generator.toRequestBodyTypeNode(req.schema) :
|
|
927
|
+
...parameters.length > 0 ? _Generator.toDeclarationNodes(parameters) : [],
|
|
928
|
+
...req?.schema ? [_Generator.toRequestBodyTypeNode(req.schema)] : []
|
|
913
929
|
].filter(Boolean),
|
|
914
930
|
void 0,
|
|
915
931
|
this.bodyBlock(
|
|
@@ -1029,7 +1045,6 @@ var AxiosAdapter = class extends Adapter {
|
|
|
1029
1045
|
)
|
|
1030
1046
|
) : []
|
|
1031
1047
|
).concat(
|
|
1032
|
-
// Add body if needed
|
|
1033
1048
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t2.createPropertyAssignment(
|
|
1034
1049
|
t2.createIdentifier(adapter.bodyFieldName),
|
|
1035
1050
|
shouldUseFormData ? t2.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t2.createIdentifier("req") : t2.createIdentifier("req")
|
|
@@ -1115,7 +1130,6 @@ var FetchAdapter = class extends Adapter {
|
|
|
1115
1130
|
)
|
|
1116
1131
|
) : []
|
|
1117
1132
|
).concat(
|
|
1118
|
-
// Add body if needed
|
|
1119
1133
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t3.createPropertyAssignment(
|
|
1120
1134
|
t3.createIdentifier(adapter.bodyFieldName),
|
|
1121
1135
|
shouldUseFormData ? t3.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t3.createCallExpression(
|
|
@@ -1173,7 +1187,7 @@ var FetchAdapter = class extends Adapter {
|
|
|
1173
1187
|
],
|
|
1174
1188
|
void 0,
|
|
1175
1189
|
t3.createToken(SyntaxKind2.EqualsGreaterThanToken),
|
|
1176
|
-
t3.createAsExpression(
|
|
1190
|
+
response?.schema ? t3.createAsExpression(
|
|
1177
1191
|
t3.createParenthesizedExpression(
|
|
1178
1192
|
t3.createAwaitExpression(
|
|
1179
1193
|
t3.createCallExpression(
|
|
@@ -1187,6 +1201,17 @@ var FetchAdapter = class extends Adapter {
|
|
|
1187
1201
|
)
|
|
1188
1202
|
),
|
|
1189
1203
|
response?.schema ? Generator.toTypeNode(response.schema) : t3.createToken(SyntaxKind2.UnknownKeyword)
|
|
1204
|
+
) : t3.createParenthesizedExpression(
|
|
1205
|
+
t3.createAwaitExpression(
|
|
1206
|
+
t3.createCallExpression(
|
|
1207
|
+
t3.createPropertyAccessExpression(
|
|
1208
|
+
t3.createIdentifier("response"),
|
|
1209
|
+
t3.createIdentifier("json")
|
|
1210
|
+
),
|
|
1211
|
+
void 0,
|
|
1212
|
+
[]
|
|
1213
|
+
)
|
|
1214
|
+
)
|
|
1190
1215
|
)
|
|
1191
1216
|
)
|
|
1192
1217
|
]
|
|
@@ -1297,13 +1322,25 @@ var V2 = class {
|
|
|
1297
1322
|
enum: enum_,
|
|
1298
1323
|
format: format2,
|
|
1299
1324
|
allOf: allOf?.map(
|
|
1300
|
-
(s) => Base.isRef(s) ? {
|
|
1325
|
+
(s) => Base.isRef(s) ? {
|
|
1326
|
+
...s,
|
|
1327
|
+
ref: s.$ref,
|
|
1328
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1329
|
+
} : this.toBaseSchema(s, enums)
|
|
1301
1330
|
),
|
|
1302
1331
|
anyOf: anyOf?.map(
|
|
1303
|
-
(s) => Base.isRef(s) ? {
|
|
1332
|
+
(s) => Base.isRef(s) ? {
|
|
1333
|
+
...s,
|
|
1334
|
+
ref: s.$ref,
|
|
1335
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1336
|
+
} : this.toBaseSchema(s, enums)
|
|
1304
1337
|
),
|
|
1305
1338
|
oneOf: oneOf?.map(
|
|
1306
|
-
(s) => Base.isRef(s) ? {
|
|
1339
|
+
(s) => Base.isRef(s) ? {
|
|
1340
|
+
...s,
|
|
1341
|
+
ref: s.$ref,
|
|
1342
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
1343
|
+
} : this.toBaseSchema(s, enums)
|
|
1307
1344
|
),
|
|
1308
1345
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1309
1346
|
const propSchema = properties[p];
|
|
@@ -1333,7 +1370,8 @@ var V2 = class {
|
|
|
1333
1370
|
type,
|
|
1334
1371
|
items,
|
|
1335
1372
|
enum: enum_,
|
|
1336
|
-
properties
|
|
1373
|
+
properties,
|
|
1374
|
+
schema
|
|
1337
1375
|
} = parameter;
|
|
1338
1376
|
if (enum_) {
|
|
1339
1377
|
const type2 = Base.upperCamelCase(upLevelSchemaKey) + Base.upperCamelCase(name);
|
|
@@ -1355,17 +1393,36 @@ var V2 = class {
|
|
|
1355
1393
|
}
|
|
1356
1394
|
};
|
|
1357
1395
|
}
|
|
1396
|
+
if (items) {
|
|
1397
|
+
return {
|
|
1398
|
+
name,
|
|
1399
|
+
required,
|
|
1400
|
+
description,
|
|
1401
|
+
in: parameter.in,
|
|
1402
|
+
schema: {
|
|
1403
|
+
type,
|
|
1404
|
+
items
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
if (schema && Base.isRef(schema)) {
|
|
1409
|
+
return {
|
|
1410
|
+
name,
|
|
1411
|
+
required,
|
|
1412
|
+
description,
|
|
1413
|
+
in: parameter.in,
|
|
1414
|
+
schema: {
|
|
1415
|
+
type: Base.upperCamelCase(Base.ref2name(schema.$ref))
|
|
1416
|
+
}
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1358
1419
|
return {
|
|
1359
1420
|
name,
|
|
1360
1421
|
required,
|
|
1361
1422
|
description,
|
|
1362
1423
|
in: parameter.in,
|
|
1363
|
-
schema:
|
|
1364
|
-
type,
|
|
1365
|
-
items
|
|
1366
|
-
} : {
|
|
1424
|
+
schema: {
|
|
1367
1425
|
type,
|
|
1368
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
1369
1426
|
properties
|
|
1370
1427
|
}
|
|
1371
1428
|
};
|
|
@@ -1444,6 +1501,7 @@ var V2 = class {
|
|
|
1444
1501
|
if (code in responses2) {
|
|
1445
1502
|
const response = responses2[code];
|
|
1446
1503
|
const responseSchema = this.getResponseByRef(response);
|
|
1504
|
+
const inBodyOnlyHasBody = inBody && inBody.length === 1 && inBody[0].in === "body" && inBody[0].name === "body";
|
|
1447
1505
|
methodApis.push({
|
|
1448
1506
|
method,
|
|
1449
1507
|
operationId,
|
|
@@ -1452,7 +1510,12 @@ var V2 = class {
|
|
|
1452
1510
|
description: description_,
|
|
1453
1511
|
parameters: uniqueParameterName.map((name) => notInBody.find((p) => p.name === name)).filter(Boolean),
|
|
1454
1512
|
responses: responseSchema,
|
|
1455
|
-
requestBody: inBody.length > 0 ? [
|
|
1513
|
+
requestBody: inBody.length > 0 ? inBodyOnlyHasBody ? [
|
|
1514
|
+
{
|
|
1515
|
+
type: "application/json" /* JSON */,
|
|
1516
|
+
schema: inBody[0].schema
|
|
1517
|
+
}
|
|
1518
|
+
] : [
|
|
1456
1519
|
{
|
|
1457
1520
|
type: "application/json" /* JSON */,
|
|
1458
1521
|
schema: {
|
|
@@ -1520,7 +1583,11 @@ var V3 = class {
|
|
|
1520
1583
|
type: upLevelSchemaKey + refName
|
|
1521
1584
|
};
|
|
1522
1585
|
}
|
|
1523
|
-
|
|
1586
|
+
const resolvedSchema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1587
|
+
if (!resolvedSchema) {
|
|
1588
|
+
return { type: "unknown" };
|
|
1589
|
+
}
|
|
1590
|
+
schema = resolvedSchema;
|
|
1524
1591
|
}
|
|
1525
1592
|
return this.toBaseSchema(schema, enums, "", upLevelSchemaKey + refName);
|
|
1526
1593
|
}
|
|
@@ -1529,7 +1596,14 @@ var V3 = class {
|
|
|
1529
1596
|
*/
|
|
1530
1597
|
getParameterByRef(schema, enums = [], upLevelSchemaKey = "") {
|
|
1531
1598
|
if (Base.isRef(schema)) {
|
|
1532
|
-
|
|
1599
|
+
const resolvedSchema = this.doc.components?.parameters?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1600
|
+
if (!resolvedSchema) {
|
|
1601
|
+
return {
|
|
1602
|
+
name: "unknown",
|
|
1603
|
+
in: "query"
|
|
1604
|
+
};
|
|
1605
|
+
}
|
|
1606
|
+
schema = resolvedSchema;
|
|
1533
1607
|
}
|
|
1534
1608
|
const {
|
|
1535
1609
|
name,
|
|
@@ -1578,7 +1652,11 @@ var V3 = class {
|
|
|
1578
1652
|
*/
|
|
1579
1653
|
getResponseByRef(schema) {
|
|
1580
1654
|
if (Base.isRef(schema)) {
|
|
1581
|
-
|
|
1655
|
+
const resolvedSchema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1656
|
+
if (!resolvedSchema) {
|
|
1657
|
+
return [];
|
|
1658
|
+
}
|
|
1659
|
+
schema = resolvedSchema;
|
|
1582
1660
|
}
|
|
1583
1661
|
const { content = {} } = schema;
|
|
1584
1662
|
return Object.keys(content).map((c) => ({
|
|
@@ -1591,7 +1669,11 @@ var V3 = class {
|
|
|
1591
1669
|
*/
|
|
1592
1670
|
getRequestBodyByRef(schema, enums = []) {
|
|
1593
1671
|
if (Base.isRef(schema)) {
|
|
1594
|
-
|
|
1672
|
+
const resolvedSchema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1673
|
+
if (!resolvedSchema) {
|
|
1674
|
+
return [];
|
|
1675
|
+
}
|
|
1676
|
+
schema = resolvedSchema;
|
|
1595
1677
|
}
|
|
1596
1678
|
const { content = {} } = schema;
|
|
1597
1679
|
return Object.keys(content).map((c) => ({
|
|
@@ -1660,13 +1742,25 @@ var V3 = class {
|
|
|
1660
1742
|
enum: enum_,
|
|
1661
1743
|
format: format2,
|
|
1662
1744
|
allOf: allOf?.map(
|
|
1663
|
-
(s) => Base.isRef(s) ? {
|
|
1745
|
+
(s) => Base.isRef(s) ? {
|
|
1746
|
+
...s,
|
|
1747
|
+
ref: s.$ref,
|
|
1748
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1749
|
+
} : this.toBaseSchema(s, enums)
|
|
1664
1750
|
),
|
|
1665
1751
|
anyOf: anyOf?.map(
|
|
1666
|
-
(s) => Base.isRef(s) ? {
|
|
1752
|
+
(s) => Base.isRef(s) ? {
|
|
1753
|
+
...s,
|
|
1754
|
+
ref: s.$ref,
|
|
1755
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1756
|
+
} : this.toBaseSchema(s, enums)
|
|
1667
1757
|
),
|
|
1668
1758
|
oneOf: oneOf?.map(
|
|
1669
|
-
(s) => Base.isRef(s) ? {
|
|
1759
|
+
(s) => Base.isRef(s) ? {
|
|
1760
|
+
...s,
|
|
1761
|
+
ref: s.$ref,
|
|
1762
|
+
type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
|
|
1763
|
+
} : this.toBaseSchema(s, enums)
|
|
1670
1764
|
),
|
|
1671
1765
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1672
1766
|
const propSchema = properties[p];
|
|
@@ -1963,13 +2057,25 @@ var V3_1 = class {
|
|
|
1963
2057
|
enum: enum_,
|
|
1964
2058
|
format: format2,
|
|
1965
2059
|
allOf: allOf?.map(
|
|
1966
|
-
(s) => Base.isRef(s) ? {
|
|
2060
|
+
(s) => Base.isRef(s) ? {
|
|
2061
|
+
...s,
|
|
2062
|
+
ref: s.$ref,
|
|
2063
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2064
|
+
} : this.toBaseSchema(s, enums)
|
|
1967
2065
|
),
|
|
1968
2066
|
anyOf: anyOf?.map(
|
|
1969
|
-
(s) => Base.isRef(s) ? {
|
|
2067
|
+
(s) => Base.isRef(s) ? {
|
|
2068
|
+
...s,
|
|
2069
|
+
ref: s.$ref,
|
|
2070
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2071
|
+
} : this.toBaseSchema(s, enums)
|
|
1970
2072
|
),
|
|
1971
2073
|
oneOf: oneOf?.map(
|
|
1972
|
-
(s) => Base.isRef(s) ? {
|
|
2074
|
+
(s) => Base.isRef(s) ? {
|
|
2075
|
+
...s,
|
|
2076
|
+
ref: s.$ref,
|
|
2077
|
+
type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
|
|
2078
|
+
} : this.toBaseSchema(s, enums)
|
|
1973
2079
|
),
|
|
1974
2080
|
properties: Object.keys(properties).reduce((acc, p) => {
|
|
1975
2081
|
const propSchema = properties[p];
|
|
@@ -2191,8 +2297,8 @@ import { execaCommand } from "execa";
|
|
|
2191
2297
|
import fs from "fs-extra";
|
|
2192
2298
|
var PLUGIN_NAME = "apiCodeGen";
|
|
2193
2299
|
var logger2 = createScopedLogger2("api-codegen-vite-plugin");
|
|
2194
|
-
var tsc = async () => {
|
|
2195
|
-
await execaCommand(
|
|
2300
|
+
var tsc = async (path) => {
|
|
2301
|
+
await execaCommand(`npx tsc ${path} --noEmit`);
|
|
2196
2302
|
};
|
|
2197
2303
|
function apiCodeGenPlugin(options) {
|
|
2198
2304
|
let firstRun = true;
|
|
@@ -2210,9 +2316,13 @@ function apiCodeGenPlugin(options) {
|
|
|
2210
2316
|
const code = await codeGen(codeGenInitOptions);
|
|
2211
2317
|
await fs.createFile(codeGenInitOptions.output);
|
|
2212
2318
|
await fs.writeFile(codeGenInitOptions.output, code);
|
|
2319
|
+
try {
|
|
2320
|
+
await tsc(codeGenInitOptions.output);
|
|
2321
|
+
} catch (error) {
|
|
2322
|
+
logger2.error(error);
|
|
2323
|
+
}
|
|
2213
2324
|
} catch (error) {
|
|
2214
|
-
logger2.error(`Failed to generate api ${name}`);
|
|
2215
|
-
console.error(error);
|
|
2325
|
+
logger2.error(`Failed to generate api ${name}: ${error}`);
|
|
2216
2326
|
}
|
|
2217
2327
|
return {
|
|
2218
2328
|
...proxies_,
|
|
@@ -2222,17 +2332,11 @@ function apiCodeGenPlugin(options) {
|
|
|
2222
2332
|
Promise.resolve({})
|
|
2223
2333
|
);
|
|
2224
2334
|
logger2.info("-------> api codegen finished <--------");
|
|
2225
|
-
try {
|
|
2226
|
-
await tsc();
|
|
2227
|
-
} catch (error) {
|
|
2228
|
-
logger2.error(error);
|
|
2229
|
-
process.exit(1);
|
|
2230
|
-
}
|
|
2231
2335
|
return {
|
|
2232
2336
|
...config,
|
|
2233
2337
|
server: {
|
|
2234
2338
|
...config.server ?? {},
|
|
2235
|
-
proxy: proxies
|
|
2339
|
+
proxy: Object.assign({}, config.server?.proxy ?? {}, proxies)
|
|
2236
2340
|
}
|
|
2237
2341
|
};
|
|
2238
2342
|
}
|