@orpc/client 0.0.0-next.d7b5662 → 0.0.0-next.da8ae32

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/fetch.js CHANGED
@@ -1,46 +1,111 @@
1
1
  // src/adapters/fetch/orpc-link.ts
2
- import { ORPCPayloadCodec } from "@orpc/server/fetch";
3
- import { ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim } from "@orpc/shared";
4
- import { ORPCError } from "@orpc/shared/error";
5
- var ORPCLink = class {
2
+ import { ORPCError } from "@orpc/contract";
3
+ import { fetchReToStandardBody } from "@orpc/server/fetch";
4
+ import { RPCSerializer } from "@orpc/server/standard";
5
+ import { isObject, trim } from "@orpc/shared";
6
+ import { contentDisposition } from "@tinyhttp/content-disposition";
7
+ var RPCLink = class {
8
+ fetch;
9
+ rpcSerializer;
10
+ maxURLLength;
11
+ fallbackMethod;
12
+ getMethod;
13
+ getHeaders;
14
+ url;
6
15
  constructor(options) {
7
- this.options = options;
8
16
  this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
9
- this.payloadCodec = options.payloadCodec ?? new ORPCPayloadCodec();
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
+ };
10
27
  }
11
- fetch;
12
- payloadCodec;
13
28
  async call(path, input, options) {
14
- const url = `${trim(this.options.url, "/")}/${path.map(encodeURIComponent).join("/")}`;
15
- const encoded = this.payloadCodec.encode(input);
16
- const headers = new Headers(encoded.headers);
17
- headers.append(ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE);
18
- const clientContext = options.context;
19
- let customHeaders = await this.options.headers?.(input, clientContext);
20
- customHeaders = customHeaders instanceof Headers ? customHeaders : new Headers(customHeaders);
21
- for (const [key, value] of customHeaders.entries()) {
22
- headers.append(key, value);
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", contentDisposition(encoded.body instanceof File ? encoded.body.name : "blob"));
23
33
  }
24
- const response = await this.fetch(url, {
25
- method: "POST",
26
- headers,
34
+ const response = await this.fetch(encoded.url, {
35
+ method: encoded.method,
36
+ headers: encoded.headers,
27
37
  body: encoded.body,
28
38
  signal: options.signal
29
39
  }, clientContext);
30
- const decoded = await this.payloadCodec.decode(response);
31
- if (!response.ok) {
32
- const error = ORPCError.fromJSON(decoded) ?? new ORPCError({
33
- status: response.status,
34
- code: "INTERNAL_SERVER_ERROR",
35
- message: "Internal server error",
36
- cause: decoded
37
- });
38
- throw error;
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" && isObject(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 (input === void 0) {
82
+ return {
83
+ body: void 0,
84
+ method,
85
+ headers,
86
+ url
87
+ };
39
88
  }
40
- return decoded;
89
+ if (isObject(serialized)) {
90
+ if (!headers.has("content-type")) {
91
+ headers.set("content-type", "application/json");
92
+ }
93
+ return {
94
+ body: JSON.stringify(serialized),
95
+ method,
96
+ headers,
97
+ url
98
+ };
99
+ }
100
+ return {
101
+ body: serialized,
102
+ method,
103
+ headers,
104
+ url
105
+ };
41
106
  }
42
107
  };
43
108
  export {
44
- ORPCLink
109
+ RPCLink
45
110
  };
46
111
  //# sourceMappingURL=fetch.js.map
package/dist/index.js CHANGED
@@ -24,16 +24,20 @@ var DynamicLink = class {
24
24
  this.linkResolver = linkResolver;
25
25
  }
26
26
  async call(path, input, options) {
27
- const resolvedLink = await this.linkResolver(path, input, options);
28
- const output = await resolvedLink.call(path, input, options);
27
+ const clientContext = options.context ?? {};
28
+ const resolvedLink = await this.linkResolver(path, input, clientContext);
29
+ const output = await resolvedLink.call(path, input, { ...options, context: clientContext });
29
30
  return output;
30
31
  }
31
32
  };
32
33
 
33
34
  // src/index.ts
34
- export * from "@orpc/shared/error";
35
+ import { isDefinedError, ORPCError, safe } from "@orpc/contract";
35
36
  export {
36
37
  DynamicLink,
37
- createORPCClient
38
+ ORPCError,
39
+ createORPCClient,
40
+ isDefinedError,
41
+ safe
38
42
  };
39
43
  //# sourceMappingURL=index.js.map
@@ -1,19 +1,46 @@
1
- import type { ProcedureClientOptions } from '@orpc/server';
1
+ import type { ClientContext, ClientOptions, HTTPMethod } from '@orpc/contract';
2
2
  import type { Promisable } from '@orpc/shared';
3
3
  import type { ClientLink } from '../../types';
4
4
  import type { FetchWithContext } from './types';
5
- import { type PublicORPCPayloadCodec } from '@orpc/server/fetch';
6
- export interface ORPCLinkOptions<TClientContext> {
5
+ import { RPCSerializer } from '@orpc/server/standard';
6
+ export interface RPCLinkOptions<TClientContext extends ClientContext> {
7
+ /**
8
+ * Base url for all requests.
9
+ */
7
10
  url: string;
8
- headers?: (input: unknown, context: TClientContext) => Promisable<Headers | Record<string, 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>>;
9
31
  fetch?: FetchWithContext<TClientContext>;
10
- payloadCodec?: PublicORPCPayloadCodec;
32
+ rpcSerializer?: RPCSerializer;
11
33
  }
12
- export declare class ORPCLink<TClientContext> implements ClientLink<TClientContext> {
13
- private readonly options;
34
+ export declare class RPCLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
14
35
  private readonly fetch;
15
- private readonly payloadCodec;
16
- constructor(options: ORPCLinkOptions<TClientContext>);
17
- call(path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext>): Promise<unknown>;
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;
18
45
  }
19
46
  //# sourceMappingURL=orpc-link.d.ts.map
@@ -1,4 +1,5 @@
1
- export interface FetchWithContext<TClientContext> {
2
- (input: RequestInfo | URL, init: RequestInit | undefined, context: TClientContext): Promise<Response>;
1
+ import type { ClientContext } from '@orpc/contract';
2
+ export interface FetchWithContext<TClientContext extends ClientContext> {
3
+ (url: Request | string | URL, init: RequestInit | undefined, context: TClientContext): Promise<Response>;
3
4
  }
4
5
  //# sourceMappingURL=types.d.ts.map
@@ -1,5 +1,5 @@
1
- import type { ContractRouter } from '@orpc/contract';
2
- import type { ANY_ROUTER, RouterClient } from '@orpc/server';
1
+ import type { AnyContractRouter, ClientContext, ContractRouterClient } from '@orpc/contract';
2
+ import type { AnyRouter, RouterClient } from '@orpc/server';
3
3
  import type { ClientLink } from './types';
4
4
  export interface createORPCClientOptions {
5
5
  /**
@@ -7,5 +7,5 @@ export interface createORPCClientOptions {
7
7
  */
8
8
  path?: string[];
9
9
  }
10
- export declare function createORPCClient<TRouter extends ANY_ROUTER | ContractRouter, TClientContext = unknown>(link: ClientLink<TClientContext>, options?: createORPCClientOptions): RouterClient<TRouter, TClientContext>;
10
+ export declare function createORPCClient<TRouter extends AnyRouter | AnyContractRouter, TClientContext extends ClientContext = Record<never, never>>(link: ClientLink<TClientContext>, options?: createORPCClientOptions): TRouter extends AnyRouter ? RouterClient<TRouter, TClientContext> : TRouter extends AnyContractRouter ? ContractRouterClient<TRouter, TClientContext> : never;
11
11
  //# sourceMappingURL=client.d.ts.map
@@ -1,15 +1,13 @@
1
- import type { ProcedureClientOptions } from '@orpc/server';
1
+ import type { ClientContext, ClientOptions } from '@orpc/contract';
2
2
  import type { Promisable } from '@orpc/shared';
3
3
  import type { ClientLink } from './types';
4
4
  /**
5
5
  * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
6
6
  * based on the request path, input, and context.
7
7
  */
8
- export declare class DynamicLink<TClientContext> implements ClientLink<TClientContext> {
8
+ export declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
9
9
  private readonly linkResolver;
10
- constructor(linkResolver: (path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext> & {
11
- context: TClientContext;
12
- }) => Promisable<ClientLink<TClientContext>>);
13
- call(path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext>): Promise<unknown>;
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>;
14
12
  }
15
13
  //# sourceMappingURL=dynamic-link.d.ts.map
@@ -2,5 +2,5 @@
2
2
  export * from './client';
3
3
  export * from './dynamic-link';
4
4
  export * from './types';
5
- export * from '@orpc/shared/error';
5
+ export { isDefinedError, ORPCError, safe } from '@orpc/contract';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1,5 +1,5 @@
1
- import type { ProcedureClientOptions } from '@orpc/server';
2
- export interface ClientLink<TClientContext> {
3
- call: (path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext>) => Promise<unknown>;
1
+ import type { ClientContext, ClientOptions } from '@orpc/contract';
2
+ export interface ClientLink<TClientContext extends ClientContext> {
3
+ call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
4
4
  }
5
5
  //# sourceMappingURL=types.d.ts.map
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.d7b5662",
4
+ "version": "0.0.0-next.da8ae32",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -33,16 +33,15 @@
33
33
  "!**/*.tsbuildinfo",
34
34
  "dist"
35
35
  ],
36
- "peerDependencies": {
37
- "@orpc/contract": "0.0.0-next.d7b5662"
38
- },
39
36
  "dependencies": {
40
- "@orpc/server": "0.0.0-next.d7b5662",
41
- "@orpc/shared": "0.0.0-next.d7b5662"
37
+ "@tinyhttp/content-disposition": "^2.2.2",
38
+ "@orpc/contract": "0.0.0-next.da8ae32",
39
+ "@orpc/server": "0.0.0-next.da8ae32",
40
+ "@orpc/shared": "0.0.0-next.da8ae32"
42
41
  },
43
42
  "devDependencies": {
44
43
  "zod": "^3.24.1",
45
- "@orpc/openapi": "0.0.0-next.d7b5662"
44
+ "@orpc/openapi": "0.0.0-next.da8ae32"
46
45
  },
47
46
  "scripts": {
48
47
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",