@orpc/trpc 2.0.0-beta.49 → 2.0.0-beta.50

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/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from './to-orpc-router.ts';
2
- export * from './to-trpc-meta.ts';
1
+ import { ToORPCOutput, ToORPCRouterResult, toORPCRouter } from "./to-orpc-router.js";
2
+ import { toTRPCMeta } from "./to-trpc-meta.js";
3
+ export { ToORPCOutput, ToORPCRouterResult, toORPCRouter, toTRPCMeta };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from './to-orpc-router.js';
2
- export * from './to-trpc-meta.js';
1
+ export { toORPCRouter } from "./to-orpc-router.js";
2
+ export { toTRPCMeta } from "./to-trpc-meta.js";
@@ -1,10 +1,8 @@
1
- import * as ORPC from '@orpc/server';
2
- import type { AsyncIteratorClass } from '@orpc/shared';
3
- import type { AnyProcedure, AnyRouter, inferRouterContext } from '@trpc/server';
1
+ import * as ORPC from "@orpc/server";
2
+ import { AsyncIteratorClass } from "@orpc/shared";
3
+ import { AnyProcedure, AnyRouter, inferRouterContext } from "@trpc/server";
4
4
  export type ToORPCOutput<T> = T extends AsyncIterable<infer TData, infer TReturn, infer TNext> ? AsyncIteratorClass<TData, TReturn, TNext> : T;
5
- export type ToORPCRouterResult<TContext extends ORPC.Context, TRecord extends Record<string, any>> = {
6
- [K in keyof TRecord]: TRecord[K] extends AnyProcedure ? ORPC.Procedure<TContext, object, ORPC.Schema<TRecord[K]['_def']['$types']['input'], unknown>, ORPC.Schema<unknown, ToORPCOutput<TRecord[K]['_def']['$types']['output']>>, object> : TRecord[K] extends Record<string, any> ? ToORPCRouterResult<TContext, TRecord[K]> : never;
7
- };
5
+ export type ToORPCRouterResult<TContext extends ORPC.Context, TRecord extends Record<string, any>> = { [K in keyof TRecord]: TRecord[K] extends AnyProcedure ? ORPC.Procedure<TContext, object, ORPC.Schema<TRecord[K]["_def"]["$types"]["input"], unknown>, ORPC.Schema<unknown, ToORPCOutput<TRecord[K]["_def"]["$types"]["output"]>>, object> : TRecord[K] extends Record<string, any> ? ToORPCRouterResult<TContext, TRecord[K]> : never; };
8
6
  /**
9
7
  * Converts a tRPC router into an oRPC router that works with any oRPC feature.
10
8
  *
@@ -13,4 +11,4 @@ export type ToORPCRouterResult<TContext extends ORPC.Context, TRecord extends Re
13
11
  *
14
12
  * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
15
13
  */
16
- export declare function toORPCRouter<T extends AnyRouter>(router: T): ToORPCRouterResult<inferRouterContext<T>, T['_def']['record']>;
14
+ export declare function toORPCRouter<T extends AnyRouter>(router: T): ToORPCRouterResult<inferRouterContext<T>, T["_def"]["record"]>;
@@ -1,125 +1,110 @@
1
- import * as ORPC from '@orpc/server';
2
- import { isNonPrimitive, set, wrapAsyncIterator } from '@orpc/shared';
3
- import { isTrackedEnvelope, TRPCError } from '@trpc/server';
4
- import { isAsyncIterable, isObject } from '@trpc/server/unstable-core-do-not-import';
1
+ import * as ORPC from "@orpc/server";
2
+ import { isNonPrimitive, set, wrapAsyncIterator } from "@orpc/shared";
3
+ import { TRPCError, isTrackedEnvelope } from "@trpc/server";
4
+ import { isAsyncIterable, isObject } from "@trpc/server/unstable-core-do-not-import";
5
+ // src/to-orpc-router.ts
5
6
  /**
6
- * Converts a tRPC router into an oRPC router that works with any oRPC feature.
7
- *
8
- * @remarks
9
- * **Note**: tRPC Error Formatting is not supported — errors thrown by tRPC are wrapped in `ORPCError`.
10
- *
11
- * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
12
- */
13
- export function toORPCRouter(router) {
14
- const result = recordToORPCRouterRecord(router._def.record);
15
- for (const [key, item] of Object.entries(router._def.lazy)) {
16
- set(result, key.split('.'), new ORPC.Lazy({
17
- meta: {},
18
- loader: async () => {
19
- const router = await item.ref();
20
- return { default: toORPCRouter(router) };
21
- },
22
- }));
23
- }
24
- return result;
7
+ * Converts a tRPC router into an oRPC router that works with any oRPC feature.
8
+ *
9
+ * @remarks
10
+ * **Note**: tRPC Error Formatting is not supported — errors thrown by tRPC are wrapped in `ORPCError`.
11
+ *
12
+ * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
13
+ */
14
+ function toORPCRouter(router) {
15
+ const result = recordToORPCRouterRecord(router._def.record);
16
+ for (const [key, item] of Object.entries(router._def.lazy)) set(result, key.split("."), new ORPC.Lazy({
17
+ meta: {},
18
+ loader: async () => {
19
+ return { default: toORPCRouter(await item.ref()) };
20
+ }
21
+ }));
22
+ return result;
25
23
  }
26
24
  function recordToORPCRouterRecord(records) {
27
- const orpcRouter = {};
28
- for (const key of Object.keys(records)) {
29
- const item = records[key];
30
- if (typeof item === 'function') {
31
- orpcRouter[key] = toORPCProcedure(item);
32
- }
33
- else {
34
- orpcRouter[key] = recordToORPCRouterRecord(item);
35
- }
36
- }
37
- return orpcRouter;
25
+ const orpcRouter = {};
26
+ for (const key of Object.keys(records)) {
27
+ const item = records[key];
28
+ if (typeof item === "function") orpcRouter[key] = toORPCProcedure(item);
29
+ else orpcRouter[key] = recordToORPCRouterRecord(item);
30
+ }
31
+ return orpcRouter;
38
32
  }
39
33
  function toORPCProcedure(procedure) {
40
- const inputSchema = toStandardSchema(procedure._def.inputs.at(-1));
41
- const outputSchema = toStandardSchema(procedure._def.output);
42
- const meta = (procedure._def.meta ?? {});
43
- return new ORPC.Procedure({
44
- errorMap: {},
45
- meta,
46
- // `.router` (and similar) rebuilds meta from its own meta + the procedure's meta plugins,
47
- // so the tRPC meta must be a plugin too, otherwise it would be dropped.
48
- metaPlugins: [toTRPCMetaPlugin(meta)],
49
- orderedMiddlewares: [],
50
- inputSchemas: inputSchema ? [inputSchema] : undefined,
51
- outputSchemas: outputSchema ? [outputSchema] : undefined,
52
- // tRPC procedure calling already validates the input/output
53
- disableInputValidation: true,
54
- disableOutputValidation: true,
55
- handler: async ({ context, signal, path, input, lastEventId }) => {
56
- try {
57
- const trpcInput = lastEventId !== undefined && (input === undefined || isObject(input))
58
- ? { ...input, lastEventId }
59
- : input;
60
- const output = await procedure({
61
- ctx: context,
62
- signal,
63
- path: path.join('.'),
64
- type: procedure._def.type,
65
- // Only the raw input: tRPC merges `input` into the parsed input, so passing it would keep keys the schema strips
66
- getRawInput: async () => trpcInput,
67
- // TODO: this should infer from context when using oRPC Batch Plugin
68
- batchIndex: 0,
69
- });
70
- if (isAsyncIterable(output)) {
71
- return wrapAsyncIterator(output[Symbol.asyncIterator](), {
72
- mapResult: (result) => {
73
- if (isTrackedEnvelope(result.value)) {
74
- const [id, data] = result.value;
75
- return {
76
- done: result.done,
77
- value: ORPC.withEventMeta({
78
- id,
79
- data,
80
- }, {
81
- id,
82
- }),
83
- };
84
- }
85
- return result;
86
- },
87
- mapError: toORPCError,
88
- });
89
- }
90
- return output;
91
- }
92
- catch (cause) {
93
- throw toORPCError(cause);
94
- }
95
- },
96
- });
34
+ const inputSchema = toStandardSchema(procedure._def.inputs.at(-1));
35
+ const outputSchema = toStandardSchema(procedure._def.output);
36
+ const meta = procedure._def.meta ?? {};
37
+ return new ORPC.Procedure({
38
+ errorMap: {},
39
+ meta,
40
+ metaPlugins: [toTRPCMetaPlugin(meta)],
41
+ orderedMiddlewares: [],
42
+ inputSchemas: inputSchema ? [inputSchema] : void 0,
43
+ outputSchemas: outputSchema ? [outputSchema] : void 0,
44
+ disableInputValidation: true,
45
+ disableOutputValidation: true,
46
+ handler: async ({ context, signal, path, input, lastEventId }) => {
47
+ try {
48
+ const trpcInput = lastEventId !== void 0 && (input === void 0 || isObject(input)) ? {
49
+ ...input,
50
+ lastEventId
51
+ } : input;
52
+ const output = await procedure({
53
+ ctx: context,
54
+ signal,
55
+ path: path.join("."),
56
+ type: procedure._def.type,
57
+ getRawInput: async () => trpcInput,
58
+ batchIndex: 0
59
+ });
60
+ if (isAsyncIterable(output)) return wrapAsyncIterator(output[Symbol.asyncIterator](), {
61
+ mapResult: (result) => {
62
+ if (isTrackedEnvelope(result.value)) {
63
+ const [id, data] = result.value;
64
+ return {
65
+ done: result.done,
66
+ value: ORPC.withEventMeta({
67
+ id,
68
+ data
69
+ }, { id })
70
+ };
71
+ }
72
+ return result;
73
+ },
74
+ mapError: toORPCError
75
+ });
76
+ return output;
77
+ } catch (cause) {
78
+ throw toORPCError(cause);
79
+ }
80
+ }
81
+ });
97
82
  }
98
83
  /**
99
- * Re-applies tRPC meta on top of the incoming meta, shallow merged like tRPC `.meta` calls.
100
- */
84
+ * Re-applies tRPC meta on top of the incoming meta, shallow merged like tRPC `.meta` calls.
85
+ */
101
86
  function toTRPCMetaPlugin(trpcMeta) {
102
- return {
103
- name: '~trpc',
104
- init: (meta) => ({ ...meta, ...trpcMeta }),
105
- };
87
+ return {
88
+ name: "~trpc",
89
+ init: (meta) => ({
90
+ ...meta,
91
+ ...trpcMeta
92
+ })
93
+ };
106
94
  }
107
95
  function toORPCError(error) {
108
- if (error instanceof TRPCError) {
109
- return new ORPC.ORPCError(error.code, {
110
- message: error.message,
111
- cause: error,
112
- });
113
- }
114
- return error;
96
+ if (error instanceof TRPCError) return new ORPC.ORPCError(error.code, {
97
+ message: error.message,
98
+ cause: error
99
+ });
100
+ return error;
115
101
  }
116
102
  /**
117
- * Ensures the parser is a standard schema before exposing it to oRPC,
118
- * so schemas remain usable for type inference and OpenAPI generation.
119
- */
103
+ * Ensures the parser is a standard schema before exposing it to oRPC,
104
+ * so schemas remain usable for type inference and OpenAPI generation.
105
+ */
120
106
  function toStandardSchema(schema) {
121
- if (!isNonPrimitive(schema) || !('~standard' in schema) || !isNonPrimitive(schema['~standard'])) {
122
- return undefined;
123
- }
124
- return schema;
107
+ if (!isNonPrimitive(schema) || !("~standard" in schema) || !isNonPrimitive(schema["~standard"])) return;
108
+ return schema;
125
109
  }
110
+ export { toORPCRouter };
@@ -1,4 +1,4 @@
1
- import type { AnyMetaPlugin } from '@orpc/contract';
1
+ import { AnyMetaPlugin } from "@orpc/contract";
2
2
  /**
3
3
  * Resolve oRPC meta plugins into a plain meta object usable as tRPC meta.
4
4
  *
@@ -28,4 +28,4 @@ import type { AnyMetaPlugin } from '@orpc/contract';
28
28
  *
29
29
  * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
30
30
  */
31
- export declare function toTRPCMeta(...plugins: AnyMetaPlugin[]): Record<string, any>;
31
+ export declare function toTRPCMeta(...plugins: AnyMetaPlugin[]): Record<string, any>;
@@ -1,34 +1,36 @@
1
- import { resolveMetaPlugins } from '@orpc/contract';
1
+ import { resolveMetaPlugins } from "@orpc/contract";
2
+ // src/to-trpc-meta.ts
2
3
  /**
3
- * Resolve oRPC meta plugins into a plain meta object usable as tRPC meta.
4
- *
5
- * This simulates how oRPC builders apply meta plugins, so well-known plugins
6
- * like `openapi` from `@orpc/openapi` can be used with tRPC builders.
7
- *
8
- * @remarks
9
- * **Warning**: Chained tRPC `.meta` calls merge shallowly, so oRPC metadata merge logic
10
- * (e.g. accumulating openapi `tags`) only works within a single `toTRPCMeta` call.
11
- *
12
- * @example
13
- * ```ts
14
- * const example = t.procedure
15
- * .meta(toTRPCMeta(openapi({ path: '/hello' })))
16
- * .query(() => 'Hello, World!')
17
- *
18
- * const merged = t.procedure
19
- * .meta({
20
- * ...toTRPCMeta(
21
- * openapi({ path: '/hello' }),
22
- * openapi({ method: 'POST' }),
23
- * ),
24
- * other: 'value',
25
- * })
26
- * .mutation(() => 'Hello, World!')
27
- * ```
28
- *
29
- * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
30
- */
31
- export function toTRPCMeta(...plugins) {
32
- const [meta] = resolveMetaPlugins({}, undefined, plugins);
33
- return meta;
4
+ * Resolve oRPC meta plugins into a plain meta object usable as tRPC meta.
5
+ *
6
+ * This simulates how oRPC builders apply meta plugins, so well-known plugins
7
+ * like `openapi` from `@orpc/openapi` can be used with tRPC builders.
8
+ *
9
+ * @remarks
10
+ * **Warning**: Chained tRPC `.meta` calls merge shallowly, so oRPC metadata merge logic
11
+ * (e.g. accumulating openapi `tags`) only works within a single `toTRPCMeta` call.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const example = t.procedure
16
+ * .meta(toTRPCMeta(openapi({ path: '/hello' })))
17
+ * .query(() => 'Hello, World!')
18
+ *
19
+ * const merged = t.procedure
20
+ * .meta({
21
+ * ...toTRPCMeta(
22
+ * openapi({ path: '/hello' }),
23
+ * openapi({ method: 'POST' }),
24
+ * ),
25
+ * other: 'value',
26
+ * })
27
+ * .mutation(() => 'Hello, World!')
28
+ * ```
29
+ *
30
+ * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
31
+ */
32
+ function toTRPCMeta(...plugins) {
33
+ const [meta] = resolveMetaPlugins({}, void 0, plugins);
34
+ return meta;
34
35
  }
36
+ export { toTRPCMeta };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orpc/trpc",
3
- "version": "2.0.0-beta.49",
3
+ "version": "2.0.0-beta.50",
4
4
  "description": "tRPC interop for oRPC: reuse existing tRPC routers with oRPC clients and OpenAPI",
5
5
  "keywords": [
6
6
  "interop",
@@ -37,12 +37,12 @@
37
37
  }
38
38
  },
39
39
  "dependencies": {
40
- "@orpc/contract": "2.0.0-beta.49",
41
- "@orpc/server": "2.0.0-beta.49",
42
- "@orpc/shared": "2.0.0-beta.49"
40
+ "@orpc/contract": "2.0.0-beta.50",
41
+ "@orpc/server": "2.0.0-beta.50",
42
+ "@orpc/shared": "2.0.0-beta.50"
43
43
  },
44
44
  "devDependencies": {
45
- "@orpc/openapi": "2.0.0-beta.49",
45
+ "@orpc/openapi": "2.0.0-beta.50",
46
46
  "@trpc/server": "^11.23.0",
47
47
  "zod": "^4.6.7"
48
48
  },