@rest-hooks/normalizr
Advanced tools
Comparing version 6.0.0-beta.6 to 6.0.0-beta.7
@@ -6,2 +6,18 @@ # Change Log | ||
## [6.0.0-beta.7](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@6.0.0-beta.6...@rest-hooks/normalizr@6.0.0-beta.7) (2020-05-19) | ||
### 💅 Enhancement | ||
* Better dev error messaging with unexpected string to normalize ([#342](https://github.com/coinbase/rest-hooks/issues/342)) ([efc1b60](https://github.com/coinbase/rest-hooks/commit/efc1b609dc6e3b61b48f69a6bcb51c268cd39ef1)) | ||
### 🏠 Internal | ||
* Add Entity tests for handling of getters ([8d1e552](https://github.com/coinbase/rest-hooks/commit/8d1e55269a624a6dc38cc90e0c257d720a81d3c2)) | ||
* Add test for taking string response when expecting entity ([dfb2625](https://github.com/coinbase/rest-hooks/commit/dfb26259f47a21c7d95e9edc38a73e5db1367158)) | ||
* Add test: methods are not expected in Entities ([d7c26e6](https://github.com/coinbase/rest-hooks/commit/d7c26e6a0c40b4fd059c496670f9dff394fc6c62)) | ||
## [6.0.0-beta.6](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@6.0.0-beta.5...@rest-hooks/normalizr@6.0.0-beta.6) (2020-05-14) | ||
@@ -8,0 +24,0 @@ |
@@ -625,3 +625,30 @@ define(['exports'], function (exports) { 'use strict'; | ||
if (input === null || typeof input !== schemaType) { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const parseWorks = input => { | ||
try { | ||
return typeof JSON.parse(input) !== 'string'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
if (typeof input === 'string' && parseWorks(input)) { | ||
throw new Error(`Normalizing a string, but this does match schema. | ||
Parsing this input string as JSON worked. This likely indicates fetch function did not parse | ||
the JSON. By default, this only happens if "content-type" header includes "json". | ||
See https://resthooks.io/docs/guides/custom-networking for more information | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}". | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
} | ||
} | ||
@@ -628,0 +655,0 @@ |
@@ -1,1 +0,1 @@ | ||
define(["exports"],(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class f extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(f.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class p extends f{static denormalize(e,t){return[e,!0]}}const y=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof p?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},O=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,O,s,i)}return r.normalize(e,t,n,O,s,i)};var g=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});e.Entity=f,e.FlatEntity=p,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=y(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=d,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`);const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:O(e,e,void 0,t,i,{})}},e.schema=g,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
define(["exports"],(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class d extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function f(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(d.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class p extends d{static denormalize(e,t){return[e,!0]}}const y=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:f(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof p?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},g=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,g,s,i)}return r.normalize(e,t,n,g,s,i)};var O=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});e.Entity=d,e.FlatEntity=p,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=y(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=f,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n){if("production"!==process.env.NODE_ENV){const r=e=>{try{return"string"!=typeof JSON.parse(e)}catch(e){return!1}};throw"string"==typeof e&&r(e)?new Error(`Normalizing a string, but this does match schema.\n\nParsing this input string as JSON worked. This likely indicates fetch function did not parse\nthe JSON. By default, this only happens if "content-type" header includes "json".\nSee https://resthooks.io/docs/guides/custom-networking for more information\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`):new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`)}throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`)}const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}},e.schema=O,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -626,3 +626,30 @@ var rest_hooks_normalizr = (function (exports) { | ||
if (input === null || typeof input !== schemaType) { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const parseWorks = input => { | ||
try { | ||
return typeof JSON.parse(input) !== 'string'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
if (typeof input === 'string' && parseWorks(input)) { | ||
throw new Error(`Normalizing a string, but this does match schema. | ||
Parsing this input string as JSON worked. This likely indicates fetch function did not parse | ||
the JSON. By default, this only happens if "content-type" header includes "json". | ||
See https://resthooks.io/docs/guides/custom-networking for more information | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}". | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
} | ||
} | ||
@@ -629,0 +656,0 @@ |
@@ -1,1 +0,1 @@ | ||
var rest_hooks_normalizr=function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class f extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(f.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class y extends f{static denormalize(e,t){return[e,!0]}}const p=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof y?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},g=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,g,s,i)}return r.normalize(e,t,n,g,s,i)};var O=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});return e.Entity=f,e.FlatEntity=y,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=p(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=d,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`);const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}},e.schema=O,e}({}); | ||
var rest_hooks_normalizr=function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class d extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function f(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(d.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class p extends d{static denormalize(e,t){return[e,!0]}}const y=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:f(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof p?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},g=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,g,s,i)}return r.normalize(e,t,n,g,s,i)};var O=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});return e.Entity=d,e.FlatEntity=p,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=y(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=f,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n){if("production"!==process.env.NODE_ENV){const r=e=>{try{return"string"!=typeof JSON.parse(e)}catch(e){return!1}};throw"string"==typeof e&&r(e)?new Error(`Normalizing a string, but this does match schema.\n\nParsing this input string as JSON worked. This likely indicates fetch function did not parse\nthe JSON. By default, this only happens if "content-type" header includes "json".\nSee https://resthooks.io/docs/guides/custom-networking for more information\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`):new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`)}throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`)}const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}},e.schema=O,e}({}); |
@@ -621,3 +621,30 @@ /** | ||
if (input === null || typeof input !== schemaType) { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const parseWorks = input => { | ||
try { | ||
return typeof JSON.parse(input) !== 'string'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
if (typeof input === 'string' && parseWorks(input)) { | ||
throw new Error(`Normalizing a string, but this does match schema. | ||
Parsing this input string as JSON worked. This likely indicates fetch function did not parse | ||
the JSON. By default, this only happens if "content-type" header includes "json". | ||
See https://resthooks.io/docs/guides/custom-networking for more information | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}". | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
} | ||
} | ||
@@ -624,0 +651,0 @@ |
@@ -1,1 +0,1 @@ | ||
function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(t,n){const r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):[t,!0]}}const r=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),i=(e,t,n,i,o,c,a)=>(e=r(e),s(t).map((t,r)=>o(t,n,i,e,c,a))),o=(e,t,n)=>{e=r(e);let s=!0;return void 0===t&&e&&([,s]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,s]};const c=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},a=(n,r,s)=>{if(e(r))return t(n,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(n).forEach(e=>{const[t,r]=s(i[e],n[e]);void 0!==i[e]&&(i[e]=t),r||(o=!1)}),[i,o]};const u=Symbol("Defined Members"),h=Symbol("unq");class l{toString(){return this[h]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,u,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,h,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[u].includes(t)}static toObjectDefined(e){const t={};for(const n of e[u])t[n]=e[n];return t}static keysDefined(e){return e[u]}static normalize(...e){return c(this.schema,...e)}static denormalize(...e){const[t,n]=a(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}l.schema={};class m extends l{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(n,r){if(e(n)){const[e,s]=t(this.schema,n,r);return[this.fromJS(e.toObject()),s]}let s=!0;const i=n;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[o,c]=r(n[e],t);c||(s=!1),this.hasDefined(n,e)&&i[e]!==o&&(i[e]=o)}),[i,s]}}function f(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(m.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return l.fromJS.call(this,e)});class d extends m{static denormalize(e,t){return[e,!0]}}const p=t=>{const n={},r=y(t);return[function t(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?o:a)(i,s,t)}return null===s?[s,!0]:f(i)?void 0===s?[s,!1]:((t,n,r,s,i)=>{const o=s(t,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][t]){const s=e(o)||o instanceof d?o:n.fromJS(o);i[n.key][t]=s,[i[n.key][t],c]=n.denormalize(s,r)}return[i[n.key][t],c]})(s,i,t,r,n):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]},n]},y=t=>{const n=e(t);return(e,r)=>{const s=r.key;return"object"==typeof e?e:n?t.getIn([s,e]):t[s]&&t[s][e]}},b=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=p(n);return[...r(e,t),s]}return[void 0,!1,{}]},g=(e,t,n,r,s,o)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?i:c)(r,e,t,n,g,s,o)}return r.normalize(e,t,n,g,s,o)};const O=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`);const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}};var j=Object.freeze({__proto__:null,Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends n{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}}});export{m as Entity,d as FlatEntity,l as SimpleRecord,b as denormalize,f as isEntity,O as normalize,j as schema}; | ||
function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(t,n){const r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):[t,!0]}}const r=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),i=(e,t,n,i,o,c,a)=>(e=r(e),s(t).map((t,r)=>o(t,n,i,e,c,a))),o=(e,t,n)=>{e=r(e);let s=!0;return void 0===t&&e&&([,s]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,s]};const c=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},a=(n,r,s)=>{if(e(r))return t(n,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(n).forEach(e=>{const[t,r]=s(i[e],n[e]);void 0!==i[e]&&(i[e]=t),r||(o=!1)}),[i,o]};const u=Symbol("Defined Members"),h=Symbol("unq");class l{toString(){return this[h]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,u,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,h,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[u].includes(t)}static toObjectDefined(e){const t={};for(const n of e[u])t[n]=e[n];return t}static keysDefined(e){return e[u]}static normalize(...e){return c(this.schema,...e)}static denormalize(...e){const[t,n]=a(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}l.schema={};class m extends l{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(n,r){if(e(n)){const[e,s]=t(this.schema,n,r);return[this.fromJS(e.toObject()),s]}let s=!0;const i=n;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[o,c]=r(n[e],t);c||(s=!1),this.hasDefined(n,e)&&i[e]!==o&&(i[e]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(m.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return l.fromJS.call(this,e)});class f extends m{static denormalize(e,t){return[e,!0]}}const p=t=>{const n={},r=y(t);return[function t(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?o:a)(i,s,t)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((t,n,r,s,i)=>{const o=s(t,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][t]){const s=e(o)||o instanceof f?o:n.fromJS(o);i[n.key][t]=s,[i[n.key][t],c]=n.denormalize(s,r)}return[i[n.key][t],c]})(s,i,t,r,n):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]},n]},y=t=>{const n=e(t);return(e,r)=>{const s=r.key;return"object"==typeof e?e:n?t.getIn([s,e]):t[s]&&t[s][e]}},b=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=p(n);return[...r(e,t),s]}return[void 0,!1,{}]},g=(e,t,n,r,s,o)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?i:c)(r,e,t,n,g,s,o)}return r.normalize(e,t,n,g,s,o)};const O=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n){if("production"!==process.env.NODE_ENV){const r=e=>{try{return"string"!=typeof JSON.parse(e)}catch(e){return!1}};throw"string"==typeof e&&r(e)?new Error(`Normalizing a string, but this does match schema.\n\nParsing this input string as JSON worked. This likely indicates fetch function did not parse\nthe JSON. By default, this only happens if "content-type" header includes "json".\nSee https://resthooks.io/docs/guides/custom-networking for more information\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`):new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`)}throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`)}const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}};var S=Object.freeze({__proto__:null,Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends n{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}}});export{m as Entity,f as FlatEntity,l as SimpleRecord,b as denormalize,d as isEntity,O as normalize,S as schema}; |
@@ -627,3 +627,30 @@ 'use strict'; | ||
if (input === null || typeof input !== schemaType) { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const parseWorks = input => { | ||
try { | ||
return typeof JSON.parse(input) !== 'string'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
if (typeof input === 'string' && parseWorks(input)) { | ||
throw new Error(`Normalizing a string, but this does match schema. | ||
Parsing this input string as JSON worked. This likely indicates fetch function did not parse | ||
the JSON. By default, this only happens if "content-type" header includes "json". | ||
See https://resthooks.io/docs/guides/custom-networking for more information | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}". | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
} | ||
} | ||
@@ -630,0 +657,0 @@ |
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}Object.defineProperty(exports,"__esModule",{value:!0});class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(t,n){const r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):[t,!0]}}const r=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),i=(e,t,n,i,o,c,a)=>(e=r(e),s(t).map((t,r)=>o(t,n,i,e,c,a))),o=(e,t,n)=>{e=r(e);let s=!0;return void 0===t&&e&&([,s]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,s]};const c=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},a=(n,r,s)=>{if(e(r))return t(n,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(n).forEach(e=>{const[t,r]=s(i[e],n[e]);void 0!==i[e]&&(i[e]=t),r||(o=!1)}),[i,o]};const u=Symbol("Defined Members"),h=Symbol("unq");class l{toString(){return this[h]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,u,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,h,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[u].includes(t)}static toObjectDefined(e){const t={};for(const n of e[u])t[n]=e[n];return t}static keysDefined(e){return e[u]}static normalize(...e){return c(this.schema,...e)}static denormalize(...e){const[t,n]=a(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}l.schema={};class m extends l{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(n,r){if(e(n)){const[e,s]=t(this.schema,n,r);return[this.fromJS(e.toObject()),s]}let s=!0;const i=n;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[o,c]=r(n[e],t);c||(s=!1),this.hasDefined(n,e)&&i[e]!==o&&(i[e]=o)}),[i,s]}}function f(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(m.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return l.fromJS.call(this,e)});class d extends m{static denormalize(e,t){return[e,!0]}}const p=t=>{const n={},r=y(t);return[function t(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?o:a)(i,s,t)}return null===s?[s,!0]:f(i)?void 0===s?[s,!1]:((t,n,r,s,i)=>{const o=s(t,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][t]){const s=e(o)||o instanceof d?o:n.fromJS(o);i[n.key][t]=s,[i[n.key][t],c]=n.denormalize(s,r)}return[i[n.key][t],c]})(s,i,t,r,n):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]},n]},y=t=>{const n=e(t);return(e,r)=>{const s=r.key;return"object"==typeof e?e:n?t.getIn([s,e]):t[s]&&t[s][e]}},b=(e,t,n,r,s,o)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?i:c)(r,e,t,n,b,s,o)}return r.normalize(e,t,n,b,s,o)};var O=Object.freeze({__proto__:null,Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends n{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}}});exports.Entity=m,exports.FlatEntity=d,exports.SimpleRecord=l,exports.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=p(n);return[...r(e,t),s]}return[void 0,!1,{}]},exports.isEntity=f,exports.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`);const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:b(e,e,void 0,t,i,{})}},exports.schema=O; | ||
"use strict";function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}Object.defineProperty(exports,"__esModule",{value:!0});class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(t,n){const r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):[t,!0]}}const r=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),i=(e,t,n,i,o,c,a)=>(e=r(e),s(t).map((t,r)=>o(t,n,i,e,c,a))),o=(e,t,n)=>{e=r(e);let s=!0;return void 0===t&&e&&([,s]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,s]};const c=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},a=(n,r,s)=>{if(e(r))return t(n,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(n).forEach(e=>{const[t,r]=s(i[e],n[e]);void 0!==i[e]&&(i[e]=t),r||(o=!1)}),[i,o]};const u=Symbol("Defined Members"),h=Symbol("unq");class l{toString(){return this[h]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,u,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,h,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[u].includes(t)}static toObjectDefined(e){const t={};for(const n of e[u])t[n]=e[n];return t}static keysDefined(e){return e[u]}static normalize(...e){return c(this.schema,...e)}static denormalize(...e){const[t,n]=a(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}l.schema={};class m extends l{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(n,r){if(e(n)){const[e,s]=t(this.schema,n,r);return[this.fromJS(e.toObject()),s]}let s=!0;const i=n;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[o,c]=r(n[e],t);c||(s=!1),this.hasDefined(n,e)&&i[e]!==o&&(i[e]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(m.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return l.fromJS.call(this,e)});class f extends m{static denormalize(e,t){return[e,!0]}}const p=t=>{const n={},r=y(t);return[function t(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?o:a)(i,s,t)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((t,n,r,s,i)=>{const o=s(t,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][t]){const s=e(o)||o instanceof f?o:n.fromJS(o);i[n.key][t]=s,[i[n.key][t],c]=n.denormalize(s,r)}return[i[n.key][t],c]})(s,i,t,r,n):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]},n]},y=t=>{const n=e(t);return(e,r)=>{const s=r.key;return"object"==typeof e?e:n?t.getIn([s,e]):t[s]&&t[s][e]}},b=(e,t,n,r,s,o)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?i:c)(r,e,t,n,b,s,o)}return r.normalize(e,t,n,b,s,o)};var g=Object.freeze({__proto__:null,Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends n{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}}});exports.Entity=m,exports.FlatEntity=f,exports.SimpleRecord=l,exports.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=p(n);return[...r(e,t),s]}return[void 0,!1,{}]},exports.isEntity=d,exports.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n){if("production"!==process.env.NODE_ENV){const r=e=>{try{return"string"!=typeof JSON.parse(e)}catch(e){return!1}};throw"string"==typeof e&&r(e)?new Error(`Normalizing a string, but this does match schema.\n\nParsing this input string as JSON worked. This likely indicates fetch function did not parse\nthe JSON. By default, this only happens if "content-type" header includes "json".\nSee https://resthooks.io/docs/guides/custom-networking for more information\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`):new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`)}throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`)}const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:b(e,e,void 0,t,i,{})}},exports.schema=g; |
@@ -629,3 +629,30 @@ (function (global, factory) { | ||
if (input === null || typeof input !== schemaType) { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const parseWorks = input => { | ||
try { | ||
return typeof JSON.parse(input) !== 'string'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
if (typeof input === 'string' && parseWorks(input)) { | ||
throw new Error(`Normalizing a string, but this does match schema. | ||
Parsing this input string as JSON worked. This likely indicates fetch function did not parse | ||
the JSON. By default, this only happens if "content-type" header includes "json". | ||
See https://resthooks.io/docs/guides/custom-networking for more information | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}". | ||
Schema: ${JSON.stringify(schema, undefined, 2)} | ||
Input: "${input}"`); | ||
} | ||
} else { | ||
throw new Error(`Unexpected input given to normalize. Expected type to be "${schemaType}", found "${input === null ? 'null' : typeof input}".`); | ||
} | ||
} | ||
@@ -632,0 +659,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).rest_hooks_normalizr={})}(this,(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class f extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(f.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class p extends f{static denormalize(e,t){return[e,!0]}}const y=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof p?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},O=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,O,s,i)}return r.normalize(e,t,n,O,s,i)};var g=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});e.Entity=f,e.FlatEntity=p,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=y(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=d,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`);const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:O(e,e,void 0,t,i,{})}},e.schema=g,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).rest_hooks_normalizr={})}(this,(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,s)=>{const i=`${s}`,[o,c]=n(t.get(i),e[i]);return c||(r=!1),t.has(i)?t.set(i,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,s,i){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,s,i);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const s=e=>{if(Array.isArray(e)&&e.length>1)throw new Error(`Expected schema definition to be a single schema, but found ${e.length}.`);return e[0]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=s(e),i(t).map((t,s)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=s(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=s(t[n],t,n,r,i,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,s)=>{if(t(r))return n(e,r,s);const i=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=s(i[t],e[t]);void 0!==i[t]&&(i[t]=n),r||(o=!1)}),[i,o]};const h=Symbol("Defined Members"),l=Symbol("unq");class m{toString(){return this[l]}static fromJS(e={},t,n){const r=new this(e);return Object.assign(r,e),Object.defineProperty(r,h,{value:Object.keys(e),writable:!1}),Object.defineProperty(r,l,{value:`${Math.random()}`,writable:!1}),r}static merge(e,t){const n=Object.assign(this.toObjectDefined(e),this.toObjectDefined(t));return this.fromJS(n)}static hasDefined(e,t){return e[h].includes(t)}static toObjectDefined(e){const t={};for(const n of e[h])t[n]=e[n];return t}static keysDefined(e){return e[h]}static normalize(...e){return a(this.schema,...e)}static denormalize(...e){const[t,n]=u(this.schema,...e);return[this.fromJS(t),n]}static asSchema(){return"development"===process.env.NODE_ENV&&console.error("asSchema() is deprecated - use Entity directly instead."),this}}m.schema={};class f extends m{static get key(){if("production"!==process.env.NODE_ENV&&""===this.name)throw new Error("Entity classes without a name must define `static get key()`");return this.name}static pk(e,t,n){return this.prototype.pk.call(e,t,n)||n}static normalize(e,t,n,r,s,i){const o=this.fromJS(e,t,n);if("production"!==process.env.NODE_ENV){const e=new this,t=new Set(Object.keys(e)),n=this.keysDefined(o),[r,s,i]=[[],[],[]];for(const e of n)t.has(e)?r.push(e):i.push(e);for(const e of t)r.includes(e)||s.push(e);if((Math.max(n.length/2,1)<=i.length&&t.size>Math.max(i.length,2)||r.length<Math.min(1,t.size/2))&&t.size){const e=new Error(`Attempted to initialize ${this.name} with substantially different than expected keys\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Expected keys:\n Found: ${r}\n Missing: ${s}\n Unexpected keys: ${i}\n Value: ${JSON.stringify(this.toObjectDefined(o),null,2)}`);throw e.status=400,e}}const c=o.pk(t,n);if(void 0===c||""===c){if("production"!==process.env.NODE_ENV){const t=new Error(`Missing usable resource key when normalizing response.\n\n This is likely due to a malformed response.\n Try inspecting the network response or fetch() return value.\n\n Entity: ${this.name}\n Value: ${e&&JSON.stringify(e,null,2)}\n `);throw t.status=400,t}return}const a=this.key;return a in i||(i[a]={}),c in i[a]||(i[a][c]=[]),i[a][c].some(t=>t===e)||(i[a][c].push(e),Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(o,e)&&"object"==typeof o[e]){const t=this.schema[e];o[e]=r(o[e],o,e,t,s,i)}}),s(this,o,o,t,n)),c}static denormalize(e,r){if(t(e)){const[t,s]=n(this.schema,e,r);return[this.fromJS(t.toObject()),s]}let s=!0;const i=e;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[o,c]=r(e[t],n);c||(s=!1),this.hasDefined(e,t)&&i[t]!==o&&(i[t]=o)}),[i,s]}}function d(e){return null!==e&&void 0!==e.pk}"production"!==process.env.NODE_ENV&&(f.fromJS=function(e){if(void 0===this.prototype.pk)throw new Error("cannot construct on abstract types");return m.fromJS.call(this,e)});class p extends f{static denormalize(e,t){return[e,!0]}}const y=e=>{const n={},r=b(e);return[function e(s,i){if(!i)return[s,!0];if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,e)}return null===s?[s,!0]:d(i)?void 0===s?[s,!1]:((e,n,r,s,i)=>{const o=s(e,n);if("object"!=typeof o||null===o)return[o,!1];i[n.key]||(i[n.key]={});let c=!0;if(!i[n.key][e]){const s=t(o)||o instanceof p?o:n.fromJS(o);i[n.key][e]=s,[i[n.key][e],c]=n.denormalize(s,r)}return[i[n.key][e],c]})(s,i,e,r,n):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]},n]},b=e=>{const n=t(e);return(t,r)=>{const s=r.key;return"object"==typeof t?t:n?e.getIn([s,t]):e[s]&&e[s][t]}},g=(e,t,n,r,s,i)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,g,s,i)}return r.normalize(e,t,n,g,s,i)};var O=Object.freeze({__proto__:null,Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,s,i){return this.normalizeValue(e,t,n,r,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,s,i){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign(Object.assign({},t),{},{[n]:this.normalizeValue(c,e,n,r,s,i)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(n=!1),Object.assign(Object.assign({},r),{},{[s]:o})},{}),n]}},Array:class extends r{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign(Object.assign({},t),{},{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}}});e.Entity=f,e.FlatEntity=p,e.SimpleRecord=m,e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("schema needed");if(void 0!==e){const[r,s]=y(n);return[...r(e,t),s]}return[void 0,!1,{}]},e.isEntity=d,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n){if("production"!==process.env.NODE_ENV){const r=e=>{try{return"string"!=typeof JSON.parse(e)}catch(e){return!1}};throw"string"==typeof e&&r(e)?new Error(`Normalizing a string, but this does match schema.\n\nParsing this input string as JSON worked. This likely indicates fetch function did not parse\nthe JSON. By default, this only happens if "content-type" header includes "json".\nSee https://resthooks.io/docs/guides/custom-networking for more information\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`):new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".\n\n Schema: ${JSON.stringify(t,void 0,2)}\n Input: "${e}"`)}throw new Error(`Unexpected input given to normalize. Expected type to be "${n}", found "${null===e?"null":typeof e}".`)}const r={},s={},i=((e,t)=>(n,r,s,i,o)=>{const c=n.key,a=n.pk(s,i,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn(`Index not found in entity. Indexes must be top-level members of your entity.\nIndex: ${e}\nEntity: ${JSON.stringify(r,void 0,2)}`)}}})(r,s);return{entities:r,indexes:s,result:g(e,e,void 0,t,i,{})}},e.schema=O,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@rest-hooks/normalizr", | ||
"version": "6.0.0-beta.6", | ||
"version": "6.0.0-beta.7", | ||
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications", | ||
@@ -62,3 +62,3 @@ "homepage": "https://github.com/coinbase/rest-hooks/tree/master/packages/normalizr#readme", | ||
}, | ||
"gitHead": "081abed3b41210c2f81e74cbaaeda943cf086e1f" | ||
"gitHead": "d0cc5c3dd34a3092583ea9dbb8f8fbbe9c939ad2" | ||
} |
Sorry, the diff of this file is not supported yet
197857
3390