@orpc/trpc 2.0.0-beta.48 → 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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
6
6
  </a>
7
7
  <a href="https://www.npmjs.com/package/@orpc/trpc">
8
- <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Ftrpc?logo=npm" />
8
+ <img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Ftrpc?logo=npm" />
9
9
  </a>
10
10
  <a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
11
11
  <img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
@@ -41,10 +41,11 @@ export const router = {
41
41
  Then call them from a client that knows every input and output, without generating anything:
42
42
 
43
43
  ```ts
44
- import type { RouterClient } from '@orpc/server'
45
- import type { router } from './router'
46
44
  import { createORPCClient } from '@orpc/client'
47
45
  import { RPCLink } from '@orpc/client/fetch'
46
+ import type { RouterClient } from '@orpc/server'
47
+
48
+ import type { router } from './router'
48
49
 
49
50
  const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
50
51
  const client: RouterClient<typeof router> = createORPCClient(link)
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
208
209
  <tr>
209
210
  <td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
210
211
  <td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&amp;v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
211
- <td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&amp;v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
212
212
  </tr>
213
213
  </table>
214
214
 
@@ -0,0 +1,3 @@
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 ADDED
@@ -0,0 +1,2 @@
1
+ export { toORPCRouter } from "./to-orpc-router.js";
2
+ export { toTRPCMeta } from "./to-trpc-meta.js";
@@ -0,0 +1,14 @@
1
+ import * as ORPC from "@orpc/server";
2
+ import { AsyncIteratorClass } from "@orpc/shared";
3
+ import { AnyProcedure, AnyRouter, inferRouterContext } from "@trpc/server";
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>> = { [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; };
6
+ /**
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
+ export declare function toORPCRouter<T extends AnyRouter>(router: T): ToORPCRouterResult<inferRouterContext<T>, T["_def"]["record"]>;
@@ -0,0 +1,110 @@
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
6
+ /**
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;
23
+ }
24
+ function recordToORPCRouterRecord(records) {
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;
32
+ }
33
+ function toORPCProcedure(procedure) {
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
+ });
82
+ }
83
+ /**
84
+ * Re-applies tRPC meta on top of the incoming meta, shallow merged like tRPC `.meta` calls.
85
+ */
86
+ function toTRPCMetaPlugin(trpcMeta) {
87
+ return {
88
+ name: "~trpc",
89
+ init: (meta) => ({
90
+ ...meta,
91
+ ...trpcMeta
92
+ })
93
+ };
94
+ }
95
+ function toORPCError(error) {
96
+ if (error instanceof TRPCError) return new ORPC.ORPCError(error.code, {
97
+ message: error.message,
98
+ cause: error
99
+ });
100
+ return error;
101
+ }
102
+ /**
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
+ */
106
+ function toStandardSchema(schema) {
107
+ if (!isNonPrimitive(schema) || !("~standard" in schema) || !isNonPrimitive(schema["~standard"])) return;
108
+ return schema;
109
+ }
110
+ export { toORPCRouter };
@@ -0,0 +1,31 @@
1
+ import { AnyMetaPlugin } from "@orpc/contract";
2
+ /**
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 declare function toTRPCMeta(...plugins: AnyMetaPlugin[]): Record<string, any>;
@@ -1,4 +1,5 @@
1
- import type { AnyMetaPlugin } from "@orpc/contract";
1
+ import { resolveMetaPlugins } from "@orpc/contract";
2
+ // src/to-trpc-meta.ts
2
3
  /**
3
4
  * Resolve oRPC meta plugins into a plain meta object usable as tRPC meta.
4
5
  *
@@ -28,4 +29,8 @@ import type { AnyMetaPlugin } from "@orpc/contract";
28
29
  *
29
30
  * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
30
31
  */
31
- export declare function toTRPCMeta(...plugins: AnyMetaPlugin[]): Record<string, any>;
32
+ function toTRPCMeta(...plugins) {
33
+ const [meta] = resolveMetaPlugins({}, void 0, plugins);
34
+ return meta;
35
+ }
36
+ export { toTRPCMeta };
package/package.json CHANGED
@@ -1,55 +1,53 @@
1
1
  {
2
2
  "name": "@orpc/trpc",
3
- "type": "module",
4
- "version": "2.0.0-beta.48",
3
+ "version": "2.0.0-beta.50",
5
4
  "description": "tRPC interop for oRPC: reuse existing tRPC routers with oRPC clients and OpenAPI",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
5
+ "keywords": [
6
+ "interop",
7
+ "migration",
8
+ "openapi",
9
+ "orpc",
10
+ "rpc",
11
+ "trpc",
12
+ "typesafe",
13
+ "typescript"
10
14
  ],
11
15
  "homepage": "https://orpc.dev",
16
+ "license": "MIT",
12
17
  "repository": {
13
18
  "type": "git",
14
19
  "url": "git+https://github.com/middleapi/orpc.git",
15
20
  "directory": "packages/trpc"
16
21
  },
17
- "keywords": [
18
- "orpc",
19
- "trpc",
20
- "interop",
21
- "migration",
22
- "rpc",
23
- "openapi",
24
- "typescript",
25
- "typesafe"
22
+ "funding": [
23
+ "https://github.com/sponsors/dinwwwh",
24
+ "https://opencollective.com/middleapi"
26
25
  ],
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "type": "module",
27
30
  "sideEffects": false,
28
31
  "exports": {
29
32
  "./package.json": "./package.json",
30
33
  ".": {
31
- "types": "./dist/index.d.mts",
32
- "import": "./dist/index.mjs",
33
- "default": "./dist/index.mjs"
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.js",
36
+ "default": "./dist/index.js"
34
37
  }
35
38
  },
36
- "files": [
37
- "dist"
38
- ],
39
- "peerDependencies": {
40
- "@trpc/server": ">=11.4.2"
41
- },
42
39
  "dependencies": {
43
- "@orpc/contract": "2.0.0-beta.48",
44
- "@orpc/server": "2.0.0-beta.48",
45
- "@orpc/shared": "2.0.0-beta.48"
40
+ "@orpc/contract": "2.0.0-beta.50",
41
+ "@orpc/server": "2.0.0-beta.50",
42
+ "@orpc/shared": "2.0.0-beta.50"
46
43
  },
47
44
  "devDependencies": {
48
- "@orpc/openapi": "2.0.0-beta.48",
49
- "@trpc/server": "^11.22.1",
45
+ "@orpc/openapi": "2.0.0-beta.50",
46
+ "@trpc/server": "^11.23.0",
50
47
  "zod": "^4.6.7"
51
48
  },
52
- "scripts": {
53
- "type:check": "tsc -b"
54
- }
49
+ "peerDependencies": {
50
+ "@trpc/server": ">=11.4.2"
51
+ },
52
+ "scripts": {}
55
53
  }
package/dist/index.d.mts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./to-orpc-router.mjs";
2
- export * from "./to-trpc-meta.mjs";
package/dist/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- export * from "./to-orpc-router.mjs";
2
- export * from "./to-trpc-meta.mjs";
@@ -1,17 +0,0 @@
1
- import type { AsyncIteratorClass } from "@orpc/shared";
2
- import type { AnyProcedure, AnyRouter, inferRouterContext } from "@trpc/server";
3
- import * as ORPC from "@orpc/server";
4
- export type ToORPCOutput<T> = T extends AsyncIterable<infer TData, infer TReturn, infer TNext> ? AsyncIteratorClass<TData, TReturn, TNext> : T;
5
- export type ToORPCRouterResult<
6
- TContext extends ORPC.Context,
7
- TRecord extends Record<string, any>
8
- > = { [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 };
9
- /**
10
- * Converts a tRPC router into an oRPC router that works with any oRPC feature.
11
- *
12
- * @remarks
13
- * **Note**: tRPC Error Formatting is not supported — errors thrown by tRPC are wrapped in `ORPCError`.
14
- *
15
- * @see {@link https://orpc.dev/docs/integrations/trpc | tRPC Integration}
16
- */
17
- export declare function toORPCRouter<T extends AnyRouter>(router: T): ToORPCRouterResult<inferRouterContext<T>, T["_def"]["record"]>;
@@ -1,99 +0,0 @@
1
- import * as ORPC from "@orpc/server";
2
- import { isTypescriptObject, set, wrapAsyncIterator } from "@orpc/shared";
3
- import { isTrackedEnvelope, TRPCError } from "@trpc/server";
4
- import { isAsyncIterable, isObject } from "@trpc/server/unstable-core-do-not-import";
5
-
6
- export function toORPCRouter(router) {
7
- const result = recordToORPCRouterRecord(router._def.record);
8
- for (const [key, item] of Object.entries(router._def.lazy)) {
9
- set(result, key.split("."), new ORPC.Lazy({
10
- meta: {},
11
- loader: async () => {
12
- const router = await item.ref();
13
- return { default: toORPCRouter(router) };
14
- }
15
- }));
16
- }
17
- return result;
18
- }
19
- function recordToORPCRouterRecord(records) {
20
- const orpcRouter = {};
21
- for (const key of Object.keys(records)) {
22
- const item = records[key];
23
- if (typeof item === "function") {
24
- orpcRouter[key] = toORPCProcedure(item);
25
- } else {
26
- orpcRouter[key] = recordToORPCRouterRecord(item);
27
- }
28
- }
29
- return orpcRouter;
30
- }
31
- function toORPCProcedure(procedure) {
32
- const inputSchema = toStandardSchema(procedure._def.inputs.at(-1));
33
- const outputSchema = toStandardSchema(procedure._def.output);
34
- return new ORPC.Procedure({
35
- errorMap: {},
36
- meta: procedure._def.meta ?? {},
37
- orderedMiddlewares: [],
38
- inputSchemas: inputSchema ? [inputSchema] : undefined,
39
- outputSchemas: outputSchema ? [outputSchema] : undefined,
40
-
41
- disableInputValidation: true,
42
- disableOutputValidation: true,
43
- handler: async ({ context, signal, path, input, lastEventId }) => {
44
- try {
45
- const trpcInput = lastEventId !== undefined && (input === undefined || isObject(input)) ? {
46
- ...input,
47
- lastEventId
48
- } : input;
49
- const output = await procedure({
50
- ctx: context,
51
- signal,
52
- path: path.join("."),
53
- type: procedure._def.type,
54
-
55
- getRawInput: async () => trpcInput,
56
-
57
- batchIndex: 0
58
- });
59
- if (isAsyncIterable(output)) {
60
- 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
- }
77
- return output;
78
- } catch (cause) {
79
- throw toORPCError(cause);
80
- }
81
- }
82
- });
83
- }
84
- function toORPCError(error) {
85
- if (error instanceof TRPCError) {
86
- return new ORPC.ORPCError(error.code, {
87
- message: error.message,
88
- cause: error
89
- });
90
- }
91
- return error;
92
- }
93
-
94
- function toStandardSchema(schema) {
95
- if (!isTypescriptObject(schema) || !("~standard" in schema) || !isTypescriptObject(schema["~standard"])) {
96
- return undefined;
97
- }
98
- return schema;
99
- }
@@ -1,6 +0,0 @@
1
- import { resolveMetaPlugins } from "@orpc/contract";
2
-
3
- export function toTRPCMeta(...plugins) {
4
- const [meta] = resolveMetaPlugins({}, undefined, plugins);
5
- return meta;
6
- }