@asteasolutions/zod-to-openapi
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -17,2 +17,3 @@ import { OpenAPIObject, InfoObject, ServerObject, SecurityRequirementObject, TagObject, ExternalDocumentationObject, ComponentsObject } from 'openapi3-ts'; | ||
private pathRefs; | ||
private webhookRefs; | ||
private rawComponents; | ||
@@ -37,3 +38,5 @@ constructor(definitions: OpenAPIDefinitions[]); | ||
private getParameters; | ||
private generatePath; | ||
private generateSingleRoute; | ||
private generateSingleWebhook; | ||
private getResponse; | ||
@@ -40,0 +43,0 @@ private getBodyContent; |
@@ -18,2 +18,3 @@ "use strict"; | ||
const zod_is_type_1 = require("./lib/zod-is-type"); | ||
const enum_info_1 = require("./lib/enum-info"); | ||
class OpenAPIGenerator { | ||
@@ -25,2 +26,3 @@ constructor(definitions) { | ||
this.pathRefs = {}; | ||
this.webhookRefs = {}; | ||
this.rawComponents = []; | ||
@@ -31,3 +33,5 @@ this.sortDefinitions(); | ||
this.definitions.forEach(definition => this.generateSingle(definition)); | ||
return Object.assign(Object.assign({}, config), { components: this.buildComponents(), paths: this.pathRefs }); | ||
return Object.assign(Object.assign(Object.assign({}, config), { components: this.buildComponents(), paths: this.pathRefs }), (Object.keys(this.webhookRefs).length && { | ||
webhooks: this.webhookRefs, | ||
})); | ||
} | ||
@@ -55,2 +59,3 @@ generateComponents() { | ||
'route', | ||
'webhook', | ||
]; | ||
@@ -74,2 +79,5 @@ this.definitions.sort((left, right) => { | ||
return; | ||
case 'webhook': | ||
this.generateSingleWebhook(definition.webhook); | ||
return; | ||
case 'component': | ||
@@ -246,3 +254,3 @@ this.rawComponents.push(definition); | ||
const { content } = requestBody, rest = __rest(requestBody, ["content"]); | ||
const requestBodyContent = this.getBodyContent(requestBody.content); | ||
const requestBodyContent = this.getBodyContent(content); | ||
return Object.assign(Object.assign({}, rest), { content: requestBodyContent }); | ||
@@ -264,3 +272,3 @@ } | ||
} | ||
generateSingleRoute(route) { | ||
generatePath(route) { | ||
const { method, path, request, responses } = route, pathItemConfig = __rest(route, ["method", "path", "request", "responses"]); | ||
@@ -275,5 +283,14 @@ const generatedResponses = (0, lodash_1.mapValues)(responses, response => { | ||
}; | ||
this.pathRefs[path] = Object.assign(Object.assign({}, this.pathRefs[path]), routeDoc); | ||
return routeDoc; | ||
} | ||
generateSingleRoute(route) { | ||
const routeDoc = this.generatePath(route); | ||
this.pathRefs[route.path] = Object.assign(Object.assign({}, this.pathRefs[route.path]), routeDoc); | ||
return routeDoc; | ||
} | ||
generateSingleWebhook(route) { | ||
const routeDoc = this.generatePath(route); | ||
this.webhookRefs[route.path] = Object.assign(Object.assign({}, this.webhookRefs[route.path]), routeDoc); | ||
return routeDoc; | ||
} | ||
getResponse(_a) { | ||
@@ -370,9 +387,18 @@ var { content } = _a, rest = __rest(_a, ["content"]); | ||
if ((0, zod_is_type_1.isZodType)(zodSchema, 'ZodNativeEnum')) { | ||
const enumValues = Object.values(zodSchema._def.values); | ||
// ZodNativeEnum can accepts number values for enum but in odd format | ||
// Not worth it for now so using plain string | ||
const { type, values } = (0, enum_info_1.enumInfo)(zodSchema._def.values); | ||
if (type === 'mixed') { | ||
// enum Test { | ||
// A = 42, | ||
// B = 'test', | ||
// } | ||
// | ||
// const result = z.nativeEnum(Test).parse('42'); | ||
// | ||
// This is an error, so we can't just say it's a 'string' | ||
throw new errors_1.ZodToOpenAPIError('Enum has mixed string and number values, please specify the OpenAPI type manually'); | ||
} | ||
return { | ||
type: 'string', | ||
type: type === 'numeric' ? 'number' : 'string', | ||
nullable: isNullable ? true : undefined, | ||
enum: enumValues, | ||
enum: values, | ||
}; | ||
@@ -379,0 +405,0 @@ } |
@@ -54,2 +54,5 @@ import { CallbackObject, ComponentsObject, EncodingObject, ExampleObject, ExamplesObject, HeaderObject, HeadersObject, ISpecificationExtension, LinkObject, LinksObject, OperationObject, ParameterObject, ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject, SecuritySchemeObject } from 'openapi3-ts'; | ||
route: RouteConfig; | ||
} | { | ||
type: 'webhook'; | ||
webhook: RouteConfig; | ||
}; | ||
@@ -74,2 +77,6 @@ export declare class OpenAPIRegistry { | ||
/** | ||
* Registers a new webhook that would be generated under webhooks: | ||
*/ | ||
registerWebhook(webhook: RouteConfig): void; | ||
/** | ||
* Registers a raw OpenAPI component. Use this if you have a simple object instead of a Zod schema. | ||
@@ -76,0 +83,0 @@ * |
@@ -46,2 +46,11 @@ "use strict"; | ||
/** | ||
* Registers a new webhook that would be generated under webhooks: | ||
*/ | ||
registerWebhook(webhook) { | ||
this._definitions.push({ | ||
type: 'webhook', | ||
webhook, | ||
}); | ||
} | ||
/** | ||
* Registers a raw OpenAPI component. Use this if you have a simple object instead of a Zod schema. | ||
@@ -48,0 +57,0 @@ * |
{ | ||
"name": "@asteasolutions/zod-to-openapi", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Builds OpenAPI schemas from Zod schemas", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59802
19
1099