Skip to content

Commit ac63b91

Browse files
committed
remove non default export to another file
1 parent ecfc94f commit ac63b91

File tree

4 files changed

+51
-48
lines changed

4 files changed

+51
-48
lines changed

src/index.mjs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1+
import response from './lib/response';
2+
13
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-
});
184

195
export default function(url, options) {
206
options = options || {};
@@ -44,5 +30,3 @@ export default function(url, options) {
4430
request.send(options.body || null);
4531
});
4632
}
47-
48-
export { response };

src/lib/response.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default function response (request, headers) {
2+
return {
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+
}

test/index.js

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

44
describe('unfetch', () => {
@@ -81,33 +81,4 @@ 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-
});
11384
});

test/lib/response.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import response from '../../src/lib/response.mjs';
2+
3+
describe('response()', () => {
4+
it('returns text()', () => response({ responseText: 'A passing test.' })
5+
.text()
6+
.then((text) => expect(text).toBe('A passing test.'))
7+
);
8+
9+
it('returns blob()', () => response({ response: 'A passing test.' })
10+
.blob()
11+
.then((text) => expect(text.toString()).toBe(new Blob(['A passing test.']).toString()))
12+
);
13+
14+
it('returns headers', () => {
15+
const all = [['x-foo', 'bar'], ['x-baz', 'boo']];
16+
const result = response({}, { all }).headers.entries();
17+
expect(result).toEqual(all);
18+
});
19+
20+
it('returns header keys', () => {
21+
const result = response({}, { keys: ['x-foo'] }).headers.keys();
22+
expect(result).toEqual(['x-foo']);
23+
});
24+
25+
it('returns headers has', () => {
26+
const raw = { 'x-foo': 'bar', 'x-baz': 'boo' };
27+
const test = response({}, { raw }).headers;
28+
expect(test.has('x-foo')).toBe(true);
29+
});
30+
});

0 commit comments

Comments
 (0)