-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.ts
42 lines (37 loc) · 956 Bytes
/
logging.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
import {
HolFilter,
HolRequest,
HolResponse,
} from './model'
export const consoleLogger = logger({
before(request: HolRequest) {
console.log('input', request)
},
fulfilled(request: HolRequest, response: HolResponse) {
console.log('fulfilled', response)
},
failed(request: HolRequest, error: any) {
console.log('failed', error)
},
})
export interface RequestLogger {
before(request: HolRequest): void
fulfilled(request: HolRequest, response: HolResponse): void
failed(request: HolRequest, error: any): void
}
export function logger(logger: RequestLogger): HolFilter {
return function LoggingFilter(request, execute) {
logger.before(request)
return execute(request).then(
(response) => {
logger.fulfilled(request, response)
return response
},
(error) => {
logger.failed(request, error)
console.log('reject', error)
throw error
},
)
}
}