@maestro-js/custom-errors 1.0.0-alpha.22 → 1.0.0-alpha.24
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.d.ts +25 -1
- package/dist/index.js +24 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
declare function
|
|
1
|
+
declare function createMany<Entries extends Record<string, CustomErrors.CreateManyEntry>>(entries: Entries): CustomErrors.CreateManyErrors<Entries>;
|
|
2
|
+
declare function makeCustomError<Input = undefined, AdditionalAttributes extends Record<string, any> = Input extends Record<string, any> ? Input : {}, StatusCode extends number | CustomErrors.HttpStatusCodeName | null | undefined = undefined>({ name, message, additionalAttributes, httpStatusCode, meta }: {
|
|
2
3
|
name?: string;
|
|
3
4
|
message?: string | ((input: Input) => string) | null | undefined;
|
|
4
5
|
additionalAttributes?: AdditionalAttributes | ((input: Input) => AdditionalAttributes) | null | undefined;
|
|
5
6
|
httpStatusCode?: StatusCode;
|
|
7
|
+
meta?: Record<string, unknown> | null | undefined;
|
|
6
8
|
}): CustomErrors.CustomErrorConstructor<Input, AdditionalAttributes, StatusCode>;
|
|
7
9
|
declare const httpStatusCodeNames: {
|
|
8
10
|
readonly movedPermanently: 301;
|
|
@@ -22,6 +24,7 @@ declare const httpStatusCodeNames: {
|
|
|
22
24
|
readonly internalServerError: 500;
|
|
23
25
|
};
|
|
24
26
|
declare function getHttpStatusCode(error: unknown): number | null;
|
|
27
|
+
declare function getMeta(error: unknown): Record<string, unknown> | null;
|
|
25
28
|
declare const MovedPermanentlyError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "movedPermanently">;
|
|
26
29
|
declare const FoundError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "found">;
|
|
27
30
|
declare const RedirectError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "redirect">;
|
|
@@ -40,7 +43,9 @@ declare const InternalServerError: CustomErrors.CustomErrorConstructor<string |
|
|
|
40
43
|
|
|
41
44
|
declare const CustomErrors: {
|
|
42
45
|
create: typeof makeCustomError;
|
|
46
|
+
createMany: typeof createMany;
|
|
43
47
|
getHttpStatusCode: typeof getHttpStatusCode;
|
|
48
|
+
getMeta: typeof getMeta;
|
|
44
49
|
MovedPermanentlyError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "movedPermanently">;
|
|
45
50
|
FoundError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "found">;
|
|
46
51
|
RedirectError: CustomErrors.CustomErrorConstructor<string | undefined, {}, "redirect">;
|
|
@@ -69,6 +74,25 @@ declare namespace CustomErrors {
|
|
|
69
74
|
readonly prototype: Error;
|
|
70
75
|
};
|
|
71
76
|
type HttpStatusCodeName = keyof typeof httpStatusCodeNames;
|
|
77
|
+
type CreateManyEntry = {
|
|
78
|
+
message?: string | ((input: any) => string) | null | undefined;
|
|
79
|
+
additionalAttributes?: Record<string, any> | ((input: any) => Record<string, any>) | null | undefined;
|
|
80
|
+
httpStatusCode: number | HttpStatusCodeName;
|
|
81
|
+
meta?: Record<string, unknown> | null | undefined;
|
|
82
|
+
};
|
|
83
|
+
type CreateManyErrors<Entries extends Record<string, CreateManyEntry>> = {
|
|
84
|
+
[Name in keyof Entries]: CustomErrorConstructor<CreateManyEntryInput<Entries[Name]>, CreateManyEntryAttributes<Entries[Name]>, Entries[Name]['httpStatusCode']>;
|
|
85
|
+
};
|
|
86
|
+
type CreateManyEntryInput<Entry extends CreateManyEntry> = Entry extends {
|
|
87
|
+
message: (input: infer Input) => string;
|
|
88
|
+
} ? Input : Entry extends {
|
|
89
|
+
additionalAttributes: (input: infer Input) => Record<string, any>;
|
|
90
|
+
} ? Input : undefined;
|
|
91
|
+
type CreateManyEntryAttributes<Entry extends CreateManyEntry> = Entry extends {
|
|
92
|
+
additionalAttributes: (input: any) => infer Attributes extends Record<string, any>;
|
|
93
|
+
} ? Attributes : Entry extends {
|
|
94
|
+
additionalAttributes: infer Attributes extends Record<string, any>;
|
|
95
|
+
} ? Attributes : {};
|
|
72
96
|
}
|
|
73
97
|
|
|
74
98
|
export { BadRequestError, ConflictError, CustomErrors, ForbiddenError, FoundError, InternalServerError, LockedError, MovedPermanentlyError, NotFoundError, NotModifiedError, PaymentRequiredError, RedirectError, TooManyRequestsError, UnauthorizedError, UnprocessableContentError, ValidationError };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
function createMany(entries) {
|
|
3
|
+
return Object.fromEntries(
|
|
4
|
+
Object.entries(entries).map(([name, entry]) => [name, makeCustomError({ ...entry, name })])
|
|
5
|
+
);
|
|
6
|
+
}
|
|
2
7
|
function makeCustomError({
|
|
3
8
|
name,
|
|
4
9
|
message,
|
|
5
10
|
additionalAttributes,
|
|
6
|
-
httpStatusCode
|
|
11
|
+
httpStatusCode,
|
|
12
|
+
meta
|
|
7
13
|
}) {
|
|
8
14
|
class MyError extends Error {
|
|
9
15
|
constructor(input) {
|
|
@@ -25,6 +31,9 @@ function makeCustomError({
|
|
|
25
31
|
if (name) {
|
|
26
32
|
MyError.prototype.name = name;
|
|
27
33
|
}
|
|
34
|
+
if (meta != null) {
|
|
35
|
+
MyError.prototype["meta"] = meta;
|
|
36
|
+
}
|
|
28
37
|
return MyError;
|
|
29
38
|
}
|
|
30
39
|
var httpStatusCodeNames = {
|
|
@@ -58,6 +67,18 @@ function getHttpStatusCode(error) {
|
|
|
58
67
|
return null;
|
|
59
68
|
}
|
|
60
69
|
}
|
|
70
|
+
function getMeta(error) {
|
|
71
|
+
if (error instanceof Error) {
|
|
72
|
+
const meta = error["meta"];
|
|
73
|
+
if (typeof meta === "object" && meta !== null) {
|
|
74
|
+
return meta;
|
|
75
|
+
} else {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
61
82
|
var MovedPermanentlyError = makeCustomError({
|
|
62
83
|
name: "MovedPermanentlyError",
|
|
63
84
|
message: (input) => input ?? "Moved Permanently",
|
|
@@ -135,7 +156,9 @@ var InternalServerError = makeCustomError({
|
|
|
135
156
|
});
|
|
136
157
|
var CustomErrors = {
|
|
137
158
|
create: makeCustomError,
|
|
159
|
+
createMany,
|
|
138
160
|
getHttpStatusCode,
|
|
161
|
+
getMeta,
|
|
139
162
|
MovedPermanentlyError,
|
|
140
163
|
FoundError,
|
|
141
164
|
RedirectError,
|