@orpc/server 0.0.0-next.9dfcee8 → 0.0.0-next.9f70405
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 +24 -17
- package/dist/adapters/bun-ws/index.d.mts +35 -0
- package/dist/adapters/bun-ws/index.d.ts +35 -0
- package/dist/adapters/bun-ws/index.mjs +50 -0
- package/dist/adapters/crossws/index.d.mts +30 -0
- package/dist/adapters/crossws/index.d.ts +30 -0
- package/dist/adapters/crossws/index.mjs +50 -0
- package/dist/adapters/fetch/index.d.mts +61 -11
- package/dist/adapters/fetch/index.d.ts +61 -11
- package/dist/adapters/fetch/index.mjs +109 -8
- package/dist/adapters/node/index.d.mts +63 -22
- package/dist/adapters/node/index.d.ts +63 -22
- package/dist/adapters/node/index.mjs +87 -24
- package/dist/adapters/standard/index.d.mts +6 -6
- package/dist/adapters/standard/index.d.ts +6 -6
- package/dist/adapters/standard/index.mjs +3 -3
- package/dist/adapters/websocket/index.d.mts +27 -0
- package/dist/adapters/websocket/index.d.ts +27 -0
- package/dist/adapters/websocket/index.mjs +37 -0
- package/dist/adapters/ws/index.d.mts +28 -0
- package/dist/adapters/ws/index.d.ts +28 -0
- package/dist/adapters/ws/index.mjs +37 -0
- package/dist/index.d.mts +598 -47
- package/dist/index.d.ts +598 -47
- package/dist/index.mjs +147 -15
- package/dist/plugins/index.d.mts +140 -15
- package/dist/plugins/index.d.ts +140 -15
- package/dist/plugins/index.mjs +162 -14
- package/dist/shared/server.BRoxSiSC.d.mts +12 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.BjiJH9Vo.d.ts +10 -0
- package/dist/shared/server.Cy1vfSiG.d.ts +12 -0
- package/dist/shared/{server.BtxZnWJ9.mjs → server.DG7Tamti.mjs} +22 -29
- package/dist/shared/{server.BYTulgUc.d.mts → server.DPWk5pjW.d.mts} +61 -13
- package/dist/shared/{server.BYTulgUc.d.ts → server.DPWk5pjW.d.ts} +61 -13
- package/dist/shared/server.QUe9N8P4.d.mts +10 -0
- package/dist/shared/{server.jG7ZuX3S.mjs → server.SxlTJfG2.mjs} +53 -28
- package/dist/shared/server.YZzrREz9.d.ts +74 -0
- package/dist/shared/server.eWLxY3lq.d.mts +74 -0
- package/package.json +43 -20
- package/dist/adapters/hono/index.d.mts +0 -21
- package/dist/adapters/hono/index.d.ts +0 -21
- package/dist/adapters/hono/index.mjs +0 -32
- package/dist/adapters/next/index.d.mts +0 -28
- package/dist/adapters/next/index.d.ts +0 -28
- package/dist/adapters/next/index.mjs +0 -29
- package/dist/shared/server.B3Tm0IXY.d.ts +0 -75
- package/dist/shared/server.BeJithK4.d.mts +0 -75
- package/dist/shared/server.Bz_xNBjz.d.mts +0 -8
- package/dist/shared/server.DoP20NVH.mjs +0 -29
- package/dist/shared/server.EhgR_5_I.d.ts +0 -8
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -1,12 +1,11 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
|
2
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
2
3
|
import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
|
3
|
-
import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
4
4
|
|
5
|
-
type Context = Record<
|
5
|
+
type Context = Record<PropertyKey, any>;
|
6
6
|
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
7
7
|
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
8
8
|
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
9
|
-
type ContextExtendsGuard<T extends Context, U extends Context> = T extends U ? unknown : never;
|
10
9
|
|
11
10
|
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
12
11
|
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
@@ -29,6 +28,9 @@ interface Lazy<T> {
|
|
29
28
|
};
|
30
29
|
}
|
31
30
|
type Lazyable<T> = T | Lazy<T>;
|
31
|
+
/**
|
32
|
+
* Create a lazy thing.
|
33
|
+
*/
|
32
34
|
declare function lazy<T>(loader: () => Promise<{
|
33
35
|
default: T;
|
34
36
|
}>, meta?: LazyMeta): Lazy<T>;
|
@@ -57,7 +59,15 @@ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends
|
|
57
59
|
outputValidationIndex: number;
|
58
60
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
59
61
|
}
|
62
|
+
/**
|
63
|
+
* This class represents a procedure.
|
64
|
+
*
|
65
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
66
|
+
*/
|
60
67
|
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
68
|
+
/**
|
69
|
+
* This property holds the defined options.
|
70
|
+
*/
|
61
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
62
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
63
73
|
}
|
@@ -88,6 +98,11 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
88
98
|
next: MiddlewareNextFn<TOutput>;
|
89
99
|
errors: TErrorConstructorMap;
|
90
100
|
}
|
101
|
+
/**
|
102
|
+
* A function that represents a middleware.
|
103
|
+
*
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
105
|
+
*/
|
91
106
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
92
107
|
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
93
108
|
}
|
@@ -98,47 +113,80 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
98
113
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
99
114
|
|
100
115
|
type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
101
|
-
interface ProcedureClientInterceptorOptions<TInitialContext extends Context,
|
116
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
102
117
|
context: TInitialContext;
|
103
|
-
input:
|
118
|
+
input: unknown;
|
104
119
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
105
120
|
path: readonly string[];
|
106
121
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
107
122
|
signal?: AbortSignal;
|
108
123
|
lastEventId: string | undefined;
|
109
124
|
}
|
110
|
-
|
111
|
-
* Options for creating a procedure caller with comprehensive type safety
|
112
|
-
*/
|
113
|
-
type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
125
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
114
126
|
/**
|
115
127
|
* This is helpful for logging and analytics.
|
116
128
|
*/
|
117
129
|
path?: readonly string[];
|
118
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
130
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
|
119
131
|
} & (Record<never, never> extends TInitialContext ? {
|
120
132
|
context?: Value<TInitialContext, [clientContext: TClientContext]>;
|
121
133
|
} : {
|
122
134
|
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
123
135
|
});
|
124
|
-
|
136
|
+
/**
|
137
|
+
* Create Server-side client from a procedure.
|
138
|
+
*
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
140
|
+
*/
|
141
|
+
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
|
125
142
|
|
143
|
+
/**
|
144
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
145
|
+
*
|
146
|
+
* @info A procedure is a router too.
|
147
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
148
|
+
*/
|
126
149
|
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
|
127
150
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
128
151
|
};
|
129
152
|
type AnyRouter = Router<any, any>;
|
130
153
|
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
154
|
+
/**
|
155
|
+
* Infer all initial context of the router.
|
156
|
+
*
|
157
|
+
* @info A procedure is a router too.
|
158
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
159
|
+
*/
|
131
160
|
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
132
161
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
133
162
|
};
|
163
|
+
/**
|
164
|
+
* Infer all current context of the router.
|
165
|
+
*
|
166
|
+
* @info A procedure is a router too.
|
167
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
168
|
+
*/
|
134
169
|
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
|
135
170
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
136
171
|
};
|
172
|
+
/**
|
173
|
+
* Infer all router inputs
|
174
|
+
*
|
175
|
+
* @info A procedure is a router too.
|
176
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
177
|
+
*/
|
137
178
|
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
138
179
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
139
180
|
};
|
181
|
+
/**
|
182
|
+
* Infer all router outputs
|
183
|
+
*
|
184
|
+
* @info A procedure is a router too.
|
185
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
186
|
+
*/
|
140
187
|
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
141
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
142
189
|
};
|
143
190
|
|
144
|
-
export {
|
191
|
+
export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
|
192
|
+
export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DPWk5pjW.mjs';
|
2
|
+
import { g as StandardHandleOptions } from './server.eWLxY3lq.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 };
|
@@ -1,51 +1,66 @@
|
|
1
|
+
import { toHttpPath, StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
1
2
|
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
|
3
|
+
import { toArray, intercept, parseEmptyableJSON } from '@orpc/shared';
|
4
|
+
import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.DG7Tamti.mjs';
|
5
|
+
|
6
|
+
class CompositeStandardHandlerPlugin {
|
7
|
+
plugins;
|
8
|
+
constructor(plugins = []) {
|
9
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
10
|
+
}
|
11
|
+
init(options, router) {
|
12
|
+
for (const plugin of this.plugins) {
|
13
|
+
plugin.init?.(options, router);
|
14
|
+
}
|
15
|
+
}
|
16
|
+
}
|
5
17
|
|
6
18
|
class StandardHandler {
|
7
19
|
constructor(router, matcher, codec, options) {
|
8
20
|
this.matcher = matcher;
|
9
21
|
this.codec = codec;
|
10
|
-
|
11
|
-
|
12
|
-
this.
|
22
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
23
|
+
plugins.init(options, router);
|
24
|
+
this.interceptors = toArray(options.interceptors);
|
25
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
26
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
13
27
|
this.matcher.init(router);
|
14
28
|
}
|
15
|
-
|
16
|
-
|
29
|
+
interceptors;
|
30
|
+
clientInterceptors;
|
31
|
+
rootInterceptors;
|
32
|
+
async handle(request, options) {
|
33
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
34
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
35
|
+
return { matched: false, response: void 0 };
|
36
|
+
}
|
17
37
|
return intercept(
|
18
|
-
this.
|
19
|
-
{
|
20
|
-
request,
|
21
|
-
...options,
|
22
|
-
context: options?.context ?? {}
|
23
|
-
// context is optional only when all fields are optional so we can safely force it to have a context
|
24
|
-
},
|
38
|
+
this.rootInterceptors,
|
39
|
+
{ ...options, request, prefix },
|
25
40
|
async (interceptorOptions) => {
|
26
41
|
let isDecoding = false;
|
27
42
|
try {
|
28
43
|
return await intercept(
|
29
|
-
this.
|
44
|
+
this.interceptors,
|
30
45
|
interceptorOptions,
|
31
|
-
async (
|
32
|
-
const method =
|
33
|
-
const url =
|
34
|
-
const pathname =
|
35
|
-
const match = await this.matcher.match(method, pathname);
|
46
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
47
|
+
const method = request2.method;
|
48
|
+
const url = request2.url;
|
49
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
50
|
+
const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`);
|
36
51
|
if (!match) {
|
37
52
|
return { matched: false, response: void 0 };
|
38
53
|
}
|
39
54
|
const client = createProcedureClient(match.procedure, {
|
40
|
-
context
|
55
|
+
context,
|
41
56
|
path: match.path,
|
42
|
-
interceptors: this.
|
57
|
+
interceptors: this.clientInterceptors
|
43
58
|
});
|
44
59
|
isDecoding = true;
|
45
|
-
const input = await this.codec.decode(
|
60
|
+
const input = await this.codec.decode(request2, match.params, match.procedure);
|
46
61
|
isDecoding = false;
|
47
|
-
const lastEventId = Array.isArray(
|
48
|
-
const output = await client(input, { signal:
|
62
|
+
const lastEventId = Array.isArray(request2.headers["last-event-id"]) ? request2.headers["last-event-id"].at(-1) : request2.headers["last-event-id"];
|
63
|
+
const output = await client(input, { signal: request2.signal, lastEventId });
|
49
64
|
const response = this.codec.encode(output, match.procedure);
|
50
65
|
return {
|
51
66
|
matched: true,
|
@@ -54,7 +69,7 @@ class StandardHandler {
|
|
54
69
|
}
|
55
70
|
);
|
56
71
|
} catch (e) {
|
57
|
-
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
72
|
+
const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
58
73
|
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
59
74
|
cause: e
|
60
75
|
}) : toORPCError(e);
|
@@ -155,4 +170,14 @@ class StandardRPCMatcher {
|
|
155
170
|
}
|
156
171
|
}
|
157
172
|
|
158
|
-
|
173
|
+
class StandardRPCHandler extends StandardHandler {
|
174
|
+
constructor(router, options = {}) {
|
175
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
176
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
177
|
+
const matcher = new StandardRPCMatcher();
|
178
|
+
const codec = new StandardRPCCodec(serializer);
|
179
|
+
super(router, matcher, codec, options);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCHandler as b, StandardRPCMatcher as c };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
|
3
|
+
import { Interceptor, ThrowableError } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, f as AnyRouter, h as AnyProcedure, F as ProcedureClientInterceptorOptions } from './server.DPWk5pjW.js';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
10
|
+
}
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
12
|
+
protected readonly plugins: TPlugin[];
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
15
|
+
}
|
16
|
+
|
17
|
+
type StandardParams = Record<string, string>;
|
18
|
+
type StandardMatchResult = {
|
19
|
+
path: readonly string[];
|
20
|
+
procedure: AnyProcedure;
|
21
|
+
params?: StandardParams;
|
22
|
+
} | undefined;
|
23
|
+
interface StandardMatcher {
|
24
|
+
init(router: AnyRouter): void;
|
25
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
26
|
+
}
|
27
|
+
interface StandardCodec {
|
28
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
29
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
30
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
31
|
+
}
|
32
|
+
|
33
|
+
interface StandardHandleOptions<T extends Context> {
|
34
|
+
prefix?: HTTPPath;
|
35
|
+
context: T;
|
36
|
+
}
|
37
|
+
type StandardHandleResult = {
|
38
|
+
matched: true;
|
39
|
+
response: StandardResponse;
|
40
|
+
} | {
|
41
|
+
matched: false;
|
42
|
+
response: undefined;
|
43
|
+
};
|
44
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
45
|
+
request: StandardLazyRequest;
|
46
|
+
}
|
47
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
48
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
49
|
+
/**
|
50
|
+
* Interceptors at the request level, helpful when you want catch errors
|
51
|
+
*/
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
|
62
|
+
}
|
63
|
+
declare class StandardHandler<T extends Context> {
|
64
|
+
private readonly matcher;
|
65
|
+
private readonly codec;
|
66
|
+
private readonly interceptors;
|
67
|
+
private readonly clientInterceptors;
|
68
|
+
private readonly rootInterceptors;
|
69
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
70
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
71
|
+
}
|
72
|
+
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as i };
|
74
|
+
export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardCodec as c, StandardParams as d, StandardMatcher as e, StandardMatchResult as f, StandardHandleOptions as g, StandardHandleResult as h };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
|
3
|
+
import { Interceptor, ThrowableError } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, f as AnyRouter, h as AnyProcedure, F as ProcedureClientInterceptorOptions } from './server.DPWk5pjW.mjs';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
10
|
+
}
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
12
|
+
protected readonly plugins: TPlugin[];
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
15
|
+
}
|
16
|
+
|
17
|
+
type StandardParams = Record<string, string>;
|
18
|
+
type StandardMatchResult = {
|
19
|
+
path: readonly string[];
|
20
|
+
procedure: AnyProcedure;
|
21
|
+
params?: StandardParams;
|
22
|
+
} | undefined;
|
23
|
+
interface StandardMatcher {
|
24
|
+
init(router: AnyRouter): void;
|
25
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
26
|
+
}
|
27
|
+
interface StandardCodec {
|
28
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
29
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
30
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
31
|
+
}
|
32
|
+
|
33
|
+
interface StandardHandleOptions<T extends Context> {
|
34
|
+
prefix?: HTTPPath;
|
35
|
+
context: T;
|
36
|
+
}
|
37
|
+
type StandardHandleResult = {
|
38
|
+
matched: true;
|
39
|
+
response: StandardResponse;
|
40
|
+
} | {
|
41
|
+
matched: false;
|
42
|
+
response: undefined;
|
43
|
+
};
|
44
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
45
|
+
request: StandardLazyRequest;
|
46
|
+
}
|
47
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
48
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
49
|
+
/**
|
50
|
+
* Interceptors at the request level, helpful when you want catch errors
|
51
|
+
*/
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
|
62
|
+
}
|
63
|
+
declare class StandardHandler<T extends Context> {
|
64
|
+
private readonly matcher;
|
65
|
+
private readonly codec;
|
66
|
+
private readonly interceptors;
|
67
|
+
private readonly clientInterceptors;
|
68
|
+
private readonly rootInterceptors;
|
69
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
70
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
71
|
+
}
|
72
|
+
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as i };
|
74
|
+
export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardCodec as c, StandardParams as d, StandardMatcher as e, StandardMatchResult as f, StandardHandleOptions as g, StandardHandleResult as h };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/server",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.9f70405",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -34,39 +34,62 @@
|
|
34
34
|
"import": "./dist/adapters/fetch/index.mjs",
|
35
35
|
"default": "./dist/adapters/fetch/index.mjs"
|
36
36
|
},
|
37
|
-
"./hono": {
|
38
|
-
"types": "./dist/adapters/hono/index.d.mts",
|
39
|
-
"import": "./dist/adapters/hono/index.mjs",
|
40
|
-
"default": "./dist/adapters/hono/index.mjs"
|
41
|
-
},
|
42
|
-
"./next": {
|
43
|
-
"types": "./dist/adapters/next/index.d.mts",
|
44
|
-
"import": "./dist/adapters/next/index.mjs",
|
45
|
-
"default": "./dist/adapters/next/index.mjs"
|
46
|
-
},
|
47
37
|
"./node": {
|
48
38
|
"types": "./dist/adapters/node/index.d.mts",
|
49
39
|
"import": "./dist/adapters/node/index.mjs",
|
50
40
|
"default": "./dist/adapters/node/index.mjs"
|
41
|
+
},
|
42
|
+
"./websocket": {
|
43
|
+
"types": "./dist/adapters/websocket/index.d.mts",
|
44
|
+
"import": "./dist/adapters/websocket/index.mjs",
|
45
|
+
"default": "./dist/adapters/websocket/index.mjs"
|
46
|
+
},
|
47
|
+
"./crossws": {
|
48
|
+
"types": "./dist/adapters/crossws/index.d.mts",
|
49
|
+
"import": "./dist/adapters/crossws/index.mjs",
|
50
|
+
"default": "./dist/adapters/crossws/index.mjs"
|
51
|
+
},
|
52
|
+
"./ws": {
|
53
|
+
"types": "./dist/adapters/ws/index.d.mts",
|
54
|
+
"import": "./dist/adapters/ws/index.mjs",
|
55
|
+
"default": "./dist/adapters/ws/index.mjs"
|
56
|
+
},
|
57
|
+
"./bun-ws": {
|
58
|
+
"types": "./dist/adapters/bun-ws/index.d.mts",
|
59
|
+
"import": "./dist/adapters/bun-ws/index.mjs",
|
60
|
+
"default": "./dist/adapters/bun-ws/index.mjs"
|
51
61
|
}
|
52
62
|
},
|
53
63
|
"files": [
|
54
64
|
"dist"
|
55
65
|
],
|
56
66
|
"peerDependencies": {
|
57
|
-
"
|
58
|
-
"
|
67
|
+
"crossws": ">=0.3.4",
|
68
|
+
"ws": ">=8.18.1"
|
69
|
+
},
|
70
|
+
"peerDependenciesMeta": {
|
71
|
+
"crossws": {
|
72
|
+
"optional": true
|
73
|
+
},
|
74
|
+
"ws": {
|
75
|
+
"optional": true
|
76
|
+
}
|
59
77
|
},
|
60
78
|
"dependencies": {
|
61
|
-
"@orpc/
|
62
|
-
"@orpc/shared": "0.0.0-next.
|
63
|
-
"@orpc/
|
64
|
-
"@orpc/
|
65
|
-
"@orpc/standard-server-
|
66
|
-
"@orpc/standard-server-
|
79
|
+
"@orpc/contract": "0.0.0-next.9f70405",
|
80
|
+
"@orpc/shared": "0.0.0-next.9f70405",
|
81
|
+
"@orpc/client": "0.0.0-next.9f70405",
|
82
|
+
"@orpc/standard-server": "0.0.0-next.9f70405",
|
83
|
+
"@orpc/standard-server-node": "0.0.0-next.9f70405",
|
84
|
+
"@orpc/standard-server-fetch": "0.0.0-next.9f70405",
|
85
|
+
"@orpc/standard-server-peer": "0.0.0-next.9f70405"
|
67
86
|
},
|
68
87
|
"devDependencies": {
|
69
|
-
"
|
88
|
+
"@types/ws": "^8.18.1",
|
89
|
+
"crossws": "^0.3.4",
|
90
|
+
"next": "^15.3.0",
|
91
|
+
"supertest": "^7.1.0",
|
92
|
+
"ws": "^8.18.1"
|
70
93
|
},
|
71
94
|
"scripts": {
|
72
95
|
"build": "unbuild",
|
@@ -1,21 +0,0 @@
|
|
1
|
-
import { FetchHandler } from '../fetch/index.mjs';
|
2
|
-
export { FetchHandleResult, RPCHandler } from '../fetch/index.mjs';
|
3
|
-
import { Value, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
-
import { Context as Context$1, MiddlewareHandler } from 'hono';
|
5
|
-
import { C as Context } from '../../shared/server.BYTulgUc.mjs';
|
6
|
-
import { S as StandardHandleOptions } from '../../shared/server.BeJithK4.mjs';
|
7
|
-
import '@orpc/standard-server-fetch';
|
8
|
-
import '../../shared/server.Bz_xNBjz.mjs';
|
9
|
-
import '@orpc/client/standard';
|
10
|
-
import '@orpc/client';
|
11
|
-
import '@orpc/contract';
|
12
|
-
import '@orpc/standard-server';
|
13
|
-
|
14
|
-
type CreateMiddlewareOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
15
|
-
context?: Value<T, [Context$1]>;
|
16
|
-
} : {
|
17
|
-
context: Value<T, [Context$1]>;
|
18
|
-
});
|
19
|
-
declare function createMiddleware<T extends Context>(handler: FetchHandler<T>, ...[options]: MaybeOptionalOptions<CreateMiddlewareOptions<T>>): MiddlewareHandler;
|
20
|
-
|
21
|
-
export { type CreateMiddlewareOptions, FetchHandler, createMiddleware };
|
@@ -1,21 +0,0 @@
|
|
1
|
-
import { FetchHandler } from '../fetch/index.js';
|
2
|
-
export { FetchHandleResult, RPCHandler } from '../fetch/index.js';
|
3
|
-
import { Value, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
-
import { Context as Context$1, MiddlewareHandler } from 'hono';
|
5
|
-
import { C as Context } from '../../shared/server.BYTulgUc.js';
|
6
|
-
import { S as StandardHandleOptions } from '../../shared/server.B3Tm0IXY.js';
|
7
|
-
import '@orpc/standard-server-fetch';
|
8
|
-
import '../../shared/server.EhgR_5_I.js';
|
9
|
-
import '@orpc/client/standard';
|
10
|
-
import '@orpc/client';
|
11
|
-
import '@orpc/contract';
|
12
|
-
import '@orpc/standard-server';
|
13
|
-
|
14
|
-
type CreateMiddlewareOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
15
|
-
context?: Value<T, [Context$1]>;
|
16
|
-
} : {
|
17
|
-
context: Value<T, [Context$1]>;
|
18
|
-
});
|
19
|
-
declare function createMiddleware<T extends Context>(handler: FetchHandler<T>, ...[options]: MaybeOptionalOptions<CreateMiddlewareOptions<T>>): MiddlewareHandler;
|
20
|
-
|
21
|
-
export { type CreateMiddlewareOptions, FetchHandler, createMiddleware };
|
@@ -1,32 +0,0 @@
|
|
1
|
-
export { R as RPCHandler } from '../../shared/server.DoP20NVH.mjs';
|
2
|
-
import { value } from '@orpc/shared';
|
3
|
-
import '@orpc/client/standard';
|
4
|
-
import '@orpc/standard-server-fetch';
|
5
|
-
import '../../shared/server.jG7ZuX3S.mjs';
|
6
|
-
import '@orpc/client';
|
7
|
-
import '../../shared/server.Q6ZmnTgO.mjs';
|
8
|
-
import '../../shared/server.BtxZnWJ9.mjs';
|
9
|
-
import '@orpc/contract';
|
10
|
-
|
11
|
-
function createMiddleware(handler, ...[options]) {
|
12
|
-
return async (c, next) => {
|
13
|
-
const bodyProps = /* @__PURE__ */ new Set(["arrayBuffer", "blob", "formData", "json", "text"]);
|
14
|
-
const request = c.req.method === "GET" || c.req.method === "HEAD" ? c.req.raw : new Proxy(c.req.raw, {
|
15
|
-
// https://github.com/honojs/middleware/blob/main/packages/trpc-server/src/index.ts#L39
|
16
|
-
get(target, prop) {
|
17
|
-
if (bodyProps.has(prop)) {
|
18
|
-
return () => c.req[prop]();
|
19
|
-
}
|
20
|
-
return Reflect.get(target, prop, target);
|
21
|
-
}
|
22
|
-
});
|
23
|
-
const context = await value(options?.context ?? {}, c);
|
24
|
-
const { matched, response } = await handler.handle(request, { ...options, context });
|
25
|
-
if (matched) {
|
26
|
-
return c.newResponse(response.body, response);
|
27
|
-
}
|
28
|
-
await next();
|
29
|
-
};
|
30
|
-
}
|
31
|
-
|
32
|
-
export { createMiddleware };
|
@@ -1,28 +0,0 @@
|
|
1
|
-
import { FetchHandler } from '../fetch/index.mjs';
|
2
|
-
export { FetchHandleResult, RPCHandler } from '../fetch/index.mjs';
|
3
|
-
import { Value, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
-
import { NextRequest } from 'next/server';
|
5
|
-
import { C as Context } from '../../shared/server.BYTulgUc.mjs';
|
6
|
-
import { S as StandardHandleOptions } from '../../shared/server.BeJithK4.mjs';
|
7
|
-
import '@orpc/standard-server-fetch';
|
8
|
-
import '../../shared/server.Bz_xNBjz.mjs';
|
9
|
-
import '@orpc/client/standard';
|
10
|
-
import '@orpc/client';
|
11
|
-
import '@orpc/contract';
|
12
|
-
import '@orpc/standard-server';
|
13
|
-
|
14
|
-
type ServeOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
15
|
-
context?: Value<T, [NextRequest]>;
|
16
|
-
} : {
|
17
|
-
context: Value<T, [NextRequest]>;
|
18
|
-
});
|
19
|
-
interface ServeResult {
|
20
|
-
GET(req: NextRequest): Promise<Response>;
|
21
|
-
POST(req: NextRequest): Promise<Response>;
|
22
|
-
PUT(req: NextRequest): Promise<Response>;
|
23
|
-
PATCH(req: NextRequest): Promise<Response>;
|
24
|
-
DELETE(req: NextRequest): Promise<Response>;
|
25
|
-
}
|
26
|
-
declare function serve<T extends Context>(handler: FetchHandler<T>, ...[options]: MaybeOptionalOptions<ServeOptions<T>>): ServeResult;
|
27
|
-
|
28
|
-
export { FetchHandler, type ServeOptions, type ServeResult, serve };
|