-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathfetchObservable.ts
126 lines (106 loc) · 3.22 KB
/
fetchObservable.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
import type { InstrumentedMethodCall } from '../tools/instrumentMethod'
import { instrumentMethod } from '../tools/instrumentMethod'
import { monitor } from '../tools/monitor'
import { Observable } from '../tools/observable'
import { assign } from '../tools/utils/polyfills'
import type { ClocksState } from '../tools/utils/timeUtils'
import { clocksNow } from '../tools/utils/timeUtils'
import { normalizeUrl } from '../tools/utils/urlPolyfill'
interface FetchContextBase {
method: string
startClocks: ClocksState
input: unknown
init?: RequestInit
url: string
handlingStack?: string
}
export interface FetchStartContext extends FetchContextBase {
state: 'start'
}
export interface FetchResolveContext extends FetchContextBase {
state: 'resolve'
status: number
response?: Response
responseType?: string
isAborted: boolean
error?: Error
}
export type FetchContext = FetchStartContext | FetchResolveContext
let fetchObservable: Observable<FetchContext> | undefined
export function initFetchObservable() {
if (!fetchObservable) {
fetchObservable = createFetchObservable()
}
return fetchObservable
}
export function resetFetchObservable() {
fetchObservable = undefined
}
function createFetchObservable() {
return new Observable<FetchContext>((observable) => {
if (!window.fetch) {
return
}
const { stop } = instrumentMethod(window, 'fetch', (call) => beforeSend(call, observable), {
computeHandlingStack: true,
})
return stop
})
}
function beforeSend(
{ parameters, onPostCall, handlingStack }: InstrumentedMethodCall<Window, 'fetch'>,
observable: Observable<FetchContext>
) {
const [input, init] = parameters
let methodFromParams = init && init.method
if (methodFromParams === undefined && input instanceof Request) {
methodFromParams = input.method
}
const method = methodFromParams !== undefined ? String(methodFromParams).toUpperCase() : 'GET'
const url = input instanceof Request ? input.url : normalizeUrl(String(input))
const startClocks = clocksNow()
const context: FetchStartContext = {
state: 'start',
init,
input,
method,
startClocks,
url,
handlingStack,
}
observable.notify(context)
// Those properties can be changed by observable subscribers
parameters[0] = context.input as RequestInfo | URL
parameters[1] = context.init
onPostCall((responsePromise) => afterSend(observable, responsePromise, context))
}
function afterSend(
observable: Observable<FetchContext>,
responsePromise: Promise<Response>,
startContext: FetchStartContext
) {
const context = startContext as unknown as FetchResolveContext
function reportFetch(partialContext: Partial<FetchResolveContext>) {
context.state = 'resolve'
assign(context, partialContext)
observable.notify(context)
}
responsePromise.then(
monitor((response) => {
reportFetch({
response,
responseType: response.type,
status: response.status,
isAborted: false,
})
}),
monitor((error: Error) => {
reportFetch({
status: 0,
isAborted:
context.init?.signal?.aborted || (error instanceof DOMException && error.code === DOMException.ABORT_ERR),
error,
})
})
)
}