Parse + merge nested response headers to fix content-type checking (#147)
* Parse + merge nested response headers to fix content-type checking * Remove redundant parsing of nested headers in Client.login
This commit is contained in:
parent
7e426b6029
commit
217985107a
1 changed files with 23 additions and 8 deletions
|
|
@ -208,13 +208,6 @@ export default class Client {
|
|||
|
||||
if (response.headers.has(HEADER_TOKEN)) {
|
||||
this.token = response.headers.get(HEADER_TOKEN);
|
||||
} else {
|
||||
// weird case where fetch does not parse the header correctly
|
||||
const parseHeader = response.headers.get(HEADER_CONTENT_TYPE).split('\n');
|
||||
const filter = parseHeader.filter((h) => h.indexOf('Token:') > -1);
|
||||
if (filter.length) {
|
||||
this.token = filter[0].replace('Token: ', '');
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
|
|
@ -631,7 +624,8 @@ export default class Client {
|
|||
doFetchWithResponse = async (url, options) => {
|
||||
const response = await fetch(url, this.getOptions(options));
|
||||
|
||||
const contentType = response.headers.get(HEADER_CONTENT_TYPE);
|
||||
const headers = parseAndMergeNestedHeaders(response.headers);
|
||||
const contentType = headers.get(HEADER_CONTENT_TYPE);
|
||||
const isJson = contentType && contentType.indexOf(CONTENT_TYPE_JSON) !== -1;
|
||||
|
||||
let data;
|
||||
|
|
@ -662,3 +656,24 @@ export default class Client {
|
|||
throw {message: msg, status_code: data.status_code, url};
|
||||
};
|
||||
}
|
||||
|
||||
function parseAndMergeNestedHeaders(originalHeaders) {
|
||||
// TODO: This is a workaround for https://github.com/matthew-andrews/isomorphic-fetch/issues/97
|
||||
// The real solution is to set Access-Control-Expose-Headers on the server
|
||||
const headers = new Map();
|
||||
let nestedHeaders = new Map();
|
||||
originalHeaders.forEach((val, key) => {
|
||||
const capitalizedKey = key.replace(/\b[a-z]/g, (l) => l.toUpperCase());
|
||||
let realVal = val;
|
||||
if (val && val.match(/\n\S+:\s\S+/)) {
|
||||
const nestedHeaderStrings = val.split('\n');
|
||||
realVal = nestedHeaderStrings.shift();
|
||||
const moreNestedHeaders = new Map(
|
||||
nestedHeaderStrings.map((h) => h.split(/:\s/))
|
||||
);
|
||||
nestedHeaders = new Map([...nestedHeaders, ...moreNestedHeaders]);
|
||||
}
|
||||
headers.set(capitalizedKey, realVal);
|
||||
});
|
||||
return new Map([...headers, ...nestedHeaders]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue