serverless-offline
Version:
Emulate AWS λ and API Gateway locally when developing your Serverless project
45 lines (41 loc) • 1.81 kB
JavaScript
;
const utils = require('./utils');
/*
Mimicks the request context object
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html
*/
module.exports = function createLambdaProxyContext(request, options, stageVariables) {
const authPrincipalId = request.auth && request.auth.credentials && request.auth.credentials.user;
return {
authorizer: {
principalId: authPrincipalId || process.env.PRINCIPAL_ID || 'offlineContext_authorizer_principalId', // See #24
},
path: request.route.path,
headers: request.headers,
pathParameters: utils.nullIfEmpty(request.params),
requestContext: {
accountId: 'offlineContext_accountId',
resourceId: 'offlineContext_resourceId',
stage: options.stage,
requestId: `offlineContext_requestId_${utils.random().toString(10).slice(2)}`,
identity: {
cognitoIdentityPoolId: 'offlineContext_cognitoIdentityPoolId',
accountId: 'offlineContext_accountId',
cognitoIdentityId: 'offlineContext_cognitoIdentityId',
caller: 'offlineContext_caller',
apiKey: 'offlineContext_apiKey',
sourceIp: request.info.remoteAddress,
cognitoAuthenticationType: 'offlineContext_cognitoAuthenticationType',
cognitoAuthenticationProvider: 'offlineContext_cognitoAuthenticationProvider',
userArn: 'offlineContext_userArn',
userAgent: request.headers['user-agent'] || '',
user: 'offlineContext_user',
},
},
resource: 'offlineContext_resource',
httpMethod: request.method.toUpperCase(),
queryStringParameters: utils.nullIfEmpty(request.query),
body: request.payload && JSON.stringify(request.payload),
stageVariables: utils.nullIfEmpty(stageVariables),
};
};