@orpc/server 0.0.0-next.c3068b4 → 0.0.0-next.c40d0c9
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/README.md +131 -0
- package/dist/adapters/fetch/index.d.mts +54 -0
- package/dist/adapters/fetch/index.d.ts +54 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +22 -0
- package/dist/adapters/hono/index.d.ts +22 -0
- package/dist/{hono.js → adapters/hono/index.mjs} +12 -21
- package/dist/adapters/next/index.d.mts +29 -0
- package/dist/adapters/next/index.d.ts +29 -0
- package/dist/adapters/next/index.mjs +29 -0
- package/dist/adapters/node/index.d.mts +53 -0
- package/dist/adapters/node/index.d.ts +53 -0
- package/dist/adapters/node/index.mjs +85 -0
- package/dist/adapters/standard/index.d.mts +26 -0
- package/dist/adapters/standard/index.d.ts +26 -0
- package/dist/adapters/standard/index.mjs +6 -0
- package/dist/index.d.mts +291 -0
- package/dist/index.d.ts +291 -0
- package/dist/{index.js → index.mjs} +115 -156
- package/dist/plugins/index.d.mts +30 -0
- package/dist/plugins/index.d.ts +30 -0
- package/dist/plugins/index.mjs +107 -0
- package/dist/shared/server.B77ImKAP.d.mts +8 -0
- package/dist/shared/server.BHlRCrf_.d.ts +66 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.Cud5qk0c.d.ts +10 -0
- package/dist/shared/server.CzxlqYZL.d.mts +66 -0
- package/dist/shared/server.DCCkTG29.mjs +162 -0
- package/dist/shared/server.DGnN7q3R.d.mts +10 -0
- package/dist/shared/server.DLt5njUb.d.mts +143 -0
- package/dist/shared/server.DLt5njUb.d.ts +143 -0
- package/dist/shared/server.DUF89eb-.d.ts +8 -0
- package/dist/shared/server.Prfzu_zf.mjs +98 -0
- package/dist/shared/server.e3W6AG3-.mjs +370 -0
- package/package.json +32 -29
- package/dist/chunk-6RSW63UJ.js +0 -136
- package/dist/chunk-JHLUGXCM.js +0 -294
- package/dist/chunk-NOA3GBJQ.js +0 -380
- package/dist/fetch.js +0 -17
- package/dist/next.js +0 -38
- package/dist/node.js +0 -173
- package/dist/src/adapters/fetch/index.d.ts +0 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/fetch/types.d.ts +0 -13
- package/dist/src/adapters/fetch/utils.d.ts +0 -6
- package/dist/src/adapters/hono/index.d.ts +0 -3
- package/dist/src/adapters/hono/middleware.d.ts +0 -13
- package/dist/src/adapters/next/index.d.ts +0 -3
- package/dist/src/adapters/next/serve.d.ts +0 -20
- package/dist/src/adapters/node/index.d.ts +0 -4
- package/dist/src/adapters/node/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/node/types.d.ts +0 -21
- package/dist/src/adapters/node/utils.d.ts +0 -5
- package/dist/src/adapters/standard/handler.d.ts +0 -33
- package/dist/src/adapters/standard/index.d.ts +0 -7
- package/dist/src/adapters/standard/rpc-codec.d.ts +0 -15
- package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
- package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
- package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -16
- package/dist/src/adapters/standard/types.d.ts +0 -44
- package/dist/src/builder-variants.d.ts +0 -74
- package/dist/src/builder.d.ts +0 -57
- package/dist/src/config.d.ts +0 -6
- package/dist/src/context.d.ts +0 -9
- package/dist/src/hidden.d.ts +0 -8
- package/dist/src/implementer-procedure.d.ts +0 -30
- package/dist/src/implementer-variants.d.ts +0 -17
- package/dist/src/implementer.d.ts +0 -28
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-utils.d.ts +0 -6
- package/dist/src/lazy.d.ts +0 -22
- package/dist/src/middleware-decorated.d.ts +0 -10
- package/dist/src/middleware-utils.d.ts +0 -5
- package/dist/src/middleware.d.ts +0 -37
- package/dist/src/procedure-client.d.ts +0 -20
- package/dist/src/procedure-decorated.d.ts +0 -21
- package/dist/src/procedure-utils.d.ts +0 -17
- package/dist/src/procedure.d.ts +0 -29
- package/dist/src/router-accessible-lazy.d.ts +0 -8
- package/dist/src/router-client.d.ts +0 -22
- package/dist/src/router.d.ts +0 -29
- package/dist/src/utils.d.ts +0 -24
- package/dist/standard.js +0 -16
@@ -0,0 +1,98 @@
|
|
1
|
+
import { ORPCError } from '@orpc/client';
|
2
|
+
import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
3
|
+
import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.DCCkTG29.mjs';
|
4
|
+
import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
|
5
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
6
|
+
import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
|
7
|
+
|
8
|
+
class BodyLimitPlugin {
|
9
|
+
maxBodySize;
|
10
|
+
constructor(options) {
|
11
|
+
this.maxBodySize = options.maxBodySize;
|
12
|
+
}
|
13
|
+
initRuntimeAdapter(options) {
|
14
|
+
options.adapterInterceptors ??= [];
|
15
|
+
options.adapterInterceptors.push(async (options2) => {
|
16
|
+
if (!options2.request.body) {
|
17
|
+
return options2.next();
|
18
|
+
}
|
19
|
+
let currentBodySize = 0;
|
20
|
+
const rawReader = options2.request.body.getReader();
|
21
|
+
const reader = new ReadableStream({
|
22
|
+
start: async (controller) => {
|
23
|
+
try {
|
24
|
+
if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
|
25
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
while (true) {
|
29
|
+
const { done, value } = await rawReader.read();
|
30
|
+
if (done) {
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
currentBodySize += value.length;
|
34
|
+
if (currentBodySize > this.maxBodySize) {
|
35
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
36
|
+
break;
|
37
|
+
}
|
38
|
+
controller.enqueue(value);
|
39
|
+
}
|
40
|
+
} finally {
|
41
|
+
controller.close();
|
42
|
+
}
|
43
|
+
}
|
44
|
+
});
|
45
|
+
const requestInit = { body: reader, duplex: "half" };
|
46
|
+
return options2.next({
|
47
|
+
...options2,
|
48
|
+
request: new Request(options2.request, requestInit)
|
49
|
+
});
|
50
|
+
});
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
class FetchHandler {
|
55
|
+
constructor(standardHandler, options = {}) {
|
56
|
+
this.standardHandler = standardHandler;
|
57
|
+
for (const plugin of toArray(options.plugins)) {
|
58
|
+
plugin.initRuntimeAdapter?.(options);
|
59
|
+
}
|
60
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
61
|
+
this.toFetchResponseOptions = options;
|
62
|
+
}
|
63
|
+
toFetchResponseOptions;
|
64
|
+
adapterInterceptors;
|
65
|
+
async handle(request, ...rest) {
|
66
|
+
return intercept(
|
67
|
+
this.adapterInterceptors,
|
68
|
+
{
|
69
|
+
...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
|
70
|
+
request,
|
71
|
+
toFetchResponseOptions: this.toFetchResponseOptions
|
72
|
+
},
|
73
|
+
async ({ request: request2, toFetchResponseOptions, ...options }) => {
|
74
|
+
const standardRequest = toStandardLazyRequest(request2);
|
75
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
76
|
+
if (!result.matched) {
|
77
|
+
return result;
|
78
|
+
}
|
79
|
+
return {
|
80
|
+
matched: true,
|
81
|
+
response: toFetchResponse(result.response, toFetchResponseOptions)
|
82
|
+
};
|
83
|
+
}
|
84
|
+
);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
class RPCHandler extends FetchHandler {
|
89
|
+
constructor(router, options = {}) {
|
90
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
91
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
92
|
+
const matcher = new StandardRPCMatcher();
|
93
|
+
const codec = new StandardRPCCodec(serializer);
|
94
|
+
super(new StandardHandler(router, matcher, codec, options), options);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
export { BodyLimitPlugin as B, FetchHandler as F, RPCHandler as R };
|
@@ -0,0 +1,370 @@
|
|
1
|
+
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
|
+
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
3
|
+
import { value, intercept } from '@orpc/shared';
|
4
|
+
|
5
|
+
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
|
+
function lazy(loader, meta = {}) {
|
7
|
+
return {
|
8
|
+
[LAZY_SYMBOL]: {
|
9
|
+
loader,
|
10
|
+
meta
|
11
|
+
}
|
12
|
+
};
|
13
|
+
}
|
14
|
+
function isLazy(item) {
|
15
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
|
16
|
+
}
|
17
|
+
function getLazyMeta(lazied) {
|
18
|
+
return lazied[LAZY_SYMBOL].meta;
|
19
|
+
}
|
20
|
+
function unlazy(lazied) {
|
21
|
+
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
22
|
+
}
|
23
|
+
|
24
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
25
|
+
if (compare.length > middlewares.length) {
|
26
|
+
return false;
|
27
|
+
}
|
28
|
+
for (let i = 0; i < middlewares.length; i++) {
|
29
|
+
if (compare[i] === void 0) {
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
if (middlewares[i] !== compare[i]) {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return true;
|
37
|
+
}
|
38
|
+
function mergeMiddlewares(first, second, options) {
|
39
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
40
|
+
return second;
|
41
|
+
}
|
42
|
+
return [...first, ...second];
|
43
|
+
}
|
44
|
+
function addMiddleware(middlewares, addition) {
|
45
|
+
return [...middlewares, addition];
|
46
|
+
}
|
47
|
+
|
48
|
+
class Procedure {
|
49
|
+
"~orpc";
|
50
|
+
constructor(def) {
|
51
|
+
this["~orpc"] = def;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
function isProcedure(item) {
|
55
|
+
if (item instanceof Procedure) {
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
59
|
+
}
|
60
|
+
|
61
|
+
function mergeCurrentContext(context, other) {
|
62
|
+
return { ...context, ...other };
|
63
|
+
}
|
64
|
+
|
65
|
+
function createORPCErrorConstructorMap(errors) {
|
66
|
+
const proxy = new Proxy(errors, {
|
67
|
+
get(target, code) {
|
68
|
+
if (typeof code !== "string") {
|
69
|
+
return Reflect.get(target, code);
|
70
|
+
}
|
71
|
+
const item = (...[options]) => {
|
72
|
+
const config = errors[code];
|
73
|
+
return new ORPCError(code, {
|
74
|
+
defined: Boolean(config),
|
75
|
+
status: config?.status,
|
76
|
+
message: options?.message ?? config?.message,
|
77
|
+
data: options?.data,
|
78
|
+
cause: options?.cause
|
79
|
+
});
|
80
|
+
};
|
81
|
+
return item;
|
82
|
+
}
|
83
|
+
});
|
84
|
+
return proxy;
|
85
|
+
}
|
86
|
+
async function validateORPCError(map, error) {
|
87
|
+
const { code, status, message, data, cause, defined } = error;
|
88
|
+
const config = map?.[error.code];
|
89
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
90
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
91
|
+
}
|
92
|
+
if (!config.data) {
|
93
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
94
|
+
}
|
95
|
+
const validated = await config.data["~standard"].validate(error.data);
|
96
|
+
if (validated.issues) {
|
97
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
98
|
+
}
|
99
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
100
|
+
}
|
101
|
+
|
102
|
+
function middlewareOutputFn(output) {
|
103
|
+
return { output, context: {} };
|
104
|
+
}
|
105
|
+
|
106
|
+
function createProcedureClient(lazyableProcedure, ...[options]) {
|
107
|
+
return async (...[input, callerOptions]) => {
|
108
|
+
const path = options?.path ?? [];
|
109
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
110
|
+
const clientContext = callerOptions?.context ?? {};
|
111
|
+
const context = await value(options?.context ?? {}, clientContext);
|
112
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
113
|
+
try {
|
114
|
+
return await intercept(
|
115
|
+
options?.interceptors ?? [],
|
116
|
+
{
|
117
|
+
context,
|
118
|
+
input,
|
119
|
+
// input only optional when it undefinable so we can safely cast it
|
120
|
+
errors,
|
121
|
+
path,
|
122
|
+
procedure,
|
123
|
+
signal: callerOptions?.signal,
|
124
|
+
lastEventId: callerOptions?.lastEventId
|
125
|
+
},
|
126
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
127
|
+
);
|
128
|
+
} catch (e) {
|
129
|
+
if (!(e instanceof ORPCError)) {
|
130
|
+
throw e;
|
131
|
+
}
|
132
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
133
|
+
throw validated;
|
134
|
+
}
|
135
|
+
};
|
136
|
+
}
|
137
|
+
async function validateInput(procedure, input) {
|
138
|
+
const schema = procedure["~orpc"].inputSchema;
|
139
|
+
if (!schema) {
|
140
|
+
return input;
|
141
|
+
}
|
142
|
+
const result = await schema["~standard"].validate(input);
|
143
|
+
if (result.issues) {
|
144
|
+
throw new ORPCError("BAD_REQUEST", {
|
145
|
+
message: "Input validation failed",
|
146
|
+
data: {
|
147
|
+
issues: result.issues
|
148
|
+
},
|
149
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
150
|
+
});
|
151
|
+
}
|
152
|
+
return result.value;
|
153
|
+
}
|
154
|
+
async function validateOutput(procedure, output) {
|
155
|
+
const schema = procedure["~orpc"].outputSchema;
|
156
|
+
if (!schema) {
|
157
|
+
return output;
|
158
|
+
}
|
159
|
+
const result = await schema["~standard"].validate(output);
|
160
|
+
if (result.issues) {
|
161
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
162
|
+
message: "Output validation failed",
|
163
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
164
|
+
});
|
165
|
+
}
|
166
|
+
return result.value;
|
167
|
+
}
|
168
|
+
async function executeProcedureInternal(procedure, options) {
|
169
|
+
const middlewares = procedure["~orpc"].middlewares;
|
170
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
171
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
172
|
+
let currentIndex = 0;
|
173
|
+
let currentContext = options.context;
|
174
|
+
let currentInput = options.input;
|
175
|
+
const next = async (...[nextOptions]) => {
|
176
|
+
const index = currentIndex;
|
177
|
+
const midContext = nextOptions?.context ?? {};
|
178
|
+
currentIndex += 1;
|
179
|
+
currentContext = mergeCurrentContext(currentContext, midContext);
|
180
|
+
if (index === inputValidationIndex) {
|
181
|
+
currentInput = await validateInput(procedure, currentInput);
|
182
|
+
}
|
183
|
+
const mid = middlewares[index];
|
184
|
+
const result = mid ? {
|
185
|
+
context: midContext,
|
186
|
+
output: (await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn)).output
|
187
|
+
} : {
|
188
|
+
context: midContext,
|
189
|
+
output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput })
|
190
|
+
};
|
191
|
+
if (index === outputValidationIndex) {
|
192
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
193
|
+
return {
|
194
|
+
context: result.context,
|
195
|
+
output: validatedOutput
|
196
|
+
};
|
197
|
+
}
|
198
|
+
return result;
|
199
|
+
};
|
200
|
+
return (await next()).output;
|
201
|
+
}
|
202
|
+
|
203
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
204
|
+
function setHiddenRouterContract(router, contract) {
|
205
|
+
return new Proxy(router, {
|
206
|
+
get(target, key) {
|
207
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
208
|
+
return contract;
|
209
|
+
}
|
210
|
+
return Reflect.get(target, key);
|
211
|
+
}
|
212
|
+
});
|
213
|
+
}
|
214
|
+
function getHiddenRouterContract(router) {
|
215
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
216
|
+
}
|
217
|
+
|
218
|
+
function getRouter(router, path) {
|
219
|
+
let current = router;
|
220
|
+
for (let i = 0; i < path.length; i++) {
|
221
|
+
const segment = path[i];
|
222
|
+
if (!current) {
|
223
|
+
return void 0;
|
224
|
+
}
|
225
|
+
if (isProcedure(current)) {
|
226
|
+
return void 0;
|
227
|
+
}
|
228
|
+
if (!isLazy(current)) {
|
229
|
+
current = current[segment];
|
230
|
+
continue;
|
231
|
+
}
|
232
|
+
const lazied = current;
|
233
|
+
const rest = path.slice(i);
|
234
|
+
return lazy(async () => {
|
235
|
+
const unwrapped = await unlazy(lazied);
|
236
|
+
const next = getRouter(unwrapped.default, rest);
|
237
|
+
return unlazy(next);
|
238
|
+
}, getLazyMeta(lazied));
|
239
|
+
}
|
240
|
+
return current;
|
241
|
+
}
|
242
|
+
function createAccessibleLazyRouter(lazied) {
|
243
|
+
const recursive = new Proxy(lazied, {
|
244
|
+
get(target, key) {
|
245
|
+
if (typeof key !== "string") {
|
246
|
+
return Reflect.get(target, key);
|
247
|
+
}
|
248
|
+
const next = getRouter(lazied, [key]);
|
249
|
+
return createAccessibleLazyRouter(next);
|
250
|
+
}
|
251
|
+
});
|
252
|
+
return recursive;
|
253
|
+
}
|
254
|
+
function enhanceRouter(router, options) {
|
255
|
+
if (isLazy(router)) {
|
256
|
+
const laziedMeta = getLazyMeta(router);
|
257
|
+
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
258
|
+
const enhanced2 = lazy(async () => {
|
259
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
260
|
+
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
261
|
+
return unlazy(enhanced3);
|
262
|
+
}, {
|
263
|
+
...laziedMeta,
|
264
|
+
prefix: enhancedPrefix
|
265
|
+
});
|
266
|
+
const accessible = createAccessibleLazyRouter(enhanced2);
|
267
|
+
return accessible;
|
268
|
+
}
|
269
|
+
if (isProcedure(router)) {
|
270
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
271
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
272
|
+
const enhanced2 = new Procedure({
|
273
|
+
...router["~orpc"],
|
274
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
275
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
276
|
+
middlewares: newMiddlewares,
|
277
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
278
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
279
|
+
});
|
280
|
+
return enhanced2;
|
281
|
+
}
|
282
|
+
const enhanced = {};
|
283
|
+
for (const key in router) {
|
284
|
+
enhanced[key] = enhanceRouter(router[key], options);
|
285
|
+
}
|
286
|
+
return enhanced;
|
287
|
+
}
|
288
|
+
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
289
|
+
let currentRouter = options.router;
|
290
|
+
const hiddenContract = getHiddenRouterContract(options.router);
|
291
|
+
if (hiddenContract !== void 0) {
|
292
|
+
currentRouter = hiddenContract;
|
293
|
+
}
|
294
|
+
if (isLazy(currentRouter)) {
|
295
|
+
lazyOptions.push({
|
296
|
+
router: currentRouter,
|
297
|
+
path: options.path
|
298
|
+
});
|
299
|
+
} else if (isContractProcedure(currentRouter)) {
|
300
|
+
callback({
|
301
|
+
contract: currentRouter,
|
302
|
+
path: options.path
|
303
|
+
});
|
304
|
+
} else {
|
305
|
+
for (const key in currentRouter) {
|
306
|
+
traverseContractProcedures(
|
307
|
+
{
|
308
|
+
router: currentRouter[key],
|
309
|
+
path: [...options.path, key]
|
310
|
+
},
|
311
|
+
callback,
|
312
|
+
lazyOptions
|
313
|
+
);
|
314
|
+
}
|
315
|
+
}
|
316
|
+
return lazyOptions;
|
317
|
+
}
|
318
|
+
async function resolveContractProcedures(options, callback) {
|
319
|
+
const pending = [options];
|
320
|
+
for (const options2 of pending) {
|
321
|
+
const lazyOptions = traverseContractProcedures(options2, callback);
|
322
|
+
for (const options3 of lazyOptions) {
|
323
|
+
const { default: router } = await unlazy(options3.router);
|
324
|
+
pending.push({
|
325
|
+
router,
|
326
|
+
path: options3.path
|
327
|
+
});
|
328
|
+
}
|
329
|
+
}
|
330
|
+
}
|
331
|
+
async function unlazyRouter(router) {
|
332
|
+
if (isProcedure(router)) {
|
333
|
+
return router;
|
334
|
+
}
|
335
|
+
const unlazied = {};
|
336
|
+
for (const key in router) {
|
337
|
+
const item = router[key];
|
338
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
339
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
340
|
+
}
|
341
|
+
return unlazied;
|
342
|
+
}
|
343
|
+
|
344
|
+
function createAssertedLazyProcedure(lazied) {
|
345
|
+
const lazyProcedure = lazy(async () => {
|
346
|
+
const { default: maybeProcedure } = await unlazy(lazied);
|
347
|
+
if (!isProcedure(maybeProcedure)) {
|
348
|
+
throw new Error(`
|
349
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
350
|
+
This should be caught by TypeScript compilation.
|
351
|
+
Please report this issue if this makes you feel uncomfortable.
|
352
|
+
`);
|
353
|
+
}
|
354
|
+
return { default: maybeProcedure };
|
355
|
+
}, getLazyMeta(lazied));
|
356
|
+
return lazyProcedure;
|
357
|
+
}
|
358
|
+
function createContractedProcedure(procedure, contract) {
|
359
|
+
return new Procedure({
|
360
|
+
...procedure["~orpc"],
|
361
|
+
errorMap: contract["~orpc"].errorMap,
|
362
|
+
route: contract["~orpc"].route,
|
363
|
+
meta: contract["~orpc"].meta
|
364
|
+
});
|
365
|
+
}
|
366
|
+
function call(procedure, input, ...rest) {
|
367
|
+
return createProcedureClient(procedure, ...rest)(input);
|
368
|
+
}
|
369
|
+
|
370
|
+
export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/server",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.c40d0c9",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -15,42 +15,42 @@
|
|
15
15
|
],
|
16
16
|
"exports": {
|
17
17
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
18
|
+
"types": "./dist/index.d.mts",
|
19
|
+
"import": "./dist/index.mjs",
|
20
|
+
"default": "./dist/index.mjs"
|
21
|
+
},
|
22
|
+
"./plugins": {
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
21
26
|
},
|
22
27
|
"./standard": {
|
23
|
-
"types": "./dist/
|
24
|
-
"import": "./dist/standard.
|
25
|
-
"default": "./dist/standard.
|
28
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
29
|
+
"import": "./dist/adapters/standard/index.mjs",
|
30
|
+
"default": "./dist/adapters/standard/index.mjs"
|
26
31
|
},
|
27
32
|
"./fetch": {
|
28
|
-
"types": "./dist/
|
29
|
-
"import": "./dist/fetch.
|
30
|
-
"default": "./dist/fetch.
|
33
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
34
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
35
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
31
36
|
},
|
32
37
|
"./hono": {
|
33
|
-
"types": "./dist/
|
34
|
-
"import": "./dist/hono.
|
35
|
-
"default": "./dist/hono.
|
38
|
+
"types": "./dist/adapters/hono/index.d.mts",
|
39
|
+
"import": "./dist/adapters/hono/index.mjs",
|
40
|
+
"default": "./dist/adapters/hono/index.mjs"
|
36
41
|
},
|
37
42
|
"./next": {
|
38
|
-
"types": "./dist/
|
39
|
-
"import": "./dist/next.
|
40
|
-
"default": "./dist/next.
|
43
|
+
"types": "./dist/adapters/next/index.d.mts",
|
44
|
+
"import": "./dist/adapters/next/index.mjs",
|
45
|
+
"default": "./dist/adapters/next/index.mjs"
|
41
46
|
},
|
42
47
|
"./node": {
|
43
|
-
"types": "./dist/
|
44
|
-
"import": "./dist/node.
|
45
|
-
"default": "./dist/node.
|
46
|
-
},
|
47
|
-
"./🔒/*": {
|
48
|
-
"types": "./dist/src/*.d.ts"
|
48
|
+
"types": "./dist/adapters/node/index.d.mts",
|
49
|
+
"import": "./dist/adapters/node/index.mjs",
|
50
|
+
"default": "./dist/adapters/node/index.mjs"
|
49
51
|
}
|
50
52
|
},
|
51
53
|
"files": [
|
52
|
-
"!**/*.map",
|
53
|
-
"!**/*.tsbuildinfo",
|
54
54
|
"dist"
|
55
55
|
],
|
56
56
|
"peerDependencies": {
|
@@ -58,15 +58,18 @@
|
|
58
58
|
"next": ">=14.0.0"
|
59
59
|
},
|
60
60
|
"dependencies": {
|
61
|
-
"
|
62
|
-
"@orpc/
|
63
|
-
"@orpc/shared": "0.0.0-next.
|
61
|
+
"@orpc/client": "0.0.0-next.c40d0c9",
|
62
|
+
"@orpc/standard-server": "0.0.0-next.c40d0c9",
|
63
|
+
"@orpc/shared": "0.0.0-next.c40d0c9",
|
64
|
+
"@orpc/standard-server-fetch": "0.0.0-next.c40d0c9",
|
65
|
+
"@orpc/standard-server-node": "0.0.0-next.c40d0c9",
|
66
|
+
"@orpc/contract": "0.0.0-next.c40d0c9"
|
64
67
|
},
|
65
68
|
"devDependencies": {
|
66
|
-
"
|
69
|
+
"supertest": "^7.0.0"
|
67
70
|
},
|
68
71
|
"scripts": {
|
69
|
-
"build": "
|
72
|
+
"build": "unbuild",
|
70
73
|
"build:watch": "pnpm run build --watch",
|
71
74
|
"type:check": "tsc -b"
|
72
75
|
}
|
package/dist/chunk-6RSW63UJ.js
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
RPCCodec,
|
3
|
-
RPCMatcher,
|
4
|
-
StandardHandler
|
5
|
-
} from "./chunk-JHLUGXCM.js";
|
6
|
-
|
7
|
-
// src/adapters/fetch/utils.ts
|
8
|
-
import { once } from "@orpc/shared";
|
9
|
-
import cd from "content-disposition";
|
10
|
-
function fetchHeadersToStandardHeaders(headers) {
|
11
|
-
const standardHeaders = {};
|
12
|
-
for (const [key, value] of headers) {
|
13
|
-
if (Array.isArray(standardHeaders[key])) {
|
14
|
-
standardHeaders[key].push(value);
|
15
|
-
} else if (standardHeaders[key] !== void 0) {
|
16
|
-
standardHeaders[key] = [standardHeaders[key], value];
|
17
|
-
} else {
|
18
|
-
standardHeaders[key] = value;
|
19
|
-
}
|
20
|
-
}
|
21
|
-
return standardHeaders;
|
22
|
-
}
|
23
|
-
async function fetchReToStandardBody(re) {
|
24
|
-
if (!re.body) {
|
25
|
-
return void 0;
|
26
|
-
}
|
27
|
-
const contentDisposition = re.headers.get("content-disposition");
|
28
|
-
const fileName = contentDisposition ? cd.parse(contentDisposition).parameters.filename : void 0;
|
29
|
-
if (fileName) {
|
30
|
-
const blob2 = await re.blob();
|
31
|
-
return new File([blob2], fileName, {
|
32
|
-
type: blob2.type
|
33
|
-
});
|
34
|
-
}
|
35
|
-
const contentType = re.headers.get("content-type");
|
36
|
-
if (!contentType || contentType.startsWith("application/json")) {
|
37
|
-
const text = await re.text();
|
38
|
-
if (!text) {
|
39
|
-
return void 0;
|
40
|
-
}
|
41
|
-
return JSON.parse(text);
|
42
|
-
}
|
43
|
-
if (contentType.startsWith("multipart/form-data")) {
|
44
|
-
return await re.formData();
|
45
|
-
}
|
46
|
-
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
47
|
-
return new URLSearchParams(await re.text());
|
48
|
-
}
|
49
|
-
if (contentType.startsWith("text/")) {
|
50
|
-
return await re.text();
|
51
|
-
}
|
52
|
-
const blob = await re.blob();
|
53
|
-
return new File([blob], "blob", {
|
54
|
-
type: blob.type
|
55
|
-
});
|
56
|
-
}
|
57
|
-
function fetchRequestToStandardRequest(request) {
|
58
|
-
const url = new URL(request.url);
|
59
|
-
return {
|
60
|
-
raw: { request },
|
61
|
-
url,
|
62
|
-
signal: request.signal,
|
63
|
-
method: request.method,
|
64
|
-
body: once(() => {
|
65
|
-
return fetchReToStandardBody(request);
|
66
|
-
}),
|
67
|
-
get headers() {
|
68
|
-
const headers = fetchHeadersToStandardHeaders(request.headers);
|
69
|
-
Object.defineProperty(this, "headers", { value: headers, writable: true });
|
70
|
-
return headers;
|
71
|
-
},
|
72
|
-
set headers(value) {
|
73
|
-
Object.defineProperty(this, "headers", { value, writable: true });
|
74
|
-
}
|
75
|
-
};
|
76
|
-
}
|
77
|
-
function standardResponseToFetchHeaders(response) {
|
78
|
-
const fetchHeaders = new Headers();
|
79
|
-
for (const [key, value] of Object.entries(response.headers)) {
|
80
|
-
if (Array.isArray(value)) {
|
81
|
-
for (const v of value) {
|
82
|
-
fetchHeaders.append(key, v);
|
83
|
-
}
|
84
|
-
} else if (value !== void 0) {
|
85
|
-
fetchHeaders.append(key, value);
|
86
|
-
}
|
87
|
-
}
|
88
|
-
if (response.body instanceof Blob && !fetchHeaders.has("content-disposition")) {
|
89
|
-
fetchHeaders.set("content-disposition", cd(response.body instanceof File ? response.body.name : "blob"));
|
90
|
-
} else if (!(response.body instanceof Blob) && !(response.body instanceof URLSearchParams) && !(response.body instanceof FormData) && response.body !== void 0 && !fetchHeaders.has("content-type")) {
|
91
|
-
fetchHeaders.set("content-type", "application/json");
|
92
|
-
}
|
93
|
-
return fetchHeaders;
|
94
|
-
}
|
95
|
-
function standardBodyToFetchBody(body) {
|
96
|
-
if (body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams) {
|
97
|
-
return body;
|
98
|
-
}
|
99
|
-
return JSON.stringify(body);
|
100
|
-
}
|
101
|
-
function standardResponseToFetchResponse(response) {
|
102
|
-
return new Response(standardBodyToFetchBody(response.body), {
|
103
|
-
headers: standardResponseToFetchHeaders(response),
|
104
|
-
status: response.status
|
105
|
-
});
|
106
|
-
}
|
107
|
-
|
108
|
-
// src/adapters/fetch/rpc-handler.ts
|
109
|
-
var RPCHandler = class {
|
110
|
-
standardHandler;
|
111
|
-
constructor(router, options) {
|
112
|
-
const matcher = options?.matcher ?? new RPCMatcher();
|
113
|
-
const codec = options?.codec ?? new RPCCodec();
|
114
|
-
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
115
|
-
}
|
116
|
-
async handle(request, ...rest) {
|
117
|
-
const standardRequest = fetchRequestToStandardRequest(request);
|
118
|
-
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
119
|
-
if (!result.matched) {
|
120
|
-
return result;
|
121
|
-
}
|
122
|
-
return {
|
123
|
-
matched: true,
|
124
|
-
response: standardResponseToFetchResponse(result.response)
|
125
|
-
};
|
126
|
-
}
|
127
|
-
};
|
128
|
-
|
129
|
-
export {
|
130
|
-
fetchReToStandardBody,
|
131
|
-
fetchRequestToStandardRequest,
|
132
|
-
standardBodyToFetchBody,
|
133
|
-
standardResponseToFetchResponse,
|
134
|
-
RPCHandler
|
135
|
-
};
|
136
|
-
//# sourceMappingURL=chunk-6RSW63UJ.js.map
|