-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathrequest.ts
124 lines (99 loc) · 2.81 KB
/
request.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
import { Body, cloneBody, extractContentType, getInstanceBody } from './body'
import { Headers as BaseHeaders } from './headers'
import { NextURL } from '../next-url'
import { notImplemented } from '../utils'
export const INTERNALS = Symbol('internal request')
class BaseRequest extends Body implements Request {
[INTERNALS]: {
credentials: RequestCredentials
headers: Headers
method: string
redirect: RequestRedirect
url: NextURL
}
constructor(input: BaseRequest | string, init: RequestInit = {}) {
const method = init.method?.toUpperCase() ?? 'GET'
if (
(method === 'GET' || method === 'HEAD') &&
(init.body || (input instanceof BaseRequest && getInstanceBody(input)))
) {
throw new TypeError('Request with GET/HEAD method cannot have body')
}
let inputBody: BodyInit | null = null
if (init.body) {
inputBody = init.body
} else if (input instanceof BaseRequest && getInstanceBody(input)) {
inputBody = cloneBody(input)
}
super(inputBody)
const headers = new BaseHeaders(
init.headers || getProp(input, 'headers') || {}
)
if (inputBody !== null) {
const contentType = extractContentType(this)
if (contentType !== null && !headers.has('Content-Type')) {
headers.append('Content-Type', contentType)
}
}
this[INTERNALS] = {
credentials:
init.credentials || getProp(input, 'credentials') || 'same-origin',
headers,
method,
redirect: init.redirect || getProp(input, 'redirect') || 'follow',
url: new NextURL(typeof input === 'string' ? input : input.url),
}
}
get url() {
return this[INTERNALS].url.toString()
}
get credentials() {
return this[INTERNALS].credentials
}
get method() {
return this[INTERNALS].method
}
get headers() {
return this[INTERNALS].headers
}
get redirect() {
return this[INTERNALS].redirect
}
public clone() {
return new BaseRequest(this)
}
get cache() {
return notImplemented('Request', 'cache')
}
get integrity() {
return notImplemented('Request', 'integrity')
}
get keepalive() {
return notImplemented('Request', 'keepalive')
}
get mode() {
return notImplemented('Request', 'mode')
}
get destination() {
return notImplemented('Request', 'destination')
}
get referrer() {
return notImplemented('Request', 'referrer')
}
get referrerPolicy() {
return notImplemented('Request', 'referrerPolicy')
}
get signal() {
return notImplemented('Request', 'signal')
}
get [Symbol.toStringTag]() {
return 'Request'
}
}
export { BaseRequest as Request }
function getProp<K extends keyof BaseRequest>(
input: BaseRequest | string,
key: K
): BaseRequest[K] | undefined {
return input instanceof BaseRequest ? input[key] : undefined
}