@moccona/apicodegen 0.0.2 → 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 +1 -2
- package/bin/cli.js +2248 -0
- package/npm/index.cjs +91 -63
- package/npm/index.cjs.map +1 -1
- package/npm/index.d.cts +8 -8
- package/npm/index.d.ts +8 -8
- package/npm/index.js +91 -63
- package/npm/index.js.map +1 -1
- package/npm/vite/index.d.ts +1 -1
- package/npm/vite/index.js +91 -63
- package/npm/vite/index.js.map +1 -1
- package/package.json +25 -31
package/npm/index.cjs
CHANGED
|
@@ -310,19 +310,24 @@ var Base = class _Base {
|
|
|
310
310
|
*/
|
|
311
311
|
static async fetchDoc(url, requestInit = {}) {
|
|
312
312
|
const agent = new import_undici.Agent({
|
|
313
|
-
connect: {
|
|
314
|
-
|
|
315
|
-
|
|
313
|
+
connect: { rejectUnauthorized: false }
|
|
314
|
+
});
|
|
315
|
+
const { body, statusCode } = await (0, import_undici.request)(url, {
|
|
316
|
+
method: "GET",
|
|
317
|
+
dispatcher: agent,
|
|
318
|
+
...requestInit
|
|
316
319
|
});
|
|
320
|
+
if (statusCode >= 400) {
|
|
321
|
+
throw new Error(
|
|
322
|
+
`Failed to fetch OpenAPI documentation from ${url}: HTTP ${statusCode}`
|
|
323
|
+
);
|
|
324
|
+
}
|
|
317
325
|
try {
|
|
318
|
-
const { body } = await (0, import_undici.request)(url, {
|
|
319
|
-
method: "GET",
|
|
320
|
-
dispatcher: agent,
|
|
321
|
-
...requestInit
|
|
322
|
-
});
|
|
323
326
|
return body.json();
|
|
324
327
|
} catch (error) {
|
|
325
|
-
throw
|
|
328
|
+
throw new Error(
|
|
329
|
+
`Failed to parse JSON response from ${url}: ${error instanceof Error ? error.message : String(error)}`
|
|
330
|
+
);
|
|
326
331
|
}
|
|
327
332
|
}
|
|
328
333
|
/**
|
|
@@ -331,12 +336,11 @@ var Base = class _Base {
|
|
|
331
336
|
* @returns - The matched MediaTypes or null.
|
|
332
337
|
*/
|
|
333
338
|
static getMediaType(mediaType) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
return;
|
|
339
|
+
const mediaTypeValues = Object.values(MediaTypes);
|
|
340
|
+
const found = mediaTypeValues.find(
|
|
341
|
+
(type) => mediaType.includes(type)
|
|
342
|
+
);
|
|
343
|
+
return found;
|
|
340
344
|
}
|
|
341
345
|
/**
|
|
342
346
|
* Checks if a schema is a valid enum type that isn't boolean.
|
|
@@ -371,17 +375,21 @@ var Base = class _Base {
|
|
|
371
375
|
* @returns - Array of unique enum schemas.
|
|
372
376
|
*/
|
|
373
377
|
static uniqueEnums(enums) {
|
|
374
|
-
const
|
|
375
|
-
for (const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
uniqueEnums_.push(enumObject);
|
|
378
|
+
const enumMap = /* @__PURE__ */ new Map();
|
|
379
|
+
for (const e of enums) {
|
|
380
|
+
const existing = enumMap.get(e.name);
|
|
381
|
+
if (existing) {
|
|
382
|
+
for (const value of e.enum) {
|
|
383
|
+
existing.add(value);
|
|
381
384
|
}
|
|
385
|
+
} else {
|
|
386
|
+
enumMap.set(e.name, new Set(e.enum));
|
|
382
387
|
}
|
|
383
388
|
}
|
|
384
|
-
return
|
|
389
|
+
return Array.from(enumMap.entries()).map(([name, values]) => ({
|
|
390
|
+
name,
|
|
391
|
+
enum: Array.from(values)
|
|
392
|
+
}));
|
|
385
393
|
}
|
|
386
394
|
/**
|
|
387
395
|
* Finds the first occurrence of a matching enum schema in an array.
|
|
@@ -398,7 +406,7 @@ var Base = class _Base {
|
|
|
398
406
|
* @returns - True if the object is a reference.
|
|
399
407
|
*/
|
|
400
408
|
static isRef(schema) {
|
|
401
|
-
return "$ref" in schema && typeof schema.$ref === "string";
|
|
409
|
+
return typeof schema === "object" && schema !== null && "$ref" in schema && typeof schema.$ref === "string";
|
|
402
410
|
}
|
|
403
411
|
};
|
|
404
412
|
|
|
@@ -662,24 +670,24 @@ var Generator = class _Generator {
|
|
|
662
670
|
}
|
|
663
671
|
return import_typescript.factory.createToken(import_typescript.SyntaxKind.UnknownKeyword);
|
|
664
672
|
}
|
|
665
|
-
static
|
|
673
|
+
static toDeclarationNodes(parameters) {
|
|
666
674
|
const objectElements = [];
|
|
667
675
|
const typeObjectElements = [];
|
|
676
|
+
const refParameters = [];
|
|
668
677
|
for (const parameter of parameters) {
|
|
669
678
|
if (parameter.ref) {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
)
|
|
682
|
-
void 0
|
|
679
|
+
const refName = Base.ref2name(parameter.ref);
|
|
680
|
+
refParameters.push(
|
|
681
|
+
import_typescript.factory.createParameterDeclaration(
|
|
682
|
+
void 0,
|
|
683
|
+
void 0,
|
|
684
|
+
import_typescript.factory.createIdentifier(Base.camelCase(Base.normalize(refName))),
|
|
685
|
+
void 0,
|
|
686
|
+
import_typescript.factory.createTypeReferenceNode(
|
|
687
|
+
import_typescript.factory.createIdentifier(Base.upperCamelCase(Base.normalize(refName)))
|
|
688
|
+
),
|
|
689
|
+
void 0
|
|
690
|
+
)
|
|
683
691
|
);
|
|
684
692
|
} else {
|
|
685
693
|
const { name, schema, required } = parameter;
|
|
@@ -700,14 +708,18 @@ var Generator = class _Generator {
|
|
|
700
708
|
);
|
|
701
709
|
}
|
|
702
710
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
+
if (objectElements.length > 0) {
|
|
712
|
+
const objectParam = import_typescript.factory.createParameterDeclaration(
|
|
713
|
+
void 0,
|
|
714
|
+
void 0,
|
|
715
|
+
import_typescript.factory.createObjectBindingPattern(objectElements),
|
|
716
|
+
void 0,
|
|
717
|
+
import_typescript.factory.createTypeLiteralNode(typeObjectElements),
|
|
718
|
+
void 0
|
|
719
|
+
);
|
|
720
|
+
return [objectParam, ...refParameters];
|
|
721
|
+
}
|
|
722
|
+
return refParameters;
|
|
711
723
|
}
|
|
712
724
|
static toFormDataStatement(parameters, requestBody) {
|
|
713
725
|
const statements = [];
|
|
@@ -961,8 +973,8 @@ var Generator = class _Generator {
|
|
|
961
973
|
Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
|
|
962
974
|
void 0,
|
|
963
975
|
[
|
|
964
|
-
parameters.length > 0 ? _Generator.
|
|
965
|
-
req?.schema ? _Generator.toRequestBodyTypeNode(req.schema) :
|
|
976
|
+
...parameters.length > 0 ? _Generator.toDeclarationNodes(parameters) : [],
|
|
977
|
+
...req?.schema ? [_Generator.toRequestBodyTypeNode(req.schema)] : []
|
|
966
978
|
].filter(Boolean),
|
|
967
979
|
void 0,
|
|
968
980
|
this.bodyBlock(
|
|
@@ -1082,7 +1094,6 @@ var AxiosAdapter = class extends Adapter {
|
|
|
1082
1094
|
)
|
|
1083
1095
|
) : []
|
|
1084
1096
|
).concat(
|
|
1085
|
-
// Add body if needed
|
|
1086
1097
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? import_typescript2.factory.createPropertyAssignment(
|
|
1087
1098
|
import_typescript2.factory.createIdentifier(adapter.bodyFieldName),
|
|
1088
1099
|
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")
|
|
@@ -1168,7 +1179,6 @@ var FetchAdapter = class extends Adapter {
|
|
|
1168
1179
|
)
|
|
1169
1180
|
) : []
|
|
1170
1181
|
).concat(
|
|
1171
|
-
// Add body if needed
|
|
1172
1182
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? import_typescript3.factory.createPropertyAssignment(
|
|
1173
1183
|
import_typescript3.factory.createIdentifier(adapter.bodyFieldName),
|
|
1174
1184
|
shouldUseFormData ? import_typescript3.factory.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? import_typescript3.factory.createCallExpression(
|
|
@@ -1622,7 +1632,11 @@ var V3 = class {
|
|
|
1622
1632
|
type: upLevelSchemaKey + refName
|
|
1623
1633
|
};
|
|
1624
1634
|
}
|
|
1625
|
-
|
|
1635
|
+
const resolvedSchema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1636
|
+
if (!resolvedSchema) {
|
|
1637
|
+
return { type: "unknown" };
|
|
1638
|
+
}
|
|
1639
|
+
schema = resolvedSchema;
|
|
1626
1640
|
}
|
|
1627
1641
|
return this.toBaseSchema(schema, enums, "", upLevelSchemaKey + refName);
|
|
1628
1642
|
}
|
|
@@ -1631,7 +1645,14 @@ var V3 = class {
|
|
|
1631
1645
|
*/
|
|
1632
1646
|
getParameterByRef(schema, enums = [], upLevelSchemaKey = "") {
|
|
1633
1647
|
if (Base.isRef(schema)) {
|
|
1634
|
-
|
|
1648
|
+
const resolvedSchema = this.doc.components?.parameters?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1649
|
+
if (!resolvedSchema) {
|
|
1650
|
+
return {
|
|
1651
|
+
name: "unknown",
|
|
1652
|
+
in: "query"
|
|
1653
|
+
};
|
|
1654
|
+
}
|
|
1655
|
+
schema = resolvedSchema;
|
|
1635
1656
|
}
|
|
1636
1657
|
const {
|
|
1637
1658
|
name,
|
|
@@ -1680,7 +1701,11 @@ var V3 = class {
|
|
|
1680
1701
|
*/
|
|
1681
1702
|
getResponseByRef(schema) {
|
|
1682
1703
|
if (Base.isRef(schema)) {
|
|
1683
|
-
|
|
1704
|
+
const resolvedSchema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1705
|
+
if (!resolvedSchema) {
|
|
1706
|
+
return [];
|
|
1707
|
+
}
|
|
1708
|
+
schema = resolvedSchema;
|
|
1684
1709
|
}
|
|
1685
1710
|
const { content = {} } = schema;
|
|
1686
1711
|
return Object.keys(content).map((c) => ({
|
|
@@ -1693,7 +1718,11 @@ var V3 = class {
|
|
|
1693
1718
|
*/
|
|
1694
1719
|
getRequestBodyByRef(schema, enums = []) {
|
|
1695
1720
|
if (Base.isRef(schema)) {
|
|
1696
|
-
|
|
1721
|
+
const resolvedSchema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1722
|
+
if (!resolvedSchema) {
|
|
1723
|
+
return [];
|
|
1724
|
+
}
|
|
1725
|
+
schema = resolvedSchema;
|
|
1697
1726
|
}
|
|
1698
1727
|
const { content = {} } = schema;
|
|
1699
1728
|
return Object.keys(content).map((c) => ({
|
|
@@ -2317,8 +2346,8 @@ var import_execa = require("execa");
|
|
|
2317
2346
|
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
2318
2347
|
var PLUGIN_NAME = "apiCodeGen";
|
|
2319
2348
|
var logger2 = (0, import_logger2.createScopedLogger)("api-codegen-vite-plugin");
|
|
2320
|
-
var tsc = async () => {
|
|
2321
|
-
await (0, import_execa.execaCommand)(
|
|
2349
|
+
var tsc = async (path) => {
|
|
2350
|
+
await (0, import_execa.execaCommand)(`npx tsc ${path} --noEmit`);
|
|
2322
2351
|
};
|
|
2323
2352
|
function apiCodeGenPlugin(options) {
|
|
2324
2353
|
let firstRun = true;
|
|
@@ -2336,9 +2365,13 @@ function apiCodeGenPlugin(options) {
|
|
|
2336
2365
|
const code = await codeGen(codeGenInitOptions);
|
|
2337
2366
|
await import_fs_extra.default.createFile(codeGenInitOptions.output);
|
|
2338
2367
|
await import_fs_extra.default.writeFile(codeGenInitOptions.output, code);
|
|
2368
|
+
try {
|
|
2369
|
+
await tsc(codeGenInitOptions.output);
|
|
2370
|
+
} catch (error) {
|
|
2371
|
+
logger2.error(error);
|
|
2372
|
+
}
|
|
2339
2373
|
} catch (error) {
|
|
2340
|
-
logger2.error(`Failed to generate api ${name}`);
|
|
2341
|
-
console.error(error);
|
|
2374
|
+
logger2.error(`Failed to generate api ${name}: ${error}`);
|
|
2342
2375
|
}
|
|
2343
2376
|
return {
|
|
2344
2377
|
...proxies_,
|
|
@@ -2348,11 +2381,6 @@ function apiCodeGenPlugin(options) {
|
|
|
2348
2381
|
Promise.resolve({})
|
|
2349
2382
|
);
|
|
2350
2383
|
logger2.info("-------> api codegen finished <--------");
|
|
2351
|
-
try {
|
|
2352
|
-
await tsc();
|
|
2353
|
-
} catch (error) {
|
|
2354
|
-
logger2.error(error);
|
|
2355
|
-
}
|
|
2356
2384
|
return {
|
|
2357
2385
|
...config,
|
|
2358
2386
|
server: {
|