@trpc/server 10.0.0-proxy-alpha.76 → 10.0.0-proxy-alpha.77
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-281ac25a.mjs → TRPCError-e99e7cbe.mjs} +1 -1
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/index.mjs +3 -3
- package/dist/adapters/express.js +4 -3
- package/dist/adapters/express.mjs +5 -4
- package/dist/adapters/fastify/index.js +3 -2
- package/dist/adapters/fastify/index.mjs +4 -3
- package/dist/adapters/fetch/index.js +3 -2
- package/dist/adapters/fetch/index.mjs +4 -3
- package/dist/adapters/next.js +4 -3
- package/dist/adapters/next.mjs +5 -4
- package/dist/adapters/node-http/index.js +4 -3
- package/dist/adapters/node-http/index.mjs +5 -4
- package/dist/adapters/standalone.js +4 -3
- package/dist/adapters/standalone.mjs +5 -4
- package/dist/adapters/ws.mjs +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +0 -40
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/procedure.d.ts +3 -0
- package/dist/core/procedure.d.ts.map +1 -1
- package/dist/core/router.d.ts +17 -32
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/types.d.ts +5 -5
- package/dist/core/types.d.ts.map +1 -1
- package/dist/deprecated/interop.d.ts +20 -43
- package/dist/deprecated/interop.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/index.js +23 -23
- package/dist/index.mjs +5 -5
- package/dist/{nodeHTTPRequestHandler-c3dbb39e.js → nodeHTTPRequestHandler-7295e689.js} +1 -1
- package/dist/{nodeHTTPRequestHandler-c30139c8.mjs → nodeHTTPRequestHandler-e4a030ab.mjs} +2 -2
- package/dist/{router-6b03ff5f.mjs → resolveHTTPResponse-3955937e.mjs} +411 -601
- package/dist/{router-da6086c3.js → resolveHTTPResponse-9a70b9b2.js} +414 -601
- package/dist/router-4f222389.mjs +450 -0
- package/dist/router-fee6849a.js +452 -0
- package/dist/shared/index.d.ts +0 -1
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/subscription.mjs +1 -1
- package/dist/types.d.ts +0 -10
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/core/index.ts +4 -0
- package/src/core/internals/__generated__/mergeRoutersGeneric.ts +0 -40
- package/src/core/internals/procedureBuilder.ts +9 -6
- package/src/core/procedure.ts +3 -1
- package/src/core/router.ts +22 -38
- package/src/core/types.ts +7 -18
- package/src/deprecated/interop.ts +30 -78
- package/src/http/resolveHTTPResponse.ts +8 -2
- package/src/shared/index.ts +0 -2
- package/src/types.ts +0 -12
- package/dist/resolveHTTPResponse-1b339cff.mjs +0 -260
- package/dist/resolveHTTPResponse-d7a8a210.js +0 -264
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var TRPCError = require('./TRPCError-fe9113e5.js');
|
|
4
|
-
var transformTRPCResponse = require('./transformTRPCResponse-11ac69cc.js');
|
|
5
|
-
var codes = require('./codes-1c24d919.js');
|
|
6
|
-
|
|
7
|
-
/* istanbul ignore file */ function assertNotBrowser() {
|
|
8
|
-
if (typeof window !== 'undefined' && !('Deno' in window) && process.env.NODE_ENV !== 'test' && process.env.JEST_WORKER_ID === undefined) {
|
|
9
|
-
throw new Error('Imported server-only code in the browser');
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function invert(obj) {
|
|
14
|
-
const newObj = Object.create(null);
|
|
15
|
-
for(const key in obj){
|
|
16
|
-
const v = obj[key];
|
|
17
|
-
newObj[v] = key;
|
|
18
|
-
}
|
|
19
|
-
return newObj;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const TRPC_ERROR_CODES_BY_NUMBER = invert(codes.TRPC_ERROR_CODES_BY_KEY);
|
|
23
|
-
const JSONRPC2_TO_HTTP_CODE = {
|
|
24
|
-
PARSE_ERROR: 400,
|
|
25
|
-
BAD_REQUEST: 400,
|
|
26
|
-
NOT_FOUND: 404,
|
|
27
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
28
|
-
UNAUTHORIZED: 401,
|
|
29
|
-
FORBIDDEN: 403,
|
|
30
|
-
TIMEOUT: 408,
|
|
31
|
-
CONFLICT: 409,
|
|
32
|
-
CLIENT_CLOSED_REQUEST: 499,
|
|
33
|
-
PRECONDITION_FAILED: 412,
|
|
34
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
35
|
-
METHOD_NOT_SUPPORTED: 405
|
|
36
|
-
};
|
|
37
|
-
function getStatusCodeFromKey(code) {
|
|
38
|
-
return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
|
|
39
|
-
}
|
|
40
|
-
function getHTTPStatusCode(json) {
|
|
41
|
-
const arr = Array.isArray(json) ? json : [
|
|
42
|
-
json
|
|
43
|
-
];
|
|
44
|
-
const httpStatuses = new Set(arr.map((res)=>{
|
|
45
|
-
if ('error' in res) {
|
|
46
|
-
const data = res.error.data;
|
|
47
|
-
if (typeof data.httpStatus === 'number') {
|
|
48
|
-
return data.httpStatus;
|
|
49
|
-
}
|
|
50
|
-
const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
|
|
51
|
-
return getStatusCodeFromKey(code);
|
|
52
|
-
}
|
|
53
|
-
return 200;
|
|
54
|
-
}));
|
|
55
|
-
if (httpStatuses.size !== 1) {
|
|
56
|
-
return 207;
|
|
57
|
-
}
|
|
58
|
-
const httpStatus = httpStatuses.values().next().value;
|
|
59
|
-
return httpStatus;
|
|
60
|
-
}
|
|
61
|
-
function getHTTPStatusCodeFromError(error) {
|
|
62
|
-
const { code } = error;
|
|
63
|
-
return getStatusCodeFromKey(code);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
67
|
-
GET: 'query',
|
|
68
|
-
POST: 'mutation'
|
|
69
|
-
};
|
|
70
|
-
function getRawProcedureInputOrThrow(req) {
|
|
71
|
-
try {
|
|
72
|
-
if (req.method === 'GET') {
|
|
73
|
-
if (!req.query.has('input')) {
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
const raw = req.query.get('input');
|
|
77
|
-
return JSON.parse(raw);
|
|
78
|
-
}
|
|
79
|
-
if (typeof req.body === 'string') {
|
|
80
|
-
// A mutation with no inputs will have req.body === ''
|
|
81
|
-
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
82
|
-
}
|
|
83
|
-
return req.body;
|
|
84
|
-
} catch (err) {
|
|
85
|
-
throw new TRPCError.TRPCError({
|
|
86
|
-
code: 'PARSE_ERROR',
|
|
87
|
-
cause: TRPCError.getCauseFromUnknown(err)
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async function resolveHTTPResponse(opts) {
|
|
92
|
-
const { createContext , onError , router , req } = opts;
|
|
93
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
94
|
-
if (req.method === 'HEAD') {
|
|
95
|
-
// can be used for lambda warmup
|
|
96
|
-
return {
|
|
97
|
-
status: 204
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
101
|
-
let ctx = undefined;
|
|
102
|
-
let paths = undefined;
|
|
103
|
-
const isBatchCall = !!req.query.get('batch');
|
|
104
|
-
function endResponse(untransformedJSON, errors) {
|
|
105
|
-
let status = getHTTPStatusCode(untransformedJSON);
|
|
106
|
-
const headers = {
|
|
107
|
-
'Content-Type': 'application/json'
|
|
108
|
-
};
|
|
109
|
-
const meta = opts.responseMeta?.({
|
|
110
|
-
ctx,
|
|
111
|
-
paths,
|
|
112
|
-
type,
|
|
113
|
-
data: Array.isArray(untransformedJSON) ? untransformedJSON : [
|
|
114
|
-
untransformedJSON
|
|
115
|
-
],
|
|
116
|
-
errors
|
|
117
|
-
}) ?? {};
|
|
118
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})){
|
|
119
|
-
headers[key] = value;
|
|
120
|
-
}
|
|
121
|
-
if (meta.status) {
|
|
122
|
-
status = meta.status;
|
|
123
|
-
}
|
|
124
|
-
const transformedJSON = transformTRPCResponse.transformTRPCResponse(router, untransformedJSON);
|
|
125
|
-
const body = JSON.stringify(transformedJSON);
|
|
126
|
-
return {
|
|
127
|
-
body,
|
|
128
|
-
status,
|
|
129
|
-
headers
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
if (opts.error) {
|
|
134
|
-
throw opts.error;
|
|
135
|
-
}
|
|
136
|
-
if (isBatchCall && !batchingEnabled) {
|
|
137
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
138
|
-
}
|
|
139
|
-
if (type === 'subscription') {
|
|
140
|
-
throw new TRPCError.TRPCError({
|
|
141
|
-
message: 'Subscriptions should use wsLink',
|
|
142
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
if (type === 'unknown') {
|
|
146
|
-
throw new TRPCError.TRPCError({
|
|
147
|
-
message: `Unexpected request method ${req.method}`,
|
|
148
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
const rawInput = getRawProcedureInputOrThrow(req);
|
|
152
|
-
paths = isBatchCall ? opts.path.split(',') : [
|
|
153
|
-
opts.path
|
|
154
|
-
];
|
|
155
|
-
ctx = await createContext();
|
|
156
|
-
const deserializeInputValue = (rawValue)=>{
|
|
157
|
-
return typeof rawValue !== 'undefined' ? router._def.transformer.input.deserialize(rawValue) : rawValue;
|
|
158
|
-
};
|
|
159
|
-
const getInputs = ()=>{
|
|
160
|
-
if (!isBatchCall) {
|
|
161
|
-
return {
|
|
162
|
-
0: deserializeInputValue(rawInput)
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
|
|
166
|
-
throw new TRPCError.TRPCError({
|
|
167
|
-
code: 'BAD_REQUEST',
|
|
168
|
-
message: '"input" needs to be an object when doing a batch call'
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
const input = {};
|
|
172
|
-
for(const key in rawInput){
|
|
173
|
-
const k = key;
|
|
174
|
-
const rawValue = rawInput[k];
|
|
175
|
-
const value = deserializeInputValue(rawValue);
|
|
176
|
-
input[k] = value;
|
|
177
|
-
}
|
|
178
|
-
return input;
|
|
179
|
-
};
|
|
180
|
-
const inputs = getInputs();
|
|
181
|
-
const rawResults = await Promise.all(paths.map(async (path, index)=>{
|
|
182
|
-
const input = inputs[index];
|
|
183
|
-
try {
|
|
184
|
-
const caller = router.createCaller(ctx);
|
|
185
|
-
const output = await caller[type](path, input);
|
|
186
|
-
return {
|
|
187
|
-
input,
|
|
188
|
-
path,
|
|
189
|
-
data: output
|
|
190
|
-
};
|
|
191
|
-
} catch (cause) {
|
|
192
|
-
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
193
|
-
onError?.({
|
|
194
|
-
error,
|
|
195
|
-
path,
|
|
196
|
-
input,
|
|
197
|
-
ctx,
|
|
198
|
-
type: type,
|
|
199
|
-
req
|
|
200
|
-
});
|
|
201
|
-
return {
|
|
202
|
-
input,
|
|
203
|
-
path,
|
|
204
|
-
error
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
}));
|
|
208
|
-
const errors = rawResults.flatMap((obj)=>obj.error ? [
|
|
209
|
-
obj.error
|
|
210
|
-
] : []);
|
|
211
|
-
const resultEnvelopes = rawResults.map((obj)=>{
|
|
212
|
-
const { path , input } = obj;
|
|
213
|
-
if (obj.error) {
|
|
214
|
-
return {
|
|
215
|
-
error: router.getErrorShape({
|
|
216
|
-
error: obj.error,
|
|
217
|
-
type,
|
|
218
|
-
path,
|
|
219
|
-
input,
|
|
220
|
-
ctx
|
|
221
|
-
})
|
|
222
|
-
};
|
|
223
|
-
} else {
|
|
224
|
-
return {
|
|
225
|
-
result: {
|
|
226
|
-
data: obj.data
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
|
|
232
|
-
return endResponse(result, errors);
|
|
233
|
-
} catch (cause) {
|
|
234
|
-
// we get here if
|
|
235
|
-
// - batching is called when it's not enabled
|
|
236
|
-
// - `createContext()` throws
|
|
237
|
-
// - post body is too large
|
|
238
|
-
// - input deserialization fails
|
|
239
|
-
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
240
|
-
onError?.({
|
|
241
|
-
error,
|
|
242
|
-
path: undefined,
|
|
243
|
-
input: undefined,
|
|
244
|
-
ctx,
|
|
245
|
-
type: type,
|
|
246
|
-
req
|
|
247
|
-
});
|
|
248
|
-
return endResponse({
|
|
249
|
-
error: router.getErrorShape({
|
|
250
|
-
error,
|
|
251
|
-
type,
|
|
252
|
-
path: undefined,
|
|
253
|
-
input: undefined,
|
|
254
|
-
ctx
|
|
255
|
-
})
|
|
256
|
-
}, [
|
|
257
|
-
error
|
|
258
|
-
]);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
exports.assertNotBrowser = assertNotBrowser;
|
|
263
|
-
exports.getHTTPStatusCodeFromError = getHTTPStatusCodeFromError;
|
|
264
|
-
exports.resolveHTTPResponse = resolveHTTPResponse;
|