@orpc/shared 0.27.0 → 0.28.0
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/index.js +30 -12
- package/dist/src/error.d.ts +1 -64
- package/dist/src/index.d.ts +2 -1
- package/package.json +2 -7
- package/dist/chunk-CCTAECMC.js +0 -88
- package/dist/error.js +0 -11
package/dist/index.js
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
convertToStandardError
|
|
3
|
-
} from "./chunk-CCTAECMC.js";
|
|
4
|
-
|
|
5
1
|
// src/constants.ts
|
|
6
2
|
var ORPC_HANDLER_HEADER = "x-orpc-handler";
|
|
7
3
|
var ORPC_HANDLER_VALUE = "orpc";
|
|
8
4
|
|
|
5
|
+
// src/error.ts
|
|
6
|
+
import { isPlainObject } from "is-what";
|
|
7
|
+
function toError(error) {
|
|
8
|
+
if (error instanceof Error) {
|
|
9
|
+
return error;
|
|
10
|
+
}
|
|
11
|
+
if (typeof error === "string") {
|
|
12
|
+
return new Error(error, { cause: error });
|
|
13
|
+
}
|
|
14
|
+
if (isPlainObject(error)) {
|
|
15
|
+
if ("message" in error && typeof error.message === "string") {
|
|
16
|
+
return new Error(error.message, { cause: error });
|
|
17
|
+
}
|
|
18
|
+
if ("name" in error && typeof error.name === "string") {
|
|
19
|
+
return new Error(error.name, { cause: error });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return new Error("Unknown error", { cause: error });
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
// src/hook.ts
|
|
10
26
|
async function executeWithHooks(options) {
|
|
11
27
|
const interceptors = convertToArray(options.hooks?.interceptor);
|
|
@@ -34,12 +50,12 @@ async function executeWithHooks(options) {
|
|
|
34
50
|
await onSuccesses[i](state, options.context, options.meta);
|
|
35
51
|
}
|
|
36
52
|
} catch (e) {
|
|
37
|
-
state = { status: "error", input: options.input, error:
|
|
53
|
+
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
38
54
|
for (let i = onErrors.length - 1; i >= 0; i--) {
|
|
39
55
|
try {
|
|
40
56
|
await onErrors[i](state, options.context, options.meta);
|
|
41
57
|
} catch (e2) {
|
|
42
|
-
state = { status: "error", input: options.input, error:
|
|
58
|
+
state = { status: "error", input: options.input, error: toError(e2), output: void 0 };
|
|
43
59
|
}
|
|
44
60
|
}
|
|
45
61
|
}
|
|
@@ -47,7 +63,7 @@ async function executeWithHooks(options) {
|
|
|
47
63
|
try {
|
|
48
64
|
await onFinishes[i](state, options.context, options.meta);
|
|
49
65
|
} catch (e) {
|
|
50
|
-
state = { status: "error", input: options.input, error:
|
|
66
|
+
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
51
67
|
}
|
|
52
68
|
}
|
|
53
69
|
if (state.status === "error") {
|
|
@@ -76,7 +92,7 @@ function parseJSONSafely(text) {
|
|
|
76
92
|
}
|
|
77
93
|
|
|
78
94
|
// src/object.ts
|
|
79
|
-
import { isPlainObject } from "is-what";
|
|
95
|
+
import { isPlainObject as isPlainObject2 } from "is-what";
|
|
80
96
|
function set(root, segments, value2) {
|
|
81
97
|
const ref = { root };
|
|
82
98
|
let currentRef = ref;
|
|
@@ -112,7 +128,7 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
112
128
|
payload.forEach((v, i) => {
|
|
113
129
|
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
114
130
|
});
|
|
115
|
-
} else if (
|
|
131
|
+
} else if (isPlainObject2(payload)) {
|
|
116
132
|
for (const key in payload) {
|
|
117
133
|
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
118
134
|
}
|
|
@@ -157,8 +173,8 @@ function value(value2, ...args) {
|
|
|
157
173
|
}
|
|
158
174
|
|
|
159
175
|
// src/index.ts
|
|
160
|
-
import { isPlainObject as
|
|
161
|
-
import { guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
176
|
+
import { isPlainObject as isPlainObject3 } from "is-what";
|
|
177
|
+
import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
162
178
|
export {
|
|
163
179
|
ORPC_HANDLER_HEADER,
|
|
164
180
|
ORPC_HANDLER_VALUE,
|
|
@@ -167,13 +183,15 @@ export {
|
|
|
167
183
|
executeWithHooks,
|
|
168
184
|
findDeepMatches,
|
|
169
185
|
get,
|
|
186
|
+
group,
|
|
170
187
|
guard,
|
|
171
|
-
|
|
188
|
+
isPlainObject3 as isPlainObject,
|
|
172
189
|
mapEntries,
|
|
173
190
|
mapValues,
|
|
174
191
|
omit,
|
|
175
192
|
parseJSONSafely,
|
|
176
193
|
set,
|
|
194
|
+
toError,
|
|
177
195
|
trim,
|
|
178
196
|
value
|
|
179
197
|
};
|
package/dist/src/error.d.ts
CHANGED
|
@@ -1,65 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const ORPC_ERROR_CODE_STATUSES: {
|
|
3
|
-
readonly BAD_REQUEST: 400;
|
|
4
|
-
readonly UNAUTHORIZED: 401;
|
|
5
|
-
readonly FORBIDDEN: 403;
|
|
6
|
-
readonly NOT_FOUND: 404;
|
|
7
|
-
readonly METHOD_NOT_SUPPORTED: 405;
|
|
8
|
-
readonly NOT_ACCEPTABLE: 406;
|
|
9
|
-
readonly TIMEOUT: 408;
|
|
10
|
-
readonly CONFLICT: 409;
|
|
11
|
-
readonly PRECONDITION_FAILED: 412;
|
|
12
|
-
readonly PAYLOAD_TOO_LARGE: 413;
|
|
13
|
-
readonly UNSUPPORTED_MEDIA_TYPE: 415;
|
|
14
|
-
readonly UNPROCESSABLE_CONTENT: 422;
|
|
15
|
-
readonly TOO_MANY_REQUESTS: 429;
|
|
16
|
-
readonly CLIENT_CLOSED_REQUEST: 499;
|
|
17
|
-
readonly INTERNAL_SERVER_ERROR: 500;
|
|
18
|
-
readonly NOT_IMPLEMENTED: 501;
|
|
19
|
-
readonly BAD_GATEWAY: 502;
|
|
20
|
-
readonly SERVICE_UNAVAILABLE: 503;
|
|
21
|
-
readonly GATEWAY_TIMEOUT: 504;
|
|
22
|
-
};
|
|
23
|
-
export type ORPCErrorCode = keyof typeof ORPC_ERROR_CODE_STATUSES;
|
|
24
|
-
export interface ORPCErrorJSON<TCode extends ORPCErrorCode, TData> {
|
|
25
|
-
code: TCode;
|
|
26
|
-
status: number;
|
|
27
|
-
message: string;
|
|
28
|
-
data: TData;
|
|
29
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
30
|
-
}
|
|
31
|
-
export type ANY_ORPC_ERROR_JSON = ORPCErrorJSON<any, any>;
|
|
32
|
-
export type WELL_ORPC_ERROR_JSON = ORPCErrorJSON<ORPCErrorCode, unknown>;
|
|
33
|
-
export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
34
|
-
zz$oe: {
|
|
35
|
-
code: TCode;
|
|
36
|
-
status?: number;
|
|
37
|
-
message?: string;
|
|
38
|
-
cause?: unknown;
|
|
39
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
40
|
-
} & (undefined extends TData ? {
|
|
41
|
-
data?: TData;
|
|
42
|
-
} : {
|
|
43
|
-
data: TData;
|
|
44
|
-
});
|
|
45
|
-
constructor(zz$oe: {
|
|
46
|
-
code: TCode;
|
|
47
|
-
status?: number;
|
|
48
|
-
message?: string;
|
|
49
|
-
cause?: unknown;
|
|
50
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
51
|
-
} & (undefined extends TData ? {
|
|
52
|
-
data?: TData;
|
|
53
|
-
} : {
|
|
54
|
-
data: TData;
|
|
55
|
-
}));
|
|
56
|
-
get code(): TCode;
|
|
57
|
-
get status(): number;
|
|
58
|
-
get data(): TData;
|
|
59
|
-
get issues(): readonly StandardSchemaV1.Issue[] | undefined;
|
|
60
|
-
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
61
|
-
static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined;
|
|
62
|
-
}
|
|
63
|
-
export type WELL_ORPC_ERROR = ORPCError<ORPCErrorCode, unknown>;
|
|
64
|
-
export declare function convertToStandardError(error: unknown): Error;
|
|
1
|
+
export declare function toError(error: unknown): Error;
|
|
65
2
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './constants';
|
|
2
|
+
export * from './error';
|
|
2
3
|
export * from './function';
|
|
3
4
|
export * from './hook';
|
|
4
5
|
export * from './json';
|
|
@@ -6,6 +7,6 @@ export * from './object';
|
|
|
6
7
|
export * from './proxy';
|
|
7
8
|
export * from './value';
|
|
8
9
|
export { isPlainObject } from 'is-what';
|
|
9
|
-
export { guard, mapEntries, mapValues, omit, trim } from 'radash';
|
|
10
|
+
export { group, guard, mapEntries, mapValues, omit, trim } from 'radash';
|
|
10
11
|
export type * from 'type-fest';
|
|
11
12
|
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.28.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -19,11 +19,6 @@
|
|
|
19
19
|
"import": "./dist/index.js",
|
|
20
20
|
"default": "./dist/index.js"
|
|
21
21
|
},
|
|
22
|
-
"./error": {
|
|
23
|
-
"types": "./dist/src/error.d.ts",
|
|
24
|
-
"import": "./dist/error.js",
|
|
25
|
-
"default": "./dist/error.js"
|
|
26
|
-
},
|
|
27
22
|
"./🔒/*": {
|
|
28
23
|
"types": "./dist/src/*.d.ts"
|
|
29
24
|
}
|
|
@@ -40,7 +35,7 @@
|
|
|
40
35
|
"type-fest": "^4.26.1"
|
|
41
36
|
},
|
|
42
37
|
"scripts": {
|
|
43
|
-
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --
|
|
38
|
+
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
|
44
39
|
"build:watch": "pnpm run build --watch",
|
|
45
40
|
"type:check": "tsc -b"
|
|
46
41
|
}
|
package/dist/chunk-CCTAECMC.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// src/error.ts
|
|
2
|
-
var ORPC_ERROR_CODE_STATUSES = {
|
|
3
|
-
BAD_REQUEST: 400,
|
|
4
|
-
UNAUTHORIZED: 401,
|
|
5
|
-
FORBIDDEN: 403,
|
|
6
|
-
NOT_FOUND: 404,
|
|
7
|
-
METHOD_NOT_SUPPORTED: 405,
|
|
8
|
-
NOT_ACCEPTABLE: 406,
|
|
9
|
-
TIMEOUT: 408,
|
|
10
|
-
CONFLICT: 409,
|
|
11
|
-
PRECONDITION_FAILED: 412,
|
|
12
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
13
|
-
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
14
|
-
UNPROCESSABLE_CONTENT: 422,
|
|
15
|
-
TOO_MANY_REQUESTS: 429,
|
|
16
|
-
CLIENT_CLOSED_REQUEST: 499,
|
|
17
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
18
|
-
NOT_IMPLEMENTED: 501,
|
|
19
|
-
BAD_GATEWAY: 502,
|
|
20
|
-
SERVICE_UNAVAILABLE: 503,
|
|
21
|
-
GATEWAY_TIMEOUT: 504
|
|
22
|
-
};
|
|
23
|
-
var ORPCError = class _ORPCError extends Error {
|
|
24
|
-
constructor(zz$oe) {
|
|
25
|
-
if (zz$oe.status && (zz$oe.status < 400 || zz$oe.status >= 600)) {
|
|
26
|
-
throw new Error("The ORPCError status code must be in the 400-599 range.");
|
|
27
|
-
}
|
|
28
|
-
super(zz$oe.message, { cause: zz$oe.cause });
|
|
29
|
-
this.zz$oe = zz$oe;
|
|
30
|
-
}
|
|
31
|
-
get code() {
|
|
32
|
-
return this.zz$oe.code;
|
|
33
|
-
}
|
|
34
|
-
get status() {
|
|
35
|
-
return this.zz$oe.status ?? ORPC_ERROR_CODE_STATUSES[this.code];
|
|
36
|
-
}
|
|
37
|
-
get data() {
|
|
38
|
-
return this.zz$oe.data;
|
|
39
|
-
}
|
|
40
|
-
get issues() {
|
|
41
|
-
return this.zz$oe.issues;
|
|
42
|
-
}
|
|
43
|
-
toJSON() {
|
|
44
|
-
return {
|
|
45
|
-
code: this.code,
|
|
46
|
-
status: this.status,
|
|
47
|
-
message: this.message,
|
|
48
|
-
data: this.data,
|
|
49
|
-
issues: this.issues
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
static fromJSON(json) {
|
|
53
|
-
if (typeof json !== "object" || json === null || !("code" in json) || !Object.keys(ORPC_ERROR_CODE_STATUSES).find((key) => json.code === key) || !("status" in json) || typeof json.status !== "number" || "message" in json && json.message !== void 0 && typeof json.message !== "string" || "issues" in json && json.issues !== void 0 && !Array.isArray(json.issues)) {
|
|
54
|
-
return void 0;
|
|
55
|
-
}
|
|
56
|
-
return new _ORPCError({
|
|
57
|
-
code: json.code,
|
|
58
|
-
status: json.status,
|
|
59
|
-
message: json.message,
|
|
60
|
-
data: json.data,
|
|
61
|
-
issues: json.issues
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
function convertToStandardError(error) {
|
|
66
|
-
if (error instanceof Error) {
|
|
67
|
-
return error;
|
|
68
|
-
}
|
|
69
|
-
if (typeof error === "string") {
|
|
70
|
-
return new Error(error, { cause: error });
|
|
71
|
-
}
|
|
72
|
-
if (typeof error === "object" && error !== null) {
|
|
73
|
-
if ("message" in error && typeof error.message === "string") {
|
|
74
|
-
return new Error(error.message, { cause: error });
|
|
75
|
-
}
|
|
76
|
-
if ("name" in error && typeof error.name === "string") {
|
|
77
|
-
return new Error(error.name, { cause: error });
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return new Error("Unknown error", { cause: error });
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export {
|
|
84
|
-
ORPC_ERROR_CODE_STATUSES,
|
|
85
|
-
ORPCError,
|
|
86
|
-
convertToStandardError
|
|
87
|
-
};
|
|
88
|
-
//# sourceMappingURL=chunk-CCTAECMC.js.map
|