Skip to content

Commit ecfc94f

Browse files
committed
refactor response create function outside of promise handler
not sure that this is favorable or not but thought that it might be and it allows for easier testing of the creation of responses and their properties.
1 parent c5bdee7 commit ecfc94f

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
npm-debug.log
44
.DS_Store
55
/polyfill/index.js
6+
coverage

src/index.mjs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1+
const regex = /^(.*?):[^\S\n]*([\s\S]*?)$/gm;
2+
const response = (request, headers) => ({
3+
ok: (request.status/100|0) == 2, // 200-299
4+
statusText: request.statusText,
5+
status: request.status,
6+
url: request.responseURL,
7+
text: () => Promise.resolve(request.responseText),
8+
json: () => Promise.resolve(JSON.parse(request.responseText)),
9+
blob: () => Promise.resolve(new Blob([request.response])),
10+
clone: () => response(request, headers),
11+
headers: {
12+
keys: () => headers.keys,
13+
entries: () => headers.all,
14+
get: n => headers.raw[n.toLowerCase()],
15+
has: n => n.toLowerCase() in headers.raw
16+
}
17+
});
18+
119
export default function(url, options) {
220
options = options || {};
321
return new Promise( (resolve, reject) => {
422
const request = new XMLHttpRequest();
5-
const keys = [];
6-
const all = [];
7-
const headers = {};
8-
9-
const response = () => ({
10-
ok: (request.status/100|0) == 2, // 200-299
11-
statusText: request.statusText,
12-
status: request.status,
13-
url: request.responseURL,
14-
text: () => Promise.resolve(request.responseText),
15-
json: () => Promise.resolve(JSON.parse(request.responseText)),
16-
blob: () => Promise.resolve(new Blob([request.response])),
17-
clone: response,
18-
headers: {
19-
keys: () => keys,
20-
entries: () => all,
21-
get: n => headers[n.toLowerCase()],
22-
has: n => n.toLowerCase() in headers
23-
}
24-
});
2523

2624
request.open(options.method || 'get', url, true);
2725

2826
request.onload = () => {
29-
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
30-
keys.push(key = key.toLowerCase());
31-
all.push([key, value]);
32-
headers[key] = headers[key] ? `${headers[key]},${value}` : value;
27+
const head = { all: [], keys: [], raw: {} };
28+
request.getAllResponseHeaders().replace(regex, (m, key, value) => {
29+
head.all.push([key, value]);
30+
head.keys.push(key = key.toLowerCase());
31+
head.raw[key] = head.raw[key] ? `${head.raw[key]},${value}` : value;
3332
});
34-
resolve(response());
33+
resolve(response(request, head));
3534
};
3635

3736
request.onerror = reject;
@@ -45,3 +44,5 @@ export default function(url, options) {
4544
request.send(options.body || null);
4645
});
4746
}
47+
48+
export { response };

test/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from '../src/index.mjs';
1+
import fetch, { response } from '../src/index.mjs';
22
import fetchDist from '..';
33

44
describe('unfetch', () => {
@@ -81,4 +81,33 @@ describe('unfetch', () => {
8181
return p;
8282
});
8383
});
84+
85+
describe('response()', () => {
86+
it('returns text()', () => response({ responseText: 'A passing test.' })
87+
.text()
88+
.then((text) => expect(text).toBe('A passing test.'))
89+
);
90+
91+
it('returns blob()', () => response({ response: 'A passing test.' })
92+
.blob()
93+
.then((text) => expect(text.toString()).toBe(new Blob(['A passing test.']).toString()))
94+
);
95+
96+
it('returns headers', () => {
97+
const all = [['x-foo', 'bar'], ['x-baz', 'boo']];
98+
const result = response({}, { all }).headers.entries();
99+
expect(result).toEqual(all);
100+
});
101+
102+
it('returns header keys', () => {
103+
const result = response({}, { keys: ['x-foo'] }).headers.keys();
104+
expect(result).toEqual(['x-foo']);
105+
});
106+
107+
it('returns headers has', () => {
108+
const raw = { 'x-foo': 'bar', 'x-baz': 'boo' };
109+
const test = response({}, { raw }).headers;
110+
expect(test.has('x-foo')).toBe(true);
111+
});
112+
});
84113
});

0 commit comments

Comments
 (0)