@trpc/server 11.0.0-rc.747 → 11.0.0-rc.749
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/index.d.ts +1 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +1 -1
- package/dist/adapters/node-http/writeResponse.js +2 -2
- package/dist/adapters/node-http/writeResponse.mjs +1 -1
- package/dist/adapters/ws.js +2 -2
- package/dist/adapters/ws.mjs +2 -2
- package/dist/bundle-analysis.json +122 -117
- package/dist/index.js +4 -3
- package/dist/index.mjs +1 -1
- package/dist/unstable-core-do-not-import/http/{isAbortError.d.ts → abortError.d.ts} +2 -1
- package/dist/unstable-core-do-not-import/http/abortError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/{isAbortError.js → abortError.js} +4 -0
- package/dist/unstable-core-do-not-import/http/abortError.mjs +10 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +2 -2
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +12 -10
- package/dist/unstable-core-do-not-import/http/contentType.mjs +12 -10
- package/dist/unstable-core-do-not-import/http/resolveResponse.js +2 -2
- package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +2 -2
- package/dist/unstable-core-do-not-import/procedureBuilder.js +1 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -0
- package/dist/unstable-core-do-not-import/router.d.ts +20 -3
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.js +102 -6
- package/dist/unstable-core-do-not-import/router.mjs +102 -8
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/sse.js +12 -2
- package/dist/unstable-core-do-not-import/stream/sse.mjs +11 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +2 -2
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +5 -7
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +5 -7
- package/dist/unstable-core-do-not-import.d.ts +1 -1
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +5 -2
- package/dist/unstable-core-do-not-import.mjs +2 -2
- package/package.json +2 -2
- package/src/@trpc/server/index.ts +1 -0
- package/src/adapters/ws.ts +1 -1
- package/src/unstable-core-do-not-import/http/{isAbortError.ts → abortError.ts} +4 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +48 -42
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +2 -2
- package/src/unstable-core-do-not-import/procedureBuilder.ts +1 -0
- package/src/unstable-core-do-not-import/router.ts +156 -14
- package/src/unstable-core-do-not-import/stream/sse.ts +12 -1
- package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +5 -8
- package/src/unstable-core-do-not-import.ts +1 -1
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/isAbortError.mjs +0 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abortError.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/abortError.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,YAAY,GAAG,KAAK,GAAG;IAAE,IAAI,EAAE,YAAY,CAAA;CAAE,CAExD;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAe,GAAG,KAAK,CAE7D"}
|
|
@@ -5,5 +5,9 @@ var utils = require('../utils.js');
|
|
|
5
5
|
function isAbortError(error) {
|
|
6
6
|
return utils.isObject(error) && error['name'] === 'AbortError';
|
|
7
7
|
}
|
|
8
|
+
function throwAbortError(message = 'AbortError') {
|
|
9
|
+
throw new DOMException(message, 'AbortError');
|
|
10
|
+
}
|
|
8
11
|
|
|
9
12
|
exports.isAbortError = isAbortError;
|
|
13
|
+
exports.throwAbortError = throwAbortError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isObject } from '../utils.mjs';
|
|
2
|
+
|
|
3
|
+
function isAbortError(error) {
|
|
4
|
+
return isObject(error) && error['name'] === 'AbortError';
|
|
5
|
+
}
|
|
6
|
+
function throwAbortError(message = 'AbortError') {
|
|
7
|
+
throw new DOMException(message, 'AbortError');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { isAbortError, throwAbortError };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnyRouter } from '../router';
|
|
2
2
|
import type { TRPCRequestInfo } from './types';
|
|
3
3
|
type GetRequestInfoOptions = {
|
|
4
4
|
path: string;
|
|
@@ -8,6 +8,6 @@ type GetRequestInfoOptions = {
|
|
|
8
8
|
headers: Headers;
|
|
9
9
|
router: AnyRouter;
|
|
10
10
|
};
|
|
11
|
-
export declare function getRequestInfo(opts: GetRequestInfoOptions): TRPCRequestInfo
|
|
11
|
+
export declare function getRequestInfo(opts: GetRequestInfoOptions): Promise<TRPCRequestInfo>;
|
|
12
12
|
export {};
|
|
13
13
|
//# sourceMappingURL=contentType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contentType.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/contentType.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"contentType.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/contentType.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAG/D,OAAO,KAAK,EAAoB,eAAe,EAAE,MAAM,SAAS,CAAC;AAEjE,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IAChB,YAAY,EAAE,eAAe,CAAC;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC;AA+QF,wBAAsB,cAAc,CAClC,IAAI,EAAE,qBAAqB,GAC1B,OAAO,CAAC,eAAe,CAAC,CAG1B"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var TRPCError = require('../error/TRPCError.js');
|
|
4
|
+
var router = require('../router.js');
|
|
4
5
|
var utils = require('../utils.js');
|
|
5
6
|
var parseConnectionParams = require('./parseConnectionParams.js');
|
|
6
7
|
|
|
@@ -45,7 +46,7 @@ const jsonContentTypeHandler = {
|
|
|
45
46
|
isMatch (req) {
|
|
46
47
|
return !!req.headers.get('content-type')?.startsWith('application/json');
|
|
47
48
|
},
|
|
48
|
-
parse (opts) {
|
|
49
|
+
async parse (opts) {
|
|
49
50
|
const { req } = opts;
|
|
50
51
|
const isBatchCall = opts.searchParams.get('batch') === '1';
|
|
51
52
|
const paths = isBatchCall ? opts.path.split(',') : [
|
|
@@ -84,8 +85,8 @@ const jsonContentTypeHandler = {
|
|
|
84
85
|
}
|
|
85
86
|
return acc;
|
|
86
87
|
});
|
|
87
|
-
const calls = paths.map((path, index)=>{
|
|
88
|
-
const procedure = opts.router._def
|
|
88
|
+
const calls = await Promise.all(paths.map(async (path, index)=>{
|
|
89
|
+
const procedure = await router.getProcedureAtPath(opts.router._def, path);
|
|
89
90
|
return {
|
|
90
91
|
path,
|
|
91
92
|
procedure,
|
|
@@ -113,7 +114,7 @@ const jsonContentTypeHandler = {
|
|
|
113
114
|
return getInputs.result()?.[index];
|
|
114
115
|
}
|
|
115
116
|
};
|
|
116
|
-
});
|
|
117
|
+
}));
|
|
117
118
|
const types = new Set(calls.map((call)=>call.procedure?._def.type).filter(Boolean));
|
|
118
119
|
/* istanbul ignore if -- @preserve */ if (types.size > 1) {
|
|
119
120
|
throw new TRPCError.TRPCError({
|
|
@@ -139,7 +140,7 @@ const formDataContentTypeHandler = {
|
|
|
139
140
|
isMatch (req) {
|
|
140
141
|
return !!req.headers.get('content-type')?.startsWith('multipart/form-data');
|
|
141
142
|
},
|
|
142
|
-
parse (opts) {
|
|
143
|
+
async parse (opts) {
|
|
143
144
|
const { req } = opts;
|
|
144
145
|
if (req.method !== 'POST') {
|
|
145
146
|
throw new TRPCError.TRPCError({
|
|
@@ -151,6 +152,7 @@ const formDataContentTypeHandler = {
|
|
|
151
152
|
const fd = await req.formData();
|
|
152
153
|
return fd;
|
|
153
154
|
});
|
|
155
|
+
const procedure = await router.getProcedureAtPath(opts.router._def, opts.path);
|
|
154
156
|
return {
|
|
155
157
|
accept: null,
|
|
156
158
|
calls: [
|
|
@@ -158,7 +160,7 @@ const formDataContentTypeHandler = {
|
|
|
158
160
|
path: opts.path,
|
|
159
161
|
getRawInput: getInputs.read,
|
|
160
162
|
result: getInputs.result,
|
|
161
|
-
procedure
|
|
163
|
+
procedure
|
|
162
164
|
}
|
|
163
165
|
],
|
|
164
166
|
isBatchCall: false,
|
|
@@ -173,7 +175,7 @@ const octetStreamContentTypeHandler = {
|
|
|
173
175
|
isMatch (req) {
|
|
174
176
|
return !!req.headers.get('content-type')?.startsWith('application/octet-stream');
|
|
175
177
|
},
|
|
176
|
-
parse (opts) {
|
|
178
|
+
async parse (opts) {
|
|
177
179
|
const { req } = opts;
|
|
178
180
|
if (req.method !== 'POST') {
|
|
179
181
|
throw new TRPCError.TRPCError({
|
|
@@ -190,7 +192,7 @@ const octetStreamContentTypeHandler = {
|
|
|
190
192
|
path: opts.path,
|
|
191
193
|
getRawInput: getInputs.read,
|
|
192
194
|
result: getInputs.result,
|
|
193
|
-
procedure: opts.router._def
|
|
195
|
+
procedure: await router.getProcedureAtPath(opts.router._def, opts.path)
|
|
194
196
|
}
|
|
195
197
|
],
|
|
196
198
|
isBatchCall: false,
|
|
@@ -221,9 +223,9 @@ function getContentTypeHandler(req) {
|
|
|
221
223
|
message: req.headers.has('content-type') ? `Unsupported content-type "${req.headers.get('content-type')}` : 'Missing content-type header'
|
|
222
224
|
});
|
|
223
225
|
}
|
|
224
|
-
function getRequestInfo(opts) {
|
|
226
|
+
async function getRequestInfo(opts) {
|
|
225
227
|
const handler = getContentTypeHandler(opts.req);
|
|
226
|
-
return handler.parse(opts);
|
|
228
|
+
return await handler.parse(opts);
|
|
227
229
|
}
|
|
228
230
|
|
|
229
231
|
exports.getRequestInfo = getRequestInfo;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TRPCError } from '../error/TRPCError.mjs';
|
|
2
|
+
import { getProcedureAtPath } from '../router.mjs';
|
|
2
3
|
import { isObject, unsetMarker } from '../utils.mjs';
|
|
3
4
|
import { parseConnectionParamsFromString } from './parseConnectionParams.mjs';
|
|
4
5
|
|
|
@@ -43,7 +44,7 @@ const jsonContentTypeHandler = {
|
|
|
43
44
|
isMatch (req) {
|
|
44
45
|
return !!req.headers.get('content-type')?.startsWith('application/json');
|
|
45
46
|
},
|
|
46
|
-
parse (opts) {
|
|
47
|
+
async parse (opts) {
|
|
47
48
|
const { req } = opts;
|
|
48
49
|
const isBatchCall = opts.searchParams.get('batch') === '1';
|
|
49
50
|
const paths = isBatchCall ? opts.path.split(',') : [
|
|
@@ -82,8 +83,8 @@ const jsonContentTypeHandler = {
|
|
|
82
83
|
}
|
|
83
84
|
return acc;
|
|
84
85
|
});
|
|
85
|
-
const calls = paths.map((path, index)=>{
|
|
86
|
-
const procedure = opts.router._def
|
|
86
|
+
const calls = await Promise.all(paths.map(async (path, index)=>{
|
|
87
|
+
const procedure = await getProcedureAtPath(opts.router._def, path);
|
|
87
88
|
return {
|
|
88
89
|
path,
|
|
89
90
|
procedure,
|
|
@@ -111,7 +112,7 @@ const jsonContentTypeHandler = {
|
|
|
111
112
|
return getInputs.result()?.[index];
|
|
112
113
|
}
|
|
113
114
|
};
|
|
114
|
-
});
|
|
115
|
+
}));
|
|
115
116
|
const types = new Set(calls.map((call)=>call.procedure?._def.type).filter(Boolean));
|
|
116
117
|
/* istanbul ignore if -- @preserve */ if (types.size > 1) {
|
|
117
118
|
throw new TRPCError({
|
|
@@ -137,7 +138,7 @@ const formDataContentTypeHandler = {
|
|
|
137
138
|
isMatch (req) {
|
|
138
139
|
return !!req.headers.get('content-type')?.startsWith('multipart/form-data');
|
|
139
140
|
},
|
|
140
|
-
parse (opts) {
|
|
141
|
+
async parse (opts) {
|
|
141
142
|
const { req } = opts;
|
|
142
143
|
if (req.method !== 'POST') {
|
|
143
144
|
throw new TRPCError({
|
|
@@ -149,6 +150,7 @@ const formDataContentTypeHandler = {
|
|
|
149
150
|
const fd = await req.formData();
|
|
150
151
|
return fd;
|
|
151
152
|
});
|
|
153
|
+
const procedure = await getProcedureAtPath(opts.router._def, opts.path);
|
|
152
154
|
return {
|
|
153
155
|
accept: null,
|
|
154
156
|
calls: [
|
|
@@ -156,7 +158,7 @@ const formDataContentTypeHandler = {
|
|
|
156
158
|
path: opts.path,
|
|
157
159
|
getRawInput: getInputs.read,
|
|
158
160
|
result: getInputs.result,
|
|
159
|
-
procedure
|
|
161
|
+
procedure
|
|
160
162
|
}
|
|
161
163
|
],
|
|
162
164
|
isBatchCall: false,
|
|
@@ -171,7 +173,7 @@ const octetStreamContentTypeHandler = {
|
|
|
171
173
|
isMatch (req) {
|
|
172
174
|
return !!req.headers.get('content-type')?.startsWith('application/octet-stream');
|
|
173
175
|
},
|
|
174
|
-
parse (opts) {
|
|
176
|
+
async parse (opts) {
|
|
175
177
|
const { req } = opts;
|
|
176
178
|
if (req.method !== 'POST') {
|
|
177
179
|
throw new TRPCError({
|
|
@@ -188,7 +190,7 @@ const octetStreamContentTypeHandler = {
|
|
|
188
190
|
path: opts.path,
|
|
189
191
|
getRawInput: getInputs.read,
|
|
190
192
|
result: getInputs.result,
|
|
191
|
-
procedure: opts.router._def
|
|
193
|
+
procedure: await getProcedureAtPath(opts.router._def, opts.path)
|
|
192
194
|
}
|
|
193
195
|
],
|
|
194
196
|
isBatchCall: false,
|
|
@@ -219,9 +221,9 @@ function getContentTypeHandler(req) {
|
|
|
219
221
|
message: req.headers.has('content-type') ? `Unsupported content-type "${req.headers.get('content-type')}` : 'Missing content-type header'
|
|
220
222
|
});
|
|
221
223
|
}
|
|
222
|
-
function getRequestInfo(opts) {
|
|
224
|
+
async function getRequestInfo(opts) {
|
|
223
225
|
const handler = getContentTypeHandler(opts.req);
|
|
224
|
-
return handler.parse(opts);
|
|
226
|
+
return await handler.parse(opts);
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
export { getRequestInfo };
|
|
@@ -142,11 +142,11 @@ async function resolveResponse(opts) {
|
|
|
142
142
|
}
|
|
143
143
|
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
144
144
|
const allowMethodOverride = (opts.allowMethodOverride ?? false) && req.method === 'POST';
|
|
145
|
-
const infoTuple = utils.run(()=>{
|
|
145
|
+
const infoTuple = await utils.run(async ()=>{
|
|
146
146
|
try {
|
|
147
147
|
return [
|
|
148
148
|
undefined,
|
|
149
|
-
contentType.getRequestInfo({
|
|
149
|
+
await contentType.getRequestInfo({
|
|
150
150
|
req,
|
|
151
151
|
path: decodeURIComponent(opts.path),
|
|
152
152
|
router,
|
|
@@ -140,11 +140,11 @@ async function resolveResponse(opts) {
|
|
|
140
140
|
}
|
|
141
141
|
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
142
142
|
const allowMethodOverride = (opts.allowMethodOverride ?? false) && req.method === 'POST';
|
|
143
|
-
const infoTuple = run(()=>{
|
|
143
|
+
const infoTuple = await run(async ()=>{
|
|
144
144
|
try {
|
|
145
145
|
return [
|
|
146
146
|
undefined,
|
|
147
|
-
getRequestInfo({
|
|
147
|
+
await getRequestInfo({
|
|
148
148
|
req,
|
|
149
149
|
path: decodeURIComponent(opts.path),
|
|
150
150
|
router,
|
|
@@ -31,6 +31,21 @@ ctx: TRoot['ctx'] | (() => MaybePromise<TRoot['ctx']>), options?: {
|
|
|
31
31
|
onError?: RouterCallerErrorHandler<TRoot['ctx']>;
|
|
32
32
|
signal?: AbortSignal;
|
|
33
33
|
}) => DecorateRouterRecord<TRecord>;
|
|
34
|
+
declare const lazySymbol: unique symbol;
|
|
35
|
+
export type Lazy<TAny> = (() => Promise<TAny>) & {
|
|
36
|
+
[lazySymbol]: true;
|
|
37
|
+
};
|
|
38
|
+
type LazyLoader<TAny> = {
|
|
39
|
+
load: () => Promise<void>;
|
|
40
|
+
ref: Lazy<TAny>;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Lazy load a router
|
|
44
|
+
* @see https://trpc.io/docs/server/merging-routers#lazy-load
|
|
45
|
+
*/
|
|
46
|
+
export declare function lazy<TRouter extends AnyRouter>(getRouter: () => Promise<TRouter | {
|
|
47
|
+
[key: string]: TRouter;
|
|
48
|
+
}>): Lazy<NoInfer<TRouter>>;
|
|
34
49
|
export interface Router<TRoot extends AnyRootTypes, TRecord extends RouterRecord> {
|
|
35
50
|
_def: {
|
|
36
51
|
_config: RootConfig<TRoot>;
|
|
@@ -38,6 +53,7 @@ export interface Router<TRoot extends AnyRootTypes, TRecord extends RouterRecord
|
|
|
38
53
|
procedure?: never;
|
|
39
54
|
procedures: TRecord;
|
|
40
55
|
record: TRecord;
|
|
56
|
+
lazy: Record<string, LazyLoader<AnyRouter>>;
|
|
41
57
|
};
|
|
42
58
|
/**
|
|
43
59
|
* @deprecated use `t.createCallerFactory(router)` instead
|
|
@@ -52,20 +68,21 @@ export type inferRouterContext<TRouter extends AnyRouter> = inferRouterRootTypes
|
|
|
52
68
|
export type inferRouterError<TRouter extends AnyRouter> = inferRouterRootTypes<TRouter>['errorShape'];
|
|
53
69
|
export type inferRouterMeta<TRouter extends AnyRouter> = inferRouterRootTypes<TRouter>['meta'];
|
|
54
70
|
export type CreateRouterOptions = {
|
|
55
|
-
[key: string]: AnyProcedure | AnyRouter | CreateRouterOptions
|
|
71
|
+
[key: string]: AnyProcedure | AnyRouter | CreateRouterOptions | Lazy<AnyRouter>;
|
|
56
72
|
};
|
|
57
73
|
export type DecorateCreateRouterOptions<TRouterOptions extends CreateRouterOptions> = {
|
|
58
|
-
[K in keyof TRouterOptions]: TRouterOptions[K] extends infer $Value ? $Value extends AnyProcedure ? $Value : $Value extends Router<any, infer TRecord> ? TRecord : $Value extends CreateRouterOptions ? DecorateCreateRouterOptions<$Value> : never : never;
|
|
74
|
+
[K in keyof TRouterOptions]: TRouterOptions[K] extends infer $Value ? $Value extends AnyProcedure ? $Value : $Value extends Router<any, infer TRecord> ? TRecord : $Value extends Lazy<Router<any, infer TRecord>> ? TRecord : $Value extends CreateRouterOptions ? DecorateCreateRouterOptions<$Value> : never : never;
|
|
59
75
|
};
|
|
60
76
|
/**
|
|
61
77
|
* @internal
|
|
62
78
|
*/
|
|
63
79
|
export declare function createRouterFactory<TRoot extends AnyRootTypes>(config: RootConfig<TRoot>): <TInput extends CreateRouterOptions>(input: TInput) => BuiltRouter<TRoot, DecorateCreateRouterOptions<TInput>>;
|
|
80
|
+
export declare function getProcedureAtPath(_def: AnyRouter['_def'], path: string): Promise<AnyProcedure | null>;
|
|
64
81
|
/**
|
|
65
82
|
* @internal
|
|
66
83
|
*/
|
|
67
84
|
export declare function callProcedure(opts: ProcedureCallOptions<unknown> & {
|
|
68
|
-
|
|
85
|
+
_def: AnyRouter['_def'];
|
|
69
86
|
allowMethodOverride?: boolean;
|
|
70
87
|
}): Promise<any>;
|
|
71
88
|
export declare function createCallerFactory<TRoot extends AnyRootTypes>(): <TRecord extends RouterRecord>(router: Pick<Router<TRoot, TRecord>, "_def">) => RouterCaller<TRoot, TRecord>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/unstable-core-do-not-import/router.ts"],"names":[],"mappings":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AK,SAAS,YAAY,MAC1B,OAAO,SAAS,YAAY,UACpD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,KAC3C,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAgDhC;AAED,gBAAgB;AAChB,KAAK,YAAY,CACf,QAAQ,SAAS,SAAS,EAAE,EAC5B,KAAK,SAAS,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,EACrE,OAAO,SAAS,YAAY,GAAG,EAAE,IAC/B,QAAQ,SAAS;IACnB,MAAM,IAAI,SAAS,SAAS;IAC5B,GAAG,MAAM,IAAI,SAAS,SAAS,EAAE;CAClC,GACG,YAAY,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,GAC3D,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AAEhC,wBAAgB,YAAY,CAAC,QAAQ,SAAS,SAAS,EAAE,EACvD,GAAG,UAAU,EAAE,CAAC,GAAG,QAAQ,CAAC,GAC3B,YAAY,CAAC,QAAQ,CAAC,CAoDxB"}
|
|
@@ -6,8 +6,37 @@ var TRPCError = require('./error/TRPCError.js');
|
|
|
6
6
|
var transformer = require('./transformer.js');
|
|
7
7
|
var utils = require('./utils.js');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const lazySymbol = Symbol('lazy');
|
|
10
|
+
/**
|
|
11
|
+
* Lazy load a router
|
|
12
|
+
* @see https://trpc.io/docs/server/merging-routers#lazy-load
|
|
13
|
+
*/ function lazy(getRouter) {
|
|
14
|
+
let cachedPromise = null;
|
|
15
|
+
const lazyGetter = ()=>{
|
|
16
|
+
if (!cachedPromise) {
|
|
17
|
+
cachedPromise = utils.run(async ()=>{
|
|
18
|
+
const mod = await getRouter();
|
|
19
|
+
// if the module is a router, return it
|
|
20
|
+
if (isRouter(mod)) {
|
|
21
|
+
return mod;
|
|
22
|
+
}
|
|
23
|
+
const routers = Object.values(mod);
|
|
24
|
+
if (routers.length !== 1 || !isRouter(routers[0])) {
|
|
25
|
+
throw new Error("Invalid router module - either define exactly 1 export or return the router directly.\nExample: `experimental_lazy(() => import('./slow.js').then((m) => m.slowRouter))`");
|
|
26
|
+
}
|
|
27
|
+
return routers[0];
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return cachedPromise;
|
|
31
|
+
};
|
|
32
|
+
lazyGetter[lazySymbol] = true;
|
|
33
|
+
return lazyGetter;
|
|
34
|
+
}
|
|
35
|
+
function isLazy(input) {
|
|
36
|
+
return typeof input === 'function' && lazySymbol in input;
|
|
37
|
+
}
|
|
38
|
+
function isRouter(value) {
|
|
39
|
+
return utils.isObject(value) && utils.isObject(value['_def']) && 'router' in value['_def'];
|
|
11
40
|
}
|
|
12
41
|
const emptyRouter = {
|
|
13
42
|
_ctx: null,
|
|
@@ -40,9 +69,51 @@ const emptyRouter = {
|
|
|
40
69
|
throw new Error('Reserved words used in `router({})` call: ' + Array.from(reservedWordsUsed).join(', '));
|
|
41
70
|
}
|
|
42
71
|
const procedures = utils.omitPrototype({});
|
|
72
|
+
const lazy = utils.omitPrototype({});
|
|
73
|
+
function createLazyLoader(opts) {
|
|
74
|
+
return {
|
|
75
|
+
ref: opts.ref,
|
|
76
|
+
load: async ()=>{
|
|
77
|
+
const router = await opts.ref();
|
|
78
|
+
const lazyPath = [
|
|
79
|
+
...opts.path,
|
|
80
|
+
opts.key
|
|
81
|
+
];
|
|
82
|
+
const lazyKey = lazyPath.join('.');
|
|
83
|
+
opts.aggregate[opts.key] = step(router._def.record, lazyPath);
|
|
84
|
+
delete lazy[lazyKey];
|
|
85
|
+
// add lazy loaders for nested routers
|
|
86
|
+
for (const [nestedKey, nestedItem] of Object.entries(router._def.lazy)){
|
|
87
|
+
const nestedRouterKey = [
|
|
88
|
+
...lazyPath,
|
|
89
|
+
nestedKey
|
|
90
|
+
].join('.');
|
|
91
|
+
// console.log('adding lazy', nestedRouterKey);
|
|
92
|
+
lazy[nestedRouterKey] = createLazyLoader({
|
|
93
|
+
ref: nestedItem.ref,
|
|
94
|
+
path: lazyPath,
|
|
95
|
+
key: nestedKey,
|
|
96
|
+
aggregate: opts.aggregate[opts.key]
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
43
102
|
function step(from, path = []) {
|
|
44
103
|
const aggregate = utils.omitPrototype({});
|
|
45
104
|
for (const [key, item] of Object.entries(from ?? {})){
|
|
105
|
+
if (isLazy(item)) {
|
|
106
|
+
lazy[[
|
|
107
|
+
...path,
|
|
108
|
+
key
|
|
109
|
+
].join('.')] = createLazyLoader({
|
|
110
|
+
path,
|
|
111
|
+
ref: item,
|
|
112
|
+
key,
|
|
113
|
+
aggregate
|
|
114
|
+
});
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
46
117
|
if (isRouter(item)) {
|
|
47
118
|
aggregate[key] = step(item._def.record, [
|
|
48
119
|
...path,
|
|
@@ -75,6 +146,7 @@ const emptyRouter = {
|
|
|
75
146
|
_config: config,
|
|
76
147
|
router: true,
|
|
77
148
|
procedures,
|
|
149
|
+
lazy,
|
|
78
150
|
...emptyRouter,
|
|
79
151
|
record
|
|
80
152
|
};
|
|
@@ -92,11 +164,27 @@ const emptyRouter = {
|
|
|
92
164
|
function isProcedure(procedureOrRouter) {
|
|
93
165
|
return typeof procedureOrRouter === 'function';
|
|
94
166
|
}
|
|
167
|
+
async function getProcedureAtPath(_def, path) {
|
|
168
|
+
let procedure = _def.procedures[path];
|
|
169
|
+
while(!procedure){
|
|
170
|
+
const key = Object.keys(_def.lazy).find((key)=>path.startsWith(key));
|
|
171
|
+
// console.log(`found lazy: ${key ?? 'NOPE'} (fullPath: ${fullPath})`);
|
|
172
|
+
if (!key) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
// console.log('loading', key, '.......');
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
177
|
+
const lazyRouter = _def.lazy[key];
|
|
178
|
+
await lazyRouter.load();
|
|
179
|
+
procedure = _def.procedures[path];
|
|
180
|
+
}
|
|
181
|
+
return procedure;
|
|
182
|
+
}
|
|
95
183
|
/**
|
|
96
184
|
* @internal
|
|
97
|
-
*/ function callProcedure(opts) {
|
|
185
|
+
*/ async function callProcedure(opts) {
|
|
98
186
|
const { type, path } = opts;
|
|
99
|
-
const proc = opts.
|
|
187
|
+
const proc = await getProcedureAtPath(opts._def, path);
|
|
100
188
|
if (!proc || !isProcedure(proc) || proc._def.type !== type && !opts.allowMethodOverride) {
|
|
101
189
|
throw new TRPCError.TRPCError({
|
|
102
190
|
code: 'NOT_FOUND',
|
|
@@ -120,9 +208,15 @@ function createCallerFactory() {
|
|
|
120
208
|
if (path.length === 1 && path[0] === '_def') {
|
|
121
209
|
return _def;
|
|
122
210
|
}
|
|
123
|
-
const procedure = _def
|
|
211
|
+
const procedure = await getProcedureAtPath(_def, fullPath);
|
|
124
212
|
let ctx = undefined;
|
|
125
213
|
try {
|
|
214
|
+
if (!procedure) {
|
|
215
|
+
throw new TRPCError.TRPCError({
|
|
216
|
+
code: 'NOT_FOUND',
|
|
217
|
+
message: `No procedure found on path "${path}"`
|
|
218
|
+
});
|
|
219
|
+
}
|
|
126
220
|
ctx = utils.isFunction(ctxOrCallback) ? await Promise.resolve(ctxOrCallback()) : ctxOrCallback;
|
|
127
221
|
return await procedure({
|
|
128
222
|
path: fullPath,
|
|
@@ -137,7 +231,7 @@ function createCallerFactory() {
|
|
|
137
231
|
error: TRPCError.getTRPCErrorFromUnknown(cause),
|
|
138
232
|
input: args[0],
|
|
139
233
|
path: fullPath,
|
|
140
|
-
type: procedure
|
|
234
|
+
type: procedure?._def.type ?? 'unknown'
|
|
141
235
|
});
|
|
142
236
|
throw cause;
|
|
143
237
|
}
|
|
@@ -179,4 +273,6 @@ function mergeRouters(...routerList) {
|
|
|
179
273
|
exports.callProcedure = callProcedure;
|
|
180
274
|
exports.createCallerFactory = createCallerFactory;
|
|
181
275
|
exports.createRouterFactory = createRouterFactory;
|
|
276
|
+
exports.getProcedureAtPath = getProcedureAtPath;
|
|
277
|
+
exports.lazy = lazy;
|
|
182
278
|
exports.mergeRouters = mergeRouters;
|