From 87dd8c2cf986999d8a948ccdc7715f956213350a Mon Sep 17 00:00:00 2001 From: Pau Ramon Revilla Date: Thu, 9 Mar 2017 22:38:00 +0100 Subject: [PATCH] Cleanup the release --- package.json | 2 +- src/Collection.js | 52 +++++++++++++++++---------------------- src/ErrorObject.js | 12 +++++++++ src/Model.js | 61 +++++++++++++++++++--------------------------- src/Request.js | 15 ++++++++++++ src/types.js | 18 +++++--------- 6 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 src/ErrorObject.js create mode 100644 src/Request.js diff --git a/package.json b/package.json index 887e257..68819b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "2.0.0", + "version": "2.0.2", "description": "REST conventions for mobx.", "repository": { "type": "git", diff --git a/src/Collection.js b/src/Collection.js index f11275e..a147db8 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -1,5 +1,5 @@ // @flow -import { observable, action, IObservableArray, runInAction } from 'mobx' +import { observable, action, IObservableArray, runInAction, toJS } from 'mobx' import Model from './Model' import { isEmpty, @@ -11,12 +11,14 @@ import { map, last } from 'lodash' +import ErrorObject from './ErrorObject' +import Request from './Request' import apiClient from './apiClient' -import type { Label, CreateOptions, ErrorType, Request, SetOptions, Id } from './types' +import type { Label, CreateOptions, SetOptions, Id } from './types' export default class Collection { - request: ?Request = observable.shallowObject(null) - error: ?ErrorType = observable.shallowObject(null) + @observable request: ?Request = null + @observable error: ?ErrorObject = null models: IObservableArray = observable.shallowArray([]) constructor (data: Array<{[key: string]: any}> = []) { @@ -39,6 +41,14 @@ export default class Collection { return Model } + /** + * Returns a JSON representation + * of the collection + */ + toJS () { + return toJS(this.models) + } + /** * Questions whether the request exists * and matches a certain label @@ -192,18 +202,10 @@ export default class Collection { model = attributesOrModel instanceof Model ? attributesOrModel : last(this.add([attributesOrModel])) - model.request = { - label, - abort, - progress: 0 - } + model.request = new Request(label, abort, 0) } - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let data: {} @@ -214,7 +216,7 @@ export default class Collection { if (model) { this.remove([model.id]) } - this.error = { label, body } + this.error = new ErrorObject(label, body) this.request = null }) @@ -249,23 +251,19 @@ export default class Collection { options.data ) - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let data: Array<{[key: string]: any}> try { data = await promise - } catch (err) { + } catch (body) { runInAction('fetch-error', () => { - this.error = { label, body: err } + this.error = new ErrorObject(label, body) this.request = null }) - throw err + throw body } runInAction('fetch-done', () => { @@ -292,11 +290,7 @@ export default class Collection { body || {} ) - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let response @@ -305,7 +299,7 @@ export default class Collection { } catch (body) { runInAction('accept-fail', () => { this.request = null - this.error = { label, body } + this.error = new ErrorObject(label, body) }) throw body diff --git a/src/ErrorObject.js b/src/ErrorObject.js new file mode 100644 index 0000000..946bd8d --- /dev/null +++ b/src/ErrorObject.js @@ -0,0 +1,12 @@ +// @flow +import type { Label } from './types' + +export default class Request { + label: Label + body: {} + + constructor (label: Label, body: {}) { + this.label = label + this.body = body + } +} diff --git a/src/Model.js b/src/Model.js index 2e4153f..2b0f6c0 100644 --- a/src/Model.js +++ b/src/Model.js @@ -4,15 +4,16 @@ import { action, ObservableMap, computed, - runInAction + runInAction, + toJS } from 'mobx' import Collection from './Collection' import { uniqueId, isString, debounce } from 'lodash' import apiClient from './apiClient' +import Request from './Request' +import ErrorObject from './ErrorObject' import type { OptimisticId, - ErrorType, - Request, Id, Label, DestroyOptions, @@ -21,17 +22,25 @@ import type { } from './types' export default class Model { - request: ?Request = observable.shallowObject(null) - error: ?ErrorType = observable.shallowObject(null) + @observable request: ?Request = null + @observable error: ?ErrorObject = null + attributes: ObservableMap optimisticId: OptimisticId = uniqueId('i_') collection: ?Collection<*> = null - attributes: ObservableMap constructor (attributes: {[key: string]: any} = {}) { this.attributes = observable.map(attributes) } + /** + * Returns a JSON representation + * of the model + */ + toJS () { + return toJS(this.attributes) + } + /** * Return the base url used in * the `url` method @@ -133,11 +142,7 @@ export default class Model { options.data ) - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let data @@ -145,7 +150,7 @@ export default class Model { data = await promise } catch (body) { runInAction('fetch-error', () => { - this.error = { label, body } + this.error = new ErrorObject(label, body) this.request = null }) @@ -205,11 +210,7 @@ export default class Model { if (optimistic) this.set(newAttributes) - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let response @@ -219,7 +220,7 @@ export default class Model { runInAction('save-fail', () => { this.request = null this.set(originalAttributes) - this.error = { label, body } + this.error = new ErrorObject(label, body) }) throw isString(body) ? new Error(body) : body @@ -256,11 +257,7 @@ export default class Model { ) if (optimistic) { - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) } let data: {} @@ -269,7 +266,7 @@ export default class Model { data = await promise } catch (body) { runInAction('create-error', () => { - this.error = { label, body } + this.error = new ErrorObject(label, body) this.request = null }) @@ -305,11 +302,7 @@ export default class Model { this.collection.remove([this.id]) } - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) try { await promise @@ -318,7 +311,7 @@ export default class Model { if (optimistic && this.collection) { this.collection.add([this.attributes.toJS()]) } - this.error = { label, body } + this.error = new ErrorObject(label, body) this.request = null }) @@ -351,11 +344,7 @@ export default class Model { body || {} ) - this.request = { - label, - abort, - progress: 0 - } + this.request = new Request(label, abort, 0) let response @@ -364,7 +353,7 @@ export default class Model { } catch (body) { runInAction('accept-fail', () => { this.request = null - this.error = { label, body } + this.error = new ErrorObject(label, body) }) throw body diff --git a/src/Request.js b/src/Request.js new file mode 100644 index 0000000..b5a5fca --- /dev/null +++ b/src/Request.js @@ -0,0 +1,15 @@ +// @flow +import { observable } from 'mobx' +import type { Label } from './types' + +export default class Request { + label: Label + abort: () => void + progress: number + + constructor (label: Label, abort: () => void, progress: number) { + this.label = label + this.abort = abort + this.progress = observable(progress) + } +} diff --git a/src/types.js b/src/types.js index 40cec91..0e53565 100644 --- a/src/types.js +++ b/src/types.js @@ -19,15 +19,9 @@ export type SaveOptions = { onProgress?: () => mixed; } -export type ErrorType = { - label: Label; - body: {}; -} - -export type Request = { - label: Label; +export type Response = { abort: () => void; - progress: number; + promise: Promise<*>; } export type SetOptions = { @@ -38,8 +32,8 @@ export type SetOptions = { } export type Adapter = { - get (path: string, data?: {}, options?: {}): Request; - post (path: string, data?: {}, options?: {}): Request; - put (path: string, data?: {}, options?: {}): Request; - del (path: string, options?: {}): Request; + get (path: string, data?: {}, options?: {}): Response; + post (path: string, data?: {}, options?: {}): Response; + put (path: string, data?: {}, options?: {}): Response; + del (path: string, options?: {}): Response; }