@orpc/client 0.0.0-next.db1f26d → 0.0.0-next.e361acd

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -1,28 +1,30 @@
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 { ORPC_PROTOCOL_HEADER, ORPC_PROTOCOL_VALUE, trim } from "@orpc/shared";
7
3
  import { ORPCError } from "@orpc/shared/error";
8
4
  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 serializer = new ORPCSerializer();
6
+ var deserializer = new ORPCDeserializer();
7
+ function createProcedureFetchClient(options) {
8
+ const client = async (...[input, callerOptions]) => {
9
+ const fetchClient = options.fetch ?? fetch;
14
10
  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);
11
+ const headers = new Headers({
12
+ [ORPC_PROTOCOL_HEADER]: ORPC_PROTOCOL_VALUE
13
+ });
14
+ let customHeaders = await options.headers?.(input);
15
+ customHeaders = customHeaders instanceof Headers ? customHeaders : new Headers(customHeaders);
16
+ for (const [key, value] of customHeaders.entries()) {
17
+ headers.append(key, value);
18
+ }
19
+ const serialized = serializer.serialize(input);
20
+ for (const [key, value] of serialized.headers.entries()) {
21
+ headers.append(key, value);
20
22
  }
21
- headers.set(ORPC_HEADER, ORPC_HEADER_VALUE);
22
- const response = await fetch_(url, {
23
+ const response = await fetchClient(url, {
23
24
  method: "POST",
24
25
  headers,
25
- body
26
+ body: serialized.body,
27
+ signal: callerOptions?.signal
26
28
  });
27
29
  const json = await (async () => {
28
30
  try {
@@ -47,14 +49,12 @@ function createProcedureClient(options) {
47
49
  return client;
48
50
  }
49
51
 
50
- // src/router.ts
51
- function createRouterClient(options) {
52
+ // src/router-fetch-client.ts
53
+ function createRouterFetchClient(options) {
52
54
  const path = options?.path ?? [];
53
55
  const client = new Proxy(
54
- createProcedureClient({
55
- baseURL: options.baseURL,
56
- fetch: options.fetch,
57
- headers: options.headers,
56
+ createProcedureFetchClient({
57
+ ...options,
58
58
  path
59
59
  }),
60
60
  {
@@ -62,7 +62,7 @@ function createRouterClient(options) {
62
62
  if (typeof key !== "string") {
63
63
  return Reflect.get(target, key);
64
64
  }
65
- return createRouterClient({
65
+ return createRouterFetchClient({
66
66
  ...options,
67
67
  path: [...path, key]
68
68
  });
@@ -74,10 +74,10 @@ function createRouterClient(options) {
74
74
 
75
75
  // src/index.ts
76
76
  export * from "@orpc/shared/error";
77
- var createORPCClient = createRouterClient;
77
+ var createORPCFetchClient = createRouterFetchClient;
78
78
  export {
79
- createORPCClient,
80
- createProcedureClient,
81
- createRouterClient
79
+ createORPCFetchClient,
80
+ createProcedureFetchClient,
81
+ createRouterFetchClient
82
82
  };
83
83
  //# 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.db1f26d",
4
+ "version": "0.0.0-next.e361acd",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,16 +29,16 @@
29
29
  "dist"
30
30
  ],
31
31
  "peerDependencies": {
32
- "@orpc/contract": "0.0.0-next.db1f26d",
33
- "@orpc/server": "0.0.0-next.db1f26d"
32
+ "@orpc/contract": "0.0.0-next.e361acd",
33
+ "@orpc/server": "0.0.0-next.e361acd"
34
34
  },
35
35
  "dependencies": {
36
- "@orpc/shared": "0.0.0-next.db1f26d",
37
- "@orpc/transformer": "0.0.0-next.db1f26d"
36
+ "@orpc/transformer": "0.0.0-next.e361acd",
37
+ "@orpc/shared": "0.0.0-next.e361acd"
38
38
  },
39
39
  "devDependencies": {
40
- "zod": "^3.23.8",
41
- "@orpc/openapi": "0.0.0-next.db1f26d"
40
+ "zod": "^3.24.1",
41
+ "@orpc/openapi": "0.0.0-next.e361acd"
42
42
  },
43
43
  "scripts": {
44
44
  "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