generated from ehmicky/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d.ts
113 lines (108 loc) · 3.79 KB
/
main.d.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { ErrorObject } from 'error-serializer'
import type { Info, ErrorInstance } from 'modern-errors'
export type { ErrorObject }
/**
* `modern-errors-serialize` plugin.
*
* This plugin adds `BaseError.toJSON()`, `BaseError.fromJSON()`,
* `BaseError.serialize()` and `BaseError.parse()` to serialize/parse errors
* to/from plain objects.
*/
declare const plugin: {
name: 'serialize'
instanceMethods: {
/**
* Converts `error` to an error plain object that is
* [serializable](https://github.com/ehmicky/error-serializer#json-safety)
* to JSON
* ([or YAML](https://github.com/ehmicky/error-serializer#custom-serializationparsing),
* etc.). All
* [error properties](https://github.com/ehmicky/error-serializer#additional-error-properties)
* are kept.
* [Plugin options](https://github.com/ehmicky/modern-errors#plugin-options)
* are also preserved.
*
* Nested error instances are serialized deeply. If `error` is not an error
* instance, it is first
* [normalized](https://github.com/ehmicky/modern-errors#invalid-errors) to
* one.
*
* This is also set as
* [`error.toJSON()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior).
* Therefore
* [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
* automatically calls it.
*
* @example
* ```js
* const error = new InputError('Wrong file.', { props: { filePath } })
* const errorObject = BaseError.toJSON(error)
* // { name: 'InputError', message: 'Wrong file', stack: '...', filePath: '...' }
* const errorString = JSON.stringify(errorObject)
* // '{"name":"InputError",...}'
* ```
*/
toJSON: (info: Info['instanceMethods']) => ErrorObject
}
staticMethods: {
/**
* Converts `errorObject` to an error instance.
* The original error classes are preserved.
*
* Nested error plain objects are parsed deeply. If `errorObject` is not an
* error plain object, it is first normalized to one.
*
* @example
* ```js
* const newErrorObject = JSON.parse(errorString)
* const newError = BaseError.fromJSON(newErrorObject)
* // InputError: Wrong file.
* // at ...
* // filePath: '...'
* ```
*/
fromJSON: (
info: Info['staticMethods'],
errorObject: unknown,
) => ErrorInstance
/**
* This is like `BaseError.toJSON(value)` except, if `value` is not an error
* instance, it is kept as is. However, any nested error instances is still
* serialized.
*
* @example
* ```js
* const error = new InputError('Wrong file.')
* const deepArray = [{}, { error }]
*
* const jsonString = JSON.stringify(BaseError.serialize(deepArray))
* const newDeepArray = JSON.parse(jsonString)
*
* const newError = BaseError.parse(newDeepArray)[1].error
* // InputError: Wrong file.
* // at ...
* ```
*/
serialize: (info: Info['staticMethods'], error: unknown) => unknown
/**
* This is like `BaseError.fromJSON(value)` except, if `value` is not an
* error plain object, it is kept as is. However, any nested error plain
* object is still parsed.
*
* @example
* ```js
* const error = new InputError('Wrong file.')
* const deepArray = [{}, { error }]
*
* const jsonString = JSON.stringify(BaseError.serialize(deepArray))
* const newDeepArray = JSON.parse(jsonString)
*
* const newError = BaseError.parse(newDeepArray)[1].error
* // InputError: Wrong file.
* // at ...
* ```
*/
parse: (info: Info['staticMethods'], errorObject: unknown) => unknown
}
}
export default plugin