@orpc/client 0.0.0-next.c59d67c → 0.0.0-next.cba521d
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/fetch.js +89 -0
- package/dist/index.js +31 -72
- 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 +4 -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 +12 -10
- package/dist/src/procedure.d.ts +0 -27
- package/dist/src/router.d.ts +0 -34
package/dist/fetch.js
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
// src/adapters/fetch/orpc-link.ts
|
2
|
+
import { ORPCError } from "@orpc/contract";
|
3
|
+
import { ORPCPayloadCodec } from "@orpc/server/fetch";
|
4
|
+
import { ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim } from "@orpc/shared";
|
5
|
+
var RPCLink = class {
|
6
|
+
fetch;
|
7
|
+
payloadCodec;
|
8
|
+
maxURLLength;
|
9
|
+
fallbackMethod;
|
10
|
+
getMethod;
|
11
|
+
getHeaders;
|
12
|
+
url;
|
13
|
+
constructor(options) {
|
14
|
+
this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
15
|
+
this.payloadCodec = options.payloadCodec ?? new ORPCPayloadCodec();
|
16
|
+
this.maxURLLength = options.maxURLLength ?? 2083;
|
17
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
18
|
+
this.url = options.url;
|
19
|
+
this.getMethod = async (path, input, context) => {
|
20
|
+
return await options.method?.(path, input, context) ?? this.fallbackMethod;
|
21
|
+
};
|
22
|
+
this.getHeaders = async (path, input, context) => {
|
23
|
+
return new Headers(await options.headers?.(path, input, context));
|
24
|
+
};
|
25
|
+
}
|
26
|
+
async call(path, input, options) {
|
27
|
+
const clientContext = options.context;
|
28
|
+
const encoded = await this.encode(path, input, options);
|
29
|
+
const response = await this.fetch(encoded.url, {
|
30
|
+
method: encoded.method,
|
31
|
+
headers: encoded.headers,
|
32
|
+
body: encoded.body,
|
33
|
+
signal: options.signal
|
34
|
+
}, clientContext);
|
35
|
+
const decoded = await this.payloadCodec.decode(response);
|
36
|
+
if (!response.ok) {
|
37
|
+
if (ORPCError.isValidJSON(decoded)) {
|
38
|
+
throw new ORPCError(decoded);
|
39
|
+
}
|
40
|
+
throw new ORPCError({
|
41
|
+
status: response.status,
|
42
|
+
code: "INTERNAL_SERVER_ERROR",
|
43
|
+
message: "Internal server error",
|
44
|
+
cause: decoded
|
45
|
+
});
|
46
|
+
}
|
47
|
+
return decoded;
|
48
|
+
}
|
49
|
+
async encode(path, input, options) {
|
50
|
+
const clientContext = options.context;
|
51
|
+
const expectMethod = await this.getMethod(path, input, clientContext);
|
52
|
+
const methods = /* @__PURE__ */ new Set([expectMethod, this.fallbackMethod]);
|
53
|
+
const baseHeaders = await this.getHeaders(path, input, clientContext);
|
54
|
+
const baseUrl = new URL(`${trim(this.url, "/")}/${path.map(encodeURIComponent).join("/")}`);
|
55
|
+
baseHeaders.append(ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE);
|
56
|
+
for (const method of methods) {
|
57
|
+
const url = new URL(baseUrl);
|
58
|
+
const headers = new Headers(baseHeaders);
|
59
|
+
const encoded = this.payloadCodec.encode(input, method, this.fallbackMethod);
|
60
|
+
if (encoded.query) {
|
61
|
+
for (const [key, value] of encoded.query.entries()) {
|
62
|
+
url.searchParams.append(key, value);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
if (url.toString().length > this.maxURLLength) {
|
66
|
+
continue;
|
67
|
+
}
|
68
|
+
if (encoded.headers) {
|
69
|
+
for (const [key, value] of encoded.headers.entries()) {
|
70
|
+
headers.append(key, value);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
return {
|
74
|
+
url,
|
75
|
+
headers,
|
76
|
+
method: encoded.method,
|
77
|
+
body: encoded.body
|
78
|
+
};
|
79
|
+
}
|
80
|
+
throw new ORPCError({
|
81
|
+
code: "BAD_REQUEST",
|
82
|
+
message: "Cannot encode the request, please check the url length or payload."
|
83
|
+
});
|
84
|
+
}
|
85
|
+
};
|
86
|
+
export {
|
87
|
+
RPCLink
|
88
|
+
};
|
89
|
+
//# sourceMappingURL=fetch.js.map
|
package/dist/index.js
CHANGED
@@ -1,83 +1,42 @@
|
|
1
|
-
// src/
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
const deserializer = new ORPCDeserializer();
|
12
|
-
const client = async (input) => {
|
13
|
-
const fetch_ = options.fetch ?? fetch;
|
14
|
-
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);
|
20
|
-
}
|
21
|
-
headers.set(ORPC_HEADER, ORPC_HEADER_VALUE);
|
22
|
-
const response = await fetch_(url, {
|
23
|
-
method: "POST",
|
24
|
-
headers,
|
25
|
-
body
|
26
|
-
});
|
27
|
-
const json = await (async () => {
|
28
|
-
try {
|
29
|
-
return await deserializer.deserialize(response);
|
30
|
-
} catch (e) {
|
31
|
-
throw new ORPCError({
|
32
|
-
code: "INTERNAL_SERVER_ERROR",
|
33
|
-
message: "Cannot parse response.",
|
34
|
-
cause: e
|
35
|
-
});
|
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);
|
36
11
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
status: response.status,
|
41
|
-
code: "INTERNAL_SERVER_ERROR",
|
42
|
-
message: "Internal server error"
|
12
|
+
return createORPCClient(link, {
|
13
|
+
...options,
|
14
|
+
path: [...path, key]
|
43
15
|
});
|
44
16
|
}
|
45
|
-
|
46
|
-
|
47
|
-
return client;
|
17
|
+
});
|
18
|
+
return recursive;
|
48
19
|
}
|
49
20
|
|
50
|
-
// src/
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
get(target, key) {
|
62
|
-
if (typeof key !== "string") {
|
63
|
-
return Reflect.get(target, key);
|
64
|
-
}
|
65
|
-
return createRouterClient({
|
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 resolvedLink = await this.linkResolver(path, input, options.context);
|
28
|
+
const output = await resolvedLink.call(path, input, options);
|
29
|
+
return output;
|
30
|
+
}
|
31
|
+
};
|
74
32
|
|
75
33
|
// src/index.ts
|
76
|
-
|
77
|
-
var createORPCClient = createRouterClient;
|
34
|
+
import { isDefinedError, ORPCError, safe } from "@orpc/contract";
|
78
35
|
export {
|
36
|
+
DynamicLink,
|
37
|
+
ORPCError,
|
79
38
|
createORPCClient,
|
80
|
-
|
81
|
-
|
39
|
+
isDefinedError,
|
40
|
+
safe
|
82
41
|
};
|
83
42
|
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import type { 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 { type PublicORPCPayloadCodec } from '@orpc/server/fetch';
|
6
|
+
export interface RPCLinkOptions<TClientContext> {
|
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
|
+
* Do not use GET as fallback method, it's very dangerous.
|
26
|
+
*
|
27
|
+
* @default 'POST'
|
28
|
+
*/
|
29
|
+
fallbackMethod?: HTTPMethod;
|
30
|
+
headers?: (path: readonly string[], input: unknown, context: TClientContext) => Promisable<Headers | Record<string, string>>;
|
31
|
+
fetch?: FetchWithContext<TClientContext>;
|
32
|
+
payloadCodec?: PublicORPCPayloadCodec;
|
33
|
+
}
|
34
|
+
export declare class RPCLink<TClientContext> implements ClientLink<TClientContext> {
|
35
|
+
private readonly fetch;
|
36
|
+
private readonly payloadCodec;
|
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,11 @@
|
|
1
|
+
import type { ContractRouter, ContractRouterClient } from '@orpc/contract';
|
2
|
+
import type { ANY_ROUTER, 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 ANY_ROUTER | ContractRouter, TClientContext = unknown>(link: ClientLink<TClientContext>, options?: createORPCClientOptions): TRouter extends ContractRouter ? ContractRouterClient<TRouter, TClientContext> : TRouter extends ANY_ROUTER ? RouterClient<TRouter, TClientContext> : never;
|
11
|
+
//# sourceMappingURL=client.d.ts.map
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { 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> 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 createORPCClient: typeof createRouterClient;
|
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
|
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.cba521d",
|
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,17 @@
|
|
28
33
|
"!**/*.tsbuildinfo",
|
29
34
|
"dist"
|
30
35
|
],
|
31
|
-
"peerDependencies": {
|
32
|
-
"@orpc/contract": "0.0.0-next.c59d67c",
|
33
|
-
"@orpc/server": "0.0.0-next.c59d67c"
|
34
|
-
},
|
35
36
|
"dependencies": {
|
36
|
-
"@orpc/
|
37
|
-
"@orpc/
|
37
|
+
"@orpc/contract": "0.0.0-next.cba521d",
|
38
|
+
"@orpc/server": "0.0.0-next.cba521d",
|
39
|
+
"@orpc/shared": "0.0.0-next.cba521d"
|
38
40
|
},
|
39
41
|
"devDependencies": {
|
40
|
-
"zod": "^3.
|
41
|
-
"@orpc/openapi": "0.0.0-next.
|
42
|
+
"zod": "^3.24.1",
|
43
|
+
"@orpc/openapi": "0.0.0-next.cba521d"
|
42
44
|
},
|
43
45
|
"scripts": {
|
44
|
-
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
46
|
+
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
45
47
|
"build:watch": "pnpm run build --watch",
|
46
48
|
"type:check": "tsc -b"
|
47
49
|
}
|
package/dist/src/procedure.d.ts
DELETED
@@ -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
|
package/dist/src/router.d.ts
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
import type { ContractProcedure, ContractRouter, SchemaOutput } from '@orpc/contract';
|
2
|
-
import type { 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> ? 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
|