@orpc/client 0.0.0-next.b825e0c → 0.0.0-next.b953b88

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.
Files changed (37) hide show
  1. package/README.md +98 -0
  2. package/dist/adapters/fetch/index.d.mts +46 -0
  3. package/dist/adapters/fetch/index.d.ts +46 -0
  4. package/dist/adapters/fetch/index.mjs +45 -0
  5. package/dist/adapters/message-port/index.d.mts +59 -0
  6. package/dist/adapters/message-port/index.d.ts +59 -0
  7. package/dist/adapters/message-port/index.mjs +71 -0
  8. package/dist/adapters/standard/index.d.mts +10 -0
  9. package/dist/adapters/standard/index.d.ts +10 -0
  10. package/dist/adapters/standard/index.mjs +4 -0
  11. package/dist/adapters/websocket/index.d.mts +29 -0
  12. package/dist/adapters/websocket/index.d.ts +29 -0
  13. package/dist/adapters/websocket/index.mjs +45 -0
  14. package/dist/index.d.mts +185 -0
  15. package/dist/index.d.ts +185 -0
  16. package/dist/index.mjs +82 -0
  17. package/dist/plugins/index.d.mts +202 -0
  18. package/dist/plugins/index.d.ts +202 -0
  19. package/dist/plugins/index.mjs +400 -0
  20. package/dist/shared/client.BG98rYdO.d.ts +45 -0
  21. package/dist/shared/client.BOYsZIRq.d.mts +29 -0
  22. package/dist/shared/client.BOYsZIRq.d.ts +29 -0
  23. package/dist/shared/client.Bwgm6dgk.d.mts +45 -0
  24. package/dist/shared/client.C176log5.d.ts +91 -0
  25. package/dist/shared/client.DKmRtVO2.mjs +390 -0
  26. package/dist/shared/client.Ycwr4Tuo.d.mts +91 -0
  27. package/dist/shared/client.txdq_i5V.mjs +180 -0
  28. package/package.json +32 -18
  29. package/dist/fetch.js +0 -103
  30. package/dist/index.js +0 -42
  31. package/dist/src/adapters/fetch/index.d.ts +0 -3
  32. package/dist/src/adapters/fetch/orpc-link.d.ts +0 -46
  33. package/dist/src/adapters/fetch/types.d.ts +0 -4
  34. package/dist/src/client.d.ts +0 -11
  35. package/dist/src/dynamic-link.d.ts +0 -13
  36. package/dist/src/index.d.ts +0 -6
  37. package/dist/src/types.d.ts +0 -5
@@ -0,0 +1,180 @@
1
+ import { resolveMaybeOptionalOptions, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
2
+ import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
+
4
+ const COMMON_ORPC_ERROR_DEFS = {
5
+ BAD_REQUEST: {
6
+ status: 400,
7
+ message: "Bad Request"
8
+ },
9
+ UNAUTHORIZED: {
10
+ status: 401,
11
+ message: "Unauthorized"
12
+ },
13
+ FORBIDDEN: {
14
+ status: 403,
15
+ message: "Forbidden"
16
+ },
17
+ NOT_FOUND: {
18
+ status: 404,
19
+ message: "Not Found"
20
+ },
21
+ METHOD_NOT_SUPPORTED: {
22
+ status: 405,
23
+ message: "Method Not Supported"
24
+ },
25
+ NOT_ACCEPTABLE: {
26
+ status: 406,
27
+ message: "Not Acceptable"
28
+ },
29
+ TIMEOUT: {
30
+ status: 408,
31
+ message: "Request Timeout"
32
+ },
33
+ CONFLICT: {
34
+ status: 409,
35
+ message: "Conflict"
36
+ },
37
+ PRECONDITION_FAILED: {
38
+ status: 412,
39
+ message: "Precondition Failed"
40
+ },
41
+ PAYLOAD_TOO_LARGE: {
42
+ status: 413,
43
+ message: "Payload Too Large"
44
+ },
45
+ UNSUPPORTED_MEDIA_TYPE: {
46
+ status: 415,
47
+ message: "Unsupported Media Type"
48
+ },
49
+ UNPROCESSABLE_CONTENT: {
50
+ status: 422,
51
+ message: "Unprocessable Content"
52
+ },
53
+ TOO_MANY_REQUESTS: {
54
+ status: 429,
55
+ message: "Too Many Requests"
56
+ },
57
+ CLIENT_CLOSED_REQUEST: {
58
+ status: 499,
59
+ message: "Client Closed Request"
60
+ },
61
+ INTERNAL_SERVER_ERROR: {
62
+ status: 500,
63
+ message: "Internal Server Error"
64
+ },
65
+ NOT_IMPLEMENTED: {
66
+ status: 501,
67
+ message: "Not Implemented"
68
+ },
69
+ BAD_GATEWAY: {
70
+ status: 502,
71
+ message: "Bad Gateway"
72
+ },
73
+ SERVICE_UNAVAILABLE: {
74
+ status: 503,
75
+ message: "Service Unavailable"
76
+ },
77
+ GATEWAY_TIMEOUT: {
78
+ status: 504,
79
+ message: "Gateway Timeout"
80
+ }
81
+ };
82
+ function fallbackORPCErrorStatus(code, status) {
83
+ return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
84
+ }
85
+ function fallbackORPCErrorMessage(code, message) {
86
+ return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
87
+ }
88
+ class ORPCError extends Error {
89
+ defined;
90
+ code;
91
+ status;
92
+ data;
93
+ constructor(code, ...rest) {
94
+ const options = resolveMaybeOptionalOptions(rest);
95
+ if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
96
+ throw new Error("[ORPCError] Invalid error status code.");
97
+ }
98
+ const message = fallbackORPCErrorMessage(code, options.message);
99
+ super(message, options);
100
+ this.code = code;
101
+ this.status = fallbackORPCErrorStatus(code, options.status);
102
+ this.defined = options.defined ?? false;
103
+ this.data = options.data;
104
+ }
105
+ toJSON() {
106
+ return {
107
+ defined: this.defined,
108
+ code: this.code,
109
+ status: this.status,
110
+ message: this.message,
111
+ data: this.data
112
+ };
113
+ }
114
+ }
115
+ function isDefinedError(error) {
116
+ return error instanceof ORPCError && error.defined;
117
+ }
118
+ function toORPCError(error) {
119
+ return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
120
+ message: "Internal server error",
121
+ cause: error
122
+ });
123
+ }
124
+ function isORPCErrorStatus(status) {
125
+ return status < 200 || status >= 400;
126
+ }
127
+ function isORPCErrorJson(json) {
128
+ if (!isObject(json)) {
129
+ return false;
130
+ }
131
+ const validKeys = ["defined", "code", "status", "message", "data"];
132
+ if (Object.keys(json).some((k) => !validKeys.includes(k))) {
133
+ return false;
134
+ }
135
+ return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
136
+ }
137
+ function createORPCErrorFromJson(json, options = {}) {
138
+ return new ORPCError(json.code, {
139
+ ...options,
140
+ ...json
141
+ });
142
+ }
143
+
144
+ function mapEventIterator(iterator, maps) {
145
+ const mapError = async (error) => {
146
+ let mappedError = await maps.error(error);
147
+ if (mappedError !== error) {
148
+ const meta = getEventMeta(error);
149
+ if (meta && isTypescriptObject(mappedError)) {
150
+ mappedError = withEventMeta(mappedError, meta);
151
+ }
152
+ }
153
+ return mappedError;
154
+ };
155
+ return new AsyncIteratorClass(async () => {
156
+ const { done, value } = await (async () => {
157
+ try {
158
+ return await iterator.next();
159
+ } catch (error) {
160
+ throw await mapError(error);
161
+ }
162
+ })();
163
+ let mappedValue = await maps.value(value, done);
164
+ if (mappedValue !== value) {
165
+ const meta = getEventMeta(value);
166
+ if (meta && isTypescriptObject(mappedValue)) {
167
+ mappedValue = withEventMeta(mappedValue, meta);
168
+ }
169
+ }
170
+ return { done, value: mappedValue };
171
+ }, async () => {
172
+ try {
173
+ await iterator.return?.();
174
+ } catch (error) {
175
+ throw await mapError(error);
176
+ }
177
+ });
178
+ }
179
+
180
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, isORPCErrorJson as c, createORPCErrorFromJson as d, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.b825e0c",
4
+ "version": "0.0.0-next.b953b88",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,36 +15,50 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
+ },
27
+ "./standard": {
28
+ "types": "./dist/adapters/standard/index.d.mts",
29
+ "import": "./dist/adapters/standard/index.mjs",
30
+ "default": "./dist/adapters/standard/index.mjs"
21
31
  },
22
32
  "./fetch": {
23
- "types": "./dist/src/adapters/fetch/index.d.ts",
24
- "import": "./dist/fetch.js",
25
- "default": "./dist/fetch.js"
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
36
+ },
37
+ "./websocket": {
38
+ "types": "./dist/adapters/websocket/index.d.mts",
39
+ "import": "./dist/adapters/websocket/index.mjs",
40
+ "default": "./dist/adapters/websocket/index.mjs"
26
41
  },
27
- "./🔒/*": {
28
- "types": "./dist/src/*.d.ts"
42
+ "./message-port": {
43
+ "types": "./dist/adapters/message-port/index.d.mts",
44
+ "import": "./dist/adapters/message-port/index.mjs",
45
+ "default": "./dist/adapters/message-port/index.mjs"
29
46
  }
30
47
  },
31
48
  "files": [
32
- "!**/*.map",
33
- "!**/*.tsbuildinfo",
34
49
  "dist"
35
50
  ],
36
51
  "dependencies": {
37
- "content-disposition": "^0.5.4",
38
- "@orpc/contract": "0.0.0-next.b825e0c",
39
- "@orpc/server": "0.0.0-next.b825e0c",
40
- "@orpc/shared": "0.0.0-next.b825e0c"
52
+ "@orpc/shared": "0.0.0-next.b953b88",
53
+ "@orpc/standard-server": "0.0.0-next.b953b88",
54
+ "@orpc/standard-server-fetch": "0.0.0-next.b953b88",
55
+ "@orpc/standard-server-peer": "0.0.0-next.b953b88"
41
56
  },
42
57
  "devDependencies": {
43
- "zod": "^3.24.1",
44
- "@orpc/openapi": "0.0.0-next.b825e0c"
58
+ "zod": "^4.1.1"
45
59
  },
46
60
  "scripts": {
47
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
61
+ "build": "unbuild",
48
62
  "build:watch": "pnpm run build --watch",
49
63
  "type:check": "tsc -b"
50
64
  }
package/dist/fetch.js DELETED
@@ -1,103 +0,0 @@
1
- // src/adapters/fetch/orpc-link.ts
2
- import { ORPCError } from "@orpc/contract";
3
- import { fetchReToStandardBody } from "@orpc/server/fetch";
4
- import { RPCSerializer } from "@orpc/server/standard";
5
- import { isPlainObject, trim } from "@orpc/shared";
6
- import cd from "content-disposition";
7
- var RPCLink = class {
8
- fetch;
9
- rpcSerializer;
10
- maxURLLength;
11
- fallbackMethod;
12
- getMethod;
13
- getHeaders;
14
- url;
15
- constructor(options) {
16
- this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
17
- this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
18
- this.maxURLLength = options.maxURLLength ?? 2083;
19
- this.fallbackMethod = options.fallbackMethod ?? "POST";
20
- this.url = options.url;
21
- this.getMethod = async (path, input, context) => {
22
- return await options.method?.(path, input, context) ?? this.fallbackMethod;
23
- };
24
- this.getHeaders = async (path, input, context) => {
25
- return new Headers(await options.headers?.(path, input, context));
26
- };
27
- }
28
- async call(path, input, options) {
29
- const clientContext = options.context;
30
- const encoded = await this.encode(path, input, options);
31
- if (encoded.body instanceof Blob && !encoded.headers.has("content-disposition")) {
32
- encoded.headers.set("content-disposition", cd(encoded.body instanceof File ? encoded.body.name : "blob"));
33
- }
34
- const response = await this.fetch(encoded.url, {
35
- method: encoded.method,
36
- headers: encoded.headers,
37
- body: encoded.body,
38
- signal: options.signal
39
- }, clientContext);
40
- const body = await fetchReToStandardBody(response);
41
- const deserialized = (() => {
42
- try {
43
- return this.rpcSerializer.deserialize(body);
44
- } catch (error) {
45
- if (response.ok) {
46
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
47
- message: "Invalid RPC response",
48
- cause: error
49
- });
50
- }
51
- throw new ORPCError(response.status.toString(), {
52
- message: response.statusText
53
- });
54
- }
55
- })();
56
- if (response.ok) {
57
- return deserialized;
58
- }
59
- throw ORPCError.fromJSON(deserialized);
60
- }
61
- async encode(path, input, options) {
62
- const clientContext = options.context;
63
- const expectMethod = await this.getMethod(path, input, clientContext);
64
- const headers = await this.getHeaders(path, input, clientContext);
65
- const url = new URL(`${trim(this.url, "/")}/${path.map(encodeURIComponent).join("/")}`);
66
- headers.append("x-orpc-handler", "rpc");
67
- const serialized = this.rpcSerializer.serialize(input);
68
- if (expectMethod === "GET" && isPlainObject(serialized)) {
69
- const tryURL = new URL(url);
70
- tryURL.searchParams.append("data", JSON.stringify(serialized));
71
- if (tryURL.toString().length <= this.maxURLLength) {
72
- return {
73
- body: void 0,
74
- method: expectMethod,
75
- headers,
76
- url: tryURL
77
- };
78
- }
79
- }
80
- const method = expectMethod === "GET" ? this.fallbackMethod : expectMethod;
81
- if (isPlainObject(serialized)) {
82
- if (!headers.has("content-type")) {
83
- headers.set("content-type", "application/json");
84
- }
85
- return {
86
- body: JSON.stringify(serialized),
87
- method,
88
- headers,
89
- url
90
- };
91
- }
92
- return {
93
- body: serialized,
94
- method,
95
- headers,
96
- url
97
- };
98
- }
99
- };
100
- export {
101
- RPCLink
102
- };
103
- //# sourceMappingURL=fetch.js.map
package/dist/index.js DELETED
@@ -1,42 +0,0 @@
1
- // src/client.ts
2
- function createORPCClient(link, options) {
3
- const path = options?.path ?? [];
4
- const procedureClient = async (...[input, options2]) => {
5
- return await link.call(path, input, options2 ?? {});
6
- };
7
- const recursive = new Proxy(procedureClient, {
8
- get(target, key) {
9
- if (typeof key !== "string") {
10
- return Reflect.get(target, key);
11
- }
12
- return createORPCClient(link, {
13
- ...options,
14
- path: [...path, key]
15
- });
16
- }
17
- });
18
- return recursive;
19
- }
20
-
21
- // src/dynamic-link.ts
22
- var DynamicLink = class {
23
- constructor(linkResolver) {
24
- this.linkResolver = linkResolver;
25
- }
26
- async call(path, input, options) {
27
- const resolvedLink = await this.linkResolver(path, input, options.context);
28
- const output = await resolvedLink.call(path, input, options);
29
- return output;
30
- }
31
- };
32
-
33
- // src/index.ts
34
- import { isDefinedError, ORPCError, safe } from "@orpc/contract";
35
- export {
36
- DynamicLink,
37
- ORPCError,
38
- createORPCClient,
39
- isDefinedError,
40
- safe
41
- };
42
- //# sourceMappingURL=index.js.map
@@ -1,3 +0,0 @@
1
- export * from './orpc-link';
2
- export * from './types';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1,46 +0,0 @@
1
- import type { ClientOptions, HTTPMethod } from '@orpc/contract';
2
- import type { Promisable } from '@orpc/shared';
3
- import type { ClientLink } from '../../types';
4
- import type { FetchWithContext } from './types';
5
- import { RPCSerializer } from '@orpc/server/standard';
6
- export interface RPCLinkOptions<TClientContext> {
7
- /**
8
- * Base url for all requests.
9
- */
10
- url: string;
11
- /**
12
- * The maximum length of the URL.
13
- *
14
- * @default 2083
15
- */
16
- maxURLLength?: number;
17
- /**
18
- * The method used to make the request.
19
- *
20
- * @default 'POST'
21
- */
22
- method?(path: readonly string[], input: unknown, context: TClientContext): Promisable<HTTPMethod | undefined>;
23
- /**
24
- * The method to use when the payload cannot safely pass to the server with method return from method function.
25
- * GET is not allowed, it's very dangerous.
26
- *
27
- * @default 'POST'
28
- */
29
- fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
30
- headers?(path: readonly string[], input: unknown, context: TClientContext): Promisable<Headers | Record<string, string>>;
31
- fetch?: FetchWithContext<TClientContext>;
32
- rpcSerializer?: RPCSerializer;
33
- }
34
- export declare class RPCLink<TClientContext> implements ClientLink<TClientContext> {
35
- private readonly fetch;
36
- private readonly rpcSerializer;
37
- private readonly maxURLLength;
38
- private readonly fallbackMethod;
39
- private readonly getMethod;
40
- private readonly getHeaders;
41
- private readonly url;
42
- constructor(options: RPCLinkOptions<TClientContext>);
43
- call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
44
- private encode;
45
- }
46
- //# sourceMappingURL=orpc-link.d.ts.map
@@ -1,4 +0,0 @@
1
- export interface FetchWithContext<TClientContext> {
2
- (input: RequestInfo | URL, init: RequestInit | undefined, context: TClientContext): Promise<Response>;
3
- }
4
- //# sourceMappingURL=types.d.ts.map
@@ -1,11 +0,0 @@
1
- import type { AnyContractRouter, ContractRouterClient } from '@orpc/contract';
2
- import type { AnyRouter, RouterClient } from '@orpc/server';
3
- import type { ClientLink } from './types';
4
- export interface createORPCClientOptions {
5
- /**
6
- * Use as base path for all procedure, useful when you only want to call a subset of the procedure.
7
- */
8
- path?: string[];
9
- }
10
- export declare function createORPCClient<TRouter extends AnyRouter | AnyContractRouter, TClientContext = unknown>(link: ClientLink<TClientContext>, options?: createORPCClientOptions): TRouter extends AnyRouter ? RouterClient<TRouter, TClientContext> : TRouter extends AnyContractRouter ? ContractRouterClient<TRouter, TClientContext> : never;
11
- //# sourceMappingURL=client.d.ts.map
@@ -1,13 +0,0 @@
1
- import type { ClientOptions } from '@orpc/contract';
2
- import type { Promisable } from '@orpc/shared';
3
- import type { ClientLink } from './types';
4
- /**
5
- * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
6
- * based on the request path, input, and context.
7
- */
8
- export declare class DynamicLink<TClientContext> implements ClientLink<TClientContext> {
9
- private readonly linkResolver;
10
- constructor(linkResolver: (path: readonly string[], input: unknown, context: TClientContext) => Promisable<ClientLink<TClientContext>>);
11
- call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
12
- }
13
- //# sourceMappingURL=dynamic-link.d.ts.map
@@ -1,6 +0,0 @@
1
- /** unnoq */
2
- export * from './client';
3
- export * from './dynamic-link';
4
- export * from './types';
5
- export { isDefinedError, ORPCError, safe } from '@orpc/contract';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1,5 +0,0 @@
1
- import type { ClientOptions } from '@orpc/contract';
2
- export interface ClientLink<TClientContext> {
3
- call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
4
- }
5
- //# sourceMappingURL=types.d.ts.map