Skip to content
This repository has been archived by the owner on Nov 27, 2019. It is now read-only.

Commit

Permalink
Added possibility to pass options such as validate function and trans…
Browse files Browse the repository at this point in the history
…form function
  • Loading branch information
shmuga committed Apr 27, 2017
1 parent d8b9468 commit 83029ff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ module.exports.lambda = R((data) => {
});
```

Also it's possible to pass `handler` as second param and `settings` object as first.

Default `settings` fields:
```
const defaultSettings = {
transform: data => data, // called first
validate: data => data, // than validate and after validation handler is called
}
```

## Responses

Expand Down
40 changes: 30 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,44 @@ const isEmpty = v => v === null || v === undefined;
/**
* Wrapper for function that handled aws event, context, callback
* and returns unified response for errors and success statuses
* @param {object} settings - Optional settings for function
* @param {handlerFunction} handler - Handles the response
*/
function R(handler) {
function R(...params) {
const defaultSettings = {
validate: data => Promise.resolve(data),
transform: data => Promise.resolve(data),
};

let settings;
let handler;

if (params.length === 2) {
[settings, handler] = params;
}

if (params.length === 1) {
[handler] = params;
settings = {};
}

const usedSettings = Object.assign({}, defaultSettings, settings);

return (event, context, callback) => {
const startTime = process.hrtime();
const data = event && event.body
? JSON.parse(event.body)
: event.queryStringParameters || {};

// calling handler for function
let result;
try {
result = handler(data);
} catch (e) {
result = Promise.reject(e);
}
let transformed;

Promise.resolve(result)
Promise.resolve(usedSettings.transform(data))
.then(usedSettings.validate)
.then((transformedData) => {
transformed = transformedData;
return handler(transformedData);
})
.then((response) => {
callback(null, {
statusCode: 200,
Expand All @@ -45,7 +65,7 @@ function R(handler) {
requestId: context.requestId,
dataAvailable: !isEmpty(response),
executionTimeInMs: calculateExecution(process.hrtime(startTime)),
originalRequest: data,
originalRequest: transformed,
data: response,
}),
});
Expand All @@ -61,7 +81,7 @@ function R(handler) {
requestId: context.requestId,
dataAvailable: false,
executionTimeInMs: calculateExecution(process.hrtime(startTime)),
originalRequest: data,
originalRequest: transformed,
errorMessage: err.message || 'Unknown error. No error message',
errorName: err.name || 'UnknownError',
errorData: err.data ? err.data : null,
Expand Down
40 changes: 40 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@ const { expect } = require('chai');
const R = require('../lib/index');

describe('Test handling request', () => {
it('should check if transform works correctly', (cb) => {
const event = require('./sample-requests/GET-request-aws.json');

const handler = R({
transform: (data) => {
expect(data).to.be.deep.equal(event.queryStringParameters);
return Object.assign({}, data, { newField: 12134 });
}
},(data) => {
expect(data.newField).to.be.equal(12134);
return { ok: 1 };
});

handler(event, { requestId: 12345 }, (err, result) => {

cb();
});
});


it('should check if validation works correctly', (cb) => {
const event = require('./sample-requests/GET-request-aws.json');

const handler = R({
validate: (data) => {
expect(data).to.be.deep.equal(event.queryStringParameters);
throw new Error('VALIDATION');
}
},(data) => {
return { ok: 1 };
});

handler(event, { requestId: 12345 }, (err, result) => {
expect(result.body).to.be.a('string');
const body = JSON.parse(result.body);
expect(body.errorMessage).to.be.equal('VALIDATION');
cb();
});
});

it('should check if get params parsed correctly', (cb) => {
const event = require('./sample-requests/GET-request-aws.json');

Expand Down

0 comments on commit 83029ff

Please sign in to comment.