zenko 0.1.10-beta.2 → 0.1.10-beta.4
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 +21 -11
- package/dist/cli.cjs +94 -176
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.mjs +94 -176
- package/dist/cli.mjs.map +1 -1
- package/dist/index.cjs +13 -74
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -1
- package/dist/index.d.ts +48 -1
- package/dist/index.mjs +12 -71
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
type OpenAPISpec = {
|
|
2
|
+
openapi: string;
|
|
3
|
+
info: unknown;
|
|
4
|
+
paths: Record<string, Record<string, unknown>>;
|
|
5
|
+
webhooks?: Record<string, Record<string, unknown>>;
|
|
6
|
+
components?: {
|
|
7
|
+
schemas?: Record<string, unknown>;
|
|
8
|
+
parameters?: Record<string, unknown>;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
type TypesHelperMode = "package" | "inline" | "file";
|
|
12
|
+
type TypesConfig = {
|
|
13
|
+
emit?: boolean;
|
|
14
|
+
helpers?: TypesHelperMode;
|
|
15
|
+
helpersOutput?: string;
|
|
16
|
+
treeShake?: boolean;
|
|
17
|
+
optionalType?: "optional" | "nullable" | "nullish";
|
|
18
|
+
};
|
|
19
|
+
type GenerateOptions = {
|
|
20
|
+
strictDates?: boolean;
|
|
21
|
+
strictNumeric?: boolean;
|
|
22
|
+
types?: TypesConfig;
|
|
23
|
+
operationIds?: string[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Generates TypeScript client code from an OpenAPI specification.
|
|
27
|
+
*
|
|
28
|
+
* @param spec - The OpenAPI specification object.
|
|
29
|
+
* @param options - Configuration options controlling code generation behavior.
|
|
30
|
+
* @returns Generated TypeScript code as a string.
|
|
31
|
+
*/
|
|
32
|
+
declare function generate(spec: OpenAPISpec, options?: GenerateOptions): string;
|
|
33
|
+
|
|
34
|
+
type PathFn<TArgs extends unknown[] = []> = (...args: TArgs) => string;
|
|
35
|
+
type RequestMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";
|
|
36
|
+
type HeaderFn<TArgs extends unknown[] = [], TResult = Record<string, unknown> | Record<string, never>> = (...args: TArgs) => TResult;
|
|
37
|
+
type AnyHeaderFn = HeaderFn<any, unknown> | (() => unknown);
|
|
38
|
+
type OperationErrors<TError = unknown> = TError extends Record<string, unknown> ? TError : Record<string, TError>;
|
|
39
|
+
type OperationDefinition<TMethod extends RequestMethod, TPath extends (...args: any[]) => string, TRequest = undefined, TResponse = undefined, THeaders extends AnyHeaderFn | undefined = undefined, TErrors extends OperationErrors | undefined = undefined> = {
|
|
40
|
+
method: TMethod;
|
|
41
|
+
path: TPath;
|
|
42
|
+
request?: TRequest;
|
|
43
|
+
response?: TResponse;
|
|
44
|
+
headers?: THeaders;
|
|
45
|
+
errors?: TErrors;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { type GenerateOptions, type HeaderFn, type OpenAPISpec, type OperationDefinition, type OperationErrors, type PathFn, type TypesConfig, generate };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// src/utils/topological-sort.ts
|
|
2
2
|
function topologicalSort(schemas) {
|
|
3
3
|
const visited = /* @__PURE__ */ new Set();
|
|
4
4
|
const visiting = /* @__PURE__ */ new Set();
|
|
@@ -47,7 +47,7 @@ function extractRefName(ref) {
|
|
|
47
47
|
return ref.split("/").pop() || "Unknown";
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
//
|
|
50
|
+
// src/utils/property-name.ts
|
|
51
51
|
function isValidJSIdentifier(name) {
|
|
52
52
|
if (!name) return false;
|
|
53
53
|
const firstChar = name.at(0);
|
|
@@ -127,7 +127,7 @@ function formatPropertyName(name) {
|
|
|
127
127
|
return isValidJSIdentifier(name) ? name : `"${name}"`;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
//
|
|
130
|
+
// src/utils/string-utils.ts
|
|
131
131
|
function toCamelCase(str) {
|
|
132
132
|
return str.replace(/-([a-zA-Z])/g, (_, letter) => letter.toUpperCase()).replace(/-+$/, "");
|
|
133
133
|
}
|
|
@@ -135,7 +135,7 @@ function capitalize(str) {
|
|
|
135
135
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
//
|
|
138
|
+
// src/utils/http-status.ts
|
|
139
139
|
var statusNameMap = {
|
|
140
140
|
"400": "badRequest",
|
|
141
141
|
"401": "unauthorized",
|
|
@@ -201,7 +201,7 @@ function isErrorStatus(status) {
|
|
|
201
201
|
return code >= 400;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
//
|
|
204
|
+
// src/utils/tree-shaking.ts
|
|
205
205
|
function analyzeZenkoUsage(operations) {
|
|
206
206
|
const usage = {
|
|
207
207
|
usesHeaderFn: false,
|
|
@@ -241,7 +241,7 @@ function hasAnyErrors(errors) {
|
|
|
241
241
|
return Boolean(errors && Object.keys(errors).length > 0);
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
-
//
|
|
244
|
+
// src/utils/collect-inline-types.ts
|
|
245
245
|
function collectInlineRequestTypes(operations, spec) {
|
|
246
246
|
const requestTypesToGenerate = /* @__PURE__ */ new Map();
|
|
247
247
|
const operationLookup = /* @__PURE__ */ new Map();
|
|
@@ -318,48 +318,8 @@ function collectInlineResponseTypes(operations, spec) {
|
|
|
318
318
|
}
|
|
319
319
|
return responseTypesToGenerate;
|
|
320
320
|
}
|
|
321
|
-
function collectInlineErrorTypes(operations, spec) {
|
|
322
|
-
const errorTypesToGenerate = /* @__PURE__ */ new Map();
|
|
323
|
-
const operationLookup = /* @__PURE__ */ new Map();
|
|
324
|
-
for (const [, pathItem] of Object.entries(spec.paths || {})) {
|
|
325
|
-
for (const [, operation] of Object.entries(pathItem)) {
|
|
326
|
-
const op = operation;
|
|
327
|
-
if (op.operationId) {
|
|
328
|
-
operationLookup.set(op.operationId, op);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
for (const [, pathItem] of Object.entries(spec.webhooks || {})) {
|
|
333
|
-
for (const [, operation] of Object.entries(pathItem)) {
|
|
334
|
-
const op = operation;
|
|
335
|
-
if (op.operationId) {
|
|
336
|
-
operationLookup.set(op.operationId, op);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
for (const op of operations) {
|
|
341
|
-
const operation = operationLookup.get(op.operationId);
|
|
342
|
-
if (!operation) continue;
|
|
343
|
-
const responses = operation.responses || {};
|
|
344
|
-
for (const [statusCode, response] of Object.entries(responses)) {
|
|
345
|
-
if (isErrorStatus(statusCode) && response.content) {
|
|
346
|
-
const content = response.content;
|
|
347
|
-
const jsonContent = content["application/json"];
|
|
348
|
-
if (jsonContent && jsonContent.schema) {
|
|
349
|
-
const schema = jsonContent.schema;
|
|
350
|
-
const identifier = mapStatusToIdentifier(statusCode);
|
|
351
|
-
const typeName = `${capitalize(toCamelCase(op.operationId))}${capitalize(identifier)}`;
|
|
352
|
-
if (!schema.$ref || schema.allOf || schema.oneOf || schema.anyOf) {
|
|
353
|
-
errorTypesToGenerate.set(typeName, schema);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
return errorTypesToGenerate;
|
|
360
|
-
}
|
|
361
321
|
|
|
362
|
-
//
|
|
322
|
+
// src/utils/collect-referenced-schemas.ts
|
|
363
323
|
function findContentType(content) {
|
|
364
324
|
const contentTypes = Object.keys(content);
|
|
365
325
|
if (contentTypes.includes("application/json")) {
|
|
@@ -497,7 +457,7 @@ function collectReferencedSchemas(operations, spec) {
|
|
|
497
457
|
return referenced;
|
|
498
458
|
}
|
|
499
459
|
|
|
500
|
-
//
|
|
460
|
+
// src/utils/generate-helper-file.ts
|
|
501
461
|
function generateHelperFile() {
|
|
502
462
|
const output = [];
|
|
503
463
|
output.push("// Generated helper types for Zenko");
|
|
@@ -539,7 +499,7 @@ function generateHelperFile() {
|
|
|
539
499
|
return output.join("\n");
|
|
540
500
|
}
|
|
541
501
|
|
|
542
|
-
//
|
|
502
|
+
// src/zenko.ts
|
|
543
503
|
function generateWithMetadata(spec, options = {}) {
|
|
544
504
|
const output = [];
|
|
545
505
|
const generatedTypes = /* @__PURE__ */ new Set();
|
|
@@ -765,9 +725,6 @@ function generateWithMetadata(spec, options = {}) {
|
|
|
765
725
|
}
|
|
766
726
|
return result;
|
|
767
727
|
}
|
|
768
|
-
function generateFromDocument(spec, options = {}) {
|
|
769
|
-
return generateWithMetadata(spec, options);
|
|
770
|
-
}
|
|
771
728
|
function generateRequestTypes(output, operations, spec, nameMap, schemaOptions) {
|
|
772
729
|
const requestTypesToGenerate = collectInlineRequestTypes(operations, spec);
|
|
773
730
|
if (requestTypesToGenerate.size > 0) {
|
|
@@ -790,8 +747,7 @@ function generateRequestTypes(output, operations, spec, nameMap, schemaOptions)
|
|
|
790
747
|
}
|
|
791
748
|
function generateResponseTypes(output, operations, spec, nameMap, schemaOptions) {
|
|
792
749
|
const responseTypesToGenerate = collectInlineResponseTypes(operations, spec);
|
|
793
|
-
|
|
794
|
-
if (responseTypesToGenerate.size > 0 || errorTypesToGenerate.size > 0) {
|
|
750
|
+
if (responseTypesToGenerate.size > 0) {
|
|
795
751
|
output.push("// Generated Response Types");
|
|
796
752
|
output.push("");
|
|
797
753
|
for (const [typeName, schema] of responseTypesToGenerate) {
|
|
@@ -807,23 +763,10 @@ function generateResponseTypes(output, operations, spec, nameMap, schemaOptions)
|
|
|
807
763
|
output.push(`export type ${typeName} = z.infer<typeof ${typeName}>;`);
|
|
808
764
|
output.push("");
|
|
809
765
|
}
|
|
810
|
-
for (const [typeName, schema] of errorTypesToGenerate) {
|
|
811
|
-
const generatedSchema = generateZodSchema(
|
|
812
|
-
typeName,
|
|
813
|
-
schema,
|
|
814
|
-
/* @__PURE__ */ new Set(),
|
|
815
|
-
schemaOptions,
|
|
816
|
-
nameMap
|
|
817
|
-
);
|
|
818
|
-
output.push(generatedSchema);
|
|
819
|
-
output.push("");
|
|
820
|
-
output.push(`export type ${typeName} = z.infer<typeof ${typeName}>;`);
|
|
821
|
-
output.push("");
|
|
822
|
-
}
|
|
823
766
|
}
|
|
824
767
|
}
|
|
825
768
|
function generate(spec, options = {}) {
|
|
826
|
-
return
|
|
769
|
+
return generateWithMetadata(spec, options).output;
|
|
827
770
|
}
|
|
828
771
|
function appendOperationField(buffer, key, value) {
|
|
829
772
|
if (!value) return;
|
|
@@ -1552,8 +1495,6 @@ function applyNumericBounds(schema, builder) {
|
|
|
1552
1495
|
return builder;
|
|
1553
1496
|
}
|
|
1554
1497
|
export {
|
|
1555
|
-
generate
|
|
1556
|
-
generateFromDocument,
|
|
1557
|
-
generateWithMetadata
|
|
1498
|
+
generate
|
|
1558
1499
|
};
|
|
1559
1500
|
//# sourceMappingURL=index.mjs.map
|