|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | 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; |
9 | 9 |
|
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 | + } |
43 | 54 | } |
44 | | - } |
| 55 | + }; |
45 | 56 | }; |
0 commit comments