@ts-rest/open-api
Advanced tools
Comparing version 1.3.0 to 2.0.1
# @ts-rest/open-api | ||
## 2.0.1 | ||
### Patch Changes | ||
- 119aed6: Bump versions to 2.0.1 | ||
## 2.0.0 | ||
### Major Changes | ||
- 4792b26: Change contract to support multiple responses, for different statuses | ||
### Patch Changes | ||
- 4792b26: Add error handling support to express | ||
## 1.3.0 | ||
@@ -4,0 +20,0 @@ |
{ | ||
"name": "@ts-rest/open-api", | ||
"version": "1.3.0", | ||
"version": "2.0.1", | ||
"type": "commonjs", | ||
@@ -9,4 +9,4 @@ "main": "./src/index.js", | ||
"peerDependencies": { | ||
"@ts-rest/core": "1.3.0", | ||
"zod": "^3.17.10", | ||
"@ts-rest/core": "2.0.1", | ||
"zod": "^3.18.0", | ||
"openapi3-ts": "^2.0.2", | ||
@@ -13,0 +13,0 @@ "zod-to-json-schema": "^3.17.1", |
@@ -22,2 +22,11 @@ "use strict"; | ||
}; | ||
const isZodObject = (body) => { | ||
return (body === null || body === void 0 ? void 0 : body.safeParse) !== undefined; | ||
}; | ||
const getResponseSchemaFromZod = (response) => isZodObject(response) | ||
? (0, zod_to_json_schema_1.default)(response, { | ||
name: 'zodObject', | ||
target: 'openApi3', | ||
}).definitions['zodObject'] | ||
: undefined; | ||
const generateOpenApi = (router, options) => { | ||
@@ -32,5 +41,2 @@ const paths = getPathsFromRouter(router); | ||
}; | ||
const isZodObject = (body) => { | ||
return (body === null || body === void 0 ? void 0 : body.safeParse) !== undefined; | ||
}; | ||
const pathObject = paths.reduce((acc, path) => { | ||
@@ -40,14 +46,17 @@ var _a, _b; | ||
.match(/{[^}]+}/g)) === null || _a === void 0 ? void 0 : _a.map((param) => param.slice(1, -1)); | ||
const bodySchema = ((_b = path.route) === null || _b === void 0 ? void 0 : _b.__type) === 'AppRouteMutation' && isZodObject(path.route.body) | ||
? (0, zod_to_json_schema_1.default)(path.route.body, { | ||
name: 'zodObject', | ||
target: 'openApi3', | ||
}).definitions['zodObject'] | ||
: undefined; | ||
const responseSchema = isZodObject(path.route.response) | ||
? (0, zod_to_json_schema_1.default)(path.route.response, { | ||
name: 'zodObject', | ||
target: 'openApi3', | ||
}).definitions['zodObject'] | ||
: undefined; | ||
const bodySchema = ((_b = path.route) === null || _b === void 0 ? void 0 : _b.__tsType) === 'AppRouteMutation' && | ||
getResponseSchemaFromZod(path.route.responses); | ||
const responses = Object.keys(path.route.responses).reduce((acc, key) => { | ||
const keyAsNumber = Number(key); | ||
const responseSchema = getResponseSchemaFromZod(path.route.responses[keyAsNumber]); | ||
return Object.assign(Object.assign({}, acc), { [keyAsNumber]: Object.assign({ description: `${keyAsNumber}` }, (responseSchema | ||
? { | ||
content: { | ||
'application/json': { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
} | ||
: {})) }); | ||
}, {}); | ||
const newPath = Object.assign(Object.assign({ description: path.route.description, summary: path.route.summary, deprecated: path.route.deprecated, tags: path.paths, parameters: paramsFromPath === null || paramsFromPath === void 0 ? void 0 : paramsFromPath.map((param) => ({ | ||
@@ -68,13 +77,3 @@ name: param, | ||
} | ||
: {})), { responses: { | ||
200: Object.assign({ description: 'Success' }, (responseSchema | ||
? { | ||
content: { | ||
'application/json': { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
} | ||
: {})), | ||
} }); | ||
: {})), { responses }); | ||
acc[path.path] = Object.assign(Object.assign({}, acc[path.path]), { [mapMethod[path.route.method]]: newPath }); | ||
@@ -81,0 +80,0 @@ return acc; |
Sorry, the diff of this file is not supported yet
8463
91