Skip to content

Commit

Permalink
Introduce basic model, collection and error patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
suda committed Feb 10, 2017
1 parent 766c4d0 commit 22f578d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions src/collections/Base.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 14 additions & 0 deletions src/errors/CustomError.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/errors/NotImplementedError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import CustomError from './CustomError';

export default class NotImplementedError extends CustomError {}
11 changes: 11 additions & 0 deletions src/models/Base.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions src/models/BuildTarget.js
Original file line number Diff line number Diff line change
@@ -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 {

}
9 changes: 9 additions & 0 deletions test/errors/NotImplementedError.js
Original file line number Diff line number Diff line change
@@ -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;
})
});

0 comments on commit 22f578d

Please sign in to comment.