-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrequest.ts
More file actions
29 lines (23 loc) · 815 Bytes
/
request.ts
File metadata and controls
29 lines (23 loc) · 815 Bytes
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
import { atom } from 'nanostores'
export interface RequestMethod {
(url: string, opts?: RequestInit): Promise<Response>
}
export let request: RequestMethod
export function setRequestMethod(method: RequestMethod): void {
request = method
}
export let proxyDebug = atom<((headers: Headers) => void) | false>(false)
export function setProxyAsRequestMethod(proxyUrl: string): void {
setRequestMethod(async (url, opts = {}) => {
let debug = proxyDebug.get()
if (debug) {
let headers = new Headers(opts.headers)
headers.set('x-slowreader-debug', '1')
opts = { ...opts, headers }
}
let response = await fetch(proxyUrl + encodeURIComponent(url), opts)
if (debug) debug(response.headers)
Object.defineProperty(response, 'url', { value: url })
return response
})
}