@asteasolutions/zod-to-openapi
Advanced tools
Comparing version 1.4.1 to 2.0.0
@@ -21,5 +21,2 @@ export declare class ZodToOpenAPIError { | ||
} | ||
export declare class MissingResponseDescriptionError extends ZodToOpenAPIError { | ||
constructor(); | ||
} | ||
interface UnknownZodTypeErrorProps { | ||
@@ -26,0 +23,0 @@ schemaName?: string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UnknownZodTypeError = exports.MissingResponseDescriptionError = exports.MissingParameterDataError = exports.ConflictError = exports.ZodToOpenAPIError = void 0; | ||
exports.UnknownZodTypeError = exports.MissingParameterDataError = exports.ConflictError = exports.ZodToOpenAPIError = void 0; | ||
class ZodToOpenAPIError { | ||
@@ -24,8 +24,2 @@ constructor(message) { | ||
exports.MissingParameterDataError = MissingParameterDataError; | ||
class MissingResponseDescriptionError extends ZodToOpenAPIError { | ||
constructor() { | ||
super('Missing response description. Please specify `description` and using `ZodSchema.openapi`.'); | ||
} | ||
} | ||
exports.MissingResponseDescriptionError = MissingResponseDescriptionError; | ||
class UnknownZodTypeError extends ZodToOpenAPIError { | ||
@@ -32,0 +26,0 @@ constructor(data) { |
@@ -27,2 +27,3 @@ import type { z } from 'zod'; | ||
export declare function isZodType<TypeName extends keyof ZodTypes>(schema: object, typeName: TypeName): schema is ZodTypes[TypeName]; | ||
export declare function isAnyZodType(schema: object): schema is z.ZodType; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isZodType = void 0; | ||
exports.isAnyZodType = exports.isZodType = void 0; | ||
function isZodType(schema, typeName) { | ||
@@ -8,1 +8,5 @@ return schema.constructor.name === typeName; | ||
exports.isZodType = isZodType; | ||
function isAnyZodType(schema) { | ||
return '_def' in schema; | ||
} | ||
exports.isAnyZodType = isAnyZodType; |
@@ -37,3 +37,3 @@ import { OpenAPIObject, InfoObject, ServerObject, SecurityRequirementObject, TagObject, ExternalDocumentationObject, ComponentsObject } from 'openapi3-ts'; | ||
private getResponse; | ||
private descriptionFromResponseConfig; | ||
private getBodyContent; | ||
private toOpenAPISchema; | ||
@@ -40,0 +40,0 @@ private isOptionalSchema; |
@@ -236,18 +236,9 @@ "use strict"; | ||
} | ||
getRequestBody(bodySchema) { | ||
if (!bodySchema) { | ||
getRequestBody(requestBody) { | ||
if (!requestBody) { | ||
return; | ||
} | ||
const schema = this.generateInnerSchema(bodySchema); | ||
const metadata = this.getMetadata(bodySchema); | ||
return { | ||
description: metadata === null || metadata === void 0 ? void 0 : metadata.description, | ||
required: !bodySchema.isOptional(), | ||
content: { | ||
// TODO: Maybe should be coming from metadata | ||
'application/json': { | ||
schema, | ||
}, | ||
}, | ||
}; | ||
const { content } = requestBody, rest = __rest(requestBody, ["content"]); | ||
const requestBodyContent = this.getBodyContent(requestBody.content); | ||
return Object.assign(Object.assign({}, rest), { content: requestBodyContent }); | ||
} | ||
@@ -281,35 +272,17 @@ getParameters(request) { | ||
} | ||
getResponse(response) { | ||
const description = this.descriptionFromResponseConfig(response); | ||
if ((0, zod_is_type_1.isZodType)(response, 'ZodVoid')) { | ||
return { description }; | ||
} | ||
const responseSchema = this.generateInnerSchema(response.schema); | ||
return { | ||
description, | ||
headers: response.headers, | ||
links: response.links, | ||
content: { | ||
[response.mediaType]: { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
}; | ||
getResponse(_a) { | ||
var { content } = _a, rest = __rest(_a, ["content"]); | ||
const responseContent = content | ||
? { content: this.getBodyContent(content) } | ||
: {}; | ||
return Object.assign(Object.assign({}, rest), responseContent); | ||
} | ||
descriptionFromResponseConfig(response) { | ||
if ((0, zod_is_type_1.isZodType)(response, 'ZodVoid')) { | ||
const metadata = this.getMetadata(response); | ||
if (!(metadata === null || metadata === void 0 ? void 0 : metadata.description)) { | ||
throw new errors_1.MissingResponseDescriptionError(); | ||
getBodyContent(content) { | ||
return (0, lodash_1.mapValues)(content, config => { | ||
if (!(0, zod_is_type_1.isAnyZodType)(config.schema)) { | ||
return { schema: config.schema }; | ||
} | ||
return metadata.description; | ||
} | ||
if (response.description) { | ||
return response.description; | ||
} | ||
const metadata = this.getMetadata(response.schema); | ||
if (!(metadata === null || metadata === void 0 ? void 0 : metadata.description)) { | ||
throw new errors_1.MissingResponseDescriptionError(); | ||
} | ||
return metadata.description; | ||
const schema = this.generateInnerSchema(config.schema); | ||
return { schema }; | ||
}); | ||
} | ||
@@ -316,0 +289,0 @@ toOpenAPISchema(zodSchema, isNullable) { |
@@ -1,11 +0,24 @@ | ||
import { CallbackObject, ComponentsObject, ExampleObject, HeaderObject, HeadersObject, ISpecificationExtension, LinkObject, LinksObject, OperationObject, ParameterObject, RequestBodyObject, ResponseObject, SchemaObject, SecuritySchemeObject } from 'openapi3-ts'; | ||
import type { ZodVoid, ZodObject, ZodSchema, ZodType } from 'zod'; | ||
import { CallbackObject, ComponentsObject, EncodingObject, ExampleObject, ExamplesObject, HeaderObject, HeadersObject, ISpecificationExtension, LinkObject, LinksObject, OperationObject, ParameterObject, ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject, SecuritySchemeObject } from 'openapi3-ts'; | ||
import type { ZodObject, ZodSchema, ZodType } from 'zod'; | ||
declare type Method = 'get' | 'post' | 'put' | 'delete' | 'patch'; | ||
export declare type ResponseConfig = { | ||
mediaType: string; | ||
schema: ZodType<unknown>; | ||
export interface ZodMediaTypeObject { | ||
schema: ZodType<unknown> | SchemaObject | ReferenceObject; | ||
examples?: ExamplesObject; | ||
example?: any; | ||
encoding?: EncodingObject; | ||
} | ||
export interface ZodContentObject { | ||
[mediaType: string]: ZodMediaTypeObject; | ||
} | ||
export interface ZodRequestBody { | ||
description?: string; | ||
content: ZodContentObject; | ||
required?: boolean; | ||
} | ||
export interface ResponseConfig { | ||
description: string; | ||
headers?: HeadersObject; | ||
links?: LinksObject; | ||
} | ZodVoid; | ||
content?: ZodContentObject; | ||
} | ||
export interface RouteConfig extends OperationObject { | ||
@@ -15,3 +28,3 @@ method: Method; | ||
request?: { | ||
body?: ZodType<unknown>; | ||
body?: ZodRequestBody; | ||
params?: ZodObject<any>; | ||
@@ -18,0 +31,0 @@ query?: ZodObject<any>; |
@@ -29,3 +29,2 @@ "use strict"; | ||
const initialExtend = this.extend; | ||
// TODO: This does an overload everytime. So .extend().openapi() makes this change twice | ||
result.extend = function (...args) { | ||
@@ -32,0 +31,0 @@ var _a; |
{ | ||
"name": "@asteasolutions/zod-to-openapi", | ||
"version": "1.4.1", | ||
"version": "2.0.0", | ||
"description": "Builds OpenAPI schemas from Zod schemas", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -208,8 +208,12 @@ # Zod to OpenAPI | ||
200: { | ||
mediaType: 'application/json', | ||
schema: UserSchema.openapi({ | ||
description: 'Object with user data.', | ||
}), | ||
description: 'Object with user data.', | ||
content: { | ||
'application/json': { | ||
schema: UserSchema, | ||
}, | ||
}, | ||
}, | ||
204: z.void(), | ||
204: { | ||
description: 'No content - successful operation', | ||
}, | ||
}, | ||
@@ -250,7 +254,9 @@ }); | ||
- `query`, `params` - being instances of `ZodObject` | ||
- `body` - being any `zod` instance | ||
- `body` - an object with a `description` and a `content` record where: | ||
- the key is a `mediaType` string like `application/json` | ||
- and the value is an object with a `schema` of any `zod` type | ||
- `headers` - an array of `zod` instances | ||
- `responses` - an object where the key is the status code or `default` and the value is either: | ||
- an instance of `ZodVoid` - meaning a no content response | ||
- an object with `mediaType` (a string like `application/json`) and a `schema` of any zod type | ||
- `responses` - an object where the key is the status code or `default` and the value is an object with a `description` and a `content` record where: | ||
- the key is a `mediaType` string like `application/json` | ||
- and the value is an object with a `schema` of any `zod` type | ||
@@ -257,0 +263,0 @@ #### Defining route parameters |
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
389
54335
963