-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
147 lines (124 loc) · 3.4 KB
/
model.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
import { BodyDecoder } from './codec'
export class HolMetadata {
private readonly metadata = new Map<symbol, any>()
get<T>(key: HolMetadataKey<T>): T | undefined {
return this.metadata.get(key.symbol) as T | undefined
}
put<T>(key: HolMetadataKey<T>, value: T): T | undefined {
const old = this.metadata.get(key.symbol)
this.metadata.set(key.symbol, value)
return old
}
compute<T>(key: HolMetadataKey<T>, compute: (value: T | undefined) => T) {
this.put(key, compute(this.get(key)))
}
putAll(other: HolMetadata) {
for (let [symbol, value] of other.metadata.entries()) {
this.metadata.set(symbol, value)
}
}
merge(other: HolMetadata): HolMetadata {
const out = this.clone()
out.putAll(other)
return out
}
clone(): HolMetadata {
const out = new HolMetadata()
out.putAll(this)
return out
}
entries(): IterableIterator<[symbol, any]> {
return this.metadata.entries()
}
}
export class HolMetadataKey<T> {
readonly symbol: symbol
constructor(description: string) {
this.symbol = Symbol(description)
}
cast(value: any): T {
return value as T
}
}
export interface HolResponse {
response: Response
metadata: HolMetadata
get statusCode(): number
get successful(): boolean
get clientError(): boolean
get serverError(): boolean
get headers(): Headers
body<T>(decoder: BodyDecoder<T>): Promise<T>
}
export class HolError implements Error {
name: string
message: string
stack?: string
cause: any
metadata: HolMetadata
constructor(error: any, metadata: HolMetadata) {
if (error instanceof HolError) {
this.name = error.name
this.message = error.message
this.stack = error.stack
this.cause = error.cause
this.metadata = error.metadata.merge(metadata)
} else if (typeof error === 'string') {
this.name = error
this.message = error
this.stack = undefined
this.cause = error
this.metadata = metadata
} else {
this.name = error?.name ?? 'unknown'
this.message = error?.message ?? ''
this.stack = error?.stack
this.cause = error
this.metadata = metadata
}
}
static rethrowWithMetadata<T>(error: any, metadata: HolMetadata): never {
if (error instanceof HolError) {
error.metadata.putAll(metadata)
throw error
} else {
throw new HolError(error, metadata)
}
}
}
export type HolInput = RequestInfo | URL
export class HolRequest {
input: HolInput
init: RequestInit
readonly metadata: HolMetadata
constructor(input: HolInput, init?: RequestInit, metadata?: HolMetadata) {
this.input = input
this.init = init ?? {}
this.metadata = metadata ?? new HolMetadata()
}
get headers() {
let headers = this.init.headers
if (!headers) {
headers = new Headers()
this.init.headers = headers;
} else if (!(headers instanceof Headers)) {
headers = new Headers(this.init.headers)
this.init.headers = headers
}
return headers;
}
clone(cloneMetadata: boolean) {
return {
...this,
init: {
...this.init,
},
metadata: cloneMetadata ? this.metadata.clone() : this.metadata,
}
}
toFetchRequest(): Request {
return new Request(this.input, this.init)
}
}
export type Hol = (request: HolRequest) => Promise<HolResponse>
export type HolFilter = (request: HolRequest, execute: Hol) => Promise<HolResponse>