@orpc/client 0.0.0-next.db1f26d → 0.0.0-next.dc23561
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/README.md +98 -0
- package/dist/adapters/fetch/index.d.mts +46 -0
- package/dist/adapters/fetch/index.d.ts +46 -0
- package/dist/adapters/fetch/index.mjs +45 -0
- package/dist/adapters/message-port/index.d.mts +59 -0
- package/dist/adapters/message-port/index.d.ts +59 -0
- package/dist/adapters/message-port/index.mjs +71 -0
- package/dist/adapters/standard/index.d.mts +10 -0
- package/dist/adapters/standard/index.d.ts +10 -0
- package/dist/adapters/standard/index.mjs +4 -0
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +45 -0
- package/dist/index.d.mts +185 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.mjs +82 -0
- package/dist/plugins/index.d.mts +202 -0
- package/dist/plugins/index.d.ts +202 -0
- package/dist/plugins/index.mjs +400 -0
- package/dist/shared/client.BG98rYdO.d.ts +45 -0
- package/dist/shared/client.BOYsZIRq.d.mts +29 -0
- package/dist/shared/client.BOYsZIRq.d.ts +29 -0
- package/dist/shared/client.Bwgm6dgk.d.mts +45 -0
- package/dist/shared/client.C176log5.d.ts +91 -0
- package/dist/shared/client.DKmRtVO2.mjs +390 -0
- package/dist/shared/client.Ycwr4Tuo.d.mts +91 -0
- package/dist/shared/client.txdq_i5V.mjs +180 -0
- package/package.json +34 -17
- package/dist/index.js +0 -83
- package/dist/src/index.d.ts +0 -7
- package/dist/src/procedure.d.ts +0 -27
- package/dist/src/router.d.ts +0 -34
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
|
|
2
|
+
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
|
3
|
+
|
|
4
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
|
5
|
+
BAD_REQUEST: {
|
|
6
|
+
status: 400,
|
|
7
|
+
message: "Bad Request"
|
|
8
|
+
},
|
|
9
|
+
UNAUTHORIZED: {
|
|
10
|
+
status: 401,
|
|
11
|
+
message: "Unauthorized"
|
|
12
|
+
},
|
|
13
|
+
FORBIDDEN: {
|
|
14
|
+
status: 403,
|
|
15
|
+
message: "Forbidden"
|
|
16
|
+
},
|
|
17
|
+
NOT_FOUND: {
|
|
18
|
+
status: 404,
|
|
19
|
+
message: "Not Found"
|
|
20
|
+
},
|
|
21
|
+
METHOD_NOT_SUPPORTED: {
|
|
22
|
+
status: 405,
|
|
23
|
+
message: "Method Not Supported"
|
|
24
|
+
},
|
|
25
|
+
NOT_ACCEPTABLE: {
|
|
26
|
+
status: 406,
|
|
27
|
+
message: "Not Acceptable"
|
|
28
|
+
},
|
|
29
|
+
TIMEOUT: {
|
|
30
|
+
status: 408,
|
|
31
|
+
message: "Request Timeout"
|
|
32
|
+
},
|
|
33
|
+
CONFLICT: {
|
|
34
|
+
status: 409,
|
|
35
|
+
message: "Conflict"
|
|
36
|
+
},
|
|
37
|
+
PRECONDITION_FAILED: {
|
|
38
|
+
status: 412,
|
|
39
|
+
message: "Precondition Failed"
|
|
40
|
+
},
|
|
41
|
+
PAYLOAD_TOO_LARGE: {
|
|
42
|
+
status: 413,
|
|
43
|
+
message: "Payload Too Large"
|
|
44
|
+
},
|
|
45
|
+
UNSUPPORTED_MEDIA_TYPE: {
|
|
46
|
+
status: 415,
|
|
47
|
+
message: "Unsupported Media Type"
|
|
48
|
+
},
|
|
49
|
+
UNPROCESSABLE_CONTENT: {
|
|
50
|
+
status: 422,
|
|
51
|
+
message: "Unprocessable Content"
|
|
52
|
+
},
|
|
53
|
+
TOO_MANY_REQUESTS: {
|
|
54
|
+
status: 429,
|
|
55
|
+
message: "Too Many Requests"
|
|
56
|
+
},
|
|
57
|
+
CLIENT_CLOSED_REQUEST: {
|
|
58
|
+
status: 499,
|
|
59
|
+
message: "Client Closed Request"
|
|
60
|
+
},
|
|
61
|
+
INTERNAL_SERVER_ERROR: {
|
|
62
|
+
status: 500,
|
|
63
|
+
message: "Internal Server Error"
|
|
64
|
+
},
|
|
65
|
+
NOT_IMPLEMENTED: {
|
|
66
|
+
status: 501,
|
|
67
|
+
message: "Not Implemented"
|
|
68
|
+
},
|
|
69
|
+
BAD_GATEWAY: {
|
|
70
|
+
status: 502,
|
|
71
|
+
message: "Bad Gateway"
|
|
72
|
+
},
|
|
73
|
+
SERVICE_UNAVAILABLE: {
|
|
74
|
+
status: 503,
|
|
75
|
+
message: "Service Unavailable"
|
|
76
|
+
},
|
|
77
|
+
GATEWAY_TIMEOUT: {
|
|
78
|
+
status: 504,
|
|
79
|
+
message: "Gateway Timeout"
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
function fallbackORPCErrorStatus(code, status) {
|
|
83
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
84
|
+
}
|
|
85
|
+
function fallbackORPCErrorMessage(code, message) {
|
|
86
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
87
|
+
}
|
|
88
|
+
class ORPCError extends Error {
|
|
89
|
+
defined;
|
|
90
|
+
code;
|
|
91
|
+
status;
|
|
92
|
+
data;
|
|
93
|
+
constructor(code, ...rest) {
|
|
94
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
95
|
+
if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
|
|
96
|
+
throw new Error("[ORPCError] Invalid error status code.");
|
|
97
|
+
}
|
|
98
|
+
const message = fallbackORPCErrorMessage(code, options.message);
|
|
99
|
+
super(message, options);
|
|
100
|
+
this.code = code;
|
|
101
|
+
this.status = fallbackORPCErrorStatus(code, options.status);
|
|
102
|
+
this.defined = options.defined ?? false;
|
|
103
|
+
this.data = options.data;
|
|
104
|
+
}
|
|
105
|
+
toJSON() {
|
|
106
|
+
return {
|
|
107
|
+
defined: this.defined,
|
|
108
|
+
code: this.code,
|
|
109
|
+
status: this.status,
|
|
110
|
+
message: this.message,
|
|
111
|
+
data: this.data
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function isDefinedError(error) {
|
|
116
|
+
return error instanceof ORPCError && error.defined;
|
|
117
|
+
}
|
|
118
|
+
function toORPCError(error) {
|
|
119
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
120
|
+
message: "Internal server error",
|
|
121
|
+
cause: error
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function isORPCErrorStatus(status) {
|
|
125
|
+
return status < 200 || status >= 400;
|
|
126
|
+
}
|
|
127
|
+
function isORPCErrorJson(json) {
|
|
128
|
+
if (!isObject(json)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
132
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
|
|
136
|
+
}
|
|
137
|
+
function createORPCErrorFromJson(json, options = {}) {
|
|
138
|
+
return new ORPCError(json.code, {
|
|
139
|
+
...options,
|
|
140
|
+
...json
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function mapEventIterator(iterator, maps) {
|
|
145
|
+
const mapError = async (error) => {
|
|
146
|
+
let mappedError = await maps.error(error);
|
|
147
|
+
if (mappedError !== error) {
|
|
148
|
+
const meta = getEventMeta(error);
|
|
149
|
+
if (meta && isTypescriptObject(mappedError)) {
|
|
150
|
+
mappedError = withEventMeta(mappedError, meta);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return mappedError;
|
|
154
|
+
};
|
|
155
|
+
return new AsyncIteratorClass(async () => {
|
|
156
|
+
const { done, value } = await (async () => {
|
|
157
|
+
try {
|
|
158
|
+
return await iterator.next();
|
|
159
|
+
} catch (error) {
|
|
160
|
+
throw await mapError(error);
|
|
161
|
+
}
|
|
162
|
+
})();
|
|
163
|
+
let mappedValue = await maps.value(value, done);
|
|
164
|
+
if (mappedValue !== value) {
|
|
165
|
+
const meta = getEventMeta(value);
|
|
166
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
|
167
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { done, value: mappedValue };
|
|
171
|
+
}, async () => {
|
|
172
|
+
try {
|
|
173
|
+
await iterator.return?.();
|
|
174
|
+
} catch (error) {
|
|
175
|
+
throw await mapError(error);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, isORPCErrorJson as c, createORPCErrorFromJson as d, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|
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.dc23561",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,33 +15,50 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
21
|
},
|
|
22
|
-
"
|
|
23
|
-
"types": "./dist/
|
|
22
|
+
"./plugins": {
|
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./standard": {
|
|
28
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
|
29
|
+
"import": "./dist/adapters/standard/index.mjs",
|
|
30
|
+
"default": "./dist/adapters/standard/index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./fetch": {
|
|
33
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
|
34
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
|
35
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"./websocket": {
|
|
38
|
+
"types": "./dist/adapters/websocket/index.d.mts",
|
|
39
|
+
"import": "./dist/adapters/websocket/index.mjs",
|
|
40
|
+
"default": "./dist/adapters/websocket/index.mjs"
|
|
41
|
+
},
|
|
42
|
+
"./message-port": {
|
|
43
|
+
"types": "./dist/adapters/message-port/index.d.mts",
|
|
44
|
+
"import": "./dist/adapters/message-port/index.mjs",
|
|
45
|
+
"default": "./dist/adapters/message-port/index.mjs"
|
|
24
46
|
}
|
|
25
47
|
},
|
|
26
48
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
49
|
"dist"
|
|
30
50
|
],
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"@orpc/contract": "0.0.0-next.db1f26d",
|
|
33
|
-
"@orpc/server": "0.0.0-next.db1f26d"
|
|
34
|
-
},
|
|
35
51
|
"dependencies": {
|
|
36
|
-
"@orpc/shared": "0.0.0-next.
|
|
37
|
-
"@orpc/
|
|
52
|
+
"@orpc/shared": "0.0.0-next.dc23561",
|
|
53
|
+
"@orpc/standard-server": "0.0.0-next.dc23561",
|
|
54
|
+
"@orpc/standard-server-fetch": "0.0.0-next.dc23561",
|
|
55
|
+
"@orpc/standard-server-peer": "0.0.0-next.dc23561"
|
|
38
56
|
},
|
|
39
57
|
"devDependencies": {
|
|
40
|
-
"zod": "^
|
|
41
|
-
"@orpc/openapi": "0.0.0-next.db1f26d"
|
|
58
|
+
"zod": "^4.1.1"
|
|
42
59
|
},
|
|
43
60
|
"scripts": {
|
|
44
|
-
"build": "
|
|
61
|
+
"build": "unbuild",
|
|
45
62
|
"build:watch": "pnpm run build --watch",
|
|
46
63
|
"type:check": "tsc -b"
|
|
47
64
|
}
|
package/dist/index.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
// src/procedure.ts
|
|
2
|
-
import {
|
|
3
|
-
ORPC_HEADER,
|
|
4
|
-
ORPC_HEADER_VALUE
|
|
5
|
-
} from "@orpc/contract";
|
|
6
|
-
import { trim } from "@orpc/shared";
|
|
7
|
-
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;
|
|
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
|
-
});
|
|
36
|
-
}
|
|
37
|
-
})();
|
|
38
|
-
if (!response.ok) {
|
|
39
|
-
throw ORPCError.fromJSON(json) ?? new ORPCError({
|
|
40
|
-
status: response.status,
|
|
41
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
42
|
-
message: "Internal server error"
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return json;
|
|
46
|
-
};
|
|
47
|
-
return client;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/router.ts
|
|
51
|
-
function createRouterClient(options) {
|
|
52
|
-
const path = options?.path ?? [];
|
|
53
|
-
const client = new Proxy(
|
|
54
|
-
createProcedureClient({
|
|
55
|
-
baseURL: options.baseURL,
|
|
56
|
-
fetch: options.fetch,
|
|
57
|
-
headers: options.headers,
|
|
58
|
-
path
|
|
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
|
-
}
|
|
74
|
-
|
|
75
|
-
// src/index.ts
|
|
76
|
-
export * from "@orpc/shared/error";
|
|
77
|
-
var createORPCClient = createRouterClient;
|
|
78
|
-
export {
|
|
79
|
-
createORPCClient,
|
|
80
|
-
createProcedureClient,
|
|
81
|
-
createRouterClient
|
|
82
|
-
};
|
|
83
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/index.d.ts
DELETED
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 { 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
|