forked from terrysahaidak/fetch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.js
189 lines (156 loc) · 4.16 KB
/
fetch.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(function() {
'use strict';
if (window.fetch) {
return
}
function Headers(headers) {
this.map = {}
if (headers instanceof Headers) {
headers.forEach(function(name, values) {
values.forEach(function(value) {
this.append(name, value)
})
})
} else if (headers) {
Object.getOwnPropertyNames(headers).forEach(function(name) {
this.append(name, headers[name])
})
}
}
Headers.prototype.append = function(name, value) {
var list = this.map[name]
if (!list) {
list = []
this.map[name] = list
}
list.push(value)
}
Headers.prototype.delete = function(name) {
delete this.map[name]
}
Headers.prototype.get = function(name) {
var values = this.map[name]
return values ? values[0] : null
}
Headers.prototype.getAll = function(name) {
return this.map[name] || []
}
Headers.prototype.has = function(name) {
return this.map.hasOwnProperty(name)
}
Headers.prototype.set = function(name, value) {
this.map[name] = [value]
}
// Instead of iterable for now.
Headers.prototype.forEach = function(callback) {
Object.getOwnPropertyNames(this.map).forEach(function(name) {
callback(name, this.map[name])
})
}
function Body() {
this.body = null
this.bodyUsed = false
this.arrayBuffer = function() {
throw 'Not implemented yet'
}
this.blob = function() {
return Promise.resolve(new Blob([this.body]))
}
this.formData = function() {
throw 'Not implemented yet'
}
this.json = function() {
var body = this.body
return new Promise(function(resolve, reject) {
try {
resolve(JSON.parse(body))
} catch (ex) {
reject(ex)
}
})
}
this.text = function() {
return Promise.resolve(this.body)
}
return this
}
function Request(url, options) {
options = options || {}
this.url = url
this.body = options.body
this.credentials = options.credentials || null
this.headers = new Headers(options.headers)
this.method = options.method || 'GET'
this.mode = options.mode || null
this.referrer = null
}
function encode(params) {
var pairs = Object.getOwnPropertyNames(params).map(function(name) {
var key = encodeURIComponent(name)
var value = encodeURIComponent(params[name])
return [key, value].join('&')
})
return pairs.join('&')
}
function isObject(value) {
try {
return Object.getPrototypeOf(value) === Object.prototype
} catch (ex) {
// Probably a string literal.
return false
}
}
function headers(xhr) {
var headers = new Headers()
var pairs = xhr.getAllResponseHeaders().trim().split("\n")
pairs.forEach(function(header) {
var split = header.trim().split(':')
var key = split.shift().trim()
var value = split.join(':').trim()
headers.append(key, value)
})
return headers
}
Request.prototype.fetch = function() {
var self = this
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest()
xhr.onload = function() {
var options = {
status: xhr.status,
statusText: xhr.statusText,
headers: headers(xhr)
}
resolve(new Response(xhr.responseText, options))
}
xhr.onerror = function() {
reject()
}
xhr.open(self.method, self.url)
self.headers.forEach(function(name, values) {
values.forEach(function(value) {
xhr.setRequestHeader(name, value)
})
})
var body = self.body
if (isObject(self.body)) {
xhr.setRequestHeader('application/x-www-form-urlencoded;charset=UTF-8')
body = encode(self.body)
}
xhr.send(body)
})
}
Body.call(Request.prototype)
function Response(body, options) {
this.body = body
this.type = 'default'
this.url = null
this.status = options.status
this.statusText = options.statusText
this.headers = options.headers
}
Body.call(Response.prototype)
window.fetch = function (url, options) {
return new Request(url, options).fetch()
}
})()