rewrite lambda functions

This commit is contained in:
Max Böck 2019-04-13 19:01:19 +02:00
parent 9007eaed56
commit f75843f6db
4 changed files with 15 additions and 15 deletions

View file

@ -2,18 +2,17 @@ import meta from '../../src/data/meta'
import members from '../../src/data/members'
export const redirect = site => {
const location = site && site.url ? site.url : meta.url
return {
statusCode: 302,
headers: {
Location: location,
Location: site.url,
'Cache-Control': 'no-cache, no-store, must-revalidate'
},
body: `Redirecting..."`
body: `Redirecting to "${site.title}"`
}
}
export const getIndex = url => members.findIndex(site => site.url.includes(url))
export const getIndex = url => members.findIndex(site => url.includes(site.url))
export const getNext = url => {
const index = getIndex(url)
@ -35,6 +34,8 @@ export const getPrevious = url => {
export const getRandom = url => {
const randomIndex = Math.floor(Math.random() * members.length)
const filtered = members.filter(site => !site.url.includes(url))
return filtered[randomIndex]
const sites = url
? members.filter(site => !url.includes(site.url))
: members
return sites[randomIndex]
}

View file

@ -1,9 +1,8 @@
import { redirect, getNext } from './common/utils'
import { redirect, getNext, getRandom } from './common/utils'
exports.handler = function(event, context, callback) {
const { host } = event.headers
console.log(event.headers)
const next = getNext(host)
const { referer } = event.headers
const next = getNext(referer) || getRandom()
callback(null, redirect(next))
}

View file

@ -1,8 +1,8 @@
import { redirect, getPrevious } from './common/utils'
import { redirect, getPrevious, getRandom } from './common/utils'
exports.handler = function(event, context, callback) {
const { host } = event.headers
const prev = getPrevious(host)
const { referer } = event.headers
const prev = getPrevious(referer) || getRandom()
callback(null, redirect(prev))
}

View file

@ -1,8 +1,8 @@
import { redirect, getRandom } from './common/utils'
exports.handler = function(event, context, callback) {
const { host } = event.headers
const random = getRandom(host)
const { referer } = event.headers
const random = getRandom(referer)
callback(null, redirect(random))
}