This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow options with 'successCodes' to be passed to requests
- Loading branch information
1 parent
ee2cef7
commit 264db49
Showing
7 changed files
with
82 additions
and
39 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
export function setUrl(configuration, path) { | ||
if ( configuration && configuration.baseUrl ) return configuration.baseUrl + path | ||
else return path | ||
if (configuration && configuration.baseUrl) | ||
return configuration.baseUrl + path; | ||
else return path; | ||
} | ||
|
||
export function setAuth(configuration, manualAuth) { | ||
if ( manualAuth ) return manualAuth | ||
else if ( configuration && configuration.username ) return { | ||
'username': configuration.username, | ||
'password': configuration.password, | ||
'sendImmediately': ( configuration.authType != 'digest' ) | ||
} | ||
else return null | ||
if (manualAuth) return manualAuth; | ||
else if (configuration && configuration.username) | ||
return { | ||
username: configuration.username, | ||
password: configuration.password, | ||
sendImmediately: configuration.authType != 'digest', | ||
}; | ||
else return null; | ||
} | ||
|
||
export function assembleError({ response, error }) { | ||
export function assembleError({ response, error, params }) { | ||
if (response) { | ||
if ([200,201,202].indexOf(response.statusCode) > -1) return false; | ||
const customCodes = params.options && params.options.successCodes; | ||
if ((customCodes || [200, 201, 202]).indexOf(response.statusCode) > -1) | ||
return false; | ||
} | ||
if (error) return error; | ||
return new Error(`Server responded with: \n${JSON.stringify(response, null, 2)}`) | ||
return new Error( | ||
`Server responded with: \n${JSON.stringify(response, null, 2)}` | ||
); | ||
} | ||
|
||
export function tryJson(data) { | ||
try { | ||
return JSON.parse(data) | ||
} catch(e) { | ||
return {body: data} | ||
return JSON.parse(data); | ||
} catch (e) { | ||
return { body: data }; | ||
} | ||
} |