orpc-fn 0.7.0 → 0.9.0
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 +66 -28
- package/dist/assemble-procedure.d.ts.map +1 -1
- package/dist/assemble-procedure.js +3 -1
- package/dist/assemble-procedure.js.map +1 -1
- package/dist/client.d.ts +13 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +21 -1
- package/dist/client.js.map +1 -1
- package/dist/create-fn.d.ts +63 -5
- package/dist/create-fn.d.ts.map +1 -1
- package/dist/create-fn.js +45 -16
- package/dist/create-fn.js.map +1 -1
- package/dist/expected-client-error.d.ts +6 -0
- package/dist/expected-client-error.d.ts.map +1 -1
- package/dist/expected-client-error.js +16 -0
- package/dist/expected-client-error.js.map +1 -1
- package/dist/hono.d.ts +44 -22
- package/dist/hono.d.ts.map +1 -1
- package/dist/hono.js +51 -20
- package/dist/hono.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/issues.d.ts +12 -0
- package/dist/issues.d.ts.map +1 -0
- package/dist/issues.js +22 -0
- package/dist/issues.js.map +1 -0
- package/dist/json-schema.d.ts +3 -6
- package/dist/json-schema.d.ts.map +1 -1
- package/dist/json-schema.js +12 -7
- package/dist/json-schema.js.map +1 -1
- package/dist/live/codec.d.ts +2 -0
- package/dist/live/codec.d.ts.map +1 -1
- package/dist/live/codec.js +21 -16
- package/dist/live/codec.js.map +1 -1
- package/dist/live/fn-live.d.ts +6 -19
- package/dist/live/fn-live.d.ts.map +1 -1
- package/dist/live/fn-live.js +15 -23
- package/dist/live/fn-live.js.map +1 -1
- package/dist/live/index.d.ts +2 -2
- package/dist/live/index.d.ts.map +1 -1
- package/dist/live/index.js +1 -1
- package/dist/live/index.js.map +1 -1
- package/dist/live/memory.d.ts +11 -1
- package/dist/live/memory.d.ts.map +1 -1
- package/dist/live/memory.js +14 -1
- package/dist/live/memory.js.map +1 -1
- package/dist/live/pub-sub.d.ts +50 -8
- package/dist/live/pub-sub.d.ts.map +1 -1
- package/dist/live/pub-sub.js +79 -26
- package/dist/live/pub-sub.js.map +1 -1
- package/dist/live/redis-bun.d.ts +6 -1
- package/dist/live/redis-bun.d.ts.map +1 -1
- package/dist/live/redis-bun.js +10 -1
- package/dist/live/redis-bun.js.map +1 -1
- package/dist/live/scoped-change-bus.d.ts +3 -1
- package/dist/live/scoped-change-bus.d.ts.map +1 -1
- package/dist/live/scoped-change-bus.js +3 -0
- package/dist/live/scoped-change-bus.js.map +1 -1
- package/dist/mastra.d.ts +16 -3
- package/dist/mastra.d.ts.map +1 -1
- package/dist/mastra.js +15 -3
- package/dist/mastra.js.map +1 -1
- package/dist/mcp/core.d.ts +31 -14
- package/dist/mcp/core.d.ts.map +1 -1
- package/dist/mcp/core.js +39 -20
- package/dist/mcp/core.js.map +1 -1
- package/dist/mcp/sdk.d.ts +6 -2
- package/dist/mcp/sdk.d.ts.map +1 -1
- package/dist/mcp/sdk.js +9 -3
- package/dist/mcp/sdk.js.map +1 -1
- package/dist/mcp/server.d.ts +6 -2
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +9 -3
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp.d.ts +3 -7
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +4 -6
- package/dist/mcp.js.map +1 -1
- package/dist/meta.d.ts +14 -2
- package/dist/meta.d.ts.map +1 -1
- package/dist/meta.js +6 -2
- package/dist/meta.js.map +1 -1
- package/dist/otel.d.ts +16 -4
- package/dist/otel.d.ts.map +1 -1
- package/dist/otel.js +40 -10
- package/dist/otel.js.map +1 -1
- package/dist/react.d.ts +6 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +5 -1
- package/dist/react.js.map +1 -1
- package/dist/route-options.d.ts.map +1 -1
- package/dist/route-options.js +4 -0
- package/dist/route-options.js.map +1 -1
- package/dist/stream-call.d.ts +15 -0
- package/dist/stream-call.d.ts.map +1 -0
- package/dist/stream-call.js +13 -0
- package/dist/stream-call.js.map +1 -0
- package/dist/timing.d.ts +2 -0
- package/dist/timing.d.ts.map +1 -1
- package/dist/timing.js +2 -0
- package/dist/timing.js.map +1 -1
- package/dist/tool-errors.d.ts +24 -0
- package/dist/tool-errors.d.ts.map +1 -0
- package/dist/tool-errors.js +36 -0
- package/dist/tool-errors.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/vue.d.ts +47 -4
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +39 -9
- package/dist/vue.js.map +1 -1
- package/package.json +2 -2
- package/src/assemble-procedure.ts +3 -1
- package/src/client.ts +26 -2
- package/src/create-fn.ts +149 -19
- package/src/expected-client-error.ts +19 -0
- package/src/hono.ts +120 -41
- package/src/index.ts +3 -2
- package/src/issues.ts +35 -0
- package/src/json-schema.ts +16 -10
- package/src/live/codec.ts +21 -19
- package/src/live/fn-live.ts +18 -36
- package/src/live/index.ts +2 -2
- package/src/live/memory.ts +22 -1
- package/src/live/pub-sub.ts +142 -37
- package/src/live/redis-bun.ts +23 -2
- package/src/live/scoped-change-bus.ts +10 -1
- package/src/mastra.ts +25 -6
- package/src/mcp/core.ts +76 -29
- package/src/mcp/sdk.ts +27 -2
- package/src/mcp/server.ts +27 -2
- package/src/mcp.ts +5 -16
- package/src/meta.ts +28 -4
- package/src/otel.ts +41 -9
- package/src/react.ts +10 -3
- package/src/route-options.ts +8 -0
- package/src/stream-call.ts +15 -0
- package/src/timing.ts +3 -0
- package/src/tool-errors.ts +52 -0
- package/src/types.ts +2 -0
- package/src/vue.ts +92 -19
package/dist/create-fn.js
CHANGED
|
@@ -4,13 +4,13 @@ import { normalizeRoute } from './route-options.js';
|
|
|
4
4
|
import { ORPCError, os } from '@orpc/server';
|
|
5
5
|
import { procedureDefinition } from './compatibility.js';
|
|
6
6
|
import { createBoundCall } from './bound-call.js';
|
|
7
|
-
import { isExpectedClientError } from './expected-client-error.js';
|
|
8
|
-
import { createFnLive } from './live/fn-live.js';
|
|
7
|
+
import { isExpectedClientError, markExpected } from './expected-client-error.js';
|
|
8
|
+
import { createFnLive, fnLivePatch } from './live/fn-live.js';
|
|
9
9
|
import { createLiveRuntime } from './live/pub-sub.js';
|
|
10
10
|
import { createDefaultLogger, protectLogger } from './logger.js';
|
|
11
11
|
import { readFnMeta } from './meta.js';
|
|
12
12
|
import { createTracing, errorAttributes, errorMessageOf } from './otel.js';
|
|
13
|
-
import { RAW_INPUT, REQUEST_TIMING } from './timing.js';
|
|
13
|
+
import { EXTRAS_OVERRIDE, RAW_INPUT, REQUEST_TIMING } from './timing.js';
|
|
14
14
|
import { createRouter } from './router.js';
|
|
15
15
|
import { RESERVED_OPTION_KEYS } from './types.js';
|
|
16
16
|
/**
|
|
@@ -19,7 +19,20 @@ import { RESERVED_OPTION_KEYS } from './types.js';
|
|
|
19
19
|
*/
|
|
20
20
|
export function createFn(options) {
|
|
21
21
|
const runtime = options;
|
|
22
|
-
const
|
|
22
|
+
const configuredExpected = runtime.isExpectedError ?? isExpectedClientError;
|
|
23
|
+
const isExpectedError = (error) => {
|
|
24
|
+
let expected;
|
|
25
|
+
try {
|
|
26
|
+
expected = configuredExpected(error);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
expected = isExpectedClientError(error);
|
|
30
|
+
}
|
|
31
|
+
if (expected)
|
|
32
|
+
markExpected(error);
|
|
33
|
+
return expected;
|
|
34
|
+
};
|
|
35
|
+
const tracing = createTracing(runtime.otel, isExpectedError);
|
|
23
36
|
const defaultLogger = protectLogger(createDefaultLogger('fn'));
|
|
24
37
|
const createLogger = (scope, span) => {
|
|
25
38
|
try {
|
|
@@ -30,7 +43,10 @@ export function createFn(options) {
|
|
|
30
43
|
return defaultLogger;
|
|
31
44
|
}
|
|
32
45
|
};
|
|
33
|
-
|
|
46
|
+
if (runtime.errorMessages !== undefined &&
|
|
47
|
+
runtime.errorMessages !== 'all' &&
|
|
48
|
+
runtime.errorMessages !== 'unexpected')
|
|
49
|
+
throw new TypeError("orpc-fn: errorMessages must be 'all' or 'unexpected'");
|
|
34
50
|
const guards = runtime.guards ?? {};
|
|
35
51
|
const isGuard = (option) => Object.hasOwn(guards, option);
|
|
36
52
|
for (const guard of Object.keys(guards)) {
|
|
@@ -61,11 +77,24 @@ export function createFn(options) {
|
|
|
61
77
|
const fn = (routeOptions) => {
|
|
62
78
|
const { handler, input, output, name, procedure, meta, stream, errors, guardResolvers, route, guardChecks } = normalizeRoute(routeOptions, isGuard);
|
|
63
79
|
const key = resolveKey(procedure, name);
|
|
80
|
+
const staticGuards = Object.fromEntries(guardChecks);
|
|
81
|
+
const dynamicGuards = Object.keys(guardResolvers);
|
|
82
|
+
runtime.routePolicy?.({
|
|
83
|
+
name,
|
|
84
|
+
procedure: key,
|
|
85
|
+
guards: staticGuards,
|
|
86
|
+
dynamicGuards,
|
|
87
|
+
meta,
|
|
88
|
+
stream,
|
|
89
|
+
tags: route.tags ?? []
|
|
90
|
+
});
|
|
64
91
|
const built = buildProcedure({
|
|
65
92
|
name,
|
|
66
93
|
procedure: key,
|
|
67
94
|
route,
|
|
68
95
|
meta,
|
|
96
|
+
guards: staticGuards,
|
|
97
|
+
dynamicGuards,
|
|
69
98
|
input,
|
|
70
99
|
output,
|
|
71
100
|
stream,
|
|
@@ -73,7 +102,7 @@ export function createFn(options) {
|
|
|
73
102
|
handler: async (params) => {
|
|
74
103
|
const { context, signal } = params;
|
|
75
104
|
const state = context[invocation];
|
|
76
|
-
const { [invocation]: _invocation, ...observedContext } = context;
|
|
105
|
+
const { [invocation]: _invocation, [EXTRAS_OVERRIDE]: overrides, ...observedContext } = context;
|
|
77
106
|
state.context = observedContext;
|
|
78
107
|
state.input = params.input;
|
|
79
108
|
state.handlerStarted = true;
|
|
@@ -126,7 +155,8 @@ export function createFn(options) {
|
|
|
126
155
|
const scoped = await runtime.extrasByProcedure?.[key]?.(extraParams);
|
|
127
156
|
const extras = {
|
|
128
157
|
...shared,
|
|
129
|
-
...scoped
|
|
158
|
+
...scoped,
|
|
159
|
+
...overrides
|
|
130
160
|
};
|
|
131
161
|
if (extras && typeof extras === 'object') {
|
|
132
162
|
for (const reserved of [
|
|
@@ -228,13 +258,7 @@ export function createFn(options) {
|
|
|
228
258
|
error: errorMessageOf(hookError)
|
|
229
259
|
});
|
|
230
260
|
}
|
|
231
|
-
|
|
232
|
-
try {
|
|
233
|
-
expected = isExpectedError(error);
|
|
234
|
-
}
|
|
235
|
-
catch {
|
|
236
|
-
expected = isExpectedClientError(error);
|
|
237
|
-
}
|
|
261
|
+
const expected = !success && isExpectedError(error);
|
|
238
262
|
const level = success ? 'info' : expected ? 'warn' : 'error';
|
|
239
263
|
logger[level]('fn.completed', {
|
|
240
264
|
...extra,
|
|
@@ -243,7 +267,9 @@ export function createFn(options) {
|
|
|
243
267
|
procedure: event.procedure,
|
|
244
268
|
duration_ms: event.durationMs,
|
|
245
269
|
status: success ? 'success' : 'failed',
|
|
246
|
-
...(
|
|
270
|
+
...(success
|
|
271
|
+
? {}
|
|
272
|
+
: errorAttributes(error, !(expected && runtime.errorMessages === 'unexpected')))
|
|
247
273
|
});
|
|
248
274
|
};
|
|
249
275
|
const live = createLiveRuntime({
|
|
@@ -252,6 +278,7 @@ export function createFn(options) {
|
|
|
252
278
|
createLogger,
|
|
253
279
|
pubsub: runtime.pubsub
|
|
254
280
|
});
|
|
281
|
+
const readMeta = (procedure) => readFnMeta(procedure);
|
|
255
282
|
// The runtimes are untyped; `Fn`, `FnLive`, `CreatePubSub` and
|
|
256
283
|
// `CreatePublisher` are their typed surface.
|
|
257
284
|
return {
|
|
@@ -261,11 +288,13 @@ export function createFn(options) {
|
|
|
261
288
|
createChannel: live.createChannel,
|
|
262
289
|
isGuard
|
|
263
290
|
}),
|
|
291
|
+
fnLivePatch,
|
|
264
292
|
createPubSub: live.createPubSub,
|
|
265
293
|
createPublisher: live.createPublisher,
|
|
266
294
|
createRouter,
|
|
267
|
-
readMeta
|
|
295
|
+
readMeta,
|
|
268
296
|
createCall: (context, signal) => createBoundCall(context, signal, tracing),
|
|
297
|
+
createTestCall: (context, { extras, signal } = {}) => createBoundCall(extras ? { ...context, [EXTRAS_OVERRIDE]: extras } : context, signal, tracing),
|
|
269
298
|
shutdown: live.shutdown,
|
|
270
299
|
drainPubSubSubscribers: live.drainPubSubSubscribers,
|
|
271
300
|
activePubSubSubscriberCount: live.activePubSubSubscriberCount
|
package/dist/create-fn.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-fn.js","sourceRoot":"","sources":["../src/create-fn.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,wBAAwB,EACxB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAqB,SAAS,EAAE,EAAE,EAAE,MAAM,cAAc,CAAA;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AACxD,OAAO,EAAkB,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,EAAE,YAAY,EAAe,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EAGN,iBAAiB,EAEjB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACN,mBAAmB,EACnB,aAAa,EAGb,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAEN,aAAa,EACb,eAAe,EACf,cAAc,EAId,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,SAAS,EAAE,cAAc,EAAyB,MAAM,aAAa,CAAA;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,O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Aa7D,CAAA;AACF,CAAC","sourcesContent":["import {\n\ttype BuilderChain,\n\tcreateProcedureAssembler\n} from './assemble-procedure.js'\nimport { isAsyncIteratorObject } from './stream.js'\nimport { normalizeRoute } from './route-options.js'\nimport { type AnyProcedure, ORPCError, os } from '@orpc/server'\nimport { procedureDefinition } from './compatibility.js'\nimport { type BoundCall, createBoundCall } from './bound-call.js'\nimport { isExpectedClientError } from './expected-client-error.js'\nimport { createFnLive, type FnLive } from './live/fn-live.js'\nimport {\n\ttype CreatePublisher,\n\ttype CreatePubSub,\n\tcreateLiveRuntime,\n\ttype PubSubRuntimeOptions\n} from './live/pub-sub.js'\nimport {\n\tcreateDefaultLogger,\n\tprotectLogger,\n\ttype FnLogger,\n\ttype LogAttributes\n} from './logger.js'\nimport { readFnMeta } from './meta.js'\nimport {\n\ttype AttributeValue,\n\tcreateTracing,\n\terrorAttributes,\n\terrorMessageOf,\n\ttype OtelApiLike,\n\ttype SpanLike,\n\ttype SpanOf\n} from './otel.js'\nimport { RAW_INPUT, REQUEST_TIMING, type InvocationTiming } from './timing.js'\nimport { createRouter } from './router.js'\nimport {\n\ttype ProcedureHookParams,\n\ttype CompletedHookEvent,\n\ttype CurrentContextOf,\n\ttype BuilderLike,\n\ttype FiniteExtras,\n\ttype ScopedExtrasGuard,\n\ttype Fn,\n\ttype FnCompletedEvent,\n\ttype FnDefinition,\n\ttype Guard,\n\ttype GuardParams,\n\ttype MaybePromise,\n\ttype RejectKeys,\n\tRESERVED_OPTION_KEYS,\n\ttype ReservedOptionKey\n} from './types.js'\n\n/** `CreateFnOptions` with the generics erased: what the implementation reads. */\ntype RuntimeOptions = {\n\tprocedures: Record<string, BuilderChain>\n\tdefault?: string\n\textras?: (params: Record<string, unknown>) => unknown\n\textrasByProcedure?: Record<\n\t\tstring,\n\t\t(params: Record<string, unknown>) => unknown\n\t>\n\tguards?: Record<\n\t\tstring,\n\t\t(value: unknown, params: GuardParams<unknown, unknown>) => unknown\n\t>\n\totel?: OtelApiLike\n\tlogger?: (scope: string, span: SpanLike | undefined) => FnLogger\n\tspanAttributes?: (params: {\n\t\tcontext: unknown\n\t\tname: string\n\t\tprocedure: string\n\t\tmeta: Record<string, unknown>\n\t}) => Record<string, AttributeValue | undefined>\n\tonCompleted?: (event: FnCompletedEvent) => LogAttributes | undefined\n\tisExpectedError?: (error: unknown) => boolean\n\tpubsub?: PubSubRuntimeOptions\n}\n\nexport type CreateFnOptions<\n\tTProcedures extends Record<string, BuilderLike>,\n\tTDefault extends keyof TProcedures & string,\n\tTExtras extends object,\n\tTGuards,\n\tTMeta extends object,\n\tTTag extends string,\n\tTOtel extends OtelApiLike | undefined,\n\tTLogger extends FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n> = {\n\t/** Named oRPC builders, e.g. `{ public: os.$context<Ctx>(), protected: authed }`. */\n\tprocedures: TProcedures\n\t/**\n\t * Builder used when a route omits `procedure`. Leave it out to make every\n\t * route name its procedure, so none gets one by accident.\n\t */\n\tdefault?: TDefault\n\t/** Per-call values merged into every handler's params; may be async. */\n\textras?: (\n\t\tparams: ProcedureHookParams<\n\t\t\tTProcedures,\n\t\t\t{\n\t\t\t\tspan: SpanOf<TOtel> | undefined\n\t\t\t\tsignal: AbortSignal | undefined\n\t\t\t\tname: string\n\t\t\t}\n\t\t>\n\t) => MaybePromise<TExtras>\n\textrasByProcedure?: {\n\t\t[K in keyof TScoped & keyof TProcedures]: (params: {\n\t\t\tcontext: CurrentContextOf<TProcedures[K]>\n\t\t\tspan: SpanOf<TOtel> | undefined\n\t\t\tsignal: AbortSignal | undefined\n\t\t\tname: string\n\t\t\tprocedure: K\n\t\t}) => MaybePromise<TScoped[K] & ScopedExtrasGuard<NoInfer<TScoped[K]>>>\n\t}\n\t/**\n\t * Checks run before the handler; each key becomes a typed `fn()` option.\n\t * Keys must not reuse route options. False denies, true or void allows.\n\t */\n\tguards?: TGuards &\n\t\tRecord<\n\t\t\tstring,\n\t\t\tGuard<\n\t\t\t\tnever,\n\t\t\t\tProcedureHookParams<\n\t\t\t\t\tTProcedures,\n\t\t\t\t\tOmit<GuardParams<unknown, TMeta>, 'context' | 'procedure'>\n\t\t\t\t>\n\t\t\t>\n\t\t> &\n\t\tRejectKeys<\n\t\t\tNoInfer<TGuards>,\n\t\t\tReservedOptionKey,\n\t\t\t'orpc-fn: this guard name is a route option'\n\t\t>\n\t/**\n\t * Typed route metadata contract: `meta: defineMeta<AppMeta>()`.\n\t * Route declarations place values inside their own `meta` object.\n\t */\n\tmeta?: TMeta\n\t/** Allowed route tags: `tags: ['internal', 'external']`. */\n\ttags?: readonly TTag[]\n\t/** `import * as otel from '@opentelemetry/api'`. Omit for no tracing. */\n\totel?: TOtel\n\tlogger?: (scope: string, span: SpanOf<TOtel> | undefined) => TLogger\n\t/** Extra span attributes per call; undefined values are skipped. */\n\tspanAttributes?: (\n\t\tparams: ProcedureHookParams<\n\t\t\tTProcedures,\n\t\t\t{ name: string; meta: Partial<TMeta> }\n\t\t>\n\t) => Record<string, AttributeValue | undefined>\n\t/** Called after the procedure (or stream) settles; returned attributes join the `fn.completed` log. */\n\tonCompleted?: (\n\t\tevent: CompletedHookEvent<TProcedures, SpanOf<TOtel>>\n\t\t// biome-ignore lint/suspicious/noConfusingVoidType: lets hooks that return nothing type-check\n\t) => LogAttributes | undefined | void\n\t/** Errors that are the caller's fault, logged at `warn`. Default: 4xx `ORPCError` or `AbortError`. */\n\tisExpectedError?: (error: unknown) => boolean\n\t/** Needed for `fnLive` and `createPubSub`. */\n\tpubsub?: PubSubRuntimeOptions\n}\n\ntype Definition<\n\tTProcedures extends Record<string, BuilderLike>,\n\tTDefault extends string,\n\tTExtras extends object,\n\tTGuards,\n\tTMeta extends object,\n\tTTag extends string,\n\tTOtel,\n\tTLogger extends FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n> = {\n\tprocedures: TProcedures\n\tdefault: TDefault\n\textras: TExtras\n\textrasByProcedure: TScoped\n\tguards: TGuards\n\tmeta: TMeta\n\ttag: TTag\n\tspan: SpanOf<TOtel>\n\tlogger: TLogger\n}\n\nexport type FnFactory<TDef extends FnDefinition> = {\n\tfn: Fn<TDef>\n\tfnLive: FnLive<TDef>\n\tcreatePubSub: CreatePubSub<TDef>\n\tcreatePublisher: CreatePublisher\n\tcreateRouter: typeof createRouter\n\treadMeta: typeof readFnMeta\n\t/**\n\t * A `call` outside any handler (seeders, scripts, tests), traced with this\n\t * instance's OpenTelemetry.\n\t */\n\tcreateCall: <TContext>(\n\t\tcontext: TContext,\n\t\tsignal?: AbortSignal\n\t) => BoundCall<TContext>\n\t/** Terminal, idempotent shutdown: close streams and await owned resources. */\n\tshutdown: () => Promise<void>\n\tdrainPubSubSubscribers: () => number\n\tactivePubSubSubscriberCount: () => number\n}\n\n/**\n * Builds the app's `fn` and friends once. Everything app-specific (auth,\n * context, database, queues, i18n, permissions) is injected here.\n */\nexport function createFn<\n\tconst TProcedures extends Record<string, BuilderLike>,\n\tTDefault extends keyof TProcedures & string = never,\n\tTExtras extends object = Record<never, never>,\n\tTGuards extends object = Record<never, never>,\n\tTMeta extends object = Record<never, never>,\n\tconst TTag extends string = string,\n\tTOtel extends OtelApiLike | undefined = undefined,\n\tTLogger extends FnLogger = FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n>(\n\toptions: CreateFnOptions<\n\t\tTProcedures,\n\t\tTDefault,\n\t\tTExtras,\n\t\tTGuards,\n\t\tTMeta,\n\t\tTTag,\n\t\tTOtel,\n\t\tTLogger,\n\t\tTScoped\n\t> &\n\t\tFiniteExtras<NoInfer<TExtras>>\n): FnFactory<\n\tDefinition<\n\t\tTProcedures,\n\t\tTDefault,\n\t\tTExtras,\n\t\tTGuards,\n\t\tTMeta,\n\t\tTTag,\n\t\tTOtel,\n\t\tTLogger,\n\t\tTScoped\n\t>\n> {\n\tconst runtime = options as unknown as RuntimeOptions\n\tconst tracing = createTracing(runtime.otel)\n\tconst defaultLogger = protectLogger(createDefaultLogger('fn'))\n\tconst createLogger = (\n\t\tscope: string,\n\t\tspan: SpanLike | undefined\n\t): FnLogger => {\n\t\ttry {\n\t\t\treturn protectLogger(\n\t\t\t\truntime.logger?.(scope, span) ??\n\t\t\t\t\t(scope === 'fn' ? defaultLogger : createDefaultLogger(scope))\n\t\t\t)\n\t\t} catch {\n\t\t\treturn defaultLogger\n\t\t}\n\t}\n\tconst isExpectedError = runtime.isExpectedError ?? isExpectedClientError\n\tconst guards = runtime.guards ?? {}\n\tconst isGuard = (option: string) => Object.hasOwn(guards, option)\n\tfor (const guard of Object.keys(guards)) {\n\t\tif (typeof guards[guard] !== 'function')\n\t\t\tthrow new TypeError(`orpc-fn: guard ${guard} must be a function`)\n\t\tif ((RESERVED_OPTION_KEYS as readonly string[]).includes(guard)) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: guard \"${guard}\" reuses a route option name; rename it`\n\t\t\t)\n\t\t}\n\t}\n\tif (runtime.default && !Object.hasOwn(runtime.procedures, runtime.default))\n\t\tthrow new TypeError(`orpc-fn: unknown default procedure ${runtime.default}`)\n\tfor (const [key, builder] of Object.entries(runtime.procedures)) {\n\t\tconst def = procedureDefinition(builder)\n\t\tif (def?.inputSchema !== undefined || def?.outputSchema !== undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: procedure \"${key}\" sets an input or output schema; set schemas on routes instead`\n\t\t\t)\n\t\t}\n\t}\n\n\tconst resolveKey = (procedure: string | undefined, name: string) => {\n\t\tconst key = procedure ?? runtime.default\n\t\tif (key === undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: \"${name}\" needs a procedure; createFn has no default`\n\t\t\t)\n\t\t}\n\t\treturn key\n\t}\n\n\tconst buildProcedure = createProcedureAssembler(\n\t\truntime.procedures,\n\t\tresolveKey\n\t)\n\n\tconst invocation = Symbol('orpc-fn.invocation')\n\ttype Invocation = {\n\t\tspan: SpanLike | undefined\n\t\tlogger: FnLogger\n\t\tinput: unknown\n\t\tcontext: unknown\n\t\thandlerStarted: boolean\n\t\trawInput: unknown\n\t}\n\t// The overload-to-runtime boundary is adapted once at the returned factory.\n\tconst fn = (routeOptions: Record<string, unknown>): AnyProcedure => {\n\t\tconst {\n\t\t\thandler,\n\t\t\tinput,\n\t\t\toutput,\n\t\t\tname,\n\t\t\tprocedure,\n\t\t\tmeta,\n\t\t\tstream,\n\t\t\terrors,\n\t\t\tguardResolvers,\n\t\t\troute,\n\t\t\tguardChecks\n\t\t} = normalizeRoute(routeOptions, isGuard)\n\t\tconst key = resolveKey(procedure, name)\n\t\tconst built = buildProcedure({\n\t\t\tname,\n\t\t\tprocedure: key,\n\t\t\troute,\n\t\t\tmeta,\n\t\t\tinput,\n\t\t\toutput,\n\t\t\tstream,\n\t\t\t...(errors ? { errors } : {}),\n\t\t\thandler: async (params) => {\n\t\t\t\tconst { context, signal } = params\n\t\t\t\tconst state = context[invocation] as Invocation\n\t\t\t\tconst { [invocation]: _invocation, ...observedContext } = context\n\t\t\t\tstate.context = observedContext\n\t\t\t\tstate.input = params.input\n\t\t\t\tstate.handlerStarted = true\n\t\t\t\ttry {\n\t\t\t\t\tconst attributes = runtime.spanAttributes?.({\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\tmeta\n\t\t\t\t\t})\n\t\t\t\t\tfor (const [attribute, value] of Object.entries(attributes ?? {}))\n\t\t\t\t\t\tif (value !== undefined) state.span?.setAttribute(attribute, value)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tstate.logger.warn('spanAttributes hook failed', {\n\t\t\t\t\t\terror: errorMessageOf(error)\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconst resolvedChecks = [...guardChecks]\n\t\t\t\tfor (const [guard, resolver] of Object.entries(guardResolvers)) {\n\t\t\t\t\tconst value = await resolver({ input: params.input, context, signal })\n\t\t\t\t\tif (value !== undefined) resolvedChecks.push([guard, value])\n\t\t\t\t}\n\t\t\t\tfor (const [guard, value] of resolvedChecks) {\n\t\t\t\t\tconst result = await guards[guard]?.(value, {\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t\tinput: params.input,\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\tmeta,\n\t\t\t\t\t\tsignal\n\t\t\t\t\t})\n\t\t\t\t\tif (result === false)\n\t\t\t\t\t\tthrow new ORPCError('FORBIDDEN', {\n\t\t\t\t\t\t\tmessage: `Guard \"${guard}\" denied this call`\n\t\t\t\t\t\t})\n\t\t\t\t\tif (result !== undefined && result !== true)\n\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t`orpc-fn: guard \"${guard}\" must return void or boolean`\n\t\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst extraParams = {\n\t\t\t\t\tcontext,\n\t\t\t\t\tspan: state.span,\n\t\t\t\t\tsignal,\n\t\t\t\t\tname,\n\t\t\t\t\tprocedure: key\n\t\t\t\t}\n\t\t\t\tconst shared = await runtime.extras?.(extraParams)\n\t\t\t\tconst scoped = await runtime.extrasByProcedure?.[key]?.(extraParams)\n\t\t\t\tconst extras = {\n\t\t\t\t\t...(shared as object | undefined),\n\t\t\t\t\t...(scoped as object | undefined)\n\t\t\t\t}\n\t\t\t\tif (extras && typeof extras === 'object') {\n\t\t\t\t\tfor (const reserved of [\n\t\t\t\t\t\t'input',\n\t\t\t\t\t\t'context',\n\t\t\t\t\t\t'call',\n\t\t\t\t\t\t'signal',\n\t\t\t\t\t\t'span',\n\t\t\t\t\t\t'logger',\n\t\t\t\t\t\t'errors',\n\t\t\t\t\t\t'lastEventId'\n\t\t\t\t\t]) {\n\t\t\t\t\t\tif (Object.hasOwn(extras, reserved))\n\t\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t\t`orpc-fn: extra \"${reserved}\" shadows a handler parameter`\n\t\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn handler({\n\t\t\t\t\t...(extras as object | undefined),\n\t\t\t\t\tinput: params.input,\n\t\t\t\t\tcontext,\n\t\t\t\t\tcall: createBoundCall(context, signal, tracing),\n\t\t\t\t\tsignal,\n\t\t\t\t\tspan: state.span,\n\t\t\t\t\tlogger: state.logger,\n\t\t\t\t\terrors: params.errors,\n\t\t\t\t\tlastEventId: params.lastEventId,\n\t\t\t\t\t[RAW_INPUT]: state.rawInput\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\t\t// Prepending middleware shifts oRPC's validation indexes, covering auth,\n\t\t// input validation, output validation, and the final stream lifetime.\n\t\treturn os\n\t\t\t.use(async ({ context, next }, rawInput) => {\n\t\t\t\tconst started = performance.now()\n\t\t\t\tconst state: Invocation = {\n\t\t\t\t\tspan: undefined,\n\t\t\t\t\tlogger: defaultLogger,\n\t\t\t\t\tinput: undefined,\n\t\t\t\t\tcontext,\n\t\t\t\t\thandlerStarted: false,\n\t\t\t\t\trawInput\n\t\t\t\t}\n\t\t\t\tconst result = await tracing.inSpan(\n\t\t\t\t\tname,\n\t\t\t\t\t'SERVER',\n\t\t\t\t\tasync (span) => {\n\t\t\t\t\t\tstate.span = span\n\t\t\t\t\t\tstate.logger = createLogger('fn', span)\n\t\t\t\t\t\tspan?.setAttribute('fn.operation', name)\n\t\t\t\t\t\tspan?.setAttribute('fn.procedure', key)\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await next({ context: { [invocation]: state } })\n\t\t\t\t\t\t\tif (isAsyncIteratorObject(result.output) !== stream) {\n\t\t\t\t\t\t\t\tif (isAsyncIteratorObject(result.output)) {\n\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\tawait result.output.return?.()\n\t\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\t\t/* Release the unexpected stream. */\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t\t\t`orpc-fn: ${name} returned a result inconsistent with stream; declare stream: true for iterators`\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn result.output\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tconst timing = (context as Record<symbol, unknown>)[\n\t\t\t\t\t\t\t\tREQUEST_TIMING\n\t\t\t\t\t\t\t] as InvocationTiming | undefined\n\t\t\t\t\t\t\tif (timing && !(context as Record<symbol, unknown>)[invocation]) {\n\t\t\t\t\t\t\t\ttiming.procedureMs += performance.now() - started\n\t\t\t\t\t\t\t\ttiming.calls++\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t(span, ...error) => {\n\t\t\t\t\t\tconst durationMs =\n\t\t\t\t\t\t\tMath.round((performance.now() - started) * 100) / 100\n\t\t\t\t\t\tspan?.setAttribute('fn.duration_ms', durationMs)\n\t\t\t\t\t\tlogCompleted(state.logger, {\n\t\t\t\t\t\t\tname,\n\t\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\t\tsuccess: error.length === 0,\n\t\t\t\t\t\t\terror: error[0],\n\t\t\t\t\t\t\tinput: state.input,\n\t\t\t\t\t\t\tcontext: state.context,\n\t\t\t\t\t\t\tspan,\n\t\t\t\t\t\t\tmeta,\n\t\t\t\t\t\t\thandlerStarted: state.handlerStarted\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t)\n\t\t\t\treturn { output: result, context }\n\t\t\t})\n\t\t\t.router({ route: built }).route\n\t}\n\n\tconst logCompleted = (logger: FnLogger, event: FnCompletedEvent) => {\n\t\tconst { error, success } = event\n\t\tlet extra: LogAttributes | undefined\n\t\ttry {\n\t\t\textra = runtime.onCompleted?.(event)\n\t\t} catch (hookError) {\n\t\t\tlogger.warn('onCompleted hook failed', {\n\t\t\t\terror: errorMessageOf(hookError)\n\t\t\t})\n\t\t}\n\t\tlet expected = false\n\t\ttry {\n\t\t\texpected = isExpectedError(error)\n\t\t} catch {\n\t\t\texpected = isExpectedClientError(error)\n\t\t}\n\t\tconst level = success ? 'info' : expected ? 'warn' : 'error'\n\t\tlogger[level]('fn.completed', {\n\t\t\t...extra,\n\t\t\tevent: 'fn.completed',\n\t\t\toperation: event.name,\n\t\t\tprocedure: event.procedure,\n\t\t\tduration_ms: event.durationMs,\n\t\t\tstatus: success ? 'success' : 'failed',\n\t\t\t...(error === undefined ? {} : errorAttributes(error))\n\t\t})\n\t}\n\n\tconst live = createLiveRuntime({\n\t\tfn,\n\t\ttracing,\n\t\tcreateLogger,\n\t\tpubsub: runtime.pubsub\n\t})\n\n\t// The runtimes are untyped; `Fn`, `FnLive`, `CreatePubSub` and\n\t// `CreatePublisher` are their typed surface.\n\treturn {\n\t\tfn,\n\t\tfnLive: createFnLive({\n\t\t\tfn,\n\t\t\tcreateChannel: live.createChannel,\n\t\t\tisGuard\n\t\t}),\n\t\tcreatePubSub: live.createPubSub,\n\t\tcreatePublisher: live.createPublisher,\n\t\tcreateRouter,\n\t\treadMeta: readFnMeta,\n\t\tcreateCall: <TContext>(context: TContext, signal?: AbortSignal) =>\n\t\t\tcreateBoundCall(context, signal, tracing),\n\t\tshutdown: live.shutdown,\n\t\tdrainPubSubSubscribers: live.drainPubSubSubscribers,\n\t\tactivePubSubSubscriberCount: live.activePubSubSubscriberCount\n\t} as unknown as FnFactory<\n\t\tDefinition<\n\t\t\tTProcedures,\n\t\t\tTDefault,\n\t\t\tTExtras,\n\t\t\tTGuards,\n\t\t\tTMeta,\n\t\t\tTTag,\n\t\t\tTOtel,\n\t\t\tTLogger,\n\t\t\tTScoped\n\t\t>\n\t>\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-fn.js","sourceRoot":"","sources":["../src/create-fn.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,wBA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AAC,CAAA;IAE3E,+DAA+D;IAC/D,6CAA6C;IAC7C,OAAO;QACN,EAAE;QACF,MAAM,EAAE,YAAY,CAAC;YACpB,EAAE;YACF,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,OAAO;SACP,CAAC;QACF,WAAW;QACX,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,eAAe,EAAE,IAAI,CAAC,eAAe;QACrC,YAAY;QACZ,QAAQ;QACR,UAAU,EAAE,CAAW,OAAiB,EAAE,MAAoB,EAAE,EAAE,CACjE,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC;QAC1C,cAAc,EAAE,CACf,OAAiB,EACjB,EAAE,MAAM,EAAE,MAAM,EAAE,GAA8C,EAAE,EACjE,EAAE,CACH,eAAe,CACd,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,EAC5D,MAAM,EACN,OAAO,CACP;QACF,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,sBAAsB,EAAE,IAAI,CAAC,sBAAsB;QACnD,2BAA2B,EAAE,IAAI,CAAC,2BAA2B;KAa7D,CAAA;AACF,CAAC","sourcesContent":["import {\n\ttype BuilderChain,\n\tcreateProcedureAssembler\n} from './assemble-procedure.js'\nimport { isAsyncIteratorObject } from './stream.js'\nimport { normalizeRoute } from './route-options.js'\nimport { type AnyProcedure, ORPCError, os } from '@orpc/server'\nimport { procedureDefinition } from './compatibility.js'\nimport { type BoundCall, createBoundCall } from './bound-call.js'\nimport { isExpectedClientError, markExpected } from './expected-client-error.js'\nimport { createFnLive, type FnLive, fnLivePatch } from './live/fn-live.js'\nimport {\n\ttype CreatePublisher,\n\ttype CreatePubSub,\n\tcreateLiveRuntime,\n\ttype PubSubRuntimeOptions\n} from './live/pub-sub.js'\nimport {\n\tcreateDefaultLogger,\n\tprotectLogger,\n\ttype FnLogger,\n\ttype LogAttributes\n} from './logger.js'\nimport { type FnMeta, readFnMeta } from './meta.js'\nimport {\n\ttype AttributeValue,\n\tcreateTracing,\n\terrorAttributes,\n\terrorMessageOf,\n\ttype OtelApiLike,\n\ttype SpanLike,\n\ttype SpanOf\n} from './otel.js'\nimport {\n\tEXTRAS_OVERRIDE,\n\tRAW_INPUT,\n\tREQUEST_TIMING,\n\ttype InvocationTiming\n} from './timing.js'\nimport { createRouter } from './router.js'\nimport {\n\ttype ProcedureHookParams,\n\ttype CompletedHookEvent,\n\ttype CurrentContextOf,\n\ttype BuilderLike,\n\ttype FiniteExtras,\n\ttype ScopedExtrasGuard,\n\ttype Fn,\n\ttype FnCompletedEvent,\n\ttype FnDefinition,\n\ttype Guard,\n\ttype GuardOptions,\n\ttype GuardParams,\n\ttype MaybePromise,\n\ttype RejectKeys,\n\tRESERVED_OPTION_KEYS,\n\ttype ReservedOptionKey\n} from './types.js'\n\n/** `CreateFnOptions` with the generics erased: what the implementation reads. */\ntype RuntimeOptions = {\n\tprocedures: Record<string, BuilderChain>\n\tdefault?: string\n\textras?: (params: Record<string, unknown>) => unknown\n\textrasByProcedure?: Record<\n\t\tstring,\n\t\t(params: Record<string, unknown>) => unknown\n\t>\n\tguards?: Record<\n\t\tstring,\n\t\t(value: unknown, params: GuardParams<unknown, unknown>) => unknown\n\t>\n\totel?: OtelApiLike\n\tlogger?: (scope: string, span: SpanLike | undefined) => FnLogger\n\tspanAttributes?: (params: {\n\t\tcontext: unknown\n\t\tname: string\n\t\tprocedure: string\n\t\tmeta: Record<string, unknown>\n\t}) => Record<string, AttributeValue | undefined>\n\tonCompleted?: (event: FnCompletedEvent) => LogAttributes | undefined\n\tisExpectedError?: (error: unknown) => boolean\n\terrorMessages?: 'all' | 'unexpected'\n\troutePolicy?: (route: Record<string, unknown>) => void\n\tpubsub?: PubSubRuntimeOptions\n}\n\n/** What `routePolicy` sees of each route when it is defined. */\nexport type RoutePolicyParams<\n\tTKey extends string = string,\n\tTGuards = Record<string, unknown>,\n\tTMeta extends object = Record<string, unknown>,\n\tTTag extends string = string\n> = {\n\tname: string\n\tprocedure: TKey\n\t/** Guard values set on the route itself. */\n\tguards: GuardOptions<TGuards>\n\t/** Guards the route decides per call through `guardResolvers`. */\n\tdynamicGuards: readonly (keyof TGuards & string)[]\n\tmeta: Partial<TMeta>\n\tstream: boolean\n\ttags: readonly TTag[]\n}\n\nexport type CreateFnOptions<\n\tTProcedures extends Record<string, BuilderLike>,\n\tTDefault extends keyof TProcedures & string,\n\tTExtras extends object,\n\tTGuards,\n\tTMeta extends object,\n\tTTag extends string,\n\tTOtel extends OtelApiLike | undefined,\n\tTLogger extends FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n> = {\n\t/** Named oRPC builders, e.g. `{ public: os.$context<Ctx>(), protected: authed }`. */\n\tprocedures: TProcedures\n\t/**\n\t * Builder used when a route omits `procedure`. Leave it out to make every\n\t * route name its procedure, so none gets one by accident.\n\t */\n\tdefault?: TDefault\n\t/** Per-call values merged into every handler's params; may be async. */\n\textras?: (\n\t\tparams: ProcedureHookParams<\n\t\t\tTProcedures,\n\t\t\t{\n\t\t\t\tspan: SpanOf<TOtel> | undefined\n\t\t\t\tsignal: AbortSignal | undefined\n\t\t\t\tname: string\n\t\t\t}\n\t\t>\n\t) => MaybePromise<TExtras>\n\textrasByProcedure?: {\n\t\t[K in keyof TScoped & keyof TProcedures]: (params: {\n\t\t\tcontext: CurrentContextOf<TProcedures[K]>\n\t\t\tspan: SpanOf<TOtel> | undefined\n\t\t\tsignal: AbortSignal | undefined\n\t\t\tname: string\n\t\t\tprocedure: K\n\t\t}) => MaybePromise<TScoped[K] & ScopedExtrasGuard<NoInfer<TScoped[K]>>>\n\t}\n\t/**\n\t * Checks run before the handler; each key becomes a typed `fn()` option.\n\t * Keys must not reuse route options. False denies, true or void allows.\n\t */\n\tguards?: TGuards &\n\t\tRecord<\n\t\t\tstring,\n\t\t\tGuard<\n\t\t\t\tnever,\n\t\t\t\tProcedureHookParams<\n\t\t\t\t\tTProcedures,\n\t\t\t\t\tOmit<GuardParams<unknown, TMeta>, 'context' | 'procedure'>\n\t\t\t\t>\n\t\t\t>\n\t\t> &\n\t\tRejectKeys<\n\t\t\tNoInfer<TGuards>,\n\t\t\tReservedOptionKey,\n\t\t\t'orpc-fn: this guard name is a route option'\n\t\t>\n\t/**\n\t * Typed route metadata contract: `meta: defineMeta<AppMeta>()`.\n\t * Route declarations place values inside their own `meta` object.\n\t */\n\tmeta?: TMeta\n\t/** Allowed route tags: `tags: ['internal', 'external']`. */\n\ttags?: readonly TTag[]\n\t/** `import * as otel from '@opentelemetry/api'`. Omit for no tracing. */\n\totel?: TOtel\n\tlogger?: (scope: string, span: SpanOf<TOtel> | undefined) => TLogger\n\t/** Extra span attributes per call; undefined values are skipped. */\n\tspanAttributes?: (\n\t\tparams: ProcedureHookParams<\n\t\t\tTProcedures,\n\t\t\t{ name: string; meta: Partial<TMeta> }\n\t\t>\n\t) => Record<string, AttributeValue | undefined>\n\t/** Called after the procedure (or stream) settles; returned attributes join the `fn.completed` log. */\n\tonCompleted?: (\n\t\tevent: CompletedHookEvent<TProcedures, SpanOf<TOtel>>\n\t\t// biome-ignore lint/suspicious/noConfusingVoidType: lets hooks that return nothing type-check\n\t) => LogAttributes | undefined | void\n\t/**\n\t * Errors that are the caller's fault: logged at `warn`, and their span keeps\n\t * an unset status. `mountOrpc` and the tool adapters follow it for errors\n\t * a route threw. Default: 4xx `ORPCError` or `AbortError`.\n\t */\n\tisExpectedError?: (error: unknown) => boolean\n\t/**\n\t * `'unexpected'` leaves the message of expected errors out of `fn.completed`\n\t * (it was written for the caller and may name what they asked for);\n\t * `error_code` and `error_type` stay. Default: `'all'`.\n\t */\n\terrorMessages?: 'all' | 'unexpected'\n\t/**\n\t * Sees every route (`fn`, `fnLive`, pub/sub subscriptions) when it is\n\t * defined; throw to reject it, e.g. a protected route with no permission:\n\t * `if (procedure === 'protected' && !('permission' in guards)) throw ...`.\n\t */\n\troutePolicy?: (\n\t\troute: RoutePolicyParams<\n\t\t\tkeyof TProcedures & string,\n\t\t\tNoInfer<TGuards>,\n\t\t\tNoInfer<TMeta>,\n\t\t\tNoInfer<TTag>\n\t\t>\n\t) => void\n\t/** Needed for `fnLive` and `createPubSub`. */\n\tpubsub?: PubSubRuntimeOptions\n}\n\ntype Definition<\n\tTProcedures extends Record<string, BuilderLike>,\n\tTDefault extends string,\n\tTExtras extends object,\n\tTGuards,\n\tTMeta extends object,\n\tTTag extends string,\n\tTOtel,\n\tTLogger extends FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n> = {\n\tprocedures: TProcedures\n\tdefault: TDefault\n\textras: TExtras\n\textrasByProcedure: TScoped\n\tguards: TGuards\n\tmeta: TMeta\n\ttag: TTag\n\tspan: SpanOf<TOtel>\n\tlogger: TLogger\n}\n\n/** Extras a test call may replace: any shared or scoped extra. */\nexport type TestCallExtras<TDef extends FnDefinition> = Partial<\n\tTDef['extras']\n> &\n\t(TDef extends { extrasByProcedure: infer E }\n\t\t? [keyof E] extends [never]\n\t\t\t? object\n\t\t\t: Partial<NonNullable<E[keyof E]>>\n\t\t: object)\n\nexport type FnFactory<TDef extends FnDefinition> = {\n\tfn: Fn<TDef>\n\tfnLive: FnLive<TDef>\n\t/** Same as `fnLivePatch` from `orpc-fn/live`. */\n\tfnLivePatch: typeof fnLivePatch\n\tcreatePubSub: CreatePubSub<TDef>\n\tcreatePublisher: CreatePublisher\n\tcreateRouter: typeof createRouter\n\t/** `readFnMeta`, with guard values typed by this factory's guards. */\n\treadMeta: <T extends AnyProcedure>(\n\t\tprocedure: T\n\t) => Omit<ReturnType<typeof readFnMeta<T>>, 'guards' | 'dynamicGuards'> & {\n\t\tguards: GuardOptions<TDef['guards']>\n\t\tdynamicGuards: readonly (keyof TDef['guards'] & string)[]\n\t}\n\t/**\n\t * A `call` outside any handler (seeders, scripts, tests), traced with this\n\t * instance's OpenTelemetry.\n\t */\n\tcreateCall: <TContext>(\n\t\tcontext: TContext,\n\t\tsignal?: AbortSignal\n\t) => BoundCall<TContext>\n\t/**\n\t * `createCall` for tests: middleware, guards and schemas run as in\n\t * production, and `extras` replace the configured ones (`{ db: fakeDb }`)\n\t * for this call and the calls it makes.\n\t */\n\tcreateTestCall: <TContext>(\n\t\tcontext: TContext,\n\t\toptions?: { extras?: TestCallExtras<TDef>; signal?: AbortSignal }\n\t) => BoundCall<TContext>\n\t/** Terminal, idempotent shutdown: close streams and await owned resources. */\n\tshutdown: () => Promise<void>\n\t/**\n\t * End every open subscription and return how many were closed. With\n\t * `overMs`, spread the closes evenly over that window so clients do not\n\t * reconnect in one burst; the promise resolves after the last one.\n\t */\n\tdrainPubSubSubscribers: {\n\t\t(): number\n\t\t(options: { overMs: number }): Promise<number>\n\t}\n\tactivePubSubSubscriberCount: () => number\n}\n\n/**\n * Builds the app's `fn` and friends once. Everything app-specific (auth,\n * context, database, queues, i18n, permissions) is injected here.\n */\nexport function createFn<\n\tconst TProcedures extends Record<string, BuilderLike>,\n\tTDefault extends keyof TProcedures & string = never,\n\tTExtras extends object = Record<never, never>,\n\tTGuards extends object = Record<never, never>,\n\tTMeta extends object = Record<never, never>,\n\tconst TTag extends string = string,\n\tTOtel extends OtelApiLike | undefined = undefined,\n\tTLogger extends FnLogger = FnLogger,\n\tTScoped extends Partial<Record<keyof TProcedures, object>> = Record<\n\t\tnever,\n\t\tnever\n\t>\n>(\n\toptions: CreateFnOptions<\n\t\tTProcedures,\n\t\tTDefault,\n\t\tTExtras,\n\t\tTGuards,\n\t\tTMeta,\n\t\tTTag,\n\t\tTOtel,\n\t\tTLogger,\n\t\tTScoped\n\t> &\n\t\tFiniteExtras<NoInfer<TExtras>>\n): FnFactory<\n\tDefinition<\n\t\tTProcedures,\n\t\tTDefault,\n\t\tTExtras,\n\t\tTGuards,\n\t\tTMeta,\n\t\tTTag,\n\t\tTOtel,\n\t\tTLogger,\n\t\tTScoped\n\t>\n> {\n\tconst runtime = options as unknown as RuntimeOptions\n\tconst configuredExpected = runtime.isExpectedError ?? isExpectedClientError\n\tconst isExpectedError = (error: unknown) => {\n\t\tlet expected: boolean\n\t\ttry {\n\t\t\texpected = configuredExpected(error)\n\t\t} catch {\n\t\t\texpected = isExpectedClientError(error)\n\t\t}\n\t\tif (expected) markExpected(error)\n\t\treturn expected\n\t}\n\tconst tracing = createTracing(runtime.otel, isExpectedError)\n\tconst defaultLogger = protectLogger(createDefaultLogger('fn'))\n\tconst createLogger = (\n\t\tscope: string,\n\t\tspan: SpanLike | undefined\n\t): FnLogger => {\n\t\ttry {\n\t\t\treturn protectLogger(\n\t\t\t\truntime.logger?.(scope, span) ??\n\t\t\t\t\t(scope === 'fn' ? defaultLogger : createDefaultLogger(scope))\n\t\t\t)\n\t\t} catch {\n\t\t\treturn defaultLogger\n\t\t}\n\t}\n\tif (\n\t\truntime.errorMessages !== undefined &&\n\t\truntime.errorMessages !== 'all' &&\n\t\truntime.errorMessages !== 'unexpected'\n\t)\n\t\tthrow new TypeError(\"orpc-fn: errorMessages must be 'all' or 'unexpected'\")\n\tconst guards = runtime.guards ?? {}\n\tconst isGuard = (option: string) => Object.hasOwn(guards, option)\n\tfor (const guard of Object.keys(guards)) {\n\t\tif (typeof guards[guard] !== 'function')\n\t\t\tthrow new TypeError(`orpc-fn: guard ${guard} must be a function`)\n\t\tif ((RESERVED_OPTION_KEYS as readonly string[]).includes(guard)) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: guard \"${guard}\" reuses a route option name; rename it`\n\t\t\t)\n\t\t}\n\t}\n\tif (runtime.default && !Object.hasOwn(runtime.procedures, runtime.default))\n\t\tthrow new TypeError(`orpc-fn: unknown default procedure ${runtime.default}`)\n\tfor (const [key, builder] of Object.entries(runtime.procedures)) {\n\t\tconst def = procedureDefinition(builder)\n\t\tif (def?.inputSchema !== undefined || def?.outputSchema !== undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: procedure \"${key}\" sets an input or output schema; set schemas on routes instead`\n\t\t\t)\n\t\t}\n\t}\n\n\tconst resolveKey = (procedure: string | undefined, name: string) => {\n\t\tconst key = procedure ?? runtime.default\n\t\tif (key === undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`orpc-fn: \"${name}\" needs a procedure; createFn has no default`\n\t\t\t)\n\t\t}\n\t\treturn key\n\t}\n\n\tconst buildProcedure = createProcedureAssembler(\n\t\truntime.procedures,\n\t\tresolveKey\n\t)\n\n\tconst invocation = Symbol('orpc-fn.invocation')\n\ttype Invocation = {\n\t\tspan: SpanLike | undefined\n\t\tlogger: FnLogger\n\t\tinput: unknown\n\t\tcontext: unknown\n\t\thandlerStarted: boolean\n\t\trawInput: unknown\n\t}\n\t// The overload-to-runtime boundary is adapted once at the returned factory.\n\tconst fn = (routeOptions: Record<string, unknown>): AnyProcedure => {\n\t\tconst {\n\t\t\thandler,\n\t\t\tinput,\n\t\t\toutput,\n\t\t\tname,\n\t\t\tprocedure,\n\t\t\tmeta,\n\t\t\tstream,\n\t\t\terrors,\n\t\t\tguardResolvers,\n\t\t\troute,\n\t\t\tguardChecks\n\t\t} = normalizeRoute(routeOptions, isGuard)\n\t\tconst key = resolveKey(procedure, name)\n\t\tconst staticGuards = Object.fromEntries(guardChecks)\n\t\tconst dynamicGuards = Object.keys(guardResolvers)\n\t\truntime.routePolicy?.({\n\t\t\tname,\n\t\t\tprocedure: key,\n\t\t\tguards: staticGuards,\n\t\t\tdynamicGuards,\n\t\t\tmeta,\n\t\t\tstream,\n\t\t\ttags: (route.tags as string[] | undefined) ?? []\n\t\t})\n\t\tconst built = buildProcedure({\n\t\t\tname,\n\t\t\tprocedure: key,\n\t\t\troute,\n\t\t\tmeta,\n\t\t\tguards: staticGuards,\n\t\t\tdynamicGuards,\n\t\t\tinput,\n\t\t\toutput,\n\t\t\tstream,\n\t\t\t...(errors ? { errors } : {}),\n\t\t\thandler: async (params) => {\n\t\t\t\tconst { context, signal } = params\n\t\t\t\tconst state = context[invocation] as Invocation\n\t\t\t\tconst {\n\t\t\t\t\t[invocation]: _invocation,\n\t\t\t\t\t[EXTRAS_OVERRIDE]: overrides,\n\t\t\t\t\t...observedContext\n\t\t\t\t} = context\n\t\t\t\tstate.context = observedContext\n\t\t\t\tstate.input = params.input\n\t\t\t\tstate.handlerStarted = true\n\t\t\t\ttry {\n\t\t\t\t\tconst attributes = runtime.spanAttributes?.({\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\tmeta\n\t\t\t\t\t})\n\t\t\t\t\tfor (const [attribute, value] of Object.entries(attributes ?? {}))\n\t\t\t\t\t\tif (value !== undefined) state.span?.setAttribute(attribute, value)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tstate.logger.warn('spanAttributes hook failed', {\n\t\t\t\t\t\terror: errorMessageOf(error)\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconst resolvedChecks = [...guardChecks]\n\t\t\t\tfor (const [guard, resolver] of Object.entries(guardResolvers)) {\n\t\t\t\t\tconst value = await resolver({ input: params.input, context, signal })\n\t\t\t\t\tif (value !== undefined) resolvedChecks.push([guard, value])\n\t\t\t\t}\n\t\t\t\tfor (const [guard, value] of resolvedChecks) {\n\t\t\t\t\tconst result = await guards[guard]?.(value, {\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t\tinput: params.input,\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\tmeta,\n\t\t\t\t\t\tsignal\n\t\t\t\t\t})\n\t\t\t\t\tif (result === false)\n\t\t\t\t\t\tthrow new ORPCError('FORBIDDEN', {\n\t\t\t\t\t\t\tmessage: `Guard \"${guard}\" denied this call`\n\t\t\t\t\t\t})\n\t\t\t\t\tif (result !== undefined && result !== true)\n\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t`orpc-fn: guard \"${guard}\" must return void or boolean`\n\t\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst extraParams = {\n\t\t\t\t\tcontext,\n\t\t\t\t\tspan: state.span,\n\t\t\t\t\tsignal,\n\t\t\t\t\tname,\n\t\t\t\t\tprocedure: key\n\t\t\t\t}\n\t\t\t\tconst shared = await runtime.extras?.(extraParams)\n\t\t\t\tconst scoped = await runtime.extrasByProcedure?.[key]?.(extraParams)\n\t\t\t\tconst extras = {\n\t\t\t\t\t...(shared as object | undefined),\n\t\t\t\t\t...(scoped as object | undefined),\n\t\t\t\t\t...(overrides as object | undefined)\n\t\t\t\t}\n\t\t\t\tif (extras && typeof extras === 'object') {\n\t\t\t\t\tfor (const reserved of [\n\t\t\t\t\t\t'input',\n\t\t\t\t\t\t'context',\n\t\t\t\t\t\t'call',\n\t\t\t\t\t\t'signal',\n\t\t\t\t\t\t'span',\n\t\t\t\t\t\t'logger',\n\t\t\t\t\t\t'errors',\n\t\t\t\t\t\t'lastEventId'\n\t\t\t\t\t]) {\n\t\t\t\t\t\tif (Object.hasOwn(extras, reserved))\n\t\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t\t`orpc-fn: extra \"${reserved}\" shadows a handler parameter`\n\t\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn handler({\n\t\t\t\t\t...(extras as object | undefined),\n\t\t\t\t\tinput: params.input,\n\t\t\t\t\tcontext,\n\t\t\t\t\tcall: createBoundCall(context, signal, tracing),\n\t\t\t\t\tsignal,\n\t\t\t\t\tspan: state.span,\n\t\t\t\t\tlogger: state.logger,\n\t\t\t\t\terrors: params.errors,\n\t\t\t\t\tlastEventId: params.lastEventId,\n\t\t\t\t\t[RAW_INPUT]: state.rawInput\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\t\t// Prepending middleware shifts oRPC's validation indexes, covering auth,\n\t\t// input validation, output validation, and the final stream lifetime.\n\t\treturn os\n\t\t\t.use(async ({ context, next }, rawInput) => {\n\t\t\t\tconst started = performance.now()\n\t\t\t\tconst state: Invocation = {\n\t\t\t\t\tspan: undefined,\n\t\t\t\t\tlogger: defaultLogger,\n\t\t\t\t\tinput: undefined,\n\t\t\t\t\tcontext,\n\t\t\t\t\thandlerStarted: false,\n\t\t\t\t\trawInput\n\t\t\t\t}\n\t\t\t\tconst result = await tracing.inSpan(\n\t\t\t\t\tname,\n\t\t\t\t\t'SERVER',\n\t\t\t\t\tasync (span) => {\n\t\t\t\t\t\tstate.span = span\n\t\t\t\t\t\tstate.logger = createLogger('fn', span)\n\t\t\t\t\t\tspan?.setAttribute('fn.operation', name)\n\t\t\t\t\t\tspan?.setAttribute('fn.procedure', key)\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await next({ context: { [invocation]: state } })\n\t\t\t\t\t\t\tif (isAsyncIteratorObject(result.output) !== stream) {\n\t\t\t\t\t\t\t\tif (isAsyncIteratorObject(result.output)) {\n\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\tawait result.output.return?.()\n\t\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\t\t/* Release the unexpected stream. */\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t\t\t\t`orpc-fn: ${name} returned a result inconsistent with stream; declare stream: true for iterators`\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn result.output\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tconst timing = (context as Record<symbol, unknown>)[\n\t\t\t\t\t\t\t\tREQUEST_TIMING\n\t\t\t\t\t\t\t] as InvocationTiming | undefined\n\t\t\t\t\t\t\tif (timing && !(context as Record<symbol, unknown>)[invocation]) {\n\t\t\t\t\t\t\t\ttiming.procedureMs += performance.now() - started\n\t\t\t\t\t\t\t\ttiming.calls++\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t(span, ...error) => {\n\t\t\t\t\t\tconst durationMs =\n\t\t\t\t\t\t\tMath.round((performance.now() - started) * 100) / 100\n\t\t\t\t\t\tspan?.setAttribute('fn.duration_ms', durationMs)\n\t\t\t\t\t\tlogCompleted(state.logger, {\n\t\t\t\t\t\t\tname,\n\t\t\t\t\t\t\tprocedure: key,\n\t\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\t\tsuccess: error.length === 0,\n\t\t\t\t\t\t\terror: error[0],\n\t\t\t\t\t\t\tinput: state.input,\n\t\t\t\t\t\t\tcontext: state.context,\n\t\t\t\t\t\t\tspan,\n\t\t\t\t\t\t\tmeta,\n\t\t\t\t\t\t\thandlerStarted: state.handlerStarted\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t)\n\t\t\t\treturn { output: result, context }\n\t\t\t})\n\t\t\t.router({ route: built }).route\n\t}\n\n\tconst logCompleted = (logger: FnLogger, event: FnCompletedEvent) => {\n\t\tconst { error, success } = event\n\t\tlet extra: LogAttributes | undefined\n\t\ttry {\n\t\t\textra = runtime.onCompleted?.(event)\n\t\t} catch (hookError) {\n\t\t\tlogger.warn('onCompleted hook failed', {\n\t\t\t\terror: errorMessageOf(hookError)\n\t\t\t})\n\t\t}\n\t\tconst expected = !success && isExpectedError(error)\n\t\tconst level = success ? 'info' : expected ? 'warn' : 'error'\n\t\tlogger[level]('fn.completed', {\n\t\t\t...extra,\n\t\t\tevent: 'fn.completed',\n\t\t\toperation: event.name,\n\t\t\tprocedure: event.procedure,\n\t\t\tduration_ms: event.durationMs,\n\t\t\tstatus: success ? 'success' : 'failed',\n\t\t\t...(success\n\t\t\t\t? {}\n\t\t\t\t: errorAttributes(\n\t\t\t\t\t\terror,\n\t\t\t\t\t\t!(expected && runtime.errorMessages === 'unexpected')\n\t\t\t\t\t))\n\t\t})\n\t}\n\n\tconst live = createLiveRuntime({\n\t\tfn,\n\t\ttracing,\n\t\tcreateLogger,\n\t\tpubsub: runtime.pubsub\n\t})\n\n\tconst readMeta = (procedure: AnyProcedure): FnMeta => readFnMeta(procedure)\n\n\t// The runtimes are untyped; `Fn`, `FnLive`, `CreatePubSub` and\n\t// `CreatePublisher` are their typed surface.\n\treturn {\n\t\tfn,\n\t\tfnLive: createFnLive({\n\t\t\tfn,\n\t\t\tcreateChannel: live.createChannel,\n\t\t\tisGuard\n\t\t}),\n\t\tfnLivePatch,\n\t\tcreatePubSub: live.createPubSub,\n\t\tcreatePublisher: live.createPublisher,\n\t\tcreateRouter,\n\t\treadMeta,\n\t\tcreateCall: <TContext>(context: TContext, signal?: AbortSignal) =>\n\t\t\tcreateBoundCall(context, signal, tracing),\n\t\tcreateTestCall: <TContext>(\n\t\t\tcontext: TContext,\n\t\t\t{ extras, signal }: { extras?: object; signal?: AbortSignal } = {}\n\t\t) =>\n\t\t\tcreateBoundCall(\n\t\t\t\textras ? { ...context, [EXTRAS_OVERRIDE]: extras } : context,\n\t\t\t\tsignal,\n\t\t\t\ttracing\n\t\t\t),\n\t\tshutdown: live.shutdown,\n\t\tdrainPubSubSubscribers: live.drainPubSubSubscribers,\n\t\tactivePubSubSubscriberCount: live.activePubSubSubscriberCount\n\t} as unknown as FnFactory<\n\t\tDefinition<\n\t\t\tTProcedures,\n\t\t\tTDefault,\n\t\t\tTExtras,\n\t\t\tTGuards,\n\t\t\tTMeta,\n\t\t\tTTag,\n\t\t\tTOtel,\n\t\t\tTLogger,\n\t\t\tTScoped\n\t\t>\n\t>\n}\n"]}
|
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
* drives, not server defects, so they must not be logged at `error` severity
|
|
6
6
|
* or reported to error tracking. Only 5xx and unknown failures are defects.
|
|
7
7
|
*
|
|
8
|
+
* It is also true for any error a `createFn({ isExpectedError })` classified
|
|
9
|
+
* as expected when a route threw it, so `mountOrpc` and the tool adapters
|
|
10
|
+
* follow the factory's rule without being told it again.
|
|
11
|
+
*
|
|
8
12
|
* This is the default `isExpectedError`; extend it with `createFn({ isExpectedError })`.
|
|
9
13
|
*/
|
|
10
14
|
export declare function isExpectedClientError(error: unknown): boolean;
|
|
15
|
+
/** @internal Remembers that the factory's `isExpectedError` accepted `error`. */
|
|
16
|
+
export declare function markExpected(error: unknown): void;
|
|
11
17
|
//# sourceMappingURL=expected-client-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expected-client-error.d.ts","sourceRoot":"","sources":["../src/expected-client-error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"expected-client-error.d.ts","sourceRoot":"","sources":["../src/expected-client-error.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAS7D;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAEjD"}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { ORPCError } from '@orpc/server';
|
|
2
|
+
// ponytail: one set per package copy; apps with two installed copies of
|
|
3
|
+
// orpc-fn must still pass isExpectedError to mountOrpc and the tool adapters.
|
|
4
|
+
const classifiedExpected = new WeakSet();
|
|
2
5
|
/**
|
|
3
6
|
* An expected client failure: an `ORPCError` with a 4xx status (validation,
|
|
4
7
|
* auth, not-found) or an `AbortError` (the client closed the connection, e.g.
|
|
@@ -6,13 +9,26 @@ import { ORPCError } from '@orpc/server';
|
|
|
6
9
|
* drives, not server defects, so they must not be logged at `error` severity
|
|
7
10
|
* or reported to error tracking. Only 5xx and unknown failures are defects.
|
|
8
11
|
*
|
|
12
|
+
* It is also true for any error a `createFn({ isExpectedError })` classified
|
|
13
|
+
* as expected when a route threw it, so `mountOrpc` and the tool adapters
|
|
14
|
+
* follow the factory's rule without being told it again.
|
|
15
|
+
*
|
|
9
16
|
* This is the default `isExpectedError`; extend it with `createFn({ isExpectedError })`.
|
|
10
17
|
*/
|
|
11
18
|
export function isExpectedClientError(error) {
|
|
12
19
|
if (error instanceof ORPCError && error.status < 500)
|
|
13
20
|
return true;
|
|
21
|
+
if (typeof error === 'object' &&
|
|
22
|
+
error !== null &&
|
|
23
|
+
classifiedExpected.has(error))
|
|
24
|
+
return true;
|
|
14
25
|
return isAbortError(error);
|
|
15
26
|
}
|
|
27
|
+
/** @internal Remembers that the factory's `isExpectedError` accepted `error`. */
|
|
28
|
+
export function markExpected(error) {
|
|
29
|
+
if (typeof error === 'object' && error !== null)
|
|
30
|
+
classifiedExpected.add(error);
|
|
31
|
+
}
|
|
16
32
|
function isAbortError(error) {
|
|
17
33
|
return (typeof error === 'object' &&
|
|
18
34
|
error !== null &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expected-client-error.js","sourceRoot":"","sources":["../src/expected-client-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC
|
|
1
|
+
{"version":3,"file":"expected-client-error.js","sourceRoot":"","sources":["../src/expected-client-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,wEAAwE;AACxE,8EAA8E;AAC9E,MAAM,kBAAkB,GAAG,IAAI,OAAO,EAAU,CAAA;AAEhD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAc;IACnD,IAAI,KAAK,YAAY,SAAS,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG;QAAE,OAAO,IAAI,CAAA;IACjE,IACC,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,kBAAkB,CAAC,GAAG,CAAC,KAAK,CAAC;QAE7B,OAAO,IAAI,CAAA;IACZ,OAAO,YAAY,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,YAAY,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,kBAAkB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;AAC/E,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IACnC,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,YAAY,CACnD,CAAA;AACF,CAAC","sourcesContent":["import { ORPCError } from '@orpc/server'\n\n// ponytail: one set per package copy; apps with two installed copies of\n// orpc-fn must still pass isExpectedError to mountOrpc and the tool adapters.\nconst classifiedExpected = new WeakSet<object>()\n\n/**\n * An expected client failure: an `ORPCError` with a 4xx status (validation,\n * auth, not-found) or an `AbortError` (the client closed the connection, e.g.\n * a browser leaving a live stream). These are normal outcomes the caller\n * drives, not server defects, so they must not be logged at `error` severity\n * or reported to error tracking. Only 5xx and unknown failures are defects.\n *\n * It is also true for any error a `createFn({ isExpectedError })` classified\n * as expected when a route threw it, so `mountOrpc` and the tool adapters\n * follow the factory's rule without being told it again.\n *\n * This is the default `isExpectedError`; extend it with `createFn({ isExpectedError })`.\n */\nexport function isExpectedClientError(error: unknown): boolean {\n\tif (error instanceof ORPCError && error.status < 500) return true\n\tif (\n\t\ttypeof error === 'object' &&\n\t\terror !== null &&\n\t\tclassifiedExpected.has(error)\n\t)\n\t\treturn true\n\treturn isAbortError(error)\n}\n\n/** @internal Remembers that the factory's `isExpectedError` accepted `error`. */\nexport function markExpected(error: unknown): void {\n\tif (typeof error === 'object' && error !== null) classifiedExpected.add(error)\n}\n\nfunction isAbortError(error: unknown): boolean {\n\treturn (\n\t\ttypeof error === 'object' &&\n\t\terror !== null &&\n\t\t(error as { name?: unknown }).name === 'AbortError'\n\t)\n}\n"]}
|
package/dist/hono.d.ts
CHANGED
|
@@ -2,9 +2,15 @@ import type { OpenAPIGeneratorGenerateOptions } from '@orpc/openapi';
|
|
|
2
2
|
import { type AnyRouter, type InferRouterInitialContext } from '@orpc/server';
|
|
3
3
|
import type { StandardHandlerPlugin } from '@orpc/server/standard';
|
|
4
4
|
import type { Context as HonoContext, Env, Hono } from 'hono';
|
|
5
|
-
import type
|
|
5
|
+
import { hasTag, type ProcedureFilter } from './meta.js';
|
|
6
|
+
export { hasTag, type ProcedureFilter };
|
|
6
7
|
/** Per-request timings in ms; `fn()` adds `handler_ms`. Sent as Server-Timing. */
|
|
7
8
|
export type RequestTiming = Record<string, number>;
|
|
9
|
+
/**
|
|
10
|
+
* Run `work` and add its duration to `timing[key]` (rounded to 0.01 ms),
|
|
11
|
+
* whether it resolves or throws: `await timed(context.timing, 'auth_ms', () => getSession(headers))`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function timed<T>(timing: RequestTiming, key: string, work: () => T | Promise<T>): Promise<T>;
|
|
8
14
|
/** What every request starts with; the default context when `context` is omitted. */
|
|
9
15
|
export type BaseContext = {
|
|
10
16
|
headers: Headers;
|
|
@@ -23,28 +29,39 @@ type ContextOption<E extends Env, R extends AnyRouter> = BaseContext extends Inf
|
|
|
23
29
|
};
|
|
24
30
|
type Path = `/${string}`;
|
|
25
31
|
type Plugin<T extends object> = StandardHandlerPlugin<T>;
|
|
32
|
+
export type OpenApiMountOptions<R extends AnyRouter> = {
|
|
33
|
+
prefix: Path;
|
|
34
|
+
info?: {
|
|
35
|
+
title: string;
|
|
36
|
+
version: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
};
|
|
39
|
+
/** Docs page, relative to `prefix` (default '/'); the spec is at `/spec.json`. */
|
|
40
|
+
docsPath?: Path;
|
|
41
|
+
/** false: serve the REST routes without the docs page and `spec.json`. */
|
|
42
|
+
docs?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The procedures to publish (also filters the spec), e.g.
|
|
45
|
+
* `hasTag('external')`. Required, so publishing is a choice;
|
|
46
|
+
* `() => true` publishes every route.
|
|
47
|
+
*/
|
|
48
|
+
filter: ProcedureFilter;
|
|
49
|
+
/** Turn JSON strings back into `Date`s etc. for `z.date()` inputs. */
|
|
50
|
+
smartCoercion?: boolean;
|
|
51
|
+
/** Extra spec fields, e.g. `components.securitySchemes` and `security`. */
|
|
52
|
+
spec?: Omit<OpenAPIGeneratorGenerateOptions, 'filter'>;
|
|
53
|
+
plugins?: Plugin<InferRouterInitialContext<R>>[];
|
|
54
|
+
};
|
|
26
55
|
export type MountOrpcOptions<E extends Env, R extends AnyRouter> = ContextOption<E, R> & {
|
|
27
56
|
router: R;
|
|
28
57
|
/** Mount the RPC handler (oRPC's `RPCLink`) here. Omit to skip it. */
|
|
29
58
|
rpcPrefix?: Path;
|
|
30
|
-
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
description?: string;
|
|
37
|
-
};
|
|
38
|
-
/** Docs page, relative to `prefix` (default '/'); the spec is at `/spec.json`. */
|
|
39
|
-
docsPath?: Path;
|
|
40
|
-
/** Only expose matching procedures (also filters the spec). */
|
|
41
|
-
filter?: ProcedureFilter;
|
|
42
|
-
/** Turn JSON strings back into `Date`s etc. for `z.date()` inputs. */
|
|
43
|
-
smartCoercion?: boolean;
|
|
44
|
-
/** Extra spec fields, e.g. `components.securitySchemes` and `security`. */
|
|
45
|
-
spec?: Omit<OpenAPIGeneratorGenerateOptions, 'filter'>;
|
|
46
|
-
plugins?: Plugin<InferRouterInitialContext<R>>[];
|
|
47
|
-
};
|
|
59
|
+
/**
|
|
60
|
+
* Mount the OpenAPI (REST) handler and its Scalar docs here. Needs the
|
|
61
|
+
* optional peers `@orpc/openapi`, `@orpc/json-schema` and `@orpc/zod`,
|
|
62
|
+
* loaded on the first request to `prefix`.
|
|
63
|
+
*/
|
|
64
|
+
openapi?: OpenApiMountOptions<R>;
|
|
48
65
|
/** Collapse parallel RPC calls into one HTTP request. Default: true (25 calls). */
|
|
49
66
|
batch?: boolean | {
|
|
50
67
|
maxSize: number;
|
|
@@ -59,7 +76,10 @@ export type MountOrpcOptions<E extends Env, R extends AnyRouter> = ContextOption
|
|
|
59
76
|
compress?: boolean;
|
|
60
77
|
/** SSE keep-alive comments every N ms (default 15000), or false. */
|
|
61
78
|
sseKeepAlive?: boolean | number;
|
|
62
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Extra RPC plugins. CORS belongs to Hono's `cors()` on the app, once; a
|
|
81
|
+
* cross-origin client with batching needs `x-orpc-batch` in its `allowHeaders`.
|
|
82
|
+
*/
|
|
63
83
|
plugins?: Plugin<InferRouterInitialContext<R>>[];
|
|
64
84
|
/** Rewrite request headers before they reach the context (see `normalizeExpoOrigin`). */
|
|
65
85
|
normalizeHeaders?: (headers: Headers) => Headers;
|
|
@@ -76,7 +96,10 @@ export type MountOrpcOptions<E extends Env, R extends AnyRouter> = ContextOption
|
|
|
76
96
|
url: string;
|
|
77
97
|
method: string;
|
|
78
98
|
}) => void;
|
|
79
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Default: `isExpectedClientError`, which also accepts any error the
|
|
101
|
+
* route's `createFn({ isExpectedError })` classified as expected.
|
|
102
|
+
*/
|
|
80
103
|
isExpectedError?: (error: unknown) => boolean;
|
|
81
104
|
};
|
|
82
105
|
/**
|
|
@@ -102,5 +125,4 @@ export declare function formatServerTiming(totalMs: number, timing: Record<strin
|
|
|
102
125
|
export declare function finishOrpcHeaders(response: Response, serverTiming: string | undefined): Headers;
|
|
103
126
|
/** Mount oRPC's RPC and OpenAPI handlers on a Hono app. */
|
|
104
127
|
export declare function mountOrpc<E extends Env, R extends AnyRouter>(app: Hono<E>, options: MountOrpcOptions<E, R>): void;
|
|
105
|
-
export {};
|
|
106
128
|
//# sourceMappingURL=hono.d.ts.map
|
package/dist/hono.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hono.d.ts","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hono.d.ts","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,eAAe,CAAA;AACpE,OAAO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,OAAO,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC7B,IAAI,CAyHN"}
|
package/dist/hono.js
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import { Readable, pipeline } from 'node:stream';
|
|
2
2
|
import zlib from 'node:zlib';
|
|
3
|
-
import { SmartCoercionPlugin } from '@orpc/json-schema';
|
|
4
|
-
import { OpenAPIHandler } from '@orpc/openapi/fetch';
|
|
5
|
-
import { OpenAPIReferencePlugin } from '@orpc/openapi/plugins';
|
|
6
3
|
import { onError as orpcOnError } from '@orpc/server';
|
|
7
4
|
import { RPCHandler } from '@orpc/server/fetch';
|
|
8
5
|
import { BatchHandlerPlugin } from '@orpc/server/plugins';
|
|
9
|
-
import { ZodToJsonSchemaConverter } from '@orpc/zod/zod4';
|
|
10
6
|
import { REQUEST_TIMING } from './timing.js';
|
|
11
7
|
import { isExpectedClientError } from './expected-client-error.js';
|
|
8
|
+
import { hasTag } from './meta.js';
|
|
9
|
+
export { hasTag };
|
|
10
|
+
/**
|
|
11
|
+
* Run `work` and add its duration to `timing[key]` (rounded to 0.01 ms),
|
|
12
|
+
* whether it resolves or throws: `await timed(context.timing, 'auth_ms', () => getSession(headers))`.
|
|
13
|
+
*/
|
|
14
|
+
export async function timed(timing, key, work) {
|
|
15
|
+
const started = performance.now();
|
|
16
|
+
try {
|
|
17
|
+
return await work();
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
timing[key] =
|
|
21
|
+
(timing[key] ?? 0) + Math.round((performance.now() - started) * 100) / 100;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
12
24
|
const arrivals = new WeakMap();
|
|
13
25
|
/**
|
|
14
26
|
* Marks a request at the server's front door (`Bun.serve({ fetch })`), before
|
|
@@ -140,7 +152,7 @@ export function mountOrpc(app, options) {
|
|
|
140
152
|
[REQUEST_TIMING]: invocationTiming
|
|
141
153
|
}
|
|
142
154
|
});
|
|
143
|
-
if (!matched)
|
|
155
|
+
if (!matched || !response)
|
|
144
156
|
return next();
|
|
145
157
|
const timed = serverTiming
|
|
146
158
|
? formatServerTiming(performance.now() - started, {
|
|
@@ -180,14 +192,33 @@ export function mountOrpc(app, options) {
|
|
|
180
192
|
}
|
|
181
193
|
if (options.openapi) {
|
|
182
194
|
const { openapi } = options;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
195
|
+
if (typeof openapi.filter !== 'function')
|
|
196
|
+
throw new TypeError('orpc-fn: mountOrpc openapi needs a filter; pass () => true to publish every route');
|
|
197
|
+
let loading;
|
|
198
|
+
mount(openapi.prefix, {
|
|
199
|
+
handle: async (request, handleOptions) => {
|
|
200
|
+
loading ??= createOpenApiHandler(router, openapi, keepAlive, options.onError ? report : undefined);
|
|
201
|
+
return (await loading).handle(request, handleOptions);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Loads the OpenAPI peers only for apps that mount it. */
|
|
207
|
+
async function createOpenApiHandler(router, openapi, keepAlive, report) {
|
|
208
|
+
const [{ SmartCoercionPlugin }, { OpenAPIHandler }, { OpenAPIReferencePlugin }, { ZodToJsonSchemaConverter }] = await Promise.all([
|
|
209
|
+
import('@orpc/json-schema'),
|
|
210
|
+
import('@orpc/openapi/fetch'),
|
|
211
|
+
import('@orpc/openapi/plugins'),
|
|
212
|
+
import('@orpc/zod/zod4')
|
|
213
|
+
]);
|
|
214
|
+
const converters = [new ZodToJsonSchemaConverter()];
|
|
215
|
+
const plugins = [
|
|
216
|
+
...(openapi.plugins ?? [])
|
|
217
|
+
];
|
|
218
|
+
if (openapi.smartCoercion) {
|
|
219
|
+
plugins.push(new SmartCoercionPlugin({ schemaConverters: converters }));
|
|
220
|
+
}
|
|
221
|
+
if (openapi.docs !== false) {
|
|
191
222
|
plugins.push(new OpenAPIReferencePlugin({
|
|
192
223
|
docsProvider: 'scalar',
|
|
193
224
|
...(openapi.docsPath ? { docsPath: openapi.docsPath } : {}),
|
|
@@ -195,16 +226,16 @@ export function mountOrpc(app, options) {
|
|
|
195
226
|
specGenerateOptions: {
|
|
196
227
|
info: openapi.info ?? { title: 'API', version: '0.0.0' },
|
|
197
228
|
servers: [{ url: openapi.prefix }],
|
|
198
|
-
|
|
229
|
+
filter: openapi.filter,
|
|
199
230
|
...openapi.spec
|
|
200
231
|
}
|
|
201
232
|
}));
|
|
202
|
-
mount(openapi.prefix, new OpenAPIHandler(router, {
|
|
203
|
-
...keepAlive,
|
|
204
|
-
...filter,
|
|
205
|
-
plugins,
|
|
206
|
-
interceptors: options.onError ? [orpcOnError(report)] : []
|
|
207
|
-
}));
|
|
208
233
|
}
|
|
234
|
+
return new OpenAPIHandler(router, {
|
|
235
|
+
...keepAlive,
|
|
236
|
+
filter: openapi.filter,
|
|
237
|
+
plugins,
|
|
238
|
+
interceptors: report ? [orpcOnError(report)] : []
|
|
239
|
+
});
|
|
209
240
|
}
|
|
210
241
|
//# sourceMappingURL=hono.js.map
|