@orpc/server 0.0.0-next.fd1db03 → 0.0.0-next.ff5907c
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/chunk-47YYO5JS.js +32 -0
- package/dist/chunk-77FU7QSO.js +181 -0
- package/dist/chunk-MHVECKBC.js +421 -0
- package/dist/chunk-WQNNSBXW.js +120 -0
- package/dist/fetch.js +6 -320
- package/dist/hono.js +34 -0
- package/dist/index.js +280 -385
- package/dist/next.js +31 -0
- package/dist/node.js +31 -0
- package/dist/plugins.js +11 -0
- package/dist/src/adapters/fetch/index.d.ts +3 -0
- package/dist/src/adapters/fetch/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/fetch/types.d.ts +14 -0
- package/dist/src/adapters/hono/index.d.ts +3 -0
- package/dist/src/adapters/hono/middleware.d.ts +12 -0
- package/dist/src/adapters/next/index.d.ts +3 -0
- package/dist/src/adapters/next/serve.d.ts +19 -0
- package/dist/src/adapters/node/index.d.ts +3 -0
- package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/node/types.d.ts +22 -0
- package/dist/src/adapters/standard/handler.d.ts +53 -0
- package/dist/src/adapters/standard/index.d.ts +6 -0
- package/dist/src/adapters/standard/rpc-codec.d.ts +16 -0
- package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
- package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
- package/dist/src/adapters/standard/types.d.ts +21 -0
- package/dist/src/builder-variants.d.ts +75 -0
- package/dist/src/builder.d.ts +53 -30
- package/dist/src/config.d.ts +6 -0
- package/dist/src/context.d.ts +8 -0
- package/dist/src/error.d.ts +12 -0
- package/dist/src/hidden.d.ts +6 -4
- package/dist/src/implementer-procedure.d.ts +33 -0
- package/dist/src/implementer-variants.d.ts +18 -0
- package/dist/src/implementer.d.ts +29 -0
- package/dist/src/index.d.ts +13 -11
- package/dist/src/lazy-utils.d.ts +4 -2
- package/dist/src/lazy.d.ts +9 -5
- package/dist/src/middleware-decorated.d.ts +8 -5
- package/dist/src/middleware-utils.d.ts +5 -0
- package/dist/src/middleware.d.ts +30 -15
- package/dist/src/plugins/base.d.ts +11 -0
- package/dist/src/plugins/cors.d.ts +19 -0
- package/dist/src/plugins/index.d.ts +4 -0
- package/dist/src/plugins/response-headers.d.ts +10 -0
- package/dist/src/procedure-client.d.ts +22 -23
- package/dist/src/procedure-decorated.d.ts +21 -11
- package/dist/src/procedure-utils.d.ts +19 -0
- package/dist/src/procedure.d.ts +26 -18
- package/dist/src/router-accessible-lazy.d.ts +8 -0
- package/dist/src/router-client.d.ts +8 -21
- package/dist/src/router.d.ts +26 -12
- package/dist/src/utils.d.ts +23 -2
- package/dist/standard.js +13 -0
- package/package.json +39 -6
- package/dist/chunk-6A7XHEBH.js +0 -189
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/index.d.ts +0 -7
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -16
- package/dist/src/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/fetch/super-json.d.ts +0 -12
- package/dist/src/fetch/types.d.ts +0 -16
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/procedure-builder.d.ts +0 -22
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/types.d.ts +0 -12
@@ -0,0 +1,32 @@
|
|
1
|
+
import {
|
2
|
+
RPCCodec,
|
3
|
+
RPCMatcher,
|
4
|
+
StandardHandler
|
5
|
+
} from "./chunk-77FU7QSO.js";
|
6
|
+
|
7
|
+
// src/adapters/fetch/rpc-handler.ts
|
8
|
+
import { toFetchResponse, toStandardRequest } from "@orpc/server-standard-fetch";
|
9
|
+
var RPCHandler = class {
|
10
|
+
standardHandler;
|
11
|
+
constructor(router, options) {
|
12
|
+
const matcher = options?.matcher ?? new RPCMatcher();
|
13
|
+
const codec = options?.codec ?? new RPCCodec();
|
14
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
15
|
+
}
|
16
|
+
async handle(request, ...rest) {
|
17
|
+
const standardRequest = toStandardRequest(request);
|
18
|
+
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
19
|
+
if (!result.matched) {
|
20
|
+
return result;
|
21
|
+
}
|
22
|
+
return {
|
23
|
+
matched: true,
|
24
|
+
response: toFetchResponse(result.response)
|
25
|
+
};
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
export {
|
30
|
+
RPCHandler
|
31
|
+
};
|
32
|
+
//# sourceMappingURL=chunk-47YYO5JS.js.map
|
@@ -0,0 +1,181 @@
|
|
1
|
+
import {
|
2
|
+
convertPathToHttpPath,
|
3
|
+
createContractedProcedure,
|
4
|
+
createProcedureClient,
|
5
|
+
eachContractProcedure,
|
6
|
+
getRouterChild,
|
7
|
+
isProcedure,
|
8
|
+
unlazy
|
9
|
+
} from "./chunk-MHVECKBC.js";
|
10
|
+
import {
|
11
|
+
CompositePlugin
|
12
|
+
} from "./chunk-WQNNSBXW.js";
|
13
|
+
|
14
|
+
// src/adapters/standard/handler.ts
|
15
|
+
import { ORPCError, toORPCError } from "@orpc/client";
|
16
|
+
import { intercept, trim } from "@orpc/shared";
|
17
|
+
var StandardHandler = class {
|
18
|
+
constructor(router, matcher, codec, options = {}) {
|
19
|
+
this.matcher = matcher;
|
20
|
+
this.codec = codec;
|
21
|
+
this.options = options;
|
22
|
+
this.plugin = new CompositePlugin(options.plugins);
|
23
|
+
this.plugin.init(this.options);
|
24
|
+
this.matcher.init(router);
|
25
|
+
}
|
26
|
+
plugin;
|
27
|
+
handle(request, ...[options]) {
|
28
|
+
return intercept(
|
29
|
+
this.options.rootInterceptors ?? [],
|
30
|
+
{
|
31
|
+
request,
|
32
|
+
...options,
|
33
|
+
context: options?.context ?? {}
|
34
|
+
// context is optional only when all fields are optional so we can safely force it to have a context
|
35
|
+
},
|
36
|
+
async (interceptorOptions) => {
|
37
|
+
let isDecoding = false;
|
38
|
+
try {
|
39
|
+
return await intercept(
|
40
|
+
this.options.interceptors ?? [],
|
41
|
+
interceptorOptions,
|
42
|
+
async (interceptorOptions2) => {
|
43
|
+
const method = interceptorOptions2.request.method;
|
44
|
+
const url = interceptorOptions2.request.url;
|
45
|
+
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
46
|
+
const match = await this.matcher.match(method, pathname);
|
47
|
+
if (!match) {
|
48
|
+
return { matched: false, response: void 0 };
|
49
|
+
}
|
50
|
+
const client = createProcedureClient(match.procedure, {
|
51
|
+
context: interceptorOptions2.context,
|
52
|
+
path: match.path,
|
53
|
+
interceptors: this.options.clientInterceptors
|
54
|
+
});
|
55
|
+
isDecoding = true;
|
56
|
+
const input = await this.codec.decode(request, match.params, match.procedure);
|
57
|
+
isDecoding = false;
|
58
|
+
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
59
|
+
const output = await client(input, { signal: request.signal, lastEventId });
|
60
|
+
const response = this.codec.encode(output, match.procedure);
|
61
|
+
return {
|
62
|
+
matched: true,
|
63
|
+
response
|
64
|
+
};
|
65
|
+
}
|
66
|
+
);
|
67
|
+
} catch (e) {
|
68
|
+
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
69
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
70
|
+
cause: e
|
71
|
+
}) : toORPCError(e);
|
72
|
+
const response = this.codec.encodeError(error);
|
73
|
+
return {
|
74
|
+
matched: true,
|
75
|
+
response
|
76
|
+
};
|
77
|
+
}
|
78
|
+
}
|
79
|
+
);
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
// src/adapters/standard/rpc-codec.ts
|
84
|
+
import { RPCSerializer } from "@orpc/client/rpc";
|
85
|
+
var RPCCodec = class {
|
86
|
+
serializer;
|
87
|
+
constructor(options = {}) {
|
88
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
89
|
+
}
|
90
|
+
async decode(request, _params, _procedure) {
|
91
|
+
const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
92
|
+
return this.serializer.deserialize(serialized);
|
93
|
+
}
|
94
|
+
encode(output, _procedure) {
|
95
|
+
return {
|
96
|
+
status: 200,
|
97
|
+
headers: {},
|
98
|
+
body: this.serializer.serialize(output)
|
99
|
+
};
|
100
|
+
}
|
101
|
+
encodeError(error) {
|
102
|
+
return {
|
103
|
+
status: error.status,
|
104
|
+
headers: {},
|
105
|
+
body: this.serializer.serialize(error.toJSON())
|
106
|
+
};
|
107
|
+
}
|
108
|
+
};
|
109
|
+
|
110
|
+
// src/adapters/standard/rpc-matcher.ts
|
111
|
+
var RPCMatcher = class {
|
112
|
+
tree = {};
|
113
|
+
pendingRouters = [];
|
114
|
+
init(router, path = []) {
|
115
|
+
const laziedOptions = eachContractProcedure({
|
116
|
+
router,
|
117
|
+
path
|
118
|
+
}, ({ path: path2, contract }) => {
|
119
|
+
const httpPath = convertPathToHttpPath(path2);
|
120
|
+
if (isProcedure(contract)) {
|
121
|
+
this.tree[httpPath] = {
|
122
|
+
path: path2,
|
123
|
+
contract,
|
124
|
+
procedure: contract,
|
125
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
126
|
+
router
|
127
|
+
};
|
128
|
+
} else {
|
129
|
+
this.tree[httpPath] = {
|
130
|
+
path: path2,
|
131
|
+
contract,
|
132
|
+
procedure: void 0,
|
133
|
+
router
|
134
|
+
};
|
135
|
+
}
|
136
|
+
});
|
137
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
138
|
+
...option,
|
139
|
+
httpPathPrefix: convertPathToHttpPath(option.path)
|
140
|
+
})));
|
141
|
+
}
|
142
|
+
async match(_method, pathname) {
|
143
|
+
if (this.pendingRouters.length) {
|
144
|
+
const newPendingRouters = [];
|
145
|
+
for (const pendingRouter of this.pendingRouters) {
|
146
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
147
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
148
|
+
this.init(router, pendingRouter.path);
|
149
|
+
} else {
|
150
|
+
newPendingRouters.push(pendingRouter);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
this.pendingRouters = newPendingRouters;
|
154
|
+
}
|
155
|
+
const match = this.tree[pathname];
|
156
|
+
if (!match) {
|
157
|
+
return void 0;
|
158
|
+
}
|
159
|
+
if (!match.procedure) {
|
160
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
161
|
+
if (!isProcedure(maybeProcedure)) {
|
162
|
+
throw new Error(`
|
163
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
164
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
165
|
+
`);
|
166
|
+
}
|
167
|
+
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
168
|
+
}
|
169
|
+
return {
|
170
|
+
path: match.path,
|
171
|
+
procedure: match.procedure
|
172
|
+
};
|
173
|
+
}
|
174
|
+
};
|
175
|
+
|
176
|
+
export {
|
177
|
+
StandardHandler,
|
178
|
+
RPCCodec,
|
179
|
+
RPCMatcher
|
180
|
+
};
|
181
|
+
//# sourceMappingURL=chunk-77FU7QSO.js.map
|
@@ -0,0 +1,421 @@
|
|
1
|
+
// src/lazy.ts
|
2
|
+
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
3
|
+
function lazy(loader) {
|
4
|
+
return {
|
5
|
+
[LAZY_LOADER_SYMBOL]: loader
|
6
|
+
};
|
7
|
+
}
|
8
|
+
function isLazy(item) {
|
9
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
10
|
+
}
|
11
|
+
function unlazy(lazied) {
|
12
|
+
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
13
|
+
}
|
14
|
+
|
15
|
+
// src/procedure.ts
|
16
|
+
import { isContractProcedure } from "@orpc/contract";
|
17
|
+
var Procedure = class {
|
18
|
+
"~orpc";
|
19
|
+
constructor(def) {
|
20
|
+
this["~orpc"] = def;
|
21
|
+
}
|
22
|
+
};
|
23
|
+
function isProcedure(item) {
|
24
|
+
if (item instanceof Procedure) {
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
28
|
+
}
|
29
|
+
|
30
|
+
// src/lazy-utils.ts
|
31
|
+
function flatLazy(lazied) {
|
32
|
+
const flattenLoader = async () => {
|
33
|
+
let current = await unlazy(lazied);
|
34
|
+
while (true) {
|
35
|
+
if (!isLazy(current.default)) {
|
36
|
+
break;
|
37
|
+
}
|
38
|
+
current = await unlazy(current.default);
|
39
|
+
}
|
40
|
+
return current;
|
41
|
+
};
|
42
|
+
return lazy(flattenLoader);
|
43
|
+
}
|
44
|
+
function createLazyProcedureFormAnyLazy(lazied) {
|
45
|
+
const lazyProcedure = lazy(async () => {
|
46
|
+
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
47
|
+
if (!isProcedure(maybeProcedure)) {
|
48
|
+
throw new Error(`
|
49
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
50
|
+
This should be caught by TypeScript compilation.
|
51
|
+
Please report this issue if this makes you feel uncomfortable.
|
52
|
+
`);
|
53
|
+
}
|
54
|
+
return { default: maybeProcedure };
|
55
|
+
});
|
56
|
+
return lazyProcedure;
|
57
|
+
}
|
58
|
+
|
59
|
+
// src/middleware.ts
|
60
|
+
function middlewareOutputFn(output) {
|
61
|
+
return { output, context: {} };
|
62
|
+
}
|
63
|
+
|
64
|
+
// src/procedure-client.ts
|
65
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
66
|
+
import { ValidationError } from "@orpc/contract";
|
67
|
+
import { intercept, toError, value } from "@orpc/shared";
|
68
|
+
|
69
|
+
// src/error.ts
|
70
|
+
import { fallbackORPCErrorStatus, ORPCError } from "@orpc/client";
|
71
|
+
function createORPCErrorConstructorMap(errors) {
|
72
|
+
const proxy = new Proxy(errors, {
|
73
|
+
get(target, code) {
|
74
|
+
if (typeof code !== "string") {
|
75
|
+
return Reflect.get(target, code);
|
76
|
+
}
|
77
|
+
const item = (...[options]) => {
|
78
|
+
const config = errors[code];
|
79
|
+
return new ORPCError(code, {
|
80
|
+
defined: Boolean(config),
|
81
|
+
status: config?.status,
|
82
|
+
message: options?.message ?? config?.message,
|
83
|
+
data: options?.data,
|
84
|
+
cause: options?.cause
|
85
|
+
});
|
86
|
+
};
|
87
|
+
return item;
|
88
|
+
}
|
89
|
+
});
|
90
|
+
return proxy;
|
91
|
+
}
|
92
|
+
async function validateORPCError(map, error) {
|
93
|
+
const { code, status, message, data, cause, defined } = error;
|
94
|
+
const config = map?.[error.code];
|
95
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
96
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
97
|
+
}
|
98
|
+
if (!config.data) {
|
99
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
100
|
+
}
|
101
|
+
const validated = await config.data["~standard"].validate(error.data);
|
102
|
+
if (validated.issues) {
|
103
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
104
|
+
}
|
105
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
106
|
+
}
|
107
|
+
|
108
|
+
// src/procedure-client.ts
|
109
|
+
function createProcedureClient(lazyableProcedure, ...[options]) {
|
110
|
+
return async (...[input, callerOptions]) => {
|
111
|
+
const path = options?.path ?? [];
|
112
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
113
|
+
const clientContext = callerOptions?.context ?? {};
|
114
|
+
const context = await value(options?.context ?? {}, clientContext);
|
115
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
116
|
+
try {
|
117
|
+
return await intercept(
|
118
|
+
options?.interceptors ?? [],
|
119
|
+
{
|
120
|
+
context,
|
121
|
+
input,
|
122
|
+
// input only optional when it undefinable so we can safely cast it
|
123
|
+
errors,
|
124
|
+
path,
|
125
|
+
procedure,
|
126
|
+
signal: callerOptions?.signal,
|
127
|
+
lastEventId: callerOptions?.lastEventId
|
128
|
+
},
|
129
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
130
|
+
);
|
131
|
+
} catch (e) {
|
132
|
+
if (!(e instanceof ORPCError2)) {
|
133
|
+
throw toError(e);
|
134
|
+
}
|
135
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
136
|
+
throw validated;
|
137
|
+
}
|
138
|
+
};
|
139
|
+
}
|
140
|
+
async function validateInput(procedure, input) {
|
141
|
+
const schema = procedure["~orpc"].inputSchema;
|
142
|
+
if (!schema) {
|
143
|
+
return input;
|
144
|
+
}
|
145
|
+
const result = await schema["~standard"].validate(input);
|
146
|
+
if (result.issues) {
|
147
|
+
throw new ORPCError2("BAD_REQUEST", {
|
148
|
+
message: "Input validation failed",
|
149
|
+
data: {
|
150
|
+
issues: result.issues
|
151
|
+
},
|
152
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
153
|
+
});
|
154
|
+
}
|
155
|
+
return result.value;
|
156
|
+
}
|
157
|
+
async function validateOutput(procedure, output) {
|
158
|
+
const schema = procedure["~orpc"].outputSchema;
|
159
|
+
if (!schema) {
|
160
|
+
return output;
|
161
|
+
}
|
162
|
+
const result = await schema["~standard"].validate(output);
|
163
|
+
if (result.issues) {
|
164
|
+
throw new ORPCError2("INTERNAL_SERVER_ERROR", {
|
165
|
+
message: "Output validation failed",
|
166
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
167
|
+
});
|
168
|
+
}
|
169
|
+
return result.value;
|
170
|
+
}
|
171
|
+
async function executeProcedureInternal(procedure, options) {
|
172
|
+
const middlewares = procedure["~orpc"].middlewares;
|
173
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
174
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
175
|
+
let currentIndex = 0;
|
176
|
+
let currentContext = options.context;
|
177
|
+
let currentInput = options.input;
|
178
|
+
const next = async (...[nextOptions]) => {
|
179
|
+
const index = currentIndex;
|
180
|
+
currentIndex += 1;
|
181
|
+
currentContext = { ...currentContext, ...nextOptions?.context };
|
182
|
+
if (index === inputValidationIndex) {
|
183
|
+
currentInput = await validateInput(procedure, currentInput);
|
184
|
+
}
|
185
|
+
const mid = middlewares[index];
|
186
|
+
const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
|
187
|
+
if (index === outputValidationIndex) {
|
188
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
189
|
+
return {
|
190
|
+
...result,
|
191
|
+
output: validatedOutput
|
192
|
+
};
|
193
|
+
}
|
194
|
+
return result;
|
195
|
+
};
|
196
|
+
return (await next({})).output;
|
197
|
+
}
|
198
|
+
|
199
|
+
// src/hidden.ts
|
200
|
+
var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
201
|
+
function setRouterContract(obj, contract) {
|
202
|
+
return new Proxy(obj, {
|
203
|
+
get(target, key) {
|
204
|
+
if (key === ROUTER_CONTRACT_SYMBOL) {
|
205
|
+
return contract;
|
206
|
+
}
|
207
|
+
return Reflect.get(target, key);
|
208
|
+
}
|
209
|
+
});
|
210
|
+
}
|
211
|
+
function getRouterContract(obj) {
|
212
|
+
return obj[ROUTER_CONTRACT_SYMBOL];
|
213
|
+
}
|
214
|
+
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
215
|
+
function deepSetLazyRouterPrefix(router, prefix) {
|
216
|
+
return new Proxy(router, {
|
217
|
+
get(target, key) {
|
218
|
+
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
219
|
+
const val = Reflect.get(target, key);
|
220
|
+
if (isLazy(val)) {
|
221
|
+
return deepSetLazyRouterPrefix(val, prefix);
|
222
|
+
}
|
223
|
+
return val;
|
224
|
+
}
|
225
|
+
return prefix;
|
226
|
+
}
|
227
|
+
});
|
228
|
+
}
|
229
|
+
function getLazyRouterPrefix(obj) {
|
230
|
+
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
231
|
+
}
|
232
|
+
|
233
|
+
// src/router.ts
|
234
|
+
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
235
|
+
|
236
|
+
// src/middleware-utils.ts
|
237
|
+
function dedupeMiddlewares(compare, middlewares) {
|
238
|
+
let min = 0;
|
239
|
+
for (let i = 0; i < middlewares.length; i++) {
|
240
|
+
const index = compare.indexOf(middlewares[i], min);
|
241
|
+
if (index === -1) {
|
242
|
+
return middlewares.slice(i);
|
243
|
+
}
|
244
|
+
min = index + 1;
|
245
|
+
}
|
246
|
+
return [];
|
247
|
+
}
|
248
|
+
function mergeMiddlewares(first, second) {
|
249
|
+
return [...first, ...dedupeMiddlewares(first, second)];
|
250
|
+
}
|
251
|
+
function addMiddleware(middlewares, addition) {
|
252
|
+
return [...middlewares, addition];
|
253
|
+
}
|
254
|
+
|
255
|
+
// src/router.ts
|
256
|
+
function adaptRouter(router, options) {
|
257
|
+
if (isLazy(router)) {
|
258
|
+
const adapted2 = lazy(async () => {
|
259
|
+
const unlaziedRouter = (await unlazy(router)).default;
|
260
|
+
const adapted3 = adaptRouter(unlaziedRouter, options);
|
261
|
+
return { default: adapted3 };
|
262
|
+
});
|
263
|
+
const accessible = createAccessibleLazyRouter(adapted2);
|
264
|
+
const currentPrefix = getLazyRouterPrefix(router);
|
265
|
+
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
266
|
+
if (prefix) {
|
267
|
+
return deepSetLazyRouterPrefix(accessible, prefix);
|
268
|
+
}
|
269
|
+
return accessible;
|
270
|
+
}
|
271
|
+
if (isProcedure(router)) {
|
272
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
273
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
274
|
+
const adapted2 = new Procedure({
|
275
|
+
...router["~orpc"],
|
276
|
+
route: adaptRoute(router["~orpc"].route, options),
|
277
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
278
|
+
middlewares: newMiddlewares,
|
279
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
280
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
281
|
+
});
|
282
|
+
return adapted2;
|
283
|
+
}
|
284
|
+
const adapted = {};
|
285
|
+
for (const key in router) {
|
286
|
+
adapted[key] = adaptRouter(router[key], options);
|
287
|
+
}
|
288
|
+
return adapted;
|
289
|
+
}
|
290
|
+
function getRouterChild(router, ...path) {
|
291
|
+
let current = router;
|
292
|
+
for (let i = 0; i < path.length; i++) {
|
293
|
+
const segment = path[i];
|
294
|
+
if (!current) {
|
295
|
+
return void 0;
|
296
|
+
}
|
297
|
+
if (isProcedure(current)) {
|
298
|
+
return void 0;
|
299
|
+
}
|
300
|
+
if (!isLazy(current)) {
|
301
|
+
current = current[segment];
|
302
|
+
continue;
|
303
|
+
}
|
304
|
+
const lazied = current;
|
305
|
+
const rest = path.slice(i);
|
306
|
+
const newLazy = lazy(async () => {
|
307
|
+
const unwrapped = await unlazy(lazied);
|
308
|
+
if (!unwrapped.default) {
|
309
|
+
return unwrapped;
|
310
|
+
}
|
311
|
+
const next = getRouterChild(unwrapped.default, ...rest);
|
312
|
+
return { default: next };
|
313
|
+
});
|
314
|
+
return flatLazy(newLazy);
|
315
|
+
}
|
316
|
+
return current;
|
317
|
+
}
|
318
|
+
|
319
|
+
// src/router-accessible-lazy.ts
|
320
|
+
function createAccessibleLazyRouter(lazied) {
|
321
|
+
const flattenLazy = flatLazy(lazied);
|
322
|
+
const recursive = new Proxy(flattenLazy, {
|
323
|
+
get(target, key) {
|
324
|
+
if (typeof key !== "string") {
|
325
|
+
return Reflect.get(target, key);
|
326
|
+
}
|
327
|
+
const next = getRouterChild(flattenLazy, key);
|
328
|
+
return createAccessibleLazyRouter(next);
|
329
|
+
}
|
330
|
+
});
|
331
|
+
return recursive;
|
332
|
+
}
|
333
|
+
|
334
|
+
// src/utils.ts
|
335
|
+
import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
|
336
|
+
function eachContractProcedure(options, callback, laziedOptions = []) {
|
337
|
+
const hiddenContract = getRouterContract(options.router);
|
338
|
+
if (hiddenContract) {
|
339
|
+
return eachContractProcedure(
|
340
|
+
{
|
341
|
+
router: hiddenContract,
|
342
|
+
path: options.path
|
343
|
+
},
|
344
|
+
callback,
|
345
|
+
laziedOptions
|
346
|
+
);
|
347
|
+
}
|
348
|
+
if (isLazy(options.router)) {
|
349
|
+
laziedOptions.push({
|
350
|
+
lazied: options.router,
|
351
|
+
path: options.path
|
352
|
+
});
|
353
|
+
} else if (isContractProcedure2(options.router)) {
|
354
|
+
callback({
|
355
|
+
contract: options.router,
|
356
|
+
path: options.path
|
357
|
+
});
|
358
|
+
} else {
|
359
|
+
for (const key in options.router) {
|
360
|
+
eachContractProcedure(
|
361
|
+
{
|
362
|
+
router: options.router[key],
|
363
|
+
path: [...options.path, key]
|
364
|
+
},
|
365
|
+
callback,
|
366
|
+
laziedOptions
|
367
|
+
);
|
368
|
+
}
|
369
|
+
}
|
370
|
+
return laziedOptions;
|
371
|
+
}
|
372
|
+
async function eachAllContractProcedure(options, callback) {
|
373
|
+
const pending = [options];
|
374
|
+
for (const item of pending) {
|
375
|
+
const lazies = eachContractProcedure(item, callback);
|
376
|
+
for (const lazy2 of lazies) {
|
377
|
+
const { default: router } = await unlazy(lazy2.lazied);
|
378
|
+
pending.push({
|
379
|
+
path: lazy2.path,
|
380
|
+
router
|
381
|
+
});
|
382
|
+
}
|
383
|
+
}
|
384
|
+
}
|
385
|
+
function convertPathToHttpPath(path) {
|
386
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
387
|
+
}
|
388
|
+
function createContractedProcedure(contract, procedure) {
|
389
|
+
return new Procedure({
|
390
|
+
...procedure["~orpc"],
|
391
|
+
errorMap: contract["~orpc"].errorMap,
|
392
|
+
route: contract["~orpc"].route,
|
393
|
+
meta: contract["~orpc"].meta
|
394
|
+
});
|
395
|
+
}
|
396
|
+
|
397
|
+
export {
|
398
|
+
LAZY_LOADER_SYMBOL,
|
399
|
+
lazy,
|
400
|
+
isLazy,
|
401
|
+
unlazy,
|
402
|
+
Procedure,
|
403
|
+
isProcedure,
|
404
|
+
flatLazy,
|
405
|
+
createLazyProcedureFormAnyLazy,
|
406
|
+
addMiddleware,
|
407
|
+
middlewareOutputFn,
|
408
|
+
createProcedureClient,
|
409
|
+
setRouterContract,
|
410
|
+
getRouterContract,
|
411
|
+
deepSetLazyRouterPrefix,
|
412
|
+
getLazyRouterPrefix,
|
413
|
+
createAccessibleLazyRouter,
|
414
|
+
adaptRouter,
|
415
|
+
getRouterChild,
|
416
|
+
eachContractProcedure,
|
417
|
+
eachAllContractProcedure,
|
418
|
+
convertPathToHttpPath,
|
419
|
+
createContractedProcedure
|
420
|
+
};
|
421
|
+
//# sourceMappingURL=chunk-MHVECKBC.js.map
|