Skip to content

Commit

Permalink
v2.1.0 Upgrade zod-openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
mcampa committed Dec 2, 2024
1 parent f60b36b commit a3a4106
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 41 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@ Note: This project is a fork of a fork, with full credit to the original authors

## Changelog

- 2.0.4 Upgrade to tRPC 11.0.0-rc.648
- 2.0.3 Added support for array inputs for GET requests
- v2.1.0

- Updated the minimum version of `zod-openapi` to 4.1.0.
- Changed `zod-openapi` to a peer dependency.
- The `protect` option now defaults to `true`.
- Improved Error schema titles

- v2.0.4

- Upgraded to tRPC 11.0.0-rc.648.

- v2.0.3
- Added support for array inputs in GET requests.

## Usage

Expand Down Expand Up @@ -363,7 +374,7 @@ Please see [full typings here](src/types.ts).
| `enabled` | `boolean` | Exposes this procedure to `trpc-to-openapi` adapters and on the OpenAPI document. | `false` | `true` |
| `method` | `HttpMethod` | HTTP method this endpoint is exposed on. Value can be `GET`, `POST`, `PATCH`, `PUT` or `DELETE`. | `true` | `undefined` |
| `path` | `string` | Pathname this endpoint is exposed on. Value must start with `/`, specify path parameters using `{}`. | `true` | `undefined` |
| `protect` | `boolean` | Requires this endpoint to use a security scheme. | `false` | `false` |
| `protect` | `boolean` | Requires this endpoint to use a security scheme. | `false` | `true` |
| `summary` | `string` | A short summary of the endpoint included in the OpenAPI document. | `false` | `undefined` |
| `description` | `string` | A verbose description of the endpoint included in the OpenAPI document. | `false` | `undefined` |
| `tags` | `string[]` | A list of tags used for logical grouping of endpoints in the OpenAPI document. | `false` | `undefined` |
Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module.exports = {
escapeString: true,
printBasicPrototype: true,
},
prettierPath: require.resolve('formatter-for-jest-snapshots'),
};
37 changes: 28 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trpc-to-openapi",
"version": "2.0.4",
"version": "2.1.0",
"description": "tRPC OpenAPI",
"author": "mcampa",
"private": false,
Expand Down Expand Up @@ -50,14 +50,14 @@
},
"peerDependencies": {
"@trpc/server": "^11.0.0-rc.648",
"zod": "^3.23.8"
"zod": "^3.23.8",
"zod-openapi": "^4.1.0"
},
"dependencies": {
"co-body": "^6.1.0",
"h3": "^1.6.4",
"lodash.clonedeep": "^4.5.0",
"openapi3-ts": "4.3.3",
"zod-openapi": "^2.19.0"
"openapi3-ts": "4.3.3"
},
"devDependencies": {
"@trpc/client": "^11.0.0-rc.648",
Expand All @@ -79,6 +79,7 @@
"node-fetch": "^2.6.11",
"openapi-schema-validator": "^12.1.1",
"prettier": "^3.4.1",
"formatter-for-jest-snapshots": "npm:prettier@^2",
"rimraf": "^6.0.1",
"superjson": "^1.12.3",
"ts-jest": "^29.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/generator/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export const getOpenApiPathsObject = (

const {
method,
protect,
summary,
description,
tags,
requestHeaders,
responseHeaders,
successDescription,
errorResponses,
protect = true,
} = openapi;

const path = normalizePath(openapi.path);
Expand Down Expand Up @@ -101,7 +101,7 @@ export const getOpenApiPathsObject = (
});
}
const isInputRequired = !inputParser.isOptional();
const o = inputParser?._def?.openapi;
const o = inputParser?._def.zodOpenApi?.openapi;
const inputSchema = unwrapZodType(inputParser, true).openapi({
...(o?.title ? { title: o?.title } : {}),
...(o?.description ? { description: o?.description } : {}),
Expand Down Expand Up @@ -150,7 +150,7 @@ export const getOpenApiPathsObject = (
outputParser,
httpMethod,
responseHeaders,
protect ?? false,
protect,
hasInputs(inputParser),
successDescription,
errorResponses,
Expand Down
16 changes: 10 additions & 6 deletions src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
extendZodWithOpenApi,
} from 'zod-openapi';

import { HTTP_STATUS_TRPC_ERROR_CODE, TRPC_ERROR_CODE_MESSAGE } from '../adapters/node-http/errors';
import {
HTTP_STATUS_TRPC_ERROR_CODE,
TRPC_ERROR_CODE_HTTP_STATUS,
TRPC_ERROR_CODE_MESSAGE,
} from '../adapters/node-http/errors';
import { OpenApiContentType } from '../types';
import {
instanceofZodType,
Expand Down Expand Up @@ -114,7 +118,7 @@ export const getRequestBodyObject = (
pathParameters.forEach((pathParameter) => {
mask[pathParameter] = true;
});
const o = schema._def.openapi;
const o = schema._def.zodOpenApi?.openapi;
const dedupedSchema = schema.omit(mask).openapi({
...(o?.title ? { title: o?.title } : {}),
...(o?.description ? { description: o?.description } : {}),
Expand Down Expand Up @@ -143,7 +147,7 @@ export const hasInputs = (schema: unknown) =>
const errorResponseObjectByCode: Record<string, ZodOpenApiResponseObject> = {};

export const errorResponseObject = (
code = 'INTERNAL_SERVER_ERROR',
code: TRPCError['code'] = 'INTERNAL_SERVER_ERROR',
message?: string,
issues?: { message: string }[],
): ZodOpenApiResponseObject => {
Expand Down Expand Up @@ -171,7 +175,7 @@ export const errorResponseObject = (
}),
})
.openapi({
title: 'Error',
title: `${message ?? 'Internal server'} error (${TRPC_ERROR_CODE_HTTP_STATUS[code] ?? 500})`,
description: 'The error information',
example: {
code: code ?? 'INTERNAL_SERVER_ERROR',
Expand All @@ -190,11 +194,11 @@ export const errorResponseObject = (
export const errorResponseFromStatusCode = (status: number) => {
const code = HTTP_STATUS_TRPC_ERROR_CODE[status];
const message = code && TRPC_ERROR_CODE_MESSAGE[code];
return errorResponseObject(code ?? 'UNKNOWN_ERROR', message ?? 'Unknown error');
return errorResponseObject(code, message ?? 'Unknown error');
};

export const errorResponseFromMessage = (status: number, message: string) =>
errorResponseObject(HTTP_STATUS_TRPC_ERROR_CODE[status] ?? 'UNKNOWN_ERROR', message);
errorResponseObject(HTTP_STATUS_TRPC_ERROR_CODE[status], message);

export const getResponsesObject = (
schema: ZodTypeAny,
Expand Down
Loading

0 comments on commit a3a4106

Please sign in to comment.