api-messages
v1.0.1
Published
Library to maintain api response messages into a single place. Useful when you want to reuse messages in multiple places to maintain consistency. Also when you want to update messages at a later time and not have to change your code. For instance, a techn
Downloads
5
Readme
api-messages
api-messages is a small library to unify messages into a single place. The library contains a small set of generic messages. You must provide a file with your custom messages in the configure
method.
Install:
npm install --save api-messages
Initialize:
import ApiMessages from 'api-messages'
const apiMessages = new ApiMessages();
apiMessages.configure({
source: 'path-to-your-source.json'
});
Usage
Give the path of the message from the source json file.
const messsage = apiMessages.get('errors.authentication.emailNotVerified');
Interpolation
You can interpolate values by passing a second object with key/values to the get
function.
Interpolation property names in your configuration file must match object property names. Interpolation properties are surrounded as folows: #{propertyName}
.
Typescript code
const messsage = apiMessages.get('errors.authentication.emailNotVerified', {
name: 'John',
email: '[email protected]'
});
JSON configuration file
{
"errors": {
"authentication": {
"emailNotVerified": "Hi #{name}. Your email #{email} has not been verified."
}
}
}
Output
Hi John. Your email [email protected] has not been verified.
Defaults
The library offers a small set of generic error messages.
const message = apiMessages.defaults.http.ok;
Available default messages:
http: {
ok: 'Success.',
created: 'Operation successful.',
badRequest: 'The server could not understand your request, please check your syntax.',
unauthorized: 'Please sign in before proceeding with this action.',
forbidden: 'You are not allowed to access this resource.',
notFound: 'the item you are looking for was not found',
conflict: 'There is a conflict with an existing record, please check your request.',
internalServerError: 'An error has occurred. If the problem persists, contact customer support.'
}