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