From 2a2443ef54e0f2496a23456835f0e7b3b7c14c2a Mon Sep 17 00:00:00 2001 From: Wojtek Siudzinski Date: Fri, 10 Feb 2017 14:23:26 +0000 Subject: [PATCH 1/2] Move models to separate directory --- src/Client.js | 2 +- src/{ => models}/Library.js | 0 test/Client.spec.js | 2 +- test/{ => models}/Library.spec.js | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename src/{ => models}/Library.js (100%) rename test/{ => models}/Library.spec.js (94%) diff --git a/src/Client.js b/src/Client.js index 7b46a2b..70a48a6 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1,5 +1,5 @@ import Particle from './Particle'; -import Library from './Library'; +import Library from './models/Library'; export default class Client { constructor({ auth, api = new Particle() }) { diff --git a/src/Library.js b/src/models/Library.js similarity index 100% rename from src/Library.js rename to src/models/Library.js diff --git a/test/Client.spec.js b/test/Client.spec.js index 207ad52..4e79640 100644 --- a/test/Client.spec.js +++ b/test/Client.spec.js @@ -2,7 +2,7 @@ import {expect, sinon} from './test-setup'; import Client from '../src/Client'; import * as fixtures from './fixtures'; -import Library from '../src/Library'; +import Library from '../src/models/Library'; let api; const token = 'tok'; diff --git a/test/Library.spec.js b/test/models/Library.spec.js similarity index 94% rename from test/Library.spec.js rename to test/models/Library.spec.js index 628f0f5..d6931f3 100644 --- a/test/Library.spec.js +++ b/test/models/Library.spec.js @@ -1,5 +1,5 @@ import {expect} from './test-setup'; -import Library from '../src/Library'; +import Library from '../../src/models/Library'; let client = {}; describe('Library', () => { From 65de0dbe72ca5d7e91beed1f314fe02298a41b5d Mon Sep 17 00:00:00 2001 From: Wojtek Siudzinski Date: Fri, 10 Feb 2017 17:43:32 +0000 Subject: [PATCH 2/2] Introduce basic model, collection and error patterns --- package.json | 3 +- src/collections/Base.js | 49 ++++++++++++++++++++++++++++++ src/errors/CustomError.js | 14 +++++++++ src/errors/NotImplementedError.js | 3 ++ src/models/Base.js | 11 +++++++ src/models/BuildTarget.js | 12 ++++++++ test/errors/NotImplementedError.js | 9 ++++++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/collections/Base.js create mode 100644 src/errors/CustomError.js create mode 100644 src/errors/NotImplementedError.js create mode 100644 src/models/Base.js create mode 100644 src/models/BuildTarget.js create mode 100644 test/errors/NotImplementedError.js diff --git a/package.json b/package.json index 5b1d359..9c28e1f 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,8 @@ "form-data": "https://github.com/spark/form-data/archive/v1.0.0-relative-path.tar.gz", "stream-http": "https://github.com/spark/stream-http/archive/v2.2.1.tar.gz", "superagent": "^2.2.0", - "superagent-prefix": "0.0.2" + "superagent-prefix": "0.0.2", + "verror": "^1.9.0" }, "browser": { "http": "stream-http", diff --git a/src/collections/Base.js b/src/collections/Base.js new file mode 100644 index 0000000..ff1208f --- /dev/null +++ b/src/collections/Base.js @@ -0,0 +1,49 @@ +import NotImplementedError from '../errors/NotImplementedError'; + +/** + * @interface + */ +export default class Base { + /** + * @param {Object} [filter] Predicate used when fetching + * @return {Promise} + */ + fetch(filter={}) { + throw new NotImplementedError(); + } + + /** + * @property {boolean} hasNextPage Indicates if collection has a next page + */ + get hasNextPage() { + throw new NotImplementedError(); + } + + /** + * @return {Promise} + */ + nextPage() { + throw new NotImplementedError(); + } + + /** + * @property {boolean} hasPrevPage Indicates if collection has a previous page + */ + get hasPrevPage() { + throw new NotImplementedError(); + } + + /** + * @return {Promise} + */ + prevPage() { + throw new NotImplementedError(); + } + + /** + * @property {number} page Indicates current page number + */ + get page() { + throw new NotImplementedError(); + } +} diff --git a/src/errors/CustomError.js b/src/errors/CustomError.js new file mode 100644 index 0000000..571694e --- /dev/null +++ b/src/errors/CustomError.js @@ -0,0 +1,14 @@ +import VError from 'VError'; + +class CustomError extends VError { + constructor() { + super(...arguments); + this.name = this.constructor.name; + } + + static matches(error) { + return error.name === this.name; + } +} + +export default CustomError; diff --git a/src/errors/NotImplementedError.js b/src/errors/NotImplementedError.js new file mode 100644 index 0000000..f28b624 --- /dev/null +++ b/src/errors/NotImplementedError.js @@ -0,0 +1,3 @@ +import CustomError from './CustomError'; + +export default class NotImplementedError extends CustomError {} diff --git a/src/models/Base.js b/src/models/Base.js new file mode 100644 index 0000000..1d3131d --- /dev/null +++ b/src/models/Base.js @@ -0,0 +1,11 @@ +export default class Base { + constructor(client, data) { + // Make client non-enumerable so it doesn't show up in Object.keys, JSON.stringify, etc + Object.defineProperty(this, 'client', { value: client }); + this._assignAttributes(data); + } + + _assignAttributes(data) { + Object.assign(this, data); + } +} diff --git a/src/models/BuildTarget.js b/src/models/BuildTarget.js new file mode 100644 index 0000000..4a00882 --- /dev/null +++ b/src/models/BuildTarget.js @@ -0,0 +1,12 @@ +import Base from './Base'; + +/** + * @property {string} version + * @property {number} platformId + * @property {boolean} featured + * @property {boolean} prerelease + * @property {string} vendor + */ +export default class BuildTarget extends Base { + +} diff --git a/test/errors/NotImplementedError.js b/test/errors/NotImplementedError.js new file mode 100644 index 0000000..065c324 --- /dev/null +++ b/test/errors/NotImplementedError.js @@ -0,0 +1,9 @@ +import {expect} from '../test-setup'; +import NotImplementedError from '../../src/errors/NotImplementedError'; + +describe('NotImplementedError', () => { + it('matches the error', () => { + let exception = new NotImplementedError(); + expect(NotImplementedError.matches(exception)).to.be.true; + }) +});