@trpc/server 11.0.0-rc.354 → 11.0.0-rc.361
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@trpc/server/http.d.ts +0 -1
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +2 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts +10 -0
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/content-type/json/index.js +62 -0
- package/dist/adapters/aws-lambda/content-type/json/index.mjs +60 -0
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +19 -9
- package/dist/adapters/aws-lambda/index.mjs +19 -9
- package/dist/adapters/aws-lambda/utils.d.ts +3 -12
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +12 -1
- package/dist/adapters/aws-lambda/utils.mjs +12 -2
- package/dist/adapters/content-handlers/concurrentCache.d.ts +7 -0
- package/dist/adapters/content-handlers/concurrentCache.d.ts.map +1 -0
- package/dist/adapters/content-handlers/concurrentCache.js +17 -0
- package/dist/adapters/content-handlers/concurrentCache.mjs +15 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +10 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +33 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +31 -0
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +0 -1
- package/dist/adapters/express.mjs +0 -1
- package/dist/adapters/fastify/content-type/json/index.d.ts +8 -0
- package/dist/adapters/fastify/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/fastify/content-type/json/index.js +62 -0
- package/dist/adapters/fastify/content-type/json/index.mjs +60 -0
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -9
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +10 -2
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +10 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/types.d.ts +11 -0
- package/dist/adapters/fastify/types.d.ts.map +1 -0
- package/dist/adapters/fetch/content-type/json/index.d.ts +9 -0
- package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/fetch/content-type/json/index.js +55 -0
- package/dist/adapters/fetch/content-type/json/index.mjs +53 -0
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -5
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +16 -2
- package/dist/adapters/fetch/fetchRequestHandler.mjs +16 -2
- package/dist/adapters/fetch/types.d.ts +9 -12
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +0 -1
- package/dist/adapters/next.mjs +0 -1
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +4 -25
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +25 -128
- package/dist/adapters/node-http/content-type/form-data/index.mjs +25 -103
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -12
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +4 -12
- package/dist/adapters/node-http/content-type/json/index.d.ts +4 -1
- package/dist/adapters/node-http/content-type/json/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/index.js +62 -10
- package/dist/adapters/node-http/content-type/json/index.mjs +62 -10
- package/dist/adapters/node-http/content-type/octet/index.d.ts +5 -0
- package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -0
- package/dist/adapters/node-http/content-type/octet/index.js +19 -0
- package/dist/adapters/node-http/content-type/octet/index.mjs +17 -0
- package/dist/adapters/node-http/content-type/types.d.ts +8 -0
- package/dist/adapters/node-http/content-type/types.d.ts.map +1 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +19 -21
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +20 -22
- package/dist/adapters/node-http/types.d.ts +8 -19
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +0 -1
- package/dist/adapters/standalone.mjs +0 -1
- package/dist/adapters/ws.d.ts +2 -12
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/bundle-analysis.json +241 -230
- package/dist/http.js +0 -2
- package/dist/http.mjs +0 -1
- package/dist/index.js +2 -0
- package/dist/index.mjs +1 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +16 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.js +23 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +21 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +7 -15
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -1
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +5 -5
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +26 -20
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +26 -20
- package/dist/unstable-core-do-not-import/http/types.d.ts +0 -2
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +21 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +0 -2
- package/dist/unstable-core-do-not-import.mjs +0 -1
- package/package.json +6 -7
- package/src/@trpc/server/http.ts +0 -1
- package/src/@trpc/server/index.ts +4 -0
- package/src/adapters/aws-lambda/content-type/json/index.ts +108 -0
- package/src/adapters/aws-lambda/index.ts +24 -9
- package/src/adapters/aws-lambda/utils.ts +21 -15
- package/src/adapters/content-handlers/concurrentCache.ts +16 -0
- package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +45 -0
- package/src/adapters/express.ts +1 -6
- package/src/adapters/fastify/content-type/json/index.ts +106 -0
- package/src/adapters/fastify/fastifyRequestHandler.ts +15 -21
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fastify/types.ts +22 -0
- package/src/adapters/fetch/content-type/json/index.ts +90 -0
- package/src/adapters/fetch/fetchRequestHandler.ts +22 -10
- package/src/adapters/fetch/types.ts +22 -15
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
- package/src/adapters/next.ts +1 -6
- package/src/adapters/node-http/content-type/form-data/index.ts +33 -159
- package/src/adapters/node-http/content-type/json/getPostBody.ts +9 -18
- package/src/adapters/node-http/content-type/json/index.ts +98 -9
- package/src/adapters/node-http/content-type/octet/index.ts +27 -0
- package/src/adapters/node-http/{internals/contentType.ts → content-type/types.ts} +2 -14
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +22 -35
- package/src/adapters/node-http/types.ts +46 -46
- package/src/adapters/standalone.ts +1 -2
- package/src/adapters/ws.ts +9 -14
- package/src/unstable-core-do-not-import/contentTypeParsers.ts +37 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +10 -85
- package/src/unstable-core-do-not-import/http/index.ts +0 -1
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +30 -27
- package/src/unstable-core-do-not-import/http/types.ts +0 -2
- package/src/unstable-core-do-not-import/rootConfig.ts +31 -0
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
- package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
- package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
- package/dist/adapters/node-http/internals/contentType.js +0 -8
- package/dist/adapters/node-http/internals/contentType.mjs +0 -6
- package/dist/unstable-core-do-not-import/http/contentType.js +0 -54
- package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -52
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
- package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
|
@@ -9,7 +9,12 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type * as http from 'http';
|
|
11
11
|
// @trpc/server
|
|
12
|
-
import type {
|
|
12
|
+
import type {
|
|
13
|
+
AnyRouter,
|
|
14
|
+
CreateContextCallback,
|
|
15
|
+
inferRouterContext,
|
|
16
|
+
WrapCreateContext,
|
|
17
|
+
} from '../../@trpc/server';
|
|
13
18
|
// @trpc/server/http
|
|
14
19
|
import type {
|
|
15
20
|
HTTPBaseHandlerOptions,
|
|
@@ -17,7 +22,6 @@ import type {
|
|
|
17
22
|
} from '../../@trpc/server/http';
|
|
18
23
|
// eslint-disable-next-line no-restricted-imports
|
|
19
24
|
import type { MaybePromise } from '../../unstable-core-do-not-import';
|
|
20
|
-
import type { NodeHTTPContentTypeHandler } from './internals/contentType';
|
|
21
25
|
|
|
22
26
|
interface ParsedQs {
|
|
23
27
|
[key: string]: ParsedQs | ParsedQs[] | string[] | string | undefined;
|
|
@@ -39,23 +43,14 @@ export type NodeHTTPResponse = http.ServerResponse & {
|
|
|
39
43
|
flush?: () => void;
|
|
40
44
|
};
|
|
41
45
|
|
|
42
|
-
export type
|
|
46
|
+
export type NodeHTTPConditionCreateContextOption<
|
|
43
47
|
TRouter extends AnyRouter,
|
|
44
48
|
TRequest,
|
|
45
49
|
TResponse,
|
|
46
|
-
> =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
**/
|
|
51
|
-
createContext?: NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>;
|
|
52
|
-
}
|
|
53
|
-
: {
|
|
54
|
-
/**
|
|
55
|
-
* @link https://trpc.io/docs/v11/context
|
|
56
|
-
**/
|
|
57
|
-
createContext: NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>;
|
|
58
|
-
};
|
|
50
|
+
> = CreateContextCallback<
|
|
51
|
+
inferRouterContext<TRouter>,
|
|
52
|
+
NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>
|
|
53
|
+
>;
|
|
59
54
|
|
|
60
55
|
/**
|
|
61
56
|
* @internal
|
|
@@ -65,45 +60,50 @@ type ConnectMiddleware<
|
|
|
65
60
|
TResponse extends NodeHTTPResponse = NodeHTTPResponse,
|
|
66
61
|
> = (req: TRequest, res: TResponse, next: (err?: any) => any) => void;
|
|
67
62
|
|
|
63
|
+
interface CoreNodeHTTPHandlerOptions<
|
|
64
|
+
TRouter extends AnyRouter,
|
|
65
|
+
TRequest extends NodeHTTPRequest,
|
|
66
|
+
TResponse extends NodeHTTPResponse,
|
|
67
|
+
> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
68
|
+
/**
|
|
69
|
+
* By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
|
|
70
|
+
*
|
|
71
|
+
* This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* import cors from 'cors'
|
|
75
|
+
*
|
|
76
|
+
* nodeHTTPRequestHandler({
|
|
77
|
+
* cors: cors()
|
|
78
|
+
* })
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* You can also use it for other needs which a connect/node.js compatible middleware can solve,
|
|
82
|
+
* though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
|
|
83
|
+
*/
|
|
84
|
+
middleware?: ConnectMiddleware<TRequest, TResponse>;
|
|
85
|
+
maxBodySize?: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Public API
|
|
68
89
|
export type NodeHTTPHandlerOptions<
|
|
69
90
|
TRouter extends AnyRouter,
|
|
70
91
|
TRequest extends NodeHTTPRequest,
|
|
71
92
|
TResponse extends NodeHTTPResponse,
|
|
72
|
-
> =
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
|
|
76
|
-
*
|
|
77
|
-
* This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
|
|
78
|
-
*
|
|
79
|
-
* ```ts
|
|
80
|
-
* import cors from 'cors'
|
|
81
|
-
*
|
|
82
|
-
* nodeHTTPRequestHandler({
|
|
83
|
-
* cors: cors()
|
|
84
|
-
* })
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* You can also use it for other needs which a connect/node.js compatible middleware can solve,
|
|
88
|
-
* though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
|
|
89
|
-
*/
|
|
90
|
-
middleware?: ConnectMiddleware;
|
|
91
|
-
maxBodySize?: number;
|
|
92
|
-
experimental_contentTypeHandlers?: NodeHTTPContentTypeHandler<
|
|
93
|
-
TRequest,
|
|
94
|
-
TResponse
|
|
95
|
-
>[];
|
|
96
|
-
};
|
|
93
|
+
> = CoreNodeHTTPHandlerOptions<TRouter, TRequest, TResponse> &
|
|
94
|
+
NodeHTTPConditionCreateContextOption<TRouter, TRequest, TResponse>;
|
|
97
95
|
|
|
96
|
+
// Internal user
|
|
98
97
|
export type NodeHTTPRequestHandlerOptions<
|
|
99
98
|
TRouter extends AnyRouter,
|
|
100
99
|
TRequest extends NodeHTTPRequest,
|
|
101
100
|
TResponse extends NodeHTTPResponse,
|
|
102
|
-
> =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
> = CoreNodeHTTPHandlerOptions<TRouter, TRequest, TResponse> &
|
|
102
|
+
WrapCreateContext<NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>> & {
|
|
103
|
+
req: TRequest;
|
|
104
|
+
res: TResponse;
|
|
105
|
+
path: string;
|
|
106
|
+
};
|
|
107
107
|
|
|
108
108
|
export type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
|
|
109
109
|
req: TRequest;
|
|
@@ -37,8 +37,7 @@ export function createHTTPHandler<TRouter extends AnyRouter>(
|
|
|
37
37
|
const path = url.pathname.slice(1);
|
|
38
38
|
|
|
39
39
|
await nodeHTTPRequestHandler({
|
|
40
|
-
|
|
41
|
-
...(opts as CreateHTTPHandlerOptions<AnyRouter>),
|
|
40
|
+
...opts,
|
|
42
41
|
req,
|
|
43
42
|
res,
|
|
44
43
|
path,
|
package/src/adapters/ws.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'http';
|
|
2
2
|
import type ws from 'ws';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
AnyRouter,
|
|
5
|
+
CreateContextCallback,
|
|
6
|
+
inferRouterContext,
|
|
7
|
+
} from '../@trpc/server';
|
|
4
8
|
import {
|
|
5
9
|
callProcedure,
|
|
6
10
|
getErrorShape,
|
|
@@ -46,19 +50,10 @@ export type CreateWSSContextFn<TRouter extends AnyRouter> = (
|
|
|
46
50
|
|
|
47
51
|
export type WSConnectionHandlerOptions<TRouter extends AnyRouter> =
|
|
48
52
|
BaseHandlerOptions<TRouter, IncomingMessage> &
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
**/
|
|
54
|
-
createContext?: CreateWSSContextFn<TRouter>;
|
|
55
|
-
}
|
|
56
|
-
: {
|
|
57
|
-
/**
|
|
58
|
-
* @link https://trpc.io/docs/v11/context
|
|
59
|
-
**/
|
|
60
|
-
createContext: CreateWSSContextFn<TRouter>;
|
|
61
|
-
});
|
|
53
|
+
CreateContextCallback<
|
|
54
|
+
inferRouterContext<TRouter>,
|
|
55
|
+
CreateWSSContextFn<TRouter>
|
|
56
|
+
>;
|
|
62
57
|
|
|
63
58
|
/**
|
|
64
59
|
* Web socket server handler
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TRPCError } from './error/TRPCError';
|
|
2
|
+
import type { ParserZodEsque } from './parser';
|
|
3
|
+
|
|
4
|
+
export type UtilityParser<TInput, TOutput> = ParserZodEsque<TInput, TOutput> & {
|
|
5
|
+
parse: (input: unknown) => TOutput;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// Should be the same possible types as packages/client/src/links/internals/contentTypes.ts isOctetType
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* File is only available from Node19+ but it always extends Blob so we can use that as a type until we eventually drop Node18
|
|
12
|
+
*/
|
|
13
|
+
interface FileLike extends Blob {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* When expecting a supported octet type to be passed from the frontend, this parser may be used to validate the type for your procedure. Note: the output is always a `Readable` stream.
|
|
19
|
+
*/
|
|
20
|
+
export function parseOctetInput<
|
|
21
|
+
TInput extends Blob | Uint8Array | FileLike,
|
|
22
|
+
>(): UtilityParser<TInput, ReadableStream> {
|
|
23
|
+
return {
|
|
24
|
+
_input: null as any as TInput,
|
|
25
|
+
_output: null as any as ReadableStream,
|
|
26
|
+
parse(input: unknown) {
|
|
27
|
+
if (input instanceof ReadableStream) {
|
|
28
|
+
return input;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
throw new TRPCError({
|
|
32
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
33
|
+
cause: `Parsed input was expected to be a ReadableStream but was: ${typeof input}`,
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { TRPCError } from '../error/TRPCError';
|
|
3
|
-
import type { AnyRouter } from '../router';
|
|
4
|
-
import type { CombinedDataTransformer } from '../transformer';
|
|
5
|
-
import type { MaybePromise } from '../types';
|
|
6
|
-
import type { HTTPRequest } from './types';
|
|
7
|
-
|
|
8
|
-
type GetInputs = (opts: {
|
|
9
|
-
req: HTTPRequest;
|
|
10
|
-
isBatchCall: boolean;
|
|
11
|
-
router: AnyRouter;
|
|
12
|
-
preprocessedBody: boolean;
|
|
13
|
-
}) => MaybePromise<Record<number, unknown>>;
|
|
1
|
+
import type { TRPCError } from '../error/TRPCError';
|
|
14
2
|
|
|
15
3
|
export type BodyResult =
|
|
16
4
|
| {
|
|
@@ -24,76 +12,13 @@ export type BodyResult =
|
|
|
24
12
|
| { ok: false; error: TRPCError };
|
|
25
13
|
|
|
26
14
|
export type BaseContentTypeHandler<TOptions> = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
getInputs:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const { req } = opts;
|
|
37
|
-
try {
|
|
38
|
-
if (req.method === 'GET') {
|
|
39
|
-
if (!req.query.has('input')) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
const raw = req.query.get('input');
|
|
43
|
-
return JSON.parse(raw!);
|
|
44
|
-
}
|
|
45
|
-
if (!opts.preprocessedBody && typeof req.body === 'string') {
|
|
46
|
-
// A mutation with no inputs will have req.body === ''
|
|
47
|
-
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
48
|
-
}
|
|
49
|
-
return req.body;
|
|
50
|
-
} catch (cause) {
|
|
51
|
-
throw new TRPCError({
|
|
52
|
-
code: 'PARSE_ERROR',
|
|
53
|
-
cause,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const deserializeInputValue = (
|
|
59
|
-
rawValue: unknown,
|
|
60
|
-
transformer: CombinedDataTransformer,
|
|
61
|
-
) => {
|
|
62
|
-
return typeof rawValue !== 'undefined'
|
|
63
|
-
? transformer.input.deserialize(rawValue)
|
|
64
|
-
: rawValue;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getJsonContentTypeInputs: GetInputs = (opts) => {
|
|
68
|
-
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
69
|
-
const transformer = opts.router._def._config.transformer;
|
|
70
|
-
|
|
71
|
-
if (!opts.isBatchCall) {
|
|
72
|
-
return {
|
|
73
|
-
0: deserializeInputValue(rawInput, transformer),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/* istanbul ignore if */
|
|
78
|
-
if (
|
|
79
|
-
rawInput == null ||
|
|
80
|
-
typeof rawInput !== 'object' ||
|
|
81
|
-
Array.isArray(rawInput)
|
|
82
|
-
) {
|
|
83
|
-
throw new TRPCError({
|
|
84
|
-
code: 'BAD_REQUEST',
|
|
85
|
-
message: '"input" needs to be an object when doing a batch call',
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
const input: Record<number, unknown> = {};
|
|
89
|
-
for (const key in rawInput) {
|
|
90
|
-
const k = key as any as number;
|
|
91
|
-
const rawValue = rawInput[k];
|
|
92
|
-
|
|
93
|
-
const value = deserializeInputValue(rawValue, transformer);
|
|
94
|
-
|
|
95
|
-
input[k] = value;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return input;
|
|
15
|
+
name: string;
|
|
16
|
+
isMatch(headers: Headers): boolean;
|
|
17
|
+
getInputs: (
|
|
18
|
+
opts: TOptions,
|
|
19
|
+
info: {
|
|
20
|
+
isBatchCall: boolean;
|
|
21
|
+
batch: number;
|
|
22
|
+
},
|
|
23
|
+
) => Promise<unknown>;
|
|
99
24
|
};
|
|
@@ -9,9 +9,6 @@ import type {
|
|
|
9
9
|
import { callProcedure } from '../router';
|
|
10
10
|
import type { TRPCResponse } from '../rpc';
|
|
11
11
|
import { transformTRPCResponse } from '../transformer';
|
|
12
|
-
import type { Maybe } from '../types';
|
|
13
|
-
import type { BaseContentTypeHandler } from './contentType';
|
|
14
|
-
import { getJsonContentTypeInputs } from './contentType';
|
|
15
12
|
import { getHTTPStatusCode } from './getHTTPStatusCode';
|
|
16
13
|
import type {
|
|
17
14
|
HTTPBaseHandlerOptions,
|
|
@@ -31,10 +28,6 @@ const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
|
|
|
31
28
|
POST: 'mutation',
|
|
32
29
|
};
|
|
33
30
|
|
|
34
|
-
const fallbackContentTypeHandler = {
|
|
35
|
-
getInputs: getJsonContentTypeInputs,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
31
|
type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
|
|
39
32
|
TBaseType,
|
|
40
33
|
TKey
|
|
@@ -48,9 +41,8 @@ interface ResolveHTTPRequestOptions<
|
|
|
48
41
|
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
49
42
|
req: TRequest;
|
|
50
43
|
path: string;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
preprocessedBody?: boolean;
|
|
44
|
+
getInput: (opts: { isBatchCall: boolean; batch: number }) => Promise<unknown>;
|
|
45
|
+
error?: TRPCError;
|
|
54
46
|
/**
|
|
55
47
|
* Called as soon as the response head is known.
|
|
56
48
|
* When streaming, headers will have been generated
|
|
@@ -231,8 +223,6 @@ export async function resolveHTTPResponse<
|
|
|
231
223
|
unstable_onChunk?.([-1, '']);
|
|
232
224
|
return headResponse;
|
|
233
225
|
}
|
|
234
|
-
const contentTypeHandler =
|
|
235
|
-
opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
236
226
|
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
237
227
|
const allowMethodOverride = opts.allowMethodOverride ?? false;
|
|
238
228
|
|
|
@@ -249,9 +239,6 @@ export async function resolveHTTPResponse<
|
|
|
249
239
|
req.headers['trpc-batch-mode'] === 'stream';
|
|
250
240
|
|
|
251
241
|
try {
|
|
252
|
-
if (opts.error) {
|
|
253
|
-
throw opts.error;
|
|
254
|
-
}
|
|
255
242
|
if (isBatchCall && !allowBatching) {
|
|
256
243
|
throw new TRPCError({
|
|
257
244
|
code: 'BAD_REQUEST',
|
|
@@ -272,22 +259,14 @@ export async function resolveHTTPResponse<
|
|
|
272
259
|
});
|
|
273
260
|
}
|
|
274
261
|
|
|
275
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
276
|
-
isBatchCall,
|
|
277
|
-
req,
|
|
278
|
-
router,
|
|
279
|
-
preprocessedBody: opts.preprocessedBody ?? false,
|
|
280
|
-
});
|
|
281
|
-
|
|
282
262
|
paths = isBatchCall
|
|
283
263
|
? decodeURIComponent(opts.path).split(',')
|
|
284
264
|
: [opts.path];
|
|
285
265
|
const info: TRPCRequestInfo = {
|
|
286
266
|
isBatchCall,
|
|
287
|
-
calls: paths.map((path
|
|
267
|
+
calls: paths.map((path) => ({
|
|
288
268
|
path,
|
|
289
269
|
type,
|
|
290
|
-
input: inputs[idx] ?? undefined,
|
|
291
270
|
})),
|
|
292
271
|
};
|
|
293
272
|
ctx = await opts.createContext({ info });
|
|
@@ -297,12 +276,25 @@ export async function resolveHTTPResponse<
|
|
|
297
276
|
const promises: Promise<
|
|
298
277
|
TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
299
278
|
>[] = paths.map(async (path, index) => {
|
|
300
|
-
|
|
279
|
+
async function getRawInput() {
|
|
280
|
+
return await opts.getInput({
|
|
281
|
+
isBatchCall,
|
|
282
|
+
batch: index,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
301
286
|
try {
|
|
287
|
+
if (opts.error) {
|
|
288
|
+
// sometimes an error may be generated above this function in the stack
|
|
289
|
+
// for instance a 405 error if the method is not supported
|
|
290
|
+
// But we need to handle it here to ensure the error is formatted correctly
|
|
291
|
+
throw opts.error;
|
|
292
|
+
}
|
|
293
|
+
|
|
302
294
|
const data = await callProcedure({
|
|
303
295
|
procedures: opts.router._def.procedures,
|
|
304
296
|
path,
|
|
305
|
-
getRawInput:
|
|
297
|
+
getRawInput: getRawInput,
|
|
306
298
|
ctx,
|
|
307
299
|
type,
|
|
308
300
|
allowMethodOverride,
|
|
@@ -316,6 +308,14 @@ export async function resolveHTTPResponse<
|
|
|
316
308
|
const error = getTRPCErrorFromUnknown(cause);
|
|
317
309
|
errors.push(error);
|
|
318
310
|
|
|
311
|
+
let input: unknown;
|
|
312
|
+
try {
|
|
313
|
+
input = await getRawInput();
|
|
314
|
+
} catch (e) {
|
|
315
|
+
// if `getRawInput` fails, we can't include the input in the error
|
|
316
|
+
input = undefined;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
319
|
opts.onError?.({
|
|
320
320
|
error,
|
|
321
321
|
path,
|
|
@@ -410,7 +410,10 @@ export async function resolveHTTPResponse<
|
|
|
410
410
|
unstable_onChunk([index, body]);
|
|
411
411
|
} catch (cause) {
|
|
412
412
|
const path = paths[index];
|
|
413
|
-
const input =
|
|
413
|
+
const input = await opts.getInput({
|
|
414
|
+
isBatchCall,
|
|
415
|
+
batch: index,
|
|
416
|
+
});
|
|
414
417
|
const { body } = caughtErrorToData(cause, {
|
|
415
418
|
opts,
|
|
416
419
|
ctx,
|
|
@@ -47,7 +47,6 @@ export interface HTTPRequest {
|
|
|
47
47
|
method: string;
|
|
48
48
|
query: URLSearchParams;
|
|
49
49
|
headers: HTTPHeaders;
|
|
50
|
-
body: unknown;
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
/**
|
|
@@ -66,7 +65,6 @@ export interface HTTPBaseHandlerOptions<TRouter extends AnyRouter, TRequest>
|
|
|
66
65
|
/** @internal */
|
|
67
66
|
export type ProcedureCall = {
|
|
68
67
|
type: ProcedureType;
|
|
69
|
-
input?: unknown;
|
|
70
68
|
path: string;
|
|
71
69
|
};
|
|
72
70
|
|
|
@@ -76,3 +76,34 @@ export type AnyRootTypes = CreateRootTypes<{
|
|
|
76
76
|
errorShape: any;
|
|
77
77
|
transformer: any;
|
|
78
78
|
}>;
|
|
79
|
+
|
|
80
|
+
type PartialIf<TCondition extends boolean, TType> = TCondition extends true
|
|
81
|
+
? Partial<TType>
|
|
82
|
+
: TType;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Adds a `createContext` option with a given callback function
|
|
86
|
+
* If context is the default value, then the `createContext` option is optional
|
|
87
|
+
*/
|
|
88
|
+
export type CreateContextCallback<
|
|
89
|
+
TContext,
|
|
90
|
+
TFunction extends (...args: any[]) => any,
|
|
91
|
+
> = PartialIf<
|
|
92
|
+
object extends TContext ? true : false,
|
|
93
|
+
{
|
|
94
|
+
/**
|
|
95
|
+
* @link https://trpc.io/docs/v11/context
|
|
96
|
+
**/
|
|
97
|
+
createContext: TFunction;
|
|
98
|
+
}
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
export type WrapCreateContext<TFunction extends (...args: any[]) => any> = {
|
|
105
|
+
/**
|
|
106
|
+
* @link https://trpc.io/docs/v11/context
|
|
107
|
+
**/
|
|
108
|
+
createContext?: TFunction;
|
|
109
|
+
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { UploadHandler } from './uploadHandler';
|
|
3
|
-
export declare function readableStreamToString(stream: ReadableStream<Uint8Array>, encoding?: BufferEncoding): Promise<string>;
|
|
4
|
-
export type FileUploadHandlerFilterArgs = {
|
|
5
|
-
filename: string;
|
|
6
|
-
contentType: string;
|
|
7
|
-
name: string;
|
|
8
|
-
};
|
|
9
|
-
export type FileUploadHandlerPathResolverArgs = {
|
|
10
|
-
filename: string;
|
|
11
|
-
contentType: string;
|
|
12
|
-
name: string;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Chooses the path of the file to be uploaded. If a string is not
|
|
16
|
-
* returned the file will not be written.
|
|
17
|
-
*/
|
|
18
|
-
export type FileUploadHandlerPathResolver = (args: FileUploadHandlerPathResolverArgs) => string | undefined;
|
|
19
|
-
export type FileUploadHandlerOptions = {
|
|
20
|
-
/**
|
|
21
|
-
* Avoid file conflicts by appending a count on the end of the filename
|
|
22
|
-
* if it already exists on disk. Defaults to `true`.
|
|
23
|
-
*/
|
|
24
|
-
avoidFileConflicts?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* The directory to write the upload.
|
|
27
|
-
*/
|
|
28
|
-
directory?: FileUploadHandlerPathResolver | string;
|
|
29
|
-
/**
|
|
30
|
-
* The name of the file in the directory. Can be a relative path, the directory
|
|
31
|
-
* structure will be created if it does not exist.
|
|
32
|
-
*/
|
|
33
|
-
file?: FileUploadHandlerPathResolver;
|
|
34
|
-
/**
|
|
35
|
-
* The maximum upload size allowed. If the size is exceeded an error will be thrown.
|
|
36
|
-
* Defaults to 3000000B (3MB).
|
|
37
|
-
*/
|
|
38
|
-
maxPartSize?: number;
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
* @param filename
|
|
42
|
-
* @param contentType
|
|
43
|
-
* @param name
|
|
44
|
-
*/
|
|
45
|
-
filter?(args: FileUploadHandlerFilterArgs): Promise<boolean> | boolean;
|
|
46
|
-
};
|
|
47
|
-
export declare function createFileUploadHandler({ directory, avoidFileConflicts, file, filter, maxPartSize, }?: FileUploadHandlerOptions): UploadHandler;
|
|
48
|
-
type NodeJSReadableStreamEsque = {
|
|
49
|
-
on(eventName: string | symbol, listener: (...args: any[]) => void): NodeJSReadableStreamEsque;
|
|
50
|
-
};
|
|
51
|
-
export declare class NodeOnDiskFile {
|
|
52
|
-
private filepath;
|
|
53
|
-
type: string;
|
|
54
|
-
private slicer?;
|
|
55
|
-
name: string;
|
|
56
|
-
lastModified: number;
|
|
57
|
-
webkitRelativePath: string;
|
|
58
|
-
constructor(filepath: string, type: string, slicer?: {
|
|
59
|
-
start: number;
|
|
60
|
-
end: number;
|
|
61
|
-
} | undefined);
|
|
62
|
-
get size(): number;
|
|
63
|
-
slice(start?: number, end?: number, type?: string): NodeOnDiskFile;
|
|
64
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
65
|
-
stream(): ReadableStream<any>;
|
|
66
|
-
stream(): NodeJSReadableStreamEsque;
|
|
67
|
-
text(): Promise<string>;
|
|
68
|
-
readonly [Symbol.toStringTag] = "File";
|
|
69
|
-
remove(): Promise<void>;
|
|
70
|
-
getFilePath(): string;
|
|
71
|
-
}
|
|
72
|
-
export {};
|
|
73
|
-
//# sourceMappingURL=fileUploadHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploadHandler.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/fileUploadHandler.ts"],"names":[],"mappings":";AAoBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,QAAQ,CAAC,EAAE,cAAc,mBAoB1B;AAED,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,6BAA6B,GAAG,CAC1C,IAAI,EAAE,iCAAiC,KACpC,MAAM,GAAG,SAAS,CAAC;AAExB,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;OAEG;IACH,SAAS,CAAC,EAAE,6BAA6B,GAAG,MAAM,CAAC;IACnD;;;OAGG;IACH,IAAI,CAAC,EAAE,6BAA6B,CAAC;IACrC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,MAAM,CAAC,CAAC,IAAI,EAAE,2BAA2B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CACxE,CAAC;AA4BF,wBAAgB,uBAAuB,CAAC,EACtC,SAAoB,EACpB,kBAAyB,EACzB,IAA8B,EAC9B,MAAM,EACN,WAAqB,GACtB,GAAE,wBAA6B,GAAG,aAAa,CAyD/C;AACD,KAAK,yBAAyB,GAAG;IAC/B,EAAE,CACA,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,yBAAyB,CAAC;CAC9B,CAAC;AAEF,qBAAa,cAAc;IAMvB,OAAO,CAAC,QAAQ;IACT,IAAI,EAAE,MAAM;IACnB,OAAO,CAAC,MAAM,CAAC;IAPjB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,SAAK;IACjB,kBAAkB,SAAM;gBAGd,QAAQ,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACX,MAAM,CAAC;eAAW,MAAM;aAAO,MAAM;iBAAE;IAKjD,IAAI,IAAI,IAAI,MAAM,CASjB;IAED,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,cAAc;IAkB5D,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAkBzC,MAAM,IAAI,cAAc,CAAC,GAAG,CAAC;IAC7B,MAAM,IAAI,yBAAyB;IAU7B,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAI7B,SAAgB,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU;IAE9C,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAGvB,WAAW,IAAI,MAAM;CAGtB"}
|