@orpc/client 0.0.0-next.4555a17 → 0.0.0-next.45fde13

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.
@@ -0,0 +1,208 @@
1
+ import { resolveMaybeOptionalOptions, getConstructor, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
2
+ import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
+
4
+ const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
5
+ const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.45fde13";
6
+
7
+ const COMMON_ORPC_ERROR_DEFS = {
8
+ BAD_REQUEST: {
9
+ status: 400,
10
+ message: "Bad Request"
11
+ },
12
+ UNAUTHORIZED: {
13
+ status: 401,
14
+ message: "Unauthorized"
15
+ },
16
+ FORBIDDEN: {
17
+ status: 403,
18
+ message: "Forbidden"
19
+ },
20
+ NOT_FOUND: {
21
+ status: 404,
22
+ message: "Not Found"
23
+ },
24
+ METHOD_NOT_SUPPORTED: {
25
+ status: 405,
26
+ message: "Method Not Supported"
27
+ },
28
+ NOT_ACCEPTABLE: {
29
+ status: 406,
30
+ message: "Not Acceptable"
31
+ },
32
+ TIMEOUT: {
33
+ status: 408,
34
+ message: "Request Timeout"
35
+ },
36
+ CONFLICT: {
37
+ status: 409,
38
+ message: "Conflict"
39
+ },
40
+ PRECONDITION_FAILED: {
41
+ status: 412,
42
+ message: "Precondition Failed"
43
+ },
44
+ PAYLOAD_TOO_LARGE: {
45
+ status: 413,
46
+ message: "Payload Too Large"
47
+ },
48
+ UNSUPPORTED_MEDIA_TYPE: {
49
+ status: 415,
50
+ message: "Unsupported Media Type"
51
+ },
52
+ UNPROCESSABLE_CONTENT: {
53
+ status: 422,
54
+ message: "Unprocessable Content"
55
+ },
56
+ TOO_MANY_REQUESTS: {
57
+ status: 429,
58
+ message: "Too Many Requests"
59
+ },
60
+ CLIENT_CLOSED_REQUEST: {
61
+ status: 499,
62
+ message: "Client Closed Request"
63
+ },
64
+ INTERNAL_SERVER_ERROR: {
65
+ status: 500,
66
+ message: "Internal Server Error"
67
+ },
68
+ NOT_IMPLEMENTED: {
69
+ status: 501,
70
+ message: "Not Implemented"
71
+ },
72
+ BAD_GATEWAY: {
73
+ status: 502,
74
+ message: "Bad Gateway"
75
+ },
76
+ SERVICE_UNAVAILABLE: {
77
+ status: 503,
78
+ message: "Service Unavailable"
79
+ },
80
+ GATEWAY_TIMEOUT: {
81
+ status: 504,
82
+ message: "Gateway Timeout"
83
+ }
84
+ };
85
+ function fallbackORPCErrorStatus(code, status) {
86
+ return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
87
+ }
88
+ function fallbackORPCErrorMessage(code, message) {
89
+ return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
90
+ }
91
+ const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
92
+ void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
93
+ const globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
94
+ class ORPCError extends Error {
95
+ defined;
96
+ code;
97
+ status;
98
+ data;
99
+ constructor(code, ...rest) {
100
+ const options = resolveMaybeOptionalOptions(rest);
101
+ if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
102
+ throw new Error("[ORPCError] Invalid error status code.");
103
+ }
104
+ const message = fallbackORPCErrorMessage(code, options.message);
105
+ super(message, options);
106
+ this.code = code;
107
+ this.status = fallbackORPCErrorStatus(code, options.status);
108
+ this.defined = options.defined ?? false;
109
+ this.data = options.data;
110
+ }
111
+ toJSON() {
112
+ return {
113
+ defined: this.defined,
114
+ code: this.code,
115
+ status: this.status,
116
+ message: this.message,
117
+ data: this.data
118
+ };
119
+ }
120
+ /**
121
+ * Workaround for Next.js where different contexts use separate
122
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
123
+ * `instanceof` checks across contexts.
124
+ *
125
+ * This is particularly problematic with "Optimized SSR", where orpc-client
126
+ * executes in one context but is invoked from another. When an error is thrown
127
+ * in the execution context, `instanceof ORPCError` checks fail in the
128
+ * invocation context due to separate class constructors.
129
+ *
130
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
131
+ */
132
+ static [Symbol.hasInstance](instance) {
133
+ if (globalORPCErrorConstructors.has(this)) {
134
+ const constructor = getConstructor(instance);
135
+ if (constructor && globalORPCErrorConstructors.has(constructor)) {
136
+ return true;
137
+ }
138
+ }
139
+ return super[Symbol.hasInstance](instance);
140
+ }
141
+ }
142
+ globalORPCErrorConstructors.add(ORPCError);
143
+ function isDefinedError(error) {
144
+ return error instanceof ORPCError && error.defined;
145
+ }
146
+ function toORPCError(error) {
147
+ return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
148
+ message: "Internal server error",
149
+ cause: error
150
+ });
151
+ }
152
+ function isORPCErrorStatus(status) {
153
+ return status < 200 || status >= 400;
154
+ }
155
+ function isORPCErrorJson(json) {
156
+ if (!isObject(json)) {
157
+ return false;
158
+ }
159
+ const validKeys = ["defined", "code", "status", "message", "data"];
160
+ if (Object.keys(json).some((k) => !validKeys.includes(k))) {
161
+ return false;
162
+ }
163
+ 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";
164
+ }
165
+ function createORPCErrorFromJson(json, options = {}) {
166
+ return new ORPCError(json.code, {
167
+ ...options,
168
+ ...json
169
+ });
170
+ }
171
+
172
+ function mapEventIterator(iterator, maps) {
173
+ const mapError = async (error) => {
174
+ let mappedError = await maps.error(error);
175
+ if (mappedError !== error) {
176
+ const meta = getEventMeta(error);
177
+ if (meta && isTypescriptObject(mappedError)) {
178
+ mappedError = withEventMeta(mappedError, meta);
179
+ }
180
+ }
181
+ return mappedError;
182
+ };
183
+ return new AsyncIteratorClass(async () => {
184
+ const { done, value } = await (async () => {
185
+ try {
186
+ return await iterator.next();
187
+ } catch (error) {
188
+ throw await mapError(error);
189
+ }
190
+ })();
191
+ let mappedValue = await maps.value(value, done);
192
+ if (mappedValue !== value) {
193
+ const meta = getEventMeta(value);
194
+ if (meta && isTypescriptObject(mappedValue)) {
195
+ mappedValue = withEventMeta(mappedValue, meta);
196
+ }
197
+ }
198
+ return { done, value: mappedValue };
199
+ }, async () => {
200
+ try {
201
+ await iterator.return?.();
202
+ } catch (error) {
203
+ throw await mapError(error);
204
+ }
205
+ });
206
+ }
207
+
208
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, mapEventIterator as m, toORPCError as t };
@@ -0,0 +1,91 @@
1
+ import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.BOYsZIRq.mjs';
2
+ import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.Bwgm6dgk.mjs';
3
+ import { Segment, Value, Promisable } from '@orpc/shared';
4
+ import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
5
+
6
+ declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
7
+ readonly BIGINT: 0;
8
+ readonly DATE: 1;
9
+ readonly NAN: 2;
10
+ readonly UNDEFINED: 3;
11
+ readonly URL: 4;
12
+ readonly REGEXP: 5;
13
+ readonly SET: 6;
14
+ readonly MAP: 7;
15
+ };
16
+ type StandardRPCJsonSerializedMetaItem = readonly [type: number, ...path: Segment[]];
17
+ type StandardRPCJsonSerialized = [json: unknown, meta: StandardRPCJsonSerializedMetaItem[], maps: Segment[][], blobs: Blob[]];
18
+ interface StandardRPCCustomJsonSerializer {
19
+ type: number;
20
+ condition(data: unknown): boolean;
21
+ serialize(data: any): unknown;
22
+ deserialize(serialized: any): unknown;
23
+ }
24
+ interface StandardRPCJsonSerializerOptions {
25
+ customJsonSerializers?: readonly StandardRPCCustomJsonSerializer[];
26
+ }
27
+ declare class StandardRPCJsonSerializer {
28
+ private readonly customSerializers;
29
+ constructor(options?: StandardRPCJsonSerializerOptions);
30
+ serialize(data: unknown, segments?: Segment[], meta?: StandardRPCJsonSerializedMetaItem[], maps?: Segment[][], blobs?: Blob[]): StandardRPCJsonSerialized;
31
+ deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[]): unknown;
32
+ deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[], maps: readonly Segment[][], getBlob: (index: number) => Blob): unknown;
33
+ }
34
+
35
+ declare class StandardRPCSerializer {
36
+ #private;
37
+ private readonly jsonSerializer;
38
+ constructor(jsonSerializer: StandardRPCJsonSerializer);
39
+ serialize(data: unknown): object;
40
+ deserialize(data: unknown): unknown;
41
+ }
42
+
43
+ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
44
+ /**
45
+ * Base url for all requests.
46
+ */
47
+ url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
48
+ /**
49
+ * The maximum length of the URL.
50
+ *
51
+ * @default 2083
52
+ */
53
+ maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
54
+ /**
55
+ * The method used to make the request.
56
+ *
57
+ * @default 'POST'
58
+ */
59
+ method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
60
+ /**
61
+ * The method to use when the payload cannot safely pass to the server with method return from method function.
62
+ * GET is not allowed, it's very dangerous.
63
+ *
64
+ * @default 'POST'
65
+ */
66
+ fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
67
+ /**
68
+ * Inject headers to the request.
69
+ */
70
+ headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
71
+ }
72
+ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
73
+ private readonly serializer;
74
+ private readonly baseUrl;
75
+ private readonly maxUrlLength;
76
+ private readonly fallbackMethod;
77
+ private readonly expectedMethod;
78
+ private readonly headers;
79
+ constructor(serializer: StandardRPCSerializer, options: StandardRPCLinkCodecOptions<T>);
80
+ encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
81
+ decode(response: StandardLazyResponse): Promise<unknown>;
82
+ }
83
+
84
+ interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
85
+ }
86
+ declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
87
+ constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
88
+ }
89
+
90
+ export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S, StandardRPCJsonSerializer as e, StandardRPCLink as g, StandardRPCLinkCodec as i, StandardRPCSerializer as j };
91
+ export type { StandardRPCJsonSerializedMetaItem as a, StandardRPCJsonSerialized as b, StandardRPCCustomJsonSerializer as c, StandardRPCJsonSerializerOptions as d, StandardRPCLinkOptions as f, StandardRPCLinkCodecOptions as h };
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.4555a17",
4
+ "version": "0.0.0-next.45fde13",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,33 +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
21
  },
22
- "./🔒/*": {
23
- "types": "./dist/src/*.d.ts"
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"
31
+ },
32
+ "./fetch": {
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"
41
+ },
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"
24
46
  }
25
47
  },
26
48
  "files": [
27
- "!**/*.map",
28
- "!**/*.tsbuildinfo",
29
49
  "dist"
30
50
  ],
31
- "peerDependencies": {
32
- "@orpc/contract": "0.0.0-next.4555a17",
33
- "@orpc/server": "0.0.0-next.4555a17"
34
- },
35
51
  "dependencies": {
36
- "@orpc/transformer": "0.0.0-next.4555a17",
37
- "@orpc/shared": "0.0.0-next.4555a17"
52
+ "@orpc/shared": "0.0.0-next.45fde13",
53
+ "@orpc/standard-server-peer": "0.0.0-next.45fde13",
54
+ "@orpc/standard-server": "0.0.0-next.45fde13",
55
+ "@orpc/standard-server-fetch": "0.0.0-next.45fde13"
38
56
  },
39
57
  "devDependencies": {
40
- "zod": "^3.24.1",
41
- "@orpc/openapi": "0.0.0-next.4555a17"
58
+ "zod": "^4.1.11"
42
59
  },
43
60
  "scripts": {
44
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
61
+ "build": "unbuild",
45
62
  "build:watch": "pnpm run build --watch",
46
63
  "type:check": "tsc -b"
47
64
  }
package/dist/index.js DELETED
@@ -1,86 +0,0 @@
1
- // src/procedure.ts
2
- import { ORPC_PROTOCOL_HEADER, ORPC_PROTOCOL_VALUE, trim } from "@orpc/shared";
3
- import { ORPCError } from "@orpc/shared/error";
4
- import { ORPCDeserializer, ORPCSerializer } from "@orpc/transformer";
5
- var serializer = new ORPCSerializer();
6
- var deserializer = new ORPCDeserializer();
7
- function createProcedureClient(options) {
8
- const client = async (...args) => {
9
- const [input, callerOptions] = args;
10
- const fetch_ = options.fetch ?? fetch;
11
- const url = `${trim(options.baseURL, "/")}/${options.path.map(encodeURIComponent).join("/")}`;
12
- const headers = new Headers({
13
- [ORPC_PROTOCOL_HEADER]: ORPC_PROTOCOL_VALUE
14
- });
15
- let customHeaders = await options.headers?.(input);
16
- customHeaders = customHeaders instanceof Headers ? customHeaders : new Headers(customHeaders);
17
- for (const [key, value] of customHeaders.entries()) {
18
- headers.append(key, value);
19
- }
20
- const serialized = serializer.serialize(input);
21
- for (const [key, value] of serialized.headers.entries()) {
22
- headers.append(key, value);
23
- }
24
- const response = await fetch_(url, {
25
- method: "POST",
26
- headers,
27
- body: serialized.body,
28
- signal: callerOptions?.signal
29
- });
30
- const json = await (async () => {
31
- try {
32
- return await deserializer.deserialize(response);
33
- } catch (e) {
34
- throw new ORPCError({
35
- code: "INTERNAL_SERVER_ERROR",
36
- message: "Cannot parse response.",
37
- cause: e
38
- });
39
- }
40
- })();
41
- if (!response.ok) {
42
- throw ORPCError.fromJSON(json) ?? new ORPCError({
43
- status: response.status,
44
- code: "INTERNAL_SERVER_ERROR",
45
- message: "Internal server error"
46
- });
47
- }
48
- return json;
49
- };
50
- return client;
51
- }
52
-
53
- // src/router.ts
54
- function createRouterClient(options) {
55
- const path = options?.path ?? [];
56
- const client = new Proxy(
57
- createProcedureClient({
58
- baseURL: options.baseURL,
59
- fetch: options.fetch,
60
- headers: options.headers,
61
- path
62
- }),
63
- {
64
- get(target, key) {
65
- if (typeof key !== "string") {
66
- return Reflect.get(target, key);
67
- }
68
- return createRouterClient({
69
- ...options,
70
- path: [...path, key]
71
- });
72
- }
73
- }
74
- );
75
- return client;
76
- }
77
-
78
- // src/index.ts
79
- export * from "@orpc/shared/error";
80
- var createORPCClient = createRouterClient;
81
- export {
82
- createORPCClient,
83
- createProcedureClient,
84
- createRouterClient
85
- };
86
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- /** unnoq */
2
- import { createRouterClient } from './router';
3
- export * from './procedure';
4
- export * from './router';
5
- export * from '@orpc/shared/error';
6
- export declare const createORPCClient: typeof createRouterClient;
7
- //# sourceMappingURL=index.d.ts.map
@@ -1,24 +0,0 @@
1
- import type { Caller } from '@orpc/server';
2
- import type { Promisable } from '@orpc/shared';
3
- export interface CreateProcedureClientOptions {
4
- /**
5
- * The base url of the server.
6
- */
7
- baseURL: string;
8
- /**
9
- * The fetch function used to make the request.
10
- * @default global fetch
11
- */
12
- fetch?: typeof fetch;
13
- /**
14
- * The headers used to make the request.
15
- * Invoked before the request is made.
16
- */
17
- headers?: (input: unknown) => Promisable<Headers | Record<string, string>>;
18
- /**
19
- * The path of the procedure on server.
20
- */
21
- path: string[];
22
- }
23
- export declare function createProcedureClient<TInput, TOutput>(options: CreateProcedureClientOptions): Caller<TInput, TOutput>;
24
- //# sourceMappingURL=procedure.d.ts.map
@@ -1,9 +0,0 @@
1
- import type { ContractProcedure, ContractRouter, SchemaInput, SchemaOutput } from '@orpc/contract';
2
- import type { Caller, Lazy, Procedure, Router } from '@orpc/server';
3
- import type { SetOptional } from '@orpc/shared';
4
- import type { CreateProcedureClientOptions } from './procedure';
5
- export type RouterClient<T extends Router<any> | ContractRouter> = {
6
- [K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> | Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> | Lazy<Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>> ? Caller<SchemaInput<UInputSchema>, SchemaOutput<UOutputSchema, UFuncOutput>> : T[K] extends Router<any> | ContractRouter ? RouterClient<T[K]> : never;
7
- };
8
- export declare function createRouterClient<T extends Router<any> | ContractRouter>(options: SetOptional<CreateProcedureClientOptions, 'path'>): RouterClient<T>;
9
- //# sourceMappingURL=router.d.ts.map