@devp0nt/error0 1.0.0-next.41 → 1.0.0-next.42
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/cjs/index.cjs +77 -73
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +109 -100
- package/dist/esm/index.d.ts +109 -100
- package/dist/esm/index.js +76 -72
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.test.ts +68 -36
- package/src/index.ts +258 -239
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,44 +1,52 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
1
|
+
type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
|
|
2
|
+
type NormalizeUnknownToUndefined<T> = IsUnknown<T> extends true ? undefined : T;
|
|
3
|
+
type IsOnlyUndefined<T> = [Exclude<T, undefined>] extends [never] ? true : false;
|
|
4
|
+
type InferFirstArg<TFn> = TFn extends (...args: infer TArgs) => unknown ? TArgs extends [infer TFirst, ...unknown[]] ? TFirst : undefined : undefined;
|
|
5
|
+
type InferPluginPropInput<TProp extends ErrorPluginPropOptions<any, any, any>> = NormalizeUnknownToUndefined<InferFirstArg<TProp['init']>>;
|
|
6
|
+
type ErrorPluginPropInit<TInputValue, TOutputValue> = ((input: TInputValue) => TOutputValue) | (() => TOutputValue);
|
|
7
|
+
type ErrorPluginPropSerialize<TOutputValue, TError extends Error0> = ((options: {
|
|
8
|
+
value: TOutputValue;
|
|
9
|
+
error: TError;
|
|
10
|
+
isPublic: boolean;
|
|
11
|
+
}) => unknown) | false;
|
|
12
|
+
type ErrorPluginPropDeserialize<TOutputValue> = ((options: {
|
|
13
|
+
value: unknown;
|
|
14
|
+
serialized: Record<string, unknown>;
|
|
15
|
+
}) => TOutputValue | undefined) | false;
|
|
16
|
+
type ErrorPluginPropOptions<TInputValue, TOutputValue, TError extends Error0 = Error0> = {
|
|
17
|
+
init: ErrorPluginPropInit<TInputValue, TOutputValue>;
|
|
3
18
|
resolve: (options: {
|
|
4
19
|
value: TOutputValue | undefined;
|
|
5
20
|
flow: Array<TOutputValue | undefined>;
|
|
6
21
|
error: TError;
|
|
7
22
|
}) => TOutputValue | undefined;
|
|
8
|
-
serialize:
|
|
9
|
-
|
|
10
|
-
error: TError;
|
|
11
|
-
isPublic: boolean;
|
|
12
|
-
}) => unknown;
|
|
13
|
-
deserialize: (options: {
|
|
14
|
-
value: unknown;
|
|
15
|
-
serialized: Record<string, unknown>;
|
|
16
|
-
}) => TOutputValue | undefined;
|
|
23
|
+
serialize: ErrorPluginPropSerialize<TOutputValue, TError>;
|
|
24
|
+
deserialize: ErrorPluginPropDeserialize<TOutputValue>;
|
|
17
25
|
};
|
|
18
|
-
type
|
|
19
|
-
type
|
|
20
|
-
type
|
|
26
|
+
type ErrorPluginMethodFn<TOutputValue, TArgs extends unknown[] = unknown[], TError extends Error0 = Error0> = (error: TError, ...args: TArgs) => TOutputValue;
|
|
27
|
+
type ErrorPluginAdaptResult<TOutputProps extends Record<string, unknown>> = Partial<TOutputProps> | undefined;
|
|
28
|
+
type ErrorPluginAdaptFn<TError extends Error0 = Error0, TOutputProps extends Record<string, unknown> = Record<never, never>> = ((error: TError) => void) | ((error: TError) => ErrorPluginAdaptResult<TOutputProps>);
|
|
21
29
|
type ErrorMethodRecord = {
|
|
22
30
|
args: unknown[];
|
|
23
31
|
output: unknown;
|
|
24
32
|
};
|
|
25
|
-
type
|
|
26
|
-
[key: string]:
|
|
33
|
+
type ErrorPluginProps = {
|
|
34
|
+
[key: string]: ErrorPluginPropOptions<any, any>;
|
|
27
35
|
};
|
|
28
|
-
type
|
|
29
|
-
[key: string]:
|
|
36
|
+
type ErrorPluginMethods = {
|
|
37
|
+
[key: string]: ErrorPluginMethodFn<any, any[]>;
|
|
30
38
|
};
|
|
31
|
-
type
|
|
39
|
+
type ErrorPlugin<TProps extends ErrorPluginProps = Record<never, never>, TMethods extends ErrorPluginMethods = Record<never, never>> = {
|
|
32
40
|
props?: TProps;
|
|
33
41
|
methods?: TMethods;
|
|
34
|
-
|
|
42
|
+
adapt?: Array<ErrorPluginAdaptFn<Error0, PluginOutputProps<TProps>>>;
|
|
35
43
|
};
|
|
36
|
-
type
|
|
37
|
-
type
|
|
38
|
-
type
|
|
39
|
-
[TKey in keyof TProps]: TProps[TKey] extends
|
|
44
|
+
type AddPropToPluginProps<TProps extends ErrorPluginProps, TKey extends string, TInputValue, TOutputValue> = TProps & Record<TKey, ErrorPluginPropOptions<TInputValue, TOutputValue>>;
|
|
45
|
+
type AddMethodToPluginMethods<TMethods extends ErrorPluginMethods, TKey extends string, TArgs extends unknown[], TOutputValue> = TMethods & Record<TKey, ErrorPluginMethodFn<TOutputValue, TArgs>>;
|
|
46
|
+
type PluginOutputProps<TProps extends ErrorPluginProps> = {
|
|
47
|
+
[TKey in keyof TProps]: TProps[TKey] extends ErrorPluginPropOptions<any, infer TOutputValue> ? TOutputValue : never;
|
|
40
48
|
};
|
|
41
|
-
type
|
|
49
|
+
type ErrorPluginsMap = {
|
|
42
50
|
props: Record<string, {
|
|
43
51
|
init: unknown;
|
|
44
52
|
resolve: unknown;
|
|
@@ -49,103 +57,104 @@ type IsEmptyObject<T> = keyof T extends never ? true : false;
|
|
|
49
57
|
type ErrorInputBase = {
|
|
50
58
|
cause?: unknown;
|
|
51
59
|
};
|
|
52
|
-
type
|
|
53
|
-
[TKey in keyof
|
|
54
|
-
}
|
|
55
|
-
type
|
|
56
|
-
|
|
60
|
+
type ErrorInputPluginProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
61
|
+
[TKey in keyof TPluginsMap['props'] as IsOnlyUndefined<TPluginsMap['props'][TKey]['init']> extends true ? never : TKey]?: TPluginsMap['props'][TKey]['init'];
|
|
62
|
+
};
|
|
63
|
+
type ErrorInput<TPluginsMap extends ErrorPluginsMap> = IsEmptyObject<TPluginsMap['props']> extends true ? ErrorInputBase : ErrorInputBase & ErrorInputPluginProps<TPluginsMap>;
|
|
64
|
+
type ErrorOutputProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
65
|
+
[TKey in keyof TPluginsMap['props']]: TPluginsMap['props'][TKey]['resolve'] | undefined;
|
|
57
66
|
};
|
|
58
|
-
type ErrorOutputMethods<
|
|
59
|
-
[TKey in keyof
|
|
67
|
+
type ErrorOutputMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
68
|
+
[TKey in keyof TPluginsMap['methods']]: TPluginsMap['methods'][TKey] extends {
|
|
60
69
|
args: infer TArgs extends unknown[];
|
|
61
70
|
output: infer TOutput;
|
|
62
71
|
} ? (...args: TArgs) => TOutput : never;
|
|
63
72
|
};
|
|
64
|
-
type ErrorOutput<
|
|
65
|
-
type ErrorStaticMethods<
|
|
66
|
-
[TKey in keyof
|
|
73
|
+
type ErrorOutput<TPluginsMap extends ErrorPluginsMap> = ErrorOutputProps<TPluginsMap> & ErrorOutputMethods<TPluginsMap>;
|
|
74
|
+
type ErrorStaticMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
75
|
+
[TKey in keyof TPluginsMap['methods']]: TPluginsMap['methods'][TKey] extends {
|
|
67
76
|
args: infer TArgs extends unknown[];
|
|
68
77
|
output: infer TOutput;
|
|
69
78
|
} ? (error: unknown, ...args: TArgs) => TOutput : never;
|
|
70
79
|
};
|
|
71
|
-
type
|
|
80
|
+
type EmptyPluginsMap = {
|
|
72
81
|
props: Record<never, {
|
|
73
82
|
init: never;
|
|
74
83
|
resolve: never;
|
|
75
84
|
}>;
|
|
76
85
|
methods: Record<never, ErrorMethodRecord>;
|
|
77
86
|
};
|
|
78
|
-
type
|
|
79
|
-
[TKey in keyof NonNullable<
|
|
80
|
-
init:
|
|
87
|
+
type PluginPropsMapOf<TPlugin extends ErrorPlugin> = {
|
|
88
|
+
[TKey in keyof NonNullable<TPlugin['props']>]: NonNullable<TPlugin['props']>[TKey] extends ErrorPluginPropOptions<any, infer TOutputValue> ? {
|
|
89
|
+
init: InferPluginPropInput<NonNullable<TPlugin['props']>[TKey]>;
|
|
81
90
|
resolve: TOutputValue;
|
|
82
91
|
} : never;
|
|
83
92
|
};
|
|
84
|
-
type
|
|
85
|
-
[TKey in keyof NonNullable<
|
|
93
|
+
type PluginMethodsMapOf<TPlugin extends ErrorPlugin> = {
|
|
94
|
+
[TKey in keyof NonNullable<TPlugin['methods']>]: NonNullable<TPlugin['methods']>[TKey] extends (error: Error0, ...args: infer TArgs extends unknown[]) => infer TOutput ? {
|
|
86
95
|
args: TArgs;
|
|
87
96
|
output: TOutput;
|
|
88
97
|
} : never;
|
|
89
98
|
};
|
|
90
|
-
type
|
|
91
|
-
props:
|
|
92
|
-
methods:
|
|
93
|
-
};
|
|
94
|
-
type
|
|
95
|
-
props: TMap['props'] &
|
|
96
|
-
methods: TMap['methods'] &
|
|
97
|
-
};
|
|
98
|
-
type
|
|
99
|
-
type
|
|
100
|
-
type
|
|
101
|
-
|
|
102
|
-
} ?
|
|
103
|
-
type
|
|
104
|
-
type ErrorInstanceOfMap<TMap extends
|
|
105
|
-
type BuilderError0<TProps extends
|
|
106
|
-
type
|
|
107
|
-
declare class
|
|
108
|
-
private readonly
|
|
99
|
+
type ErrorPluginsMapOfPlugin<TPlugin extends ErrorPlugin> = {
|
|
100
|
+
props: PluginPropsMapOf<TPlugin>;
|
|
101
|
+
methods: PluginMethodsMapOf<TPlugin>;
|
|
102
|
+
};
|
|
103
|
+
type ExtendErrorPluginsMap<TMap extends ErrorPluginsMap, TPlugin extends ErrorPlugin> = {
|
|
104
|
+
props: TMap['props'] & ErrorPluginsMapOfPlugin<TPlugin>['props'];
|
|
105
|
+
methods: TMap['methods'] & ErrorPluginsMapOfPlugin<TPlugin>['methods'];
|
|
106
|
+
};
|
|
107
|
+
type ExtendErrorPluginsMapWithProp<TMap extends ErrorPluginsMap, TKey extends string, TInputValue, TOutputValue> = ExtendErrorPluginsMap<TMap, ErrorPlugin<Record<TKey, ErrorPluginPropOptions<TInputValue, TOutputValue>>>>;
|
|
108
|
+
type ExtendErrorPluginsMapWithMethod<TMap extends ErrorPluginsMap, TKey extends string, TArgs extends unknown[], TOutputValue> = ExtendErrorPluginsMap<TMap, ErrorPlugin<Record<never, never>, Record<TKey, ErrorPluginMethodFn<TOutputValue, TArgs>>>>;
|
|
109
|
+
type PluginsMapOf<TClass> = TClass extends {
|
|
110
|
+
__pluginsMap?: infer TPluginsMap;
|
|
111
|
+
} ? TPluginsMap extends ErrorPluginsMap ? TPluginsMap : EmptyPluginsMap : EmptyPluginsMap;
|
|
112
|
+
type PluginsMapFromParts<TProps extends ErrorPluginProps, TMethods extends ErrorPluginMethods> = ErrorPluginsMapOfPlugin<ErrorPlugin<TProps, TMethods>>;
|
|
113
|
+
type ErrorInstanceOfMap<TMap extends ErrorPluginsMap> = Error0 & ErrorOutput<TMap>;
|
|
114
|
+
type BuilderError0<TProps extends ErrorPluginProps, TMethods extends ErrorPluginMethods> = Error0 & ErrorOutput<PluginsMapFromParts<TProps, TMethods>>;
|
|
115
|
+
type PluginOfBuilder<TBuilder> = TBuilder extends PluginError0<infer TProps, infer TMethods> ? ErrorPlugin<TProps, TMethods> : never;
|
|
116
|
+
declare class PluginError0<TProps extends ErrorPluginProps = Record<never, never>, TMethods extends ErrorPluginMethods = Record<never, never>> {
|
|
117
|
+
private readonly _plugin;
|
|
109
118
|
readonly Infer: {
|
|
110
119
|
props: TProps;
|
|
111
120
|
methods: TMethods;
|
|
112
121
|
};
|
|
113
|
-
constructor(
|
|
114
|
-
prop<TKey extends string, TInputValue, TOutputValue>(key: TKey, value:
|
|
115
|
-
method<TKey extends string, TArgs extends unknown[], TOutputValue>(key: TKey, value:
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
122
|
+
constructor(plugin?: ErrorPlugin<ErrorPluginProps, ErrorPluginMethods>);
|
|
123
|
+
prop<TKey extends string, TInputValue, TOutputValue>(key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, BuilderError0<TProps, TMethods>>): PluginError0<AddPropToPluginProps<TProps, TKey, TInputValue, TOutputValue>, TMethods>;
|
|
124
|
+
method<TKey extends string, TArgs extends unknown[], TOutputValue>(key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, BuilderError0<TProps, TMethods>>): PluginError0<TProps, AddMethodToPluginMethods<TMethods, TKey, TArgs, TOutputValue>>;
|
|
125
|
+
adapt(value: ErrorPluginAdaptFn<BuilderError0<TProps, TMethods>, PluginOutputProps<TProps>>): PluginError0<TProps, TMethods>;
|
|
126
|
+
use<TKey extends string, TInputValue, TOutputValue>(kind: 'prop', key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, BuilderError0<TProps, TMethods>>): PluginError0<AddPropToPluginProps<TProps, TKey, TInputValue, TOutputValue>, TMethods>;
|
|
127
|
+
use<TKey extends string, TArgs extends unknown[], TOutputValue>(kind: 'method', key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, BuilderError0<TProps, TMethods>>): PluginError0<TProps, AddMethodToPluginMethods<TMethods, TKey, TArgs, TOutputValue>>;
|
|
128
|
+
use(kind: 'adapt', value: ErrorPluginAdaptFn<BuilderError0<TProps, TMethods>, PluginOutputProps<TProps>>): PluginError0<TProps, TMethods>;
|
|
120
129
|
}
|
|
121
|
-
type ClassError0<
|
|
122
|
-
new (message: string, input?: ErrorInput<
|
|
130
|
+
type ClassError0<TPluginsMap extends ErrorPluginsMap = EmptyPluginsMap> = {
|
|
131
|
+
new (message: string, input?: ErrorInput<TPluginsMap>): Error0 & ErrorOutput<TPluginsMap>;
|
|
123
132
|
new (input: {
|
|
124
133
|
message: string;
|
|
125
|
-
} & ErrorInput<
|
|
126
|
-
readonly
|
|
127
|
-
from: (error: unknown) => Error0 & ErrorOutput<
|
|
134
|
+
} & ErrorInput<TPluginsMap>): Error0 & ErrorOutput<TPluginsMap>;
|
|
135
|
+
readonly __pluginsMap?: TPluginsMap;
|
|
136
|
+
from: (error: unknown) => Error0 & ErrorOutput<TPluginsMap>;
|
|
128
137
|
serialize: (error: unknown, isPublic?: boolean) => Record<string, unknown>;
|
|
129
|
-
prop: <TKey extends string, TInputValue, TOutputValue>(key: TKey, value:
|
|
130
|
-
method: <TKey extends string, TArgs extends unknown[], TOutputValue>(key: TKey, value:
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
<TBuilder extends
|
|
134
|
-
<TKey extends string, TInputValue, TOutputValue>(kind: 'prop', key: TKey, value:
|
|
135
|
-
<TKey extends string, TArgs extends unknown[], TOutputValue>(kind: 'method', key: TKey, value:
|
|
136
|
-
(kind: '
|
|
138
|
+
prop: <TKey extends string, TInputValue, TOutputValue>(key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<TPluginsMap>>) => ClassError0<ExtendErrorPluginsMapWithProp<TPluginsMap, TKey, TInputValue, TOutputValue>>;
|
|
139
|
+
method: <TKey extends string, TArgs extends unknown[], TOutputValue>(key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, ErrorInstanceOfMap<TPluginsMap>>) => ClassError0<ExtendErrorPluginsMapWithMethod<TPluginsMap, TKey, TArgs, TOutputValue>>;
|
|
140
|
+
adapt: (value: ErrorPluginAdaptFn<ErrorInstanceOfMap<TPluginsMap>, ErrorOutputProps<TPluginsMap>>) => ClassError0<TPluginsMap>;
|
|
141
|
+
use: {
|
|
142
|
+
<TBuilder extends PluginError0>(plugin: TBuilder): ClassError0<ExtendErrorPluginsMap<TPluginsMap, PluginOfBuilder<TBuilder>>>;
|
|
143
|
+
<TKey extends string, TInputValue, TOutputValue>(kind: 'prop', key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<TPluginsMap>>): ClassError0<ExtendErrorPluginsMapWithProp<TPluginsMap, TKey, TInputValue, TOutputValue>>;
|
|
144
|
+
<TKey extends string, TArgs extends unknown[], TOutputValue>(kind: 'method', key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, ErrorInstanceOfMap<TPluginsMap>>): ClassError0<ExtendErrorPluginsMapWithMethod<TPluginsMap, TKey, TArgs, TOutputValue>>;
|
|
145
|
+
(kind: 'adapt', value: ErrorPluginAdaptFn<ErrorInstanceOfMap<TPluginsMap>, ErrorOutputProps<TPluginsMap>>): ClassError0<TPluginsMap>;
|
|
137
146
|
};
|
|
138
|
-
|
|
139
|
-
} & ErrorStaticMethods<
|
|
147
|
+
plugin: () => PluginError0;
|
|
148
|
+
} & ErrorStaticMethods<TPluginsMap>;
|
|
140
149
|
declare class Error0 extends Error {
|
|
141
|
-
static readonly
|
|
142
|
-
protected static
|
|
143
|
-
private static readonly
|
|
144
|
-
private static
|
|
145
|
-
constructor(message: string, input?: ErrorInput<
|
|
150
|
+
static readonly __pluginsMap?: EmptyPluginsMap;
|
|
151
|
+
protected static _plugins: ErrorPlugin[];
|
|
152
|
+
private static readonly _emptyPlugin;
|
|
153
|
+
private static _getResolvedPlugin;
|
|
154
|
+
constructor(message: string, input?: ErrorInput<EmptyPluginsMap>);
|
|
146
155
|
constructor(input: {
|
|
147
156
|
message: string;
|
|
148
|
-
} & ErrorInput<
|
|
157
|
+
} & ErrorInput<EmptyPluginsMap>);
|
|
149
158
|
private static readonly isSelfProperty;
|
|
150
159
|
static own(error: object, key: string): unknown;
|
|
151
160
|
own(key: string): unknown;
|
|
@@ -158,22 +167,22 @@ declare class Error0 extends Error {
|
|
|
158
167
|
static is<T extends typeof Error0>(this: T, error: unknown): error is InstanceType<T>;
|
|
159
168
|
static isSerialized(error: unknown): error is Record<string, unknown>;
|
|
160
169
|
static from(error: unknown): Error0;
|
|
161
|
-
private static
|
|
170
|
+
private static _applyAdapt;
|
|
162
171
|
private static _fromSerialized;
|
|
163
172
|
private static _fromNonError0;
|
|
164
173
|
private static _extractMessage;
|
|
165
|
-
private static
|
|
166
|
-
private static
|
|
167
|
-
static prop<TThis extends typeof Error0, TKey extends string, TInputValue, TOutputValue>(this: TThis, key: TKey, value:
|
|
168
|
-
static method<TThis extends typeof Error0, TKey extends string, TArgs extends unknown[], TOutputValue>(this: TThis, key: TKey, value:
|
|
169
|
-
static
|
|
170
|
-
static
|
|
171
|
-
static
|
|
172
|
-
static
|
|
173
|
-
static
|
|
174
|
-
static
|
|
174
|
+
private static _useWithPlugin;
|
|
175
|
+
private static _pluginFromBuilder;
|
|
176
|
+
static prop<TThis extends typeof Error0, TKey extends string, TInputValue, TOutputValue>(this: TThis, key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<PluginsMapOf<TThis>>>): ClassError0<ExtendErrorPluginsMapWithProp<PluginsMapOf<TThis>, TKey, TInputValue, TOutputValue>>;
|
|
177
|
+
static method<TThis extends typeof Error0, TKey extends string, TArgs extends unknown[], TOutputValue>(this: TThis, key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, ErrorInstanceOfMap<PluginsMapOf<TThis>>>): ClassError0<ExtendErrorPluginsMapWithMethod<PluginsMapOf<TThis>, TKey, TArgs, TOutputValue>>;
|
|
178
|
+
static adapt<TThis extends typeof Error0>(this: TThis, value: ErrorPluginAdaptFn<ErrorInstanceOfMap<PluginsMapOf<TThis>>, ErrorOutputProps<PluginsMapOf<TThis>>>): ClassError0<PluginsMapOf<TThis>>;
|
|
179
|
+
static use<TThis extends typeof Error0, TBuilder extends PluginError0>(this: TThis, plugin: TBuilder): ClassError0<ExtendErrorPluginsMap<PluginsMapOf<TThis>, PluginOfBuilder<TBuilder>>>;
|
|
180
|
+
static use<TThis extends typeof Error0, TKey extends string, TInputValue, TOutputValue>(this: TThis, kind: 'prop', key: TKey, value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<PluginsMapOf<TThis>>>): ClassError0<ExtendErrorPluginsMapWithProp<PluginsMapOf<TThis>, TKey, TInputValue, TOutputValue>>;
|
|
181
|
+
static use<TThis extends typeof Error0, TKey extends string, TArgs extends unknown[], TOutputValue>(this: TThis, kind: 'method', key: TKey, value: ErrorPluginMethodFn<TOutputValue, TArgs, ErrorInstanceOfMap<PluginsMapOf<TThis>>>): ClassError0<ExtendErrorPluginsMapWithMethod<PluginsMapOf<TThis>, TKey, TArgs, TOutputValue>>;
|
|
182
|
+
static use<TThis extends typeof Error0>(this: TThis, kind: 'adapt', value: ErrorPluginAdaptFn<ErrorInstanceOfMap<PluginsMapOf<TThis>>, ErrorOutputProps<PluginsMapOf<TThis>>>): ClassError0<PluginsMapOf<TThis>>;
|
|
183
|
+
static plugin(): PluginError0;
|
|
175
184
|
static serialize(error: unknown, isPublic?: boolean): Record<string, unknown>;
|
|
176
185
|
serialize(isPublic?: boolean): object;
|
|
177
186
|
}
|
|
178
187
|
|
|
179
|
-
export { type ClassError0, Error0, type
|
|
188
|
+
export { type ClassError0, Error0, type ErrorInput, type ErrorInputBase, type ErrorOutput, type ErrorPlugin, type ErrorPluginAdaptFn, type ErrorPluginAdaptResult, type ErrorPluginMethodFn, type ErrorPluginMethods, type ErrorPluginPropOptions, type ErrorPluginProps, type ErrorPluginsMap, type IsEmptyObject, PluginError0 };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,68 +1,66 @@
|
|
|
1
|
-
class
|
|
2
|
-
|
|
1
|
+
class PluginError0 {
|
|
2
|
+
_plugin;
|
|
3
3
|
Infer = void 0;
|
|
4
|
-
constructor(
|
|
5
|
-
this.
|
|
6
|
-
props: { ...
|
|
7
|
-
methods: { ...
|
|
8
|
-
|
|
4
|
+
constructor(plugin) {
|
|
5
|
+
this._plugin = {
|
|
6
|
+
props: { ...plugin?.props ?? {} },
|
|
7
|
+
methods: { ...plugin?.methods ?? {} },
|
|
8
|
+
adapt: [...plugin?.adapt ?? []]
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
prop(key, value) {
|
|
12
|
-
return this.
|
|
12
|
+
return this.use("prop", key, value);
|
|
13
13
|
}
|
|
14
14
|
method(key, value) {
|
|
15
|
-
return this.
|
|
15
|
+
return this.use("method", key, value);
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
return this.
|
|
17
|
+
adapt(value) {
|
|
18
|
+
return this.use("adapt", value);
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
const nextProps = { ...this.
|
|
22
|
-
const nextMethods = { ...this.
|
|
23
|
-
const
|
|
24
|
-
...this._extension.refine ?? []
|
|
25
|
-
];
|
|
20
|
+
use(kind, keyOrValue, value) {
|
|
21
|
+
const nextProps = { ...this._plugin.props ?? {} };
|
|
22
|
+
const nextMethods = { ...this._plugin.methods ?? {} };
|
|
23
|
+
const nextAdapt = [...this._plugin.adapt ?? []];
|
|
26
24
|
if (kind === "prop") {
|
|
27
25
|
const key = keyOrValue;
|
|
28
26
|
if (value === void 0) {
|
|
29
|
-
throw new Error('
|
|
27
|
+
throw new Error('PluginError0.use("prop", key, value) requires value');
|
|
30
28
|
}
|
|
31
29
|
nextProps[key] = value;
|
|
32
30
|
} else if (kind === "method") {
|
|
33
31
|
const key = keyOrValue;
|
|
34
32
|
if (value === void 0) {
|
|
35
|
-
throw new Error('
|
|
33
|
+
throw new Error('PluginError0.use("method", key, value) requires value');
|
|
36
34
|
}
|
|
37
35
|
nextMethods[key] = value;
|
|
38
36
|
} else {
|
|
39
|
-
|
|
37
|
+
nextAdapt.push(keyOrValue);
|
|
40
38
|
}
|
|
41
|
-
return new
|
|
39
|
+
return new PluginError0({
|
|
42
40
|
props: nextProps,
|
|
43
41
|
methods: nextMethods,
|
|
44
|
-
|
|
42
|
+
adapt: nextAdapt
|
|
45
43
|
});
|
|
46
44
|
}
|
|
47
45
|
}
|
|
48
46
|
class Error0 extends Error {
|
|
49
|
-
static
|
|
50
|
-
static
|
|
51
|
-
static
|
|
47
|
+
static __pluginsMap;
|
|
48
|
+
static _plugins = [];
|
|
49
|
+
static _emptyPlugin = {
|
|
52
50
|
props: {},
|
|
53
51
|
methods: {},
|
|
54
|
-
|
|
52
|
+
adapt: []
|
|
55
53
|
};
|
|
56
|
-
static
|
|
54
|
+
static _getResolvedPlugin() {
|
|
57
55
|
const resolved = {
|
|
58
56
|
props: {},
|
|
59
57
|
methods: {},
|
|
60
|
-
|
|
58
|
+
adapt: []
|
|
61
59
|
};
|
|
62
|
-
for (const
|
|
63
|
-
Object.assign(resolved.props,
|
|
64
|
-
Object.assign(resolved.methods,
|
|
65
|
-
resolved.
|
|
60
|
+
for (const plugin of this._plugins) {
|
|
61
|
+
Object.assign(resolved.props, plugin.props ?? this._emptyPlugin.props);
|
|
62
|
+
Object.assign(resolved.methods, plugin.methods ?? this._emptyPlugin.methods);
|
|
63
|
+
resolved.adapt.push(...plugin.adapt ?? this._emptyPlugin.adapt);
|
|
66
64
|
}
|
|
67
65
|
return resolved;
|
|
68
66
|
}
|
|
@@ -72,8 +70,8 @@ class Error0 extends Error {
|
|
|
72
70
|
super(input.message, { cause: input.cause });
|
|
73
71
|
this.name = "Error0";
|
|
74
72
|
const ctor = this.constructor;
|
|
75
|
-
const
|
|
76
|
-
for (const [key, prop] of Object.entries(
|
|
73
|
+
const plugin = ctor._getResolvedPlugin();
|
|
74
|
+
for (const [key, prop] of Object.entries(plugin.props)) {
|
|
77
75
|
if (key in input) {
|
|
78
76
|
const ownValue = input[key];
|
|
79
77
|
this[key] = prop.init(ownValue);
|
|
@@ -167,12 +165,12 @@ class Error0 extends Error {
|
|
|
167
165
|
}
|
|
168
166
|
return this._fromNonError0(error);
|
|
169
167
|
}
|
|
170
|
-
static
|
|
171
|
-
const
|
|
172
|
-
for (const
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
Object.assign(error,
|
|
168
|
+
static _applyAdapt(error) {
|
|
169
|
+
const plugin = this._getResolvedPlugin();
|
|
170
|
+
for (const adapt of plugin.adapt) {
|
|
171
|
+
const adapted = adapt(error);
|
|
172
|
+
if (adapted && typeof adapted === "object") {
|
|
173
|
+
Object.assign(error, adapted);
|
|
176
174
|
}
|
|
177
175
|
}
|
|
178
176
|
return error;
|
|
@@ -180,13 +178,16 @@ class Error0 extends Error {
|
|
|
180
178
|
static _fromSerialized(error) {
|
|
181
179
|
const message = this._extractMessage(error);
|
|
182
180
|
if (typeof error !== "object" || error === null) {
|
|
183
|
-
return this.
|
|
181
|
+
return this._applyAdapt(new this(message, { cause: error }));
|
|
184
182
|
}
|
|
185
183
|
const errorRecord = error;
|
|
186
184
|
const recreated = new this(message);
|
|
187
|
-
const
|
|
188
|
-
const propsEntries = Object.entries(
|
|
185
|
+
const plugin = this._getResolvedPlugin();
|
|
186
|
+
const propsEntries = Object.entries(plugin.props);
|
|
189
187
|
for (const [key, prop] of propsEntries) {
|
|
188
|
+
if (prop.deserialize === false) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
190
191
|
if (!(key in errorRecord)) {
|
|
191
192
|
continue;
|
|
192
193
|
}
|
|
@@ -204,17 +205,17 @@ class Error0 extends Error {
|
|
|
204
205
|
}
|
|
205
206
|
static _fromNonError0(error) {
|
|
206
207
|
const message = this._extractMessage(error);
|
|
207
|
-
return this.
|
|
208
|
+
return this._applyAdapt(new this(message, { cause: error }));
|
|
208
209
|
}
|
|
209
210
|
static _extractMessage(error) {
|
|
210
211
|
return (typeof error === "string" ? error : typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0) || "Unknown error";
|
|
211
212
|
}
|
|
212
|
-
static
|
|
213
|
+
static _useWithPlugin(plugin) {
|
|
213
214
|
const Base = this;
|
|
214
215
|
const Error0Extended = class Error0 extends Base {
|
|
215
216
|
};
|
|
216
|
-
Error0Extended.
|
|
217
|
-
const resolved = Error0Extended.
|
|
217
|
+
Error0Extended._plugins = [...Base._plugins, plugin];
|
|
218
|
+
const resolved = Error0Extended._getResolvedPlugin();
|
|
218
219
|
for (const [key, method] of Object.entries(resolved.methods)) {
|
|
219
220
|
Object.defineProperty(Error0Extended.prototype, key, {
|
|
220
221
|
value: function(...args) {
|
|
@@ -235,61 +236,64 @@ class Error0 extends Error {
|
|
|
235
236
|
}
|
|
236
237
|
return Error0Extended;
|
|
237
238
|
}
|
|
238
|
-
static
|
|
239
|
-
const
|
|
239
|
+
static _pluginFromBuilder(plugin) {
|
|
240
|
+
const pluginRecord = plugin;
|
|
240
241
|
return {
|
|
241
|
-
props: { ...
|
|
242
|
-
methods: { ...
|
|
243
|
-
|
|
242
|
+
props: { ...pluginRecord._plugin.props ?? {} },
|
|
243
|
+
methods: { ...pluginRecord._plugin.methods ?? {} },
|
|
244
|
+
adapt: [...pluginRecord._plugin.adapt ?? []]
|
|
244
245
|
};
|
|
245
246
|
}
|
|
246
247
|
static prop(key, value) {
|
|
247
|
-
return this.
|
|
248
|
+
return this.use("prop", key, value);
|
|
248
249
|
}
|
|
249
250
|
static method(key, value) {
|
|
250
|
-
return this.
|
|
251
|
+
return this.use("method", key, value);
|
|
251
252
|
}
|
|
252
|
-
static
|
|
253
|
-
return this.
|
|
253
|
+
static adapt(value) {
|
|
254
|
+
return this.use("adapt", value);
|
|
254
255
|
}
|
|
255
|
-
static
|
|
256
|
-
if (first instanceof
|
|
257
|
-
return this.
|
|
256
|
+
static use(first, key, value) {
|
|
257
|
+
if (first instanceof PluginError0) {
|
|
258
|
+
return this._useWithPlugin(this._pluginFromBuilder(first));
|
|
258
259
|
}
|
|
259
|
-
if (first === "
|
|
260
|
+
if (first === "adapt") {
|
|
260
261
|
if (typeof key !== "function") {
|
|
261
|
-
throw new Error('Error0.
|
|
262
|
+
throw new Error('Error0.use("adapt", value) requires adapt function');
|
|
262
263
|
}
|
|
263
|
-
return this.
|
|
264
|
-
|
|
264
|
+
return this._useWithPlugin({
|
|
265
|
+
adapt: [key]
|
|
265
266
|
});
|
|
266
267
|
}
|
|
267
268
|
if (typeof key !== "string" || value === void 0) {
|
|
268
|
-
throw new Error("Error0.
|
|
269
|
+
throw new Error("Error0.use(kind, key, value) requires key and value");
|
|
269
270
|
}
|
|
270
271
|
if (first === "prop") {
|
|
271
|
-
return this.
|
|
272
|
+
return this._useWithPlugin({
|
|
272
273
|
props: { [key]: value }
|
|
273
274
|
});
|
|
274
275
|
}
|
|
275
|
-
return this.
|
|
276
|
+
return this._useWithPlugin({
|
|
276
277
|
methods: { [key]: value }
|
|
277
278
|
});
|
|
278
279
|
}
|
|
279
|
-
static
|
|
280
|
-
return new
|
|
280
|
+
static plugin() {
|
|
281
|
+
return new PluginError0();
|
|
281
282
|
}
|
|
282
283
|
static serialize(error, isPublic = true) {
|
|
283
284
|
const error0 = this.from(error);
|
|
284
285
|
const json = {
|
|
285
286
|
name: error0.name,
|
|
286
287
|
message: error0.message
|
|
287
|
-
// we do not serialize causes, it is enough that we have floated props and
|
|
288
|
+
// we do not serialize causes, it is enough that we have floated props and adapt helper
|
|
288
289
|
// cause: error0.cause,
|
|
289
290
|
};
|
|
290
|
-
const
|
|
291
|
-
const propsEntries = Object.entries(
|
|
291
|
+
const plugin = this._getResolvedPlugin();
|
|
292
|
+
const propsEntries = Object.entries(plugin.props);
|
|
292
293
|
for (const [key, prop] of propsEntries) {
|
|
294
|
+
if (prop.serialize === false) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
293
297
|
try {
|
|
294
298
|
const value = prop.resolve({ value: error0.own(key), flow: error0.flow(key), error: error0 });
|
|
295
299
|
const jsonValue = prop.serialize({ value, error: error0, isPublic });
|
|
@@ -312,6 +316,6 @@ class Error0 extends Error {
|
|
|
312
316
|
}
|
|
313
317
|
export {
|
|
314
318
|
Error0,
|
|
315
|
-
|
|
319
|
+
PluginError0
|
|
316
320
|
};
|
|
317
321
|
//# sourceMappingURL=index.js.map
|