generated from ehmicky/template-javascript
/
main.d.ts
384 lines (363 loc) · 10.5 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Any JSON value.
*/
type JSONValue =
| null
| boolean
| number
| string
| JSONValue[]
| { [key: string]: JSONValue }
interface MinimalErrorObject {
name: string
message: string
stack: string
[key: PropertyKey]: JSONValue
}
/**
* Error instance converted to a plain object
*/
export interface ErrorObject extends MinimalErrorObject {
cause?: ErrorObject
errors?: ErrorObject[]
}
/**
* `error-serializer` `serialize()` options
*/
export interface SerializeOptions {
/**
* Unless this option is `true`, nested errors are also serialized.
* They can be inside other errors, plain objects or arrays.
*
* @default false
*
* @example
* ```js
* const error = new Error('example')
* error.inner = new Error('inner')
* serialize(error).inner // { name: 'Error', message: 'inner', ... }
* serialize(error, { shallow: true }).inner // Error: inner ...
* ```
*/
readonly shallow?: boolean
/**
* By default, when the argument is not an `Error` instance, it is converted
* to one. If this option is `true`, it is kept as is instead.
*
* @default false
*
* @example
* ```js
* serialize('example') // { name: 'Error', message: 'example', ... }
* serialize('example', { loose: true }) // 'example'
* ```
*/
readonly loose?: boolean
/**
* Called before serializing each `errorInstance`.
*
* @example
* ```js
* const errors = [new Error('test')]
* errors[0].date = new Date()
*
* const errorObjects = serialize(errors, {
* // Serialize `Date` instances as strings
* beforeSerialize(error) {
* error.date = error.date.toString()
* },
* // Restore `error.date` after serializing it
* afterSerialize(error, errorObject) {
* error.date = new Date(error.date)
* },
* })
* console.log(errorObjects[0].date) // Date string
*
* const newErrors = parse(errorObjects, {
* // Parse date strings as `Date` instances
* beforeParse(errorObject) {
* errorObject.date = new Date(errorObject.date)
* },
* // Restore `errorObject.date` after parsing
* afterParse(errorObject, error) {
* errorObject.date = errorObject.date.toString()
* },
* })
* console.log(newErrors[0].date) // `Date` instance
* ```
*/
readonly beforeSerialize?: (errorInstance: Error) => void
/**
* Called after serializing each `errorInstance`.
*
* @example
* ```js
* const errors = [new Error('test')]
* errors[0].date = new Date()
*
* const errorObjects = serialize(errors, {
* loose: true,
* // Serialize `Date` instances as strings
* beforeSerialize(error) {
* error.date = error.date.toString()
* },
* // Restore `error.date` after serializing it
* afterSerialize(error, errorObject) {
* error.date = new Date(error.date)
* },
* })
* console.log(errorObjects[0].date) // Date string
*
* const newErrors = parse(errorObjects, {
* loose: true,
* // Parse date strings as `Date` instances
* beforeParse(errorObject) {
* errorObject.date = new Date(errorObject.date)
* },
* // Restore `errorObject.date` after parsing
* afterParse(errorObject, error) {
* errorObject.date = errorObject.date.toString()
* },
* })
* console.log(newErrors[0].date) // `Date` instance
* ```
*/
readonly afterSerialize?: (
errorInstance: Error,
errorObject: ErrorObject,
) => void
}
/**
* Convert an `Error` instance into a plain object.
*
* @example
* ```js
* const error = new TypeError('example')
* const errorObject = serialize(error)
* // Plain object: { name: 'TypeError', message: 'example', stack: '...' }
*
* const errorString = JSON.stringify(errorObject)
* const newErrorObject = JSON.parse(errorString)
*
* const newError = parse(newErrorObject)
* // Error instance: 'TypeError: example ...'
* ```
*/
export function serialize<Value, Options extends SerializeOptions = {}>(
errorInstance: Value,
options?: Options,
): Options['shallow'] extends true
? SerializeShallow<SerializeNormalize<Value, Options>>
: SerializeDeep<SerializeNormalize<Value, Options>>
type SerializeNormalize<
Value,
Options extends SerializeOptions,
> = Options['loose'] extends true ? Value : Value extends Error ? Value : Error
type SerializeShallow<Value> = Value extends Error
? ErrorObject & {
[Key in keyof Value as Value[Key] extends ErrorObject[Key]
? Key
: never]: Value[Key]
}
: Value
type SerializeDeep<Value> = Value extends Error
? ErrorObject & {
[Key in keyof Value as SerializeDeep<Value[Key]> extends ErrorObject[Key]
? Key
: never]: SerializeDeep<Value[Key]>
}
: Value extends object
? { [Key in keyof Value]: SerializeDeep<Value[Key]> }
: Value
// `Error` is both a `CallableFunction` and a `NewableFunction`, which makes
// `typeof Error` not work as expected.
type ErrorClass = new (message: string) => Error
/**
* `error-serializer` `parse()` options
*/
export interface ParseOptions {
/**
* Unless this option is `true`, nested error plain objects are also parsed.
* They can be inside other errors, plain objects or arrays.
*
* @default false
*
* @example
* ```js
* const error = new Error('example')
* error.inner = new Error('inner')
* const errorObject = serialize(error)
*
* parse(errorObject).inner // Error: inner ...
* parse(errorObject, { shallow: true }).inner // { name: 'Error', message: ... }
* ```
*/
readonly shallow?: boolean
/**
* By default, when the argument is not an error plain object, it is
* converted to one. If this option is `true`, it is kept as is instead.
*
* @default false
*
* @example
* ```js
* parse('example') // Error: example
* parse('example', { loose: true }) // 'example'
* ```
*/
readonly loose?: boolean
/**
* Custom error classes to keep when parsing.
*
* - Each key is an `errorObject.name`
* - Each value is the error class to use
*
* @example
* ```js
* const errorObject = serialize(new CustomError('example'))
* // `CustomError` class is kept
* const error = parse(errorObject, { classes: { CustomError } })
* // Map `CustomError` to another class
* const otherError = parse(errorObject, { classes: { CustomError: TypeError } })
* ```
*/
readonly classes?: { [ErrorClassName: string]: ErrorClass }
/**
* Called before parsing each `errorObject`.
*
* @example
* ```js
* const errors = [new Error('test')]
* errors[0].date = new Date()
*
* const errorObjects = serialize(errors, {
* loose: true,
* // Serialize `Date` instances as strings
* beforeSerialize(error) {
* error.date = error.date.toString()
* },
* // Restore `error.date` after serializing it
* afterSerialize(error, errorObject) {
* error.date = new Date(error.date)
* },
* })
* console.log(errorObjects[0].date) // Date string
*
* const newErrors = parse(errorObjects, {
* loose: true,
* // Parse date strings as `Date` instances
* beforeParse(errorObject) {
* errorObject.date = new Date(errorObject.date)
* },
* // Restore `errorObject.date` after parsing
* afterParse(errorObject, error) {
* errorObject.date = errorObject.date.toString()
* },
* })
* console.log(newErrors[0].date) // `Date` instance
* ```
*/
readonly beforeParse?: (errorObject: MinimalErrorObject) => void
/**
* Called after parsing each `errorObject`.
*
* @example
* ```js
* const errors = [new Error('test')]
* errors[0].date = new Date()
*
* const errorObjects = serialize(errors, {
* loose: true,
* // Serialize `Date` instances as strings
* beforeSerialize(error) {
* error.date = error.date.toString()
* },
* // Restore `error.date` after serializing it
* afterSerialize(error, errorObject) {
* error.date = new Date(error.date)
* },
* })
* console.log(errorObjects[0].date) // Date string
*
* const newErrors = parse(errorObjects, {
* loose: true,
* // Parse date strings as `Date` instances
* beforeParse(errorObject) {
* errorObject.date = new Date(errorObject.date)
* },
* // Restore `errorObject.date` after parsing
* afterParse(errorObject, error) {
* errorObject.date = errorObject.date.toString()
* },
* })
* console.log(newErrors[0].date) // `Date` instance
* ```
*/
readonly afterParse?: (
errorObject: MinimalErrorObject,
errorInstance: Error,
) => void
}
/**
* Convert an error plain object into an `Error` instance.
*
* @example
* ```js
* const error = new TypeError('example')
* const errorObject = serialize(error)
* // Plain object: { name: 'TypeError', message: 'example', stack: '...' }
*
* const errorString = JSON.stringify(errorObject)
* const newErrorObject = JSON.parse(errorString)
*
* const newError = parse(newErrorObject)
* // Error instance: 'TypeError: example ...'
* ```
*/
export function parse<Value, Options extends ParseOptions = {}>(
errorObject: Value,
options?: Options,
): Options['shallow'] extends true
? ParseNormalize<ParseShallow<Value, Options>, Options>
: ParseNormalize<ParseDeep<Value, Options>, Options>
type ParseNormalize<
Value,
Options extends ParseOptions,
> = Options['loose'] extends true ? Value : Value extends Error ? Value : Error
type ParseShallow<
Value,
Options extends ParseOptions,
> = Value extends MinimalErrorObject
? ParsedError<Value, Options> & {
[Key in keyof Value as Value[Key] extends (
Key extends keyof ParsedError<Value, Options>
? ParsedError<Value, Options>[Key]
: unknown
)
? Key
: never]: Value[Key]
}
: Value
type ParseDeep<
Value,
Options extends ParseOptions,
> = Value extends MinimalErrorObject
? ParsedError<Value, Options> & {
[Key in keyof Value as Key extends keyof ParsedError<Value, Options>
? ParseDeep<Value[Key], Options> extends ParsedError<
Value,
Options
>[Key]
? Key
: never
: Key]: ParseDeep<Value[Key], Options>
}
: Value extends object
? { [Key in keyof Value]: ParseDeep<Value[Key], Options> }
: Value
type ParsedError<
ErrorObjectArg extends MinimalErrorObject,
Options extends ParseOptions,
> = NonNullable<Options['classes']>[ErrorObjectArg['name']] extends ErrorClass
? InstanceType<NonNullable<Options['classes']>[ErrorObjectArg['name']]>
: Error