@orpc/client 0.34.0 → 0.35.1
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 +55 -39
- package/dist/src/adapters/fetch/orpc-link.d.ts +5 -5
- package/package.json +6 -5
package/dist/fetch.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
// src/adapters/fetch/orpc-link.ts
|
2
2
|
import { ORPCError } from "@orpc/contract";
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import { fetchReToStandardBody } from "@orpc/server/fetch";
|
4
|
+
import { RPCSerializer } from "@orpc/server/standard";
|
5
|
+
import { isPlainObject, trim } from "@orpc/shared";
|
6
|
+
import cd from "content-disposition";
|
5
7
|
var RPCLink = class {
|
6
8
|
fetch;
|
7
|
-
|
9
|
+
rpcSerializer;
|
8
10
|
maxURLLength;
|
9
11
|
fallbackMethod;
|
10
12
|
getMethod;
|
@@ -12,7 +14,7 @@ var RPCLink = class {
|
|
12
14
|
url;
|
13
15
|
constructor(options) {
|
14
16
|
this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
15
|
-
this.
|
17
|
+
this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
|
16
18
|
this.maxURLLength = options.maxURLLength ?? 2083;
|
17
19
|
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
18
20
|
this.url = options.url;
|
@@ -26,59 +28,73 @@ var RPCLink = class {
|
|
26
28
|
async call(path, input, options) {
|
27
29
|
const clientContext = options.context;
|
28
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", cd(encoded.body instanceof File ? encoded.body.name : "blob"));
|
33
|
+
}
|
29
34
|
const response = await this.fetch(encoded.url, {
|
30
35
|
method: encoded.method,
|
31
36
|
headers: encoded.headers,
|
32
37
|
body: encoded.body,
|
33
38
|
signal: options.signal
|
34
39
|
}, clientContext);
|
35
|
-
const
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
+
});
|
39
54
|
}
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
cause: decoded
|
44
|
-
});
|
55
|
+
})();
|
56
|
+
if (response.ok) {
|
57
|
+
return deserialized;
|
45
58
|
}
|
46
|
-
|
59
|
+
throw ORPCError.fromJSON(deserialized);
|
47
60
|
}
|
48
61
|
async encode(path, input, options) {
|
49
62
|
const clientContext = options.context;
|
50
63
|
const expectMethod = await this.getMethod(path, input, clientContext);
|
51
|
-
const
|
52
|
-
const
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
const
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
continue;
|
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" && isPlainObject(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
|
+
};
|
66
78
|
}
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
79
|
+
}
|
80
|
+
const method = expectMethod === "GET" ? this.fallbackMethod : expectMethod;
|
81
|
+
if (isPlainObject(serialized)) {
|
82
|
+
if (!headers.has("content-type")) {
|
83
|
+
headers.set("content-type", "application/json");
|
71
84
|
}
|
72
85
|
return {
|
73
|
-
|
86
|
+
body: JSON.stringify(serialized),
|
87
|
+
method,
|
74
88
|
headers,
|
75
|
-
|
76
|
-
body: encoded.body
|
89
|
+
url
|
77
90
|
};
|
78
91
|
}
|
79
|
-
|
80
|
-
|
81
|
-
|
92
|
+
return {
|
93
|
+
body: serialized,
|
94
|
+
method,
|
95
|
+
headers,
|
96
|
+
url
|
97
|
+
};
|
82
98
|
}
|
83
99
|
};
|
84
100
|
export {
|
@@ -2,7 +2,7 @@ import type { 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 {
|
5
|
+
import { RPCSerializer } from '@orpc/server/standard';
|
6
6
|
export interface RPCLinkOptions<TClientContext> {
|
7
7
|
/**
|
8
8
|
* Base url for all requests.
|
@@ -22,18 +22,18 @@ export interface RPCLinkOptions<TClientContext> {
|
|
22
22
|
method?(path: readonly string[], input: unknown, context: TClientContext): Promisable<HTTPMethod | undefined>;
|
23
23
|
/**
|
24
24
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
25
|
-
*
|
25
|
+
* GET is not allowed, it's very dangerous.
|
26
26
|
*
|
27
27
|
* @default 'POST'
|
28
28
|
*/
|
29
|
-
fallbackMethod?: HTTPMethod
|
29
|
+
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
30
30
|
headers?(path: readonly string[], input: unknown, context: TClientContext): Promisable<Headers | Record<string, string>>;
|
31
31
|
fetch?: FetchWithContext<TClientContext>;
|
32
|
-
|
32
|
+
rpcSerializer?: RPCSerializer;
|
33
33
|
}
|
34
34
|
export declare class RPCLink<TClientContext> implements ClientLink<TClientContext> {
|
35
35
|
private readonly fetch;
|
36
|
-
private readonly
|
36
|
+
private readonly rpcSerializer;
|
37
37
|
private readonly maxURLLength;
|
38
38
|
private readonly fallbackMethod;
|
39
39
|
private readonly getMethod;
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/client",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.35.1",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -34,13 +34,14 @@
|
|
34
34
|
"dist"
|
35
35
|
],
|
36
36
|
"dependencies": {
|
37
|
-
"
|
38
|
-
"@orpc/contract": "0.
|
39
|
-
"@orpc/
|
37
|
+
"content-disposition": "^0.5.4",
|
38
|
+
"@orpc/contract": "0.35.1",
|
39
|
+
"@orpc/server": "0.35.1",
|
40
|
+
"@orpc/shared": "0.35.1"
|
40
41
|
},
|
41
42
|
"devDependencies": {
|
42
43
|
"zod": "^3.24.1",
|
43
|
-
"@orpc/openapi": "0.
|
44
|
+
"@orpc/openapi": "0.35.1"
|
44
45
|
},
|
45
46
|
"scripts": {
|
46
47
|
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|