@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.d.cts
CHANGED
|
@@ -154,7 +154,7 @@ interface ProviderInitResult {
|
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
156
|
* @file Adapter abstract class definition
|
|
157
|
-
* @author
|
|
157
|
+
* @author wp.l
|
|
158
158
|
* @description Base adapter implementation for various code generation tools
|
|
159
159
|
*/
|
|
160
160
|
|
|
@@ -200,7 +200,7 @@ declare abstract class Adapter {
|
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
202
|
* @file Base class implementation
|
|
203
|
-
* @author
|
|
203
|
+
* @author wp.l
|
|
204
204
|
* @description Base utility class providing common methods for code generation and API handling
|
|
205
205
|
*/
|
|
206
206
|
|
|
@@ -314,13 +314,13 @@ declare abstract class Base {
|
|
|
314
314
|
* @param schema - The object to check.
|
|
315
315
|
* @returns - True if the object is a reference.
|
|
316
316
|
*/
|
|
317
|
-
static isRef(schema:
|
|
317
|
+
static isRef(schema: unknown): schema is ReferenceObject;
|
|
318
318
|
}
|
|
319
319
|
|
|
320
320
|
/**
|
|
321
|
-
* @file
|
|
322
|
-
*
|
|
323
|
-
* and processing API documentation.
|
|
321
|
+
* @file Base class implementation
|
|
322
|
+
* @author wp.l
|
|
323
|
+
* @description This file defines the `Provider` abstract class, which serves as a base for providers responsible for parsing and processing API documentation.
|
|
324
324
|
*/
|
|
325
325
|
|
|
326
326
|
/**
|
|
@@ -507,7 +507,7 @@ declare class Generator {
|
|
|
507
507
|
static isBinarySchema(schema: SchemaObject): boolean;
|
|
508
508
|
static toRequestBodyTypeNode(schema: SchemaObject): ParameterDeclaration;
|
|
509
509
|
static toTypeNode(schema: SchemaObject): TypeNode;
|
|
510
|
-
static
|
|
510
|
+
static toDeclarationNodes(parameters: ParameterObject[]): ParameterDeclaration[];
|
|
511
511
|
static toFormDataStatement(parameters: ParameterObject[], requestBody?: SchemaObject): Statement[];
|
|
512
512
|
static bodyBlock(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter): Block;
|
|
513
513
|
static schemaToStatemets(parsedDoc: ProviderInitResult, adaptor: Adapter, options: Omit<ProviderInitOptions, "docURL" | "output" | "requestOptions">): Statement[];
|
|
@@ -530,7 +530,7 @@ type apiCodeGenPluginOptions = ProviderInitOptions & {
|
|
|
530
530
|
name: string;
|
|
531
531
|
proxy?: ServerOptions["proxy"];
|
|
532
532
|
};
|
|
533
|
-
declare const tsc: () => Promise<void>;
|
|
533
|
+
declare const tsc: (path: string) => Promise<void>;
|
|
534
534
|
declare function apiCodeGenPlugin(options: apiCodeGenPluginOptions[]): PluginOption;
|
|
535
535
|
|
|
536
536
|
export { Adapter, Adaptors, ArraySchemaType, type ArrayTypeSchemaObject, AxiosAdapter, Base, type CommentObject, type Comments, type EnumSchemaObject, FetchAdapter, type FetchDocRequestInit, Generator, HttpMethods, type JSONValue, type MediaTypeObject, MediaTypes, NonArraySchemaType, OpenAPIProvider, OpenAPIVersion, type OperationObject, ParameterIn, type ParameterObject, type PathObject, type PathsObject, Provider, type ProviderInitOptions, type ProviderInitResult, type ReferenceObject, type RequestBodyObject, type ResponsesObject, SchemaFormatType, type SchemaObject, SchemaType, type SingleTypeSchemaObject, SuccessHttpStatusCode, apiCodeGenPlugin, type apiCodeGenPluginOptions, codeGen, tsc };
|
package/npm/index.d.ts
CHANGED
|
@@ -154,7 +154,7 @@ interface ProviderInitResult {
|
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
156
|
* @file Adapter abstract class definition
|
|
157
|
-
* @author
|
|
157
|
+
* @author wp.l
|
|
158
158
|
* @description Base adapter implementation for various code generation tools
|
|
159
159
|
*/
|
|
160
160
|
|
|
@@ -200,7 +200,7 @@ declare abstract class Adapter {
|
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
202
|
* @file Base class implementation
|
|
203
|
-
* @author
|
|
203
|
+
* @author wp.l
|
|
204
204
|
* @description Base utility class providing common methods for code generation and API handling
|
|
205
205
|
*/
|
|
206
206
|
|
|
@@ -314,13 +314,13 @@ declare abstract class Base {
|
|
|
314
314
|
* @param schema - The object to check.
|
|
315
315
|
* @returns - True if the object is a reference.
|
|
316
316
|
*/
|
|
317
|
-
static isRef(schema:
|
|
317
|
+
static isRef(schema: unknown): schema is ReferenceObject;
|
|
318
318
|
}
|
|
319
319
|
|
|
320
320
|
/**
|
|
321
|
-
* @file
|
|
322
|
-
*
|
|
323
|
-
* and processing API documentation.
|
|
321
|
+
* @file Base class implementation
|
|
322
|
+
* @author wp.l
|
|
323
|
+
* @description This file defines the `Provider` abstract class, which serves as a base for providers responsible for parsing and processing API documentation.
|
|
324
324
|
*/
|
|
325
325
|
|
|
326
326
|
/**
|
|
@@ -507,7 +507,7 @@ declare class Generator {
|
|
|
507
507
|
static isBinarySchema(schema: SchemaObject): boolean;
|
|
508
508
|
static toRequestBodyTypeNode(schema: SchemaObject): ParameterDeclaration;
|
|
509
509
|
static toTypeNode(schema: SchemaObject): TypeNode;
|
|
510
|
-
static
|
|
510
|
+
static toDeclarationNodes(parameters: ParameterObject[]): ParameterDeclaration[];
|
|
511
511
|
static toFormDataStatement(parameters: ParameterObject[], requestBody?: SchemaObject): Statement[];
|
|
512
512
|
static bodyBlock(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter): Block;
|
|
513
513
|
static schemaToStatemets(parsedDoc: ProviderInitResult, adaptor: Adapter, options: Omit<ProviderInitOptions, "docURL" | "output" | "requestOptions">): Statement[];
|
|
@@ -530,7 +530,7 @@ type apiCodeGenPluginOptions = ProviderInitOptions & {
|
|
|
530
530
|
name: string;
|
|
531
531
|
proxy?: ServerOptions["proxy"];
|
|
532
532
|
};
|
|
533
|
-
declare const tsc: () => Promise<void>;
|
|
533
|
+
declare const tsc: (path: string) => Promise<void>;
|
|
534
534
|
declare function apiCodeGenPlugin(options: apiCodeGenPluginOptions[]): PluginOption;
|
|
535
535
|
|
|
536
536
|
export { Adapter, Adaptors, ArraySchemaType, type ArrayTypeSchemaObject, AxiosAdapter, Base, type CommentObject, type Comments, type EnumSchemaObject, FetchAdapter, type FetchDocRequestInit, Generator, HttpMethods, type JSONValue, type MediaTypeObject, MediaTypes, NonArraySchemaType, OpenAPIProvider, OpenAPIVersion, type OperationObject, ParameterIn, type ParameterObject, type PathObject, type PathsObject, Provider, type ProviderInitOptions, type ProviderInitResult, type ReferenceObject, type RequestBodyObject, type ResponsesObject, SchemaFormatType, type SchemaObject, SchemaType, type SingleTypeSchemaObject, SuccessHttpStatusCode, apiCodeGenPlugin, type apiCodeGenPluginOptions, codeGen, tsc };
|
package/npm/index.js
CHANGED
|
@@ -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
|
|
|
@@ -613,24 +621,24 @@ var Generator = class _Generator {
|
|
|
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 = [];
|
|
@@ -912,8 +924,8 @@ var Generator = class _Generator {
|
|
|
912
924
|
Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
|
|
913
925
|
void 0,
|
|
914
926
|
[
|
|
915
|
-
parameters.length > 0 ? _Generator.
|
|
916
|
-
req?.schema ? _Generator.toRequestBodyTypeNode(req.schema) :
|
|
927
|
+
...parameters.length > 0 ? _Generator.toDeclarationNodes(parameters) : [],
|
|
928
|
+
...req?.schema ? [_Generator.toRequestBodyTypeNode(req.schema)] : []
|
|
917
929
|
].filter(Boolean),
|
|
918
930
|
void 0,
|
|
919
931
|
this.bodyBlock(
|
|
@@ -1033,7 +1045,6 @@ var AxiosAdapter = class extends Adapter {
|
|
|
1033
1045
|
)
|
|
1034
1046
|
) : []
|
|
1035
1047
|
).concat(
|
|
1036
|
-
// Add body if needed
|
|
1037
1048
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t2.createPropertyAssignment(
|
|
1038
1049
|
t2.createIdentifier(adapter.bodyFieldName),
|
|
1039
1050
|
shouldUseFormData ? t2.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t2.createIdentifier("req") : t2.createIdentifier("req")
|
|
@@ -1119,7 +1130,6 @@ var FetchAdapter = class extends Adapter {
|
|
|
1119
1130
|
)
|
|
1120
1131
|
) : []
|
|
1121
1132
|
).concat(
|
|
1122
|
-
// Add body if needed
|
|
1123
1133
|
shouldUseFormData || inBody.length > 0 || requestBody?.schema ? t3.createPropertyAssignment(
|
|
1124
1134
|
t3.createIdentifier(adapter.bodyFieldName),
|
|
1125
1135
|
shouldUseFormData ? t3.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? t3.createCallExpression(
|
|
@@ -1573,7 +1583,11 @@ var V3 = class {
|
|
|
1573
1583
|
type: upLevelSchemaKey + refName
|
|
1574
1584
|
};
|
|
1575
1585
|
}
|
|
1576
|
-
|
|
1586
|
+
const resolvedSchema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1587
|
+
if (!resolvedSchema) {
|
|
1588
|
+
return { type: "unknown" };
|
|
1589
|
+
}
|
|
1590
|
+
schema = resolvedSchema;
|
|
1577
1591
|
}
|
|
1578
1592
|
return this.toBaseSchema(schema, enums, "", upLevelSchemaKey + refName);
|
|
1579
1593
|
}
|
|
@@ -1582,7 +1596,14 @@ var V3 = class {
|
|
|
1582
1596
|
*/
|
|
1583
1597
|
getParameterByRef(schema, enums = [], upLevelSchemaKey = "") {
|
|
1584
1598
|
if (Base.isRef(schema)) {
|
|
1585
|
-
|
|
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;
|
|
1586
1607
|
}
|
|
1587
1608
|
const {
|
|
1588
1609
|
name,
|
|
@@ -1631,7 +1652,11 @@ var V3 = class {
|
|
|
1631
1652
|
*/
|
|
1632
1653
|
getResponseByRef(schema) {
|
|
1633
1654
|
if (Base.isRef(schema)) {
|
|
1634
|
-
|
|
1655
|
+
const resolvedSchema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1656
|
+
if (!resolvedSchema) {
|
|
1657
|
+
return [];
|
|
1658
|
+
}
|
|
1659
|
+
schema = resolvedSchema;
|
|
1635
1660
|
}
|
|
1636
1661
|
const { content = {} } = schema;
|
|
1637
1662
|
return Object.keys(content).map((c) => ({
|
|
@@ -1644,7 +1669,11 @@ var V3 = class {
|
|
|
1644
1669
|
*/
|
|
1645
1670
|
getRequestBodyByRef(schema, enums = []) {
|
|
1646
1671
|
if (Base.isRef(schema)) {
|
|
1647
|
-
|
|
1672
|
+
const resolvedSchema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
|
|
1673
|
+
if (!resolvedSchema) {
|
|
1674
|
+
return [];
|
|
1675
|
+
}
|
|
1676
|
+
schema = resolvedSchema;
|
|
1648
1677
|
}
|
|
1649
1678
|
const { content = {} } = schema;
|
|
1650
1679
|
return Object.keys(content).map((c) => ({
|
|
@@ -2268,8 +2297,8 @@ import { execaCommand } from "execa";
|
|
|
2268
2297
|
import fs from "fs-extra";
|
|
2269
2298
|
var PLUGIN_NAME = "apiCodeGen";
|
|
2270
2299
|
var logger2 = createScopedLogger2("api-codegen-vite-plugin");
|
|
2271
|
-
var tsc = async () => {
|
|
2272
|
-
await execaCommand(
|
|
2300
|
+
var tsc = async (path) => {
|
|
2301
|
+
await execaCommand(`npx tsc ${path} --noEmit`);
|
|
2273
2302
|
};
|
|
2274
2303
|
function apiCodeGenPlugin(options) {
|
|
2275
2304
|
let firstRun = true;
|
|
@@ -2287,9 +2316,13 @@ function apiCodeGenPlugin(options) {
|
|
|
2287
2316
|
const code = await codeGen(codeGenInitOptions);
|
|
2288
2317
|
await fs.createFile(codeGenInitOptions.output);
|
|
2289
2318
|
await fs.writeFile(codeGenInitOptions.output, code);
|
|
2319
|
+
try {
|
|
2320
|
+
await tsc(codeGenInitOptions.output);
|
|
2321
|
+
} catch (error) {
|
|
2322
|
+
logger2.error(error);
|
|
2323
|
+
}
|
|
2290
2324
|
} catch (error) {
|
|
2291
|
-
logger2.error(`Failed to generate api ${name}`);
|
|
2292
|
-
console.error(error);
|
|
2325
|
+
logger2.error(`Failed to generate api ${name}: ${error}`);
|
|
2293
2326
|
}
|
|
2294
2327
|
return {
|
|
2295
2328
|
...proxies_,
|
|
@@ -2299,11 +2332,6 @@ function apiCodeGenPlugin(options) {
|
|
|
2299
2332
|
Promise.resolve({})
|
|
2300
2333
|
);
|
|
2301
2334
|
logger2.info("-------> api codegen finished <--------");
|
|
2302
|
-
try {
|
|
2303
|
-
await tsc();
|
|
2304
|
-
} catch (error) {
|
|
2305
|
-
logger2.error(error);
|
|
2306
|
-
}
|
|
2307
2335
|
return {
|
|
2308
2336
|
...config,
|
|
2309
2337
|
server: {
|