potion-client
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -361,4 +361,4 @@ import { __assign, __extends, __read, __spread, __values } from 'tslib'; | ||
} | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -393,3 +393,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -403,20 +403,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = __values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -427,3 +428,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -435,3 +436,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -441,14 +442,18 @@ /** | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
var roots = []; | ||
function findRoots(json, skip) { | ||
var roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -459,35 +464,28 @@ var values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, __spread(findRoots(value))); | ||
var result = findRoots(value, true); | ||
try { | ||
for (var _a = __values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = __values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -494,0 +492,0 @@ /** |
@@ -303,4 +303,4 @@ import { module as module$1 } from 'angular'; | ||
} | ||
matches(uri) { | ||
return this.$uri === uri; | ||
matches(json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
} | ||
@@ -334,3 +334,3 @@ } | ||
// Find the ref in the roots. | ||
return roots.find(item => json.matches(item.uri)); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -346,3 +346,3 @@ else if (Object.keys(json).length > 0) { | ||
if (value instanceof SelfReference) { | ||
const ref = roots.find(item => value.matches(item.uri)); | ||
const ref = roots.get(value.$uri); | ||
Object.assign(json, { | ||
@@ -357,2 +357,7 @@ [key]: ref | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, { | ||
[key]: roots.get('#') | ||
}); | ||
} | ||
} | ||
@@ -366,28 +371,28 @@ return json; | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
const roots = []; | ||
function findRoots(json, skip) { | ||
const roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
const values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
for (const value of values) { | ||
roots.push(...findRoots(value)); | ||
const result = findRoots(value, true); | ||
for (const [uri, root] of result.entries()) { | ||
roots.set(uri, root); | ||
} | ||
} | ||
} | ||
// Remove duplicate entries. | ||
const result = []; | ||
for (const root of roots) { | ||
if (result.findIndex(item => root.uri === item.uri) === -1) { | ||
result.push(root); | ||
} | ||
} | ||
return result; | ||
return roots; | ||
} | ||
@@ -394,0 +399,0 @@ /** |
@@ -362,4 +362,4 @@ (function (global, factory) { | ||
} | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -394,3 +394,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -404,20 +404,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = tslib_1.__values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -428,3 +429,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -436,3 +437,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -442,14 +443,18 @@ /** | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
var roots = []; | ||
function findRoots(json, skip) { | ||
var roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -460,35 +465,28 @@ var values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, tslib_1.__spread(findRoots(value))); | ||
var result = findRoots(value, true); | ||
try { | ||
for (var _a = tslib_1.__values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = tslib_1.__values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -495,0 +493,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("tslib"),require("angular")):"function"==typeof define&&define.amd?define(["exports","tslib","angular"],e):e(t.potionClient=t.potionClient||{},t.tslib,t.angular)}(this,function(t,e,r){"use strict";function n(t,e){return void 0===e&&(e="_"),t.replace(/\.?([A-Z0-9]+)/g,function(t,r){return""+e+r.toLowerCase()}).replace(/^_/,"")}function i(t){return t.replace(/_([a-z0-9])/g,function(t){return t[1].toUpperCase()})}function o(t){return"object"==typeof t&&null!==t}function u(t){return"function"==typeof t}function a(t,r,n){return o(t)&&!Array.isArray(t)?Object.entries(t).map(function(t){var i=e.__read(t,2),o=i[0],a=i[1];return[u(r)?r(o):o,u(n)?n(a):a]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function s(t,e){var r="An error occurred while Potion tried to retrieve a resource";return t instanceof Error?t.message:"string"==typeof t?t:"string"==typeof e?r+" from '"+e+"'.":r+"."}function c(t){return Array.isArray(t)?t.map(function(t){return"object"==typeof t?c(t):t}):o(t)?Object.entries(t).map(function(t){var r=e.__read(t,2),n=r[0],o=r[1];return[i(n),"object"==typeof o?c(o):o]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function f(t,r){if("object"!=typeof t||null===t)return t;if(M.has(t))return t;if(t instanceof U){var n=t.toArray().map(function(t){return f(t,r)});return t.update(n,t.total)}if(Array.isArray(t))return t.map(function(t){return f(t,r)});if(t instanceof q)return r.find(function(e){return t.matches(e.uri)});if(Object.keys(t).length>0){!Array.isArray(t)&&t.uri&&M.add(t);try{for(var i=e.__values(Object.entries(t)),u=i.next();!u.done;u=i.next()){var a=e.__read(u.value,2);!function(e,n){if(n instanceof q){var i=r.find(function(t){return n.matches(t.uri)});Object.assign(t,(u={},u[e]=i,u))}else o(n)&&Object.assign(t,(a={},a[e]=f(n,r),a));var u,a}(a[0],a[1])}}catch(t){s={error:t}}finally{try{u&&!u.done&&(c=i.return)&&c.call(i)}finally{if(s)throw s.error}}return t}return t;var s,c}function p(t){var r=[];if(o(t)&&Object.keys(t).length>0){if(M.has(t))return[];t.uri&&r.push(t);var n=Array.isArray(t)||t instanceof U?t:Object.values(t);try{for(var i=e.__values(n),u=i.next();!u.done;u=i.next()){var a=u.value;r.push.apply(r,e.__spread(p(a)))}}catch(t){h={error:t}}finally{try{u&&!u.done&&(d=i.return)&&d.call(i)}finally{if(h)throw h.error}}}var s=[];try{for(var c=e.__values(r),f=c.next();!f.done;f=c.next())!function(t){-1===s.findIndex(function(e){return t.uri===e.uri})&&s.push(t)}(f.value)}catch(t){l={error:t}}finally{try{f&&!f.done&&(g=c.return)&&g.call(c)}finally{if(l)throw l.error}}return s;var h,d,l,g}function h(t){return new q(t)}function d(t,e){return o(t)?t instanceof R&&"string"==typeof t.uri?{$ref:""+O(t.uri,e)}:t instanceof Date?{$date:t.getTime()}:Array.isArray(t)?t.map(function(t){return d(t,e)}):a(t,function(t){return n(t)},function(t){return d(t,e)}):t}function l(t){return"string"==typeof t&&t.length>0?/^\d+$/.test(t)?parseInt(t,10):t:Number.isInteger(t)?t:null}function g(t,e){var r=t.indexOf(e+"/");return-1!==r?l(t.substring(r).split("/").pop()):null}function y(t,r){var n=Object.entries(r).find(function(r){var n=e.__read(r,1)[0];return 0===t.indexOf(n+"/")});if(n){var i=e.__read(n,2);return{resourceURI:i[0],resource:i[1]}}}function v(t,e){var r=y(t,e);return!!r&&null!==g(t,r.resourceURI)}function m(t){var e=t.$type,r=t.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof e}function b(t){var e=t.$uri,r=t.$ref,n=t.$type,i=t.$id;return"string"==typeof e?decodeURIComponent(e):"string"==typeof r?decodeURIComponent(r):m({$type:n,$id:i})?"/"+n+"/"+i:""}function P(t,e){return t.includes(e)?t.substring(e.length):t}function O(t,e){return"string"!=typeof e||t.includes(e)?t:""+e+t}function _(t){return C.getOwnMetadata(J,t)}function $(t,e){C.defineMetadata(J,e,t)}function j(t){return C.getOwnMetadata(D,t)}function w(t,e){C.defineMetadata(D,e,t)}function T(t){return C.getOwnMetadata(G,t.constructor)||Promise}function A(t,e){C.defineMetadata(G,e,t)}function S(t,e){var r=C.getOwnMetadata(k,t);return r&&r[e]}function E(t,e){var r=u(t)?t:u(t.constructor)?t.constructor:null;if(null!==r){C.defineMetadata(k,Object.assign(C.getOwnMetadata(k,r)||{},(n={},n[e]=!0,n)),r);var n}}function x(t,e){var r=(void 0===e?{}:e).method;return function(e,n){var i=void 0===n?{}:n,o=i.paginate,a=void 0!==o&&o,s=i.cache,c=void 0===s||s,f=u(this),p=""+(f?j(this):this.uri)+t,h={method:r,paginate:a,cache:c};return"GET"===r?h.search=e:["POST","PUT","PATCH"].includes(r)&&(h.data=e),_(f?this:this.constructor).fetch(p,h)}}function I(){var t={};return this.config=function(e){return o(e)?(Object.assign(t,e),t):t},this.$get=["$cacheFactory","$q","$http",function(r,n,i){var o=r.get("potion")||r("potion"),u=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e.__extends(r,t),r.prototype.request=function(t,r){var n=e.__assign({},r),o=n.method,u=void 0===o?"GET":o,a=n.search,s=n.data,c=n.cache;return i({url:t,method:u,data:s,cache:void 0===c||c,params:a}).then(function(t){var e=t.headers,r={data:t.data};return e&&(r.headers=e()),r})},r}(z),a=function(){function t(){}return t.prototype.has=function(t){return void 0!==o.get(t)},t.prototype.get=function(t){return o.get(t)},t.prototype.put=function(t,e){return o.put(t,e),o.get(t)},t.prototype.remove=function(t){o.remove(t)},t}();return A(u,n),new u(e.__assign({cache:new a},t))}],this}var R=function(){function t(t){void 0===t&&(t={}),this.$id=null,Object.assign(this,t)}return t.fetch=function(t,e){var r=(void 0===e?{}:e).cache,n=void 0===r||r,i=j(this);return _(this).fetch(i+"/"+t,{method:"GET",cache:n})},t.query=function(t,e){var r=void 0===e?{}:e,n=r.paginate,i=void 0!==n&&n,o=r.cache,u=void 0===o||o,a=j(this);return _(this).fetch(a,{method:"GET",search:t,paginate:i,cache:u})},t.first=function(t){function e(t){return t[0]}return this.query(t).then(e)},Object.defineProperty(t.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),t.prototype.equals=function(e){return e instanceof t&&(this.id===e.id&&this.constructor.name===e.constructor.name)},t.prototype.toJSON=function(){var t=this,e={};return Object.keys(this).filter(function(e){return!e.startsWith("$")&&!S(t.constructor,e)}).forEach(function(r){e[r]=t[r]}),e},t.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var t=this.constructor;return _(t).fetch(j(t),{method:"POST",data:this.toJSON(),cache:!0})},t.prototype.update=function(t){return void 0===t&&(t={}),_(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:t})},t.prototype.destroy=function(){function t(){n.get(e)&&n.remove(e)}var e=this.uri,r=_(this.constructor),n=r.cache;return r.fetch(e,{method:"DELETE"}).then(t)},t}(),U=function(){function t(t,r,n,i){var o=t.potion,u=t.uri;this.items=r,this.options=i,this.potion=o,this.uri=u;var a=e.__assign({},this.options.search),s=a.page,c=void 0===s?1:s,f=a.perPage,p=void 0===f?25:f;this.$page=c,this.$perPage=p,this.$total=parseInt(n,10)}return Object.defineProperty(t,Symbol.species,{get:function(){return t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"page",{get:function(){return this.$page},set:function(t){this.changePageTo(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),t.prototype[Symbol.iterator]=function(){return this.items.values()},t.prototype.toArray=function(){return this.items.slice(0)},t.prototype.at=function(t){return this.items[t]},t.prototype.changePageTo=function(t){var e=this.options.pagination;return this.options.search.page=t,this.$page=t,this.potion.fetch(this.uri,this.options,{pagination:e})},t.prototype.update=function(t,r){return(n=this.items).splice.apply(n,e.__spread([0,this.items.length],t)),this.$total=r,this;var n},t}(),q=function(){function t(t){this.$uri=t}return t.prototype.matches=function(t){return this.$uri===t},t}(),M=new WeakSet,N=function(){function t(){this.items=new Map}return t.prototype.has=function(t){return this.items.has(t)},t.prototype.get=function(t){return this.items.get(t)},t.prototype.put=function(t,e){return this.items.set(t,e).get(t)},t.prototype.remove=function(t){this.items.delete(t)},t}(),C=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!C||!C.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var J=Symbol("potion"),D=Symbol("potion:uri"),G=Symbol("potion:promise"),k=Symbol("potion:readonly"),z=function(){function t(t){var e=void 0===t?{}:t,r=e.host,n=void 0===r?"":r,i=e.prefix,o=void 0===i?"":i,u=e.cache;this.resources={},this.Promise=T(this),this.requests=new Map,this.cache=u||new N,this.host=n,this.prefix=o}return t.prototype.register=function(t,e,r){if(!u(e))throw new TypeError("An error occurred while trying to register a resource for "+t+". "+e+" is not a function.");return $(e,this),w(e,t),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(t){return E(e,t)}),this.resources[t]=e,e},t.prototype.registerAs=function(t,e){var r=this;return function(n){return r.register(t,n,e),n}},t.prototype.fetch=function(t,r,n){var i=P(t,this.prefix),o=e.__assign({},r,n,{origin:[]});return v(t,this.resources)&&Object.assign(o,{origin:[i]}),this.resolve(t,o).then(function(t){return f(t,p(t)),t})},t.prototype.resolve=function(t,e){var r=this,n=this,i=n.Promise,o=n.prefix,u=P(t,o);t=O(t,o);var a=function(){return r.request(""+r.host+t,r.serialize(e)).then(function(n){return r.deserialize(n,t,e)})};return"GET"!==e.method||e.paginate||e.search?a():e.cache&&this.cache.has(u)?this.cache.get(u):(this.requests.has(u)||this.requests.set(u,a().then(function(t){return r.requests.delete(u),t},function(e){r.requests.delete(u);var n=s(e,t);return i.reject(n)})),this.requests.get(u))},t.prototype.serialize=function(t){var r=this.prefix,n=t.search;return e.__assign({},t,{search:d(t.paginate?e.__assign({page:1,perPage:25},n):n,r),data:d(t.data,r)})},t.prototype.deserialize=function(t,e,r){var n=this,i=t.data,o=t.headers;return this.fromPotionJSON(i,r.origin).then(function(t){if(r.paginate){var i=o["x-total-count"]||t.length;if(r.pagination instanceof U)return r.pagination.update(t,i);var u=new U({uri:e,potion:n},t,i,r);return Object.assign(r,{pagination:u}),u}return t})},t.prototype.fromPotionJSON=function(t,r){var n=this,i=this.Promise;if("object"==typeof t&&null!==t){if(Array.isArray(t))return i.all(t.map(function(t){return n.fromPotionJSON(t,r)}));if("string"==typeof t.$uri||m(t))return this.parseURI(t).then(function(o){var u=o.resource,a=o.id,s=o.uri,c={$id:a,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(t,r);return n.cache.has(s)?i.all([f,n.cache.get(s)]).then(function(t){var r=e.__read(t,2),n=r[0],i=r[1];return Object.assign(i,n,c),i}):n.cache.put(s,f.then(function(t){return Reflect.construct(u,[e.__assign({},t,c)])}))});if("string"==typeof t.$schema)return i.resolve(c(t));if(1===Object.keys(t).length){if("string"==typeof t.$ref)return"#"===t.$ref?i.resolve(t.$ref):this.parseURI(t).then(function(t){var e=t.uri;return r.includes(e)?i.resolve(h(e)):n.resolve(e,{cache:!0,method:"GET",origin:r})});if(void 0!==t.$date)return i.resolve(new Date(t.$date))}return this.parsePotionJSONProperties(t,r)}return i.resolve(t)},t.prototype.parsePotionJSONProperties=function(t,r){var n=this,o=this.Promise,u=Object.entries(t),a=u.map(function(t){var i=e.__read(t,2)[1];return n.fromPotionJSON(i,r)}),s=u.map(function(t){return i(e.__read(t,1)[0])});return o.all(a).then(function(t){return t.map(function(t,e){return[s[e],t]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{})})},t.prototype.parseURI=function(t){var e=t.$ref,r=t.$uri,n=t.$type,i=t.$id,o=this.Promise,u=P(b({$ref:e,$uri:r,$type:n,$id:i}),this.prefix),a=y(u,this.resources);if(a){var s=a.resourceURI,c={resource:a.resource,uri:u},f=l(i);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:g(u,s)}),o.resolve(c)}return o.reject(new Error("URI '"+u+"' is an uninterpretable or unknown Potion resource."))},t}(),H={GET:function(t){return x(t,{method:"GET"})},DELETE:function(t){return x(t,{method:"DELETE"})},POST:function(t){return x(t,{method:"POST"})},PATCH:function(t){return x(t,{method:"PATCH"})},PUT:function(t){return x(t,{method:"PUT"})}},L=r.module("potion",[]).provider("potion",I);t.potion=L,t.Item=R,t.readonly=E,t.Pagination=U,t.PotionBase=z,t.Route=H,t.route=x,t.findPotionResource=y,t.fromSchemaJSON=c,t.getPotionID=g,t.getPotionURI=b,t.hasTypeAndId=m,t.isPotionURI=v,t.parsePotionID=l,t.removePrefixFromURI=P,t.addPrefixToURI=O,t.toPotionJSON=d,t.toCamelCase=i,t.toSnakeCase=n,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("tslib"),require("angular")):"function"==typeof define&&define.amd?define(["exports","tslib","angular"],e):e(t.potionClient=t.potionClient||{},t.tslib,t.angular)}(this,function(t,e,r){"use strict";function n(t,e){return void 0===e&&(e="_"),t.replace(/\.?([A-Z0-9]+)/g,function(t,r){return""+e+r.toLowerCase()}).replace(/^_/,"")}function i(t){return t.replace(/_([a-z0-9])/g,function(t){return t[1].toUpperCase()})}function o(t){return"object"==typeof t&&null!==t}function u(t){return"function"==typeof t}function a(t,r,n){return o(t)&&!Array.isArray(t)?Object.entries(t).map(function(t){var i=e.__read(t,2),o=i[0],a=i[1];return[u(r)?r(o):o,u(n)?n(a):a]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function s(t,e){var r="An error occurred while Potion tried to retrieve a resource";return t instanceof Error?t.message:"string"==typeof t?t:"string"==typeof e?r+" from '"+e+"'.":r+"."}function c(t){return Array.isArray(t)?t.map(function(t){return"object"==typeof t?c(t):t}):o(t)?Object.entries(t).map(function(t){var r=e.__read(t,2),n=r[0],o=r[1];return[i(n),"object"==typeof o?c(o):o]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function f(t,r){if("object"!=typeof t||null===t)return t;if(q.has(t))return t;if(t instanceof U){var n=t.toArray().map(function(t){return f(t,r)});return t.update(n,t.total)}if(Array.isArray(t))return t.map(function(t){return f(t,r)});if(t instanceof M)return r.get(t.$uri);if(Object.keys(t).length>0){!Array.isArray(t)&&t.uri&&q.add(t);try{for(var i=e.__values(Object.entries(t)),u=i.next();!u.done;u=i.next()){var a=e.__read(u.value,2),s=a[0],c=a[1];if(c instanceof M){var p=r.get(c.$uri);Object.assign(t,(l={},l[s]=p,l))}else o(c)?Object.assign(t,(g={},g[s]=f(c,r),g)):"string"==typeof c&&"#"===c&&Object.assign(t,(y={},y[s]=r.get("#"),y))}}catch(t){h={error:t}}finally{try{u&&!u.done&&(d=i.return)&&d.call(i)}finally{if(h)throw h.error}}return t}return t;var h,d,l,g,y}function p(t,r){var n=new Map;if(o(t)&&Object.keys(t).length>0){if(r||n.set("#",t),q.has(t))return new Map;t.uri&&!n.has(t.uri)&&n.set(t.uri,t);var i=Array.isArray(t)||t instanceof U?t:Object.values(t);try{for(var u=e.__values(i),a=u.next();!a.done;a=u.next()){var s=p(a.value,!0);try{for(var c=e.__values(s.entries()),f=c.next();!f.done;f=c.next()){var h=e.__read(f.value,2),d=h[0],l=h[1];n.set(d,l)}}catch(t){v={error:t}}finally{try{f&&!f.done&&(m=c.return)&&m.call(c)}finally{if(v)throw v.error}}}}catch(t){g={error:t}}finally{try{a&&!a.done&&(y=u.return)&&y.call(u)}finally{if(g)throw g.error}}}return n;var g,y,v,m}function h(t){return new M(t)}function d(t,e){return o(t)?t instanceof R&&"string"==typeof t.uri?{$ref:""+P(t.uri,e)}:t instanceof Date?{$date:t.getTime()}:Array.isArray(t)?t.map(function(t){return d(t,e)}):a(t,function(t){return n(t)},function(t){return d(t,e)}):t}function l(t){return"string"==typeof t&&t.length>0?/^\d+$/.test(t)?parseInt(t,10):t:Number.isInteger(t)?t:null}function g(t,e){var r=t.indexOf(e+"/");return-1!==r?l(t.substring(r).split("/").pop()):null}function y(t,r){var n=Object.entries(r).find(function(r){var n=e.__read(r,1)[0];return 0===t.indexOf(n+"/")});if(n){var i=e.__read(n,2);return{resourceURI:i[0],resource:i[1]}}}function v(t,e){var r=y(t,e);return!!r&&null!==g(t,r.resourceURI)}function m(t){var e=t.$type,r=t.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof e}function b(t){var e=t.$uri,r=t.$ref,n=t.$type,i=t.$id;return"string"==typeof e?decodeURIComponent(e):"string"==typeof r?decodeURIComponent(r):m({$type:n,$id:i})?"/"+n+"/"+i:""}function O(t,e){return t.includes(e)?t.substring(e.length):t}function P(t,e){return"string"!=typeof e||t.includes(e)?t:""+e+t}function _(t){return C.getOwnMetadata(J,t)}function $(t,e){C.defineMetadata(J,e,t)}function j(t){return C.getOwnMetadata(D,t)}function w(t,e){C.defineMetadata(D,e,t)}function T(t){return C.getOwnMetadata(G,t.constructor)||Promise}function A(t,e){C.defineMetadata(G,e,t)}function S(t,e){var r=C.getOwnMetadata(k,t);return r&&r[e]}function E(t,e){var r=u(t)?t:u(t.constructor)?t.constructor:null;if(null!==r){C.defineMetadata(k,Object.assign(C.getOwnMetadata(k,r)||{},(n={},n[e]=!0,n)),r);var n}}function x(t,e){var r=(void 0===e?{}:e).method;return function(e,n){var i=void 0===n?{}:n,o=i.paginate,a=void 0!==o&&o,s=i.cache,c=void 0===s||s,f=u(this),p=""+(f?j(this):this.uri)+t,h={method:r,paginate:a,cache:c};return"GET"===r?h.search=e:["POST","PUT","PATCH"].includes(r)&&(h.data=e),_(f?this:this.constructor).fetch(p,h)}}function I(){var t={};return this.config=function(e){return o(e)?(Object.assign(t,e),t):t},this.$get=["$cacheFactory","$q","$http",function(r,n,i){var o=r.get("potion")||r("potion"),u=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e.__extends(r,t),r.prototype.request=function(t,r){var n=e.__assign({},r),o=n.method,u=void 0===o?"GET":o,a=n.search,s=n.data,c=n.cache;return i({url:t,method:u,data:s,cache:void 0===c||c,params:a}).then(function(t){var e=t.headers,r={data:t.data};return e&&(r.headers=e()),r})},r}(z),a=function(){function t(){}return t.prototype.has=function(t){return void 0!==o.get(t)},t.prototype.get=function(t){return o.get(t)},t.prototype.put=function(t,e){return o.put(t,e),o.get(t)},t.prototype.remove=function(t){o.remove(t)},t}();return A(u,n),new u(e.__assign({cache:new a},t))}],this}var R=function(){function t(t){void 0===t&&(t={}),this.$id=null,Object.assign(this,t)}return t.fetch=function(t,e){var r=(void 0===e?{}:e).cache,n=void 0===r||r,i=j(this);return _(this).fetch(i+"/"+t,{method:"GET",cache:n})},t.query=function(t,e){var r=void 0===e?{}:e,n=r.paginate,i=void 0!==n&&n,o=r.cache,u=void 0===o||o,a=j(this);return _(this).fetch(a,{method:"GET",search:t,paginate:i,cache:u})},t.first=function(t){function e(t){return t[0]}return this.query(t).then(e)},Object.defineProperty(t.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),t.prototype.equals=function(e){return e instanceof t&&(this.id===e.id&&this.constructor.name===e.constructor.name)},t.prototype.toJSON=function(){var t=this,e={};return Object.keys(this).filter(function(e){return!e.startsWith("$")&&!S(t.constructor,e)}).forEach(function(r){e[r]=t[r]}),e},t.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var t=this.constructor;return _(t).fetch(j(t),{method:"POST",data:this.toJSON(),cache:!0})},t.prototype.update=function(t){return void 0===t&&(t={}),_(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:t})},t.prototype.destroy=function(){function t(){n.get(e)&&n.remove(e)}var e=this.uri,r=_(this.constructor),n=r.cache;return r.fetch(e,{method:"DELETE"}).then(t)},t}(),U=function(){function t(t,r,n,i){var o=t.potion,u=t.uri;this.items=r,this.options=i,this.potion=o,this.uri=u;var a=e.__assign({},this.options.search),s=a.page,c=void 0===s?1:s,f=a.perPage,p=void 0===f?25:f;this.$page=c,this.$perPage=p,this.$total=parseInt(n,10)}return Object.defineProperty(t,Symbol.species,{get:function(){return t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"page",{get:function(){return this.$page},set:function(t){this.changePageTo(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),t.prototype[Symbol.iterator]=function(){return this.items.values()},t.prototype.toArray=function(){return this.items.slice(0)},t.prototype.at=function(t){return this.items[t]},t.prototype.changePageTo=function(t){var e=this.options.pagination;return this.options.search.page=t,this.$page=t,this.potion.fetch(this.uri,this.options,{pagination:e})},t.prototype.update=function(t,r){return(n=this.items).splice.apply(n,e.__spread([0,this.items.length],t)),this.$total=r,this;var n},t}(),M=function(){function t(t){this.$uri=t}return t.prototype.matches=function(t){return o(t)&&this.$uri===t.uri},t}(),q=new WeakSet,N=function(){function t(){this.items=new Map}return t.prototype.has=function(t){return this.items.has(t)},t.prototype.get=function(t){return this.items.get(t)},t.prototype.put=function(t,e){return this.items.set(t,e).get(t)},t.prototype.remove=function(t){this.items.delete(t)},t}(),C=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!C||!C.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var J=Symbol("potion"),D=Symbol("potion:uri"),G=Symbol("potion:promise"),k=Symbol("potion:readonly"),z=function(){function t(t){var e=void 0===t?{}:t,r=e.host,n=void 0===r?"":r,i=e.prefix,o=void 0===i?"":i,u=e.cache;this.resources={},this.Promise=T(this),this.requests=new Map,this.cache=u||new N,this.host=n,this.prefix=o}return t.prototype.register=function(t,e,r){if(!u(e))throw new TypeError("An error occurred while trying to register a resource for "+t+". "+e+" is not a function.");return $(e,this),w(e,t),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(t){return E(e,t)}),this.resources[t]=e,e},t.prototype.registerAs=function(t,e){var r=this;return function(n){return r.register(t,n,e),n}},t.prototype.fetch=function(t,r,n){var i=O(t,this.prefix),o=e.__assign({},r,n,{origin:[]});return v(t,this.resources)&&Object.assign(o,{origin:[i]}),this.resolve(t,o).then(function(t){return f(t,p(t)),t})},t.prototype.resolve=function(t,e){var r=this,n=this,i=n.Promise,o=n.prefix,u=O(t,o);t=P(t,o);var a=function(){return r.request(""+r.host+t,r.serialize(e)).then(function(n){return r.deserialize(n,t,e)})};return"GET"!==e.method||e.paginate||e.search?a():e.cache&&this.cache.has(u)?this.cache.get(u):(this.requests.has(u)||this.requests.set(u,a().then(function(t){return r.requests.delete(u),t},function(e){r.requests.delete(u);var n=s(e,t);return i.reject(n)})),this.requests.get(u))},t.prototype.serialize=function(t){var r=this.prefix,n=t.search;return e.__assign({},t,{search:d(t.paginate?e.__assign({page:1,perPage:25},n):n,r),data:d(t.data,r)})},t.prototype.deserialize=function(t,e,r){var n=this,i=t.data,o=t.headers;return this.fromPotionJSON(i,r.origin).then(function(t){if(r.paginate){var i=o["x-total-count"]||t.length;if(r.pagination instanceof U)return r.pagination.update(t,i);var u=new U({uri:e,potion:n},t,i,r);return Object.assign(r,{pagination:u}),u}return t})},t.prototype.fromPotionJSON=function(t,r){var n=this,i=this.Promise;if("object"==typeof t&&null!==t){if(Array.isArray(t))return i.all(t.map(function(t){return n.fromPotionJSON(t,r)}));if("string"==typeof t.$uri||m(t))return this.parseURI(t).then(function(o){var u=o.resource,a=o.id,s=o.uri,c={$id:a,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(t,r);return n.cache.has(s)?i.all([f,n.cache.get(s)]).then(function(t){var r=e.__read(t,2),n=r[0],i=r[1];return Object.assign(i,n,c),i}):n.cache.put(s,f.then(function(t){return Reflect.construct(u,[e.__assign({},t,c)])}))});if("string"==typeof t.$schema)return i.resolve(c(t));if(1===Object.keys(t).length){if("string"==typeof t.$ref)return"#"===t.$ref?i.resolve(t.$ref):this.parseURI(t).then(function(t){var e=t.uri;return r.includes(e)?i.resolve(h(e)):n.resolve(e,{cache:!0,method:"GET",origin:r})});if(void 0!==t.$date)return i.resolve(new Date(t.$date))}return this.parsePotionJSONProperties(t,r)}return i.resolve(t)},t.prototype.parsePotionJSONProperties=function(t,r){var n=this,o=this.Promise,u=Object.entries(t),a=u.map(function(t){var i=e.__read(t,2)[1];return n.fromPotionJSON(i,r)}),s=u.map(function(t){return i(e.__read(t,1)[0])});return o.all(a).then(function(t){return t.map(function(t,e){return[s[e],t]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{})})},t.prototype.parseURI=function(t){var e=t.$ref,r=t.$uri,n=t.$type,i=t.$id,o=this.Promise,u=O(b({$ref:e,$uri:r,$type:n,$id:i}),this.prefix),a=y(u,this.resources);if(a){var s=a.resourceURI,c={resource:a.resource,uri:u},f=l(i);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:g(u,s)}),o.resolve(c)}return o.reject(new Error("URI '"+u+"' is an uninterpretable or unknown Potion resource."))},t}(),H={GET:function(t){return x(t,{method:"GET"})},DELETE:function(t){return x(t,{method:"DELETE"})},POST:function(t){return x(t,{method:"POST"})},PATCH:function(t){return x(t,{method:"PATCH"})},PUT:function(t){return x(t,{method:"PUT"})}},L=r.module("potion",[]).provider("potion",I);t.potion=L,t.Item=R,t.readonly=E,t.Pagination=U,t.PotionBase=z,t.Route=H,t.route=x,t.findPotionResource=y,t.fromSchemaJSON=c,t.getPotionID=g,t.getPotionURI=b,t.hasTypeAndId=m,t.isPotionURI=v,t.parsePotionID=l,t.removePrefixFromURI=O,t.addPrefixToURI=P,t.toPotionJSON=d,t.toCamelCase=i,t.toSnakeCase=n,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=angularjs.umd.min.js.map |
@@ -0,1 +1,11 @@ | ||
<a name="2.2.0"></a> | ||
# [2.2.0](https://github.com/biosustain/potion-node/compare/v2.1.1...v2.2.0) (2017-06-05) | ||
### Features | ||
* implement support for resolving "#" self references and close [#11](https://github.com/biosustain/potion-node/issues/11) ([311a4b0](https://github.com/biosustain/potion-node/commit/311a4b0)) | ||
<a name="2.1.1"></a> | ||
@@ -2,0 +12,0 @@ ## [2.1.1](https://github.com/biosustain/potion-node/compare/v2.1.0...v2.1.1) (2017-06-05) |
@@ -54,11 +54,11 @@ import { Item } from './item'; | ||
constructor($uri: string); | ||
matches(uri: any): boolean; | ||
matches(json: any): boolean; | ||
} | ||
export declare function replaceSelfReferences(json: any, roots: Item[]): any; | ||
export declare function replaceSelfReferences(json: any, roots: Map<string, any>): any; | ||
/** | ||
* Recursively find every object with {uri} (a Potion item usually) and return a list with all. | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
export declare function findRoots(json: any): Item[]; | ||
export declare function findRoots(json: any, skip?: boolean): Map<string, any>; | ||
/** | ||
@@ -65,0 +65,0 @@ * Generate a self reference |
102
fetch.es5.js
@@ -359,4 +359,4 @@ import { __assign, __extends, __read, __spread, __values } from 'tslib'; | ||
} | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -391,3 +391,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -401,20 +401,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = __values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -425,3 +426,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -433,3 +434,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -439,14 +440,18 @@ /** | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
var roots = []; | ||
function findRoots(json, skip) { | ||
var roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -457,35 +462,28 @@ var values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, __spread(findRoots(value))); | ||
var result = findRoots(value, true); | ||
try { | ||
for (var _a = __values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = __values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -492,0 +490,0 @@ /** |
43
fetch.js
@@ -300,4 +300,4 @@ /** | ||
} | ||
matches(uri) { | ||
return this.$uri === uri; | ||
matches(json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
} | ||
@@ -331,3 +331,3 @@ } | ||
// Find the ref in the roots. | ||
return roots.find(item => json.matches(item.uri)); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -343,3 +343,3 @@ else if (Object.keys(json).length > 0) { | ||
if (value instanceof SelfReference) { | ||
const ref = roots.find(item => value.matches(item.uri)); | ||
const ref = roots.get(value.$uri); | ||
Object.assign(json, { | ||
@@ -354,2 +354,7 @@ [key]: ref | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, { | ||
[key]: roots.get('#') | ||
}); | ||
} | ||
} | ||
@@ -363,28 +368,28 @@ return json; | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
const roots = []; | ||
function findRoots(json, skip) { | ||
const roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
const values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
for (const value of values) { | ||
roots.push(...findRoots(value)); | ||
const result = findRoots(value, true); | ||
for (const [uri, root] of result.entries()) { | ||
roots.set(uri, root); | ||
} | ||
} | ||
} | ||
// Remove duplicate entries. | ||
const result = []; | ||
for (const root of roots) { | ||
if (result.findIndex(item => root.uri === item.uri) === -1) { | ||
result.push(root); | ||
} | ||
} | ||
return result; | ||
return roots; | ||
} | ||
@@ -391,0 +396,0 @@ /** |
102
fetch.umd.js
@@ -362,4 +362,4 @@ (function (global, factory) { | ||
} | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -394,3 +394,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -404,20 +404,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = tslib_1.__values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -428,3 +429,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -436,3 +437,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -442,14 +443,18 @@ /** | ||
* @param json - A Potion JSON. | ||
* @return {Array<Item>} | ||
* @return {Array<any>} | ||
*/ | ||
function findRoots(json) { | ||
var roots = []; | ||
function findRoots(json, skip) { | ||
var roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -460,35 +465,28 @@ var values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, tslib_1.__spread(findRoots(value))); | ||
var result = findRoots(value, true); | ||
try { | ||
for (var _a = tslib_1.__values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = tslib_1.__values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -495,0 +493,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("tslib")):"function"==typeof define&&define.amd?define(["exports","tslib"],e):e(t.potionClient=t.potionClient||{},t.tslib)}(this,function(t,e){"use strict";function r(t,e){return void 0===e&&(e="_"),t.replace(/\.?([A-Z0-9]+)/g,function(t,r){return""+e+r.toLowerCase()}).replace(/^_/,"")}function n(t){return t.replace(/_([a-z0-9])/g,function(t){return t[1].toUpperCase()})}function i(t){return"object"==typeof t&&null!==t}function o(t){return"function"==typeof t}function u(t,r,n){return i(t)&&!Array.isArray(t)?Object.entries(t).map(function(t){var i=e.__read(t,2),u=i[0],a=i[1];return[o(r)?r(u):u,o(n)?n(a):a]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function a(t,e){var r="An error occurred while Potion tried to retrieve a resource";return t instanceof Error?t.message:"string"==typeof t?t:"string"==typeof e?r+" from '"+e+"'.":r+"."}function s(t){return Array.isArray(t)?t.map(function(t){return"object"==typeof t?s(t):t}):i(t)?Object.entries(t).map(function(t){var r=e.__read(t,2),i=r[0],o=r[1];return[n(i),"object"==typeof o?s(o):o]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{}):t}function c(t,r){if("object"!=typeof t||null===t)return t;if(R.has(t))return t;if(t instanceof E){var n=t.toArray().map(function(t){return c(t,r)});return t.update(n,t.total)}if(Array.isArray(t))return t.map(function(t){return c(t,r)});if(t instanceof I)return r.find(function(e){return t.matches(e.uri)});if(Object.keys(t).length>0){!Array.isArray(t)&&t.uri&&R.add(t);try{for(var o=e.__values(Object.entries(t)),u=o.next();!u.done;u=o.next()){var a=e.__read(u.value,2);!function(e,n){if(n instanceof I){var o=r.find(function(t){return n.matches(t.uri)});Object.assign(t,(u={},u[e]=o,u))}else i(n)&&Object.assign(t,(a={},a[e]=c(n,r),a));var u,a}(a[0],a[1])}}catch(t){s={error:t}}finally{try{u&&!u.done&&(f=o.return)&&f.call(o)}finally{if(s)throw s.error}}return t}return t;var s,f}function f(t){var r=[];if(i(t)&&Object.keys(t).length>0){if(R.has(t))return[];t.uri&&r.push(t);var n=Array.isArray(t)||t instanceof E?t:Object.values(t);try{for(var o=e.__values(n),u=o.next();!u.done;u=o.next()){var a=u.value;r.push.apply(r,e.__spread(f(a)))}}catch(t){p={error:t}}finally{try{u&&!u.done&&(d=o.return)&&d.call(o)}finally{if(p)throw p.error}}}var s=[];try{for(var c=e.__values(r),h=c.next();!h.done;h=c.next())!function(t){-1===s.findIndex(function(e){return t.uri===e.uri})&&s.push(t)}(h.value)}catch(t){l={error:t}}finally{try{h&&!h.done&&(y=c.return)&&y.call(c)}finally{if(l)throw l.error}}return s;var p,d,l,y}function h(t){return new I(t)}function p(t,e){return i(t)?t instanceof x&&"string"==typeof t.uri?{$ref:""+O(t.uri,e)}:t instanceof Date?{$date:t.getTime()}:Array.isArray(t)?t.map(function(t){return p(t,e)}):u(t,function(t){return r(t)},function(t){return p(t,e)}):t}function d(t){return"string"==typeof t&&t.length>0?/^\d+$/.test(t)?parseInt(t,10):t:Number.isInteger(t)?t:null}function l(t,e){var r=t.indexOf(e+"/");return-1!==r?d(t.substring(r).split("/").pop()):null}function y(t,r){var n=Object.entries(r).find(function(r){var n=e.__read(r,1)[0];return 0===t.indexOf(n+"/")});if(n){var i=e.__read(n,2);return{resourceURI:i[0],resource:i[1]}}}function g(t,e){var r=y(t,e);return!!r&&null!==l(t,r.resourceURI)}function v(t){var e=t.$type,r=t.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof e}function m(t){var e=t.$uri,r=t.$ref,n=t.$type,i=t.$id;return"string"==typeof e?decodeURIComponent(e):"string"==typeof r?decodeURIComponent(r):v({$type:n,$id:i})?"/"+n+"/"+i:""}function b(t,e){return t.includes(e)?t.substring(e.length):t}function O(t,e){return"string"!=typeof e||t.includes(e)?t:""+e+t}function P(t){return N.getOwnMetadata(q,t)}function _(t,e){N.defineMetadata(q,e,t)}function j(t){return N.getOwnMetadata(C,t)}function $(t,e){N.defineMetadata(C,e,t)}function w(t){return N.getOwnMetadata(J,t.constructor)||Promise}function T(t,e){var r=N.getOwnMetadata(M,t);return r&&r[e]}function A(t,e){var r=o(t)?t:o(t.constructor)?t.constructor:null;if(null!==r){N.defineMetadata(M,Object.assign(N.getOwnMetadata(M,r)||{},(n={},n[e]=!0,n)),r);var n}}function S(t,e){var r=(void 0===e?{}:e).method;return function(e,n){var i=void 0===n?{}:n,u=i.paginate,a=void 0!==u&&u,s=i.cache,c=void 0===s||s,f=o(this),h=""+(f?j(this):this.uri)+t,p={method:r,paginate:a,cache:c};return"GET"===r?p.search=e:["POST","PUT","PATCH"].includes(r)&&(p.data=e),P(f?this:this.constructor).fetch(h,p)}}var x=function(){function t(t){void 0===t&&(t={}),this.$id=null,Object.assign(this,t)}return t.fetch=function(t,e){var r=(void 0===e?{}:e).cache,n=void 0===r||r,i=j(this);return P(this).fetch(i+"/"+t,{method:"GET",cache:n})},t.query=function(t,e){var r=void 0===e?{}:e,n=r.paginate,i=void 0!==n&&n,o=r.cache,u=void 0===o||o,a=j(this);return P(this).fetch(a,{method:"GET",search:t,paginate:i,cache:u})},t.first=function(t){function e(t){return t[0]}return this.query(t).then(e)},Object.defineProperty(t.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),t.prototype.equals=function(e){return e instanceof t&&(this.id===e.id&&this.constructor.name===e.constructor.name)},t.prototype.toJSON=function(){var t=this,e={};return Object.keys(this).filter(function(e){return!e.startsWith("$")&&!T(t.constructor,e)}).forEach(function(r){e[r]=t[r]}),e},t.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var t=this.constructor;return P(t).fetch(j(t),{method:"POST",data:this.toJSON(),cache:!0})},t.prototype.update=function(t){return void 0===t&&(t={}),P(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:t})},t.prototype.destroy=function(){function t(){n.get(e)&&n.remove(e)}var e=this.uri,r=P(this.constructor),n=r.cache;return r.fetch(e,{method:"DELETE"}).then(t)},t}(),E=function(){function t(t,r,n,i){var o=t.potion,u=t.uri;this.items=r,this.options=i,this.potion=o,this.uri=u;var a=e.__assign({},this.options.search),s=a.page,c=void 0===s?1:s,f=a.perPage,h=void 0===f?25:f;this.$page=c,this.$perPage=h,this.$total=parseInt(n,10)}return Object.defineProperty(t,Symbol.species,{get:function(){return t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"page",{get:function(){return this.$page},set:function(t){this.changePageTo(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),t.prototype[Symbol.iterator]=function(){return this.items.values()},t.prototype.toArray=function(){return this.items.slice(0)},t.prototype.at=function(t){return this.items[t]},t.prototype.changePageTo=function(t){var e=this.options.pagination;return this.options.search.page=t,this.$page=t,this.potion.fetch(this.uri,this.options,{pagination:e})},t.prototype.update=function(t,r){return(n=this.items).splice.apply(n,e.__spread([0,this.items.length],t)),this.$total=r,this;var n},t}(),I=function(){function t(t){this.$uri=t}return t.prototype.matches=function(t){return this.$uri===t},t}(),R=new WeakSet,U=function(){function t(){this.items=new Map}return t.prototype.has=function(t){return this.items.has(t)},t.prototype.get=function(t){return this.items.get(t)},t.prototype.put=function(t,e){return this.items.set(t,e).get(t)},t.prototype.remove=function(t){this.items.delete(t)},t}(),N=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!N||!N.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var q=Symbol("potion"),C=Symbol("potion:uri"),J=Symbol("potion:promise"),M=Symbol("potion:readonly"),k=function(){function t(t){var e=void 0===t?{}:t,r=e.host,n=void 0===r?"":r,i=e.prefix,o=void 0===i?"":i,u=e.cache;this.resources={},this.Promise=w(this),this.requests=new Map,this.cache=u||new U,this.host=n,this.prefix=o}return t.prototype.register=function(t,e,r){if(!o(e))throw new TypeError("An error occurred while trying to register a resource for "+t+". "+e+" is not a function.");return _(e,this),$(e,t),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(t){return A(e,t)}),this.resources[t]=e,e},t.prototype.registerAs=function(t,e){var r=this;return function(n){return r.register(t,n,e),n}},t.prototype.fetch=function(t,r,n){var i=b(t,this.prefix),o=e.__assign({},r,n,{origin:[]});return g(t,this.resources)&&Object.assign(o,{origin:[i]}),this.resolve(t,o).then(function(t){return c(t,f(t)),t})},t.prototype.resolve=function(t,e){var r=this,n=this,i=n.Promise,o=n.prefix,u=b(t,o);t=O(t,o);var s=function(){return r.request(""+r.host+t,r.serialize(e)).then(function(n){return r.deserialize(n,t,e)})};return"GET"!==e.method||e.paginate||e.search?s():e.cache&&this.cache.has(u)?this.cache.get(u):(this.requests.has(u)||this.requests.set(u,s().then(function(t){return r.requests.delete(u),t},function(e){r.requests.delete(u);var n=a(e,t);return i.reject(n)})),this.requests.get(u))},t.prototype.serialize=function(t){var r=this.prefix,n=t.search;return e.__assign({},t,{search:p(t.paginate?e.__assign({page:1,perPage:25},n):n,r),data:p(t.data,r)})},t.prototype.deserialize=function(t,e,r){var n=this,i=t.data,o=t.headers;return this.fromPotionJSON(i,r.origin).then(function(t){if(r.paginate){var i=o["x-total-count"]||t.length;if(r.pagination instanceof E)return r.pagination.update(t,i);var u=new E({uri:e,potion:n},t,i,r);return Object.assign(r,{pagination:u}),u}return t})},t.prototype.fromPotionJSON=function(t,r){var n=this,i=this.Promise;if("object"==typeof t&&null!==t){if(Array.isArray(t))return i.all(t.map(function(t){return n.fromPotionJSON(t,r)}));if("string"==typeof t.$uri||v(t))return this.parseURI(t).then(function(o){var u=o.resource,a=o.id,s=o.uri,c={$id:a,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(t,r);return n.cache.has(s)?i.all([f,n.cache.get(s)]).then(function(t){var r=e.__read(t,2),n=r[0],i=r[1];return Object.assign(i,n,c),i}):n.cache.put(s,f.then(function(t){return Reflect.construct(u,[e.__assign({},t,c)])}))});if("string"==typeof t.$schema)return i.resolve(s(t));if(1===Object.keys(t).length){if("string"==typeof t.$ref)return"#"===t.$ref?i.resolve(t.$ref):this.parseURI(t).then(function(t){var e=t.uri;return r.includes(e)?i.resolve(h(e)):n.resolve(e,{cache:!0,method:"GET",origin:r})});if(void 0!==t.$date)return i.resolve(new Date(t.$date))}return this.parsePotionJSONProperties(t,r)}return i.resolve(t)},t.prototype.parsePotionJSONProperties=function(t,r){var i=this,o=this.Promise,u=Object.entries(t),a=u.map(function(t){var n=e.__read(t,2)[1];return i.fromPotionJSON(n,r)}),s=u.map(function(t){return n(e.__read(t,1)[0])});return o.all(a).then(function(t){return t.map(function(t,e){return[s[e],t]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(u={},u[i]=o,u));var u},{})})},t.prototype.parseURI=function(t){var e=t.$ref,r=t.$uri,n=t.$type,i=t.$id,o=this.Promise,u=b(m({$ref:e,$uri:r,$type:n,$id:i}),this.prefix),a=y(u,this.resources);if(a){var s=a.resourceURI,c={resource:a.resource,uri:u},f=d(i);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:l(u,s)}),o.resolve(c)}return o.reject(new Error("URI '"+u+"' is an uninterpretable or unknown Potion resource."))},t}(),D={GET:function(t){return S(t,{method:"GET"})},DELETE:function(t){return S(t,{method:"DELETE"})},POST:function(t){return S(t,{method:"POST"})},PATCH:function(t){return S(t,{method:"PATCH"})},PUT:function(t){return S(t,{method:"PUT"})}},G=function(t){function r(r){return t.call(this,e.__assign({},r))||this}return e.__extends(r,t),r.prototype.request=function(t,r){var n=e.__assign({},r),i=n.method,o=void 0===i?"GET":i,u=n.search,a=n.data,s=n.cache,c=void 0===s||s,f=new Headers,h={method:o,cache:c?"default":"no-cache",credentials:"include"};if(a&&(f.set("Accept","application/json"),f.set("Content-Type","application/json"),h.body=JSON.stringify(a)),Object.assign(h,{headers:f}),u){var p=1,d=Object.entries(u),l=d.length;try{for(var y=e.__values(d),g=y.next();!g.done;g=y.next()){var v=e.__read(g.value,2),m=v[0],b=v[1];1===p&&(t+="?"),t+=m+"="+b,p<l&&(t+="&"),p++}}catch(t){O={error:t}}finally{try{g&&!g.done&&(P=y.return)&&P.call(y)}finally{if(O)throw O.error}}}return fetch(new Request(t,h),h).then(function(t){if(t.ok){var e={};return t.headers&&t.headers.forEach(function(t,r){e[r]=t}),t.json().then(function(t){return{headers:e,data:t}},function(t){return t})}var r=new Error(t.statusText);throw Object.assign(r,{response:t}),r});var O,P},r}(k);t.Potion=G,t.Item=x,t.readonly=A,t.Pagination=E,t.PotionBase=k,t.Route=D,t.route=S,t.findPotionResource=y,t.fromSchemaJSON=s,t.getPotionID=l,t.getPotionURI=m,t.hasTypeAndId=v,t.isPotionURI=g,t.parsePotionID=d,t.removePrefixFromURI=b,t.addPrefixToURI=O,t.toPotionJSON=p,t.toCamelCase=n,t.toSnakeCase=r,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("tslib")):"function"==typeof define&&define.amd?define(["exports","tslib"],e):e(t.potionClient=t.potionClient||{},t.tslib)}(this,function(t,e){"use strict";function r(t,e){return void 0===e&&(e="_"),t.replace(/\.?([A-Z0-9]+)/g,function(t,r){return""+e+r.toLowerCase()}).replace(/^_/,"")}function n(t){return t.replace(/_([a-z0-9])/g,function(t){return t[1].toUpperCase()})}function i(t){return"object"==typeof t&&null!==t}function o(t){return"function"==typeof t}function a(t,r,n){return i(t)&&!Array.isArray(t)?Object.entries(t).map(function(t){var i=e.__read(t,2),a=i[0],u=i[1];return[o(r)?r(a):a,o(n)?n(u):u]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(a={},a[i]=o,a));var a},{}):t}function u(t,e){var r="An error occurred while Potion tried to retrieve a resource";return t instanceof Error?t.message:"string"==typeof t?t:"string"==typeof e?r+" from '"+e+"'.":r+"."}function s(t){return Array.isArray(t)?t.map(function(t){return"object"==typeof t?s(t):t}):i(t)?Object.entries(t).map(function(t){var r=e.__read(t,2),i=r[0],o=r[1];return[n(i),"object"==typeof o?s(o):o]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(a={},a[i]=o,a));var a},{}):t}function c(t,r){if("object"!=typeof t||null===t)return t;if(R.has(t))return t;if(t instanceof x){var n=t.toArray().map(function(t){return c(t,r)});return t.update(n,t.total)}if(Array.isArray(t))return t.map(function(t){return c(t,r)});if(t instanceof I)return r.get(t.$uri);if(Object.keys(t).length>0){!Array.isArray(t)&&t.uri&&R.add(t);try{for(var o=e.__values(Object.entries(t)),a=o.next();!a.done;a=o.next()){var u=e.__read(a.value,2),s=u[0],f=u[1];if(f instanceof I){var h=r.get(f.$uri);Object.assign(t,(l={},l[s]=h,l))}else i(f)?Object.assign(t,(g={},g[s]=c(f,r),g)):"string"==typeof f&&"#"===f&&Object.assign(t,(y={},y[s]=r.get("#"),y))}}catch(t){p={error:t}}finally{try{a&&!a.done&&(d=o.return)&&d.call(o)}finally{if(p)throw p.error}}return t}return t;var p,d,l,g,y}function f(t,r){var n=new Map;if(i(t)&&Object.keys(t).length>0){if(r||n.set("#",t),R.has(t))return new Map;t.uri&&!n.has(t.uri)&&n.set(t.uri,t);var o=Array.isArray(t)||t instanceof x?t:Object.values(t);try{for(var a=e.__values(o),u=a.next();!u.done;u=a.next()){var s=f(u.value,!0);try{for(var c=e.__values(s.entries()),h=c.next();!h.done;h=c.next()){var p=e.__read(h.value,2),d=p[0],l=p[1];n.set(d,l)}}catch(t){v={error:t}}finally{try{h&&!h.done&&(m=c.return)&&m.call(c)}finally{if(v)throw v.error}}}}catch(t){g={error:t}}finally{try{u&&!u.done&&(y=a.return)&&y.call(a)}finally{if(g)throw g.error}}}return n;var g,y,v,m}function h(t){return new I(t)}function p(t,e){return i(t)?t instanceof E&&"string"==typeof t.uri?{$ref:""+O(t.uri,e)}:t instanceof Date?{$date:t.getTime()}:Array.isArray(t)?t.map(function(t){return p(t,e)}):a(t,function(t){return r(t)},function(t){return p(t,e)}):t}function d(t){return"string"==typeof t&&t.length>0?/^\d+$/.test(t)?parseInt(t,10):t:Number.isInteger(t)?t:null}function l(t,e){var r=t.indexOf(e+"/");return-1!==r?d(t.substring(r).split("/").pop()):null}function g(t,r){var n=Object.entries(r).find(function(r){var n=e.__read(r,1)[0];return 0===t.indexOf(n+"/")});if(n){var i=e.__read(n,2);return{resourceURI:i[0],resource:i[1]}}}function y(t,e){var r=g(t,e);return!!r&&null!==l(t,r.resourceURI)}function v(t){var e=t.$type,r=t.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof e}function m(t){var e=t.$uri,r=t.$ref,n=t.$type,i=t.$id;return"string"==typeof e?decodeURIComponent(e):"string"==typeof r?decodeURIComponent(r):v({$type:n,$id:i})?"/"+n+"/"+i:""}function b(t,e){return t.includes(e)?t.substring(e.length):t}function O(t,e){return"string"!=typeof e||t.includes(e)?t:""+e+t}function P(t){return M.getOwnMetadata(N,t)}function _(t,e){M.defineMetadata(N,e,t)}function j(t){return M.getOwnMetadata(q,t)}function $(t,e){M.defineMetadata(q,e,t)}function w(t){return M.getOwnMetadata(C,t.constructor)||Promise}function T(t,e){var r=M.getOwnMetadata(J,t);return r&&r[e]}function A(t,e){var r=o(t)?t:o(t.constructor)?t.constructor:null;if(null!==r){M.defineMetadata(J,Object.assign(M.getOwnMetadata(J,r)||{},(n={},n[e]=!0,n)),r);var n}}function S(t,e){var r=(void 0===e?{}:e).method;return function(e,n){var i=void 0===n?{}:n,a=i.paginate,u=void 0!==a&&a,s=i.cache,c=void 0===s||s,f=o(this),h=""+(f?j(this):this.uri)+t,p={method:r,paginate:u,cache:c};return"GET"===r?p.search=e:["POST","PUT","PATCH"].includes(r)&&(p.data=e),P(f?this:this.constructor).fetch(h,p)}}var E=function(){function t(t){void 0===t&&(t={}),this.$id=null,Object.assign(this,t)}return t.fetch=function(t,e){var r=(void 0===e?{}:e).cache,n=void 0===r||r,i=j(this);return P(this).fetch(i+"/"+t,{method:"GET",cache:n})},t.query=function(t,e){var r=void 0===e?{}:e,n=r.paginate,i=void 0!==n&&n,o=r.cache,a=void 0===o||o,u=j(this);return P(this).fetch(u,{method:"GET",search:t,paginate:i,cache:a})},t.first=function(t){function e(t){return t[0]}return this.query(t).then(e)},Object.defineProperty(t.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),t.prototype.equals=function(e){return e instanceof t&&(this.id===e.id&&this.constructor.name===e.constructor.name)},t.prototype.toJSON=function(){var t=this,e={};return Object.keys(this).filter(function(e){return!e.startsWith("$")&&!T(t.constructor,e)}).forEach(function(r){e[r]=t[r]}),e},t.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var t=this.constructor;return P(t).fetch(j(t),{method:"POST",data:this.toJSON(),cache:!0})},t.prototype.update=function(t){return void 0===t&&(t={}),P(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:t})},t.prototype.destroy=function(){function t(){n.get(e)&&n.remove(e)}var e=this.uri,r=P(this.constructor),n=r.cache;return r.fetch(e,{method:"DELETE"}).then(t)},t}(),x=function(){function t(t,r,n,i){var o=t.potion,a=t.uri;this.items=r,this.options=i,this.potion=o,this.uri=a;var u=e.__assign({},this.options.search),s=u.page,c=void 0===s?1:s,f=u.perPage,h=void 0===f?25:f;this.$page=c,this.$perPage=h,this.$total=parseInt(n,10)}return Object.defineProperty(t,Symbol.species,{get:function(){return t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"page",{get:function(){return this.$page},set:function(t){this.changePageTo(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),t.prototype[Symbol.iterator]=function(){return this.items.values()},t.prototype.toArray=function(){return this.items.slice(0)},t.prototype.at=function(t){return this.items[t]},t.prototype.changePageTo=function(t){var e=this.options.pagination;return this.options.search.page=t,this.$page=t,this.potion.fetch(this.uri,this.options,{pagination:e})},t.prototype.update=function(t,r){return(n=this.items).splice.apply(n,e.__spread([0,this.items.length],t)),this.$total=r,this;var n},t}(),I=function(){function t(t){this.$uri=t}return t.prototype.matches=function(t){return i(t)&&this.$uri===t.uri},t}(),R=new WeakSet,U=function(){function t(){this.items=new Map}return t.prototype.has=function(t){return this.items.has(t)},t.prototype.get=function(t){return this.items.get(t)},t.prototype.put=function(t,e){return this.items.set(t,e).get(t)},t.prototype.remove=function(t){this.items.delete(t)},t}(),M=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!M||!M.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var N=Symbol("potion"),q=Symbol("potion:uri"),C=Symbol("potion:promise"),J=Symbol("potion:readonly"),k=function(){function t(t){var e=void 0===t?{}:t,r=e.host,n=void 0===r?"":r,i=e.prefix,o=void 0===i?"":i,a=e.cache;this.resources={},this.Promise=w(this),this.requests=new Map,this.cache=a||new U,this.host=n,this.prefix=o}return t.prototype.register=function(t,e,r){if(!o(e))throw new TypeError("An error occurred while trying to register a resource for "+t+". "+e+" is not a function.");return _(e,this),$(e,t),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(t){return A(e,t)}),this.resources[t]=e,e},t.prototype.registerAs=function(t,e){var r=this;return function(n){return r.register(t,n,e),n}},t.prototype.fetch=function(t,r,n){var i=b(t,this.prefix),o=e.__assign({},r,n,{origin:[]});return y(t,this.resources)&&Object.assign(o,{origin:[i]}),this.resolve(t,o).then(function(t){return c(t,f(t)),t})},t.prototype.resolve=function(t,e){var r=this,n=this,i=n.Promise,o=n.prefix,a=b(t,o);t=O(t,o);var s=function(){return r.request(""+r.host+t,r.serialize(e)).then(function(n){return r.deserialize(n,t,e)})};return"GET"!==e.method||e.paginate||e.search?s():e.cache&&this.cache.has(a)?this.cache.get(a):(this.requests.has(a)||this.requests.set(a,s().then(function(t){return r.requests.delete(a),t},function(e){r.requests.delete(a);var n=u(e,t);return i.reject(n)})),this.requests.get(a))},t.prototype.serialize=function(t){var r=this.prefix,n=t.search;return e.__assign({},t,{search:p(t.paginate?e.__assign({page:1,perPage:25},n):n,r),data:p(t.data,r)})},t.prototype.deserialize=function(t,e,r){var n=this,i=t.data,o=t.headers;return this.fromPotionJSON(i,r.origin).then(function(t){if(r.paginate){var i=o["x-total-count"]||t.length;if(r.pagination instanceof x)return r.pagination.update(t,i);var a=new x({uri:e,potion:n},t,i,r);return Object.assign(r,{pagination:a}),a}return t})},t.prototype.fromPotionJSON=function(t,r){var n=this,i=this.Promise;if("object"==typeof t&&null!==t){if(Array.isArray(t))return i.all(t.map(function(t){return n.fromPotionJSON(t,r)}));if("string"==typeof t.$uri||v(t))return this.parseURI(t).then(function(o){var a=o.resource,u=o.id,s=o.uri,c={$id:u,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(t,r);return n.cache.has(s)?i.all([f,n.cache.get(s)]).then(function(t){var r=e.__read(t,2),n=r[0],i=r[1];return Object.assign(i,n,c),i}):n.cache.put(s,f.then(function(t){return Reflect.construct(a,[e.__assign({},t,c)])}))});if("string"==typeof t.$schema)return i.resolve(s(t));if(1===Object.keys(t).length){if("string"==typeof t.$ref)return"#"===t.$ref?i.resolve(t.$ref):this.parseURI(t).then(function(t){var e=t.uri;return r.includes(e)?i.resolve(h(e)):n.resolve(e,{cache:!0,method:"GET",origin:r})});if(void 0!==t.$date)return i.resolve(new Date(t.$date))}return this.parsePotionJSONProperties(t,r)}return i.resolve(t)},t.prototype.parsePotionJSONProperties=function(t,r){var i=this,o=this.Promise,a=Object.entries(t),u=a.map(function(t){var n=e.__read(t,2)[1];return i.fromPotionJSON(n,r)}),s=a.map(function(t){return n(e.__read(t,1)[0])});return o.all(u).then(function(t){return t.map(function(t,e){return[s[e],t]}).reduce(function(t,r){var n=e.__read(r,2),i=n[0],o=n[1];return Object.assign(t,(a={},a[i]=o,a));var a},{})})},t.prototype.parseURI=function(t){var e=t.$ref,r=t.$uri,n=t.$type,i=t.$id,o=this.Promise,a=b(m({$ref:e,$uri:r,$type:n,$id:i}),this.prefix),u=g(a,this.resources);if(u){var s=u.resourceURI,c={resource:u.resource,uri:a},f=d(i);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:l(a,s)}),o.resolve(c)}return o.reject(new Error("URI '"+a+"' is an uninterpretable or unknown Potion resource."))},t}(),D={GET:function(t){return S(t,{method:"GET"})},DELETE:function(t){return S(t,{method:"DELETE"})},POST:function(t){return S(t,{method:"POST"})},PATCH:function(t){return S(t,{method:"PATCH"})},PUT:function(t){return S(t,{method:"PUT"})}},G=function(t){function r(r){return t.call(this,e.__assign({},r))||this}return e.__extends(r,t),r.prototype.request=function(t,r){var n=e.__assign({},r),i=n.method,o=void 0===i?"GET":i,a=n.search,u=n.data,s=n.cache,c=void 0===s||s,f=new Headers,h={method:o,cache:c?"default":"no-cache",credentials:"include"};if(u&&(f.set("Accept","application/json"),f.set("Content-Type","application/json"),h.body=JSON.stringify(u)),Object.assign(h,{headers:f}),a){var p=1,d=Object.entries(a),l=d.length;try{for(var g=e.__values(d),y=g.next();!y.done;y=g.next()){var v=e.__read(y.value,2),m=v[0],b=v[1];1===p&&(t+="?"),t+=m+"="+b,p<l&&(t+="&"),p++}}catch(t){O={error:t}}finally{try{y&&!y.done&&(P=g.return)&&P.call(g)}finally{if(O)throw O.error}}}return fetch(new Request(t,h),h).then(function(t){if(t.ok){var e={};return t.headers&&t.headers.forEach(function(t,r){e[r]=t}),t.json().then(function(t){return{headers:e,data:t}},function(t){return t})}var r=new Error(t.statusText);throw Object.assign(r,{response:t}),r});var O,P},r}(k);t.Potion=G,t.Item=E,t.readonly=A,t.Pagination=x,t.PotionBase=k,t.Route=D,t.route=S,t.findPotionResource=g,t.fromSchemaJSON=s,t.getPotionID=l,t.getPotionURI=m,t.hasTypeAndId=v,t.isPotionURI=y,t.parsePotionID=d,t.removePrefixFromURI=b,t.addPrefixToURI=O,t.toPotionJSON=p,t.toCamelCase=n,t.toSnakeCase=r,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=fetch.umd.min.js.map |
103
ng.es5.js
@@ -269,7 +269,7 @@ import { __assign, __extends, __read, __spread, __values } from 'tslib'; | ||
/** | ||
* @param {?} uri | ||
* @param {?} json | ||
* @return {?} | ||
*/ | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -309,3 +309,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -319,20 +319,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var /** @type {?} */ ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = __values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var /** @type {?} */ ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -343,3 +344,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -351,3 +352,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -357,14 +358,19 @@ /** | ||
* @param {?} json - A Potion JSON. | ||
* @param {?=} skip | ||
* @return {?} | ||
*/ | ||
function findRoots(json) { | ||
var /** @type {?} */ roots = []; | ||
function findRoots(json, skip) { | ||
var /** @type {?} */ roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -375,35 +381,28 @@ var /** @type {?} */ values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, __spread(findRoots(value))); | ||
var /** @type {?} */ result = findRoots(value, true); | ||
try { | ||
for (var _a = __values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = __read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var /** @type {?} */ result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = __values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(/** @type {?} */ root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -410,0 +409,0 @@ /** |
44
ng.js
@@ -225,7 +225,7 @@ import { Inject, Injectable, InjectionToken, NgModule, Optional, SkipSelf, forwardRef } from '@angular/core'; | ||
/** | ||
* @param {?} uri | ||
* @param {?} json | ||
* @return {?} | ||
*/ | ||
matches(uri) { | ||
return this.$uri === uri; | ||
matches(json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
} | ||
@@ -264,3 +264,3 @@ } | ||
// Find the ref in the roots. | ||
return roots.find(item => json.matches(item.uri)); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -276,3 +276,3 @@ else if (Object.keys(json).length > 0) { | ||
if (value instanceof SelfReference) { | ||
const /** @type {?} */ ref = roots.find(item => value.matches(item.uri)); | ||
const /** @type {?} */ ref = roots.get(value.$uri); | ||
Object.assign(json, { | ||
@@ -287,2 +287,7 @@ [key]: ref | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, { | ||
[key]: roots.get('#') | ||
}); | ||
} | ||
} | ||
@@ -296,28 +301,29 @@ return json; | ||
* @param {?} json - A Potion JSON. | ||
* @param {?=} skip | ||
* @return {?} | ||
*/ | ||
function findRoots(json) { | ||
const /** @type {?} */ roots = []; | ||
function findRoots(json, skip) { | ||
const /** @type {?} */ roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
const /** @type {?} */ values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
for (const /** @type {?} */ value of values) { | ||
roots.push(...findRoots(value)); | ||
const /** @type {?} */ result = findRoots(value, true); | ||
for (const [uri, root] of result.entries()) { | ||
roots.set(uri, root); | ||
} | ||
} | ||
} | ||
// Remove duplicate entries. | ||
const /** @type {?} */ result = []; | ||
for (const /** @type {?} */ root of roots) { | ||
if (result.findIndex(item => root.uri === item.uri) === -1) { | ||
result.push(root); | ||
} | ||
} | ||
return result; | ||
return roots; | ||
} | ||
@@ -324,0 +330,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
{"__symbolic":"module","version":3,"metadata":{"ItemOptions":{"__symbolic":"interface"},"Item":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"equals":[{"__symbolic":"method"}],"toJSON":[{"__symbolic":"method"}],"save":[{"__symbolic":"method"}],"update":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}]}},"readonly":{"__symbolic":"function"},"Pagination":{"__symbolic":"class","arity":1,"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"error","message":"Expression form not supported","line":53,"character":28,"module":"./core/pagination"},{"__symbolic":"reference","name":"Array","arguments":[{"__symbolic":"error","message":"Could not resolve type","line":53,"character":78,"context":{"typeName":"T"},"module":"./core/pagination"}]},{"__symbolic":"reference","name":"string"},{"__symbolic":"reference","name":"FetchOptions"}]}],"toArray":[{"__symbolic":"method"}],"at":[{"__symbolic":"method"}],"changePageTo":[{"__symbolic":"method"}],"update":[{"__symbolic":"method"}]}},"ItemCache":{"__symbolic":"interface"},"FetchExtras":{"__symbolic":"interface"},"ParsedURI":{"__symbolic":"interface"},"PotionBase":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"register":[{"__symbolic":"method"}],"registerAs":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}],"fetch":[{"__symbolic":"method"}],"resolve":[{"__symbolic":"method"}],"serialize":[{"__symbolic":"method"}],"deserialize":[{"__symbolic":"method"}],"fromPotionJSON":[{"__symbolic":"method"}],"parsePotionJSONProperties":[{"__symbolic":"method"}],"parseURI":[{"__symbolic":"method"}]}},"PotionOptions":{"__symbolic":"interface"},"PotionResponse":{"__symbolic":"interface"},"QueryOptions":{"__symbolic":"interface"},"RequestOptions":{"__symbolic":"interface"},"URLSearchParams":{"__symbolic":"interface"},"Route":{},"route":{"__symbolic":"function","parameters":["path","method"],"defaults":[null,{}],"value":{"__symbolic":"error","message":"Function call not supported","line":11,"character":8,"module":"./core/route"}},"findPotionResource":{"__symbolic":"function"},"fromSchemaJSON":{"__symbolic":"function"},"getPotionID":{"__symbolic":"function"},"getPotionURI":{"__symbolic":"function"},"hasTypeAndId":{"__symbolic":"function","parameters":["$type","$id"],"value":{"__symbolic":"binop","operator":"&&","left":{"__symbolic":"binop","operator":"||","left":{"__symbolic":"binop","operator":"===","left":{"__symbolic":"error","message":"Expression form not supported","line":273,"character":9,"module":"./core/utils"},"right":"string"},"right":{"__symbolic":"call","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"Number"},"member":"isInteger"},"arguments":[{"__symbolic":"reference","name":"$id"}]}},"right":{"__symbolic":"binop","operator":"===","left":{"__symbolic":"error","message":"Expression form not supported","line":273,"character":62,"module":"./core/utils"},"right":"string"}}},"isPotionURI":{"__symbolic":"function"},"parsePotionID":{"__symbolic":"function"},"removePrefixFromURI":{"__symbolic":"function"},"addPrefixToURI":{"__symbolic":"function"},"toPotionJSON":{"__symbolic":"function"},"toCamelCase":{"__symbolic":"function","parameters":["str"],"value":{"__symbolic":"error","message":"Expression form not supported","line":19,"character":20,"module":"./core/utils"}},"toSnakeCase":{"__symbolic":"function","parameters":["str","separator"],"defaults":[null,"_"],"value":{"__symbolic":"error","message":"Expression form not supported","line":11,"character":11,"module":"./core/utils"}},"PotionModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","name":"POTION_PROVIDER"}]}]}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"Potion"}]}],[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_RESOURCES"}]}]],"parameters":[{"__symbolic":"reference","name":"Potion"},{"__symbolic":"reference","name":"Array","arguments":[{"__symbolic":"reference","name":"PotionResources"}]}]}]}},"POTION_RESOURCES":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionResources"]},"PotionResources":{"__symbolic":"interface"},"POTION_CONFIG":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionConfig"]},"PotionConfig":{"__symbolic":"interface"},"POTION_HTTP":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionHttp"]},"PotionHttp":{"__symbolic":"interface"},"Potion":{"__symbolic":"class","extends":{"__symbolic":"reference","name":"PotionBase"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_CONFIG"}]}],[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_HTTP"}]}]],"parameters":[{"__symbolic":"reference","module":"@angular/http","name":"Http"},{"__symbolic":"reference","name":"any"},{"__symbolic":"reference","name":"any"}]}],"registerFromProvider":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}]}},"POTION_PROVIDER_FACTORY":{"__symbolic":"function","parameters":["parentFactory","http","config","customHttp"],"value":{"__symbolic":"binop","operator":"||","left":{"__symbolic":"reference","name":"parentFactory"},"right":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"Potion"},"arguments":[{"__symbolic":"reference","name":"http"},{"__symbolic":"reference","name":"config"},{"__symbolic":"reference","name":"customHttp"}]}}},"POTION_PROVIDER":{"provide":{"__symbolic":"reference","name":"Potion"},"useFactory":{"__symbolic":"reference","name":"POTION_PROVIDER_FACTORY"},"deps":[[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"SkipSelf"}},{"__symbolic":"reference","name":"Potion"}],{"__symbolic":"reference","module":"@angular/http","name":"Http"},[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_CONFIG"}]}],[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_HTTP"}]}]]}},"origins":{"ItemOptions":"./core/item","Item":"./core/item","readonly":"./core/metadata","Pagination":"./core/pagination","ItemCache":"./core/potion","FetchExtras":"./core/potion","FetchOptions":"./core/potion","ParsedURI":"./core/potion","PotionBase":"./core/potion","PotionOptions":"./core/potion","PotionResponse":"./core/potion","QueryOptions":"./core/potion","RequestOptions":"./core/potion","URLSearchParams":"./core/potion","Route":"./core/route","RouteType":"./core/route","route":"./core/route","findPotionResource":"./core/utils","fromSchemaJSON":"./core/utils","getPotionID":"./core/utils","getPotionURI":"./core/utils","hasTypeAndId":"./core/utils","isPotionURI":"./core/utils","parsePotionID":"./core/utils","PotionID":"./core/utils","removePrefixFromURI":"./core/utils","addPrefixToURI":"./core/utils","toPotionJSON":"./core/utils","toCamelCase":"./core/utils","toSnakeCase":"./core/utils","PotionModule":"./ng/index","POTION_RESOURCES":"./ng/potion","PotionResources":"./ng/potion","POTION_CONFIG":"./ng/potion","PotionConfig":"./ng/potion","POTION_HTTP":"./ng/potion","PotionHttp":"./ng/potion","Potion":"./ng/potion","POTION_PROVIDER_FACTORY":"./ng/potion","POTION_PROVIDER":"./ng/potion"},"importAs":"potion-client"} | ||
{"__symbolic":"module","version":3,"metadata":{"ItemOptions":{"__symbolic":"interface"},"Item":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"equals":[{"__symbolic":"method"}],"toJSON":[{"__symbolic":"method"}],"save":[{"__symbolic":"method"}],"update":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}]}},"readonly":{"__symbolic":"function"},"Pagination":{"__symbolic":"class","arity":1,"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"error","message":"Expression form not supported","line":53,"character":28,"module":"./core/pagination"},{"__symbolic":"reference","name":"Array","arguments":[{"__symbolic":"error","message":"Could not resolve type","line":53,"character":78,"context":{"typeName":"T"},"module":"./core/pagination"}]},{"__symbolic":"reference","name":"string"},{"__symbolic":"reference","name":"FetchOptions"}]}],"toArray":[{"__symbolic":"method"}],"at":[{"__symbolic":"method"}],"changePageTo":[{"__symbolic":"method"}],"update":[{"__symbolic":"method"}]}},"ItemCache":{"__symbolic":"interface"},"FetchExtras":{"__symbolic":"interface"},"ParsedURI":{"__symbolic":"interface"},"PotionBase":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"register":[{"__symbolic":"method"}],"registerAs":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}],"fetch":[{"__symbolic":"method"}],"resolve":[{"__symbolic":"method"}],"serialize":[{"__symbolic":"method"}],"deserialize":[{"__symbolic":"method"}],"fromPotionJSON":[{"__symbolic":"method"}],"parsePotionJSONProperties":[{"__symbolic":"method"}],"parseURI":[{"__symbolic":"method"}]}},"PotionOptions":{"__symbolic":"interface"},"PotionResponse":{"__symbolic":"interface"},"QueryOptions":{"__symbolic":"interface"},"RequestOptions":{"__symbolic":"interface"},"URLSearchParams":{"__symbolic":"interface"},"Route":{},"route":{"__symbolic":"function","parameters":["path","method"],"defaults":[null,{}],"value":{"__symbolic":"error","message":"Function call not supported","line":11,"character":8,"module":"./core/route"}},"findPotionResource":{"__symbolic":"function"},"fromSchemaJSON":{"__symbolic":"function"},"getPotionID":{"__symbolic":"function"},"getPotionURI":{"__symbolic":"function"},"hasTypeAndId":{"__symbolic":"function","parameters":["$type","$id"],"value":{"__symbolic":"binop","operator":"&&","left":{"__symbolic":"binop","operator":"||","left":{"__symbolic":"binop","operator":"===","left":{"__symbolic":"error","message":"Expression form not supported","line":276,"character":9,"module":"./core/utils"},"right":"string"},"right":{"__symbolic":"call","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"Number"},"member":"isInteger"},"arguments":[{"__symbolic":"reference","name":"$id"}]}},"right":{"__symbolic":"binop","operator":"===","left":{"__symbolic":"error","message":"Expression form not supported","line":276,"character":62,"module":"./core/utils"},"right":"string"}}},"isPotionURI":{"__symbolic":"function"},"parsePotionID":{"__symbolic":"function"},"removePrefixFromURI":{"__symbolic":"function"},"addPrefixToURI":{"__symbolic":"function"},"toPotionJSON":{"__symbolic":"function"},"toCamelCase":{"__symbolic":"function","parameters":["str"],"value":{"__symbolic":"error","message":"Expression form not supported","line":19,"character":20,"module":"./core/utils"}},"toSnakeCase":{"__symbolic":"function","parameters":["str","separator"],"defaults":[null,"_"],"value":{"__symbolic":"error","message":"Expression form not supported","line":11,"character":11,"module":"./core/utils"}},"PotionModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","name":"POTION_PROVIDER"}]}]}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"Potion"}]}],[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_RESOURCES"}]}]],"parameters":[{"__symbolic":"reference","name":"Potion"},{"__symbolic":"reference","name":"Array","arguments":[{"__symbolic":"reference","name":"PotionResources"}]}]}]}},"POTION_RESOURCES":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionResources"]},"PotionResources":{"__symbolic":"interface"},"POTION_CONFIG":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionConfig"]},"PotionConfig":{"__symbolic":"interface"},"POTION_HTTP":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken"},"arguments":["PotionHttp"]},"PotionHttp":{"__symbolic":"interface"},"Potion":{"__symbolic":"class","extends":{"__symbolic":"reference","name":"PotionBase"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_CONFIG"}]}],[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_HTTP"}]}]],"parameters":[{"__symbolic":"reference","module":"@angular/http","name":"Http"},{"__symbolic":"reference","name":"any"},{"__symbolic":"reference","name":"any"}]}],"registerFromProvider":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}]}},"POTION_PROVIDER_FACTORY":{"__symbolic":"function","parameters":["parentFactory","http","config","customHttp"],"value":{"__symbolic":"binop","operator":"||","left":{"__symbolic":"reference","name":"parentFactory"},"right":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"Potion"},"arguments":[{"__symbolic":"reference","name":"http"},{"__symbolic":"reference","name":"config"},{"__symbolic":"reference","name":"customHttp"}]}}},"POTION_PROVIDER":{"provide":{"__symbolic":"reference","name":"Potion"},"useFactory":{"__symbolic":"reference","name":"POTION_PROVIDER_FACTORY"},"deps":[[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"SkipSelf"}},{"__symbolic":"reference","name":"Potion"}],{"__symbolic":"reference","module":"@angular/http","name":"Http"},[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_CONFIG"}]}],[{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}},{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","name":"POTION_HTTP"}]}]]}},"origins":{"ItemOptions":"./core/item","Item":"./core/item","readonly":"./core/metadata","Pagination":"./core/pagination","ItemCache":"./core/potion","FetchExtras":"./core/potion","FetchOptions":"./core/potion","ParsedURI":"./core/potion","PotionBase":"./core/potion","PotionOptions":"./core/potion","PotionResponse":"./core/potion","QueryOptions":"./core/potion","RequestOptions":"./core/potion","URLSearchParams":"./core/potion","Route":"./core/route","RouteType":"./core/route","route":"./core/route","findPotionResource":"./core/utils","fromSchemaJSON":"./core/utils","getPotionID":"./core/utils","getPotionURI":"./core/utils","hasTypeAndId":"./core/utils","isPotionURI":"./core/utils","parsePotionID":"./core/utils","PotionID":"./core/utils","removePrefixFromURI":"./core/utils","addPrefixToURI":"./core/utils","toPotionJSON":"./core/utils","toCamelCase":"./core/utils","toSnakeCase":"./core/utils","PotionModule":"./ng/index","POTION_RESOURCES":"./ng/potion","PotionResources":"./ng/potion","POTION_CONFIG":"./ng/potion","PotionConfig":"./ng/potion","POTION_HTTP":"./ng/potion","PotionHttp":"./ng/potion","Potion":"./ng/potion","POTION_PROVIDER_FACTORY":"./ng/potion","POTION_PROVIDER":"./ng/potion"},"importAs":"potion-client"} |
103
ng.umd.js
@@ -268,7 +268,7 @@ (function (global, factory) { | ||
/** | ||
* @param {?} uri | ||
* @param {?} json | ||
* @return {?} | ||
*/ | ||
SelfReference.prototype.matches = function (uri) { | ||
return this.$uri === uri; | ||
SelfReference.prototype.matches = function (json) { | ||
return isJsObject(json) && this.$uri === json.uri; | ||
}; | ||
@@ -308,3 +308,3 @@ return SelfReference; | ||
// Find the ref in the roots. | ||
return roots.find(function (item) { return json.matches(item.uri); }); | ||
return roots.get(json.$uri); | ||
} | ||
@@ -318,20 +318,21 @@ else if (Object.keys(json).length > 0) { | ||
} | ||
var _loop_1 = function (key, value) { | ||
if (value instanceof SelfReference) { | ||
var /** @type {?} */ ref = roots.find(function (item) { return value.matches(item.uri); }); | ||
Object.assign(json, (_a = {}, | ||
_a[key] = ref, | ||
_a)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_b = {}, | ||
_b[key] = replaceSelfReferences(value, roots), | ||
_b)); | ||
} | ||
var _a, _b; | ||
}; | ||
try { | ||
for (var _a = tslib_1.__values(Object.entries(json)), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), key = _c[0], value = _c[1]; | ||
_loop_1(key, value); | ||
if (value instanceof SelfReference) { | ||
var /** @type {?} */ ref = roots.get(value.$uri); | ||
Object.assign(json, (_d = {}, | ||
_d[key] = ref, | ||
_d)); | ||
} | ||
else if (isJsObject(value)) { | ||
Object.assign(json, (_e = {}, | ||
_e[key] = replaceSelfReferences(value, roots), | ||
_e)); | ||
} | ||
else if (typeof value === 'string' && value === '#') { | ||
Object.assign(json, (_f = {}, | ||
_f[key] = roots.get('#'), | ||
_f)); | ||
} | ||
} | ||
@@ -342,3 +343,3 @@ } | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
if (_b && !_b.done && (_g = _a.return)) _g.call(_a); | ||
} | ||
@@ -350,3 +351,3 @@ finally { if (e_1) throw e_1.error; } | ||
return json; | ||
var e_1, _d; | ||
var e_1, _g, _d, _e, _f; | ||
} | ||
@@ -356,14 +357,19 @@ /** | ||
* @param {?} json - A Potion JSON. | ||
* @param {?=} skip | ||
* @return {?} | ||
*/ | ||
function findRoots(json) { | ||
var /** @type {?} */ roots = []; | ||
function findRoots(json, skip) { | ||
var /** @type {?} */ roots = new Map(); | ||
if (isJsObject(json) && Object.keys(json).length > 0) { | ||
// NOTE: We add the root json because we might encouter '#' wich resolves to the root object. | ||
if (!skip) { | ||
roots.set('#', json); | ||
} | ||
if (set.has(json)) { | ||
// If we find the root in the set it means there is no need to continue. | ||
return []; | ||
return new Map(); | ||
} | ||
else if (json.uri) { | ||
else if (json.uri && !roots.has(json.uri)) { | ||
// We only want to append unique roots. | ||
roots.push(json); | ||
roots.set(json.uri, json); | ||
} | ||
@@ -374,35 +380,28 @@ var /** @type {?} */ values = Array.isArray(json) || json instanceof Pagination ? json : Object.values(json); | ||
var value = values_1_1.value; | ||
roots.push.apply(roots, tslib_1.__spread(findRoots(value))); | ||
var /** @type {?} */ result = findRoots(value, true); | ||
try { | ||
for (var _a = tslib_1.__values(result.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
var _c = tslib_1.__read(_b.value, 2), uri = _c[0], root = _c[1]; | ||
roots.set(uri, root); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_d = _a.return)) _d.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); | ||
if (values_1_1 && !values_1_1.done && (_e = values_1.return)) _e.call(values_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
// Remove duplicate entries. | ||
var /** @type {?} */ result = []; | ||
var _loop_2 = function (root) { | ||
if (result.findIndex(function (item) { return root.uri === item.uri; }) === -1) { | ||
result.push(root); | ||
} | ||
}; | ||
try { | ||
for (var roots_1 = tslib_1.__values(roots), roots_1_1 = roots_1.next(); !roots_1_1.done; roots_1_1 = roots_1.next()) { | ||
var root = roots_1_1.value; | ||
_loop_2(/** @type {?} */ root); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (roots_1_1 && !roots_1_1.done && (_b = roots_1.return)) _b.call(roots_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return result; | ||
var e_2, _a, e_3, _b; | ||
return roots; | ||
var e_3, _e, e_2, _d; | ||
} | ||
@@ -409,0 +408,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tslib"),require("@angular/core"),require("@angular/http"),require("rxjs/add/operator/map"),require("rxjs/add/operator/toPromise")):"function"==typeof define&&define.amd?define(["exports","tslib","@angular/core","@angular/http","rxjs/add/operator/map","rxjs/add/operator/toPromise"],t):t(e.potionClient=e.potionClient||{},e.tslib,e.ng.core,e.ng.http)}(this,function(e,t,r,n){"use strict";function o(e,t){return void 0===t&&(t="_"),e.replace(/\.?([A-Z0-9]+)/g,function(e,r){return""+t+r.toLowerCase()}).replace(/^_/,"")}function i(e){return e.replace(/_([a-z0-9])/g,function(e){return e[1].toUpperCase()})}function a(e){return"object"==typeof e&&null!==e}function u(e){return 0===Object.keys(e).length}function s(e){return"function"==typeof e}function c(e,r,n){return a(e)&&!Array.isArray(e)?Object.entries(e).map(function(e){var o=t.__read(e,2),i=o[0],a=o[1];return[s(r)?r(i):i,s(n)?n(a):a]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{}):e}function f(e,t){var r="An error occurred while Potion tried to retrieve a resource";return e instanceof Error?e.message:"string"==typeof e?e:"string"==typeof t?r+" from '"+t+"'.":r+"."}function p(e){return Array.isArray(e)?e.map(function(e){return"object"==typeof e?p(e):e}):a(e)?Object.entries(e).map(function(e){var r=t.__read(e,2),n=r[0],o=r[1];return[i(n),"object"==typeof o?p(o):o]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{}):e}function h(e,r){if("object"!=typeof e||null===e)return e;if(q.has(e))return e;if(e instanceof C){var n=e.toArray().map(function(e){return h(e,r)});return e.update(n,e.total)}if(Array.isArray(e))return e.map(function(e){return h(e,r)});if(e instanceof U)return r.find(function(t){return e.matches(t.uri)});if(Object.keys(e).length>0){!Array.isArray(e)&&e.uri&&q.add(e);try{for(var o=t.__values(Object.entries(e)),i=o.next();!i.done;i=o.next()){var u=t.__read(i.value,2);!function(t,n){if(n instanceof U){var o=r.find(function(e){return n.matches(e.uri)});Object.assign(e,(i={},i[t]=o,i))}else a(n)&&Object.assign(e,(u={},u[t]=h(n,r),u));var i,u}(u[0],u[1])}}catch(e){s={error:e}}finally{try{i&&!i.done&&(c=o.return)&&c.call(o)}finally{if(s)throw s.error}}return e}return e;var s,c}function d(e){var r=[];if(a(e)&&Object.keys(e).length>0){if(q.has(e))return[];e.uri&&r.push(e);var n=Array.isArray(e)||e instanceof C?e:Object.values(e);try{for(var o=t.__values(n),i=o.next();!i.done;i=o.next()){var u=i.value;r.push.apply(r,t.__spread(d(u)))}}catch(e){p={error:e}}finally{try{i&&!i.done&&(h=o.return)&&h.call(o)}finally{if(p)throw p.error}}}var s=[];try{for(var c=t.__values(r),f=c.next();!f.done;f=c.next())!function(e){-1===s.findIndex(function(t){return e.uri===t.uri})&&s.push(e)}(f.value)}catch(e){l={error:e}}finally{try{f&&!f.done&&(y=c.return)&&y.call(c)}finally{if(l)throw l.error}}return s;var p,h,l,y}function l(e){return new U(e)}function y(e,t){return a(e)?e instanceof F&&"string"==typeof e.uri?{$ref:""+j(e.uri,t)}:e instanceof Date?{$date:e.getTime()}:Array.isArray(e)?e.map(function(e){return y(e,t)}):c(e,function(e){return o(e)},function(e){return y(e,t)}):e}function g(e){return"string"==typeof e&&e.length>0?/^\d+$/.test(e)?parseInt(e,10):e:Number.isInteger(e)?e:null}function v(e,t){var r=e.indexOf(t+"/");return-1!==r?g(e.substring(r).split("/").pop()):null}function m(e,r){var n=Object.entries(r).find(function(r){var n=t.__read(r,1)[0];return 0===e.indexOf(n+"/")});if(n){var o=t.__read(n,2);return{resourceURI:o[0],resource:o[1]}}}function O(e,t){var r=m(e,t);return!!r&&null!==v(e,r.resourceURI)}function P(e){var t=e.$type,r=e.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof t}function _(e){var t=e.$uri,r=e.$ref,n=e.$type,o=e.$id;return"string"==typeof t?decodeURIComponent(t):"string"==typeof r?decodeURIComponent(r):P({$type:n,$id:o})?"/"+n+"/"+o:""}function b(e,t){return e.includes(t)?e.substring(t.length):e}function j(e,t){return"string"!=typeof t||e.includes(t)?e:""+t+e}function w(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var n={};try{for(var o=t.__values(e),i=o.next();!i.done;i=o.next()){var a=i.value;Object.assign(n,a)}}catch(e){u={error:e}}finally{try{i&&!i.done&&(s=o.return)&&s.call(o)}finally{if(u)throw u.error}}return n;var u,s}function $(e){return J.getOwnMetadata(k,e)}function I(e,t){J.defineMetadata(k,t,e)}function T(e){return J.getOwnMetadata(H,e)}function x(e,t){J.defineMetadata(H,t,e)}function S(e){return J.getOwnMetadata(D,e.constructor)||Promise}function A(e,t){var r=J.getOwnMetadata(G,e);return r&&r[t]}function R(e,t){var r=s(e)?e:s(e.constructor)?e.constructor:null;if(null!==r){J.defineMetadata(G,Object.assign(J.getOwnMetadata(G,r)||{},(n={},n[t]=!0,n)),r);var n}}function E(e,t){var r=(void 0===t?{}:t).method;return function(t,n){var o=void 0===n?{}:n,i=o.paginate,a=void 0!==i&&i,u=o.cache,c=void 0===u||u,f=s(this),p=""+(f?T(this):this.uri)+e,h={method:r,paginate:a,cache:c};return"GET"===r?h.search=t:["POST","PUT","PATCH"].includes(r)&&(h.data=t),$(f?this:this.constructor).fetch(p,h)}}function N(e,t,r,n){return e||new Q(t,r,n)}var C=function(){function e(e,r,n,o){var i=e.potion,a=e.uri;this.items=r,this.options=o,this.potion=i,this.uri=a;var u=t.__assign({},this.options.search),s=u.page,c=void 0===s?1:s,f=u.perPage,p=void 0===f?25:f;this.$page=c,this.$perPage=p,this.$total=parseInt(n,10)}return Object.defineProperty(e,Symbol.species,{get:function(){return e},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"page",{get:function(){return this.$page},set:function(e){this.changePageTo(e)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),e.prototype[Symbol.iterator]=function(){return this.items.values()},e.prototype.toArray=function(){return this.items.slice(0)},e.prototype.at=function(e){return this.items[e]},e.prototype.changePageTo=function(e){var t=this.options.pagination;return this.options.search.page=e,this.$page=e,this.potion.fetch(this.uri,this.options,{pagination:t})},e.prototype.update=function(e,r){return(n=this.items).splice.apply(n,t.__spread([0,this.items.length],e)),this.$total=r,this;var n},e}(),U=function(){function e(e){this.$uri=e}return e.prototype.matches=function(e){return this.$uri===e},e}(),q=new WeakSet,M=function(){function e(){this.items=new Map}return e.prototype.has=function(e){return this.items.has(e)},e.prototype.get=function(e){return this.items.get(e)},e.prototype.put=function(e,t){return this.items.set(e,t).get(e)},e.prototype.remove=function(e){this.items.delete(e)},e}(),J=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!J||!J.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var k=Symbol("potion"),H=Symbol("potion:uri"),D=Symbol("potion:promise"),G=Symbol("potion:readonly"),F=function(){function e(e){void 0===e&&(e={}),this.$id=null,Object.assign(this,e)}return e.fetch=function(e,t){var r=(void 0===t?{}:t).cache,n=void 0===r||r,o=T(this);return $(this).fetch(o+"/"+e,{method:"GET",cache:n})},e.query=function(e,t){var r=void 0===t?{}:t,n=r.paginate,o=void 0!==n&&n,i=r.cache,a=void 0===i||i,u=T(this);return $(this).fetch(u,{method:"GET",search:e,paginate:o,cache:a})},e.first=function(e){function t(e){return e[0]}return this.query(e).then(t)},Object.defineProperty(e.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),e.prototype.equals=function(t){return t instanceof e&&(this.id===t.id&&this.constructor.name===t.constructor.name)},e.prototype.toJSON=function(){var e=this,t={};return Object.keys(this).filter(function(t){return!t.startsWith("$")&&!A(e.constructor,t)}).forEach(function(r){t[r]=e[r]}),t},e.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var e=this.constructor;return $(e).fetch(T(e),{method:"POST",data:this.toJSON(),cache:!0})},e.prototype.update=function(e){return void 0===e&&(e={}),$(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:e})},e.prototype.destroy=function(){function e(){n.get(t)&&n.remove(t)}var t=this.uri,r=$(this.constructor),n=r.cache;return r.fetch(t,{method:"DELETE"}).then(e)},e}(),L=function(){function e(e){var t=void 0===e?{}:e,r=t.host,n=void 0===r?"":r,o=t.prefix,i=void 0===o?"":o,a=t.cache;this.resources={},this.Promise=S(this),this.requests=new Map,this.cache=a||new M,this.host=n,this.prefix=i}return e.prototype.register=function(e,t,r){if(!s(t))throw new TypeError("An error occurred while trying to register a resource for "+e+". "+t+" is not a function.");return I(t,this),x(t,e),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(e){return R(t,e)}),this.resources[e]=t,t},e.prototype.registerAs=function(e,t){var r=this;return function(n){return r.register(e,n,t),n}},e.prototype.request=function(e,t){},e.prototype.fetch=function(e,r,n){var o=b(e,this.prefix),i=t.__assign({},r,n,{origin:[]});return O(e,this.resources)&&Object.assign(i,{origin:[o]}),this.resolve(e,i).then(function(e){return h(e,d(e)),e})},e.prototype.resolve=function(e,t){var r=this,n=this,o=n.Promise,i=n.prefix,a=b(e,i);e=j(e,i);var u=function(){return r.request(""+r.host+e,r.serialize(t)).then(function(n){return r.deserialize(n,e,t)})};return"GET"!==t.method||t.paginate||t.search?u():t.cache&&this.cache.has(a)?this.cache.get(a):(this.requests.has(a)||this.requests.set(a,u().then(function(e){return r.requests.delete(a),e},function(t){r.requests.delete(a);var n=f(t,e);return o.reject(n)})),this.requests.get(a))},e.prototype.serialize=function(e){var r=this.prefix,n=e.search;return t.__assign({},e,{search:y(e.paginate?t.__assign({page:1,perPage:25},n):n,r),data:y(e.data,r)})},e.prototype.deserialize=function(e,t,r){var n=this,o=e.data,i=e.headers;return this.fromPotionJSON(o,r.origin).then(function(e){if(r.paginate){var o=i["x-total-count"]||e.length;if(r.pagination instanceof C)return r.pagination.update(e,o);var a=new C({uri:t,potion:n},e,o,r);return Object.assign(r,{pagination:a}),a}return e})},e.prototype.fromPotionJSON=function(e,r){var n=this,o=this.Promise;if("object"==typeof e&&null!==e){if(Array.isArray(e))return o.all(e.map(function(e){return n.fromPotionJSON(e,r)}));if("string"==typeof e.$uri||P(e))return this.parseURI(e).then(function(i){var a=i.resource,u=i.id,s=i.uri,c={$id:u,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(e,r);return n.cache.has(s)?o.all([f,n.cache.get(s)]).then(function(e){var r=t.__read(e,2),n=r[0],o=r[1];return Object.assign(o,n,c),o}):n.cache.put(s,f.then(function(e){return Reflect.construct(a,[t.__assign({},e,c)])}))});if("string"==typeof e.$schema)return o.resolve(p(e));if(1===Object.keys(e).length){if("string"==typeof e.$ref)return"#"===e.$ref?o.resolve(e.$ref):this.parseURI(e).then(function(e){var t=e.uri;return r.includes(t)?o.resolve(l(t)):n.resolve(t,{cache:!0,method:"GET",origin:r})});if(void 0!==e.$date)return o.resolve(new Date(e.$date))}return this.parsePotionJSONProperties(e,r)}return o.resolve(e)},e.prototype.parsePotionJSONProperties=function(e,r){var n=this,o=this.Promise,a=Object.entries(e),u=a.map(function(e){var o=t.__read(e,2)[1];return n.fromPotionJSON(o,r)}),s=a.map(function(e){return i(t.__read(e,1)[0])});return o.all(u).then(function(e){return e.map(function(e,t){return[s[t],e]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{})})},e.prototype.parseURI=function(e){var t=e.$ref,r=e.$uri,n=e.$type,o=e.$id,i=this.Promise,a=b(_({$ref:t,$uri:r,$type:n,$id:o}),this.prefix),u=m(a,this.resources);if(u){var s=u.resourceURI,c={resource:u.resource,uri:a},f=g(o);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:v(a,s)}),i.resolve(c)}return i.reject(new Error("URI '"+a+"' is an uninterpretable or unknown Potion resource."))},e}(),z={GET:function(e){return E(e,{method:"GET"})},DELETE:function(e){return E(e,{method:"DELETE"})},POST:function(e){return E(e,{method:"POST"})},PATCH:function(e){return E(e,{method:"PATCH"})},PUT:function(e){return E(e,{method:"PUT"})}},V=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t.__extends(r,e),r.prototype.encodeKey=function(e){return encodeURIComponent(e)},r.prototype.encodeValue=function(e){return encodeURIComponent(JSON.stringify(e))},r}(n.QueryEncoder),W=new r.InjectionToken("PotionResources"),B=new r.InjectionToken("PotionConfig"),K=new r.InjectionToken("PotionHttp"),Q=function(e){function r(r,n,o){var i=e.call(this,t.__assign({},n))||this;return i.http=o||r,i}return t.__extends(r,e),r.prototype.registerFromProvider=function(e){if(e=w.apply(void 0,t.__spread(e.filter(function(e){return!u(e)}))),!u(e))try{for(var r=t.__values(Object.entries(e)),n=r.next();!n.done;n=r.next()){var o=t.__read(n.value,2),i=o[0],a=o[1];if(!this.resources.hasOwnProperty(i))if(Array.isArray(a)){var s=t.__read(a,2),c=s[0],f=s[1];this.register(i,c,f)}else this.register(i,a)}}catch(e){p={error:e}}finally{try{n&&!n.done&&(h=r.return)&&h.call(r)}finally{if(p)throw p.error}}var p,h},r.prototype.request=function(e,r){var o=t.__assign({},r),i=o.search,a=o.data,u=o.method,s=void 0===u?"GET":u,c=new n.RequestOptions({method:s,url:e});if(a){var f=new n.Headers;f.set("Content-Type","application/json; charset=utf-8"),c=c.merge({body:JSON.stringify(a),headers:f})}if(i){var p=new n.URLSearchParams("",new V);try{for(var h=t.__values(Object.entries(i)),d=h.next();!d.done;d=h.next()){var l=t.__read(d.value,2),y=l[0],g=l[1];p.append(y,g)}}catch(e){v={error:e}}finally{try{d&&!d.done&&(m=h.return)&&m.call(h)}finally{if(v)throw v.error}}c=c.merge({search:p})}return this.http.request(e,c).map(function(e){var r,o={};if(e instanceof n.Response){if(e.headers instanceof n.Headers)try{for(var i=t.__values(e.headers.keys()),a=i.next();!a.done;a=i.next()){var u=a.value;o[u.toLowerCase()]=e.headers.get(u)}}catch(e){s={error:e}}finally{try{a&&!a.done&&(c=i.return)&&c.call(i)}finally{if(s)throw s.error}}else o=e.headers||{};r=e.text().length>0?e.json():null}else r=e;return{headers:o,data:r};var s,c}).toPromise();var v,m},r}(L);Q.decorators=[{type:r.Injectable}],Q.ctorParameters=function(){return[{type:n.Http},{type:void 0,decorators:[{type:r.Optional},{type:r.Inject,args:[B]}]},{type:void 0,decorators:[{type:r.Optional},{type:r.Inject,args:[K]}]}]};var Y={provide:Q,useFactory:N,deps:[[new r.Optional,new r.SkipSelf,Q],n.Http,[new r.Optional,new r.Inject(B)],[new r.Optional,new r.Inject(K)]]},Z=function(){function e(e,t){e.registerFromProvider(t||[])}return e}();Z.decorators=[{type:r.NgModule,args:[{imports:[n.HttpModule],providers:[Y]}]}],Z.ctorParameters=function(){return[{type:Q,decorators:[{type:r.Inject,args:[r.forwardRef(function(){return Q})]}]},{type:Array,decorators:[{type:r.Optional},{type:r.Inject,args:[W]}]}]},e.Item=F,e.readonly=R,e.Pagination=C,e.PotionBase=L,e.Route=z,e.route=E,e.findPotionResource=m,e.fromSchemaJSON=p,e.getPotionID=v,e.getPotionURI=_,e.hasTypeAndId=P,e.isPotionURI=O,e.parsePotionID=g,e.removePrefixFromURI=b,e.addPrefixToURI=j,e.toPotionJSON=y,e.toCamelCase=i,e.toSnakeCase=o,e.PotionModule=Z,e.POTION_RESOURCES=W,e.POTION_CONFIG=B,e.POTION_HTTP=K,e.Potion=Q,e.POTION_PROVIDER_FACTORY=N,e.POTION_PROVIDER=Y,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tslib"),require("@angular/core"),require("@angular/http"),require("rxjs/add/operator/map"),require("rxjs/add/operator/toPromise")):"function"==typeof define&&define.amd?define(["exports","tslib","@angular/core","@angular/http","rxjs/add/operator/map","rxjs/add/operator/toPromise"],t):t(e.potionClient=e.potionClient||{},e.tslib,e.ng.core,e.ng.http)}(this,function(e,t,r,n){"use strict";function o(e,t){return void 0===t&&(t="_"),e.replace(/\.?([A-Z0-9]+)/g,function(e,r){return""+t+r.toLowerCase()}).replace(/^_/,"")}function i(e){return e.replace(/_([a-z0-9])/g,function(e){return e[1].toUpperCase()})}function a(e){return"object"==typeof e&&null!==e}function u(e){return 0===Object.keys(e).length}function s(e){return"function"==typeof e}function c(e,r,n){return a(e)&&!Array.isArray(e)?Object.entries(e).map(function(e){var o=t.__read(e,2),i=o[0],a=o[1];return[s(r)?r(i):i,s(n)?n(a):a]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{}):e}function f(e,t){var r="An error occurred while Potion tried to retrieve a resource";return e instanceof Error?e.message:"string"==typeof e?e:"string"==typeof t?r+" from '"+t+"'.":r+"."}function p(e){return Array.isArray(e)?e.map(function(e){return"object"==typeof e?p(e):e}):a(e)?Object.entries(e).map(function(e){var r=t.__read(e,2),n=r[0],o=r[1];return[i(n),"object"==typeof o?p(o):o]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{}):e}function h(e,r){if("object"!=typeof e||null===e)return e;if(q.has(e))return e;if(e instanceof C){var n=e.toArray().map(function(e){return h(e,r)});return e.update(n,e.total)}if(Array.isArray(e))return e.map(function(e){return h(e,r)});if(e instanceof U)return r.get(e.$uri);if(Object.keys(e).length>0){!Array.isArray(e)&&e.uri&&q.add(e);try{for(var o=t.__values(Object.entries(e)),i=o.next();!i.done;i=o.next()){var u=t.__read(i.value,2),s=u[0],c=u[1];if(c instanceof U){var f=r.get(c.$uri);Object.assign(e,(l={},l[s]=f,l))}else a(c)?Object.assign(e,(y={},y[s]=h(c,r),y)):"string"==typeof c&&"#"===c&&Object.assign(e,(g={},g[s]=r.get("#"),g))}}catch(e){p={error:e}}finally{try{i&&!i.done&&(d=o.return)&&d.call(o)}finally{if(p)throw p.error}}return e}return e;var p,d,l,y,g}function d(e,r){var n=new Map;if(a(e)&&Object.keys(e).length>0){if(r||n.set("#",e),q.has(e))return new Map;e.uri&&!n.has(e.uri)&&n.set(e.uri,e);var o=Array.isArray(e)||e instanceof C?e:Object.values(e);try{for(var i=t.__values(o),u=i.next();!u.done;u=i.next()){var s=d(u.value,!0);try{for(var c=t.__values(s.entries()),f=c.next();!f.done;f=c.next()){var p=t.__read(f.value,2),h=p[0],l=p[1];n.set(h,l)}}catch(e){v={error:e}}finally{try{f&&!f.done&&(m=c.return)&&m.call(c)}finally{if(v)throw v.error}}}}catch(e){y={error:e}}finally{try{u&&!u.done&&(g=i.return)&&g.call(i)}finally{if(y)throw y.error}}}return n;var y,g,v,m}function l(e){return new U(e)}function y(e,t){return a(e)?e instanceof F&&"string"==typeof e.uri?{$ref:""+j(e.uri,t)}:e instanceof Date?{$date:e.getTime()}:Array.isArray(e)?e.map(function(e){return y(e,t)}):c(e,function(e){return o(e)},function(e){return y(e,t)}):e}function g(e){return"string"==typeof e&&e.length>0?/^\d+$/.test(e)?parseInt(e,10):e:Number.isInteger(e)?e:null}function v(e,t){var r=e.indexOf(t+"/");return-1!==r?g(e.substring(r).split("/").pop()):null}function m(e,r){var n=Object.entries(r).find(function(r){var n=t.__read(r,1)[0];return 0===e.indexOf(n+"/")});if(n){var o=t.__read(n,2);return{resourceURI:o[0],resource:o[1]}}}function O(e,t){var r=m(e,t);return!!r&&null!==v(e,r.resourceURI)}function P(e){var t=e.$type,r=e.$id;return("string"==typeof r||Number.isInteger(r))&&"string"==typeof t}function b(e){var t=e.$uri,r=e.$ref,n=e.$type,o=e.$id;return"string"==typeof t?decodeURIComponent(t):"string"==typeof r?decodeURIComponent(r):P({$type:n,$id:o})?"/"+n+"/"+o:""}function _(e,t){return e.includes(t)?e.substring(t.length):e}function j(e,t){return"string"!=typeof t||e.includes(t)?e:""+t+e}function w(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var n={};try{for(var o=t.__values(e),i=o.next();!i.done;i=o.next()){var a=i.value;Object.assign(n,a)}}catch(e){u={error:e}}finally{try{i&&!i.done&&(s=o.return)&&s.call(o)}finally{if(u)throw u.error}}return n;var u,s}function $(e){return J.getOwnMetadata(k,e)}function I(e,t){J.defineMetadata(k,t,e)}function T(e){return J.getOwnMetadata(H,e)}function x(e,t){J.defineMetadata(H,t,e)}function S(e){return J.getOwnMetadata(D,e.constructor)||Promise}function A(e,t){var r=J.getOwnMetadata(G,e);return r&&r[t]}function R(e,t){var r=s(e)?e:s(e.constructor)?e.constructor:null;if(null!==r){J.defineMetadata(G,Object.assign(J.getOwnMetadata(G,r)||{},(n={},n[t]=!0,n)),r);var n}}function E(e,t){var r=(void 0===t?{}:t).method;return function(t,n){var o=void 0===n?{}:n,i=o.paginate,a=void 0!==i&&i,u=o.cache,c=void 0===u||u,f=s(this),p=""+(f?T(this):this.uri)+e,h={method:r,paginate:a,cache:c};return"GET"===r?h.search=t:["POST","PUT","PATCH"].includes(r)&&(h.data=t),$(f?this:this.constructor).fetch(p,h)}}function N(e,t,r,n){return e||new Q(t,r,n)}var C=function(){function e(e,r,n,o){var i=e.potion,a=e.uri;this.items=r,this.options=o,this.potion=i,this.uri=a;var u=t.__assign({},this.options.search),s=u.page,c=void 0===s?1:s,f=u.perPage,p=void 0===f?25:f;this.$page=c,this.$perPage=p,this.$total=parseInt(n,10)}return Object.defineProperty(e,Symbol.species,{get:function(){return e},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"page",{get:function(){return this.$page},set:function(e){this.changePageTo(e)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"perPage",{get:function(){return this.$perPage},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"pages",{get:function(){return Math.ceil(this.$total/this.$perPage)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"total",{get:function(){return this.$total},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.items.length},enumerable:!0,configurable:!0}),e.prototype[Symbol.iterator]=function(){return this.items.values()},e.prototype.toArray=function(){return this.items.slice(0)},e.prototype.at=function(e){return this.items[e]},e.prototype.changePageTo=function(e){var t=this.options.pagination;return this.options.search.page=e,this.$page=e,this.potion.fetch(this.uri,this.options,{pagination:t})},e.prototype.update=function(e,r){return(n=this.items).splice.apply(n,t.__spread([0,this.items.length],e)),this.$total=r,this;var n},e}(),U=function(){function e(e){this.$uri=e}return e.prototype.matches=function(e){return a(e)&&this.$uri===e.uri},e}(),q=new WeakSet,M=function(){function e(){this.items=new Map}return e.prototype.has=function(e){return this.items.has(e)},e.prototype.get=function(e){return this.items.get(e)},e.prototype.put=function(e,t){return this.items.set(e,t).get(e)},e.prototype.remove=function(e){this.items.delete(e)},e}(),J=function(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}().Reflect;!function(){if(!J||!J.getMetadata)throw new Error("Dependency error. reflect-metadata shim is required when using potion-node library")}();var k=Symbol("potion"),H=Symbol("potion:uri"),D=Symbol("potion:promise"),G=Symbol("potion:readonly"),F=function(){function e(e){void 0===e&&(e={}),this.$id=null,Object.assign(this,e)}return e.fetch=function(e,t){var r=(void 0===t?{}:t).cache,n=void 0===r||r,o=T(this);return $(this).fetch(o+"/"+e,{method:"GET",cache:n})},e.query=function(e,t){var r=void 0===t?{}:t,n=r.paginate,o=void 0!==n&&n,i=r.cache,a=void 0===i||i,u=T(this);return $(this).fetch(u,{method:"GET",search:e,paginate:o,cache:a})},e.first=function(e){function t(e){return e[0]}return this.query(e).then(t)},Object.defineProperty(e.prototype,"uri",{get:function(){return this.$uri},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"id",{get:function(){return this.$id},enumerable:!0,configurable:!0}),e.prototype.equals=function(t){return t instanceof e&&(this.id===t.id&&this.constructor.name===t.constructor.name)},e.prototype.toJSON=function(){var e=this,t={};return Object.keys(this).filter(function(t){return!t.startsWith("$")&&!A(e.constructor,t)}).forEach(function(r){t[r]=e[r]}),t},e.prototype.save=function(){if(this.uri||this.id)return this.update(this.toJSON());var e=this.constructor;return $(e).fetch(T(e),{method:"POST",data:this.toJSON(),cache:!0})},e.prototype.update=function(e){return void 0===e&&(e={}),$(this.constructor).fetch(this.uri,{cache:!0,method:"PATCH",data:e})},e.prototype.destroy=function(){function e(){n.get(t)&&n.remove(t)}var t=this.uri,r=$(this.constructor),n=r.cache;return r.fetch(t,{method:"DELETE"}).then(e)},e}(),L=function(){function e(e){var t=void 0===e?{}:e,r=t.host,n=void 0===r?"":r,o=t.prefix,i=void 0===o?"":o,a=t.cache;this.resources={},this.Promise=S(this),this.requests=new Map,this.cache=a||new M,this.host=n,this.prefix=i}return e.prototype.register=function(e,t,r){if(!s(t))throw new TypeError("An error occurred while trying to register a resource for "+e+". "+t+" is not a function.");return I(t,this),x(t,e),r&&Array.isArray(r.readonly)&&r.readonly.forEach(function(e){return R(t,e)}),this.resources[e]=t,t},e.prototype.registerAs=function(e,t){var r=this;return function(n){return r.register(e,n,t),n}},e.prototype.request=function(e,t){},e.prototype.fetch=function(e,r,n){var o=_(e,this.prefix),i=t.__assign({},r,n,{origin:[]});return O(e,this.resources)&&Object.assign(i,{origin:[o]}),this.resolve(e,i).then(function(e){return h(e,d(e)),e})},e.prototype.resolve=function(e,t){var r=this,n=this,o=n.Promise,i=n.prefix,a=_(e,i);e=j(e,i);var u=function(){return r.request(""+r.host+e,r.serialize(t)).then(function(n){return r.deserialize(n,e,t)})};return"GET"!==t.method||t.paginate||t.search?u():t.cache&&this.cache.has(a)?this.cache.get(a):(this.requests.has(a)||this.requests.set(a,u().then(function(e){return r.requests.delete(a),e},function(t){r.requests.delete(a);var n=f(t,e);return o.reject(n)})),this.requests.get(a))},e.prototype.serialize=function(e){var r=this.prefix,n=e.search;return t.__assign({},e,{search:y(e.paginate?t.__assign({page:1,perPage:25},n):n,r),data:y(e.data,r)})},e.prototype.deserialize=function(e,t,r){var n=this,o=e.data,i=e.headers;return this.fromPotionJSON(o,r.origin).then(function(e){if(r.paginate){var o=i["x-total-count"]||e.length;if(r.pagination instanceof C)return r.pagination.update(e,o);var a=new C({uri:t,potion:n},e,o,r);return Object.assign(r,{pagination:a}),a}return e})},e.prototype.fromPotionJSON=function(e,r){var n=this,o=this.Promise;if("object"==typeof e&&null!==e){if(Array.isArray(e))return o.all(e.map(function(e){return n.fromPotionJSON(e,r)}));if("string"==typeof e.$uri||P(e))return this.parseURI(e).then(function(i){var a=i.resource,u=i.id,s=i.uri,c={$id:u,$uri:s};r.includes(s)||r.push(s);var f=n.parsePotionJSONProperties(e,r);return n.cache.has(s)?o.all([f,n.cache.get(s)]).then(function(e){var r=t.__read(e,2),n=r[0],o=r[1];return Object.assign(o,n,c),o}):n.cache.put(s,f.then(function(e){return Reflect.construct(a,[t.__assign({},e,c)])}))});if("string"==typeof e.$schema)return o.resolve(p(e));if(1===Object.keys(e).length){if("string"==typeof e.$ref)return"#"===e.$ref?o.resolve(e.$ref):this.parseURI(e).then(function(e){var t=e.uri;return r.includes(t)?o.resolve(l(t)):n.resolve(t,{cache:!0,method:"GET",origin:r})});if(void 0!==e.$date)return o.resolve(new Date(e.$date))}return this.parsePotionJSONProperties(e,r)}return o.resolve(e)},e.prototype.parsePotionJSONProperties=function(e,r){var n=this,o=this.Promise,a=Object.entries(e),u=a.map(function(e){var o=t.__read(e,2)[1];return n.fromPotionJSON(o,r)}),s=a.map(function(e){return i(t.__read(e,1)[0])});return o.all(u).then(function(e){return e.map(function(e,t){return[s[t],e]}).reduce(function(e,r){var n=t.__read(r,2),o=n[0],i=n[1];return Object.assign(e,(a={},a[o]=i,a));var a},{})})},e.prototype.parseURI=function(e){var t=e.$ref,r=e.$uri,n=e.$type,o=e.$id,i=this.Promise,a=_(b({$ref:t,$uri:r,$type:n,$id:o}),this.prefix),u=m(a,this.resources);if(u){var s=u.resourceURI,c={resource:u.resource,uri:a},f=g(o);return null!==f?Object.assign(c,{id:f}):Object.assign(c,{id:v(a,s)}),i.resolve(c)}return i.reject(new Error("URI '"+a+"' is an uninterpretable or unknown Potion resource."))},e}(),z={GET:function(e){return E(e,{method:"GET"})},DELETE:function(e){return E(e,{method:"DELETE"})},POST:function(e){return E(e,{method:"POST"})},PATCH:function(e){return E(e,{method:"PATCH"})},PUT:function(e){return E(e,{method:"PUT"})}},V=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t.__extends(r,e),r.prototype.encodeKey=function(e){return encodeURIComponent(e)},r.prototype.encodeValue=function(e){return encodeURIComponent(JSON.stringify(e))},r}(n.QueryEncoder),W=new r.InjectionToken("PotionResources"),B=new r.InjectionToken("PotionConfig"),K=new r.InjectionToken("PotionHttp"),Q=function(e){function r(r,n,o){var i=e.call(this,t.__assign({},n))||this;return i.http=o||r,i}return t.__extends(r,e),r.prototype.registerFromProvider=function(e){if(e=w.apply(void 0,t.__spread(e.filter(function(e){return!u(e)}))),!u(e))try{for(var r=t.__values(Object.entries(e)),n=r.next();!n.done;n=r.next()){var o=t.__read(n.value,2),i=o[0],a=o[1];if(!this.resources.hasOwnProperty(i))if(Array.isArray(a)){var s=t.__read(a,2),c=s[0],f=s[1];this.register(i,c,f)}else this.register(i,a)}}catch(e){p={error:e}}finally{try{n&&!n.done&&(h=r.return)&&h.call(r)}finally{if(p)throw p.error}}var p,h},r.prototype.request=function(e,r){var o=t.__assign({},r),i=o.search,a=o.data,u=o.method,s=void 0===u?"GET":u,c=new n.RequestOptions({method:s,url:e});if(a){var f=new n.Headers;f.set("Content-Type","application/json; charset=utf-8"),c=c.merge({body:JSON.stringify(a),headers:f})}if(i){var p=new n.URLSearchParams("",new V);try{for(var h=t.__values(Object.entries(i)),d=h.next();!d.done;d=h.next()){var l=t.__read(d.value,2),y=l[0],g=l[1];p.append(y,g)}}catch(e){v={error:e}}finally{try{d&&!d.done&&(m=h.return)&&m.call(h)}finally{if(v)throw v.error}}c=c.merge({search:p})}return this.http.request(e,c).map(function(e){var r,o={};if(e instanceof n.Response){if(e.headers instanceof n.Headers)try{for(var i=t.__values(e.headers.keys()),a=i.next();!a.done;a=i.next()){var u=a.value;o[u.toLowerCase()]=e.headers.get(u)}}catch(e){s={error:e}}finally{try{a&&!a.done&&(c=i.return)&&c.call(i)}finally{if(s)throw s.error}}else o=e.headers||{};r=e.text().length>0?e.json():null}else r=e;return{headers:o,data:r};var s,c}).toPromise();var v,m},r}(L);Q.decorators=[{type:r.Injectable}],Q.ctorParameters=function(){return[{type:n.Http},{type:void 0,decorators:[{type:r.Optional},{type:r.Inject,args:[B]}]},{type:void 0,decorators:[{type:r.Optional},{type:r.Inject,args:[K]}]}]};var Y={provide:Q,useFactory:N,deps:[[new r.Optional,new r.SkipSelf,Q],n.Http,[new r.Optional,new r.Inject(B)],[new r.Optional,new r.Inject(K)]]},Z=function(){function e(e,t){e.registerFromProvider(t||[])}return e}();Z.decorators=[{type:r.NgModule,args:[{imports:[n.HttpModule],providers:[Y]}]}],Z.ctorParameters=function(){return[{type:Q,decorators:[{type:r.Inject,args:[r.forwardRef(function(){return Q})]}]},{type:Array,decorators:[{type:r.Optional},{type:r.Inject,args:[W]}]}]},e.Item=F,e.readonly=R,e.Pagination=C,e.PotionBase=L,e.Route=z,e.route=E,e.findPotionResource=m,e.fromSchemaJSON=p,e.getPotionID=v,e.getPotionURI=b,e.hasTypeAndId=P,e.isPotionURI=O,e.parsePotionID=g,e.removePrefixFromURI=_,e.addPrefixToURI=j,e.toPotionJSON=y,e.toCamelCase=i,e.toSnakeCase=o,e.PotionModule=Z,e.POTION_RESOURCES=W,e.POTION_CONFIG=B,e.POTION_HTTP=K,e.Potion=Q,e.POTION_PROVIDER_FACTORY=N,e.POTION_PROVIDER=Y,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=ng.umd.min.js.map |
{ | ||
"name": "potion-client", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"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
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
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
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
1230096
11509