@trpc/server 10.11.1 → 10.13.0
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/TRPCError-ef1ceb74.js +55 -0
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/index.mjs +2 -2
- package/dist/adapters/express.js +3 -3
- package/dist/adapters/express.mjs +3 -3
- package/dist/adapters/fastify/index.js +3 -3
- package/dist/adapters/fastify/index.mjs +3 -3
- package/dist/adapters/fetch/index.js +3 -3
- package/dist/adapters/fetch/index.mjs +3 -3
- package/dist/adapters/next.js +3 -3
- package/dist/adapters/next.mjs +3 -3
- package/dist/adapters/node-http/index.js +3 -3
- package/dist/adapters/node-http/index.mjs +3 -3
- package/dist/adapters/standalone.js +3 -3
- package/dist/adapters/standalone.mjs +3 -3
- package/dist/adapters/ws.js +10 -10
- package/dist/adapters/ws.mjs +10 -10
- package/dist/codes-ec2bad91.js +45 -0
- package/dist/{config-d356ff77.js → config-cc5b2d45.js} +1 -1
- package/dist/{config-4ca0221b.mjs → config-cd794c2a.mjs} +1 -1
- package/dist/config-e8f70019.js +225 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/internals/config.d.ts +4 -1
- package/dist/core/internals/config.d.ts.map +1 -1
- package/dist/core/internals/procedureBuilder.d.ts +3 -6
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/error/TRPCError.d.ts +1 -1
- package/dist/http/index.js +2 -2
- package/dist/http/index.mjs +2 -2
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/index-f38f12ba.js +47 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +2 -2
- package/dist/internals.d.ts +2 -3
- package/dist/internals.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-34404221.mjs → nodeHTTPRequestHandler-85d2b636.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-64cf5f55.js → nodeHTTPRequestHandler-a649e036.js} +1 -1
- package/dist/nodeHTTPRequestHandler-fa0b13d4.js +76 -0
- package/dist/observable-a1c2103f.js +83 -0
- package/dist/{resolveHTTPResponse-3408bbf7.js → resolveHTTPResponse-6cffb0ce.js} +4 -3
- package/dist/{resolveHTTPResponse-b831e644.mjs → resolveHTTPResponse-7a4f75b3.mjs} +4 -3
- package/dist/resolveHTTPResponse-fed650e4.js +205 -0
- package/dist/subscription.js +3 -3
- package/dist/subscription.mjs +3 -3
- package/dist/transformTRPCResponse-896669e0.js +28 -0
- package/dist/transformer.d.ts +35 -8
- package/dist/transformer.d.ts.map +1 -1
- package/package.json +10 -14
- package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
- package/src/adapters/ws.ts +9 -9
- package/src/core/index.ts +1 -0
- package/src/core/internals/config.ts +7 -2
- package/src/core/internals/procedureBuilder.ts +9 -20
- package/src/http/resolveHTTPResponse.ts +3 -2
- package/src/internals.ts +2 -3
- package/src/observable/observable.test.ts +15 -15
- package/src/observable/operators/map.test.ts +2 -2
- package/src/observable/operators/share.test.ts +9 -9
- package/src/subscription.ts +3 -3
- package/src/transformer.ts +36 -8
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +0 -1
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +0 -190
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +0 -7
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +0 -190
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +0 -186
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.d.ts +0 -1
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +0 -109
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.js +0 -7
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +0 -109
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +0 -104
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +0 -202
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +0 -1
- package/src/core/internals/__generated__/mergeRoutersGeneric.ts +0 -306
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { e as callProcedure, f as getHTTPStatusCode } from './config-
|
|
1
|
+
import { e as callProcedure, f as getHTTPStatusCode } from './config-cd794c2a.mjs';
|
|
2
2
|
import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-7de0a793.mjs';
|
|
3
3
|
import { t as transformTRPCResponse } from './transformTRPCResponse-7a73a2df.mjs';
|
|
4
4
|
|
|
@@ -75,7 +75,7 @@ async function resolveHTTPResponse(opts) {
|
|
|
75
75
|
if (isBatchCall && !batchingEnabled) {
|
|
76
76
|
throw new Error(`Batching is not enabled on the server`);
|
|
77
77
|
}
|
|
78
|
-
/* istanbul ignore if
|
|
78
|
+
/* istanbul ignore if -- @preserve */ if (type === 'subscription') {
|
|
79
79
|
throw new TRPCError({
|
|
80
80
|
message: 'Subscriptions should use wsLink',
|
|
81
81
|
code: 'METHOD_NOT_SUPPORTED'
|
|
@@ -101,7 +101,7 @@ async function resolveHTTPResponse(opts) {
|
|
|
101
101
|
0: deserializeInputValue(rawInput)
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
-
/* istanbul ignore if
|
|
104
|
+
/* istanbul ignore if -- @preserve */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
|
|
105
105
|
throw new TRPCError({
|
|
106
106
|
code: 'BAD_REQUEST',
|
|
107
107
|
message: '"input" needs to be an object when doing a batch call'
|
|
@@ -180,6 +180,7 @@ async function resolveHTTPResponse(opts) {
|
|
|
180
180
|
// - `createContext()` throws
|
|
181
181
|
// - post body is too large
|
|
182
182
|
// - input deserialization fails
|
|
183
|
+
// - `errorFormatter` return value is malformed
|
|
183
184
|
const error = getTRPCErrorFromUnknown(cause);
|
|
184
185
|
onError?.({
|
|
185
186
|
error,
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { e as callProcedure, f as getHTTPStatusCode } from './config-e8f70019.js';
|
|
2
|
+
import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-ef1ceb74.js';
|
|
3
|
+
import { t as transformTRPCResponse } from './transformTRPCResponse-896669e0.js';
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
6
|
+
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
7
|
+
GET: 'query',
|
|
8
|
+
POST: 'mutation',
|
|
9
|
+
};
|
|
10
|
+
function getRawProcedureInputOrThrow(req) {
|
|
11
|
+
try {
|
|
12
|
+
if (req.method === 'GET') {
|
|
13
|
+
if (!req.query.has('input')) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const raw = req.query.get('input');
|
|
17
|
+
return JSON.parse(raw);
|
|
18
|
+
}
|
|
19
|
+
if (typeof req.body === 'string') {
|
|
20
|
+
// A mutation with no inputs will have req.body === ''
|
|
21
|
+
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
22
|
+
}
|
|
23
|
+
return req.body;
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
throw new TRPCError({
|
|
27
|
+
code: 'PARSE_ERROR',
|
|
28
|
+
cause: getCauseFromUnknown(err),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function resolveHTTPResponse(opts) {
|
|
33
|
+
const { createContext, onError, router, req } = opts;
|
|
34
|
+
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
35
|
+
if (req.method === 'HEAD') {
|
|
36
|
+
// can be used for lambda warmup
|
|
37
|
+
return {
|
|
38
|
+
status: 204,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
42
|
+
let ctx = undefined;
|
|
43
|
+
let paths = undefined;
|
|
44
|
+
const isBatchCall = !!req.query.get('batch');
|
|
45
|
+
function endResponse(untransformedJSON, errors) {
|
|
46
|
+
let status = getHTTPStatusCode(untransformedJSON);
|
|
47
|
+
const headers = {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
};
|
|
50
|
+
const meta = opts.responseMeta?.({
|
|
51
|
+
ctx,
|
|
52
|
+
paths,
|
|
53
|
+
type,
|
|
54
|
+
data: Array.isArray(untransformedJSON)
|
|
55
|
+
? untransformedJSON
|
|
56
|
+
: [untransformedJSON],
|
|
57
|
+
errors,
|
|
58
|
+
}) ?? {};
|
|
59
|
+
for (const [key, value] of Object.entries(meta.headers ?? {})) {
|
|
60
|
+
headers[key] = value;
|
|
61
|
+
}
|
|
62
|
+
if (meta.status) {
|
|
63
|
+
status = meta.status;
|
|
64
|
+
}
|
|
65
|
+
const transformedJSON = transformTRPCResponse(router, untransformedJSON);
|
|
66
|
+
const body = JSON.stringify(transformedJSON);
|
|
67
|
+
return {
|
|
68
|
+
body,
|
|
69
|
+
status,
|
|
70
|
+
headers,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
if (opts.error) {
|
|
75
|
+
throw opts.error;
|
|
76
|
+
}
|
|
77
|
+
if (isBatchCall && !batchingEnabled) {
|
|
78
|
+
throw new Error(`Batching is not enabled on the server`);
|
|
79
|
+
}
|
|
80
|
+
/* istanbul ignore if -- @preserve */
|
|
81
|
+
if (type === 'subscription') {
|
|
82
|
+
throw new TRPCError({
|
|
83
|
+
message: 'Subscriptions should use wsLink',
|
|
84
|
+
code: 'METHOD_NOT_SUPPORTED',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (type === 'unknown') {
|
|
88
|
+
throw new TRPCError({
|
|
89
|
+
message: `Unexpected request method ${req.method}`,
|
|
90
|
+
code: 'METHOD_NOT_SUPPORTED',
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const rawInput = getRawProcedureInputOrThrow(req);
|
|
94
|
+
paths = isBatchCall ? opts.path.split(',') : [opts.path];
|
|
95
|
+
ctx = await createContext();
|
|
96
|
+
const deserializeInputValue = (rawValue) => {
|
|
97
|
+
return typeof rawValue !== 'undefined'
|
|
98
|
+
? router._def._config.transformer.input.deserialize(rawValue)
|
|
99
|
+
: rawValue;
|
|
100
|
+
};
|
|
101
|
+
const getInputs = () => {
|
|
102
|
+
if (!isBatchCall) {
|
|
103
|
+
return {
|
|
104
|
+
0: deserializeInputValue(rawInput),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/* istanbul ignore if -- @preserve */
|
|
108
|
+
if (rawInput == null ||
|
|
109
|
+
typeof rawInput !== 'object' ||
|
|
110
|
+
Array.isArray(rawInput)) {
|
|
111
|
+
throw new TRPCError({
|
|
112
|
+
code: 'BAD_REQUEST',
|
|
113
|
+
message: '"input" needs to be an object when doing a batch call',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const input = {};
|
|
117
|
+
for (const key in rawInput) {
|
|
118
|
+
const k = key;
|
|
119
|
+
const rawValue = rawInput[k];
|
|
120
|
+
const value = deserializeInputValue(rawValue);
|
|
121
|
+
input[k] = value;
|
|
122
|
+
}
|
|
123
|
+
return input;
|
|
124
|
+
};
|
|
125
|
+
const inputs = getInputs();
|
|
126
|
+
const rawResults = await Promise.all(paths.map(async (path, index) => {
|
|
127
|
+
const input = inputs[index];
|
|
128
|
+
try {
|
|
129
|
+
const output = await callProcedure({
|
|
130
|
+
procedures: router._def.procedures,
|
|
131
|
+
path,
|
|
132
|
+
rawInput: input,
|
|
133
|
+
ctx,
|
|
134
|
+
type,
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
input,
|
|
138
|
+
path,
|
|
139
|
+
data: output,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
catch (cause) {
|
|
143
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
144
|
+
onError?.({ error, path, input, ctx, type: type, req });
|
|
145
|
+
return {
|
|
146
|
+
input,
|
|
147
|
+
path,
|
|
148
|
+
error,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}));
|
|
152
|
+
const errors = rawResults.flatMap((obj) => (obj.error ? [obj.error] : []));
|
|
153
|
+
const resultEnvelopes = rawResults.map((obj) => {
|
|
154
|
+
const { path, input } = obj;
|
|
155
|
+
if (obj.error) {
|
|
156
|
+
return {
|
|
157
|
+
error: router.getErrorShape({
|
|
158
|
+
error: obj.error,
|
|
159
|
+
type,
|
|
160
|
+
path,
|
|
161
|
+
input,
|
|
162
|
+
ctx,
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
return {
|
|
168
|
+
result: {
|
|
169
|
+
data: obj.data,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
|
|
175
|
+
return endResponse(result, errors);
|
|
176
|
+
}
|
|
177
|
+
catch (cause) {
|
|
178
|
+
// we get here if
|
|
179
|
+
// - batching is called when it's not enabled
|
|
180
|
+
// - `createContext()` throws
|
|
181
|
+
// - post body is too large
|
|
182
|
+
// - input deserialization fails
|
|
183
|
+
// - `errorFormatter` return value is malformed
|
|
184
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
185
|
+
onError?.({
|
|
186
|
+
error,
|
|
187
|
+
path: undefined,
|
|
188
|
+
input: undefined,
|
|
189
|
+
ctx,
|
|
190
|
+
type: type,
|
|
191
|
+
req,
|
|
192
|
+
});
|
|
193
|
+
return endResponse({
|
|
194
|
+
error: router.getErrorShape({
|
|
195
|
+
error,
|
|
196
|
+
type,
|
|
197
|
+
path: undefined,
|
|
198
|
+
input: undefined,
|
|
199
|
+
ctx,
|
|
200
|
+
}),
|
|
201
|
+
}, [error]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export { resolveHTTPResponse as r };
|
package/dist/subscription.js
CHANGED
|
@@ -12,15 +12,15 @@ var observable = require('./observable-464116ac.js');
|
|
|
12
12
|
let timer;
|
|
13
13
|
let stopped = false;
|
|
14
14
|
async function _pull(emit) {
|
|
15
|
-
/* istanbul ignore next */ if (stopped) {
|
|
15
|
+
/* istanbul ignore next -- @preserve */ if (stopped) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
18
|
try {
|
|
19
19
|
await opts.pull(emit);
|
|
20
|
-
} catch (err /* istanbul ignore next */ ) {
|
|
20
|
+
} catch (err /* istanbul ignore next -- @preserve */ ) {
|
|
21
21
|
emit.error(TRPCError.getTRPCErrorFromUnknown(err));
|
|
22
22
|
}
|
|
23
|
-
/* istanbul ignore else */ if (!stopped) {
|
|
23
|
+
/* istanbul ignore else -- @preserve */ if (!stopped) {
|
|
24
24
|
timer = setTimeout(()=>_pull(emit), opts.intervalMs);
|
|
25
25
|
}
|
|
26
26
|
}
|
package/dist/subscription.mjs
CHANGED
|
@@ -8,15 +8,15 @@ import { o as observable } from './observable-ade1bad8.mjs';
|
|
|
8
8
|
let timer;
|
|
9
9
|
let stopped = false;
|
|
10
10
|
async function _pull(emit) {
|
|
11
|
-
/* istanbul ignore next */ if (stopped) {
|
|
11
|
+
/* istanbul ignore next -- @preserve */ if (stopped) {
|
|
12
12
|
return;
|
|
13
13
|
}
|
|
14
14
|
try {
|
|
15
15
|
await opts.pull(emit);
|
|
16
|
-
} catch (err /* istanbul ignore next */ ) {
|
|
16
|
+
} catch (err /* istanbul ignore next -- @preserve */ ) {
|
|
17
17
|
emit.error(getTRPCErrorFromUnknown(err));
|
|
18
18
|
}
|
|
19
|
-
/* istanbul ignore else */ if (!stopped) {
|
|
19
|
+
/* istanbul ignore else -- @preserve */ if (!stopped) {
|
|
20
20
|
timer = setTimeout(()=>_pull(emit), opts.intervalMs);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function transformTRPCResponseItem(router, item) {
|
|
2
|
+
if ('error' in item) {
|
|
3
|
+
return {
|
|
4
|
+
...item,
|
|
5
|
+
error: router._def._config.transformer.output.serialize(item.error),
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
if ('data' in item.result) {
|
|
9
|
+
return {
|
|
10
|
+
...item,
|
|
11
|
+
result: {
|
|
12
|
+
...item.result,
|
|
13
|
+
data: router._def._config.transformer.output.serialize(item.result.data),
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return item;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
|
|
21
|
+
**/
|
|
22
|
+
function transformTRPCResponse(router, itemOrItems) {
|
|
23
|
+
return Array.isArray(itemOrItems)
|
|
24
|
+
? itemOrItems.map((item) => transformTRPCResponseItem(router, item))
|
|
25
|
+
: transformTRPCResponseItem(router, itemOrItems);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { transformTRPCResponse as t };
|
package/dist/transformer.d.ts
CHANGED
|
@@ -1,23 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @public
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export interface DataTransformer {
|
|
5
5
|
serialize(object: any): any;
|
|
6
6
|
deserialize(object: any): any;
|
|
7
|
-
}
|
|
7
|
+
}
|
|
8
|
+
interface InputDataTransformer extends DataTransformer {
|
|
9
|
+
/**
|
|
10
|
+
* This function runs **on the client** before sending the data to the server.
|
|
11
|
+
*/
|
|
12
|
+
serialize(object: any): any;
|
|
13
|
+
/**
|
|
14
|
+
* This function runs **on the server** to transform the data before it is passed to the resolver
|
|
15
|
+
*/
|
|
16
|
+
deserialize(object: any): any;
|
|
17
|
+
}
|
|
18
|
+
interface OutputDataTransformer extends DataTransformer {
|
|
19
|
+
/**
|
|
20
|
+
* This function runs **on the server** before sending the data to the client.
|
|
21
|
+
*/
|
|
22
|
+
serialize(object: any): any;
|
|
23
|
+
/**
|
|
24
|
+
* This function runs **only on the client** to transform the data sent from the server.
|
|
25
|
+
*/
|
|
26
|
+
deserialize(object: any): any;
|
|
27
|
+
}
|
|
8
28
|
/**
|
|
9
29
|
* @public
|
|
10
30
|
*/
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
31
|
+
export interface CombinedDataTransformer {
|
|
32
|
+
/**
|
|
33
|
+
* Specify how the data sent from the client to the server should be transformed.
|
|
34
|
+
*/
|
|
35
|
+
input: InputDataTransformer;
|
|
36
|
+
/**
|
|
37
|
+
* Specify how the data sent from the server to the client should be transformed.
|
|
38
|
+
*/
|
|
39
|
+
output: OutputDataTransformer;
|
|
40
|
+
}
|
|
15
41
|
/**
|
|
16
42
|
* @public
|
|
17
43
|
*/
|
|
18
44
|
export declare type CombinedDataTransformerClient = {
|
|
19
|
-
input: Pick<
|
|
20
|
-
output: Pick<
|
|
45
|
+
input: Pick<CombinedDataTransformer['input'], 'serialize'>;
|
|
46
|
+
output: Pick<CombinedDataTransformer['output'], 'deserialize'>;
|
|
21
47
|
};
|
|
22
48
|
/**
|
|
23
49
|
* @public
|
|
@@ -41,4 +67,5 @@ export declare type DefaultDataTransformer = CombinedDataTransformer & {
|
|
|
41
67
|
* @internal
|
|
42
68
|
*/
|
|
43
69
|
export declare const defaultTransformer: DefaultDataTransformer;
|
|
70
|
+
export {};
|
|
44
71
|
//# sourceMappingURL=transformer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAC5B,WAAW,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;CAC/B;AAED,UAAU,oBAAqB,SAAQ,eAAe;IACpD;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAC5B;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;CAC/B;AAED,UAAU,qBAAsB,SAAQ,eAAe;IACrD;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAC5B;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,KAAK,EAAE,oBAAoB,CAAC;IAC5B;;OAEG;IACH,MAAM,EAAE,qBAAqB,CAAC;CAC/B;AAED;;GAEG;AACH,oBAAY,6BAA6B,GAAG;IAC1C,KAAK,EAAE,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC;IAC3D,MAAM,EAAE,IAAI,CAAC,uBAAuB,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAC,CAAC;CAChE,CAAC;AAEF;;GAEG;AACH,oBAAY,sBAAsB,GAAG,eAAe,GAAG,uBAAuB,CAAC;AAE/E;;GAEG;AACH,oBAAY,4BAA4B,GACpC,eAAe,GACf,6BAA6B,CAAC;AAElC;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,sBAAsB,GAClC,uBAAuB,CAKzB;AAED;;GAEG;AACH,oBAAY,sBAAsB,GAAG,uBAAuB,GAAG;IAC7D,QAAQ,EAAE,IAAI,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,sBAIhC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/server",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.13.0",
|
|
4
4
|
"description": "tRPC Server",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -126,39 +126,35 @@
|
|
|
126
126
|
"access": "public"
|
|
127
127
|
},
|
|
128
128
|
"devDependencies": {
|
|
129
|
-
"@
|
|
130
|
-
"@fastify/websocket": "^5.0.0",
|
|
131
|
-
"@miniflare/core": "^2.9.0",
|
|
129
|
+
"@fastify/websocket": "^7.1.2",
|
|
132
130
|
"@tanstack/react-query": "^4.3.8",
|
|
133
131
|
"@types/aws-lambda": "^8.10.97",
|
|
134
132
|
"@types/express": "^4.17.12",
|
|
135
133
|
"@types/hash-sum": "^1.0.0",
|
|
136
134
|
"@types/node": "^18.7.20",
|
|
137
|
-
"@types/node-fetch": "^2.5.11",
|
|
138
135
|
"@types/react": "^18.0.9",
|
|
139
136
|
"@types/ws": "^8.2.0",
|
|
140
137
|
"aws-lambda": "^1.0.7",
|
|
141
|
-
"devalue": "^
|
|
138
|
+
"devalue": "^4.0.0",
|
|
142
139
|
"eslint": "^8.30.0",
|
|
143
140
|
"expect-type": "^0.15.0",
|
|
144
141
|
"express": "^4.17.1",
|
|
145
|
-
"fastify": "^
|
|
146
|
-
"fastify-plugin": "^
|
|
142
|
+
"fastify": "^4.13.0",
|
|
143
|
+
"fastify-plugin": "^4.5.0",
|
|
147
144
|
"hash-sum": "^2.0.0",
|
|
148
|
-
"miniflare": "^2.4.0",
|
|
149
145
|
"myzod": "^1.3.1",
|
|
150
|
-
"next": "^13.
|
|
151
|
-
"node-fetch": "^2.6.1",
|
|
146
|
+
"next": "^13.2.1",
|
|
152
147
|
"react": "^18.2.0",
|
|
153
148
|
"react-dom": "^18.2.0",
|
|
154
149
|
"rollup": "^2.79.1",
|
|
155
150
|
"superjson": "^1.7.4",
|
|
156
|
-
"superstruct": "^0.
|
|
151
|
+
"superstruct": "^1.0.0",
|
|
157
152
|
"tsx": "^3.9.0",
|
|
158
153
|
"typescript": "^4.8.3",
|
|
154
|
+
"vitest": "^0.28.5",
|
|
159
155
|
"ws": "^8.0.0",
|
|
160
|
-
"yup": "^0.
|
|
156
|
+
"yup": "^1.0.0",
|
|
161
157
|
"zod": "^3.0.0"
|
|
162
158
|
},
|
|
163
|
-
"gitHead": "
|
|
159
|
+
"gitHead": "1214c2d1d239c6864a32c7df24651899551be8f8"
|
|
164
160
|
}
|
|
@@ -64,7 +64,7 @@ export async function fastifyRequestHandler<
|
|
|
64
64
|
res.statusCode = result.status;
|
|
65
65
|
}
|
|
66
66
|
for (const [key, value] of Object.entries(result.headers ?? {})) {
|
|
67
|
-
/* istanbul ignore if
|
|
67
|
+
/* istanbul ignore if -- @preserve */
|
|
68
68
|
if (typeof value === 'undefined') {
|
|
69
69
|
continue;
|
|
70
70
|
}
|
|
@@ -39,7 +39,7 @@ export async function fetchRequestHandler<TRouter extends AnyRouter>(
|
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
for (const [key, value] of Object.entries(result.headers ?? {})) {
|
|
42
|
-
/* istanbul ignore if
|
|
42
|
+
/* istanbul ignore if -- @preserve */
|
|
43
43
|
if (typeof value === 'undefined') {
|
|
44
44
|
continue;
|
|
45
45
|
}
|
package/src/adapters/ws.ts
CHANGED
|
@@ -23,19 +23,19 @@ import {
|
|
|
23
23
|
NodeHTTPCreateContextOption,
|
|
24
24
|
} from './node-http';
|
|
25
25
|
|
|
26
|
-
/* istanbul ignore next */
|
|
26
|
+
/* istanbul ignore next -- @preserve */
|
|
27
27
|
function assertIsObject(obj: unknown): asserts obj is Record<string, unknown> {
|
|
28
28
|
if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
|
|
29
29
|
throw new Error('Not an object');
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
/* istanbul ignore next */
|
|
32
|
+
/* istanbul ignore next -- @preserve */
|
|
33
33
|
function assertIsProcedureType(obj: unknown): asserts obj is ProcedureType {
|
|
34
34
|
if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
|
|
35
35
|
throw new Error('Invalid procedure type');
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
/* istanbul ignore next */
|
|
38
|
+
/* istanbul ignore next -- @preserve */
|
|
39
39
|
function assertIsRequestId(
|
|
40
40
|
obj: unknown,
|
|
41
41
|
): asserts obj is number | string | null {
|
|
@@ -48,13 +48,13 @@ function assertIsRequestId(
|
|
|
48
48
|
throw new Error('Invalid request id');
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
/* istanbul ignore next */
|
|
51
|
+
/* istanbul ignore next -- @preserve */
|
|
52
52
|
function assertIsString(obj: unknown): asserts obj is string {
|
|
53
53
|
if (typeof obj !== 'string') {
|
|
54
54
|
throw new Error('Invalid string');
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
/* istanbul ignore next */
|
|
57
|
+
/* istanbul ignore next -- @preserve */
|
|
58
58
|
function assertIsJSONRPC2OrUndefined(
|
|
59
59
|
obj: unknown,
|
|
60
60
|
): asserts obj is '2.0' | undefined {
|
|
@@ -145,7 +145,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
|
|
|
145
145
|
|
|
146
146
|
async function handleRequest(msg: TRPCClientOutgoingMessage) {
|
|
147
147
|
const { id, jsonrpc } = msg;
|
|
148
|
-
/* istanbul ignore next */
|
|
148
|
+
/* istanbul ignore next -- @preserve */
|
|
149
149
|
if (id === null) {
|
|
150
150
|
throw new TRPCError({
|
|
151
151
|
code: 'BAD_REQUEST',
|
|
@@ -230,7 +230,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
|
|
|
230
230
|
});
|
|
231
231
|
},
|
|
232
232
|
});
|
|
233
|
-
/* istanbul ignore next */
|
|
233
|
+
/* istanbul ignore next -- @preserve */
|
|
234
234
|
if (client.readyState !== client.OPEN) {
|
|
235
235
|
// if the client got disconnected whilst initializing the subscription
|
|
236
236
|
// no need to send stopped message if the client is disconnected
|
|
@@ -238,7 +238,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
|
|
|
238
238
|
return;
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
/* istanbul ignore next */
|
|
241
|
+
/* istanbul ignore next -- @preserve */
|
|
242
242
|
if (clientSubscriptions.has(id)) {
|
|
243
243
|
// duplicate request ids for client
|
|
244
244
|
stopSubscription(sub, { id, jsonrpc });
|
|
@@ -256,7 +256,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
|
|
|
256
256
|
type: 'started',
|
|
257
257
|
},
|
|
258
258
|
});
|
|
259
|
-
} catch (cause) /* istanbul ignore next */ {
|
|
259
|
+
} catch (cause) /* istanbul ignore next -- @preserve */ {
|
|
260
260
|
// procedure threw an error
|
|
261
261
|
const error = getTRPCErrorFromUnknown(cause);
|
|
262
262
|
opts.onError?.({ error, path, type, ctx, req, input });
|
package/src/core/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ErrorFormatter } from '../../error/formatter';
|
|
2
|
+
import type { TRPCErrorShape } from '../../rpc';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* The initial generics that are used in the init function
|
|
@@ -18,7 +19,8 @@ export const isServerDefault: boolean =
|
|
|
18
19
|
typeof window === 'undefined' ||
|
|
19
20
|
'Deno' in window ||
|
|
20
21
|
globalThis.process?.env?.NODE_ENV === 'test' ||
|
|
21
|
-
!!globalThis.process?.env?.JEST_WORKER_ID
|
|
22
|
+
!!globalThis.process?.env?.JEST_WORKER_ID ||
|
|
23
|
+
!!globalThis.process?.env?.VITEST_WORKER_ID;
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* The runtime config that are used and actually represents real values underneath
|
|
@@ -34,7 +36,10 @@ export interface RuntimeConfig<TTypes extends RootConfigTypes> {
|
|
|
34
36
|
* Use custom error formatting
|
|
35
37
|
* @link https://trpc.io/docs/error-formatting
|
|
36
38
|
*/
|
|
37
|
-
errorFormatter: ErrorFormatter<
|
|
39
|
+
errorFormatter: ErrorFormatter<
|
|
40
|
+
TTypes['ctx'],
|
|
41
|
+
TRPCErrorShape<number> & { [key: string]: any }
|
|
42
|
+
>;
|
|
38
43
|
/**
|
|
39
44
|
* Allow `@trpc/server` to run in non-server environments
|
|
40
45
|
* @warning **Use with caution**, this should likely mainly be used within testing.
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { TRPCError } from '../../error/TRPCError';
|
|
2
2
|
import { getTRPCErrorFromUnknown } from '../../error/utils';
|
|
3
|
-
import {
|
|
4
|
-
InferOptional,
|
|
5
|
-
MaybePromise,
|
|
6
|
-
Simplify,
|
|
7
|
-
UndefinedKeys,
|
|
8
|
-
} from '../../types';
|
|
3
|
+
import { MaybePromise, Simplify } from '../../types';
|
|
9
4
|
import {
|
|
10
5
|
MiddlewareBuilder,
|
|
11
6
|
MiddlewareFunction,
|
|
@@ -68,19 +63,9 @@ export interface BuildProcedure<
|
|
|
68
63
|
: TParams
|
|
69
64
|
> {}
|
|
70
65
|
|
|
71
|
-
type Merge<TType, TWith> = {
|
|
72
|
-
[TKey in keyof TType | keyof TWith]: TKey extends keyof TType
|
|
73
|
-
? TKey extends keyof TWith
|
|
74
|
-
? TType[TKey] & TWith[TKey]
|
|
75
|
-
: TType[TKey]
|
|
76
|
-
: TWith[TKey & keyof TWith];
|
|
77
|
-
};
|
|
78
|
-
|
|
79
66
|
type OverwriteIfDefined<TType, TWith> = UnsetMarker extends TType
|
|
80
67
|
? TWith
|
|
81
|
-
: Simplify<
|
|
82
|
-
InferOptional<Merge<TType, TWith>, UndefinedKeys<Merge<TType, TWith>>>
|
|
83
|
-
>;
|
|
68
|
+
: Simplify<TType & TWith>;
|
|
84
69
|
|
|
85
70
|
type ErrorMessage<TMessage extends string> = TMessage;
|
|
86
71
|
|
|
@@ -104,9 +89,13 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
|
|
|
104
89
|
input<$Parser extends Parser>(
|
|
105
90
|
schema: TParams['_input_out'] extends UnsetMarker
|
|
106
91
|
? $Parser
|
|
107
|
-
: inferParser<$Parser>['out'] extends Record<string, unknown>
|
|
108
|
-
? TParams['_input_out'] extends Record<string, unknown>
|
|
109
|
-
?
|
|
92
|
+
: inferParser<$Parser>['out'] extends Record<string, unknown> | undefined
|
|
93
|
+
? TParams['_input_out'] extends Record<string, unknown> | undefined
|
|
94
|
+
? undefined extends inferParser<$Parser>['out'] // if current is optional the previous must be too
|
|
95
|
+
? undefined extends TParams['_input_out']
|
|
96
|
+
? $Parser
|
|
97
|
+
: ErrorMessage<'Cannot chain an optional parser to a required parser'>
|
|
98
|
+
: $Parser
|
|
110
99
|
: ErrorMessage<'All input parsers did not resolve to an object'>
|
|
111
100
|
: ErrorMessage<'All input parsers did not resolve to an object'>,
|
|
112
101
|
): ProcedureBuilder<{
|
|
@@ -124,7 +124,7 @@ export async function resolveHTTPResponse<
|
|
|
124
124
|
if (isBatchCall && !batchingEnabled) {
|
|
125
125
|
throw new Error(`Batching is not enabled on the server`);
|
|
126
126
|
}
|
|
127
|
-
/* istanbul ignore if
|
|
127
|
+
/* istanbul ignore if -- @preserve */
|
|
128
128
|
if (type === 'subscription') {
|
|
129
129
|
throw new TRPCError({
|
|
130
130
|
message: 'Subscriptions should use wsLink',
|
|
@@ -154,7 +154,7 @@ export async function resolveHTTPResponse<
|
|
|
154
154
|
};
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
/* istanbul ignore if
|
|
157
|
+
/* istanbul ignore if -- @preserve */
|
|
158
158
|
if (
|
|
159
159
|
rawInput == null ||
|
|
160
160
|
typeof rawInput !== 'object' ||
|
|
@@ -238,6 +238,7 @@ export async function resolveHTTPResponse<
|
|
|
238
238
|
// - `createContext()` throws
|
|
239
239
|
// - post body is too large
|
|
240
240
|
// - input deserialization fails
|
|
241
|
+
// - `errorFormatter` return value is malformed
|
|
241
242
|
const error = getTRPCErrorFromUnknown(cause);
|
|
242
243
|
|
|
243
244
|
onError?.({
|
package/src/internals.ts
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
* These types have to be exported so users can generate their own types definitions files
|
|
3
3
|
*/
|
|
4
4
|
export type { DefaultErrorShape } from './error/formatter';
|
|
5
|
-
export type {
|
|
5
|
+
export type { mergeRouters } from './core/internals/mergeRouters';
|
|
6
6
|
export type { RootConfig, AnyRootConfig } from './core/internals/config';
|
|
7
7
|
export type {
|
|
8
8
|
ProcedureBuilder,
|
|
9
9
|
BuildProcedure,
|
|
10
10
|
} from './core/internals/procedureBuilder';
|
|
11
11
|
export type { Overwrite, unsetMarker } from './core/internals/utils';
|
|
12
|
-
export type { MiddlewareFunction } from './core/middleware';
|
|
13
|
-
export type { Router, RouterDef } from './core/router';
|
|
12
|
+
export type { MiddlewareFunction, MiddlewareBuilder } from './core/middleware';
|