diff --git a/.babelrc b/.babelrc index b6b98cc..7bf9aad 100644 --- a/.babelrc +++ b/.babelrc @@ -3,5 +3,8 @@ "es2015", "stage-1" ], - "plugins": ["transform-flow-strip-types", "transform-decorators-legacy"] + "plugins": [ + "transform-flow-strip-types", + "transform-decorators-legacy" + ] } diff --git a/src/Api.js b/api_client_examples/jquery.js similarity index 90% rename from src/Api.js rename to api_client_examples/jquery.js index 725e7ff..0cfc899 100644 --- a/src/Api.js +++ b/api_client_examples/jquery.js @@ -1,4 +1,3 @@ -// TODO: Move away from jQuery when a better alternative is available import jq from 'jquery' type Request = { @@ -24,7 +23,7 @@ function ajax (url: string, options: {}): Request { return {abort, promise} } -class API { +class ApiClient { basePath: string /** @@ -51,4 +50,4 @@ class API { } } -export default API +export default ApiClient diff --git a/package.json b/package.json index 4a76f27..f890332 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "0.0.13", + "version": "0.1.0", "description": "REST conventions for mobx.", "repository": { "type": "git", diff --git a/src/Collection.js b/src/Collection.js index 7092b25..e642eb5 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -2,19 +2,30 @@ // @flow import { observable, action } from 'mobx' import Model from './Model' -import Api from './Api' import arrayDiff from 'lodash.difference' import arrayLast from 'lodash.last' import type { Label, CreateOptions, Error, Request, SetOptions, Id } from './types.js' +type ApiCall = { + abort: () => void; + promise: Promise<*>; +} + +interface ApiInterface { // eslint-disable-line + fetch(path?: string): ApiCall; + post(path?: string, data: {}): ApiCall; + put(path?: string, data: {}): ApiCall; + del(path?: string): ApiCall; +} + class Collection { @observable request: ?Request @observable error: ?Error @observable models: [] = [] - api: Api + api: ApiInterface // eslint-disable-line - constructor (data: ?[]) { + constructor (data: ?[], Api: any) { this.api = new Api(this.url()) if (data) this.set(data) diff --git a/test/CollectionTest.js b/test/CollectionTest.js index 30d8ae2..5615e8d 100644 --- a/test/CollectionTest.js +++ b/test/CollectionTest.js @@ -22,9 +22,8 @@ describe('Collection', () => { beforeEach(() => { item = {id: 1, name: 'miles'} - collection = new Collection([item]) - api = new MockApi() - collection.api = api + collection = new Collection([item], MockApi) + api = collection.api }) describe('at', () => { diff --git a/test/ModelTest.js b/test/ModelTest.js index b188efa..72a21c4 100644 --- a/test/ModelTest.js +++ b/test/ModelTest.js @@ -24,10 +24,9 @@ describe('Model', () => { beforeEach(() => { item = {id: 1, name: 'miles', album: 'kind of blue'} - collection = new Collection([item]) + collection = new Collection([item], MockApi) + api = collection.api model = collection.at(0) - api = new MockApi() - collection.api = api }) describe('get', () => {