@ts-rest/open-api
Advanced tools
Comparing version 3.16.2 to 3.17.0
102
index.js
import { isAppRoute, isZodObject } from '@ts-rest/core'; | ||
import zodToJsonSchema from 'zod-to-json-schema'; | ||
import { generateSchema } from '@anatine/zod-openapi'; | ||
@@ -23,3 +23,3 @@ const getPathsFromRouter = (router, pathHistory) => { | ||
}; | ||
const getJsonSchemaFromZod = (zodObject) => { | ||
const getOpenApiSchemaFromZod = (zodObject, useOutput = false) => { | ||
const isZodObj = isZodObject(zodObject); | ||
@@ -29,10 +29,31 @@ if (!isZodObj) { | ||
} | ||
const schema = zodToJsonSchema(zodObject, { | ||
name: 'zodObject', | ||
target: 'openApi3', | ||
$refStrategy: 'none', | ||
return generateSchema(zodObject, useOutput); | ||
}; | ||
const getPathParameters = (path, zodObject) => { | ||
var _a; | ||
const isZodObj = isZodObject(zodObject); | ||
const paramsFromPath = (_a = path | ||
.match(/{[^}]+}/g)) === null || _a === void 0 ? void 0 : _a.map((param) => param.slice(1, -1)).filter((param) => { | ||
return !isZodObj || !zodObject.shape[param]; | ||
}); | ||
return schema.definitions['zodObject']; | ||
const params = (paramsFromPath === null || paramsFromPath === void 0 ? void 0 : paramsFromPath.map((param) => ({ | ||
name: param, | ||
in: 'path', | ||
required: true, | ||
schema: { | ||
type: 'string', | ||
}, | ||
}))) || []; | ||
if (isZodObj) { | ||
const paramsFromZod = Object.entries(zodObject.shape).map(([key, value]) => ({ | ||
name: key, | ||
in: 'path', | ||
required: true, | ||
schema: getOpenApiSchemaFromZod(value), | ||
})); | ||
params.push(...paramsFromZod); | ||
} | ||
return params; | ||
}; | ||
const getQuerySchemaFromZod = (zodObject, jsonQuery = false) => { | ||
const getQueryParametersFromZod = (zodObject, jsonQuery = false) => { | ||
const isZodObj = isZodObject(zodObject); | ||
@@ -42,23 +63,24 @@ if (!isZodObj) { | ||
} | ||
if (jsonQuery) { | ||
return Object.entries(zodObject.shape).map(([key, value]) => { | ||
const schema = getJsonSchemaFromZod(value); | ||
return { | ||
name: key, | ||
in: 'query', | ||
content: { | ||
'application/json': { | ||
schema: schema, | ||
return Object.entries(zodObject.shape).map(([key, value]) => { | ||
const schema = getOpenApiSchemaFromZod(value); | ||
const isObject = value._def.typeName === 'ZodObject'; | ||
const isRequired = !value.isOptional(); | ||
return { | ||
name: key, | ||
in: 'query', | ||
...(isRequired && { required: true }), | ||
...(jsonQuery | ||
? { | ||
content: { | ||
'application/json': { | ||
schema: schema, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}); | ||
} | ||
return [ | ||
{ | ||
name: 'query', | ||
in: 'query', | ||
schema: getJsonSchemaFromZod(zodObject), | ||
}, | ||
]; | ||
} | ||
: { | ||
...(isObject && { style: 'deepObject' }), | ||
schema: schema, | ||
}), | ||
}; | ||
}); | ||
}; | ||
@@ -76,3 +98,3 @@ const generateOpenApi = (router, apiDoc, options = {}) => { | ||
const pathObject = paths.reduce((acc, path) => { | ||
var _a, _b; | ||
var _a; | ||
if (options.setOperationId) { | ||
@@ -85,11 +107,10 @@ const existingOp = operationIds.get(path.id); | ||
} | ||
const paramsFromPath = (_a = path.path | ||
.match(/{[^}]+}/g)) === null || _a === void 0 ? void 0 : _a.map((param) => param.slice(1, -1)); | ||
const querySchema = getQuerySchemaFromZod(path.route.query, !!options.jsonQuery); | ||
const bodySchema = ((_b = path.route) === null || _b === void 0 ? void 0 : _b.method) !== 'GET' | ||
? getJsonSchemaFromZod(path.route.body) | ||
const pathParams = getPathParameters(path.path, path.route.pathParams); | ||
const querySchema = getQueryParametersFromZod(path.route.query, !!options.jsonQuery); | ||
const bodySchema = ((_a = path.route) === null || _a === void 0 ? void 0 : _a.method) !== 'GET' | ||
? getOpenApiSchemaFromZod(path.route.body) | ||
: null; | ||
const responses = Object.keys(path.route.responses).reduce((acc, key) => { | ||
const keyAsNumber = Number(key); | ||
const responseSchema = getJsonSchemaFromZod(path.route.responses[keyAsNumber]); | ||
const responseSchema = getOpenApiSchemaFromZod(path.route.responses[keyAsNumber], true); | ||
return { | ||
@@ -116,12 +137,3 @@ ...acc, | ||
tags: path.paths, | ||
parameters: [ | ||
...(paramsFromPath | ||
? paramsFromPath.map((param) => ({ | ||
name: param, | ||
in: 'path', | ||
required: true, | ||
})) | ||
: []), | ||
...querySchema, | ||
], | ||
parameters: [...pathParams, ...querySchema], | ||
...(options.setOperationId ? { operationId: path.id } : {}), | ||
@@ -128,0 +140,0 @@ ...(bodySchema |
{ | ||
"name": "@ts-rest/open-api", | ||
"version": "3.16.2", | ||
"version": "3.17.0", | ||
"dependencies": { | ||
"openapi3-ts": "^2.0.2", | ||
"zod-to-json-schema": "^3.17.1" | ||
"@anatine/zod-openapi": "^1.12.0", | ||
"openapi3-ts": "^2.0.2" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3.0.0", | ||
"@ts-rest/core": "3.16.2" | ||
"@ts-rest/core": "3.17.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"zod": { | ||
"optional": true | ||
} | ||
}, | ||
"typedoc": { | ||
@@ -18,0 +13,0 @@ "entryPoint": "./src/index.ts", |
Sorry, the diff of this file is not supported yet
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
19123
329
+ Added@anatine/zod-openapi@^1.12.0
+ Added@anatine/zod-openapi@1.14.2(transitive)
+ Added@ts-rest/core@3.17.0(transitive)
+ Addedts-deepmerge@6.2.1(transitive)
- Removedzod-to-json-schema@^3.17.1
- Removed@ts-rest/core@3.16.2(transitive)
- Removedzod-to-json-schema@3.24.1(transitive)