@orpc/client 0.0.0-next.911bdd9 → 0.0.0-next.9125edb

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,10 +1,11 @@
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 { toFetchBody, toStandardBody } from "@orpc/server-standard-fetch";
4
+ import { RPCSerializer } from "@orpc/server/standard";
5
+ import { isObject, trim } from "@orpc/shared";
6
+ var RPCLink = class {
6
7
  fetch;
7
- payloadCodec;
8
+ rpcSerializer;
8
9
  maxURLLength;
9
10
  fallbackMethod;
10
11
  getMethod;
@@ -12,7 +13,7 @@ var ORPCLink = class {
12
13
  url;
13
14
  constructor(options) {
14
15
  this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
15
- this.payloadCodec = options.payloadCodec ?? new ORPCPayloadCodec();
16
+ this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
16
17
  this.maxURLLength = options.maxURLLength ?? 2083;
17
18
  this.fallbackMethod = options.fallbackMethod ?? "POST";
18
19
  this.url = options.url;
@@ -24,64 +25,73 @@ var ORPCLink = class {
24
25
  };
25
26
  }
26
27
  async call(path, input, options) {
27
- const clientContext = options.context;
28
+ const clientContext = options.context ?? {};
28
29
  const encoded = await this.encode(path, input, options);
30
+ const fetchBody = toFetchBody(encoded.body, encoded.headers);
29
31
  const response = await this.fetch(encoded.url, {
30
32
  method: encoded.method,
31
33
  headers: encoded.headers,
32
- body: encoded.body,
34
+ body: fetchBody,
33
35
  signal: options.signal
34
36
  }, clientContext);
35
- const decoded = await this.payloadCodec.decode(response);
36
- if (!response.ok) {
37
- const error = ORPCError.fromJSON(decoded) ?? new ORPCError({
38
- status: response.status,
39
- code: "INTERNAL_SERVER_ERROR",
40
- message: "Internal server error",
41
- cause: decoded
42
- });
43
- throw error;
37
+ const body = await toStandardBody(response);
38
+ const deserialized = (() => {
39
+ try {
40
+ return this.rpcSerializer.deserialize(body);
41
+ } catch (error) {
42
+ if (response.ok) {
43
+ throw new ORPCError("INTERNAL_SERVER_ERROR", {
44
+ message: "Invalid RPC response",
45
+ cause: error
46
+ });
47
+ }
48
+ throw new ORPCError(response.status.toString(), {
49
+ message: response.statusText
50
+ });
51
+ }
52
+ })();
53
+ if (response.ok) {
54
+ return deserialized;
44
55
  }
45
- return decoded;
56
+ throw ORPCError.fromJSON(deserialized);
46
57
  }
47
58
  async encode(path, input, options) {
48
59
  const clientContext = options.context;
49
60
  const expectMethod = await this.getMethod(path, input, clientContext);
50
- const methods = /* @__PURE__ */ new Set([expectMethod, this.fallbackMethod]);
51
- const baseHeaders = await this.getHeaders(path, input, clientContext);
52
- const baseUrl = new URL(`${trim(this.url, "/")}/${path.map(encodeURIComponent).join("/")}`);
53
- baseHeaders.append(ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE);
54
- for (const method of methods) {
55
- const url = new URL(baseUrl);
56
- const headers = new Headers(baseHeaders);
57
- const encoded = this.payloadCodec.encode(input, method, this.fallbackMethod);
58
- if (encoded.query) {
59
- for (const [key, value] of encoded.query.entries()) {
60
- url.searchParams.append(key, value);
61
- }
62
- }
63
- if (url.toString().length > this.maxURLLength) {
64
- continue;
65
- }
66
- if (encoded.headers) {
67
- for (const [key, value] of encoded.headers.entries()) {
68
- headers.append(key, value);
69
- }
61
+ const headers = await this.getHeaders(path, input, clientContext);
62
+ const url = new URL(`${trim(this.url, "/")}/${path.map(encodeURIComponent).join("/")}`);
63
+ headers.append("x-orpc-handler", "rpc");
64
+ const serialized = this.rpcSerializer.serialize(input);
65
+ if (expectMethod === "GET" && isObject(serialized)) {
66
+ const tryURL = new URL(url);
67
+ tryURL.searchParams.append("data", JSON.stringify(serialized));
68
+ if (tryURL.toString().length <= this.maxURLLength) {
69
+ return {
70
+ body: void 0,
71
+ method: expectMethod,
72
+ headers,
73
+ url: tryURL
74
+ };
70
75
  }
76
+ }
77
+ const method = expectMethod === "GET" ? this.fallbackMethod : expectMethod;
78
+ if (input === void 0) {
71
79
  return {
72
- url,
80
+ body: void 0,
81
+ method,
73
82
  headers,
74
- method: encoded.method,
75
- body: encoded.body
83
+ url
76
84
  };
77
85
  }
78
- throw new ORPCError({
79
- code: "BAD_REQUEST",
80
- message: "Cannot encode the request, please check the url length or payload."
81
- });
86
+ return {
87
+ body: serialized,
88
+ method,
89
+ headers,
90
+ url
91
+ };
82
92
  }
83
93
  };
84
94
  export {
85
- ORPCLink
95
+ RPCLink
86
96
  };
87
97
  //# 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.context);
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,10 +1,9 @@
1
- import type { HTTPMethod } from '@orpc/contract';
2
- import type { ProcedureClientOptions } from '@orpc/server';
1
+ import type { ClientContext, ClientOptions, HTTPMethod } from '@orpc/contract';
3
2
  import type { Promisable } from '@orpc/shared';
4
3
  import type { ClientLink } from '../../types';
5
4
  import type { FetchWithContext } from './types';
6
- import { type PublicORPCPayloadCodec } from '@orpc/server/fetch';
7
- export interface ORPCLinkOptions<TClientContext> {
5
+ import { RPCSerializer } from '@orpc/server/standard';
6
+ export interface RPCLinkOptions<TClientContext extends ClientContext> {
8
7
  /**
9
8
  * Base url for all requests.
10
9
  */
@@ -20,28 +19,28 @@ export interface ORPCLinkOptions<TClientContext> {
20
19
  *
21
20
  * @default 'POST'
22
21
  */
23
- method?: (path: readonly string[], input: unknown, context: TClientContext) => Promisable<HTTPMethod | undefined>;
22
+ method?(path: readonly string[], input: unknown, context: TClientContext): Promisable<HTTPMethod | undefined>;
24
23
  /**
25
24
  * The method to use when the payload cannot safely pass to the server with method return from method function.
26
- * Do not use GET as fallback method, it's very dangerous.
25
+ * GET is not allowed, it's very dangerous.
27
26
  *
28
27
  * @default 'POST'
29
28
  */
30
- fallbackMethod?: HTTPMethod;
31
- headers?: (path: readonly string[], input: unknown, context: TClientContext) => Promisable<Headers | Record<string, string>>;
29
+ fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
30
+ headers?(path: readonly string[], input: unknown, context: TClientContext): Promisable<Headers | Record<string, string>>;
32
31
  fetch?: FetchWithContext<TClientContext>;
33
- payloadCodec?: PublicORPCPayloadCodec;
32
+ rpcSerializer?: RPCSerializer;
34
33
  }
35
- export declare class ORPCLink<TClientContext> implements ClientLink<TClientContext> {
34
+ export declare class RPCLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
36
35
  private readonly fetch;
37
- private readonly payloadCodec;
36
+ private readonly rpcSerializer;
38
37
  private readonly maxURLLength;
39
38
  private readonly fallbackMethod;
40
39
  private readonly getMethod;
41
40
  private readonly getHeaders;
42
41
  private readonly url;
43
- constructor(options: ORPCLinkOptions<TClientContext>);
44
- call(path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext>): Promise<unknown>;
42
+ constructor(options: RPCLinkOptions<TClientContext>);
43
+ call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
45
44
  private encode;
46
45
  }
47
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,13 +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
10
  constructor(linkResolver: (path: readonly string[], input: unknown, context: TClientContext) => Promisable<ClientLink<TClientContext>>);
11
- call(path: readonly string[], input: unknown, options: ProcedureClientOptions<TClientContext>): Promise<unknown>;
11
+ call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
12
12
  }
13
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.911bdd9",
4
+ "version": "0.0.0-next.9125edb",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -33,16 +33,16 @@
33
33
  "!**/*.tsbuildinfo",
34
34
  "dist"
35
35
  ],
36
- "peerDependencies": {
37
- "@orpc/contract": "0.0.0-next.911bdd9"
38
- },
39
36
  "dependencies": {
40
- "@orpc/shared": "0.0.0-next.911bdd9",
41
- "@orpc/server": "0.0.0-next.911bdd9"
37
+ "@orpc/server-standard": "^0.0.0",
38
+ "@orpc/server-standard-fetch": "^0.0.0",
39
+ "@orpc/contract": "0.0.0-next.9125edb",
40
+ "@orpc/server": "0.0.0-next.9125edb",
41
+ "@orpc/shared": "0.0.0-next.9125edb"
42
42
  },
43
43
  "devDependencies": {
44
44
  "zod": "^3.24.1",
45
- "@orpc/openapi": "0.0.0-next.911bdd9"
45
+ "@orpc/openapi": "0.0.0-next.9125edb"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",