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