From ec4045cb57a784d3e1a82fefe3fde878dde87632 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 20 Jan 2023 19:57:05 +0800 Subject: [PATCH] Added dynamic-routing-events package refs https://github.com/TryGhost/Toolbox/issues/503 - Reusing existing events inside of dynamic routing would only contribute to general confusion that is already there. Having separate "DomainEvents" is the best practice used throughout the code which is substituting generic events. - The URLResourceUpdatedEvent is supposed to be emmited whenever there's an updated to the resource already associated with the URL circumventing full url regeneration process inside of DynamicRouting --- ghost/dynamic-routing-events/.eslintrc.js | 6 ++++ ghost/dynamic-routing-events/README.md | 23 +++++++++++++ ghost/dynamic-routing-events/index.js | 3 ++ .../lib/URLResourceUpdatedEvent.js | 33 +++++++++++++++++++ ghost/dynamic-routing-events/package.json | 26 +++++++++++++++ .../dynamic-routing-events/test/.eslintrc.js | 6 ++++ .../test/dynamic-routing-events.test.js | 8 +++++ .../test/lib/URLResourceUpdatedEvent.test.js | 12 +++++++ 8 files changed, 117 insertions(+) create mode 100644 ghost/dynamic-routing-events/.eslintrc.js create mode 100644 ghost/dynamic-routing-events/README.md create mode 100644 ghost/dynamic-routing-events/index.js create mode 100644 ghost/dynamic-routing-events/lib/URLResourceUpdatedEvent.js create mode 100644 ghost/dynamic-routing-events/package.json create mode 100644 ghost/dynamic-routing-events/test/.eslintrc.js create mode 100644 ghost/dynamic-routing-events/test/dynamic-routing-events.test.js create mode 100644 ghost/dynamic-routing-events/test/lib/URLResourceUpdatedEvent.test.js diff --git a/ghost/dynamic-routing-events/.eslintrc.js b/ghost/dynamic-routing-events/.eslintrc.js new file mode 100644 index 0000000000..c9c1bcb522 --- /dev/null +++ b/ghost/dynamic-routing-events/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node' + ] +}; diff --git a/ghost/dynamic-routing-events/README.md b/ghost/dynamic-routing-events/README.md new file mode 100644 index 0000000000..b4baa20fc9 --- /dev/null +++ b/ghost/dynamic-routing-events/README.md @@ -0,0 +1,23 @@ +# Dynamic Routing Events + +Contains Dynamic Routing (URL Service) events + + +## 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 + diff --git a/ghost/dynamic-routing-events/index.js b/ghost/dynamic-routing-events/index.js new file mode 100644 index 0000000000..517d3b4fba --- /dev/null +++ b/ghost/dynamic-routing-events/index.js @@ -0,0 +1,3 @@ +module.exports = { + URLResourceUpdatedEvent: require('./lib/URLResourceUpdatedEvent') +}; diff --git a/ghost/dynamic-routing-events/lib/URLResourceUpdatedEvent.js b/ghost/dynamic-routing-events/lib/URLResourceUpdatedEvent.js new file mode 100644 index 0000000000..3b7ba8b544 --- /dev/null +++ b/ghost/dynamic-routing-events/lib/URLResourceUpdatedEvent.js @@ -0,0 +1,33 @@ +module.exports = class URLResourceUpdatedEvent { + /** + * @readonly + * @type {Object} + */ + data; + + /** + * @readonly + * @type {Date} + */ + timestamp; + + /** + * @private + */ + constructor({timestamp, ...data}) { + this.data = data; + this.timestamp = timestamp; + } + + /** + * + * @param {Object} data URL Resource + * @returns + */ + static create(data) { + return new URLResourceUpdatedEvent({ + ...data, + timestamp: data.timestamp || new Date + }); + } +}; diff --git a/ghost/dynamic-routing-events/package.json b/ghost/dynamic-routing-events/package.json new file mode 100644 index 0000000000..2bc495dabc --- /dev/null +++ b/ghost/dynamic-routing-events/package.json @@ -0,0 +1,26 @@ +{ + "name": "@tryghost/dynamic-routing-events", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Ghost/tree/main/packages/dynamic-routing-events", + "author": "Ghost Foundation", + "private": true, + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --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": { + "c8": "7.12.0", + "mocha": "10.2.0", + "sinon": "15.0.1" + }, + "dependencies": {} +} diff --git a/ghost/dynamic-routing-events/test/.eslintrc.js b/ghost/dynamic-routing-events/test/.eslintrc.js new file mode 100644 index 0000000000..829b601eb0 --- /dev/null +++ b/ghost/dynamic-routing-events/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test' + ] +}; diff --git a/ghost/dynamic-routing-events/test/dynamic-routing-events.test.js b/ghost/dynamic-routing-events/test/dynamic-routing-events.test.js new file mode 100644 index 0000000000..6e9a773f85 --- /dev/null +++ b/ghost/dynamic-routing-events/test/dynamic-routing-events.test.js @@ -0,0 +1,8 @@ +const assert = require('assert'); +const events = require('../index'); + +describe('Dynamic Routing Events', function () { + it('exports events', function () { + assert(events.URLResourceUpdatedEvent); + }); +}); diff --git a/ghost/dynamic-routing-events/test/lib/URLResourceUpdatedEvent.test.js b/ghost/dynamic-routing-events/test/lib/URLResourceUpdatedEvent.test.js new file mode 100644 index 0000000000..3cab0329ca --- /dev/null +++ b/ghost/dynamic-routing-events/test/lib/URLResourceUpdatedEvent.test.js @@ -0,0 +1,12 @@ +const assert = require('assert'); +const {URLResourceUpdatedEvent} = require('../../index'); + +describe('URLResourceUpdatedEvent', function () { + it('exports a static create method to create instances', function () { + const event = URLResourceUpdatedEvent.create({ + id: 'resource-id' + }); + + assert(event instanceof URLResourceUpdatedEvent); + }); +});