@trpc/server 10.28.2 → 10.29.1
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/adapters/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/index.mjs +1 -1
- package/dist/adapters/express.js +5 -4
- package/dist/adapters/express.mjs +5 -4
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +51 -15
- package/dist/adapters/fastify/index.mjs +51 -15
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +67 -20
- package/dist/adapters/fetch/index.mjs +67 -20
- package/dist/adapters/next.js +3 -2
- package/dist/adapters/next.mjs +3 -2
- package/dist/adapters/node-http/content-type/form-data/index.js +19 -46
- package/dist/adapters/node-http/content-type/form-data/index.mjs +19 -40
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +7 -3
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +1 -1
- package/dist/adapters/node-http/index.js +5 -4
- package/dist/adapters/node-http/index.mjs +5 -4
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +11 -1
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.js +5 -4
- package/dist/adapters/standalone.mjs +5 -4
- package/dist/batchStreamFormatter-2c1405a1.js +31 -0
- package/dist/batchStreamFormatter-93cdcdd4.js +32 -0
- package/dist/batchStreamFormatter-fc1ffb26.mjs +30 -0
- package/dist/http/batchStreamFormatter.d.ts +24 -0
- package/dist/http/batchStreamFormatter.d.ts.map +1 -0
- package/dist/http/index.d.ts +1 -0
- package/dist/http/index.d.ts.map +1 -1
- package/dist/http/index.js +3 -1
- package/dist/http/index.mjs +2 -1
- package/dist/http/internals/types.d.ts +7 -0
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +37 -2
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-5bbf93f1.js → nodeHTTPRequestHandler-071d36b5.js} +44 -13
- package/dist/{nodeHTTPRequestHandler-bd47641d.js → nodeHTTPRequestHandler-51d58a23.js} +47 -12
- package/dist/{nodeHTTPRequestHandler-0f979796.mjs → nodeHTTPRequestHandler-bb12529f.mjs} +44 -13
- package/dist/resolveHTTPResponse-1f03fdfd.js +284 -0
- package/dist/resolveHTTPResponse-dd3677b3.mjs +282 -0
- package/dist/resolveHTTPResponse-e0047ea2.js +256 -0
- package/package.json +4 -4
- package/src/adapters/fastify/fastifyRequestHandler.ts +65 -17
- package/src/adapters/fetch/fetchRequestHandler.ts +73 -25
- package/src/adapters/node-http/content-type/form-data/streamSlice.ts +19 -21
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +53 -12
- package/src/adapters/node-http/types.ts +11 -1
- package/src/http/batchStreamFormatter.ts +29 -0
- package/src/http/index.ts +1 -0
- package/src/http/internals/types.ts +8 -0
- package/src/http/resolveHTTPResponse.ts +339 -124
- package/dist/resolveHTTPResponse-4e576698.mjs +0 -174
- package/dist/resolveHTTPResponse-9523b4e3.js +0 -176
- package/dist/resolveHTTPResponse-ba557d3e.js +0 -166
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { b as callProcedure } from './config-65c5b6d1.mjs';
|
|
2
|
-
import { T as TRPCError, a as getTRPCErrorFromUnknown } from './TRPCError-2b10c8d2.mjs';
|
|
3
|
-
import { g as getErrorShape, t as transformTRPCResponse } from './transformTRPCResponse-3dca0b20.mjs';
|
|
4
|
-
import { g as getJsonContentTypeInputs } from './contentType-acc3be52.mjs';
|
|
5
|
-
import { b as getHTTPStatusCode } from './index-044a193b.mjs';
|
|
6
|
-
|
|
7
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
8
|
-
GET: 'query',
|
|
9
|
-
POST: 'mutation'
|
|
10
|
-
};
|
|
11
|
-
const fallbackContentTypeHandler = {
|
|
12
|
-
getInputs: getJsonContentTypeInputs
|
|
13
|
-
};
|
|
14
|
-
async function resolveHTTPResponse(opts) {
|
|
15
|
-
const { router , req } = opts;
|
|
16
|
-
const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
17
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
18
|
-
if (req.method === 'HEAD') {
|
|
19
|
-
// can be used for lambda warmup
|
|
20
|
-
return {
|
|
21
|
-
status: 204
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
25
|
-
let ctx = undefined;
|
|
26
|
-
let paths = undefined;
|
|
27
|
-
const isBatchCall = !!req.query.get('batch');
|
|
28
|
-
function endResponse(untransformedJSON, errors) {
|
|
29
|
-
let status = getHTTPStatusCode(untransformedJSON);
|
|
30
|
-
const headers = {
|
|
31
|
-
'Content-Type': 'application/json'
|
|
32
|
-
};
|
|
33
|
-
const meta = opts.responseMeta?.({
|
|
34
|
-
ctx,
|
|
35
|
-
paths,
|
|
36
|
-
type,
|
|
37
|
-
data: Array.isArray(untransformedJSON) ? untransformedJSON : [
|
|
38
|
-
untransformedJSON
|
|
39
|
-
],
|
|
40
|
-
errors
|
|
41
|
-
}) ?? {};
|
|
42
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})){
|
|
43
|
-
headers[key] = value;
|
|
44
|
-
}
|
|
45
|
-
if (meta.status) {
|
|
46
|
-
status = meta.status;
|
|
47
|
-
}
|
|
48
|
-
const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
|
|
49
|
-
const body = JSON.stringify(transformedJSON);
|
|
50
|
-
return {
|
|
51
|
-
body,
|
|
52
|
-
status,
|
|
53
|
-
headers
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
if (opts.error) {
|
|
58
|
-
throw opts.error;
|
|
59
|
-
}
|
|
60
|
-
if (isBatchCall && !batchingEnabled) {
|
|
61
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
62
|
-
}
|
|
63
|
-
/* istanbul ignore if -- @preserve */ if (type === 'subscription') {
|
|
64
|
-
throw new TRPCError({
|
|
65
|
-
message: 'Subscriptions should use wsLink',
|
|
66
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (type === 'unknown') {
|
|
70
|
-
throw new TRPCError({
|
|
71
|
-
message: `Unexpected request method ${req.method}`,
|
|
72
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
76
|
-
isBatchCall,
|
|
77
|
-
req,
|
|
78
|
-
router,
|
|
79
|
-
preprocessedBody: opts.preprocessedBody ?? false
|
|
80
|
-
});
|
|
81
|
-
paths = isBatchCall ? opts.path.split(',') : [
|
|
82
|
-
opts.path
|
|
83
|
-
];
|
|
84
|
-
ctx = await opts.createContext();
|
|
85
|
-
const rawResults = await Promise.all(paths.map(async (path, index)=>{
|
|
86
|
-
const input = inputs[index];
|
|
87
|
-
try {
|
|
88
|
-
const output = await callProcedure({
|
|
89
|
-
procedures: router._def.procedures,
|
|
90
|
-
path,
|
|
91
|
-
rawInput: input,
|
|
92
|
-
ctx,
|
|
93
|
-
type
|
|
94
|
-
});
|
|
95
|
-
return {
|
|
96
|
-
input,
|
|
97
|
-
path,
|
|
98
|
-
data: output
|
|
99
|
-
};
|
|
100
|
-
} catch (cause) {
|
|
101
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
102
|
-
opts.onError?.({
|
|
103
|
-
error,
|
|
104
|
-
path,
|
|
105
|
-
input,
|
|
106
|
-
ctx,
|
|
107
|
-
type: type,
|
|
108
|
-
req
|
|
109
|
-
});
|
|
110
|
-
return {
|
|
111
|
-
input,
|
|
112
|
-
path,
|
|
113
|
-
error
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}));
|
|
117
|
-
const errors = rawResults.flatMap((obj)=>obj.error ? [
|
|
118
|
-
obj.error
|
|
119
|
-
] : []);
|
|
120
|
-
const resultEnvelopes = rawResults.map((obj)=>{
|
|
121
|
-
const { path , input } = obj;
|
|
122
|
-
if (obj.error) {
|
|
123
|
-
return {
|
|
124
|
-
error: getErrorShape({
|
|
125
|
-
config: router._def._config,
|
|
126
|
-
error: obj.error,
|
|
127
|
-
type,
|
|
128
|
-
path,
|
|
129
|
-
input,
|
|
130
|
-
ctx
|
|
131
|
-
})
|
|
132
|
-
};
|
|
133
|
-
} else {
|
|
134
|
-
return {
|
|
135
|
-
result: {
|
|
136
|
-
data: obj.data
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
|
|
142
|
-
return endResponse(result, errors);
|
|
143
|
-
} catch (cause) {
|
|
144
|
-
// we get here if
|
|
145
|
-
// - batching is called when it's not enabled
|
|
146
|
-
// - `createContext()` throws
|
|
147
|
-
// - post body is too large
|
|
148
|
-
// - input deserialization fails
|
|
149
|
-
// - `errorFormatter` return value is malformed
|
|
150
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
151
|
-
opts.onError?.({
|
|
152
|
-
error,
|
|
153
|
-
path: undefined,
|
|
154
|
-
input: undefined,
|
|
155
|
-
ctx,
|
|
156
|
-
type: type,
|
|
157
|
-
req
|
|
158
|
-
});
|
|
159
|
-
return endResponse({
|
|
160
|
-
error: getErrorShape({
|
|
161
|
-
config: router._def._config,
|
|
162
|
-
error,
|
|
163
|
-
type,
|
|
164
|
-
path: undefined,
|
|
165
|
-
input: undefined,
|
|
166
|
-
ctx
|
|
167
|
-
})
|
|
168
|
-
}, [
|
|
169
|
-
error
|
|
170
|
-
]);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export { resolveHTTPResponse as r };
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var config = require('./config-f3ce0144.js');
|
|
4
|
-
var TRPCError = require('./TRPCError-16b2e52d.js');
|
|
5
|
-
var transformTRPCResponse = require('./transformTRPCResponse-48d70ef6.js');
|
|
6
|
-
var contentType = require('./contentType-8356d528.js');
|
|
7
|
-
var index = require('./index-4a823936.js');
|
|
8
|
-
|
|
9
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
10
|
-
GET: 'query',
|
|
11
|
-
POST: 'mutation'
|
|
12
|
-
};
|
|
13
|
-
const fallbackContentTypeHandler = {
|
|
14
|
-
getInputs: contentType.getJsonContentTypeInputs
|
|
15
|
-
};
|
|
16
|
-
async function resolveHTTPResponse(opts) {
|
|
17
|
-
const { router , req } = opts;
|
|
18
|
-
const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
19
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
20
|
-
if (req.method === 'HEAD') {
|
|
21
|
-
// can be used for lambda warmup
|
|
22
|
-
return {
|
|
23
|
-
status: 204
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
27
|
-
let ctx = undefined;
|
|
28
|
-
let paths = undefined;
|
|
29
|
-
const isBatchCall = !!req.query.get('batch');
|
|
30
|
-
function endResponse(untransformedJSON, errors) {
|
|
31
|
-
let status = index.getHTTPStatusCode(untransformedJSON);
|
|
32
|
-
const headers = {
|
|
33
|
-
'Content-Type': 'application/json'
|
|
34
|
-
};
|
|
35
|
-
const meta = opts.responseMeta?.({
|
|
36
|
-
ctx,
|
|
37
|
-
paths,
|
|
38
|
-
type,
|
|
39
|
-
data: Array.isArray(untransformedJSON) ? untransformedJSON : [
|
|
40
|
-
untransformedJSON
|
|
41
|
-
],
|
|
42
|
-
errors
|
|
43
|
-
}) ?? {};
|
|
44
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})){
|
|
45
|
-
headers[key] = value;
|
|
46
|
-
}
|
|
47
|
-
if (meta.status) {
|
|
48
|
-
status = meta.status;
|
|
49
|
-
}
|
|
50
|
-
const transformedJSON = transformTRPCResponse.transformTRPCResponse(router._def._config, untransformedJSON);
|
|
51
|
-
const body = JSON.stringify(transformedJSON);
|
|
52
|
-
return {
|
|
53
|
-
body,
|
|
54
|
-
status,
|
|
55
|
-
headers
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
if (opts.error) {
|
|
60
|
-
throw opts.error;
|
|
61
|
-
}
|
|
62
|
-
if (isBatchCall && !batchingEnabled) {
|
|
63
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
64
|
-
}
|
|
65
|
-
/* istanbul ignore if -- @preserve */ if (type === 'subscription') {
|
|
66
|
-
throw new TRPCError.TRPCError({
|
|
67
|
-
message: 'Subscriptions should use wsLink',
|
|
68
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
if (type === 'unknown') {
|
|
72
|
-
throw new TRPCError.TRPCError({
|
|
73
|
-
message: `Unexpected request method ${req.method}`,
|
|
74
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
78
|
-
isBatchCall,
|
|
79
|
-
req,
|
|
80
|
-
router,
|
|
81
|
-
preprocessedBody: opts.preprocessedBody ?? false
|
|
82
|
-
});
|
|
83
|
-
paths = isBatchCall ? opts.path.split(',') : [
|
|
84
|
-
opts.path
|
|
85
|
-
];
|
|
86
|
-
ctx = await opts.createContext();
|
|
87
|
-
const rawResults = await Promise.all(paths.map(async (path, index)=>{
|
|
88
|
-
const input = inputs[index];
|
|
89
|
-
try {
|
|
90
|
-
const output = await config.callProcedure({
|
|
91
|
-
procedures: router._def.procedures,
|
|
92
|
-
path,
|
|
93
|
-
rawInput: input,
|
|
94
|
-
ctx,
|
|
95
|
-
type
|
|
96
|
-
});
|
|
97
|
-
return {
|
|
98
|
-
input,
|
|
99
|
-
path,
|
|
100
|
-
data: output
|
|
101
|
-
};
|
|
102
|
-
} catch (cause) {
|
|
103
|
-
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
104
|
-
opts.onError?.({
|
|
105
|
-
error,
|
|
106
|
-
path,
|
|
107
|
-
input,
|
|
108
|
-
ctx,
|
|
109
|
-
type: type,
|
|
110
|
-
req
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
input,
|
|
114
|
-
path,
|
|
115
|
-
error
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}));
|
|
119
|
-
const errors = rawResults.flatMap((obj)=>obj.error ? [
|
|
120
|
-
obj.error
|
|
121
|
-
] : []);
|
|
122
|
-
const resultEnvelopes = rawResults.map((obj)=>{
|
|
123
|
-
const { path , input } = obj;
|
|
124
|
-
if (obj.error) {
|
|
125
|
-
return {
|
|
126
|
-
error: transformTRPCResponse.getErrorShape({
|
|
127
|
-
config: router._def._config,
|
|
128
|
-
error: obj.error,
|
|
129
|
-
type,
|
|
130
|
-
path,
|
|
131
|
-
input,
|
|
132
|
-
ctx
|
|
133
|
-
})
|
|
134
|
-
};
|
|
135
|
-
} else {
|
|
136
|
-
return {
|
|
137
|
-
result: {
|
|
138
|
-
data: obj.data
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
|
|
144
|
-
return endResponse(result, errors);
|
|
145
|
-
} catch (cause) {
|
|
146
|
-
// we get here if
|
|
147
|
-
// - batching is called when it's not enabled
|
|
148
|
-
// - `createContext()` throws
|
|
149
|
-
// - post body is too large
|
|
150
|
-
// - input deserialization fails
|
|
151
|
-
// - `errorFormatter` return value is malformed
|
|
152
|
-
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
153
|
-
opts.onError?.({
|
|
154
|
-
error,
|
|
155
|
-
path: undefined,
|
|
156
|
-
input: undefined,
|
|
157
|
-
ctx,
|
|
158
|
-
type: type,
|
|
159
|
-
req
|
|
160
|
-
});
|
|
161
|
-
return endResponse({
|
|
162
|
-
error: transformTRPCResponse.getErrorShape({
|
|
163
|
-
config: router._def._config,
|
|
164
|
-
error,
|
|
165
|
-
type,
|
|
166
|
-
path: undefined,
|
|
167
|
-
input: undefined,
|
|
168
|
-
ctx
|
|
169
|
-
})
|
|
170
|
-
}, [
|
|
171
|
-
error
|
|
172
|
-
]);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
exports.resolveHTTPResponse = resolveHTTPResponse;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { b as callProcedure } from './config-54786004.js';
|
|
2
|
-
import { T as TRPCError, a as getTRPCErrorFromUnknown } from './TRPCError-7c3909ea.js';
|
|
3
|
-
import { g as getErrorShape, t as transformTRPCResponse } from './transformTRPCResponse-7fcbe472.js';
|
|
4
|
-
import { g as getJsonContentTypeInputs } from './contentType-7354963c.js';
|
|
5
|
-
import { b as getHTTPStatusCode } from './index-ca9a128e.js';
|
|
6
|
-
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
8
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
9
|
-
GET: 'query',
|
|
10
|
-
POST: 'mutation',
|
|
11
|
-
};
|
|
12
|
-
const fallbackContentTypeHandler = {
|
|
13
|
-
getInputs: getJsonContentTypeInputs,
|
|
14
|
-
};
|
|
15
|
-
async function resolveHTTPResponse(opts) {
|
|
16
|
-
const { router, req } = opts;
|
|
17
|
-
const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
18
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
19
|
-
if (req.method === 'HEAD') {
|
|
20
|
-
// can be used for lambda warmup
|
|
21
|
-
return {
|
|
22
|
-
status: 204,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
26
|
-
let ctx = undefined;
|
|
27
|
-
let paths = undefined;
|
|
28
|
-
const isBatchCall = !!req.query.get('batch');
|
|
29
|
-
function endResponse(untransformedJSON, errors) {
|
|
30
|
-
let status = getHTTPStatusCode(untransformedJSON);
|
|
31
|
-
const headers = {
|
|
32
|
-
'Content-Type': 'application/json',
|
|
33
|
-
};
|
|
34
|
-
const meta = opts.responseMeta?.({
|
|
35
|
-
ctx,
|
|
36
|
-
paths,
|
|
37
|
-
type,
|
|
38
|
-
data: Array.isArray(untransformedJSON)
|
|
39
|
-
? untransformedJSON
|
|
40
|
-
: [untransformedJSON],
|
|
41
|
-
errors,
|
|
42
|
-
}) ?? {};
|
|
43
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})) {
|
|
44
|
-
headers[key] = value;
|
|
45
|
-
}
|
|
46
|
-
if (meta.status) {
|
|
47
|
-
status = meta.status;
|
|
48
|
-
}
|
|
49
|
-
const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
|
|
50
|
-
const body = JSON.stringify(transformedJSON);
|
|
51
|
-
return {
|
|
52
|
-
body,
|
|
53
|
-
status,
|
|
54
|
-
headers,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
if (opts.error) {
|
|
59
|
-
throw opts.error;
|
|
60
|
-
}
|
|
61
|
-
if (isBatchCall && !batchingEnabled) {
|
|
62
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
63
|
-
}
|
|
64
|
-
/* istanbul ignore if -- @preserve */
|
|
65
|
-
if (type === 'subscription') {
|
|
66
|
-
throw new TRPCError({
|
|
67
|
-
message: 'Subscriptions should use wsLink',
|
|
68
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
if (type === 'unknown') {
|
|
72
|
-
throw new TRPCError({
|
|
73
|
-
message: `Unexpected request method ${req.method}`,
|
|
74
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
78
|
-
isBatchCall,
|
|
79
|
-
req,
|
|
80
|
-
router,
|
|
81
|
-
preprocessedBody: opts.preprocessedBody ?? false,
|
|
82
|
-
});
|
|
83
|
-
paths = isBatchCall ? opts.path.split(',') : [opts.path];
|
|
84
|
-
ctx = await opts.createContext();
|
|
85
|
-
const rawResults = await Promise.all(paths.map(async (path, index) => {
|
|
86
|
-
const input = inputs[index];
|
|
87
|
-
try {
|
|
88
|
-
const output = await callProcedure({
|
|
89
|
-
procedures: router._def.procedures,
|
|
90
|
-
path,
|
|
91
|
-
rawInput: input,
|
|
92
|
-
ctx,
|
|
93
|
-
type,
|
|
94
|
-
});
|
|
95
|
-
return {
|
|
96
|
-
input,
|
|
97
|
-
path,
|
|
98
|
-
data: output,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
catch (cause) {
|
|
102
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
103
|
-
opts.onError?.({ error, path, input, ctx, type: type, req });
|
|
104
|
-
return {
|
|
105
|
-
input,
|
|
106
|
-
path,
|
|
107
|
-
error,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
}));
|
|
111
|
-
const errors = rawResults.flatMap((obj) => (obj.error ? [obj.error] : []));
|
|
112
|
-
const resultEnvelopes = rawResults.map((obj) => {
|
|
113
|
-
const { path, input } = obj;
|
|
114
|
-
if (obj.error) {
|
|
115
|
-
return {
|
|
116
|
-
error: getErrorShape({
|
|
117
|
-
config: router._def._config,
|
|
118
|
-
error: obj.error,
|
|
119
|
-
type,
|
|
120
|
-
path,
|
|
121
|
-
input,
|
|
122
|
-
ctx,
|
|
123
|
-
}),
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
return {
|
|
128
|
-
result: {
|
|
129
|
-
data: obj.data,
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
|
|
135
|
-
return endResponse(result, errors);
|
|
136
|
-
}
|
|
137
|
-
catch (cause) {
|
|
138
|
-
// we get here if
|
|
139
|
-
// - batching is called when it's not enabled
|
|
140
|
-
// - `createContext()` throws
|
|
141
|
-
// - post body is too large
|
|
142
|
-
// - input deserialization fails
|
|
143
|
-
// - `errorFormatter` return value is malformed
|
|
144
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
145
|
-
opts.onError?.({
|
|
146
|
-
error,
|
|
147
|
-
path: undefined,
|
|
148
|
-
input: undefined,
|
|
149
|
-
ctx,
|
|
150
|
-
type: type,
|
|
151
|
-
req,
|
|
152
|
-
});
|
|
153
|
-
return endResponse({
|
|
154
|
-
error: getErrorShape({
|
|
155
|
-
config: router._def._config,
|
|
156
|
-
error,
|
|
157
|
-
type,
|
|
158
|
-
path: undefined,
|
|
159
|
-
input: undefined,
|
|
160
|
-
ctx,
|
|
161
|
-
}),
|
|
162
|
-
}, [error]);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export { resolveHTTPResponse as r };
|