@kamalyb/errors 4.0.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.
@@ -0,0 +1,122 @@
1
+ import { ExtendableError } from "extendable-error";
2
+
3
+ //#region src/types.d.ts
4
+ interface ErrorProps {
5
+ message: string;
6
+ path?: string;
7
+ source?: ErrorSource;
8
+ [key: string]: string | number | boolean | undefined;
9
+ }
10
+ type CustomErrorParam = string | ErrorProps | Array<string | ErrorProps>;
11
+ type ErrorStatus = 400 | 401 | 403 | 404 | 409 | 422 | 429 | 500 | (number & {});
12
+ type ErrorSource = "body" | "params" | "query" | "headers" | "cookies" | "signedCookies";
13
+ //#endregion
14
+ //#region src/utils.d.ts
15
+ declare const Errors: readonly ["BadRequestError", "ForbiddenError", "InternalServerError", "ValidationError", "NotAuthorizedError", "NotFoundError", "RateLimitError", "CustomValidationError", "ConflictError", "GenericError"];
16
+ type ICustomError = (typeof Errors)[number];
17
+ //#endregion
18
+ //#region src/errors/custom.d.ts
19
+ declare abstract class CustomError extends ExtendableError {
20
+ abstract status: ErrorStatus;
21
+ abstract name: ICustomError;
22
+ protected constructor(message: string);
23
+ parse(param: CustomErrorParam, source?: ErrorSource): ErrorProps[];
24
+ abstract serialize(): ErrorProps[];
25
+ }
26
+ //#endregion
27
+ //#region src/errors/bad-request.d.ts
28
+ declare class BadRequestError extends CustomError {
29
+ readonly status = 400;
30
+ readonly name = "BadRequestError";
31
+ private readonly param;
32
+ readonly source?: ErrorSource;
33
+ constructor(message: string, source?: ErrorSource);
34
+ constructor(props: ErrorProps);
35
+ constructor(param: (string | ErrorProps)[]);
36
+ serialize(): ErrorProps[];
37
+ }
38
+ //#endregion
39
+ //#region src/errors/conflict.d.ts
40
+ declare class ConflictError extends CustomError {
41
+ readonly status = 409;
42
+ readonly name = "ConflictError";
43
+ private readonly param;
44
+ constructor(param?: string | ErrorProps);
45
+ serialize(): ErrorProps[];
46
+ }
47
+ //#endregion
48
+ //#region src/errors/forbidden.d.ts
49
+ declare class ForbiddenError extends CustomError {
50
+ message: string;
51
+ readonly status = 403;
52
+ readonly name = "ForbiddenError";
53
+ constructor(message?: string);
54
+ serialize(): {
55
+ message: string;
56
+ }[];
57
+ }
58
+ //#endregion
59
+ //#region src/errors/internal-server.d.ts
60
+ declare class InternalServerError extends CustomError {
61
+ message: string;
62
+ readonly status = 500;
63
+ readonly name = "InternalServerError";
64
+ constructor(message?: string);
65
+ serialize(): {
66
+ message: string;
67
+ }[];
68
+ }
69
+ //#endregion
70
+ //#region src/errors/validation.d.ts
71
+ declare class ValidationError extends CustomError {
72
+ readonly status = 422;
73
+ readonly name = "ValidationError";
74
+ private readonly param;
75
+ readonly source?: ErrorSource;
76
+ constructor(message: string, source?: ErrorSource);
77
+ constructor(props: ErrorProps);
78
+ constructor(param: (string | ErrorProps)[]);
79
+ serialize(): ErrorProps[];
80
+ }
81
+ //#endregion
82
+ //#region src/errors/not-authorized.d.ts
83
+ declare class NotAuthorizedError extends CustomError {
84
+ readonly status = 401;
85
+ readonly name = "NotAuthorizedError";
86
+ private readonly param;
87
+ constructor(message?: string);
88
+ constructor(props?: ErrorProps);
89
+ serialize(): ErrorProps[];
90
+ }
91
+ //#endregion
92
+ //#region src/errors/not-found.d.ts
93
+ declare class NotFoundError extends CustomError {
94
+ readonly status = 404;
95
+ readonly name = "NotFoundError";
96
+ constructor(param?: string | ErrorProps);
97
+ serialize(): {
98
+ message: string;
99
+ }[];
100
+ }
101
+ //#endregion
102
+ //#region src/errors/rate-limit.d.ts
103
+ declare class RateLimitError extends CustomError {
104
+ message: string;
105
+ readonly status = 429;
106
+ readonly name = "RateLimitError";
107
+ constructor(message?: string);
108
+ serialize(): {
109
+ message: string;
110
+ }[];
111
+ }
112
+ //#endregion
113
+ //#region src/errors/generic.d.ts
114
+ declare class GenericError extends CustomError {
115
+ readonly status: number;
116
+ readonly name = "GenericError";
117
+ private readonly param;
118
+ constructor(status: number, param: CustomErrorParam);
119
+ serialize(): ErrorProps[];
120
+ }
121
+ //#endregion
122
+ export { BadRequestError, ConflictError, CustomError, type ErrorProps, type ErrorSource, type ErrorStatus, Errors, ForbiddenError, GenericError, type ICustomError, InternalServerError, NotAuthorizedError, NotFoundError, RateLimitError, ValidationError };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{ExtendableError as e}from"extendable-error";function t(e,t=!0){if(typeof e==`string`)return e;if(!Array.isArray(e))return e.message;if(!e.length)return`An error occurred`;if(t)return e.map(e=>typeof e==`string`?e:e.message).join(`, `);let n=e[0];return typeof n==`string`?n:n.message}const n={401:`Unauthenticated`,403:`Forbidden`,404:`Not Found`,409:`Conflict`,429:`Too many requests`,500:`Internal server error`},r=[`BadRequestError`,`ForbiddenError`,`InternalServerError`,`ValidationError`,`NotAuthorizedError`,`NotFoundError`,`RateLimitError`,`CustomValidationError`,`ConflictError`,`GenericError`];var i=class extends e{constructor(e){super(e)}parse(e,t){return typeof e==`string`?[{message:e,source:t}]:Array.isArray(e)?e.map(e=>typeof e==`string`?{message:e}:e):[e]}},a=class extends i{status=400;name=`BadRequestError`;param;source;constructor(e,n){super(t(e)),this.param=e,this.source=n}serialize(){return this.parse(this.param,this.source)}},o=class extends i{status=409;name=`ConflictError`;param;constructor(e=n[409]){super(t(e)),this.param=e}serialize(){return this.parse(this.param)}},s=class extends i{status=403;name=`ForbiddenError`;constructor(e=n[403]){super(e),this.message=e}serialize(){return[{message:this.message}]}},c=class extends i{status=500;name=`InternalServerError`;constructor(e=n[500]){super(e),this.message=e}serialize(){return[{message:this.message}]}},l=class extends i{status=422;name=`ValidationError`;param;source;constructor(e,n){super(t(e)),this.param=e,this.source=n}serialize(){return this.parse(this.param,this.source)}},u=class extends i{status=401;name=`NotAuthorizedError`;param;constructor(e=n[401]){super(typeof e==`string`?e:e.message),this.param=e}serialize(){return this.parse(this.param)}},d=class extends i{status=404;name=`NotFoundError`;constructor(e=n[404]){super(typeof e==`string`?e:e.message)}serialize(){return[{message:this.message}]}},f=class extends i{status=429;name=`RateLimitError`;constructor(e=n[429]){super(e),this.message=e}serialize(){return[{message:this.message}]}},p=class extends i{status;name=`GenericError`;param;constructor(e,n){super(t(n)),this.param=n,this.status=e}serialize(){return this.parse(this.param)}};export{a as BadRequestError,o as ConflictError,i as CustomError,r as Errors,s as ForbiddenError,p as GenericError,c as InternalServerError,u as NotAuthorizedError,d as NotFoundError,f as RateLimitError,l as ValidationError};
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@kamalyb/errors",
3
+ "version": "4.0.0",
4
+ "type": "module",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": "./dist/index.mjs",
12
+ "types": "./dist/index.d.mts",
13
+ "scripts": {
14
+ "build": "tsc --noEmit && tsdown",
15
+ "test": "vitest run --reporter=verbose",
16
+ "typecheck": "tsc --noEmit",
17
+ "prepublishOnly": "yarn run build"
18
+ },
19
+ "license": "MIT",
20
+ "devDependencies": {
21
+ "prettier": "3.8.1",
22
+ "tsdown": "^0.21.0",
23
+ "typescript": "^5.9.3",
24
+ "vitest": "4.0.18"
25
+ },
26
+ "dependencies": {
27
+ "extendable-error": "^0.1.7"
28
+ },
29
+ "engines": {
30
+ "node": ">=22.12.0"
31
+ },
32
+ "packageManager": "yarn@4.13.0"
33
+ }