-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
96 lines (80 loc) · 2.43 KB
/
index.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
export type ResultKind = 'success' | 'failure' | 'informational' | 'warning' | 'error'
export interface IResult {
description: string
status: ResultKind
}
class ErrorResult implements IResult {
public description: string;
public status: ResultKind;
public details: string | Error
public constructor(description: string, details: string | Error) {
this.status = 'error'
this.description = description
this.details = details
}
}
type FailureKind = 'failed' | 'inconclusive'
class Failure implements IResult {
public details: string | Error
public description: string;
public status: ResultKind;
public constructor(kind: FailureKind, description: string, details: string | Error = '') {
this.description = description
this.status = 'failure'
this.details = details
}
}
export class Result implements IResult {
public description: string
public status: ResultKind
public details?: string
protected constructor(descrition: string, status: ResultKind, details?: string) {
this.description = descrition
this.status = status
this.details = details
}
public static Success(description = '', details?: string) {
return new Result(description, 'success', details)
}
public static Failure(reason: string, details?: string | Error): IResult {
return new Failure('failed', reason, details)
}
public static Warning(description: string, details?: string) {
return new Result(description, 'warning', details)
}
public static Informational(description: string, details?: string) {
return new Result(description, 'informational', details)
}
public static Error(description: string, details: string | Error = ''): IResult {
return new ErrorResult(description, details)
}
}
export interface Context {
[s: string]: any
}
/**
* Return type of check functions. Either results, or result will be reported
*/
export interface CheckResult<T extends Context = Context> {
/**
* Results to be reported
*/
result?: IResult
/**
* Results to be reported
*/
results?: IResult[]
/**
* Checks to add to queue
*/
// eslint-disable-next-line no-use-before-define
nextChecks?: checkChain<T>[]
/**
* If true, does not nest nextChecks
*/
sameLevel?: boolean
}
/**
* Function delegate which runs actual check. It can be asynchronous
*/
export type checkChain<T extends Context = Context> = (this: T) => Promise<CheckResult<T>> | CheckResult<T>