Skip to content

Add access to all headers in request response. #4

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

Merged
merged 1 commit into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ProgressEvent {
}

export interface Headers {
all: { [key: string]: string };
get(key: string): string;
}

Expand Down Expand Up @@ -140,12 +141,23 @@ export default function request(
}

class HeadersClass {
private data: any;
private data: { [key: string]: string };

constructor(headers: any) {
constructor(headers: { [key: string]: string }) {
this.data = headers;
}

get all() {
const { data } = this;
return Object.keys(data).reduce(
(headers: { [key: string]: string }, key) => {
headers[key.toLowerCase()] = data[key];
return headers;
},
{}
);
}

get(key: string) {
return String(this.data[key.toLowerCase()]);
}
Expand Down
15 changes: 13 additions & 2 deletions tests/unit/lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as moxios from 'moxios';
import request from '../../../src/lib/request';
import request, { Response } from '../../../src/lib/request';

const { registerSuite } = intern.getInterface('object');
const { assert } = intern.getPlugin('chai');
Expand All @@ -26,7 +26,8 @@ registerSuite('lib/request', {
statusText: 'OK',
response: 'foo',
headers: {
'Content-Type': 'text/plain'
'Content-Type': 'text/plain',
'A-Test-Header': 'Some Value'
}
});
})
Expand All @@ -53,6 +54,16 @@ registerSuite('lib/request', {
'text/plain',
'Unexpected content type'
);
assert.equal(
response.headers.all['content-type'],
'text/plain',
'Unexpected content type for content-type'
);
assert.equal(
response.headers.all['a-test-header'],
'Some Value',
'Unexpected content type for a-test-header'
);
return response.text();
})
)
Expand Down