From d4f8fc89a3cec75196444ac635ea76b1afcb42fa Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 13 Nov 2018 12:39:16 +0700 Subject: [PATCH] Created initial layer2 for Members SDK closes #9 --- ghost/sdk/layer2/.eslintrc.js | 6 ++ ghost/sdk/layer2/LICENSE | 21 +++++++ ghost/sdk/layer2/README.md | 68 +++++++++++++++++++++++ ghost/sdk/layer2/drop-in.js | 6 ++ ghost/sdk/layer2/index.js | 45 +++++++++++++++ ghost/sdk/layer2/package.json | 37 ++++++++++++ ghost/sdk/layer2/test/.eslintrc.js | 6 ++ ghost/sdk/layer2/test/hello.test.js | 10 ++++ ghost/sdk/layer2/test/utils/assertions.js | 11 ++++ ghost/sdk/layer2/test/utils/index.js | 11 ++++ ghost/sdk/layer2/test/utils/overrides.js | 10 ++++ 11 files changed, 231 insertions(+) create mode 100644 ghost/sdk/layer2/.eslintrc.js create mode 100644 ghost/sdk/layer2/LICENSE create mode 100644 ghost/sdk/layer2/README.md create mode 100644 ghost/sdk/layer2/drop-in.js create mode 100644 ghost/sdk/layer2/index.js create mode 100644 ghost/sdk/layer2/package.json create mode 100644 ghost/sdk/layer2/test/.eslintrc.js create mode 100644 ghost/sdk/layer2/test/hello.test.js create mode 100644 ghost/sdk/layer2/test/utils/assertions.js create mode 100644 ghost/sdk/layer2/test/utils/index.js create mode 100644 ghost/sdk/layer2/test/utils/overrides.js diff --git a/ghost/sdk/layer2/.eslintrc.js b/ghost/sdk/layer2/.eslintrc.js new file mode 100644 index 0000000000..6a5eab530d --- /dev/null +++ b/ghost/sdk/layer2/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node', + ] +}; diff --git a/ghost/sdk/layer2/LICENSE b/ghost/sdk/layer2/LICENSE new file mode 100644 index 0000000000..97c52a346d --- /dev/null +++ b/ghost/sdk/layer2/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Ghost Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/ghost/sdk/layer2/README.md b/ghost/sdk/layer2/README.md new file mode 100644 index 0000000000..02a0de0b58 --- /dev/null +++ b/ghost/sdk/layer2/README.md @@ -0,0 +1,68 @@ +# Members Layer 2 + +## Install + +`npm install @tryghost/members-layer2 --save` + +or + +`yarn add @tryghost/members-layer2` + + +## Usage + +### As a drop in script: + +This will attack a `Members` object to the window, with a `getToken` method. + +It will automatically hide and show the elements with `data-members-signin` and `data-members-signout` attributes + +```html + +``` + +### As a library + +```html + + +``` + +```javascript +const Members = require('@tryghost/members-layer2'); + +Members.init(); // Sets up binding the elements to the login/logout state + +Members.getToken(); +``` + + +## Develop + +This is a mono repository, managed with [lerna](https://lernajs.io/). + +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. + + +## Run + +- `yarn dev` + + +## Test + +- `yarn lint` run just eslint +- `yarn test` run lint and tests + + + + +# Copyright & License + +Copyright (c) 2018 Ghost Foundation - Released under the [MIT license](LICENSE). diff --git a/ghost/sdk/layer2/drop-in.js b/ghost/sdk/layer2/drop-in.js new file mode 100644 index 0000000000..d813eb87e9 --- /dev/null +++ b/ghost/sdk/layer2/drop-in.js @@ -0,0 +1,6 @@ +const Members = require('./'); +const ready = require('document-ready'); + +ready(Members.init); + +module.exports = Members; diff --git a/ghost/sdk/layer2/index.js b/ghost/sdk/layer2/index.js new file mode 100644 index 0000000000..3832a709d2 --- /dev/null +++ b/ghost/sdk/layer2/index.js @@ -0,0 +1,45 @@ +const Members = require('@tryghost/members-layer1'); + +const members = Members.create(); + +const show = el => el.style.display = 'block'; +const hide = el => el.style.display = 'none'; + +module.exports = { + getToken() { + return members.getToken(); + }, + init() { + const signin = document.querySelector('[data-members-signin]'); + const signout = document.querySelector('[data-members-signout]'); + + const render = (signedIn) => { + const promise = signedIn !== undefined ? Promise.resolve(signedIn) : members.getToken(); + return promise.then((token) => { + if (token) { + show(signout); + hide(signin); + } else { + show(signin); + hide(signout); + } + }); + }; + + signin.addEventListener('click', (event) => { + event.preventDefault(); + members.login() + .then(render); + }); + + signout.addEventListener('click', (event) => { + event.preventDefault(); + members.logout() + .then((success) => { + render(!success); + }); + }); + + return render(); + } +}; diff --git a/ghost/sdk/layer2/package.json b/ghost/sdk/layer2/package.json new file mode 100644 index 0000000000..0010990816 --- /dev/null +++ b/ghost/sdk/layer2/package.json @@ -0,0 +1,37 @@ +{ + "name": "@tryghost/members-layer2", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Ghost-SDKs/tree/master/packages/members/layer2", + "author": "Ghost Foundation", + "license": "MIT", + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test": "NODE_ENV=testing mocha './test/**/*.test.js'", + "lint": "eslint . --ext .js --cache", + "build:lib": "browserify -t varify -t es6ify -s Members index.js > build/members-layer2.lib.js", + "build:drop-in": "browserify -t varify -t es6ify -s Members drop-in.js > build/members-layer2.dropin.js", + "prebuild": "rm -rf build && mkdir build", + "build": "npm run build:lib && npm run build:drop-in", + "posttest": "yarn lint" + }, + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "browserify": "^16.2.3", + "es6ify": "^1.6.0", + "eslint": "^5.9.0", + "mocha": "5.2.0", + "should": "13.2.3", + "sinon": "7.1.1", + "varify": "^0.2.0" + }, + "dependencies": { + "@tryghost/members-layer1": "file:../layer1", + "bluebird": "^3.5.3", + "document-ready": "^2.0.1", + "ghost-ignition": "^2.9.6", + "lodash": "^4.17.11" + } +} diff --git a/ghost/sdk/layer2/test/.eslintrc.js b/ghost/sdk/layer2/test/.eslintrc.js new file mode 100644 index 0000000000..edb3308632 --- /dev/null +++ b/ghost/sdk/layer2/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test', + ] +}; diff --git a/ghost/sdk/layer2/test/hello.test.js b/ghost/sdk/layer2/test/hello.test.js new file mode 100644 index 0000000000..85d69d1e08 --- /dev/null +++ b/ghost/sdk/layer2/test/hello.test.js @@ -0,0 +1,10 @@ +// Switch these lines once there are useful utils +// const testUtils = require('./utils'); +require('./utils'); + +describe('Hello world', function () { + it('Runs a test', function () { + // TODO: Write me! + 'hello'.should.eql('hello'); + }); +}); diff --git a/ghost/sdk/layer2/test/utils/assertions.js b/ghost/sdk/layer2/test/utils/assertions.js new file mode 100644 index 0000000000..7364ee8aa1 --- /dev/null +++ b/ghost/sdk/layer2/test/utils/assertions.js @@ -0,0 +1,11 @@ +/** + * Custom Should Assertions + * + * Add any custom assertions to this file. + */ + +// Example Assertion +// should.Assertion.add('ExampleAssertion', function () { +// this.params = {operator: 'to be a valid Example Assertion'}; +// this.obj.should.be.an.Object; +// }); diff --git a/ghost/sdk/layer2/test/utils/index.js b/ghost/sdk/layer2/test/utils/index.js new file mode 100644 index 0000000000..0d67d86ff8 --- /dev/null +++ b/ghost/sdk/layer2/test/utils/index.js @@ -0,0 +1,11 @@ +/** + * Test Utilities + * + * Shared utils for writing tests + */ + +// Require overrides - these add globals for tests +require('./overrides'); + +// Require assertions - adds custom should assertions +require('./assertions'); diff --git a/ghost/sdk/layer2/test/utils/overrides.js b/ghost/sdk/layer2/test/utils/overrides.js new file mode 100644 index 0000000000..90203424ee --- /dev/null +++ b/ghost/sdk/layer2/test/utils/overrides.js @@ -0,0 +1,10 @@ +// This file is required before any test is run + +// Taken from the should wiki, this is how to make should global +// Should is a global in our eslint test config +global.should = require('should').noConflict(); +should.extend(); + +// Sinon is a simple case +// Sinon is a global in our eslint test config +global.sinon = require('sinon');