@orpc/server 0.0.0-next.93e7a4c → 0.0.0-next.9486ab5
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/adapters/hono/index.mjs +32 -0
- 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.mjs +363 -0
- 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.89QkKw3a.d.mts +10 -0
- package/dist/shared/server.B1S3zwuw.d.mts +8 -0
- package/dist/shared/server.BMkFIQUb.d.mts +66 -0
- package/dist/shared/server.BT0gne12.d.ts +8 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.CjWkNG6l.mjs +370 -0
- package/dist/shared/server.D0YVcfZk.d.mts +143 -0
- package/dist/shared/server.D0YVcfZk.d.ts +143 -0
- package/dist/shared/server.D9QduY95.mjs +161 -0
- package/dist/shared/server.Et1O6Bm7.mjs +98 -0
- package/dist/shared/server.taqJyaMn.d.ts +10 -0
- package/dist/shared/server.ywWqDZgA.d.ts +66 -0
- package/package.json +43 -15
- package/dist/chunk-FN62GL22.js +0 -182
- package/dist/fetch.js +0 -286
- package/dist/index.js +0 -518
- package/dist/src/builder.d.ts +0 -35
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/index.d.ts +0 -6
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -9
- 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/hidden.d.ts +0 -6
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/lazy-utils.d.ts +0 -4
- package/dist/src/lazy.d.ts +0 -18
- package/dist/src/middleware-decorated.d.ts +0 -8
- package/dist/src/middleware.d.ts +0 -23
- package/dist/src/procedure-builder.d.ts +0 -22
- package/dist/src/procedure-client.d.ts +0 -29
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/procedure.d.ts +0 -23
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -3
package/dist/index.mjs
ADDED
@@ -0,0 +1,363 @@
|
|
1
|
+
import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter } from '@orpc/contract';
|
2
|
+
export { ValidationError, eventIterator, type } from '@orpc/contract';
|
3
|
+
import { P as Procedure, b as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, i as isProcedure, d as isLazy, f as createAssertedLazyProcedure, g as getRouter } from './shared/server.CjWkNG6l.mjs';
|
4
|
+
export { L as LAZY_SYMBOL, p as call, r as createAccessibleLazyRouter, a as createContractedProcedure, h as createORPCErrorConstructorMap, q as getHiddenRouterContract, j as getLazyMeta, n as isStartWithMiddlewares, m as mergeCurrentContext, o as mergeMiddlewares, k as middlewareOutputFn, w as resolveContractProcedures, t as traverseContractProcedures, u as unlazy, x as unlazyRouter, v as validateORPCError } from './shared/server.CjWkNG6l.mjs';
|
5
|
+
import { toORPCError } from '@orpc/client';
|
6
|
+
export { ORPCError, isDefinedError, safe } from '@orpc/client';
|
7
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
8
|
+
export { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
9
|
+
|
10
|
+
const DEFAULT_CONFIG = {
|
11
|
+
initialInputValidationIndex: 0,
|
12
|
+
initialOutputValidationIndex: 0,
|
13
|
+
dedupeLeadingMiddlewares: true
|
14
|
+
};
|
15
|
+
function fallbackConfig(key, value) {
|
16
|
+
if (value === void 0) {
|
17
|
+
return DEFAULT_CONFIG[key];
|
18
|
+
}
|
19
|
+
return value;
|
20
|
+
}
|
21
|
+
|
22
|
+
function decorateMiddleware(middleware) {
|
23
|
+
const decorated = (...args) => middleware(...args);
|
24
|
+
decorated.mapInput = (mapInput) => {
|
25
|
+
const mapped = decorateMiddleware(
|
26
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
27
|
+
);
|
28
|
+
return mapped;
|
29
|
+
};
|
30
|
+
decorated.concat = (concatMiddleware, mapInput) => {
|
31
|
+
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
32
|
+
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
33
|
+
const merged = middleware({
|
34
|
+
...options,
|
35
|
+
next: (...[nextOptions1]) => mapped({
|
36
|
+
...options,
|
37
|
+
context: { ...options.context, ...nextOptions1?.context },
|
38
|
+
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
39
|
+
}, input, output, ...rest)
|
40
|
+
}, input, output, ...rest);
|
41
|
+
return merged;
|
42
|
+
});
|
43
|
+
return concatted;
|
44
|
+
};
|
45
|
+
return decorated;
|
46
|
+
}
|
47
|
+
|
48
|
+
function createActionableClient(client) {
|
49
|
+
const action = async (input) => {
|
50
|
+
try {
|
51
|
+
return [null, await client(input)];
|
52
|
+
} catch (error) {
|
53
|
+
return [toORPCError(error).toJSON(), void 0];
|
54
|
+
}
|
55
|
+
};
|
56
|
+
return action;
|
57
|
+
}
|
58
|
+
|
59
|
+
class DecoratedProcedure extends Procedure {
|
60
|
+
errors(errors) {
|
61
|
+
return new DecoratedProcedure({
|
62
|
+
...this["~orpc"],
|
63
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
64
|
+
});
|
65
|
+
}
|
66
|
+
meta(meta) {
|
67
|
+
return new DecoratedProcedure({
|
68
|
+
...this["~orpc"],
|
69
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
70
|
+
});
|
71
|
+
}
|
72
|
+
route(route) {
|
73
|
+
return new DecoratedProcedure({
|
74
|
+
...this["~orpc"],
|
75
|
+
route: mergeRoute(this["~orpc"].route, route)
|
76
|
+
});
|
77
|
+
}
|
78
|
+
use(middleware, mapInput) {
|
79
|
+
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
80
|
+
return new DecoratedProcedure({
|
81
|
+
...this["~orpc"],
|
82
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
83
|
+
});
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* Make this procedure callable (works like a function while still being a procedure).
|
87
|
+
*/
|
88
|
+
callable(...rest) {
|
89
|
+
const client = createProcedureClient(this, ...rest);
|
90
|
+
return new Proxy(client, {
|
91
|
+
get: (target, key) => {
|
92
|
+
return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
|
93
|
+
},
|
94
|
+
has: (target, key) => {
|
95
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
96
|
+
}
|
97
|
+
});
|
98
|
+
}
|
99
|
+
/**
|
100
|
+
* Make this procedure compatible with server action.
|
101
|
+
*/
|
102
|
+
actionable(...rest) {
|
103
|
+
const action = createActionableClient(createProcedureClient(this, ...rest));
|
104
|
+
return new Proxy(action, {
|
105
|
+
get: (target, key) => {
|
106
|
+
return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
|
107
|
+
},
|
108
|
+
has: (target, key) => {
|
109
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
110
|
+
}
|
111
|
+
});
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
class Builder {
|
116
|
+
"~orpc";
|
117
|
+
constructor(def) {
|
118
|
+
this["~orpc"] = def;
|
119
|
+
}
|
120
|
+
/**
|
121
|
+
* Reset config
|
122
|
+
*/
|
123
|
+
$config(config) {
|
124
|
+
const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
|
125
|
+
const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
|
126
|
+
return new Builder({
|
127
|
+
...this["~orpc"],
|
128
|
+
config,
|
129
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares),
|
130
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
|
131
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
|
132
|
+
});
|
133
|
+
}
|
134
|
+
/**
|
135
|
+
* Reset initial context
|
136
|
+
*/
|
137
|
+
$context() {
|
138
|
+
return new Builder({
|
139
|
+
...this["~orpc"],
|
140
|
+
middlewares: [],
|
141
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
|
142
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex)
|
143
|
+
});
|
144
|
+
}
|
145
|
+
/**
|
146
|
+
* Reset initial meta
|
147
|
+
*/
|
148
|
+
$meta(initialMeta) {
|
149
|
+
return new Builder({
|
150
|
+
...this["~orpc"],
|
151
|
+
meta: initialMeta
|
152
|
+
});
|
153
|
+
}
|
154
|
+
/**
|
155
|
+
* Reset initial route
|
156
|
+
*/
|
157
|
+
$route(initialRoute) {
|
158
|
+
return new Builder({
|
159
|
+
...this["~orpc"],
|
160
|
+
route: initialRoute
|
161
|
+
});
|
162
|
+
}
|
163
|
+
$input(initialInputSchema) {
|
164
|
+
return new Builder({
|
165
|
+
...this["~orpc"],
|
166
|
+
inputSchema: initialInputSchema
|
167
|
+
});
|
168
|
+
}
|
169
|
+
middleware(middleware) {
|
170
|
+
return decorateMiddleware(middleware);
|
171
|
+
}
|
172
|
+
errors(errors) {
|
173
|
+
return new Builder({
|
174
|
+
...this["~orpc"],
|
175
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
176
|
+
});
|
177
|
+
}
|
178
|
+
use(middleware, mapInput) {
|
179
|
+
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
180
|
+
return new Builder({
|
181
|
+
...this["~orpc"],
|
182
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
183
|
+
});
|
184
|
+
}
|
185
|
+
meta(meta) {
|
186
|
+
return new Builder({
|
187
|
+
...this["~orpc"],
|
188
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
189
|
+
});
|
190
|
+
}
|
191
|
+
route(route) {
|
192
|
+
return new Builder({
|
193
|
+
...this["~orpc"],
|
194
|
+
route: mergeRoute(this["~orpc"].route, route)
|
195
|
+
});
|
196
|
+
}
|
197
|
+
input(schema) {
|
198
|
+
return new Builder({
|
199
|
+
...this["~orpc"],
|
200
|
+
inputSchema: schema,
|
201
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
|
202
|
+
});
|
203
|
+
}
|
204
|
+
output(schema) {
|
205
|
+
return new Builder({
|
206
|
+
...this["~orpc"],
|
207
|
+
outputSchema: schema,
|
208
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
|
209
|
+
});
|
210
|
+
}
|
211
|
+
handler(handler) {
|
212
|
+
return new DecoratedProcedure({
|
213
|
+
...this["~orpc"],
|
214
|
+
handler
|
215
|
+
});
|
216
|
+
}
|
217
|
+
prefix(prefix) {
|
218
|
+
return new Builder({
|
219
|
+
...this["~orpc"],
|
220
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
221
|
+
});
|
222
|
+
}
|
223
|
+
tag(...tags) {
|
224
|
+
return new Builder({
|
225
|
+
...this["~orpc"],
|
226
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
227
|
+
});
|
228
|
+
}
|
229
|
+
router(router) {
|
230
|
+
return enhanceRouter(router, this["~orpc"]);
|
231
|
+
}
|
232
|
+
lazy(loader) {
|
233
|
+
return enhanceRouter(lazy(loader), this["~orpc"]);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
const os = new Builder({
|
237
|
+
config: {},
|
238
|
+
route: {},
|
239
|
+
meta: {},
|
240
|
+
errorMap: {},
|
241
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
|
242
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
|
243
|
+
middlewares: [],
|
244
|
+
dedupeLeadingMiddlewares: true
|
245
|
+
});
|
246
|
+
|
247
|
+
function implementerInternal(contract, config, middlewares) {
|
248
|
+
if (isContractProcedure(contract)) {
|
249
|
+
const impl2 = new Builder({
|
250
|
+
...contract["~orpc"],
|
251
|
+
config,
|
252
|
+
middlewares,
|
253
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
|
254
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length,
|
255
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
256
|
+
});
|
257
|
+
return impl2;
|
258
|
+
}
|
259
|
+
const impl = new Proxy(contract, {
|
260
|
+
get: (target, key) => {
|
261
|
+
if (typeof key !== "string") {
|
262
|
+
return Reflect.get(target, key);
|
263
|
+
}
|
264
|
+
let method;
|
265
|
+
if (key === "middleware") {
|
266
|
+
method = (mid) => decorateMiddleware(mid);
|
267
|
+
} else if (key === "use") {
|
268
|
+
method = (mid) => {
|
269
|
+
return implementerInternal(
|
270
|
+
contract,
|
271
|
+
config,
|
272
|
+
addMiddleware(middlewares, mid)
|
273
|
+
);
|
274
|
+
};
|
275
|
+
} else if (key === "router") {
|
276
|
+
method = (router) => {
|
277
|
+
const adapted = enhanceRouter(router, {
|
278
|
+
middlewares,
|
279
|
+
errorMap: {},
|
280
|
+
prefix: void 0,
|
281
|
+
tags: void 0,
|
282
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
283
|
+
});
|
284
|
+
return setHiddenRouterContract(adapted, contract);
|
285
|
+
};
|
286
|
+
} else if (key === "lazy") {
|
287
|
+
method = (loader) => {
|
288
|
+
const adapted = enhanceRouter(lazy(loader), {
|
289
|
+
middlewares,
|
290
|
+
errorMap: {},
|
291
|
+
prefix: void 0,
|
292
|
+
tags: void 0,
|
293
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
294
|
+
});
|
295
|
+
return setHiddenRouterContract(adapted, contract);
|
296
|
+
};
|
297
|
+
}
|
298
|
+
const next = getContractRouter(target, [key]);
|
299
|
+
if (!next) {
|
300
|
+
return method ?? next;
|
301
|
+
}
|
302
|
+
const nextImpl = implementerInternal(next, config, middlewares);
|
303
|
+
if (method) {
|
304
|
+
return new Proxy(method, {
|
305
|
+
get(_, key2) {
|
306
|
+
return Reflect.get(nextImpl, key2);
|
307
|
+
}
|
308
|
+
});
|
309
|
+
}
|
310
|
+
return nextImpl;
|
311
|
+
}
|
312
|
+
});
|
313
|
+
return impl;
|
314
|
+
}
|
315
|
+
function implement(contract, config = {}) {
|
316
|
+
const implInternal = implementerInternal(contract, config, []);
|
317
|
+
const impl = new Proxy(implInternal, {
|
318
|
+
get: (target, key) => {
|
319
|
+
let method;
|
320
|
+
if (key === "$context") {
|
321
|
+
method = () => impl;
|
322
|
+
} else if (key === "$config") {
|
323
|
+
method = (config2) => implement(contract, config2);
|
324
|
+
}
|
325
|
+
const next = Reflect.get(target, key);
|
326
|
+
if (!method || !next || typeof next !== "function" && typeof next !== "object") {
|
327
|
+
return method || next;
|
328
|
+
}
|
329
|
+
return new Proxy(method, {
|
330
|
+
get(_, key2) {
|
331
|
+
return Reflect.get(next, key2);
|
332
|
+
}
|
333
|
+
});
|
334
|
+
}
|
335
|
+
});
|
336
|
+
return impl;
|
337
|
+
}
|
338
|
+
|
339
|
+
function createRouterClient(router, ...[options]) {
|
340
|
+
if (isProcedure(router)) {
|
341
|
+
const caller = createProcedureClient(router, options);
|
342
|
+
return caller;
|
343
|
+
}
|
344
|
+
const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), options) : {};
|
345
|
+
const recursive = new Proxy(procedureCaller, {
|
346
|
+
get(target, key) {
|
347
|
+
if (typeof key !== "string") {
|
348
|
+
return Reflect.get(target, key);
|
349
|
+
}
|
350
|
+
const next = getRouter(router, [key]);
|
351
|
+
if (!next) {
|
352
|
+
return Reflect.get(target, key);
|
353
|
+
}
|
354
|
+
return createRouterClient(next, {
|
355
|
+
...options,
|
356
|
+
path: [...options?.path ?? [], key]
|
357
|
+
});
|
358
|
+
}
|
359
|
+
});
|
360
|
+
return recursive;
|
361
|
+
}
|
362
|
+
|
363
|
+
export { Builder, DecoratedProcedure, Procedure, addMiddleware, createActionableClient, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, isLazy, isProcedure, lazy, os, setHiddenRouterContract };
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { b as StandardHandlerInterceptorOptions, c as StandardHandlerPlugin, a as StandardHandlerOptions } from '../shared/server.BMkFIQUb.mjs';
|
2
|
+
import { Value } from '@orpc/shared';
|
3
|
+
import { C as Context } from '../shared/server.D0YVcfZk.mjs';
|
4
|
+
import '@orpc/client';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/standard-server';
|
7
|
+
|
8
|
+
interface CORSOptions<T extends Context> {
|
9
|
+
origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
10
|
+
timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
11
|
+
allowMethods?: readonly string[];
|
12
|
+
allowHeaders?: readonly string[];
|
13
|
+
maxAge?: number;
|
14
|
+
credentials?: boolean;
|
15
|
+
exposeHeaders?: readonly string[];
|
16
|
+
}
|
17
|
+
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
18
|
+
private readonly options;
|
19
|
+
constructor(options?: CORSOptions<T>);
|
20
|
+
init(options: StandardHandlerOptions<T>): void;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface ResponseHeadersPluginContext {
|
24
|
+
resHeaders?: Headers;
|
25
|
+
}
|
26
|
+
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
27
|
+
init(options: StandardHandlerOptions<T>): void;
|
28
|
+
}
|
29
|
+
|
30
|
+
export { type CORSOptions, CORSPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { b as StandardHandlerInterceptorOptions, c as StandardHandlerPlugin, a as StandardHandlerOptions } from '../shared/server.ywWqDZgA.js';
|
2
|
+
import { Value } from '@orpc/shared';
|
3
|
+
import { C as Context } from '../shared/server.D0YVcfZk.js';
|
4
|
+
import '@orpc/client';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/standard-server';
|
7
|
+
|
8
|
+
interface CORSOptions<T extends Context> {
|
9
|
+
origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
10
|
+
timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
11
|
+
allowMethods?: readonly string[];
|
12
|
+
allowHeaders?: readonly string[];
|
13
|
+
maxAge?: number;
|
14
|
+
credentials?: boolean;
|
15
|
+
exposeHeaders?: readonly string[];
|
16
|
+
}
|
17
|
+
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
18
|
+
private readonly options;
|
19
|
+
constructor(options?: CORSOptions<T>);
|
20
|
+
init(options: StandardHandlerOptions<T>): void;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface ResponseHeadersPluginContext {
|
24
|
+
resHeaders?: Headers;
|
25
|
+
}
|
26
|
+
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
27
|
+
init(options: StandardHandlerOptions<T>): void;
|
28
|
+
}
|
29
|
+
|
30
|
+
export { type CORSOptions, CORSPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
|
@@ -0,0 +1,107 @@
|
|
1
|
+
import { value } from '@orpc/shared';
|
2
|
+
|
3
|
+
class CORSPlugin {
|
4
|
+
options;
|
5
|
+
constructor(options = {}) {
|
6
|
+
const defaults = {
|
7
|
+
origin: (origin) => origin,
|
8
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
9
|
+
};
|
10
|
+
this.options = {
|
11
|
+
...defaults,
|
12
|
+
...options
|
13
|
+
};
|
14
|
+
}
|
15
|
+
init(options) {
|
16
|
+
options.rootInterceptors ??= [];
|
17
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
18
|
+
if (interceptorOptions.request.method === "OPTIONS") {
|
19
|
+
const resHeaders = {};
|
20
|
+
if (this.options.maxAge !== void 0) {
|
21
|
+
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
22
|
+
}
|
23
|
+
if (this.options.allowMethods?.length) {
|
24
|
+
resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
|
25
|
+
}
|
26
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
27
|
+
if (Array.isArray(allowHeaders) && allowHeaders.length) {
|
28
|
+
resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
|
29
|
+
} else if (typeof allowHeaders === "string") {
|
30
|
+
resHeaders["access-control-allow-headers"] = allowHeaders;
|
31
|
+
}
|
32
|
+
return {
|
33
|
+
matched: true,
|
34
|
+
response: {
|
35
|
+
status: 204,
|
36
|
+
headers: resHeaders,
|
37
|
+
body: void 0
|
38
|
+
}
|
39
|
+
};
|
40
|
+
}
|
41
|
+
return interceptorOptions.next();
|
42
|
+
});
|
43
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
44
|
+
const result = await interceptorOptions.next();
|
45
|
+
if (!result.matched) {
|
46
|
+
return result;
|
47
|
+
}
|
48
|
+
const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
|
49
|
+
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
50
|
+
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
51
|
+
if (allowedOriginArr.includes("*")) {
|
52
|
+
result.response.headers["access-control-allow-origin"] = "*";
|
53
|
+
} else {
|
54
|
+
if (allowedOriginArr.includes(origin)) {
|
55
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
56
|
+
}
|
57
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
58
|
+
}
|
59
|
+
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
60
|
+
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
61
|
+
if (allowedTimingOriginArr.includes("*")) {
|
62
|
+
result.response.headers["timing-allow-origin"] = "*";
|
63
|
+
} else if (allowedTimingOriginArr.includes(origin)) {
|
64
|
+
result.response.headers["timing-allow-origin"] = origin;
|
65
|
+
}
|
66
|
+
if (this.options.credentials) {
|
67
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
68
|
+
}
|
69
|
+
if (this.options.exposeHeaders?.length) {
|
70
|
+
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
|
71
|
+
}
|
72
|
+
return result;
|
73
|
+
});
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
class ResponseHeadersPlugin {
|
78
|
+
init(options) {
|
79
|
+
options.rootInterceptors ??= [];
|
80
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
81
|
+
const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
|
82
|
+
const result = await interceptorOptions.next({
|
83
|
+
...interceptorOptions,
|
84
|
+
context: {
|
85
|
+
...interceptorOptions.context,
|
86
|
+
resHeaders
|
87
|
+
}
|
88
|
+
});
|
89
|
+
if (!result.matched) {
|
90
|
+
return result;
|
91
|
+
}
|
92
|
+
const responseHeaders = result.response.headers;
|
93
|
+
for (const [key, value] of resHeaders) {
|
94
|
+
if (Array.isArray(responseHeaders[key])) {
|
95
|
+
responseHeaders[key].push(value);
|
96
|
+
} else if (responseHeaders[key] !== void 0) {
|
97
|
+
responseHeaders[key] = [responseHeaders[key], value];
|
98
|
+
} else {
|
99
|
+
responseHeaders[key] = value;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
return result;
|
103
|
+
});
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
export { CORSPlugin, ResponseHeadersPlugin };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.D0YVcfZk.mjs';
|
2
|
+
import { S as StandardHandleOptions } from './server.BMkFIQUb.mjs';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
|
10
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context } from './server.D0YVcfZk.mjs';
|
3
|
+
import { a as StandardHandlerOptions } from './server.BMkFIQUb.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.D0YVcfZk.mjs';
|
6
|
+
|
7
|
+
type StandardParams = Record<string, string>;
|
8
|
+
type StandardMatchResult = {
|
9
|
+
path: readonly string[];
|
10
|
+
procedure: AnyProcedure;
|
11
|
+
params?: StandardParams;
|
12
|
+
} | undefined;
|
13
|
+
interface StandardMatcher {
|
14
|
+
init(router: AnyRouter): void;
|
15
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
16
|
+
}
|
17
|
+
interface StandardCodec {
|
18
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
19
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
20
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface StandardHandleOptions<T extends Context> {
|
24
|
+
prefix?: HTTPPath;
|
25
|
+
context: T;
|
26
|
+
}
|
27
|
+
type StandardHandleResult = {
|
28
|
+
matched: true;
|
29
|
+
response: StandardResponse;
|
30
|
+
} | {
|
31
|
+
matched: false;
|
32
|
+
response: undefined;
|
33
|
+
};
|
34
|
+
interface StandardHandlerPlugin<TContext extends Context> {
|
35
|
+
init?(options: StandardHandlerOptions<TContext>): void;
|
36
|
+
}
|
37
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
38
|
+
request: StandardLazyRequest;
|
39
|
+
}
|
40
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
41
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
42
|
+
/**
|
43
|
+
* Interceptors at the request level, helpful when you want catch errors
|
44
|
+
*/
|
45
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
46
|
+
/**
|
47
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
48
|
+
*/
|
49
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
50
|
+
/**
|
51
|
+
*
|
52
|
+
* Interceptors for procedure client.
|
53
|
+
*/
|
54
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
|
55
|
+
}
|
56
|
+
declare class StandardHandler<T extends Context> {
|
57
|
+
private readonly matcher;
|
58
|
+
private readonly codec;
|
59
|
+
private readonly interceptors;
|
60
|
+
private readonly clientInterceptors;
|
61
|
+
private readonly rootInterceptors;
|
62
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
63
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
64
|
+
}
|
65
|
+
|
66
|
+
export { type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions as b, type StandardHandlerPlugin as c, type StandardCodec as d, type StandardParams as e, type StandardMatcher as f, type StandardMatchResult as g, type StandardHandleResult as h, StandardHandler as i };
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context } from './server.D0YVcfZk.js';
|
3
|
+
import { a as StandardHandlerOptions } from './server.ywWqDZgA.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|