Skip to content

Commit 827d8bc

Browse files
author
Lukas Siemon
committed
fix: swagger info now gets stored on wrap (not on execution
1 parent f24a6fe commit 827d8bc

1 file changed

Lines changed: 20 additions & 23 deletions

File tree

lib/api.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ const swagger = require("./swagger");
77

88
const endpoints = {};
99

10-
const Parser = (request, params) => {
11-
endpoints[request] = params;
12-
return (event) => {
13-
const expectedRequestMethod = request.split(" ")[0];
14-
const receivedRequestMethod = get(event, 'httpMethod');
15-
if (receivedRequestMethod !== expectedRequestMethod) {
16-
throw response.ApiError(`Request Method "${expectedRequestMethod}" expected.`, 400, 99004, {
17-
value: receivedRequestMethod
18-
});
19-
}
20-
let body;
21-
try {
22-
body = JSON.parse(get(event, 'body', '{}'));
23-
} catch (e) {
24-
throw response.ApiError("Invalid Json Body detected.", 400, 99001, {
25-
value: get(event, 'body')
26-
});
27-
}
28-
const eventParsed = Object.assign({}, event, { body });
29-
return Promise.resolve(params.map(p => p.get(eventParsed)));
30-
};
10+
const parse = (request, params, event) => {
11+
const expectedRequestMethod = request.split(" ")[0];
12+
const receivedRequestMethod = get(event, 'httpMethod');
13+
if (receivedRequestMethod !== expectedRequestMethod) {
14+
throw response.ApiError(`Request Method "${expectedRequestMethod}" expected.`, 400, 99004, {
15+
value: receivedRequestMethod
16+
});
17+
}
18+
let body;
19+
try {
20+
body = JSON.parse(get(event, 'body', '{}'));
21+
} catch (e) {
22+
throw response.ApiError("Invalid Json Body detected.", 400, 99001, {
23+
value: get(event, 'body')
24+
});
25+
}
26+
const eventParsed = Object.assign({}, event, { body });
27+
return Promise.resolve(params.map(p => p.get(eventParsed)));
3128
};
3229

3330

@@ -62,14 +59,14 @@ module.exports = (options = {}) => {
6259
if (params.filter(p => p.position === 'path').some(p => request.indexOf(`{${p.name}}`) === -1)) {
6360
throw new Error("Path Parameter not defined in given path.");
6461
}
65-
const parser = Parser(request, params);
62+
endpoints[request] = params;
6663
return rollbar
6764
.wrap((event, context, callback, rb) => limiter
6865
.check(limit, get(event, 'requestContext.identity.sourceIp'))
6966
.catch(() => {
7067
throw response.ApiError("Rate limit exceeded.", 429);
7168
})
72-
.then(() => parser(event))
69+
.then(() => parse(request, params, event))
7370
.then(paramsOut => handler(paramsOut, context, callback, rb))
7471
.then(payload => generateResponse(null, payload, callback, rb))
7572
.catch(err => generateResponse(err, null, callback, rb)));

0 commit comments

Comments
 (0)