WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit a63d45d

Browse files
authored
fix/delete-mutation-input (#124)
1 parent ba90508 commit a63d45d

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/adapters/node-http/core.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { ZodError } from 'zod';
1010
import { generateOpenApiDocument } from '../../generator';
1111
import {
1212
OpenApiErrorResponse,
13+
OpenApiMethod,
1314
OpenApiResponse,
1415
OpenApiRouter,
1516
OpenApiSuccessResponse,
1617
} from '../../types';
18+
import { acceptsRequestBody } from '../../utils/method';
1719
import { normalizePath } from '../../utils/path';
1820
import { TRPC_ERROR_CODE_HTTP_STATUS, getErrorFromUnknown } from './errors';
1921
import { getBody, getQuery } from './input';
@@ -63,7 +65,7 @@ export const createOpenApiNodeHttpHandler = <
6365
res.end(JSON.stringify(body));
6466
};
6567

66-
const method = req.method!;
68+
const method = req.method! as OpenApiMethod & 'HEAD';
6769
const reqUrl = req.url!;
6870
const url = new URL(reqUrl.startsWith('/') ? `http://127.0.0.1${reqUrl}` : reqUrl);
6971
const path = normalizePath(url.pathname);
@@ -92,7 +94,7 @@ export const createOpenApiNodeHttpHandler = <
9294
}
9395

9496
input = {
95-
...(procedure.type === 'query' ? getQuery(req, url) : await getBody(req, maxBodySize)),
97+
...(acceptsRequestBody(method) ? await getBody(req, maxBodySize) : getQuery(req, url)),
9698
...pathInput,
9799
};
98100
ctx = await createContext?.({ req, res });

src/generator/paths.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TRPCError } from '@trpc/server';
22
import { OpenAPIV3 } from 'openapi-types';
33

44
import { OpenApiRouter } from '../types';
5+
import { acceptsRequestBody } from '../utils/method';
56
import { getPathParameters, normalizePath } from '../utils/path';
67
import {
78
forEachOpenApiProcedure,
@@ -10,13 +11,6 @@ import {
1011
} from '../utils/procedure';
1112
import { getParameterObjects, getRequestBodyObject, getResponsesObject } from './schema';
1213

13-
const acceptsRequestBody = (httpMethod: OpenAPIV3.HttpMethods) => {
14-
if (httpMethod === 'get' || httpMethod === 'delete') {
15-
return false;
16-
}
17-
return true;
18-
};
19-
2014
export const getOpenApiPathsObject = (
2115
appRouter: OpenApiRouter,
2216
pathsObject: OpenAPIV3.PathsObject,
@@ -69,7 +63,7 @@ export const getOpenApiPathsObject = (
6963
description,
7064
tags: tags ?? (tag ? [tag] : undefined),
7165
security: protect ? [{ Authorization: [] }] : undefined,
72-
...(acceptsRequestBody(httpMethod)
66+
...(acceptsRequestBody(method)
7367
? {
7468
requestBody: getRequestBodyObject(inputParser, pathParameters),
7569
parameters: [

src/utils/method.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { OpenApiMethod } from '../types';
2+
3+
export const acceptsRequestBody = (method: OpenApiMethod) => {
4+
if (method === 'GET' || method === 'DELETE') {
5+
return false;
6+
}
7+
return true;
8+
};

0 commit comments

Comments
 (0)