@orpc/server 2.0.0-beta.35 → 2.0.0-beta.36
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 +29 -20
- package/dist/adapters/aws-lambda/index.mjs +2 -2
- package/dist/adapters/crossws/index.d.mts +2 -2
- package/dist/adapters/crossws/index.d.ts +2 -2
- package/dist/adapters/crossws/index.mjs +4 -3
- package/dist/adapters/fastify/index.mjs +2 -2
- package/dist/adapters/fetch/index.d.mts +1 -1
- package/dist/adapters/fetch/index.d.ts +1 -1
- package/dist/adapters/fetch/index.mjs +2 -2
- package/dist/adapters/message-port/index.d.mts +3 -3
- package/dist/adapters/message-port/index.d.ts +3 -3
- package/dist/adapters/message-port/index.mjs +4 -3
- package/dist/adapters/node/index.d.mts +1 -1
- package/dist/adapters/node/index.d.ts +1 -1
- package/dist/adapters/node/index.mjs +2 -2
- package/dist/adapters/standard/index.d.mts +91 -17
- package/dist/adapters/standard/index.d.ts +91 -17
- package/dist/adapters/standard/index.mjs +2 -2
- package/dist/adapters/standard-peer/index.d.mts +4 -9
- package/dist/adapters/standard-peer/index.d.ts +4 -9
- package/dist/adapters/standard-peer/index.mjs +10 -2
- package/dist/adapters/websocket/index.d.mts +3 -3
- package/dist/adapters/websocket/index.d.ts +3 -3
- package/dist/adapters/websocket/index.mjs +4 -3
- package/dist/index.mjs +4 -6
- package/dist/plugins/index.d.mts +1 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/shared/{server.R0uHWfBo.d.mts → server.B5K1CViq.d.mts} +20 -9
- package/dist/shared/{server.BYnwk7Xz.mjs → server.BynNaBBA.mjs} +7 -9
- package/dist/shared/{server.CAiLRqPf.d.mts → server.C7xllBNN.d.mts} +1 -1
- package/dist/shared/server.CFqSIFnT.d.mts +14 -0
- package/dist/shared/{server.DEeCcNd6.d.ts → server.Cxranw-8.d.ts} +20 -9
- package/dist/shared/{server.b2-J4t6T.d.ts → server.D0dr-F8u.d.ts} +1 -1
- package/dist/shared/server.RRVK1xTV.d.ts +14 -0
- package/dist/shared/{server.sNWd6DAG.mjs → server.vUIzQiOP.mjs} +72 -11
- package/package.json +4 -4
- package/dist/shared/server.BGMO5dst.d.ts +0 -91
- package/dist/shared/server.CX4vUnDk.mjs +0 -11
- package/dist/shared/server.DcHlvZFK.d.mts +0 -91
|
@@ -2,7 +2,7 @@ import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
|
|
|
2
2
|
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
3
3
|
import { Context } from '../../index.mjs';
|
|
4
4
|
import { StandardHandler, StandardHandlerOptions } from '../standard/index.mjs';
|
|
5
|
-
import {
|
|
5
|
+
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.mjs';
|
|
6
6
|
import { Router } from '../../index.mjs';
|
|
7
7
|
import { RPCHandlerCodecOptions } from '../standard/index.mjs';
|
|
8
8
|
import '@orpc/client';
|
|
@@ -44,7 +44,7 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
44
44
|
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
45
45
|
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
46
46
|
*/
|
|
47
|
-
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<
|
|
47
|
+
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<{
|
|
48
48
|
matched: boolean;
|
|
49
49
|
}>;
|
|
50
50
|
/**
|
|
@@ -58,7 +58,7 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
58
58
|
*
|
|
59
59
|
* Prefer this over calling `.message()` and `.close()` manually.
|
|
60
60
|
*/
|
|
61
|
-
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<
|
|
61
|
+
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): void;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {
|
|
@@ -2,7 +2,7 @@ import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
|
|
|
2
2
|
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
3
3
|
import { Context } from '../../index.js';
|
|
4
4
|
import { StandardHandler, StandardHandlerOptions } from '../standard/index.js';
|
|
5
|
-
import {
|
|
5
|
+
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
|
|
6
6
|
import { Router } from '../../index.js';
|
|
7
7
|
import { RPCHandlerCodecOptions } from '../standard/index.js';
|
|
8
8
|
import '@orpc/client';
|
|
@@ -44,7 +44,7 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
44
44
|
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
45
45
|
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
46
46
|
*/
|
|
47
|
-
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<
|
|
47
|
+
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<{
|
|
48
48
|
matched: boolean;
|
|
49
49
|
}>;
|
|
50
50
|
/**
|
|
@@ -58,7 +58,7 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
58
58
|
*
|
|
59
59
|
* Prefer this over calling `.message()` and `.close()` manually.
|
|
60
60
|
*/
|
|
61
|
-
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<
|
|
61
|
+
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): void;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { toStringOrBytes, resolveMaybeOptionalOptions, sequential, loadBytes } from '@orpc/shared';
|
|
2
2
|
import { ServerPeer, encodePeerMessage, decodePeerMessage, isClientPeerSendMessage } from '@standard-server/peer';
|
|
3
|
-
import {
|
|
4
|
-
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.
|
|
3
|
+
import { createStandardPeerRequestHandler } from '../standard-peer/index.mjs';
|
|
4
|
+
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.vUIzQiOP.mjs';
|
|
5
|
+
import '../../shared/server.W91HSRkE.mjs';
|
|
5
6
|
import '@orpc/client';
|
|
6
7
|
import '@standard-server/core';
|
|
7
8
|
import '../../shared/server.Djb_ebJT.mjs';
|
|
8
9
|
import '@orpc/contract';
|
|
9
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.BynNaBBA.mjs';
|
|
10
11
|
|
|
11
12
|
class WebSocketHandler {
|
|
12
13
|
constructor(handler, options = {}) {
|
package/dist/index.mjs
CHANGED
|
@@ -5,8 +5,8 @@ export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedData
|
|
|
5
5
|
import { L as Lazy, P as Procedure, c as createProcedureClient } from './shared/server.Djb_ebJT.mjs';
|
|
6
6
|
export { u as unlazy } from './shared/server.Djb_ebJT.mjs';
|
|
7
7
|
import { D as DecoratedProcedure } from './shared/server.24nXyhbl.mjs';
|
|
8
|
-
import { b as augmentRouter, d as augmentImplementedRouter, e as withHiddenRouterContract, f as createGuardedProcedureLazy, g as getRouter } from './shared/server.
|
|
9
|
-
export { a as DEFAULT_ERROR_STATUS, D as DEFAULT_SUCCESS_STATUS, h as call, c as createContractProcedure, i as getHiddenRouterContract, u as unlazyRouter, j as walkProcedureContractsAsync, w as walkProcedureContractsSync } from './shared/server.
|
|
8
|
+
import { b as augmentRouter, d as augmentImplementedRouter, e as withHiddenRouterContract, f as createGuardedProcedureLazy, g as getRouter } from './shared/server.BynNaBBA.mjs';
|
|
9
|
+
export { a as DEFAULT_ERROR_STATUS, D as DEFAULT_SUCCESS_STATUS, h as call, c as createContractProcedure, i as getHiddenRouterContract, u as unlazyRouter, j as walkProcedureContractsAsync, w as walkProcedureContractsSync } from './shared/server.BynNaBBA.mjs';
|
|
10
10
|
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
11
11
|
export { COMMON_ERROR_STATUS_MAP, ORPCError, RPCJsonSerializer, RPCSerializer, cloneORPCError, isDefinedError, safe, toORPCError } from '@orpc/client';
|
|
12
12
|
export { ErrorEvent, getEventMeta, unwrapEvent, withEventMeta } from '@standard-server/core';
|
|
@@ -388,13 +388,11 @@ function createRouterImplementerInternal(contract, config, middlewares) {
|
|
|
388
388
|
});
|
|
389
389
|
}
|
|
390
390
|
const implementer = {};
|
|
391
|
-
for (const key
|
|
392
|
-
const child = contract[key];
|
|
391
|
+
for (const [key, child] of Object.entries(contract)) {
|
|
393
392
|
implementer[key] = createRouterImplementerInternal(child, config, middlewares);
|
|
394
393
|
}
|
|
395
394
|
const shared = bindMethods(SharedRouterImplementer.create(contract, config, middlewares));
|
|
396
|
-
for (const key
|
|
397
|
-
const method = shared[key];
|
|
395
|
+
for (const [key, method] of Object.entries(shared)) {
|
|
398
396
|
if (key in implementer) {
|
|
399
397
|
const child = implementer[key];
|
|
400
398
|
implementer[key] = new Proxy(method, {
|
package/dist/plugins/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { Value, Promisable, ThrowableError } from '@orpc/shared';
|
|
|
2
2
|
import { StandardLazyRequest, StandardHeaders, StandardMethod } from '@standard-server/core';
|
|
3
3
|
import { Context } from '../index.mjs';
|
|
4
4
|
import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions, StandardHandlerInterceptorOptions } from '../adapters/standard/index.mjs';
|
|
5
|
-
export { R as RequestLimitHandlerPlugin, a as RequestLimitHandlerPluginOptions } from '../shared/server.
|
|
5
|
+
export { R as RequestLimitHandlerPlugin, a as RequestLimitHandlerPluginOptions } from '../shared/server.C7xllBNN.mjs';
|
|
6
6
|
import '@orpc/client';
|
|
7
7
|
import '@orpc/contract';
|
|
8
8
|
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Value, Promisable, ThrowableError } from '@orpc/shared';
|
|
|
2
2
|
import { StandardLazyRequest, StandardHeaders, StandardMethod } from '@standard-server/core';
|
|
3
3
|
import { Context } from '../index.js';
|
|
4
4
|
import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions, StandardHandlerInterceptorOptions } from '../adapters/standard/index.js';
|
|
5
|
-
export { R as RequestLimitHandlerPlugin, a as RequestLimitHandlerPluginOptions } from '../shared/server.
|
|
5
|
+
export { R as RequestLimitHandlerPlugin, a as RequestLimitHandlerPluginOptions } from '../shared/server.D0dr-F8u.js';
|
|
6
6
|
import '@orpc/client';
|
|
7
7
|
import '@orpc/contract';
|
|
8
8
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnyORPCError } from '@orpc/client';
|
|
2
|
-
import { OrderablePlugin,
|
|
2
|
+
import { OrderablePlugin, Value, Promisable, Interceptor } from '@orpc/shared';
|
|
3
3
|
import { StandardLazyRequest, StandardResponse } from '@standard-server/core';
|
|
4
4
|
import { C as Context, c as ProcedureClientInterceptor, A as AnyProcedure } from './server.BsujoJoF.mjs';
|
|
5
5
|
import { Schema, ErrorMap } from '@orpc/contract';
|
|
@@ -36,6 +36,17 @@ declare class CompositeStandardHandlerPlugin<T extends Context> implements Stand
|
|
|
36
36
|
|
|
37
37
|
interface StandardHandlerHandleOptions<T extends Context> {
|
|
38
38
|
prefix?: `/${string}` | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The initial context, or a function (sync or async) that receives the request and returns it.
|
|
41
|
+
* The function only runs once the request passes the prefix check.
|
|
42
|
+
*/
|
|
43
|
+
context: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `StandardHandlerHandleOptions` after the context has been resolved,
|
|
47
|
+
* this is what interceptors and codecs receive.
|
|
48
|
+
*/
|
|
49
|
+
interface ResolvedStandardHandlerHandleOptions<T extends Context> extends Omit<StandardHandlerHandleOptions<T>, 'context'> {
|
|
39
50
|
context: T;
|
|
40
51
|
}
|
|
41
52
|
type StandardHandlerHandleResult = {
|
|
@@ -45,11 +56,11 @@ type StandardHandlerHandleResult = {
|
|
|
45
56
|
matched: false;
|
|
46
57
|
response?: undefined;
|
|
47
58
|
};
|
|
48
|
-
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandlerCodecResolvedProcedure,
|
|
59
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandlerCodecResolvedProcedure, ResolvedStandardHandlerHandleOptions<T> {
|
|
49
60
|
request: StandardLazyRequest;
|
|
50
61
|
}
|
|
51
62
|
type StandardHandlerInterceptor<T extends Context> = Interceptor<StandardHandlerInterceptorOptions<T>, Promise<StandardResponse>>;
|
|
52
|
-
interface StandardHandlerRoutingInterceptorOptions<T extends Context> extends
|
|
63
|
+
interface StandardHandlerRoutingInterceptorOptions<T extends Context> extends ResolvedStandardHandlerHandleOptions<T> {
|
|
53
64
|
request: StandardLazyRequest;
|
|
54
65
|
}
|
|
55
66
|
type StandardHandlerRoutingInterceptor<T extends Context> = Interceptor<StandardHandlerRoutingInterceptorOptions<T>, Promise<StandardHandlerHandleResult>>;
|
|
@@ -82,7 +93,7 @@ declare class StandardHandler<T extends Context> {
|
|
|
82
93
|
private readonly interceptors;
|
|
83
94
|
private readonly clientInterceptors;
|
|
84
95
|
constructor(codec: StandardHandlerCodec<T>, options: StandardHandlerOptions<T>);
|
|
85
|
-
handle(request: StandardLazyRequest,
|
|
96
|
+
handle(request: StandardLazyRequest, options: StandardHandlerHandleOptions<T>): Promise<StandardHandlerHandleResult>;
|
|
86
97
|
}
|
|
87
98
|
declare class TracingHandlerPlugin implements StandardHandlerPlugin<any> {
|
|
88
99
|
name: string;
|
|
@@ -95,10 +106,10 @@ interface StandardHandlerCodecResolvedProcedure {
|
|
|
95
106
|
decodeInput: () => Promise<unknown>;
|
|
96
107
|
}
|
|
97
108
|
interface StandardHandlerCodec<T extends Context> {
|
|
98
|
-
resolveProcedure(request: StandardLazyRequest, options:
|
|
99
|
-
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options:
|
|
100
|
-
encodeError(error: AnyORPCError, procedure: AnyProcedure, path: string[], options:
|
|
109
|
+
resolveProcedure(request: StandardLazyRequest, options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
110
|
+
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
111
|
+
encodeError(error: AnyORPCError, procedure: AnyProcedure, path: string[], options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
101
112
|
}
|
|
102
113
|
|
|
103
|
-
export { CompositeStandardHandlerPlugin as C,
|
|
104
|
-
export type {
|
|
114
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, TracingHandlerPlugin as T };
|
|
115
|
+
export type { ResolvedStandardHandlerHandleOptions as R, StandardHandlerHandleOptions as a, StandardHandlerPlugin as b, StandardHandlerOptions as c, StandardHandlerRoutingInterceptorOptions as d, StandardHandlerInterceptorOptions as e, StandardHandlerCodec as f, StandardHandlerCodecResolvedProcedure as g, StandardHandlerHandleResult as h, StandardHandlerInterceptor as i, StandardHandlerRoutingInterceptor as j };
|
|
@@ -93,8 +93,8 @@ function augmentRouter(router, options) {
|
|
|
93
93
|
return router;
|
|
94
94
|
}
|
|
95
95
|
const enhanced = {};
|
|
96
|
-
for (const key
|
|
97
|
-
enhanced[key] = augmentRouter(
|
|
96
|
+
for (const [key, child] of Object.entries(router)) {
|
|
97
|
+
enhanced[key] = augmentRouter(child, options);
|
|
98
98
|
}
|
|
99
99
|
return enhanced;
|
|
100
100
|
}
|
|
@@ -125,8 +125,8 @@ function augmentImplementedRouter(router, options) {
|
|
|
125
125
|
return router;
|
|
126
126
|
}
|
|
127
127
|
const enhanced = {};
|
|
128
|
-
for (const key
|
|
129
|
-
enhanced[key] = augmentImplementedRouter(
|
|
128
|
+
for (const [key, child] of Object.entries(router)) {
|
|
129
|
+
enhanced[key] = augmentImplementedRouter(child, options);
|
|
130
130
|
}
|
|
131
131
|
return enhanced;
|
|
132
132
|
}
|
|
@@ -177,8 +177,7 @@ function walkProcedureContractsSyncInternal(router, callback, path, lazyResults)
|
|
|
177
177
|
if (!isTypescriptObject(router)) {
|
|
178
178
|
return;
|
|
179
179
|
}
|
|
180
|
-
for (const key
|
|
181
|
-
const value = router[key];
|
|
180
|
+
for (const [key, value] of Object.entries(router)) {
|
|
182
181
|
path.push(key);
|
|
183
182
|
if (value instanceof Lazy) {
|
|
184
183
|
lazyResults.push({ router: value, path: path.slice() });
|
|
@@ -200,8 +199,7 @@ async function walkProcedureContractsAsync(router, callback, path = []) {
|
|
|
200
199
|
if (!isTypescriptObject(router)) {
|
|
201
200
|
return;
|
|
202
201
|
}
|
|
203
|
-
for (const key
|
|
204
|
-
const value = router[key];
|
|
202
|
+
for (const [key, value] of Object.entries(router)) {
|
|
205
203
|
if (value instanceof Lazy) {
|
|
206
204
|
const { default: router2 } = await unlazy(value);
|
|
207
205
|
await walkProcedureContractsAsync(router2, callback, [...path, key]);
|
|
@@ -218,7 +216,7 @@ async function unlazyRouter(router) {
|
|
|
218
216
|
return router;
|
|
219
217
|
}
|
|
220
218
|
const unlazied = {};
|
|
221
|
-
for (const key
|
|
219
|
+
for (const key of Object.keys(router)) {
|
|
222
220
|
const item = router[key];
|
|
223
221
|
const { default: unlaziedRouter } = await unlazy(item);
|
|
224
222
|
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { C as Context } from './server.BsujoJoF.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { b as StandardHandlerPlugin, c as StandardHandlerOptions } from './server.B5K1CViq.mjs';
|
|
3
3
|
|
|
4
4
|
interface RequestLimitHandlerPluginOptions {
|
|
5
5
|
/**
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
2
|
+
import { StandardLazyRequest } from '@standard-server/core';
|
|
3
|
+
import { C as Context } from './server.BsujoJoF.mjs';
|
|
4
|
+
import { a as StandardHandlerHandleOptions } from './server.B5K1CViq.mjs';
|
|
5
|
+
|
|
6
|
+
type FriendlyStandardHandlerHandleOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (object extends T ? {
|
|
7
|
+
context?: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
8
|
+
} : {
|
|
9
|
+
context: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
10
|
+
});
|
|
11
|
+
declare function resolveFriendlyStandardHandlerHandleOptions<T extends Context>(options: FriendlyStandardHandlerHandleOptions<T>): StandardHandlerHandleOptions<T>;
|
|
12
|
+
|
|
13
|
+
export { resolveFriendlyStandardHandlerHandleOptions as r };
|
|
14
|
+
export type { FriendlyStandardHandlerHandleOptions as F };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnyORPCError } from '@orpc/client';
|
|
2
|
-
import { OrderablePlugin,
|
|
2
|
+
import { OrderablePlugin, Value, Promisable, Interceptor } from '@orpc/shared';
|
|
3
3
|
import { StandardLazyRequest, StandardResponse } from '@standard-server/core';
|
|
4
4
|
import { C as Context, c as ProcedureClientInterceptor, A as AnyProcedure } from './server.BsujoJoF.js';
|
|
5
5
|
import { Schema, ErrorMap } from '@orpc/contract';
|
|
@@ -36,6 +36,17 @@ declare class CompositeStandardHandlerPlugin<T extends Context> implements Stand
|
|
|
36
36
|
|
|
37
37
|
interface StandardHandlerHandleOptions<T extends Context> {
|
|
38
38
|
prefix?: `/${string}` | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The initial context, or a function (sync or async) that receives the request and returns it.
|
|
41
|
+
* The function only runs once the request passes the prefix check.
|
|
42
|
+
*/
|
|
43
|
+
context: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `StandardHandlerHandleOptions` after the context has been resolved,
|
|
47
|
+
* this is what interceptors and codecs receive.
|
|
48
|
+
*/
|
|
49
|
+
interface ResolvedStandardHandlerHandleOptions<T extends Context> extends Omit<StandardHandlerHandleOptions<T>, 'context'> {
|
|
39
50
|
context: T;
|
|
40
51
|
}
|
|
41
52
|
type StandardHandlerHandleResult = {
|
|
@@ -45,11 +56,11 @@ type StandardHandlerHandleResult = {
|
|
|
45
56
|
matched: false;
|
|
46
57
|
response?: undefined;
|
|
47
58
|
};
|
|
48
|
-
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandlerCodecResolvedProcedure,
|
|
59
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandlerCodecResolvedProcedure, ResolvedStandardHandlerHandleOptions<T> {
|
|
49
60
|
request: StandardLazyRequest;
|
|
50
61
|
}
|
|
51
62
|
type StandardHandlerInterceptor<T extends Context> = Interceptor<StandardHandlerInterceptorOptions<T>, Promise<StandardResponse>>;
|
|
52
|
-
interface StandardHandlerRoutingInterceptorOptions<T extends Context> extends
|
|
63
|
+
interface StandardHandlerRoutingInterceptorOptions<T extends Context> extends ResolvedStandardHandlerHandleOptions<T> {
|
|
53
64
|
request: StandardLazyRequest;
|
|
54
65
|
}
|
|
55
66
|
type StandardHandlerRoutingInterceptor<T extends Context> = Interceptor<StandardHandlerRoutingInterceptorOptions<T>, Promise<StandardHandlerHandleResult>>;
|
|
@@ -82,7 +93,7 @@ declare class StandardHandler<T extends Context> {
|
|
|
82
93
|
private readonly interceptors;
|
|
83
94
|
private readonly clientInterceptors;
|
|
84
95
|
constructor(codec: StandardHandlerCodec<T>, options: StandardHandlerOptions<T>);
|
|
85
|
-
handle(request: StandardLazyRequest,
|
|
96
|
+
handle(request: StandardLazyRequest, options: StandardHandlerHandleOptions<T>): Promise<StandardHandlerHandleResult>;
|
|
86
97
|
}
|
|
87
98
|
declare class TracingHandlerPlugin implements StandardHandlerPlugin<any> {
|
|
88
99
|
name: string;
|
|
@@ -95,10 +106,10 @@ interface StandardHandlerCodecResolvedProcedure {
|
|
|
95
106
|
decodeInput: () => Promise<unknown>;
|
|
96
107
|
}
|
|
97
108
|
interface StandardHandlerCodec<T extends Context> {
|
|
98
|
-
resolveProcedure(request: StandardLazyRequest, options:
|
|
99
|
-
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options:
|
|
100
|
-
encodeError(error: AnyORPCError, procedure: AnyProcedure, path: string[], options:
|
|
109
|
+
resolveProcedure(request: StandardLazyRequest, options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
110
|
+
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
111
|
+
encodeError(error: AnyORPCError, procedure: AnyProcedure, path: string[], options: ResolvedStandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
101
112
|
}
|
|
102
113
|
|
|
103
|
-
export { CompositeStandardHandlerPlugin as C,
|
|
104
|
-
export type {
|
|
114
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, TracingHandlerPlugin as T };
|
|
115
|
+
export type { ResolvedStandardHandlerHandleOptions as R, StandardHandlerHandleOptions as a, StandardHandlerPlugin as b, StandardHandlerOptions as c, StandardHandlerRoutingInterceptorOptions as d, StandardHandlerInterceptorOptions as e, StandardHandlerCodec as f, StandardHandlerCodecResolvedProcedure as g, StandardHandlerHandleResult as h, StandardHandlerInterceptor as i, StandardHandlerRoutingInterceptor as j };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { C as Context } from './server.BsujoJoF.js';
|
|
2
|
-
import {
|
|
2
|
+
import { b as StandardHandlerPlugin, c as StandardHandlerOptions } from './server.Cxranw-8.js';
|
|
3
3
|
|
|
4
4
|
interface RequestLimitHandlerPluginOptions {
|
|
5
5
|
/**
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
2
|
+
import { StandardLazyRequest } from '@standard-server/core';
|
|
3
|
+
import { C as Context } from './server.BsujoJoF.js';
|
|
4
|
+
import { a as StandardHandlerHandleOptions } from './server.Cxranw-8.js';
|
|
5
|
+
|
|
6
|
+
type FriendlyStandardHandlerHandleOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (object extends T ? {
|
|
7
|
+
context?: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
8
|
+
} : {
|
|
9
|
+
context: Value<Promisable<T>, [request: StandardLazyRequest]>;
|
|
10
|
+
});
|
|
11
|
+
declare function resolveFriendlyStandardHandlerHandleOptions<T extends Context>(options: FriendlyStandardHandlerHandleOptions<T>): StandardHandlerHandleOptions<T>;
|
|
12
|
+
|
|
13
|
+
export { resolveFriendlyStandardHandlerHandleOptions as r };
|
|
14
|
+
export type { FriendlyStandardHandlerHandleOptions as F };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ORPCError, toORPCError, RPCSerializer, COMMON_ERROR_STATUS_MAP } from '@orpc/client';
|
|
2
|
-
import { sortPlugins, toArray, matchesHttpPathPrefix, intercept, getTracer, runWithSpan, ORPC_NAME, isAsyncIteratorObject, override, traceAsyncIterator, traceReadableStream, recordSpanError, pathToHttpPath, normalizeHttpPath,
|
|
3
|
-
import { flattenStandardHeader, parseStandardUrl } from '@standard-server/core';
|
|
2
|
+
import { sortPlugins, toArray, matchesHttpPathPrefix, value, intercept, getTracer, runWithSpan, ORPC_NAME, isAsyncIteratorObject, override, traceAsyncIterator, traceReadableStream, recordSpanError, toTracingException, wrapAsyncIterator, wrapReadableStream, pathToHttpPath, normalizeHttpPath, parseEmptyableJSON, getOwn } from '@orpc/shared';
|
|
3
|
+
import { flattenStandardHeader, parseStandardUrl, ErrorEvent } from '@standard-server/core';
|
|
4
4
|
import { c as createProcedureClient, u as unlazy, P as Procedure } from './server.Djb_ebJT.mjs';
|
|
5
|
-
import { w as walkProcedureContractsSync, g as getRouter, c as createContractProcedure, D as DEFAULT_SUCCESS_STATUS, a as DEFAULT_ERROR_STATUS } from './server.
|
|
5
|
+
import { w as walkProcedureContractsSync, g as getRouter, c as createContractProcedure, D as DEFAULT_SUCCESS_STATUS, a as DEFAULT_ERROR_STATUS } from './server.BynNaBBA.mjs';
|
|
6
6
|
|
|
7
7
|
class CompositeStandardHandlerPlugin {
|
|
8
8
|
name = "~composite";
|
|
@@ -24,8 +24,8 @@ class StandardHandler {
|
|
|
24
24
|
constructor(codec, options) {
|
|
25
25
|
this.codec = codec;
|
|
26
26
|
options = new CompositeStandardHandlerPlugin([
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
...toArray(options.plugins),
|
|
28
|
+
new TracingHandlerPlugin()
|
|
29
29
|
]).init(options);
|
|
30
30
|
this.routingInterceptors = options.routingInterceptors;
|
|
31
31
|
this.interceptors = options.interceptors;
|
|
@@ -34,10 +34,12 @@ class StandardHandler {
|
|
|
34
34
|
routingInterceptors;
|
|
35
35
|
interceptors;
|
|
36
36
|
clientInterceptors;
|
|
37
|
-
async handle(request,
|
|
37
|
+
async handle(request, options) {
|
|
38
|
+
const { prefix } = options;
|
|
38
39
|
if (prefix && !matchesHttpPathPrefix(request.url, prefix)) {
|
|
39
40
|
return { matched: false, response: void 0 };
|
|
40
41
|
}
|
|
42
|
+
const context = await value(options.context, request);
|
|
41
43
|
return intercept(
|
|
42
44
|
this.routingInterceptors,
|
|
43
45
|
{ context, prefix, request },
|
|
@@ -108,12 +110,71 @@ class TracingHandlerPlugin {
|
|
|
108
110
|
// Should be placed before user-provided interceptors to help them access the current active span.
|
|
109
111
|
async ({ next, request }) => {
|
|
110
112
|
const tracer = getTracer();
|
|
111
|
-
|
|
113
|
+
if (!tracer) {
|
|
114
|
+
return next();
|
|
115
|
+
}
|
|
116
|
+
const parent = tracer.extract?.(request.headers);
|
|
112
117
|
const [pathname] = parseStandardUrl(request.url);
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
const span = tracer.startSpan(`${request.method} ${pathname}`, parent);
|
|
119
|
+
let result;
|
|
120
|
+
try {
|
|
121
|
+
result = await tracer.withActiveSpan(span, next);
|
|
122
|
+
} catch (e) {
|
|
123
|
+
span.recordException("error", toTracingException(e));
|
|
124
|
+
span.end();
|
|
125
|
+
throw e;
|
|
126
|
+
}
|
|
127
|
+
if (!result.matched) {
|
|
128
|
+
span.end();
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
const body = result.response.body;
|
|
132
|
+
if (isAsyncIteratorObject(body)) {
|
|
133
|
+
return {
|
|
134
|
+
...result,
|
|
135
|
+
response: {
|
|
136
|
+
...result.response,
|
|
137
|
+
/**
|
|
138
|
+
* @remarks
|
|
139
|
+
* **Warning**: Remember use `override` for remaining special properties
|
|
140
|
+
*/
|
|
141
|
+
body: override(body, wrapAsyncIterator(body, {
|
|
142
|
+
runWith: (fn) => tracer.withActiveSpan(span, fn),
|
|
143
|
+
onError(error) {
|
|
144
|
+
if (!(error instanceof ErrorEvent)) {
|
|
145
|
+
span.recordException("error", toTracingException(error));
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
onFinish() {
|
|
149
|
+
span.end();
|
|
150
|
+
}
|
|
151
|
+
}))
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (body instanceof ReadableStream) {
|
|
156
|
+
return {
|
|
157
|
+
...result,
|
|
158
|
+
response: {
|
|
159
|
+
...result.response,
|
|
160
|
+
/**
|
|
161
|
+
* @remarks
|
|
162
|
+
* **Warning**: Remember use `override` for remaining special properties
|
|
163
|
+
*/
|
|
164
|
+
body: override(body, wrapReadableStream(body, {
|
|
165
|
+
runWith: (fn) => tracer.withActiveSpan(span, fn),
|
|
166
|
+
onError(error) {
|
|
167
|
+
span.recordException("error", toTracingException(error));
|
|
168
|
+
},
|
|
169
|
+
onFinish() {
|
|
170
|
+
span.end();
|
|
171
|
+
}
|
|
172
|
+
}))
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
span.end();
|
|
177
|
+
return result;
|
|
117
178
|
},
|
|
118
179
|
...toArray(options.routingInterceptors)
|
|
119
180
|
]
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.36",
|
|
5
5
|
"description": "Build typesafe APIs with oRPC: RPC and OpenAPI support, middleware, and adapters for Node.js, Bun, Deno, Fetch, Fastify, WebSocket, AWS Lambda, and more",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -117,9 +117,9 @@
|
|
|
117
117
|
}
|
|
118
118
|
},
|
|
119
119
|
"dependencies": {
|
|
120
|
-
"@orpc/client": "2.0.0-beta.
|
|
121
|
-
"@orpc/contract": "2.0.0-beta.
|
|
122
|
-
"@orpc/shared": "2.0.0-beta.
|
|
120
|
+
"@orpc/client": "2.0.0-beta.36",
|
|
121
|
+
"@orpc/contract": "2.0.0-beta.36",
|
|
122
|
+
"@orpc/shared": "2.0.0-beta.36",
|
|
123
123
|
"@standard-server/aws-lambda": "~0.9.0",
|
|
124
124
|
"@standard-server/core": "~0.9.0",
|
|
125
125
|
"@standard-server/fastify": "~0.9.0",
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { RPCSerializer, AnyORPCError } from '@orpc/client';
|
|
2
|
-
import { Value, Public, Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardMethod, StandardLazyRequest, StandardResponse } from '@standard-server/core';
|
|
4
|
-
import { A as AnyProcedure, C as Context } from './server.BsujoJoF.js';
|
|
5
|
-
import { A as AnyRouter } from './server.CRwPbhLO.js';
|
|
6
|
-
import { AnyProcedureContract } from '@orpc/contract';
|
|
7
|
-
import { c as StandardHandlerHandleOptions, d as StandardHandlerCodec, e as StandardHandlerCodecResolvedProcedure } from './server.DEeCcNd6.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Methods that can invoke procedures by default. Browsers cannot trigger them
|
|
11
|
-
* cross-site without a CORS preflight or an HTML form, unlike `GET`, which a plain
|
|
12
|
-
* `<a>` click or redirect can trigger with `SameSite=Lax` cookies attached. Other
|
|
13
|
-
* methods (`HEAD`, `OPTIONS`, `QUERY`, ...) have safe semantics that should not
|
|
14
|
-
* invoke a procedure that can modify data.
|
|
15
|
-
*
|
|
16
|
-
* @see {@link https://orpc.dev/docs/rpc/handler#supported-http-methods | RPC Handler - Supported HTTP Methods}
|
|
17
|
-
*/
|
|
18
|
-
declare const RPC_DEFAULT_ALLOW_METHODS: readonly StandardMethod[];
|
|
19
|
-
interface RPCMatcherOptions {
|
|
20
|
-
/**
|
|
21
|
-
* Filter which procedures are exposed for matching. Return `false` to exclude.
|
|
22
|
-
*
|
|
23
|
-
* @default true
|
|
24
|
-
*/
|
|
25
|
-
filter?: Value<boolean, [procedure: AnyProcedureContract | AnyProcedure, path: string[]]>;
|
|
26
|
-
/**
|
|
27
|
-
* Restricts which HTTP methods can invoke procedures, either with a list of allowed
|
|
28
|
-
* methods or decided per request via a function. Requests using a disallowed method
|
|
29
|
-
* are treated as unmatched. `GET` is excluded by default because it is exposed to
|
|
30
|
-
* Cross-Site Request Forgery (CSRF) attacks.
|
|
31
|
-
*
|
|
32
|
-
* @default RPC_DEFAULT_ALLOW_METHODS (['POST', 'PUT', 'PATCH', 'DELETE'])
|
|
33
|
-
* @see {@link https://orpc.dev/docs/rpc/handler#supported-http-methods | RPC Handler - Supported HTTP Methods}
|
|
34
|
-
*/
|
|
35
|
-
allowMethods?: readonly StandardMethod[] | ((method: StandardMethod, procedure: AnyProcedure, path: string[]) => boolean);
|
|
36
|
-
}
|
|
37
|
-
declare class RPCMatcher {
|
|
38
|
-
private readonly filter;
|
|
39
|
-
private readonly allowMethodsSet;
|
|
40
|
-
private readonly allowMethodsFn;
|
|
41
|
-
private readonly rootRouter;
|
|
42
|
-
private readonly tree;
|
|
43
|
-
private readonly pendingLazyRouters;
|
|
44
|
-
constructor(router: AnyRouter, options?: RPCMatcherOptions);
|
|
45
|
-
private index;
|
|
46
|
-
match(method: StandardMethod, pathname: `/${string}`, prefix: `/${string}` | undefined): Promise<{
|
|
47
|
-
path: string[];
|
|
48
|
-
procedure: AnyProcedure;
|
|
49
|
-
} | undefined>;
|
|
50
|
-
private resolvePendingLazyRouters;
|
|
51
|
-
private loadPendingLazyRouters;
|
|
52
|
-
private loadPendingLazyRouter;
|
|
53
|
-
private indexPendingLazyRouter;
|
|
54
|
-
private resolveProcedure;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface RPCHandlerCodecOptions<T extends Context> extends RPCMatcherOptions {
|
|
58
|
-
/**
|
|
59
|
-
* Override the default RPC serializer.
|
|
60
|
-
*/
|
|
61
|
-
serializer?: Public<RPCSerializer>;
|
|
62
|
-
/**
|
|
63
|
-
* Resolve HTTP status for encoded successful outputs.
|
|
64
|
-
*
|
|
65
|
-
* Value should be in the `2xx` range and must be less than `400`.
|
|
66
|
-
* Return `undefined` or `null` to fallback to default
|
|
67
|
-
*
|
|
68
|
-
* @default DEFAULT_SUCCESS_STATUS (200)
|
|
69
|
-
*/
|
|
70
|
-
outputStatus?: Value<number | undefined | null, [output: unknown, procedure: AnyProcedure, path: string[], options: StandardHandlerHandleOptions<T>]>;
|
|
71
|
-
/**
|
|
72
|
-
* Mapping ORPCError Code -> HTTP Status Code
|
|
73
|
-
* The status code should be in the `4xx` or `5xx` range (must be greater than or equal to `400`).
|
|
74
|
-
*
|
|
75
|
-
* @default COMMON_ERROR_STATUS_MAP
|
|
76
|
-
*/
|
|
77
|
-
errorStatusMap?: Record<string, number> | undefined;
|
|
78
|
-
}
|
|
79
|
-
declare class RPCHandlerCodec<T extends Context> implements StandardHandlerCodec<T> {
|
|
80
|
-
private readonly matcher;
|
|
81
|
-
private readonly serializer;
|
|
82
|
-
private readonly errorStatusMap;
|
|
83
|
-
private readonly outputStatus;
|
|
84
|
-
constructor(router: AnyRouter, options?: RPCHandlerCodecOptions<T>);
|
|
85
|
-
resolveProcedure(request: StandardLazyRequest, options: StandardHandlerHandleOptions<T>): Promise<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
86
|
-
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options: StandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
87
|
-
encodeError(error: AnyORPCError, _procedure: AnyProcedure, _path: string[], _options: StandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export { RPCHandlerCodec as a, RPCMatcher as b, RPC_DEFAULT_ALLOW_METHODS as d };
|
|
91
|
-
export type { RPCHandlerCodecOptions as R, RPCMatcherOptions as c };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { value } from '@orpc/shared';
|
|
2
|
-
|
|
3
|
-
function createStandardPeerRequestHandler(handler, options) {
|
|
4
|
-
return async (request) => {
|
|
5
|
-
const context = await value(options.context ?? {}, request);
|
|
6
|
-
const { response } = await handler.handle(request, { ...options, context });
|
|
7
|
-
return response ?? { status: 404, headers: {}, body: "No procedure matched" };
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export { createStandardPeerRequestHandler as c };
|