@orpc/server 1.14.11 → 1.14.13
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 +98 -52
- package/dist/adapters/aws-lambda/index.d.mts +46 -0
- package/dist/adapters/aws-lambda/index.d.ts +46 -0
- package/dist/adapters/aws-lambda/index.mjs +40 -0
- package/dist/adapters/bun-ws/index.d.mts +36 -0
- package/dist/adapters/bun-ws/index.d.ts +36 -0
- package/dist/adapters/bun-ws/index.mjs +47 -0
- package/dist/adapters/crossws/index.d.mts +21 -45
- package/dist/adapters/crossws/index.d.ts +21 -45
- package/dist/adapters/crossws/index.mjs +18 -40
- package/dist/adapters/fastify/index.d.mts +53 -0
- package/dist/adapters/fastify/index.d.ts +53 -0
- package/dist/adapters/fastify/index.mjs +52 -0
- package/dist/adapters/fetch/index.d.mts +96 -64
- package/dist/adapters/fetch/index.d.ts +96 -64
- package/dist/adapters/fetch/index.mjs +139 -34
- package/dist/adapters/message-port/index.d.mts +34 -49
- package/dist/adapters/message-port/index.d.ts +34 -49
- package/dist/adapters/message-port/index.mjs +37 -68
- package/dist/adapters/node/index.d.mts +71 -63
- package/dist/adapters/node/index.d.ts +71 -63
- package/dist/adapters/node/index.mjs +113 -36
- package/dist/adapters/standard/index.d.mts +18 -16
- package/dist/adapters/standard/index.d.ts +18 -16
- package/dist/adapters/standard/index.mjs +5 -5
- package/dist/adapters/standard-peer/index.d.mts +14 -12
- package/dist/adapters/standard-peer/index.d.ts +14 -12
- package/dist/adapters/standard-peer/index.mjs +21 -2
- package/dist/adapters/websocket/index.d.mts +38 -53
- package/dist/adapters/websocket/index.d.ts +38 -53
- package/dist/adapters/websocket/index.mjs +42 -59
- package/dist/adapters/ws/index.d.mts +31 -0
- package/dist/adapters/ws/index.d.ts +31 -0
- package/dist/adapters/ws/index.mjs +41 -0
- package/dist/helpers/index.d.mts +4 -4
- package/dist/helpers/index.d.ts +4 -4
- package/dist/helpers/index.mjs +5 -13
- package/dist/hibernation/index.d.mts +44 -0
- package/dist/hibernation/index.d.ts +44 -0
- package/dist/hibernation/index.mjs +65 -0
- package/dist/index.d.mts +770 -166
- package/dist/index.d.ts +770 -166
- package/dist/index.mjs +399 -306
- package/dist/plugins/index.d.mts +121 -214
- package/dist/plugins/index.d.ts +121 -214
- package/dist/plugins/index.mjs +214 -576
- package/dist/shared/server.7cEtMB30.d.ts +74 -0
- package/dist/shared/server.B8gYOD5g.d.mts +12 -0
- package/dist/shared/server.BqadksTP.d.mts +74 -0
- package/dist/shared/server.C8_sRzQB.d.mts +42 -0
- package/dist/shared/server.CMf4nKky.mjs +230 -0
- package/dist/shared/server.ChyoA9XY.d.ts +42 -0
- package/dist/shared/server.D8IXzfqT.d.ts +32 -0
- package/dist/shared/server.DEBcqOjg.mjs +418 -0
- package/dist/shared/{server.W91HSRkE.mjs → server.DZ5BIITo.mjs} +2 -2
- package/dist/shared/server.DzE6WeuY.d.mts +32 -0
- package/dist/shared/server.EfTOZ2Q7.d.ts +12 -0
- package/dist/shared/server.TEVCLCFC.mjs +39 -0
- package/dist/shared/server.qKsRrdxW.d.mts +193 -0
- package/dist/shared/server.qKsRrdxW.d.ts +193 -0
- package/package.json +56 -26
- package/dist/extensions/callable.d.mts +0 -10
- package/dist/extensions/callable.d.ts +0 -10
- package/dist/extensions/callable.mjs +0 -11
- package/dist/shared/server.15O7oC1p.d.mts +0 -61
- package/dist/shared/server.B4BnEWWm.d.mts +0 -67
- package/dist/shared/server.BhsVw7m7.d.ts +0 -104
- package/dist/shared/server.BoiFpNT7.mjs +0 -224
- package/dist/shared/server.COPVXhDG.mjs +0 -271
- package/dist/shared/server.CX4vUnDk.mjs +0 -11
- package/dist/shared/server.CcR4kgje.d.mts +0 -206
- package/dist/shared/server.CcR4kgje.d.ts +0 -206
- package/dist/shared/server.CnN50DHH.d.ts +0 -67
- package/dist/shared/server.DBs0rrf9.mjs +0 -41
- package/dist/shared/server.DDFizwfU.d.mts +0 -104
- package/dist/shared/server.D_QauotT.mjs +0 -30
- package/dist/shared/server.DofEOwf-.mjs +0 -237
- package/dist/shared/server.m3szVJGU.d.ts +0 -61
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import { ORPCError, wrapAsyncIteratorPreservingEventMeta, cloneORPCError } from '@orpc/client';
|
|
2
|
-
import { reconcileORPCError, ValidationError, ProcedureContract } from '@orpc/contract';
|
|
3
|
-
import { getOrBind, resolveMaybeOptionalOptions, getConstructor, isTypescriptObject, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, override, traceAsyncIterator, traceReadableStream } from '@orpc/shared';
|
|
4
|
-
|
|
5
|
-
function createORPCErrorConstructorMap(errorMap) {
|
|
6
|
-
const proxy = new Proxy(errorMap, {
|
|
7
|
-
get(target, code) {
|
|
8
|
-
if (typeof code !== "string") {
|
|
9
|
-
return getOrBind(target, code);
|
|
10
|
-
}
|
|
11
|
-
const item = (...rest) => {
|
|
12
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
13
|
-
const config = errorMap[code];
|
|
14
|
-
const error = new ORPCError(code, {
|
|
15
|
-
message: options.message ?? config?.message,
|
|
16
|
-
data: options.data,
|
|
17
|
-
cause: options.cause
|
|
18
|
-
});
|
|
19
|
-
if (config) {
|
|
20
|
-
error.defined = true;
|
|
21
|
-
error.inferable = true;
|
|
22
|
-
}
|
|
23
|
-
return error;
|
|
24
|
-
};
|
|
25
|
-
return item;
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
return proxy;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class Lazy {
|
|
32
|
-
"~orpc";
|
|
33
|
-
constructor(def) {
|
|
34
|
-
this["~orpc"] = def;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Checks if the given instance satisfies the {@see Lazy} class/interface.
|
|
38
|
-
*/
|
|
39
|
-
static [Symbol.hasInstance](instance) {
|
|
40
|
-
if (this !== Lazy) {
|
|
41
|
-
return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
42
|
-
}
|
|
43
|
-
const constructor = getConstructor(instance);
|
|
44
|
-
if (constructor === Lazy) {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return isTypescriptObject(instance) && isTypescriptObject(instance["~orpc"]) && isTypescriptObject(instance["~orpc"].meta) && (instance["~orpc"].metaPlugins === void 0 || Array.isArray(instance["~orpc"].metaPlugins)) && typeof instance["~orpc"].loader === "function";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function unlazy(maybeLazy) {
|
|
51
|
-
return maybeLazy instanceof Lazy ? maybeLazy["~orpc"].loader() : Promise.resolve({ default: maybeLazy });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function createProcedureClient(lazyableProcedure, ...rest) {
|
|
55
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
56
|
-
return async (...[input, callerOptions]) => {
|
|
57
|
-
const path = toArray(options.path);
|
|
58
|
-
const { default: procedure } = await unlazy(lazyableProcedure);
|
|
59
|
-
const clientContext = callerOptions?.context ?? {};
|
|
60
|
-
const context = await value(options.context, clientContext) ?? {};
|
|
61
|
-
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
|
62
|
-
const reconcileError = async (e) => {
|
|
63
|
-
if (e instanceof ORPCError) {
|
|
64
|
-
return await reconcileORPCError(procedure["~orpc"].errorMap, e);
|
|
65
|
-
}
|
|
66
|
-
return e;
|
|
67
|
-
};
|
|
68
|
-
try {
|
|
69
|
-
const output = await runWithSpan("call_procedure", (span) => {
|
|
70
|
-
span?.setAttribute("procedure.path", path);
|
|
71
|
-
return intercept(
|
|
72
|
-
options.interceptors,
|
|
73
|
-
{
|
|
74
|
-
context,
|
|
75
|
-
// input can be optional if it is undefinable
|
|
76
|
-
input,
|
|
77
|
-
errors,
|
|
78
|
-
path,
|
|
79
|
-
procedure,
|
|
80
|
-
signal: callerOptions?.signal,
|
|
81
|
-
lastEventId: callerOptions?.lastEventId
|
|
82
|
-
},
|
|
83
|
-
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
|
84
|
-
);
|
|
85
|
-
});
|
|
86
|
-
if (isAsyncIteratorObject(output)) {
|
|
87
|
-
return override(output, wrapAsyncIteratorPreservingEventMeta(
|
|
88
|
-
traceAsyncIterator("consume_async_iterator_object_output", output),
|
|
89
|
-
{ mapError: reconcileError }
|
|
90
|
-
));
|
|
91
|
-
}
|
|
92
|
-
if (output instanceof ReadableStream) {
|
|
93
|
-
return override(output, traceReadableStream("consume_octet_stream_output", output));
|
|
94
|
-
}
|
|
95
|
-
return output;
|
|
96
|
-
} catch (e) {
|
|
97
|
-
throw await reconcileError(e);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
async function validateInput(i, schema, input) {
|
|
102
|
-
return runWithSpan(`validate_input.${i}`, async (span) => {
|
|
103
|
-
span?.setAttribute("input_schema.index", i);
|
|
104
|
-
const result = await schema["~standard"].validate(input);
|
|
105
|
-
if (result.issues) {
|
|
106
|
-
throw new ORPCError("BAD_REQUEST", {
|
|
107
|
-
message: "Input validation failed",
|
|
108
|
-
data: {
|
|
109
|
-
issues: result.issues
|
|
110
|
-
},
|
|
111
|
-
cause: new ValidationError({
|
|
112
|
-
message: "Input validation failed",
|
|
113
|
-
issues: result.issues,
|
|
114
|
-
invalidData: input
|
|
115
|
-
})
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
return result.value;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
async function validateOutput(i, schema, output) {
|
|
122
|
-
return runWithSpan(`validate_output.${i}`, async (span) => {
|
|
123
|
-
span?.setAttribute("output_schema.index", i);
|
|
124
|
-
const result = await schema["~standard"].validate(output);
|
|
125
|
-
if (result.issues) {
|
|
126
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
127
|
-
message: "Output validation failed",
|
|
128
|
-
cause: new ValidationError({
|
|
129
|
-
message: "Output validation failed",
|
|
130
|
-
issues: result.issues,
|
|
131
|
-
invalidData: output
|
|
132
|
-
})
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
return result.value;
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
const middlewareDone = (...rest) => {
|
|
139
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
140
|
-
return {
|
|
141
|
-
output: options.output,
|
|
142
|
-
// context can be undefined when all field is optional
|
|
143
|
-
context: options.context ?? {}
|
|
144
|
-
};
|
|
145
|
-
};
|
|
146
|
-
async function executeProcedureInternal(procedure, options) {
|
|
147
|
-
const inputSchemas = toArray(procedure["~orpc"].inputSchemas);
|
|
148
|
-
const outputSchemas = toArray(procedure["~orpc"].outputSchemas);
|
|
149
|
-
const orderedMiddlewares = procedure["~orpc"].orderedMiddlewares;
|
|
150
|
-
const next = async (midIndex, context, input) => {
|
|
151
|
-
let currentInput = input;
|
|
152
|
-
const startInputIndex = midIndex === 0 ? 0 : orderedMiddlewares[midIndex - 1].inputSchemasLengthAtUse ?? 0;
|
|
153
|
-
const endInputIndex = midIndex === orderedMiddlewares.length ? inputSchemas.length : orderedMiddlewares[midIndex].inputSchemasLengthAtUse ?? 0;
|
|
154
|
-
if (!procedure["~orpc"].disableInputValidation) {
|
|
155
|
-
for (let i = startInputIndex; i < endInputIndex; i++) {
|
|
156
|
-
currentInput = await validateInput(i, inputSchemas[i], currentInput);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
let currentOutput;
|
|
160
|
-
let currentContext = context;
|
|
161
|
-
if (midIndex < orderedMiddlewares.length) {
|
|
162
|
-
const { middleware } = orderedMiddlewares[midIndex];
|
|
163
|
-
const result = await runWithSpan(`middleware.${middleware.name}`, async (span) => {
|
|
164
|
-
span?.setAttribute("middleware.index", midIndex);
|
|
165
|
-
return await middleware(
|
|
166
|
-
{
|
|
167
|
-
...options,
|
|
168
|
-
context,
|
|
169
|
-
next: (...rest) => {
|
|
170
|
-
const nextOptions = resolveMaybeOptionalOptions(rest);
|
|
171
|
-
const nextContext = nextOptions.context ?? {};
|
|
172
|
-
return next(
|
|
173
|
-
midIndex + 1,
|
|
174
|
-
{ ...context, ...nextContext },
|
|
175
|
-
currentInput
|
|
176
|
-
);
|
|
177
|
-
},
|
|
178
|
-
lastEventId: options.lastEventId
|
|
179
|
-
},
|
|
180
|
-
currentInput,
|
|
181
|
-
middlewareDone
|
|
182
|
-
);
|
|
183
|
-
});
|
|
184
|
-
currentOutput = result.output;
|
|
185
|
-
currentContext = { ...context, ...result.context };
|
|
186
|
-
} else {
|
|
187
|
-
currentOutput = await runWithSpan(
|
|
188
|
-
"handler",
|
|
189
|
-
() => procedure["~orpc"].handler({ ...options, context, input: currentInput }, currentInput)
|
|
190
|
-
);
|
|
191
|
-
if (currentOutput instanceof ORPCError) {
|
|
192
|
-
if (procedure["~orpc"].opaqueReturnedErrors) {
|
|
193
|
-
throw currentOutput;
|
|
194
|
-
}
|
|
195
|
-
if (currentOutput.inferable && !currentOutput.defined) {
|
|
196
|
-
throw currentOutput;
|
|
197
|
-
}
|
|
198
|
-
const error = cloneORPCError(currentOutput);
|
|
199
|
-
error.defined = false;
|
|
200
|
-
error.inferable = true;
|
|
201
|
-
throw error;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const startOutputIndex = midIndex === 0 ? 0 : orderedMiddlewares[midIndex - 1].outputSchemasLengthAtUse ?? 0;
|
|
205
|
-
const endOutputIndex = midIndex === orderedMiddlewares.length ? outputSchemas.length : orderedMiddlewares[midIndex].outputSchemasLengthAtUse ?? 0;
|
|
206
|
-
if (!procedure["~orpc"].disableOutputValidation) {
|
|
207
|
-
for (let i = endOutputIndex - 1; i >= startOutputIndex; i--) {
|
|
208
|
-
currentOutput = await validateOutput(i, outputSchemas[i], currentOutput);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return { output: currentOutput, context: currentContext };
|
|
212
|
-
};
|
|
213
|
-
const { output } = await next(0, options.context, options.input);
|
|
214
|
-
return output;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
class Procedure {
|
|
218
|
-
"~orpc";
|
|
219
|
-
constructor(def) {
|
|
220
|
-
this["~orpc"] = def;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Checks if the given instance satisfies the {@see Procedure} class/interface.
|
|
224
|
-
*/
|
|
225
|
-
static [Symbol.hasInstance](instance) {
|
|
226
|
-
if (this !== Procedure) {
|
|
227
|
-
return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
228
|
-
}
|
|
229
|
-
const constructor = getConstructor(instance);
|
|
230
|
-
if (constructor === Procedure) {
|
|
231
|
-
return true;
|
|
232
|
-
}
|
|
233
|
-
return instance instanceof ProcedureContract && Array.isArray(instance["~orpc"].orderedMiddlewares) && typeof instance["~orpc"].handler === "function";
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export { Lazy as L, Procedure as P, createORPCErrorConstructorMap as a, createProcedureClient as c, unlazy as u };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { RouterContract, ProcedureContract, ORPCErrorFromErrorMap, ThrowableError, InferSchemaInput, InferSchemaOutput } from '@orpc/contract';
|
|
2
|
-
import { C as Context, a as Procedure, L as Lazyable, M as MergedContext } from './server.CcR4kgje.js';
|
|
3
|
-
|
|
4
|
-
type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any, any, any> | {
|
|
5
|
-
[k: string]: Lazyable<Router<TInitialContext>>;
|
|
6
|
-
};
|
|
7
|
-
type ContractedRouter<T extends RouterContract, TInitialContext extends Context> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<TInitialContext, any, $InputSchema, $OutputSchema, $ErrorMap, never> : {
|
|
8
|
-
[K in keyof T]: T[K] extends RouterContract ? Lazyable<ContractedRouter<T[K], TInitialContext>> : never;
|
|
9
|
-
};
|
|
10
|
-
type AnyRouter = Router<any>;
|
|
11
|
-
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $> ? $ : never;
|
|
12
|
-
/**
|
|
13
|
-
* Infer all initial context of the router.
|
|
14
|
-
*
|
|
15
|
-
* @info A procedure is a router too.
|
|
16
|
-
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
17
|
-
*/
|
|
18
|
-
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
|
19
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Infer all current context of the router.
|
|
23
|
-
*
|
|
24
|
-
* @info A procedure is a router too.
|
|
25
|
-
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
26
|
-
*/
|
|
27
|
-
type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, infer UInjectedContext, any, any, any, any> ? MergedContext<UInitialContext, UInjectedContext> : {
|
|
28
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterFinalContexts<U> : never;
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Infer all router inputs
|
|
32
|
-
*
|
|
33
|
-
* @info A procedure is a router too.
|
|
34
|
-
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
35
|
-
*/
|
|
36
|
-
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
37
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Infer all router outputs
|
|
41
|
-
*
|
|
42
|
-
* @info A procedure is a router too.
|
|
43
|
-
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
44
|
-
*/
|
|
45
|
-
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
46
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Infer the union of throwable errors for entire router.
|
|
50
|
-
*/
|
|
51
|
-
type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
|
|
52
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterError<U> : never;
|
|
53
|
-
}[keyof T];
|
|
54
|
-
/**
|
|
55
|
-
* Infer throwable errors for each procedure, preserving the router shape.
|
|
56
|
-
*/
|
|
57
|
-
type InferRouterErrors<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
|
|
58
|
-
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterErrors<U> : never;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type { AnyRouter as A, ContractedRouter as C, InferRouterInitialContext as I, Router as R, InferRouterError as a, InferRouterErrors as b, InferRouterFinalContexts as c, InferRouterInitialContexts as d, InferRouterInputs as e, InferRouterOutputs as f };
|