@ts-rest/open-api
Advanced tools
Comparing version 3.51.1 to 3.52.0-alpha.0
# @ts-rest/open-api | ||
## 3.52.0-alpha.0 | ||
### Minor Changes | ||
- 83416b3: Support automatic reusable "Components" in OpenAPI schemas, reduces final size of schema, uses the "Title" property from @anatine/zod-openapi | ||
## 3.51.1 | ||
@@ -4,0 +10,0 @@ |
@@ -128,2 +128,61 @@ 'use strict'; | ||
}; | ||
const extractReferenceSchemas = (schema, referenceSchemas) => { | ||
var _a, _b, _c; | ||
if (schema.allOf) { | ||
schema.allOf = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((subSchema) => extractReferenceSchemas(subSchema, referenceSchemas)); | ||
} | ||
if (schema.anyOf) { | ||
schema.anyOf = (_b = schema.anyOf) === null || _b === void 0 ? void 0 : _b.map((subSchema) => extractReferenceSchemas(subSchema, referenceSchemas)); | ||
} | ||
if (schema.oneOf) { | ||
schema.oneOf = (_c = schema.oneOf) === null || _c === void 0 ? void 0 : _c.map((subSchema) => extractReferenceSchemas(subSchema, referenceSchemas)); | ||
} | ||
if (schema.not) { | ||
schema.not = extractReferenceSchemas(schema.not, referenceSchemas); | ||
} | ||
if (schema.items) { | ||
schema.items = extractReferenceSchemas(schema.items, referenceSchemas); | ||
} | ||
if (schema.properties) { | ||
schema.properties = Object.entries(schema.properties).reduce((prev, [propertyName, schema]) => { | ||
prev[propertyName] = extractReferenceSchemas(schema, referenceSchemas); | ||
return prev; | ||
}, {}); | ||
} | ||
if (schema.additionalProperties) { | ||
schema.additionalProperties = | ||
typeof schema.additionalProperties != 'boolean' | ||
? extractReferenceSchemas(schema.additionalProperties, referenceSchemas) | ||
: schema.additionalProperties; | ||
} | ||
if (schema.title) { | ||
const nullable = schema.nullable; | ||
schema.nullable = undefined; | ||
if (schema.title in referenceSchemas) { | ||
if (JSON.stringify(referenceSchemas[schema.title]) !== | ||
JSON.stringify(schema)) { | ||
throw new Error(`Schema title '${schema.title}' already exists with a different schema`); | ||
} | ||
} | ||
else { | ||
referenceSchemas[schema.title] = schema; | ||
} | ||
if (nullable) { | ||
schema = { | ||
nullable: true, | ||
allOf: [ | ||
{ | ||
$ref: `#/components/schemas/${schema.title}`, | ||
}, | ||
], | ||
}; | ||
} | ||
else { | ||
schema = { | ||
$ref: `#/components/schemas/${schema.title}`, | ||
}; | ||
} | ||
} | ||
return schema; | ||
}; | ||
/** | ||
@@ -144,2 +203,3 @@ * | ||
const operationIds = new Map(); | ||
const referenceSchemas = {}; | ||
const pathObject = paths.reduce((acc, path) => { | ||
@@ -157,10 +217,16 @@ var _a, _b, _c, _d; | ||
const querySchema = getQueryParametersFromZod(path.route.query, !!options.jsonQuery); | ||
const bodySchema = ((_a = path.route) === null || _a === void 0 ? void 0 : _a.method) !== 'GET' && 'body' in path.route | ||
let bodySchema = ((_a = path.route) === null || _a === void 0 ? void 0 : _a.method) !== 'GET' && 'body' in path.route | ||
? getOpenApiSchemaFromZod(path.route.body) | ||
: null; | ||
if (bodySchema === null || bodySchema === void 0 ? void 0 : bodySchema.title) { | ||
bodySchema = extractReferenceSchemas(bodySchema, referenceSchemas); | ||
} | ||
const responses = Object.entries(path.route.responses).reduce((acc, [statusCode, response]) => { | ||
const responseSchema = getOpenApiSchemaFromZod(response, true); | ||
let responseSchema = getOpenApiSchemaFromZod(response, true); | ||
const description = core.isZodType(response) && response.description | ||
? response.description | ||
: statusCode; | ||
if (responseSchema) { | ||
responseSchema = extractReferenceSchemas(responseSchema, referenceSchemas); | ||
} | ||
return { | ||
@@ -220,2 +286,10 @@ ...acc, | ||
}, {}); | ||
if (Object.keys(referenceSchemas).length) { | ||
apiDoc['components'] = { | ||
schemas: { | ||
...referenceSchemas, | ||
}, | ||
...apiDoc['components'], | ||
}; | ||
} | ||
return { | ||
@@ -222,0 +296,0 @@ openapi: '3.0.2', |
{ | ||
"name": "@ts-rest/open-api", | ||
"version": "3.51.1", | ||
"version": "3.52.0-alpha.0", | ||
"repository": { | ||
@@ -26,5 +26,5 @@ "type": "git", | ||
"peerDependencies": { | ||
"@ts-rest/core": "~3.51.0", | ||
"@ts-rest/core": "3.52.0-alpha.0", | ||
"zod": "^3.22.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
40302
607
2