-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
75 lines (68 loc) · 1.67 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {panic as defaultPanic} from 'panicit'
export type Panic = typeof defaultPanic
export type WrapConfig = {
/**
* Set `true` to exit the program when panic is called. By default is `false`.
*
* - In browser environment, it will throw error.
* - In Node environment, it will call `process.exit()` to terminate process.
*/
panic: boolean
/**
* Customize `panic` function.
*
* This is useful when you want to do some extra logics,
* such as reporting errors or doing resource clean up before exit.
*
* By default will use `panic` from [`panicit`](https://github.com/musicq/panicit).
*/
panicFn: Panic
}
// global unwrapit config
export const wrapConfig: WrapConfig = {
panic: false,
panicFn: defaultPanic,
}
/**
* Define global config for `unwrapit`.
*
* ```ts
* import { defineUnwrapitConfig } from 'unwrapit'
*
* defineUnwrapitConfig({
* panic: true,
* panicFn: myPanicFn
* })
* ```
*/
export function defineUnwrapitConfig(config: Partial<WrapConfig>) {
if (config.panic !== undefined) {
wrapConfig.panic = !!config.panic
}
if (typeof config.panicFn === 'function') {
wrapConfig.panicFn = config.panicFn
}
}
/**
* @deprecated
* Use `defineUnwrapitConfig` instead.
*/
export const defineWrapConfig = defineUnwrapitConfig
/**
* @deprecated
* Use `defineWrapConfig` instead.
*
* ```ts
* import {wrap, setPanic, err} from 'unwrapit'
*
* setPanic((msg: string) => {
* throw new Error(msg)
* })
* const fail: Result<never, string> = err('error')
*
* fail.unwrap() // will `throw new Error('error')`
* ```
*/
export function setPanic(panic: WrapConfig['panicFn']) {
defineUnwrapitConfig({panicFn: panic})
}