@orpc/client 0.0.0-next.d42488d → 0.0.0-next.d53d856

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.
@@ -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.d42488d",
4
+ "version": "0.0.0-next.d53d856",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,32 +15,50 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
21
  },
22
- "./🔒/*": {
23
- "types": "./dist/src/*.d.ts"
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.d42488d"
33
- },
34
51
  "dependencies": {
35
- "@orpc/shared": "0.0.0-next.d42488d",
36
- "@orpc/server": "0.0.0-next.d42488d"
52
+ "@orpc/standard-server": "0.0.0-next.d53d856",
53
+ "@orpc/shared": "0.0.0-next.d53d856",
54
+ "@orpc/standard-server-peer": "0.0.0-next.d53d856",
55
+ "@orpc/standard-server-fetch": "0.0.0-next.d53d856"
37
56
  },
38
57
  "devDependencies": {
39
- "zod": "^3.24.1",
40
- "@orpc/openapi": "0.0.0-next.d42488d"
58
+ "zod": "^4.0.17"
41
59
  },
42
60
  "scripts": {
43
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
61
+ "build": "unbuild",
44
62
  "build:watch": "pnpm run build --watch",
45
63
  "type:check": "tsc -b"
46
64
  }
package/dist/index.js DELETED
@@ -1,78 +0,0 @@
1
- // src/procedure-fetch-client.ts
2
- import { ORPCPayloadCodec } from "@orpc/server/fetch";
3
- import { ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim } from "@orpc/shared";
4
- import { ORPCError } from "@orpc/shared/error";
5
- var payloadCodec = new ORPCPayloadCodec();
6
- function createProcedureFetchClient(options) {
7
- const client = async (...[input, callerOptions]) => {
8
- const fetchClient = options.fetch ?? fetch;
9
- const url = `${trim(options.baseURL, "/")}/${options.path.map(encodeURIComponent).join("/")}`;
10
- const encoded = payloadCodec.encode(input);
11
- const headers = new Headers(encoded.headers);
12
- headers.append(ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE);
13
- let customHeaders = await options.headers?.(input);
14
- customHeaders = customHeaders instanceof Headers ? customHeaders : new Headers(customHeaders);
15
- for (const [key, value] of customHeaders.entries()) {
16
- headers.append(key, value);
17
- }
18
- const response = await fetchClient(url, {
19
- method: "POST",
20
- headers,
21
- body: encoded.body,
22
- signal: callerOptions?.signal
23
- });
24
- const json = await (async () => {
25
- try {
26
- return await payloadCodec.decode(response);
27
- } catch (e) {
28
- throw new ORPCError({
29
- code: "INTERNAL_SERVER_ERROR",
30
- message: "Cannot parse response.",
31
- cause: e
32
- });
33
- }
34
- })();
35
- if (!response.ok) {
36
- throw ORPCError.fromJSON(json) ?? new ORPCError({
37
- status: response.status,
38
- code: "INTERNAL_SERVER_ERROR",
39
- message: "Internal server error"
40
- });
41
- }
42
- return json;
43
- };
44
- return client;
45
- }
46
-
47
- // src/router-fetch-client.ts
48
- function createRouterFetchClient(options) {
49
- const path = options?.path ?? [];
50
- const client = new Proxy(
51
- createProcedureFetchClient({
52
- ...options,
53
- path
54
- }),
55
- {
56
- get(target, key) {
57
- if (typeof key !== "string") {
58
- return Reflect.get(target, key);
59
- }
60
- return createRouterFetchClient({
61
- ...options,
62
- path: [...path, key]
63
- });
64
- }
65
- }
66
- );
67
- return client;
68
- }
69
-
70
- // src/index.ts
71
- export * from "@orpc/shared/error";
72
- var createORPCFetchClient = createRouterFetchClient;
73
- export {
74
- createORPCFetchClient,
75
- createProcedureFetchClient,
76
- createRouterFetchClient
77
- };
78
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- /** unnoq */
2
- import { createRouterFetchClient } from './router-fetch-client';
3
- export * from './procedure-fetch-client';
4
- export * from './router-fetch-client';
5
- export * from '@orpc/shared/error';
6
- export declare const createORPCFetchClient: typeof createRouterFetchClient;
7
- //# sourceMappingURL=index.d.ts.map
@@ -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