Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
feat(index): implement redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Jun 22, 2020
1 parent 4ffaaac commit 639c568
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 21 deletions.
166 changes: 147 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/adobe/helix-redirect#readme",
"dependencies": {
"@adobe/helix-epsagon": "1.3.12",
"@adobe/helix-shared": "^7.6.0",
"@adobe/helix-status": "7.1.3",
"@adobe/openwhisk-action-logger": "2.2.0",
"@adobe/openwhisk-action-utils": "4.2.2"
Expand Down
40 changes: 38 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,51 @@ const { wrap } = require('@adobe/openwhisk-action-utils');
const { logger } = require('@adobe/openwhisk-action-logger');
const { wrap: status } = require('@adobe/helix-status');
const { epsagon } = require('@adobe/helix-epsagon');
const { RedirectConfig } = require('@adobe/helix-shared');

/**
* This is the main function
* @param {string} name name of the person to greet
* @returns {object} a greeting
*/
function main({ name = 'world' }) {
async function main({ owner, repo, ref, path }) {

const config = await new RedirectConfig()
.withRepo(owner, repo, ref)
.init();

const { url, type } = config.match(path);

if (type === 'temporary') {
return {
statusCode: 302,
body: `moved temporarily <a href="${url}">here</a>`,
headers: {
Location: url
}
}
} else if (type === 'permanent') {
return {
statusCode: 302,
body: `moved permanently <a href="${url}">here</a>`,
headers: {
'Cache-Control': 'max-age=30000000',
Location: url
}
}
} else if (type === 'internal') {
return {
statusCode: 307,
body: `moved internally <a href="${url}">here</a>`,
headers: {
'HLX-Refetch': 'yes',
Location: url
}
}
}
return {
body: `Hello, ${name}.`,
statusCode: 204, // no content
body: `No redirect`,
};
}

Expand Down

0 comments on commit 639c568

Please sign in to comment.