Skip to content

Commit fcc0258

Browse files
committed
Fix usage
1 parent f8632d5 commit fcc0258

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

index.js

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
11
'use strict';
22

33
const getObjectPath = require('./lib/utils').getObjectPath;
4-
const executeLambdaCallback = require('./lib/lambda-callback').executeLambdaCallback;
5-
const authorizerValidationCallback = require('./lib/authorizer-callback').authorizerValidationCallback;
6-
const authorizerCheckCallback = require('./lib/authorizer-callback').authorizerCheckCallback;
7-
const decorateLambdaReqCallback = require('./lib/decorators-callback').decorateLambdaReqCallback;
8-
const decorateAddCORSCallback = require('./lib/decorators-callback').decorateAddCORSCallback;
4+
const executeLambdaCallback = require('./lib/lambda-callbacks').executeLambdaCallback;
5+
const authorizerValidationCallback = require('./lib/authorizer-callbacks').authorizerValidationCallback;
6+
const authorizerCheckCallback = require('./lib/authorizer-callbacks').authorizerCheckCallback;
7+
const decorateLambdaReqCallback = require('./lib/decorators-callbacks').decorateLambdaReqCallback;
8+
const decorateAddCORSCallback = require('./lib/decorators-callbacks').decorateAddCORSCallback;
99

10-
module.exports = ({ html, authorizers }) => (slsConf, slsHandlers) => {
11-
for (let funcId in slsConf.functions) {
12-
const funcConf = slsConf.functions[funcId];
13-
let func = getObjectPath(funcConf.handler, slsHandlers);
14-
const events = (funcConf.events || []).map(e => e.http).filter(e => !!e);
15-
for (let e of events) {
16-
const authSource = getObjectPath(['authorizer', 'identitySource'], e);
17-
const authValidatorExp = getObjectPath(['authorizer', 'identityValidationExpression'], e);
18-
const authorizerFunction = (
19-
getObjectPath(getObjectPath(['authorizer', 'arn'], e) || '', authorizers) ||
20-
getObjectPath(getObjectPath(['authorizer', 'name'], e) || '', slsHandlers)
21-
);
22-
html(
23-
e.method.toLowerCase(),
24-
`/${e.path.replace(/\{(.+?)\}/g, ':$1')}`,
25-
[
26-
decorateLambdaReqCallback(),
27-
...(authorizerFunction ? [
28-
authorizerValidationCallback(
29-
authSource,
30-
authValidatorExp ? new RegExp(authValidatorExp) : null,
31-
{
32-
regionId: 'express',
33-
accountId: 'serverlessify',
34-
apiId: `${slsConf.service}-${funcId}`,
35-
}
36-
),
37-
authorizerCheckCallback(authorizerFunction)
38-
] : []),
39-
...(e.cors ? [decorateAddCORSCallback()] : []),
40-
executeLambdaCallback(func),
41-
]
42-
)
10+
// Options:
11+
// - `html`
12+
// - `authorizers`
13+
// - `setCacheEntry`
14+
// - `getCacheEntry`
15+
module.exports = function(options) {
16+
return function(slsConf, slsHandlers) {
17+
for (let funcId in slsConf.functions) {
18+
const funcConf = slsConf.functions[funcId];
19+
let func = getObjectPath(funcConf.handler, slsHandlers);
20+
const events = (funcConf.events || []).map(e => e.http).filter(e => !!e);
21+
for (let e of events) {
22+
const authSource = getObjectPath(['authorizer', 'identitySource'], e);
23+
const authValidatorExp = getObjectPath(['authorizer', 'identityValidationExpression'], e);
24+
const authorizerFunction = (
25+
getObjectPath(getObjectPath(['authorizer', 'arn'], e) || '', options.authorizers) ||
26+
getObjectPath(getObjectPath(['authorizer', 'name'], e) || '', slsHandlers)
27+
);
28+
options.html(
29+
e.method.toLowerCase(),
30+
`/${e.path.replace(/\{(.+?)\}/g, ':$1')}`,
31+
[
32+
decorateLambdaReqCallback(),
33+
...(authorizerFunction ? [
34+
authorizerValidationCallback(
35+
authSource,
36+
authValidatorExp ? new RegExp(authValidatorExp) : null,
37+
{
38+
regionId: 'express',
39+
accountId: 'serverlessify',
40+
apiId: `${slsConf.service}-${funcId}`,
41+
}
42+
),
43+
authorizerCheckCallback(
44+
authorizerFunction,
45+
options.setCacheEntry,
46+
options.getCacheEntry
47+
)
48+
] : []),
49+
...(e.cors ? [decorateAddCORSCallback()] : []),
50+
executeLambdaCallback(func),
51+
]
52+
)
53+
}
4354
}
44-
}
55+
};
4556
};

lib/authorizer-callbacks.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
const getObjectPath = require('./utils').getObjectPath;
44

5+
// arnOptions:
6+
// - `regionId`
7+
// - `accountId`
8+
// - `apiId`
59
module.exports.authorizerValidationCallback = function(
610
identitySource,
711
identityValidationRegExp,
8-
{
9-
regionId,
10-
accountId,
11-
apiId,
12-
}
12+
arnOptions
1313
) {
1414
return function(req, res, next) {
1515
const sources = {
@@ -34,7 +34,7 @@ module.exports.authorizerValidationCallback = function(
3434
event: {
3535
type: 'TOKEN',
3636
authorizationToken: source,
37-
methodArn: `arn:serverlessify:execute-api:${regionId}:${accountId}:${apiId}/${method}${resourcePath}`,
37+
methodArn: `arn:serverlessify:execute-api:${arnOptions.regionId}:${arnOptions.accountId}:${arnOptions.apiId}/${method}${resourcePath}`,
3838
},
3939
};
4040
next();
@@ -91,7 +91,7 @@ module.exports.authorizerCheckCallback = function(
9191
}
9292

9393
const cacheKey = req.lambda.authorizer.event.authorizationToken;
94-
getCacheEntry(
94+
(getCacheEntry || function(i, cb) { cb() })(
9595
cacheKey,
9696
function(err, cachedAuthorization) {
9797
if (cachedAuthorization) {
@@ -101,7 +101,7 @@ module.exports.authorizerCheckCallback = function(
101101
req.lambda.authorizer.event,
102102
req.lambda.context,
103103
function(innerErr, awsAuthDocument) {
104-
setCacheEntry({
104+
(setCacheEntry || function(i, cb) { cb() })({
105105
key: cacheKey,
106106
value: awsAuthDocument,
107107
}, function() {

0 commit comments

Comments
 (0)