Skip to content

Commit

Permalink
Made ApiClient injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Aug 28, 2016
1 parent 8f84871 commit 81fa2ab
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"es2015",
"stage-1"
],
"plugins": ["transform-flow-strip-types", "transform-decorators-legacy"]
"plugins": [
"transform-flow-strip-types",
"transform-decorators-legacy"
]
}
5 changes: 2 additions & 3 deletions src/Api.js → api_client_examples/jquery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: Move away from jQuery when a better alternative is available
import jq from 'jquery'

type Request = {
Expand All @@ -24,7 +23,7 @@ function ajax (url: string, options: {}): Request {
return {abort, promise}
}

class API {
class ApiClient {
basePath: string

/**
Expand All @@ -51,4 +50,4 @@ class API {
}
}

export default API
export default ApiClient
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "0.0.13",
"version": "0.1.0",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
17 changes: 14 additions & 3 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions test/CollectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/ModelTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 81fa2ab

Please sign in to comment.