@orpc/client 0.0.0-next.d137cdf → 0.0.0-next.d42488d

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -1,32 +1,29 @@
1
- // src/procedure.ts
2
- import {
3
- ORPC_HEADER,
4
- ORPC_HEADER_VALUE
5
- } from "@orpc/contract";
6
- import { trim } from "@orpc/shared";
1
+ // src/procedure-fetch-client.ts
2
+ import { ORPCPayloadCodec } from "@orpc/server/fetch";
3
+ import { ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim } from "@orpc/shared";
7
4
  import { ORPCError } from "@orpc/shared/error";
8
- import { ORPCDeserializer, ORPCSerializer } from "@orpc/transformer";
9
- function createProcedureClient(options) {
10
- const serializer = new ORPCSerializer();
11
- const deserializer = new ORPCDeserializer();
12
- const client = async (input) => {
13
- const fetch_ = options.fetch ?? fetch;
5
+ var payloadCodec = new ORPCPayloadCodec();
6
+ function createProcedureFetchClient(options) {
7
+ const client = async (...[input, callerOptions]) => {
8
+ const fetchClient = options.fetch ?? fetch;
14
9
  const url = `${trim(options.baseURL, "/")}/${options.path.map(encodeURIComponent).join("/")}`;
15
- let headers = await options.headers?.(input);
16
- headers = headers instanceof Headers ? headers : new Headers(headers);
17
- const { body, headers: headers_ } = serializer.serialize(input);
18
- for (const [key, value] of headers_.entries()) {
19
- headers.set(key, value);
10
+ const encoded = payloadCodec.encode(input);
11
+ const headers = new Headers(encoded.headers);
12
+ headers.append(ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE);
13
+ let customHeaders = await options.headers?.(input);
14
+ customHeaders = customHeaders instanceof Headers ? customHeaders : new Headers(customHeaders);
15
+ for (const [key, value] of customHeaders.entries()) {
16
+ headers.append(key, value);
20
17
  }
21
- headers.set(ORPC_HEADER, ORPC_HEADER_VALUE);
22
- const response = await fetch_(url, {
18
+ const response = await fetchClient(url, {
23
19
  method: "POST",
24
20
  headers,
25
- body
21
+ body: encoded.body,
22
+ signal: callerOptions?.signal
26
23
  });
27
24
  const json = await (async () => {
28
25
  try {
29
- return await deserializer.deserialize(response);
26
+ return await payloadCodec.decode(response);
30
27
  } catch (e) {
31
28
  throw new ORPCError({
32
29
  code: "INTERNAL_SERVER_ERROR",
@@ -47,14 +44,12 @@ function createProcedureClient(options) {
47
44
  return client;
48
45
  }
49
46
 
50
- // src/router.ts
51
- function createRouterClient(options) {
47
+ // src/router-fetch-client.ts
48
+ function createRouterFetchClient(options) {
52
49
  const path = options?.path ?? [];
53
50
  const client = new Proxy(
54
- createProcedureClient({
55
- baseURL: options.baseURL,
56
- fetch: options.fetch,
57
- headers: options.headers,
51
+ createProcedureFetchClient({
52
+ ...options,
58
53
  path
59
54
  }),
60
55
  {
@@ -62,7 +57,7 @@ function createRouterClient(options) {
62
57
  if (typeof key !== "string") {
63
58
  return Reflect.get(target, key);
64
59
  }
65
- return createRouterClient({
60
+ return createRouterFetchClient({
66
61
  ...options,
67
62
  path: [...path, key]
68
63
  });
@@ -74,10 +69,10 @@ function createRouterClient(options) {
74
69
 
75
70
  // src/index.ts
76
71
  export * from "@orpc/shared/error";
77
- var createORPCClient = createRouterClient;
72
+ var createORPCFetchClient = createRouterFetchClient;
78
73
  export {
79
- createORPCClient,
80
- createProcedureClient,
81
- createRouterClient
74
+ createORPCFetchClient,
75
+ createProcedureFetchClient,
76
+ createRouterFetchClient
82
77
  };
83
78
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  /** unnoq */
2
- import { createRouterClient } from './router';
3
- export * from './procedure';
4
- export * from './router';
2
+ import { createRouterFetchClient } from './router-fetch-client';
3
+ export * from './procedure-fetch-client';
4
+ export * from './router-fetch-client';
5
5
  export * from '@orpc/shared/error';
6
- export declare const createORPCClient: typeof createRouterClient;
6
+ export declare const createORPCFetchClient: typeof createRouterFetchClient;
7
7
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,24 @@
1
+ import type { ProcedureClient } 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 createProcedureFetchClient<TInput, TOutput>(options: CreateProcedureClientOptions): ProcedureClient<TInput, TOutput>;
24
+ //# sourceMappingURL=procedure-fetch-client.d.ts.map
@@ -0,0 +1,6 @@
1
+ import type { ContractRouter } from '@orpc/contract';
2
+ import type { ANY_ROUTER, RouterClient } from '@orpc/server';
3
+ import type { SetOptional } from '@orpc/shared';
4
+ import type { CreateProcedureClientOptions } from './procedure-fetch-client';
5
+ export declare function createRouterFetchClient<T extends ANY_ROUTER | ContractRouter>(options: SetOptional<CreateProcedureClientOptions, 'path'>): RouterClient<T>;
6
+ //# sourceMappingURL=router-fetch-client.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.d137cdf",
4
+ "version": "0.0.0-next.d42488d",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,16 +29,15 @@
29
29
  "dist"
30
30
  ],
31
31
  "peerDependencies": {
32
- "@orpc/server": "0.0.0-next.d137cdf",
33
- "@orpc/contract": "0.0.0-next.d137cdf"
32
+ "@orpc/contract": "0.0.0-next.d42488d"
34
33
  },
35
34
  "dependencies": {
36
- "@orpc/shared": "0.0.0-next.d137cdf",
37
- "@orpc/transformer": "0.0.0-next.d137cdf"
35
+ "@orpc/shared": "0.0.0-next.d42488d",
36
+ "@orpc/server": "0.0.0-next.d42488d"
38
37
  },
39
38
  "devDependencies": {
40
- "zod": "^3.23.8",
41
- "@orpc/openapi": "0.0.0-next.d137cdf"
39
+ "zod": "^3.24.1",
40
+ "@orpc/openapi": "0.0.0-next.d42488d"
42
41
  },
43
42
  "scripts": {
44
43
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
@@ -1,27 +0,0 @@
1
- import type { Promisable } from '@orpc/shared';
2
- import { type Schema, type SchemaInput, type SchemaOutput } from '@orpc/contract';
3
- export interface ProcedureClient<TInputSchema extends Schema, TOutputSchema extends Schema, TFuncOutput extends SchemaOutput<TOutputSchema>> {
4
- (input: SchemaInput<TInputSchema>): Promise<SchemaOutput<TOutputSchema, TFuncOutput>>;
5
- }
6
- export interface CreateProcedureClientOptions {
7
- /**
8
- * The base url of the server.
9
- */
10
- baseURL: string;
11
- /**
12
- * The fetch function used to make the request.
13
- * @default global fetch
14
- */
15
- fetch?: typeof fetch;
16
- /**
17
- * The headers used to make the request.
18
- * Invoked before the request is made.
19
- */
20
- headers?: (input: unknown) => Promisable<Headers | Record<string, string>>;
21
- /**
22
- * The path of the procedure on server.
23
- */
24
- path: string[];
25
- }
26
- export declare function createProcedureClient<TInputSchema extends Schema, TOutputSchema extends Schema, TFuncOutput extends SchemaOutput<TOutputSchema>>(options: CreateProcedureClientOptions): ProcedureClient<TInputSchema, TOutputSchema, TFuncOutput>;
27
- //# sourceMappingURL=procedure.d.ts.map
@@ -1,34 +0,0 @@
1
- import type { ContractProcedure, ContractRouter, SchemaOutput } from '@orpc/contract';
2
- import type { Lazy, Procedure, Router } from '@orpc/server';
3
- import type { Promisable } from '@orpc/shared';
4
- import { type ProcedureClient } from './procedure';
5
- export type RouterClientWithContractRouter<TRouter extends ContractRouter> = {
6
- [K in keyof TRouter]: TRouter[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? ProcedureClient<UInputSchema, UOutputSchema, SchemaOutput<UOutputSchema>> : TRouter[K] extends ContractRouter ? RouterClientWithContractRouter<TRouter[K]> : never;
7
- };
8
- export type RouterClientWithRouter<TRouter extends Router<any>> = {
9
- [K in keyof TRouter]: TRouter[K] extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> | Lazy<Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>> ? ProcedureClient<UInputSchema, UOutputSchema, UFuncOutput> : TRouter[K] extends Router<any> ? RouterClientWithRouter<TRouter[K]> : never;
10
- };
11
- export interface CreateRouterClientOptions {
12
- /**
13
- * The base url of the server.
14
- */
15
- baseURL: string;
16
- /**
17
- * The fetch function used to make the request.
18
- * @default global fetch
19
- */
20
- fetch?: typeof fetch;
21
- /**
22
- * The headers used to make the request.
23
- * Invoked before the request is made.
24
- */
25
- headers?: (input: unknown) => Promisable<Headers | Record<string, string>>;
26
- /**
27
- * This used for internal purpose only.
28
- *
29
- * @internal
30
- */
31
- path?: string[];
32
- }
33
- export declare function createRouterClient<TRouter extends Router<any> | ContractRouter>(options: CreateRouterClientOptions): TRouter extends Router<any> ? RouterClientWithRouter<TRouter> : TRouter extends ContractRouter ? RouterClientWithContractRouter<TRouter> : never;
34
- //# sourceMappingURL=router.d.ts.map