This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service-com): Added ServiceCommunicator (#46)
* feat(service-com): Added ServiceCommunicator * feat(service-com): Added ServiceCommunicator * feat(service-com): Added ServiceCommunicator * feat(service-com): Added ServiceCommunicator
- Loading branch information
1 parent
f538de3
commit 672fd79
Showing
3 changed files
with
61 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import axios from 'axios' | ||
import Status from 'http-status' | ||
|
||
export class ServiceCommunicator { | ||
constructor ({ | ||
standardError, | ||
config | ||
}) { | ||
this.standardError = standardError | ||
this.logmsg = config.logmsg | ||
} | ||
|
||
init (serviceUrl) { | ||
this.serviceUrl = serviceUrl | ||
} | ||
|
||
async http (options) { | ||
try { | ||
const response = await axios({ | ||
baseURL: this.serviceUrl, | ||
...options | ||
}) | ||
return response.data | ||
} catch (error) { | ||
const { response } = error | ||
if (response && response.data && response.data.error) { | ||
throw this._getStandardError(response.data.error) | ||
} | ||
throw error | ||
} | ||
} | ||
|
||
_getStandardError (error) { | ||
const type = this._getErrorType(error.type) | ||
const { message, errors = [] } = error | ||
return this.standardError({ | ||
type, | ||
message, | ||
errors: errors.map(({ location, ...rest }) => ({ | ||
param: Object.keys(rest)[0], | ||
msg: Object.values(rest)[0], | ||
location | ||
})) | ||
}) | ||
} | ||
|
||
_getErrorType (type) { | ||
switch (type) { | ||
case Status['400_NAME']: | ||
return this.logmsg.errors.validationError | ||
case Status['404_NAME']: | ||
return this.logmsg.errors.notFoundError | ||
default: | ||
return type | ||
} | ||
} | ||
} |
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