@argodigital/qq-shared
Version:
Shared code for QQ projects
98 lines (97 loc) • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var logger_1 = require("../logger");
var iopipe_1 = require("@iopipe/iopipe");
var ioPipeEnabled = 'true' !== process.env.DISABLE_IOPIPE;
var headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
};
function apiwrapper(fn) {
return function (event, context, callback) {
var log = logger_1.logger(context);
var body;
try {
body = event.body ? JSON.parse(event.body) : null;
}
catch (err) {
body = null;
log.error('Error parsing body with request', event.body, err);
error(err);
}
var path = event.pathParameters ? event.pathParameters : null;
var query = event.queryStringParameters ? event.queryStringParameters : null;
var request = body ? body : path ? path : query ? query : null;
var authContext = event.requestContext && event.requestContext.authorizer ? event.requestContext.authorizer : null;
if (ioPipeEnabled) {
iopipe_1.metric('region', process.env.AWS_REGION);
iopipe_1.metric('revision', process.env.REVISION);
iopipe_1.metric('stage', process.env.STAGE);
iopipe_1.metric('body', body);
iopipe_1.metric('query-params', query);
iopipe_1.metric('path-params', path);
iopipe_1.metric('auth-context', authContext);
}
function success(message) {
if (ioPipeEnabled) {
iopipe_1.label('valid');
}
var body = JSON.stringify(message);
log.info("Successful request. Returning " + body);
return callback(null, { statusCode: 200, headers: headers, body: body });
}
function invalid(errors) {
var validationErrors = getValidationMessages(errors);
log.info('Invalid request', validationErrors, request);
if (ioPipeEnabled) {
iopipe_1.label('invalid');
iopipe_1.metric('invalid', validationErrors);
}
var body = JSON.stringify({ request: request, validationErrors: validationErrors });
return callback(null, { statusCode: 400, headers: headers, body: body });
}
function redirect(url) {
if (ioPipeEnabled) {
iopipe_1.label('redirect');
iopipe_1.metric('redirect', url);
}
headers['Location'] = url;
return callback(null, { statusCode: 302, headers: headers });
}
function error(error, statusCode) {
statusCode = statusCode || error && error.statusCode || 500;
if (500 === statusCode) {
log.error('Error', error, request);
}
error = error instanceof Error ? error.toString() : error;
if (ioPipeEnabled) {
iopipe_1.label('error');
iopipe_1.metric('error', error);
}
var body = JSON.stringify({ request: request, error: error });
return callback(undefined, { statusCode: statusCode, headers: headers, body: body });
}
var props = { event: event, body: body, query: query, path: path, success: success, invalid: invalid, error: error, redirect: redirect, log: log, authContext: authContext };
fn(props);
};
}
exports.apiwrapper = apiwrapper;
function getValidationMessages(errors) {
var messages = [];
if (errors.errors) {
messages = errors.errors;
}
else {
errors = errors;
errors.forEach(function (error) {
if (error.constraints) {
var constraint_1 = error.constraints;
messages = messages.concat(Object.keys(constraint_1).map(function (key) { return constraint_1[key]; }));
}
if (error.children) {
messages = messages.concat(getValidationMessages(error.children));
}
});
}
return messages;
}