Added initial link-redirects package

refs https://github.com/TryGhost/Team/issues/1887
This commit is contained in:
Fabien "egg" O'Carroll 2022-09-13 18:18:19 -04:00 committed by Fabien 'egg' O'Carroll
parent 4726742673
commit 8fe200ff45
11 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node'
]
};

View File

@ -0,0 +1,21 @@
# Link Redirects
## Usage
## Develop
This is a monorepo package.
Follow the instructions for the top-level repo.
1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.
## Test
- `yarn lint` run just eslint
- `yarn test` run lint and tests

View File

@ -0,0 +1 @@
module.exports = require('./lib/link-redirects');

View File

@ -0,0 +1,23 @@
const ObjectID = require('bson-objectid').default;
module.exports = class LinkRedirect {
/** @type {ObjectID} */
link_id;
/** @type {URL} */
from;
/** @type {URL} */
to;
constructor(data) {
if (!data.id) {
this.link_id = new ObjectID();
}
if (typeof data.id === 'string') {
this.link_id = ObjectID.createFromHexString(data.id);
}
this.from = data.from;
this.to = data.to;
}
};

View File

@ -0,0 +1,34 @@
const LinkRedirect = require('./LinkRedirect');
class LinkRedirectsService {
/**
* @param {URL} to
*
* @returns {Promise<LinkRedirect>}
*/
async addRedirect(to) {
const from = new URL(to);
from.searchParams.set('redirected', 'true'); // Dummy for skateboard
const link = new LinkRedirect({
to,
from
});
return link;
}
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
* @param {import('express').NextFunction} next
*
* @returns {Promise<void>}
*/
async handleRequest(req, res, next) {
return next();
}
}
module.exports = LinkRedirectsService;

View File

@ -0,0 +1,24 @@
/**
* @typedef {object} RedirectEventData
* @prop {URL} url
* @prop {import('./LinkRedirect')} link
*/
module.exports = class RedirectEvent {
/**
* @param {RedirectEventData} data
* @param {Date} timestamp
*/
constructor(data, timestamp) {
this.data = data;
this.timestamp = timestamp;
}
/**
* @param {RedirectEventData} data
* @param {Date} [timestamp]
*/
static create(data, timestamp) {
return new RedirectEvent(data, timestamp ?? new Date);
}
};

View File

@ -0,0 +1,5 @@
module.exports = {
LinkRedirect: require('./LinkRedirect'),
RedirectEvent: require('./RedirectEvent'),
LinkRedirectsService: require('./LinkRedirectsService')
};

View File

@ -0,0 +1,30 @@
{
"name": "@tryghost/link-redirects",
"version": "0.0.0",
"repository": "https://github.com/TryGhost/Ghost/tree/main/packages/link-redirects",
"author": "Ghost Foundation",
"private": true,
"main": "index.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"test:unit": "NODE_ENV=testing c8 --all --reporter text --reporter cobertura mocha './test/**/*.test.js'",
"test": "yarn test:unit",
"lint:code": "eslint *.js lib/ --ext .js --cache",
"lint": "yarn lint:code && yarn lint:test",
"lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache"
},
"files": [
"index.js",
"lib"
],
"devDependencies": {
"@types/express": "^4.17.14",
"c8": "7.12.0",
"mocha": "10.0.0",
"should": "13.2.3",
"sinon": "14.0.0"
},
"dependencies": {
"bson-objectid": "^2.0.3"
}
}

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test'
]
};

View File

@ -0,0 +1,5 @@
describe('LinkRedirectsService', function () {
it('exists', function () {
require('../lib/LinkRedirectsService');
});
});

View File

@ -3912,6 +3912,16 @@
"@types/qs" "*"
"@types/serve-static" "*"
"@types/express@^4.17.14":
version "4.17.14"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.14.tgz#143ea0557249bc1b3b54f15db4c81c3d4eb3569c"
integrity sha512-TEbt+vaPFQ+xpxFLFssxUDXj5cWCxZJjIcB7Yg0k0GMHGtgtQgpvx/MUQUeAkNbA9AAGrwkAsoeItdTgS7FMyg==
dependencies:
"@types/body-parser" "*"
"@types/express-serve-static-core" "^4.17.18"
"@types/qs" "*"
"@types/serve-static" "*"
"@types/fs-extra@^5.0.5":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-5.1.0.tgz#2a325ef97901504a3828718c390d34b8426a10a1"
@ -6882,7 +6892,7 @@ bser@2.1.1:
dependencies:
node-int64 "^0.4.0"
bson-objectid@2.0.3:
bson-objectid@2.0.3, bson-objectid@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/bson-objectid/-/bson-objectid-2.0.3.tgz#d840185172846b2f10c42ce2bcdb4a50956a9db5"
integrity sha512-WYwVtY9yqk179EPMNuF3vcxufdrGLEo2XwqdRVbfLVe9X6jLt7WKZQgP+ObOcprakBGbHxzl76tgTaieqsH29g==