Skip to content

wearedestination/aws-lambda-redirecter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Lambda Redirecter

Used by AWS lambda functions to perform redirects

Installation

yarn add @destination/aws-lambda-redirecter

Usage

The default action is to redirect to a matching request URI in the given redirect map or redirect to the homepage if no match is found.

const Redirecter = require('@destination/aws-lambda-redirecter');

const redirecter = new Redirecter('https://example.org', {
    'foo': 'bar',
});

// 301 redirect response with Location set to https://example.org/bar
const response = redirecter.getResponse('foo');

// 301 redirect response with Location set to https://example.org
const response2 = redirecter.getResponse('baz');

Configuration

Redirect as-is:

const Redirecter = require('@destination/aws-lambda-redirecter');

const redirecter = new Redirecter('https://example.org', {
    'foo': 'bar',
}, 301, Redirecter.NOTFOUND_REDIRECT_AS_IS);

// 301 redirect response with Location set to https://example.org/baz
const response = redirecter.getResponse('baz');

Return 404:

const Redirecter = require('@destination/aws-lambda-redirecter');

const redirecter = new Redirecter('https://example.org', {
    'foo': 'bar',
}, 301, Redirecter.NOTFOUND_RETURN_404);

// 404 response with body set to '404 Not Found'
const response = redirecter.getResponse('baz');

Change status code:

const Redirecter = require('@destination/aws-lambda-redirecter');

const redirecter = new Redirecter('https://example.org', {
    'foo': 'bar',
}, 302);

// 302 redirect response with Location set to https://example.org/bar
const response = redirecter.getResponse('foo');

Testing

Install dependencies:

docker run --rm -v "$PWD":/app -w /app node:14-alpine yarn install

Run tests:

docker run --rm -v "$PWD":/app -w /app node:14-alpine yarn test

About

Perform redirects from an AWS Lambda function

Resources

Stars

Watchers

Forks

Packages

No packages published