Helpers that are commonly used in microservices.
npm install api-helper --save
It helps to extract changes from old and new JSON objects and log them via the Audit Service. Currently supports user level actions only.
First, create new AuditLog instance with the Audit Service endpoint (Using config
is recommended instead of hard-coding).
For single table, you can simply call createUserLogForCreate
, createUserLogForUpdate
or createUserLogForDelete
accordingly.
- requestId: (string) Request ID from the caller
- userId: (integer) User ID who requires the action
- tableName: (string) DB table name to be affected
- recordId: (integer) DB table record ID to be affected
- oldObject: (object) Old object before changes
- newObject: (object) New object after changes
import { AuditLog } from 'api-helper';
const auditLog = new AuditLog('https://dev.audit-service.com.au', 'project name');
function createSubdomain(requestId, userId, organisationId, subdomain) {
return userManager.getUser(userId)
.then(user => validateSubdomain(subdomain))
.then(() => Organisation.update({ code: subdomain }, { where: { id: organisationId } }))
.then(() => auditLog.createUserLogForUpdate(requestId, userId, 'Organisation', organisationId, {}, { code: subdomain }));
}
It helps to use Redis cache.
Create a Redis connection. It must be called in server.js to initialise cacheHelper.
import config from 'config';
import Server from 'microservice-base';
import { cacheHelper } from 'api-helper';
import routes from './routes';
cacheHelper.setup(config.redis.endpoint);
export default function() {
const server = new Server({
name: 'ACCOUNT-SERVICE',
localPort: 4002, //DO NOT CHANGE. USED BY OTHER SERVICES FOR LOCAL TESTS.
logConfig: { ...config.cloudWatch },
docs: './docs/api.raml',
routes: routes
});
server.start();
return server;
}
Set cache with a key and a JSON object. Use third parameter expiryIn
to set expiry time - Availble time indicators are 'd' for days, 'h' for hours, and 'm' for minutes.
(eg. 3d, 2h, 30m)
If expiryIn
is not provided, no expiry time will be set.
import { responseHelper, cacheHelper } from 'api-helper';
import db from '../models';
import constants from '../helpers/constants';
const { Activity } = db.models;
const { cacheKeys, classificationStatuses, classificationStatusesAll } = constants;
function getAllActivities() {
const cacheKey = cacheKeys.primaryActivityAll;
return cacheHelper.get(cacheKey)
.then(cacheResult => {
if (cacheResult) {
return Promise.resolve(cacheResult);
}
return Activity.findAll()
.then(result => {
cacheHelper.set(cacheKey, result, '6h'); //Expired in 6h
return Promise.resolve(result);
});
});
}
Get a cached object by key.
import { responseHelper, cacheHelper } from 'api-helper';
import db from '../models';
import constants from '../helpers/constants';
const { Activity } = db.models;
const { cacheKeys, classificationStatuses, classificationStatusesAll } = constants;
function getAllActivities() {
const cacheKey = cacheKeys.primaryActivityAll;
return cacheHelper.get(cacheKey)
.then(cacheResult => {
if (cacheResult) {
return Promise.resolve(cacheResult);
}
return Activity.findAll()
.then(result => {
cacheHelper.set(cacheKey, result, '6h'); //Expired in 6h
return Promise.resolve(result);
});
});
}
It helps to create a JSON response object for any API requests.
Creates single error object with a error code, error message, and error field. Note that it returns a string to be used as the message parameter in an error class.
import { responseHelper } from 'api-helper';
function validateEmail(email) {
if (!email) {
throw new SyntaxError(responseHelper.createError(responseHelper.errorCodes.field_validation_error, 'Email is required', 'email'));
}
}
Creates an error list. Note that it returns a string to be used as the message parameter in an error class.
import { responseHelper } from 'api-helper';
function validateEmail(email) {
const errors = [
{
errorCode: responseHelper.errorCodes.field_validation_error,
errorMessage: 'Email is required',
errorField: 'email'
},
{
errorCode: responseHelper.errorCodes.unique_key_error,
errorMessage: 'Email is already registered',
errorField: 'email'
}
]
throw new SyntaxError(responseHelper.createErrors(errors));
}
Creates response data object that has no error. Note that it returns a JSON object.
import { responseHelper } from 'api-helper';
function getData() {
const testData = {
id: 1,
name: 'John',
company: 'MyCom'
};
return responseHelper.createData(testData);
}
Comming soon.