-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getLocale; role checks; remove clone; logout msg; xhr resolution;
- Loading branch information
1 parent
11e3951
commit d894f18
Showing
15 changed files
with
242 additions
and
258 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,51 +1,58 @@ | ||
const requestMap = new Map() | ||
|
||
export default params => new Promise((resolve, reject) => { | ||
export default params => new Promise(resolve => { | ||
|
||
if (!params) return reject() | ||
// Return if params are falsy. | ||
if (!params) { | ||
console.error(`xhr params are falsy.`) | ||
return; | ||
} | ||
|
||
// Set params as object with url from string. | ||
params = typeof params === 'string' ? { url: params } : params | ||
|
||
// A request url must be provided. | ||
if (!params.url) { | ||
console.error(`no xhr request url has been provided.`) | ||
return; | ||
}; | ||
|
||
// Check whether a request with the same params has already been resolved. | ||
if (params.cache && requestMap.has(params)) return resolve(requestMap.get(params)) | ||
|
||
// Assign 'GET' as default method. | ||
params.method ??= 'GET' | ||
|
||
const xhr = new XMLHttpRequest() | ||
|
||
xhr.open(params.method || 'GET', params.url) | ||
xhr.open(params.method, params.url) | ||
|
||
// Use requestHeader: null to prevent assignment of requestHeader. | ||
if (params.requestHeader !== null) { | ||
|
||
// Butter (spread) over requestHeader. | ||
const requestHeader = { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
...params.requestHeader | ||
} | ||
|
||
Object.assign(requestHeader, params.requestHeader || {}) | ||
|
||
Object.entries(requestHeader).forEach(entry=>xhr.setRequestHeader(...entry)) | ||
|
||
Object.entries(requestHeader).forEach(entry => xhr.setRequestHeader(...entry)) | ||
} | ||
|
||
xhr.responseType = params.responseType || 'json' | ||
|
||
xhr.onload = e => { | ||
|
||
if (e.target.status >= 400) { | ||
reject(new Error(e.target.status)) | ||
resolve(new Error(e.target.status)) | ||
return; | ||
} | ||
|
||
// Cache the response in the requestMap | ||
params.cache && requestMap.set(params, e.target.response) | ||
|
||
resolve(params.resolveTarget ? e.target : e.target.response) | ||
|
||
} | ||
|
||
// xhr.onerror = err => { | ||
// console.error(err) | ||
// reject(err); | ||
// }; | ||
|
||
xhr.send(params.body) | ||
|
||
}) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.