potion-client
Advanced tools
Comparing version 0.0.10 to 0.0.12
@@ -18,3 +18,2 @@ "use strict"; | ||
this.$get = ['$cacheFactory', '$q', '$http', function ($cacheFactory, $q, $http) { | ||
var cache = $cacheFactory.get('potion') || $cacheFactory('potion'); | ||
var Potion = (function (_super) { | ||
@@ -36,4 +35,4 @@ __extends(Potion, _super); | ||
/* tslint:disable: align */ | ||
return Potion.create(Object.assign({ | ||
itemCache: cache | ||
return new Potion(Object.assign({ | ||
itemCache: $cacheFactory('potion') | ||
}, options)); | ||
@@ -40,0 +39,0 @@ }]; |
export declare function readonly(target: any, property: any): void; | ||
export interface ItemConstructor { | ||
new (object: any): Item; | ||
potion: PotionBase; | ||
rootURI: string; | ||
store?: Store<Item>; | ||
} | ||
export interface ItemOptions { | ||
@@ -6,6 +12,7 @@ 'readonly'?: string[]; | ||
export declare class Item { | ||
static potion: PotionBase; | ||
static rootURI: string; | ||
static store: Store<any>; | ||
uri: string; | ||
readonly id: number; | ||
protected _potion: PotionBase; | ||
protected _rootUri: string; | ||
protected _uri: string; | ||
@@ -16,11 +23,25 @@ static fetch(id: any, options?: PotionRequestOptions): Promise<Item>; | ||
constructor(properties?: any, options?: ItemOptions); | ||
toJSON(): {}; | ||
save(): Promise<Item>; | ||
update(properties?: any): Promise<Item>; | ||
destroy(): Promise<Item>; | ||
toJSON(): {}; | ||
} | ||
export declare class Store<T extends Item> { | ||
cache: PotionItemCache<Item>; | ||
promise: any; | ||
protected _potion: PotionBase; | ||
protected _rootURI: string; | ||
private _promises; | ||
constructor(constructor: ItemConstructor); | ||
query(...args: any[]): Promise<any>; | ||
fetch(uri: any, options?: PotionRequestOptions): Promise<any>; | ||
update(item: Item, data?: any): Promise<any>; | ||
save(data?: any): Promise<any>; | ||
destroy(item: Item): Promise<any>; | ||
private _fromPotionJSON(json); | ||
} | ||
export interface PotionItemCache<T extends Item> { | ||
get(id: string): T; | ||
put(id: string, item: T): T; | ||
remove(id: string): void; | ||
get(key: string): T; | ||
put(key: string, item: T): T; | ||
remove(key: string): void; | ||
} | ||
@@ -32,3 +53,3 @@ export interface PotionOptions { | ||
export interface PotionRequestOptions { | ||
method?: 'GET' | 'PUT' | 'DELETE' | 'POST'; | ||
method?: 'GET' | 'PUT' | 'PATCH' | 'DELETE' | 'POST'; | ||
cache?: any; | ||
@@ -38,10 +59,8 @@ data?: any; | ||
export declare abstract class PotionBase { | ||
protected static promise: any; | ||
static promise: any; | ||
resources: {}; | ||
itemCache: PotionItemCache<Item>; | ||
private _prefix; | ||
private _itemCache; | ||
private _promises; | ||
static create(...args: any[]): any; | ||
constructor({prefix, itemCache}?: PotionOptions); | ||
parseUri(uri: string): { | ||
parseURI(uri: string): { | ||
uri: string; | ||
@@ -53,16 +72,12 @@ resource: any; | ||
request(uri: any, options?: PotionRequestOptions): Promise<any>; | ||
get(uri: any, options?: PotionRequestOptions): Promise<any>; | ||
update(item: Item, data?: any): Promise<any>; | ||
save(rootUri: string, data?: any): Promise<any>; | ||
destroy(item: Item): Promise<any>; | ||
register(uri: string, resource: any): void; | ||
registerAs(uri: string): ClassDecorator; | ||
private _fromPotionJson(json); | ||
} | ||
export declare function route(uri: string, {method}?: PotionRequestOptions): (options?) => Promise<any>; | ||
export declare class Route { | ||
static GET(uri: string): (options?: any) => Promise<any>; | ||
static DELETE(uri: string): (options?: any) => Promise<any>; | ||
static POST(uri: string): (options?: any) => Promise<any>; | ||
static PUT(uri: string): (options?: any) => Promise<any>; | ||
} | ||
export declare let Route: { | ||
GET(uri: string): (options?: any) => Promise<any>; | ||
DELETE(uri: string): (options?: any) => Promise<any>; | ||
POST(uri: string): (options?: any) => Promise<any>; | ||
PATCH(uri: string): (options?: any) => Promise<any>; | ||
PUT(uri: string): (options?: any) => Promise<any>; | ||
}; |
226
base.js
@@ -19,7 +19,4 @@ "use strict"; | ||
exports.readonly = readonly; | ||
var _potionMetadataKey = Symbol('potion'); | ||
var _potionUriMetadataKey = Symbol('potion:uri'); | ||
function potionForCtor(ctor) { | ||
return { potion: Reflect.getMetadata(_potionMetadataKey, ctor), rootUri: Reflect.getMetadata(_potionUriMetadataKey, ctor) }; | ||
} | ||
// TODO: when https://github.com/Microsoft/TypeScript/issues/3964 is implemented, | ||
// use `let uri = Symbol('Resource uri');` and `class Item {[uri]: string}` | ||
var Item = (function () { | ||
@@ -30,5 +27,2 @@ function Item(properties, options) { | ||
Object.assign(this, properties); | ||
var _a = potionForCtor(this.constructor), potion = _a.potion, rootUri = _a.rootUri; | ||
this._potion = potion; | ||
this._rootUri = rootUri; | ||
if (options && Array.isArray(options.readonly)) { | ||
@@ -53,3 +47,3 @@ options.readonly.forEach(function (property) { return readonly(_this, property); }); | ||
} | ||
var params = this._potion.parseUri(this.uri).params; | ||
var params = this.constructor.potion.parseURI(this.uri).params; | ||
return parseInt(params[0], 10); | ||
@@ -61,8 +55,6 @@ }, | ||
Item.fetch = function (id, options) { | ||
var _a = potionForCtor(this), potion = _a.potion, rootUri = _a.rootUri; | ||
return potion.get(rootUri + "/" + id, options); | ||
return this.store.query(id, options); | ||
}; | ||
Item.query = function (options) { | ||
var _a = potionForCtor(this), potion = _a.potion, rootUri = _a.rootUri; | ||
return potion.get(rootUri, options); | ||
return this.store.query(options); | ||
}; | ||
@@ -76,9 +68,20 @@ Item.create = function () { | ||
}; | ||
Item.prototype.save = function () { | ||
return this.constructor.store.save(this.toJSON()); | ||
}; | ||
Item.prototype.update = function (properties) { | ||
if (properties === void 0) { properties = {}; } | ||
return this.constructor.store.update(this, properties); | ||
}; | ||
Item.prototype.destroy = function () { | ||
return this.constructor.store.destroy(this); | ||
}; | ||
Item.prototype.toJSON = function () { | ||
var _this = this; | ||
var properties = {}; | ||
Object.keys(this) | ||
Object | ||
.keys(this) | ||
.filter(function (key) { | ||
var metadata = Reflect.getMetadata(_readonlyMetadataKey, _this.constructor); | ||
return key !== '_uri' && key !== '_potion' && key !== '_rootUri' && (!metadata || (metadata && !metadata[key])); | ||
return key !== '_uri' && (!metadata || (metadata && !metadata[key])); | ||
}) | ||
@@ -90,24 +93,15 @@ .forEach(function (key) { | ||
}; | ||
Item.prototype.save = function () { | ||
return this._potion.save(this._rootUri, this.toJSON()); | ||
}; | ||
Item.prototype.update = function (properties) { | ||
if (properties === void 0) { properties = {}; } | ||
return this._potion.update(this, properties); | ||
}; | ||
Item.prototype.destroy = function () { | ||
return this._potion.destroy(this); | ||
}; | ||
return Item; | ||
}()); | ||
exports.Item = Item; | ||
var PotionBase = (function () { | ||
function PotionBase(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.prefix, prefix = _c === void 0 ? '' : _c, itemCache = _b.itemCache; | ||
this.resources = {}; | ||
var Store = (function () { | ||
function Store(constructor) { | ||
this._promises = []; | ||
this._prefix = prefix; | ||
this._itemCache = itemCache; | ||
var potion = constructor.potion, rootURI = constructor.rootURI; | ||
this.cache = potion.itemCache; | ||
this.promise = potion.constructor.promise; | ||
this._potion = potion; | ||
this._rootURI = rootURI; | ||
} | ||
PotionBase.create = function () { | ||
Store.prototype.query = function () { | ||
var args = []; | ||
@@ -117,31 +111,19 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
return Reflect.construct(this, args); | ||
}; | ||
PotionBase.prototype.parseUri = function (uri) { | ||
uri = decodeURIComponent(uri); | ||
if (uri.indexOf(this._prefix) === 0) { | ||
uri = uri.substring(this._prefix.length); | ||
var id = args[0], options = args[1]; | ||
var uri = this._rootURI; | ||
if (typeof id === 'number') { | ||
uri = uri + "/" + id; | ||
} | ||
for (var _i = 0, _a = Object.entries(this.resources); _i < _a.length; _i++) { | ||
var resourceURI = _a[_i][0]; | ||
if (uri.indexOf(resourceURI + "/") === 0) { | ||
return { uri: uri, resource: this.resources[resourceURI], params: uri.substring(resourceURI.length + 1).split('/') }; | ||
} | ||
else { | ||
options = id; | ||
} | ||
throw new Error("Uninterpretable or unknown resource URI: " + uri); | ||
}; | ||
PotionBase.prototype.request = function (uri, options) { | ||
// Add the API prefix if not present | ||
if (uri.indexOf(this._prefix) === -1) { | ||
uri = "" + this._prefix + uri; | ||
} | ||
return this.fetch(uri, options); | ||
}; | ||
PotionBase.prototype.get = function (uri, options) { | ||
Store.prototype.fetch = function (uri, options) { | ||
var _this = this; | ||
// Try to get from cache | ||
if (this._itemCache && this._itemCache.get) { | ||
var item = this._itemCache.get(uri); | ||
if (this.cache && this.cache.get) { | ||
var item = this.cache.get(uri); | ||
if (item) { | ||
return this.constructor.promise.resolve(item); | ||
return this.promise.resolve(item); | ||
} | ||
@@ -159,55 +141,43 @@ } | ||
// Enforce GET method | ||
promise = this._promises[uri] = this.request(uri, Object.assign({}, options, { method: 'GET' })).then(function (json) { | ||
promise = this._promises[uri] = this._potion.request(uri, Object.assign({}, options, { method: 'GET' })).then(function (json) { | ||
delete _this._promises[uri]; // Remove pending request | ||
return _this._fromPotionJson(json); | ||
return _this._fromPotionJSON(json); | ||
}); | ||
return promise; | ||
}; | ||
PotionBase.prototype.update = function (item, data) { | ||
Store.prototype.update = function (item, data) { | ||
var _this = this; | ||
if (data === void 0) { data = {}; } | ||
return this.request(item.uri, { data: data, method: 'PUT' }).then(function (json) { return _this._fromPotionJson(json); }); | ||
return this._potion.request(item.uri, { data: data, method: 'PATCH' }).then(function (json) { return _this._fromPotionJSON(json); }); | ||
}; | ||
PotionBase.prototype.save = function (rootUri, data) { | ||
Store.prototype.save = function (data) { | ||
var _this = this; | ||
if (data === void 0) { data = {}; } | ||
return this.request(rootUri, { data: data, method: 'POST' }).then(function (json) { return _this._fromPotionJson(json); }); | ||
return this._potion.request(this._rootURI, { data: data, method: 'POST' }).then(function (json) { return _this._fromPotionJSON(json); }); | ||
}; | ||
PotionBase.prototype.destroy = function (item) { | ||
Store.prototype.destroy = function (item) { | ||
var _this = this; | ||
var uri = item.uri; | ||
return this.request(uri, { method: 'DELETE' }).then(function () { | ||
return this._potion.request(uri, { method: 'DELETE' }).then(function () { | ||
// Clear the item from cache if exists | ||
if (_this._itemCache && _this._itemCache.get && _this._itemCache.get(uri)) { | ||
_this._itemCache.remove(uri); | ||
if (_this.cache && _this.cache.get && _this.cache.get(uri)) { | ||
_this.cache.remove(uri); | ||
} | ||
}); | ||
}; | ||
PotionBase.prototype.register = function (uri, resource) { | ||
Reflect.defineMetadata(_potionMetadataKey, this, resource); | ||
Reflect.defineMetadata(_potionUriMetadataKey, uri, resource); | ||
this.resources[uri] = resource; | ||
}; | ||
PotionBase.prototype.registerAs = function (uri) { | ||
Store.prototype._fromPotionJSON = function (json) { | ||
var _this = this; | ||
return function (target) { | ||
_this.register(uri, target); | ||
return target; | ||
}; | ||
}; | ||
PotionBase.prototype._fromPotionJson = function (json) { | ||
var _this = this; | ||
if (typeof json === 'object' && json !== null) { | ||
if (json instanceof Array) { | ||
return this.constructor.promise.all(json.map(function (item) { return _this._fromPotionJson(item); })); | ||
return this.promise.all(json.map(function (item) { return _this._fromPotionJSON(item); })); | ||
} | ||
else if (typeof json.$uri === 'string') { | ||
var _a = this.parseUri(json.$uri), resource_1 = _a.resource, uri_1 = _a.uri; | ||
var _a = this._potion.parseURI(json.$uri), resource_1 = _a.resource, uri_1 = _a.uri; | ||
var promises_1 = []; | ||
var _loop_1 = function(key) { | ||
if (key === '$uri') { | ||
promises_1.push(this_1.constructor.promise.resolve([key, uri_1])); | ||
promises_1.push(this_1.promise.resolve([key, uri_1])); | ||
} | ||
else { | ||
promises_1.push(this_1._fromPotionJson(json[key]).then(function (value) { | ||
promises_1.push(this_1._fromPotionJSON(json[key]).then(function (value) { | ||
return [utils_1.toCamelCase(key), value]; | ||
@@ -222,3 +192,3 @@ })); | ||
} | ||
return this.constructor.promise.all(promises_1).then(function (propertyValuePairs) { | ||
return this.promise.all(promises_1).then(function (propertyValuePairs) { | ||
var properties = utils_1.pairsToObject(propertyValuePairs); // `propertyValuePairs` is a collection of [key, value] pairs | ||
@@ -232,4 +202,4 @@ var obj = {}; | ||
var item = Reflect.construct(resource_1, [obj]); | ||
if (_this._itemCache && _this._itemCache.put) { | ||
_this._itemCache.put(uri_1, item); | ||
if (_this.cache && _this.cache.put) { | ||
_this.cache.put(uri_1, item); | ||
} | ||
@@ -241,7 +211,7 @@ return item; | ||
if (typeof json.$ref === 'string') { | ||
var uri = this.parseUri(json.$ref).uri; | ||
return this.get(uri); | ||
var uri = this._potion.parseURI(json.$ref).uri; | ||
return this.fetch(uri); | ||
} | ||
else if (typeof json.$date !== 'undefined') { | ||
return this.constructor.promise.resolve(new Date(json.$date)); | ||
return this.promise.resolve(new Date(json.$date)); | ||
} | ||
@@ -251,3 +221,3 @@ } | ||
var _loop_2 = function(key) { | ||
promises.push(this_2._fromPotionJson(json[key]).then(function (value) { | ||
promises.push(this_2._fromPotionJSON(json[key]).then(function (value) { | ||
return [utils_1.toCamelCase(key), value]; | ||
@@ -261,3 +231,3 @@ })); | ||
} | ||
return this.constructor.promise.all(promises).then(function (propertyValuePairs) { | ||
return this.promise.all(promises).then(function (propertyValuePairs) { | ||
return utils_1.pairsToObject(propertyValuePairs); | ||
@@ -267,5 +237,49 @@ }); | ||
else { | ||
return this.constructor.promise.resolve(json); | ||
return this.promise.resolve(json); | ||
} | ||
}; | ||
return Store; | ||
}()); | ||
exports.Store = Store; | ||
var PotionBase = (function () { | ||
function PotionBase(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.prefix, prefix = _c === void 0 ? '' : _c, itemCache = _b.itemCache; | ||
this.resources = {}; | ||
this._prefix = prefix; | ||
this.itemCache = itemCache; | ||
} | ||
PotionBase.prototype.parseURI = function (uri) { | ||
uri = decodeURIComponent(uri); | ||
if (uri.indexOf(this._prefix) === 0) { | ||
uri = uri.substring(this._prefix.length); | ||
} | ||
for (var _i = 0, _a = Object.entries(this.resources); _i < _a.length; _i++) { | ||
var resourceURI = _a[_i][0]; | ||
if (uri.indexOf(resourceURI + "/") === 0) { | ||
return { uri: uri, resource: this.resources[resourceURI], params: uri.substring(resourceURI.length + 1).split('/') }; | ||
} | ||
} | ||
throw new Error("Uninterpretable or unknown resource URI: " + uri); | ||
}; | ||
PotionBase.prototype.request = function (uri, options) { | ||
// Add the API prefix if not present | ||
if (uri.indexOf(this._prefix) === -1) { | ||
uri = "" + this._prefix + uri; | ||
} | ||
return this.fetch(uri, options); | ||
}; | ||
PotionBase.prototype.register = function (uri, resource) { | ||
this.resources[uri] = resource; | ||
// `potion` and `rootURI` props must be set before `store` | ||
resource.potion = this; | ||
resource.rootURI = uri; | ||
resource.store = new Store(resource); | ||
}; | ||
PotionBase.prototype.registerAs = function (uri) { | ||
var _this = this; | ||
return function (target) { | ||
_this.register(uri, target); | ||
return target; | ||
}; | ||
}; | ||
PotionBase.promise = window.Promise; | ||
@@ -279,25 +293,25 @@ return PotionBase; | ||
var isCtor = typeof this === 'function'; | ||
var _a = potionForCtor(isCtor ? this : this.constructor), potion = _a.potion, rootUri = _a.rootUri; | ||
return potion.get("" + (isCtor ? rootUri : this.uri) + uri, Object.assign({ method: method }, options)); | ||
var _a = isCtor ? this : this.constructor, store = _a.store, rootURI = _a.rootURI; | ||
return store.fetch("" + (isCtor ? rootURI : this.uri) + uri, Object.assign({ method: method }, options)); | ||
}; | ||
} | ||
exports.route = route; | ||
var Route = (function () { | ||
function Route() { | ||
} | ||
Route.GET = function (uri) { | ||
/* tslint:disable: variable-name */ | ||
exports.Route = { | ||
GET: function (uri) { | ||
return route(uri, { method: 'GET' }); | ||
}; | ||
Route.DELETE = function (uri) { | ||
}, | ||
DELETE: function (uri) { | ||
return route(uri, { method: 'DELETE' }); | ||
}; | ||
Route.POST = function (uri) { | ||
}, | ||
POST: function (uri) { | ||
return route(uri, { method: 'POST' }); | ||
}; | ||
Route.PUT = function (uri) { | ||
}, | ||
PATCH: function (uri) { | ||
return route(uri, { method: 'PATCH' }); | ||
}, | ||
PUT: function (uri) { | ||
return route(uri, { method: 'PUT' }); | ||
}; | ||
return Route; | ||
}()); | ||
exports.Route = Route; | ||
} | ||
}; | ||
//# sourceMappingURL=base.js.map |
@@ -37,3 +37,3 @@ "use strict"; | ||
.then(parseAsText) | ||
.then(parseAsJson); | ||
.then(parseAsJSON); | ||
}; | ||
@@ -56,3 +56,3 @@ return Potion; | ||
} | ||
function parseAsJson(text) { | ||
function parseAsJSON(text) { | ||
var json; | ||
@@ -59,0 +59,0 @@ try { |
{ | ||
"name": "potion-client", | ||
"version": "0.0.10", | ||
"version": "0.0.12", | ||
"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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40399
530