FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor response create function outside of promise handler by kalisjoshua · Pull Request #132 · developit/unfetch · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (1) .mjs  (2) dotfile  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions .gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
npm-debug.log
.DS_Store
/polyfill/index.js
coverage
33 changes: 9 additions & 24 deletions src/index.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import response from './lib/response';

const regex = /^(.*?):[^\S\n]*([\s\S]*?)$/gm;

export default function(url, options) {
options = options || {};
return new Promise( (resolve, reject) => {
const request = new XMLHttpRequest();
const keys = [];
const all = [];
const headers = {};

const response = () => ({
ok: (request.status/100|0) == 2, // 200-299
statusText: request.statusText,
status: request.status,
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
clone: response,
headers: {
keys: () => keys,
entries: () => all,
get: n => headers[n.toLowerCase()],
has: n => n.toLowerCase() in headers
}
});

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

request.onload = () => {
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push(key = key.toLowerCase());
const all = [], keys = [], raw = {};
request.getAllResponseHeaders().replace(regex, (m, key, value) => {
all.push([key, value]);
headers[key] = headers[key] ? `${headers[key]},${value}` : value;
keys.push(key = key.toLowerCase());
raw[key] = raw[key] ? `${raw[key]},${value}` : value;
});
resolve(response());
resolve(response(request, all, keys, raw));
};

request.onerror = reject;
Expand Down
18 changes: 18 additions & 0 deletions src/lib/response.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default function response (request, all, keys, raw) {
return {
ok: (request.status/100|0) == 2, // 200-299
statusText: request.statusText,
status: request.status,
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
clone: () => response(request, all, keys, raw),
headers: {
keys: () => keys,
entries: () => all,
get: n => raw[n.toLowerCase()],
has: n => n.toLowerCase() in raw
}
};
}
32 changes: 32 additions & 0 deletions test/lib/response.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import response from '../../src/lib/response.mjs';

describe('response()', () => {
const raw = { 'x-foo': 'foo', 'x-bar': 'bar' };
const keys = Object.keys(raw);
const all = Object.entries(raw);
const resp = response({}, all, keys, raw);

it('returns text()', () => response({ responseText: 'A passing test.' }, [], [], {})
.text()
.then((text) => expect(text).toBe('A passing test.'))
);

it('returns blob()', () => response({ response: 'A passing test.' })
.blob()
.then((text) => expect(text.toString()).toBe(new Blob(['A passing test.']).toString()))
);

it('returns headers', () => {
expect(resp.headers.entries()).toEqual(all);
});

it('returns header keys', () => {
expect(resp.headers.keys()).toEqual(['x-foo', 'x-bar']);
});

it('returns headers has', () => {
expect(resp.headers.has('x-foo')).toBe(true);
expect(resp.headers.has('x-bar')).toBe(true);
expect(resp.headers.has('x-baz')).toBe(false);
});
});

Back | FazBrowse Home | New Git URL