Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit 16e6e8e

Browse files
author
Jun-Zhe Lai
authored
Merge pull request #11 from spring-media/minor-refactoring
avoid error if no response body
2 parents 6d502ea + f2af7d5 commit 16e6e8e

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
function handler(routeConfig) {
44
const eventProcessorMapping = extractEventProcessorMapping(routeConfig);
5+
56
return (event, context, callback) => {
7+
if (routeConfig.debug) {
8+
console.log("Lambda invoked with request:", event)
9+
}
10+
611
for (const eventProcessorName of eventProcessorMapping.keys()) {
712

813
try {
@@ -22,6 +27,10 @@ function handler(routeConfig) {
2227
console.log(error.stack);
2328
callback(error.toString());
2429
});
30+
} else {
31+
if (routeConfig.debug) {
32+
console.log("Event processor couldn't handle request.")
33+
}
2534
}
2635
} catch (error) {
2736
if (error.stack) {

lib/proxyIntegration.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,39 @@ function addCorsHeaders(toAdd) {
1414
return toAdd;
1515
}
1616

17+
function processActionAndReturn(actionConfig, event, headers, errorMapping) {
18+
return Promise.resolve(actionConfig.action(event)).then(res => {
19+
20+
if (res && res.body) {
21+
const mergedHeaders = Object.assign({}, headers, res.headers);
22+
23+
return Object.assign({
24+
statusCode: 200
25+
},
26+
res, {
27+
headers: mergedHeaders
28+
});
29+
} else {
30+
const consolidateBody = res && JSON.stringify(res) || '{}';
31+
32+
return {
33+
statusCode: 200,
34+
headers: headers,
35+
body: consolidateBody
36+
};
37+
}
38+
}).catch(err => {
39+
console.log('Error while handling action function.')
40+
return convertError(err, errorMapping, headers);
41+
});
42+
}
43+
1744
function process(proxyIntegrationConfig, event) {
45+
if (proxyIntegrationConfig.debug) {
46+
console.log("Lambda proxyIntegrationConfig: ", proxyIntegrationConfig)
47+
console.log("Lambda request: ", event)
48+
}
49+
1850
//validate config
1951
if (!Array.isArray(proxyIntegrationConfig.routes) || proxyIntegrationConfig.routes.length < 1) {
2052
throw new Error('proxyIntegration.routes must not be empty');
@@ -65,27 +97,7 @@ function process(proxyIntegrationConfig, event) {
6597
});
6698
}
6799
}
68-
return Promise.resolve(actionConfig.action(event)).then(res => {
69-
70-
if (res.body) {
71-
const mergedHeaders = Object.assign({}, headers, res.headers);
72-
73-
return Object.assign({
74-
statusCode: 200
75-
},
76-
res, {
77-
headers: mergedHeaders
78-
});
79-
}
80-
81-
return {
82-
statusCode: 200,
83-
headers: headers,
84-
body: JSON.stringify(res)
85-
};
86-
}).catch(err => {
87-
return convertError(err, errorMapping, headers);
88-
});
100+
return processActionAndReturn(actionConfig, event, headers, errorMapping);
89101
} catch (error) {
90102
console.log('Error while evaluating matching action handler', error);
91103
return Promise.resolve(convertError(error, errorMapping, headers));

test/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('processor.configuration', () => {
88
'./lib/lib2': () => Promise.resolve(2)
99
});
1010

11+
1112
it('should throw error if processor does not exist', () => {
1213
expect(() => router.handler({
1314
lib1: {},

test/proxyIntegration.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ describe('proxyIntegration.routeHandler', () => {
269269
});
270270
});
271271

272+
272273
it('should return default headers', (done) => {
273274
const routeConfig = {
274275
defaultHeaders: {'a': '1', 'b': '2'},
@@ -289,6 +290,28 @@ describe('proxyIntegration.routeHandler', () => {
289290
done();
290291
});
291292
});
293+
294+
295+
it('should return error headers', (done) => {
296+
const routeConfig = {
297+
routes: [
298+
{
299+
method: 'GET',
300+
path: '/',
301+
action: () => (Promise.resolve())
302+
}
303+
]
304+
};
305+
proxyIntegration(routeConfig, {path: '/', httpMethod: 'GET'}).then(result => {
306+
expect(result).toEqual({
307+
statusCode: 200,
308+
headers: {"Content-Type": "application/json"},
309+
body: "{}"
310+
});
311+
done();
312+
});
313+
});
314+
292315
it('should return error including CORS header', (done) => {
293316
const routeConfig = {
294317
cors: true,

0 commit comments

Comments
 (0)