Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor response create function outside of promise handler #132

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
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
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
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
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);
});
});