catch-match 2.0.1 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/src/main.ts CHANGED
@@ -1,71 +1,124 @@
1
- export type ResultType<Rsult, ErrorType> = {
2
- result?: Rsult;
3
- error?: ErrorType;
4
- };
1
+ export type ErrrorHandler<ErrorType> = (error?: ErrorType) => void
5
2
 
6
- export type ErrrorHandler<ErrorType> = (error?: ErrorType) => void;
3
+ export type FinallyCallback<Result, ErrorType = any> = (params: ResultType<Result, ErrorType>) => void
7
4
 
8
- export type FinallyCallback<Result, ErrorType> = ((params: ResultType<Result, ErrorType>) => Result | void | undefined);
5
+ export type TryBody<Result> = () => Result | Promise<Result> | never
9
6
 
10
- export type TryReturn<Result, ErrorType = any> = {
11
- catch: (error: ErrorType | ErrorType[], handler: ErrrorHandler<ErrorType>) => TryReturn<Result, ErrorType>;
12
- other: (handler: ErrrorHandler<ErrorType>) => Pick<TryReturn<Result, ErrorType>, 'finally'>,
13
- finally: (callback?: FinallyCallback<Result, ErrorType>) => ResultType<Result, ErrorType>,
14
- result: Result,
15
- error: ErrorType
7
+ export type ResultType<Result, ErrorType = any> = {
8
+ value?: Result;
9
+ error?: ErrorType;
16
10
  }
17
11
 
18
- export type TryBody<Result> = () => Result | never;
12
+ export type FinallyReturn<Return, ErrorType = any> =
13
+ ResultType<Return, ErrorType>
14
+
15
+ export type OtherReturn<Return, ErrorType = any> =
16
+ FinallyReturn<Return, ErrorType> &
17
+ {
18
+ finally: (callback: FinallyCallback<Return, ErrorType>) => ResultType<Return, ErrorType>
19
+ }
20
+
21
+ export type CatchReturn<Return, ErrorType = any> =
22
+ FinallyReturn<Return, ErrorType> &
23
+ OtherReturn<Return, ErrorType> &
24
+ {
25
+ catch: (error: ErrorType | ErrorType[], handler: ErrrorHandler<ErrorType>) => PromisedTryReturn<Return>;
26
+ other: (handler: ErrrorHandler<ErrorType>) => Pick<PromisedTryReturn<Return>, 'finally' | 'value' | 'error'>,
27
+ }
28
+
29
+ export type PromisedTryReturn<Return> =
30
+ CatchReturn<Return> |
31
+ (Promise<ResultType<Return>> & CatchReturn<Return>)
19
32
 
20
- export function $try<Return>(body: TryBody<Return>): TryReturn<Return> {
33
+ export function $try<Return>(body: TryBody<Return>): PromisedTryReturn<Return> {
21
34
  let caught = false;
22
- let error: any | { constructor?: any; };
23
- let result: Return | any;
24
- const isSameInstance = (e1: any) => e1 === error || e1 === error?.constructor;
35
+ let error: any | { constructor?: any; } | Promise<any | { constructor?: any; }>;
36
+ let bodyResponse: Return | Promise<Return>;
37
+ let result: PromisedTryReturn<Return>;
25
38
 
26
- try {
27
- result = body();
28
- } catch (e) {
29
- error = e;
39
+ const isSameError = (e1: any, error: any) => e1 === error || e1 === error?.constructor;
40
+
41
+ function handleErrors(err: any[], err2: any, handler: { (error?: any): void; (error?: any): void; (): any }) {
42
+ if (isSameError(err, err2) || (Array.isArray(err) && err.some(e => isSameError(e, err2)))) {
43
+ handler(err2);
44
+ caught = true;
45
+ }
46
+ }
47
+
48
+ function buildResult(br: typeof bodyResponse): ResultType<Return> | Promise<ResultType<Return>> {
49
+ if (br instanceof Promise) {
50
+ const bodyPromise = br as Promise<Return>;
51
+ return new Promise<ResultType<Return>>(async (resolve) => {
52
+ bodyPromise.then((response: any) => {
53
+ resolve({
54
+ value: response,
55
+ error: undefined,
56
+ });
57
+ }).catch((e: any) => {
58
+ resolve({
59
+ value: undefined,
60
+ error: e,
61
+ });
62
+ });
63
+ });
64
+ } else {
65
+ return {
66
+ value: br,
67
+ error: undefined,
68
+ };
69
+ }
30
70
  }
31
71
 
32
- const chain: TryReturn<typeof result, typeof error> = {
72
+ const chain: CatchReturn<Return, typeof error> = {
33
73
  catch: (err, handler) => {
34
- if (!err || !handler) {
35
- return chain;
74
+ if (result instanceof Promise) {
75
+ result.then((response) => {
76
+ typeof response.error !== 'undefined' && !caught && handleErrors(err, response.error, handler);
77
+ });
78
+ } else {
79
+ error && !caught && handleErrors(err, error, handler);
36
80
  }
37
- if (isSameInstance(err)) {
38
- handler && handler();
39
- caught = true;
40
- } else if (Array.isArray(err) && err.some(isSameInstance)) {
41
- handler && handler();
42
- caught = true;
43
- }
44
- return chain;
81
+ return result;
45
82
  },
46
83
 
47
- other: (handler) => {
48
- if (!caught && error) {
49
- handler && handler(error);
84
+ other: (callback) => {
85
+ if (result instanceof Promise) {
86
+ result.then((response) => {
87
+ typeof response.error !== 'undefined' && !caught && callback(response.error);
88
+ });
89
+ } else {
90
+ typeof result.error !== 'undefined' && !caught && callback && callback(result.error);
50
91
  }
51
- return {
52
- finally: chain.finally,
53
- result,
54
- error,
55
- };
92
+ return result;
56
93
  },
57
94
 
58
- finally: (callback?) => {
59
- return {
60
- result: callback ? callback({ result, error }) : result,
61
- error,
62
- };
95
+ finally: (callback) => {
96
+ if (result instanceof Promise) {
97
+ result.then((response) => {
98
+ callback(response);
99
+ });
100
+ } else {
101
+ callback({
102
+ value: result.value,
103
+ error: result.error,
104
+ });
105
+ }
106
+ return result;
63
107
  },
64
- result,
65
- error,
66
108
  };
67
109
 
68
- return chain;
110
+ try {
111
+ bodyResponse = body();
112
+ result = Object.assign(buildResult(bodyResponse), chain);
113
+ } catch (e) {
114
+ error = e;
115
+ result = Object.assign(chain, {
116
+ value: undefined,
117
+ error: e,
118
+ });
119
+ }
120
+
121
+ return result;
69
122
  }
70
123
 
71
124
  export default $try;