@orpc/shared 0.0.0 → 0.0.3

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.
Files changed (2) hide show
  1. package/dist/error.js +71 -0
  2. package/package.json +8 -8
package/dist/error.js ADDED
@@ -0,0 +1,71 @@
1
+ import { ZodError } from "zod";
2
+ const 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
+ 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
+ if (this.code === "BAD_REQUEST" && this.zz$oe.cause instanceof ZodError) {
42
+ return this.zz$oe.cause.issues;
43
+ }
44
+ return void 0;
45
+ }
46
+ toJSON() {
47
+ return {
48
+ code: this.code,
49
+ status: this.status,
50
+ message: this.message,
51
+ data: this.data,
52
+ issues: this.issues
53
+ };
54
+ }
55
+ static fromJSON(json) {
56
+ 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)) {
57
+ return void 0;
58
+ }
59
+ return new ORPCError({
60
+ code: json.code,
61
+ status: json.status,
62
+ message: Reflect.get(json, "message"),
63
+ data: Reflect.get(json, "data"),
64
+ cause: "issues" in json ? new ZodError(json.issues) : void 0
65
+ });
66
+ }
67
+ }
68
+ export {
69
+ ORPCError,
70
+ ORPC_ERROR_CODE_STATUSES
71
+ };
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "@orpc/shared",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "0.0.3",
5
5
  "author": {
6
- "name": "dinwwwh",
7
- "email": "dinwwwh@gmail.com",
8
- "url": "https://dinwwwh.com"
6
+ "name": "unnoq",
7
+ "email": "contact@unnoq.com",
8
+ "url": "https://unnoq.com"
9
9
  },
10
10
  "license": "MIT",
11
- "homepage": "https://github.com/dinwwwh/dinwwwh",
11
+ "homepage": "https://github.com/unnoq/unnoq",
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "https://github.com/dinwwwh/dinwwwh.git",
14
+ "url": "https://github.com/unnoq/unnoq.git",
15
15
  "directory": "examples/typescript-vite-package"
16
16
  },
17
17
  "keywords": [
18
- "dinwwwh"
18
+ "unnoq"
19
19
  ],
20
20
  "publishConfig": {
21
21
  "access": "public"
@@ -47,6 +47,6 @@
47
47
  },
48
48
  "scripts": {
49
49
  "build": "UNPLUGIN_ON_SUCCESS='tsc -b --noCheck' vite build",
50
- "check": "tsc -b"
50
+ "type:check": "tsc -b"
51
51
  }
52
52
  }