@orpc/client 0.0.0-next.97446ff → 0.0.0-next.989a435
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 +97 -0
- package/dist/index.js +33 -73
- package/dist/src/adapters/fetch/index.d.ts +3 -0
- package/dist/src/adapters/fetch/orpc-link.d.ts +46 -0
- package/dist/src/adapters/fetch/types.d.ts +5 -0
- package/dist/src/client.d.ts +11 -0
- package/dist/src/dynamic-link.d.ts +13 -0
- package/dist/src/index.d.ts +4 -5
- package/dist/src/types.d.ts +5 -0
- package/package.json +13 -9
- package/dist/src/procedure-fetch-client.d.ts +0 -24
- package/dist/src/router-fetch-client.d.ts +0 -6
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/adapters/fetch/orpc-link.ts
|
|
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 {
|
|
7
|
+
fetch;
|
|
8
|
+
rpcSerializer;
|
|
9
|
+
maxURLLength;
|
|
10
|
+
fallbackMethod;
|
|
11
|
+
getMethod;
|
|
12
|
+
getHeaders;
|
|
13
|
+
url;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
16
|
+
this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
|
|
17
|
+
this.maxURLLength = options.maxURLLength ?? 2083;
|
|
18
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
|
19
|
+
this.url = options.url;
|
|
20
|
+
this.getMethod = async (path, input, context) => {
|
|
21
|
+
return await options.method?.(path, input, context) ?? this.fallbackMethod;
|
|
22
|
+
};
|
|
23
|
+
this.getHeaders = async (path, input, context) => {
|
|
24
|
+
return new Headers(await options.headers?.(path, input, context));
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async call(path, input, options) {
|
|
28
|
+
const clientContext = options.context ?? {};
|
|
29
|
+
const encoded = await this.encode(path, input, options);
|
|
30
|
+
const fetchBody = toFetchBody(encoded.body, encoded.headers);
|
|
31
|
+
const response = await this.fetch(encoded.url, {
|
|
32
|
+
method: encoded.method,
|
|
33
|
+
headers: encoded.headers,
|
|
34
|
+
body: fetchBody,
|
|
35
|
+
signal: options.signal
|
|
36
|
+
}, clientContext);
|
|
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;
|
|
55
|
+
}
|
|
56
|
+
throw ORPCError.fromJSON(deserialized);
|
|
57
|
+
}
|
|
58
|
+
async encode(path, input, options) {
|
|
59
|
+
const clientContext = options.context;
|
|
60
|
+
const expectMethod = await this.getMethod(path, input, clientContext);
|
|
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
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const method = expectMethod === "GET" ? this.fallbackMethod : expectMethod;
|
|
78
|
+
if (input === void 0) {
|
|
79
|
+
return {
|
|
80
|
+
body: void 0,
|
|
81
|
+
method,
|
|
82
|
+
headers,
|
|
83
|
+
url
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
body: serialized,
|
|
88
|
+
method,
|
|
89
|
+
headers,
|
|
90
|
+
url
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
export {
|
|
95
|
+
RPCLink
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=fetch.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,83 +1,43 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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);
|
|
22
|
-
}
|
|
23
|
-
const response = await fetchClient(url, {
|
|
24
|
-
method: "POST",
|
|
25
|
-
headers,
|
|
26
|
-
body: serialized.body,
|
|
27
|
-
signal: callerOptions?.signal
|
|
28
|
-
});
|
|
29
|
-
const json = await (async () => {
|
|
30
|
-
try {
|
|
31
|
-
return await deserializer.deserialize(response);
|
|
32
|
-
} catch (e) {
|
|
33
|
-
throw new ORPCError({
|
|
34
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
35
|
-
message: "Cannot parse response.",
|
|
36
|
-
cause: e
|
|
37
|
-
});
|
|
1
|
+
// src/client.ts
|
|
2
|
+
function createORPCClient(link, options) {
|
|
3
|
+
const path = options?.path ?? [];
|
|
4
|
+
const procedureClient = async (...[input, options2]) => {
|
|
5
|
+
return await link.call(path, input, options2 ?? {});
|
|
6
|
+
};
|
|
7
|
+
const recursive = new Proxy(procedureClient, {
|
|
8
|
+
get(target, key) {
|
|
9
|
+
if (typeof key !== "string") {
|
|
10
|
+
return Reflect.get(target, key);
|
|
38
11
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
status: response.status,
|
|
43
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
44
|
-
message: "Internal server error"
|
|
12
|
+
return createORPCClient(link, {
|
|
13
|
+
...options,
|
|
14
|
+
path: [...path, key]
|
|
45
15
|
});
|
|
46
16
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return client;
|
|
17
|
+
});
|
|
18
|
+
return recursive;
|
|
50
19
|
}
|
|
51
20
|
|
|
52
|
-
// src/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
return createRouterFetchClient({
|
|
66
|
-
...options,
|
|
67
|
-
path: [...path, key]
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
return client;
|
|
73
|
-
}
|
|
21
|
+
// src/dynamic-link.ts
|
|
22
|
+
var DynamicLink = class {
|
|
23
|
+
constructor(linkResolver) {
|
|
24
|
+
this.linkResolver = linkResolver;
|
|
25
|
+
}
|
|
26
|
+
async 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 });
|
|
30
|
+
return output;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
74
33
|
|
|
75
34
|
// src/index.ts
|
|
76
|
-
|
|
77
|
-
var createORPCFetchClient = createRouterFetchClient;
|
|
35
|
+
import { isDefinedError, ORPCError, safe } from "@orpc/contract";
|
|
78
36
|
export {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
37
|
+
DynamicLink,
|
|
38
|
+
ORPCError,
|
|
39
|
+
createORPCClient,
|
|
40
|
+
isDefinedError,
|
|
41
|
+
safe
|
|
82
42
|
};
|
|
83
43
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ClientContext, ClientOptions, HTTPMethod } from '@orpc/contract';
|
|
2
|
+
import type { Promisable } from '@orpc/shared';
|
|
3
|
+
import type { ClientLink } from '../../types';
|
|
4
|
+
import type { FetchWithContext } from './types';
|
|
5
|
+
import { RPCSerializer } from '@orpc/server/standard';
|
|
6
|
+
export interface RPCLinkOptions<TClientContext extends ClientContext> {
|
|
7
|
+
/**
|
|
8
|
+
* Base url for all requests.
|
|
9
|
+
*/
|
|
10
|
+
url: 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>>;
|
|
31
|
+
fetch?: FetchWithContext<TClientContext>;
|
|
32
|
+
rpcSerializer?: RPCSerializer;
|
|
33
|
+
}
|
|
34
|
+
export declare class RPCLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
|
|
35
|
+
private readonly fetch;
|
|
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;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=orpc-link.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
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>;
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AnyContractRouter, ClientContext, ContractRouterClient } from '@orpc/contract';
|
|
2
|
+
import type { AnyRouter, RouterClient } from '@orpc/server';
|
|
3
|
+
import type { ClientLink } from './types';
|
|
4
|
+
export interface createORPCClientOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Use as base path for all procedure, useful when you only want to call a subset of the procedure.
|
|
7
|
+
*/
|
|
8
|
+
path?: string[];
|
|
9
|
+
}
|
|
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
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ClientContext, ClientOptions } from '@orpc/contract';
|
|
2
|
+
import type { Promisable } from '@orpc/shared';
|
|
3
|
+
import type { ClientLink } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
|
|
6
|
+
* based on the request path, input, and context.
|
|
7
|
+
*/
|
|
8
|
+
export declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
|
|
9
|
+
private readonly linkResolver;
|
|
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>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=dynamic-link.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/** unnoq */
|
|
2
|
-
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export
|
|
6
|
-
export declare const createORPCFetchClient: typeof createRouterFetchClient;
|
|
2
|
+
export * from './client';
|
|
3
|
+
export * from './dynamic-link';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export { isDefinedError, ORPCError, safe } from '@orpc/contract';
|
|
7
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
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
|
+
}
|
|
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.
|
|
4
|
+
"version": "0.0.0-next.989a435",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"import": "./dist/index.js",
|
|
20
20
|
"default": "./dist/index.js"
|
|
21
21
|
},
|
|
22
|
+
"./fetch": {
|
|
23
|
+
"types": "./dist/src/adapters/fetch/index.d.ts",
|
|
24
|
+
"import": "./dist/fetch.js",
|
|
25
|
+
"default": "./dist/fetch.js"
|
|
26
|
+
},
|
|
22
27
|
"./🔒/*": {
|
|
23
28
|
"types": "./dist/src/*.d.ts"
|
|
24
29
|
}
|
|
@@ -28,20 +33,19 @@
|
|
|
28
33
|
"!**/*.tsbuildinfo",
|
|
29
34
|
"dist"
|
|
30
35
|
],
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"@orpc/contract": "0.0.0-next.97446ff",
|
|
33
|
-
"@orpc/server": "0.0.0-next.97446ff"
|
|
34
|
-
},
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@orpc/
|
|
37
|
-
"@orpc/
|
|
37
|
+
"@orpc/server-standard": "^0.0.0",
|
|
38
|
+
"@orpc/server-standard-fetch": "^0.0.0",
|
|
39
|
+
"@orpc/contract": "0.0.0-next.989a435",
|
|
40
|
+
"@orpc/server": "0.0.0-next.989a435",
|
|
41
|
+
"@orpc/shared": "0.0.0-next.989a435"
|
|
38
42
|
},
|
|
39
43
|
"devDependencies": {
|
|
40
44
|
"zod": "^3.24.1",
|
|
41
|
-
"@orpc/openapi": "0.0.0-next.
|
|
45
|
+
"@orpc/openapi": "0.0.0-next.989a435"
|
|
42
46
|
},
|
|
43
47
|
"scripts": {
|
|
44
|
-
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
|
48
|
+
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
|
45
49
|
"build:watch": "pnpm run build --watch",
|
|
46
50
|
"type:check": "tsc -b"
|
|
47
51
|
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
|
@@ -1,6 +0,0 @@
|
|
|
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
|