potion-client
Advanced tools
Comparing version
@@ -75,6 +75,6 @@ export declare function readonly(target: any, property: any): void; | ||
abstract request(uri: any, options?: PotionRequestOptions): Promise<any>; | ||
fetch(uri: any, options?: PotionRequestOptions): Promise<any>; | ||
fetch(uri: any, options?: PotionRequestOptions, paginationObj?: Pagination<any>): Promise<Item | Item[] | Pagination<Item> | any>; | ||
register(uri: string, resource: any): void; | ||
registerAs(uri: string): ClassDecorator; | ||
private _fromPotionJSON(json, {cache}?); | ||
private _fromPotionJSON(json); | ||
} | ||
@@ -86,3 +86,4 @@ export declare class Pagination<T extends Item> { | ||
readonly length: number; | ||
private _itemConstructor; | ||
private _potion; | ||
private _uri; | ||
private _items; | ||
@@ -92,5 +93,8 @@ private _page; | ||
private _total; | ||
constructor(itemConstructor: ItemConstructor, items: any, count: any, options: PaginationOptions); | ||
constructor({potion, uri}: { | ||
potion: any; | ||
uri: any; | ||
}, items: any, count: any, options: PaginationOptions); | ||
[Symbol.iterator](): IterableIterator<Item>; | ||
update(items: any, count: any): void; | ||
} |
109
base.js
@@ -75,3 +75,3 @@ "use strict"; | ||
// Try to get from cache | ||
if (this.cache && this.cache.get) { | ||
if (cache && this.cache && this.cache.get) { | ||
var item = this.cache.get(uri); | ||
@@ -95,6 +95,5 @@ if (item) { | ||
.fetch(uri, { cache: cache, method: 'GET' }) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
.then(function (item) { | ||
delete _this._promises[uri]; // Remove pending request | ||
return data; | ||
return item; | ||
}); | ||
@@ -104,3 +103,2 @@ return promise; | ||
Store.prototype.query = function (_a, paginationObj) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.paginate, paginate = _c === void 0 ? false : _c, _d = _b.cache, cache = _d === void 0 ? true : _d, _e = _b.page, page = _e === void 0 ? 1 : _e, _f = _b.perPage, perPage = _f === void 0 ? 5 : _f; | ||
@@ -111,18 +109,3 @@ var options = { cache: cache, method: 'GET' }; | ||
} | ||
return Reflect | ||
.getMetadata(_potionMetadataKey, this._itemConstructor) | ||
.fetch(Reflect.getMetadata(_potionURIMetadataKey, this._itemConstructor), options) | ||
.then(function (_a) { | ||
var headers = _a.headers, data = _a.data; | ||
var count = headers['x-total-count'] || data.length; | ||
if (paginate) { | ||
if (paginationObj) { | ||
paginationObj.update(data, count); | ||
} | ||
else { | ||
return new Pagination(_this._itemConstructor, data, count, options.data); | ||
} | ||
} | ||
return data; | ||
}); | ||
return Reflect.getMetadata(_potionMetadataKey, this._itemConstructor).fetch(Reflect.getMetadata(_potionURIMetadataKey, this._itemConstructor), options, paginationObj); | ||
}; | ||
@@ -132,9 +115,3 @@ Store.prototype.update = function (item, data, _a) { | ||
var _b = (_a === void 0 ? {} : _a).cache, cache = _b === void 0 ? true : _b; | ||
return Reflect | ||
.getMetadata(_potionMetadataKey, this._itemConstructor) | ||
.fetch(item.uri, { data: data, cache: cache, method: 'PATCH' }) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
return data; | ||
}); | ||
return Reflect.getMetadata(_potionMetadataKey, this._itemConstructor).fetch(item.uri, { data: data, cache: cache, method: 'PATCH' }); | ||
}; | ||
@@ -144,9 +121,3 @@ Store.prototype.save = function (data, _a) { | ||
var _b = (_a === void 0 ? {} : _a).cache, cache = _b === void 0 ? true : _b; | ||
return Reflect | ||
.getMetadata(_potionMetadataKey, this._itemConstructor) | ||
.fetch(Reflect.getMetadata(_potionURIMetadataKey, this._itemConstructor), { data: data, cache: cache, method: 'POST' }) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
return data; | ||
}); | ||
return Reflect.getMetadata(_potionMetadataKey, this._itemConstructor).fetch(Reflect.getMetadata(_potionURIMetadataKey, this._itemConstructor), { data: data, cache: cache, method: 'POST' }); | ||
}; | ||
@@ -174,9 +145,3 @@ Store.prototype.destroy = function (item) { | ||
uri = "" + (isCtor ? Reflect.getMetadata(_potionURIMetadataKey, this) : this.uri) + uri; | ||
return Reflect | ||
.getMetadata(_potionMetadataKey, isCtor ? this : this.constructor) | ||
.fetch(uri, Object.assign({ method: method }, options)) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
return data; | ||
}); | ||
return Reflect.getMetadata(_potionMetadataKey, isCtor ? this : this.constructor).fetch(uri, Object.assign({ method: method }, options)); | ||
}; | ||
@@ -223,3 +188,3 @@ } | ||
}; | ||
PotionBase.prototype.fetch = function (uri, options) { | ||
PotionBase.prototype.fetch = function (uri, options, paginationObj) { | ||
var _this = this; | ||
@@ -231,5 +196,21 @@ // Add the API prefix if not present | ||
} | ||
return this.request(uri, options).then(function (_a) { | ||
return this | ||
.request(uri, options) | ||
.then(function (_a) { | ||
var data = _a.data, headers = _a.headers; | ||
return _this._fromPotionJSON(data, options).then(function (json) { return ({ headers: headers, data: json }); }); | ||
return _this._fromPotionJSON(data).then(function (json) { return ({ headers: headers, data: json }); }); | ||
}) | ||
.then(function (_a) { | ||
var headers = _a.headers, data = _a.data; | ||
var _b = options && options.data ? options.data : {}, _c = _b.page, page = _c === void 0 ? null : _c, _d = _b.perPage, perPage = _d === void 0 ? null : _d; | ||
if (page || perPage) { | ||
var count = headers['x-total-count'] || data.length; | ||
if (paginationObj) { | ||
paginationObj.update(data, count); | ||
} | ||
else { | ||
return new Pagination({ uri: uri, potion: _this }, data, count, { page: page, perPage: perPage }); | ||
} | ||
} | ||
return data; | ||
}); | ||
@@ -250,12 +231,11 @@ }; | ||
}; | ||
PotionBase.prototype._fromPotionJSON = function (json, _a) { | ||
PotionBase.prototype._fromPotionJSON = function (json) { | ||
var _this = this; | ||
var cache = (_a === void 0 ? {} : _a).cache; | ||
var promise = this.constructor.promise; | ||
if (typeof json === 'object' && json !== null) { | ||
if (json instanceof Array) { | ||
return promise.all(json.map(function (item) { return _this._fromPotionJSON(item, { cache: cache }); })); | ||
return promise.all(json.map(function (item) { return _this._fromPotionJSON(item); })); | ||
} | ||
else if (typeof json.$uri === 'string') { | ||
var _b = this.parseURI(json.$uri), resource_1 = _b.resource, uri = _b.uri; | ||
var _a = this.parseURI(json.$uri), resource_1 = _a.resource, uri = _a.uri; | ||
var promises_1 = []; | ||
@@ -267,3 +247,3 @@ var _loop_1 = function(key) { | ||
else { | ||
promises_1.push(this_1._fromPotionJSON(json[key], { cache: cache }).then(function (value) { | ||
promises_1.push(this_1._fromPotionJSON(json[key]).then(function (value) { | ||
return [utils_1.toCamelCase(key), value]; | ||
@@ -274,4 +254,4 @@ })); | ||
var this_1 = this; | ||
for (var _i = 0, _c = Object.keys(json); _i < _c.length; _i++) { | ||
var key = _c[_i]; | ||
for (var _i = 0, _b = Object.keys(json); _i < _b.length; _i++) { | ||
var key = _b[_i]; | ||
_loop_1(key); | ||
@@ -289,3 +269,3 @@ } | ||
var item = Reflect.construct(resource_1, [obj]); | ||
if (cache && _this.cache && _this.cache.put) { | ||
if (_this.cache && _this.cache.put) { | ||
_this.cache.put(uri, item); | ||
@@ -306,6 +286,3 @@ } | ||
} | ||
return this.fetch(uri).then(function (_a) { | ||
var data = _a.data; | ||
return data; | ||
}); | ||
return this.fetch(uri); | ||
} | ||
@@ -318,3 +295,3 @@ else if (typeof json.$date !== 'undefined') { | ||
var _loop_2 = function(key) { | ||
promises.push(this_2._fromPotionJSON(json[key], { cache: cache }).then(function (value) { | ||
promises.push(this_2._fromPotionJSON(json[key]).then(function (value) { | ||
return [utils_1.toCamelCase(key), value]; | ||
@@ -324,4 +301,4 @@ })); | ||
var this_2 = this; | ||
for (var _d = 0, _e = Object.keys(json); _d < _e.length; _d++) { | ||
var key = _e[_d]; | ||
for (var _c = 0, _d = Object.keys(json); _c < _d.length; _c++) { | ||
var key = _d[_c]; | ||
_loop_2(key); | ||
@@ -342,10 +319,12 @@ } | ||
var Pagination = (function () { | ||
function Pagination(itemConstructor, items, count, options) { | ||
function Pagination(_a, items, count, options) { | ||
var potion = _a.potion, uri = _a.uri; | ||
this._items = []; | ||
this._itemConstructor = itemConstructor; | ||
this._potion = potion; | ||
this._uri = uri; | ||
(_b = this._items).push.apply(_b, items); | ||
this._page = options.page; | ||
this._perPage = options.perPage; | ||
this._total = count; | ||
(_a = this._items).push.apply(_a, items); | ||
var _a; | ||
var _b; | ||
} | ||
@@ -357,3 +336,3 @@ Object.defineProperty(Pagination.prototype, "page", { | ||
set: function (page) { | ||
this._itemConstructor.store.query({ page: page, perPage: this.perPage }, this); | ||
this._potion.fetch({ page: page, perPage: this.perPage }, this); | ||
this._page = page; | ||
@@ -360,0 +339,0 @@ }, |
{ | ||
"name": "potion-client", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "A ES6 client for APIs written in Flask-Potion", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
Sorry, the diff of this file is not supported yet
49426
-1.09%615
-2.69%