@data-client/rest 0.11.1 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Rest={})}(this,(function(e){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function n(e,t){return e(t={exports:{}},t.exports),t.exports}var r=t(n((function(e){e.exports=function(e){if(null==e)throw new TypeError("Cannot destructure "+e)},e.exports.__esModule=!0,e.exports.default=e.exports}))),s=t(n((function(e){e.exports=function(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e},e.exports.__esModule=!0,e.exports.default=e.exports}))),i=t(n((function(e){var t=0;e.exports=function(e){return"__private_"+t+++"_"+e},e.exports.__esModule=!0,e.exports.default=e.exports})));function o(e){return!("function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}class a{constructor(e,t){this.schema=void 0,t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){"_schemaAttribute"in e&&!this._schemaAttribute?(this.schema=e.schema,this._schemaAttribute=e._schemaAttribute):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,o,a){if(!e)return e;const c=this.inferSchema(e,t,n);if(!c)return e;const u=r(e,t,n,c,s,i,o,a);return this.isSingleSchema||null==u?u:{id:u,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,t){const n=!this.isSingleSchema&&e&&(o(e)?e.get("schema"):e.schema);if(!this.isSingleSchema&&!n)return e;return t((this.isSingleSchema?void 0:o(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n])}}class c extends a{normalize(e,t,n,r,s,i,o,a){return Object.keys(e).reduce(((t,n,c)=>{const u=e[n];return null!=u?Object.assign({},t,{[n]:this.normalizeValue(u,e,n,r,s,i,o,a)}):t}),{})}denormalize(e,t,n){return Object.keys(e).reduce(((t,r)=>{const s=e[r],i=this.denormalizeValue(s,n);return i&&"symbol"!=typeof i?Object.assign({},t,{[r]:i}):t}),{})}queryKey(e,t,n,r){}}const u=e=>void 0!==e&&"symbol"!=typeof e;class h extends a{normalize(e,t,n,r,s,i,o,a){const c=(e=>Array.isArray(e)?e:Object.keys(e).map((t=>e[t])))(e);return c.map(((e,c)=>this.normalizeValue(e,t,n,r,s,i,o,a)))}denormalize(e,t,n){return e.map?e.map((e=>this.denormalizeValue(e,n))).filter(u):e}queryKey(e,t,n,r){}toJSON(){return[this.schema]}}const l=Symbol("INVALID");function f(e,t,n,r){if(o(t))return function(e,t,n){var r;let s;const i=Object.keys(e).reduce(((t,r)=>{const i=`${r}`,o=n(t.get(i),e[i]);return"symbol"==typeof o&&(s=o),t.has(i)?t.set(i,o):t}),t);return null!=(r=s)?r:i}(e,t,r);const s=Object.assign({},t);for(const t of Object.keys(e)){const n=r(s[t],e[t]);if(void 0!==s[t]&&(s[t]=n),"symbol"==typeof n)return n}return s}class d{constructor(e){this.schema=void 0,this.define(e)}define(e){this.schema=Object.keys(e).reduce(((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})}),this.schema||{})}normalize(...e){return((e,t,n,r,s,i,o,a,c)=>{const u=Object.assign({},t);return Object.keys(e).forEach((n=>{const r=e[n],h=s(t[n],t,n,r,i,o,a,c);void 0===h?delete u[n]:u[n]=h})),u})(this.schema,...e)}denormalize(e,t,n){return f(this.schema,e,0,n)}queryKey(e,t,n,r){return function(e,t,n,r,s){const i={};return Object.keys(e).forEach((o=>{i[o]=n(e[o],t,r,s)})),i}(this.schema,e,t,n,r)}}const p=Symbol("create"),m=(e,t)=>[...e,...t],y=(e,t)=>[...t,...e],g=(e,t)=>Object.assign({},e,t),b=e=>[...e],v=e=>Object.assign({},e);class O{addWith(e,t){return x(this,e,t)}createCollectionFilter(...e){return t=>Object.entries(t).every((([t,n])=>{var r;return this.nonFilterArgumentKeys(t)||`${e[0][t]}`===n||`${null==(r=e[1])?void 0:r[t]}`===n}))}nonFilterArgumentKeys(e){return e.startsWith("order")}constructor(e,t){if(this.schema=Array.isArray(e)?new h(e[0]):e,t?"nestKey"in t?this.nestKey=t.nestKey:this.argsKey="argsKey"in t?t.argsKey:e=>Object.assign({},e):this.argsKey=e=>Object.assign({},e),this.key=`COLLECT:${this.schema.constructor.name}(${this.schema.schema.key})`,null!=t&&t.nonFilterArgumentKeys){const{nonFilterArgumentKeys:e}=t;"function"==typeof e?this.nonFilterArgumentKeys=e:e instanceof RegExp?this.nonFilterArgumentKeys=t=>e.test(t):this.nonFilterArgumentKeys=t=>e.includes(t)}else null!=t&&t.createCollectionFilter&&(this.createCollectionFilter=t.createCollectionFilter.bind(this));this.schema instanceof h?(this.createIfValid=b,this.push=x(this,m),this.unshift=x(this,y)):e instanceof c&&(this.createIfValid=v,this.assign=x(this,g))}get cacheWith(){return this.schema.schema}toJSON(){return{name:`Collection(${this.schema.schema.name})`,schema:this.schema.schema.toJSON(),key:this.key}}pk(e,t,n,r){const s=this.argsKey?this.argsKey(...r):this.nestKey(t,n);for(const e in s)"string"!=typeof s[e]&&void 0!==s[e]&&(s[e]=`${s[e]}`);return function(e){const t=Object.keys(e).sort(),n={};for(const r of t)n[r]=e[r];return JSON.stringify(n)}(s)}normalize(e,t,n,r,s,i,o,a){const c=this.schema.normalize(e,t,n,r,s,i,o,a),u=this.pk(c,t,n,a);return s(this,c,u),u}merge(e,t){return t}shouldReorder(e,t,n,r){return t.fetchedAt<e.fetchedAt}mergeWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?this.merge(r,n):this.merge(n,r)}mergeMetaWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?e:t}queryKey(e,t,n,r){if(this.argsKey){const t=this.pk(void 0,void 0,"",e);if(n(this.key,t))return t}}denormalize(e,t,n){return this.schema.denormalize(e,t,n)}}function x(e,t,n){const r={merge:{value:t},normalize:{value:E},infer:{value:j}};return e.schema instanceof h&&(r.createIfValid={value:w},r.denormalize={value:k}),n&&(r.createCollectionFilter={value:n}),Object.create(e,r)}function j(){}function E(e,t,n,r,s,i,o,a){i[p]={};const c=this.schema.normalize(this.schema instanceof h&&!Array.isArray(e)?[e]:e,t,n,r,s,i,o,a),u=this.createCollectionFilter(...a);return o[this.key]&&Object.keys(o[this.key]).forEach((e=>{u(JSON.parse(e))&&s(this,c,e)})),c}function w(e){return Array.isArray(e)?[...e]:Object.assign({},e)}function k(e,t,n){return Array.isArray(e)?this.schema.denormalize(e,t,n):this.schema.denormalize([e],t,n)[0]}var S=t(n((function(e){e.exports=function(e,t){if(null==e)return{};var n,r,s={},i=Object.keys(e);for(r=0;r<i.length;r++)n=i[r],t.indexOf(n)>=0||(s[n]=e[n]);return s},e.exports.__esModule=!0,e.exports.default=e.exports})));const A=["pk","schema","key"];function _(e,t={}){class n extends e{static toString(){return this.key}static toJSON(){return{name:this.name,schema:this.schema,key:this.key}}static pk(e,t,n,r){return this.prototype.pk.call(e,t,n,r)}static shouldUpdate(e,t,n,r){return!0}static shouldReorder(e,t,n,r){return t.fetchedAt<e.fetchedAt}static merge(e,t){return Object.assign({},e,t)}static mergeWithStore(e,t,n,r){return this.shouldUpdate(e,t,n,r)?typeof r!=typeof n?r:this.shouldReorder(e,t,n,r)?this.merge(r,n):this.merge(n,r):n}static mergeMetaWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?e:t}static fromJS(e={}){const t=new this(e);return Object.assign(t,e),t}static createIfValid(e){if(!this.validate(e))return this.fromJS(e)}static process(e,t,n,r){return Object.assign({},e)}static normalize(e,t,n,r,s,i,o,a){const c=this.process(e,t,n,a);let u=this.pk(c,t,n,a);u=void 0===u||""===u||"undefined"===u?`MISS-${Math.random()}`:`${u}`;const h=this.key;if(h in i||(i[h]={}),u in i[h]||(i[h][u]=[]),i[h][u].some((t=>t===e)))return u;return function(e){if(e){const t=new Error(e);throw t.status=400,t}}(this.validate(c)),i[h][u].push(e),Object.keys(this.schema).forEach((e=>{Object.hasOwn(c,e)&&(c[e]=r(c[e],c,e,this.schema[e],s,i,o,a))})),s(this,c,u),u}static validate(e){}static queryKey(e,t,n,r){if(!e[0])return;const s=function(e,t,n){if(["string","number"].includes(typeof t[0]))return`${t[0]}`;const r=e.pk(t[0],void 0,"",t);if(void 0!==r&&""!==r)return r;const s=(i=t[0],o=e.indexes,o?o.find((e=>Object.hasOwn(i,e))):void 0);var i,o;if(!s)return;const a=t[0][s];return n(e.key,s,a)[a]}(this,e,r);return n(this.key,s)?s:void 0}static denormalize(e,t,n){if("symbol"==typeof e)return e;for(const t of Object.keys(this.schema)){const r=this.schema[t],s=n(e[t],r);if("symbol"==typeof s){if(this.defaults[t])return s;e[t]=void 0}else e[t]=s}return e}static get defaults(){return Object.hasOwn(this,"__defaults")||Object.defineProperty(this,"__defaults",{value:new this,writable:!0,configurable:!0}),this.__defaults}}const r=S(t,A);if(Object.assign(n,r),"schema"in t?n.schema=t.schema:e.schema||(n.schema={}),"pk"in t?"function"==typeof t.pk?n.prototype.pk=function(e,n){return t.pk(this,e,n)}:n.prototype.pk=function(){return this[t.pk]}:"function"!=typeof e.prototype.pk&&(n.prototype.pk=function(){return this.id}),"key"in t)Object.defineProperty(n,"key",{value:t.key,configurable:!0,writable:!0,enumerable:!0});else if(!("key"in e)){function s(e){Object.defineProperty(this,"key",{value:e,writable:!0,enumerable:!0,configurable:!0})}const i=function(){return"EntityMixin"===this.name?e.name:this.name},o="undefined"!=typeof document&&document.CLS_MANGLE?function(){return null==document.CLS_MANGLE||document.CLS_MANGLE(this),Object.defineProperty(n,"key",{get:i,set:s,enumerable:!0,configurable:!0}),i.call(this)}:i;Object.defineProperty(n,"key",{get:o,set:s,enumerable:!0,configurable:!0})}return n}var P=Object.freeze({__proto__:null,Union:class extends a{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,o,a){return this.normalizeValue(e,t,n,r,s,i,o,a)}denormalize(e,t,n){return this.denormalizeValue(e,n)}queryKey(e,t,n,r){if(!e[0])return;const s=this.getSchemaAttribute(e[0],void 0,""),i=this.schema[s];if(void 0===i)return;const o=t(i,e,n,r);return void 0!==o?{id:o,schema:s}:void 0}},Values:c,Array:h,All:class extends h{constructor(e,t){super(e,t)}normalize(e,t,n,r,s,i,o,a){super.normalize(e,t,n,r,s,i,o,a)}queryKey(e,t,n,r){if(this.isSingleSchema){const e=n(this.schema.key);return void 0===e?l:Object.values(e).map((e=>e&&this.schema.pk(e)))}let s=!1;const i=Object.values(this.schema).flatMap((e=>{const t=n(e.key);return void 0===t?[]:(s=!0,Object.values(t).map((t=>({id:t&&e.pk(t),schema:this.getSchemaAttribute(t,void 0,void 0)}))))}));return s?i:l}},Object:d,Invalidate:class{constructor(e){this._entity=e}get key(){return this._entity.key}normalize(e,t,n,r,s,i,o,a){const c=this._entity.process(e,t,n,a),u=this._entity.pk(c,t,n,a);return s(this,l,u),u}merge(e,t){return t}mergeWithStore(e,t,n,r){return this.merge(n,r)}mergeMetaWithStore(e,t,n,r){return t}queryKey(e,t,n,r){}denormalize(e,t,n){return n(e,this._entity)}_denormalizeNullable(){return{}}_normalizeNullable(){return{}}},Collection:O,Entity:_,Query:class{constructor(e,t){this.schema=e,this.process=t}normalize(...e){return this.schema.normalize(...e)}denormalize(e,t,n){const r=n(e,this.schema);return"symbol"==typeof r?void 0:this.process(r,...t)}queryKey(e,t,n,r){return t(this.schema,e,n,r)}}});const R=class{};class C extends(_(R)){static process(e,t,n,r){return super.process(e,t,n,r)}}var T;const z="undefined"!=typeof document;let N=z&&!(null!=(T=document.querySelector("meta[http-equiv='Content-Security-Policy']"))&&T.getAttribute("content"));try{N||Function()}catch(e){N=!0,z&&console.error("Content Security Policy: The previous CSP log can be safely ignored - @data-client/endpoint will use setPrototypeOf instead")}var $;class K extends Function{constructor(e,t){let n,r;return N?(n=(...e)=>n.fetch(...e),Object.setPrototypeOf(n,new.target.prototype)):(super("return arguments.callee.fetch.apply(arguments.callee, arguments)"),n=this),e&&(n.fetch=e),t&&"name"in t||!e||!e.name||"anonymous"===e.name||(r=e.name),Object.defineProperty(n,"name",{get(){return r||this.__name},set(e){this.__name=e}}),Object.assign(n,t),n}key(...e){return`${this.name} ${JSON.stringify(e)}`}testKey(e){return e.startsWith(this.name)}bind(e,...t){const n=this.fetch,r=this.key;return this.extend({fetch(){return n.apply(null!=e?e:this,t)},key(){return r.apply(this,t)}})}extend(e){class t extends this.constructor{}return Object.assign(t.prototype,this),new t(e.fetch,e)}}$=K,(()=>{if("undefined"!=typeof document&&document.FUNC_MANGLE){const e=$.prototype.key;$.prototype.key=function(...t){return null==document.FUNC_MANGLE||document.FUNC_MANGLE(this),this.prototype.key=e,e.call(this,...t)}}})();const M=K;function F(e,t){void 0===t&&(t={});for(var n=function(e){for(var t=[],n=0;n<e.length;){var r=e[n];if("*"!==r&&"+"!==r&&"?"!==r)if("\\"!==r)if("{"!==r)if("}"!==r)if(":"!==r)if("("!==r)t.push({type:"CHAR",index:n,value:e[n++]});else{var s=1,i="";if("?"===e[a=n+1])throw new TypeError('Pattern cannot start with "?" at '.concat(a));for(;a<e.length;)if("\\"!==e[a]){if(")"===e[a]){if(0==--s){a++;break}}else if("("===e[a]&&(s++,"?"!==e[a+1]))throw new TypeError("Capturing groups are not allowed at ".concat(a));i+=e[a++]}else i+=e[a++]+e[a++];if(s)throw new TypeError("Unbalanced pattern at ".concat(n));if(!i)throw new TypeError("Missing pattern at ".concat(n));t.push({type:"PATTERN",index:n,value:i}),n=a}else{for(var o="",a=n+1;a<e.length;){var c=e.charCodeAt(a);if(!(c>=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||95===c))break;o+=e[a++]}if(!o)throw new TypeError("Missing parameter name at ".concat(n));t.push({type:"NAME",index:n,value:o}),n=a}else t.push({type:"CLOSE",index:n,value:e[n++]});else t.push({type:"OPEN",index:n,value:e[n++]});else t.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});else t.push({type:"MODIFIER",index:n,value:e[n++]})}return t.push({type:"END",index:n,value:""}),t}(e),r=t.prefixes,s=void 0===r?"./":r,i="[^".concat(L(t.delimiter||"/#?"),"]+?"),o=[],a=0,c=0,u="",h=function(e){if(c<n.length&&n[c].type===e)return n[c++].value},l=function(e){var t=h(e);if(void 0!==t)return t;var r=n[c],s=r.type,i=r.index;throw new TypeError("Unexpected ".concat(s," at ").concat(i,", expected ").concat(e))},f=function(){for(var e,t="";e=h("CHAR")||h("ESCAPED_CHAR");)t+=e;return t};c<n.length;){var d=h("CHAR"),p=h("NAME"),m=h("PATTERN");if(p||m){var y=d||"";-1===s.indexOf(y)&&(u+=y,y=""),u&&(o.push(u),u=""),o.push({name:p||a++,prefix:y,suffix:"",pattern:m||i,modifier:h("MODIFIER")||""})}else{var g=d||h("ESCAPED_CHAR");if(g)u+=g;else if(u&&(o.push(u),u=""),h("OPEN")){y=f();var b=h("NAME")||"",v=h("PATTERN")||"",O=f();l("CLOSE"),o.push({name:b||(v?a++:""),pattern:b&&!v?i:v,prefix:y,suffix:O,modifier:h("MODIFIER")||""})}else l("END")}}return o}function I(e,t){return function(e,t){void 0===t&&(t={});var n=U(t),r=t.encode,s=void 0===r?function(e){return e}:r,i=t.validate,o=void 0===i||i,a=e.map((function(e){if("object"==typeof e)return new RegExp("^(?:".concat(e.pattern,")$"),n)}));return function(t){for(var n="",r=0;r<e.length;r++){var i=e[r];if("string"!=typeof i){var c=t?t[i.name]:void 0,u="?"===i.modifier||"*"===i.modifier,h="*"===i.modifier||"+"===i.modifier;if(Array.isArray(c)){if(!h)throw new TypeError('Expected "'.concat(i.name,'" to not repeat, but got an array'));if(0===c.length){if(u)continue;throw new TypeError('Expected "'.concat(i.name,'" to not be empty'))}for(var l=0;l<c.length;l++){var f=s(c[l],i);if(o&&!a[r].test(f))throw new TypeError('Expected all "'.concat(i.name,'" to match "').concat(i.pattern,'", but got "').concat(f,'"'));n+=i.prefix+f+i.suffix}}else if("string"!=typeof c&&"number"!=typeof c){if(!u){var d=h?"an array":"a string";throw new TypeError('Expected "'.concat(i.name,'" to be ').concat(d))}}else{f=s(String(c),i);if(o&&!a[r].test(f))throw new TypeError('Expected "'.concat(i.name,'" to match "').concat(i.pattern,'", but got "').concat(f,'"'));n+=i.prefix+f+i.suffix}}else n+=i}return n}}(F(e,t),t)}function L(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function U(e){return e&&e.sensitive?"":"i"}function q(e,t,n){return function(e,t,n){void 0===n&&(n={});for(var r=n.strict,s=void 0!==r&&r,i=n.start,o=void 0===i||i,a=n.end,c=void 0===a||a,u=n.encode,h=void 0===u?function(e){return e}:u,l=n.delimiter,f=void 0===l?"/#?":l,d=n.endsWith,p="[".concat(L(void 0===d?"":d),"]|$"),m="[".concat(L(f),"]"),y=o?"^":"",g=0,b=e;g<b.length;g++){var v=b[g];if("string"==typeof v)y+=L(h(v));else{var O=L(h(v.prefix)),x=L(h(v.suffix));if(v.pattern)if(t&&t.push(v),O||x)if("+"===v.modifier||"*"===v.modifier){var j="*"===v.modifier?"?":"";y+="(?:".concat(O,"((?:").concat(v.pattern,")(?:").concat(x).concat(O,"(?:").concat(v.pattern,"))*)").concat(x,")").concat(j)}else y+="(?:".concat(O,"(").concat(v.pattern,")").concat(x,")").concat(v.modifier);else"+"===v.modifier||"*"===v.modifier?y+="((?:".concat(v.pattern,")").concat(v.modifier,")"):y+="(".concat(v.pattern,")").concat(v.modifier);else y+="(?:".concat(O).concat(x,")").concat(v.modifier)}}if(c)s||(y+="".concat(m,"?")),y+=n.endsWith?"(?=".concat(p,")"):"$";else{var E=e[e.length-1],w="string"==typeof E?m.indexOf(E[E.length-1])>-1:void 0===E;s||(y+="(?:".concat(m,"(?=").concat(p,"))?")),w||(y+="(?=".concat(m,"|").concat(p,")"))}return new RegExp(y,U(n))}(F(e,n),t,n)}function D(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;for(var n=/\((?:\?<(.*?)>)?(?!\?)/g,r=0,s=n.exec(e.source);s;)t.push({name:s[1]||r++,prefix:"",suffix:"",modifier:"",pattern:""}),s=n.exec(e.source);return e}(e,t):Array.isArray(e)?function(e,t,n){var r=e.map((function(e){return D(e,t,n).source}));return new RegExp("(?:".concat(r.join("|"),")"),U(n))}(e,t,n):q(e,t,n)}function V(e,t){if("object"!=typeof e||void 0===e||Array.isArray(e))return;if(e instanceof O)return t(e);const n=Object.assign({},e instanceof d?e.schema:e);for(const e in n){if(!n[e])continue;const r=V(n[e],t);if(r)return r}}function W(e,t){if("object"!=typeof e||void 0===e)return e;if(e instanceof O)return t(e);const n=Object.assign({},e instanceof d?e.schema:e);for(const e in n)n[e]&&(n[e]=W(n[e],t));return n}class J extends Error{constructor(e){super(e.statusText||`Network response not 'ok': ${e.status}`),this.name="NetworkError",this.status=e.status,this.response=e}}function G(e,t){const n=new Set(e),r=[...e];for(const e of t)n.has(e)||r.push(e);return r}const H=Object.create(null);function B(e){return e in H||(H[e]=I(e,{encode:encodeURIComponent,validate:!1})),H[e]}const Q=Object.create(null);function X(e){return e in Q||(Q[e]=new Set(F(e).map((e=>"string"==typeof e?e:`${e.name}`)))),Q[e]}const Y=Object.prototype,Z=Object.getPrototypeOf;var ee=i("hasBody");class te extends K{constructor(e){var t;super(null!=(t=e.fetch)?t:async function(...e){const t=s(this,ee)[ee]&&e.length<2?{}:e[0]||{},n=s(this,ee)[ee]?e[e.length-1]:void 0;return this.fetchResponse(this.url(t),await this.getRequestInit(n)).then((e=>this.parseResponse(e))).then((t=>this.process(t,...e)))},e),Object.defineProperty(this,ee,{writable:!0,value:void 0}),(!("sideEffect"in this)||"method"in e&&!("sideEffect"in e))&&(this.sideEffect="GET"!==e.method&&void 0!==e.method||void 0),void 0===this.method&&(this.method=this.sideEffect?"POST":"GET"),void 0===this.urlPrefix&&(this.urlPrefix=""),s(this,ee)[ee]=!("body"in this&&void 0===this.body||["GET","DELETE"].includes(this.method)),Object.defineProperty(this,"name",{get(){return"__name"in this?this.__name:this.urlPrefix+this.path}})}key(...e){return`${this.method} ${this.url(s(this,ee)[ee]&&e.length<2?{}:e[0]||{})}`}url(e={}){const t=B(this.path)(e),n=X(this.path),r={};return Object.keys(e).forEach((t=>{n.has(t)||(r[t]=e[t])})),Object.keys(r).length?`${this.urlPrefix}${t}?${this.searchToString(r)}`:`${this.urlPrefix}${t}`}searchToString(e){return function(e){const t=new URLSearchParams(e);return t.sort(),t.toString()}(e)}getHeaders(e){return e}async getRequestInit(e){const t=null!==(n=e)&&"object"==typeof n&&Z(n)===Y;var n;t&&(e=JSON.stringify(e));const r=Object.assign({},this.requestInit,{method:this.method,signal:this.signal,body:e});return e&&!t||(r.headers=Object.assign({"Content-Type":"application/json"},r.headers)),r.headers=await this.getHeaders(r.headers),r}fetchResponse(e,t){return fetch(e,t).then((e=>{if(!e.ok)throw new J(e);return e})).catch((e=>{throw e instanceof TypeError&&(e.status=500),e}))}parseResponse(e){var t;return 204===e.status?Promise.resolve(null):null!=(t=e.headers.get("content-type"))&&t.includes("json")?e.json().catch((e=>{throw e.status=400,e})):e.text().then((t=>{if(["string","undefined"].includes(typeof this.schema)||null===this.schema||this.process!==te.prototype.process)return t;const n=new J(e);throw n.status=404,n.message=`Unexpected text response for schema ${this.schema}`,n}))}process(e){return e}errorPolicy(e){return e.status>=500?"soft":void 0}get pathRegex(){return D(this.path)}testKey(e){const t=this.method+" "+this.urlPrefix;if(!e.startsWith(t))return!1;let n=e.lastIndexOf("?");return-1===n&&(n=void 0),this.pathRegex.test(e.substring(t.length,n))}extend(e){class t extends this.constructor{}return Object.assign(t.prototype,this),new t(Object.assign({fetch:this.fetch},e))}paginated(e){if("string"==typeof e){const t=e;e=e=>{let n=Object.assign({},(r(e),e));return delete n[t],[n]}}let t=!1;const n=W(this.schema,(n=>(t=!0,function(e,t){return Object.create(t,{name:{value:`Pagination(${t.schema})`},merge:{value:G},pk:{value:function(n,r,s,i){return t.pk.call(this,n,r,s,e(...i))}}})}(e,n))));if(!t)throw new Error("Missing Collection");const s=this;return this.extend({schema:n,key(...t){return s.key.call(this,...e(...t))},name:this.name+".getPage"})}get getPage(){return this.paginated(this.paginationField)}get push(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.push)),name:this.name+".create"})}get unshift(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.unshift)),name:this.name+".create"})}get assign(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.assign)),name:this.name+".create"})}}const ne=["path","schema","Endpoint","Collection","optimistic","paginationField"],{Invalidate:re,Collection:se}=P;function ie(e,t,n){return Object.assign({},t,ae(n))}function oe(e,t){return t}function ae(e){return e instanceof FormData?Object.fromEntries(e.entries()):e}Object.hasOwn=Object.hasOwn||function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},e.Endpoint=K,e.Entity=C,e.ExtendableEndpoint=M,e.INVALID=l,e.NetworkError=J,e.RestEndpoint=te,e.createResource=function(e){let{path:t,schema:n,Endpoint:r=te,Collection:s=se,optimistic:i,paginationField:o}=e,a=S(e,ne);const c=function(e){const t=e.lastIndexOf(":");if(-1===t)throw new Error("Resource path requires at least one :parameter");let n=e.substring(0,t);return"/"===n[n.length-1]&&(n=n.substring(0,n.length-1)),n}(t),u=e=>`${null==n?void 0:n.name}.${e}`;function h(e,t,n){e[t]=e[t].extend(n)}const l=Object.assign({},a),f=Object.assign({},a),d=new r(Object.assign({},a,{path:t,schema:n,name:u("get")}));i&&(l.getOptimisticResponse=ie,f.getOptimisticResponse=function(e){return function(t,n,r){const s=t.get(e,n);if(!s)throw t.abort;return Object.assign({},n,s,ae(r))}}(n));const p=new r(Object.assign({},l,{paginationField:o,path:c,schema:new s([n]),name:u("getList")}));return{get:d,getList:p,create:p.push.extend({name:u("create")}),update:new r(Object.assign({},l,{path:t,schema:n,method:"PUT",name:u("update")})),partialUpdate:new r(Object.assign({},f,{path:t,schema:n,method:"PATCH",name:u("partialUpdate")})),delete:new r(Object.assign({},l,{path:t,schema:n.process?new re(n):n,method:"DELETE",name:u("delete"),process:(e,t)=>e&&Object.keys(e).length?e:t,getOptimisticResponse:i?oe:void 0})),extend(...e){if("string"==typeof e[0]){const[t,n]=e;if(t in this){const e=Object.assign({},this);return h(e,t,n),e}return Object.assign({},this,{[t]:this.get.extend(n)})}if("function"==typeof e[0]){const t=e[0](this);return Object.assign({},this,t)}const t=e[0],n=Object.assign({},this);for(const e in t)h(n,e,t[e]);return n}}},e.getUrlBase=B,e.getUrlTokens=X,e.hookifyResource=function(e,t){const n={};return Object.keys(e).forEach((r=>{const s=e[r];var i;void 0!==s.extend&&(n[`use${i=r,i.charAt(0).toUpperCase()+i.slice(1)}`]=()=>{const e=t();return s.extend({requestInit:e})})})),n},e.schema=P,e.validateRequired=function(e,t){let n="";if(Object.keys(t).some((t=>!Object.hasOwn(e,t)&&(n=t,!0))))return`Missing key ${n}`},Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Rest={})}(this,(function(e){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function n(e,t){return e(t={exports:{}},t.exports),t.exports}var r=t(n((function(e){e.exports=function(e){if(null==e)throw new TypeError("Cannot destructure "+e)},e.exports.__esModule=!0,e.exports.default=e.exports}))),s=t(n((function(e){e.exports=function(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e},e.exports.__esModule=!0,e.exports.default=e.exports}))),i=t(n((function(e){var t=0;e.exports=function(e){return"__private_"+t+++"_"+e},e.exports.__esModule=!0,e.exports.default=e.exports})));function o(e){return!("function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}class a{constructor(e,t){this.schema=void 0,t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){"_schemaAttribute"in e&&!this._schemaAttribute?(this.schema=e.schema,this._schemaAttribute=e._schemaAttribute):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,o,a){if(!e)return e;const c=this.inferSchema(e,t,n);if(!c)return e;const u=r(e,t,n,c,s,i,o,a);return this.isSingleSchema||null==u?u:{id:u,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,t){const n=!this.isSingleSchema&&e&&(o(e)?e.get("schema"):e.schema);if(!this.isSingleSchema&&!n)return e;return t((this.isSingleSchema?void 0:o(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n])}}class c extends a{normalize(e,t,n,r,s,i,o,a){return Object.keys(e).reduce(((t,n,c)=>{const u=e[n];return null!=u?Object.assign({},t,{[n]:this.normalizeValue(u,e,n,r,s,i,o,a)}):t}),{})}denormalize(e,t,n){return Object.keys(e).reduce(((t,r)=>{const s=e[r],i=this.denormalizeValue(s,n);return i&&"symbol"!=typeof i?Object.assign({},t,{[r]:i}):t}),{})}queryKey(e,t,n,r){}}const u=e=>void 0!==e&&"symbol"!=typeof e;class h extends a{normalize(e,t,n,r,s,i,o,a){const c=(e=>Array.isArray(e)?e:Object.keys(e).map((t=>e[t])))(e);return c.map(((e,c)=>this.normalizeValue(e,t,n,r,s,i,o,a)))}denormalize(e,t,n){return e.map?e.map((e=>this.denormalizeValue(e,n))).filter(u):e}queryKey(e,t,n,r){}toJSON(){return[this.schema]}}const l=Symbol("INVALID");function f(e,t,n,r){if(o(t))return function(e,t,n){var r;let s;const i=Object.keys(e).reduce(((t,r)=>{const i=`${r}`,o=n(t.get(i),e[i]);return"symbol"==typeof o&&(s=o),t.has(i)?t.set(i,o):t}),t);return null!=(r=s)?r:i}(e,t,r);const s=Object.assign({},t);for(const t of Object.keys(e)){const n=r(s[t],e[t]);if(void 0!==s[t]&&(s[t]=n),"symbol"==typeof n)return n}return s}class p{constructor(e){this.schema=void 0,this.define(e)}define(e){this.schema=Object.keys(e).reduce(((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})}),this.schema||{})}normalize(...e){return((e,t,n,r,s,i,o,a,c)=>{const u=Object.assign({},t);return Object.keys(e).forEach((n=>{const r=e[n],h=s(t[n],t,n,r,i,o,a,c);void 0===h?delete u[n]:u[n]=h})),u})(this.schema,...e)}denormalize(e,t,n){return f(this.schema,e,0,n)}queryKey(e,t,n,r){return function(e,t,n,r,s){const i={};return Object.keys(e).forEach((o=>{i[o]=n(e[o],t,r,s)})),i}(this.schema,e,t,n,r)}}const d=Symbol("create"),m=(e,t)=>[...e,...t],y=(e,t)=>[...t,...e],g=(e,t)=>Object.assign({},e,t),b=e=>[...e],v=e=>Object.assign({},e);class O{addWith(e,t){return x(this,e,t)}createCollectionFilter(...e){return t=>Object.entries(t).every((([t,n])=>{var r;return this.nonFilterArgumentKeys(t)||`${e[0][t]}`===n||`${null==(r=e[1])?void 0:r[t]}`===n}))}nonFilterArgumentKeys(e){return e.startsWith("order")}constructor(e,t){if(this.schema=Array.isArray(e)?new h(e[0]):e,t?"nestKey"in t?this.nestKey=t.nestKey:this.argsKey="argsKey"in t?t.argsKey:e=>Object.assign({},e):this.argsKey=e=>Object.assign({},e),this.key=`COLLECT:${this.schema.constructor.name}(${this.schema.schema.key})`,null!=t&&t.nonFilterArgumentKeys){const{nonFilterArgumentKeys:e}=t;"function"==typeof e?this.nonFilterArgumentKeys=e:e instanceof RegExp?this.nonFilterArgumentKeys=t=>e.test(t):this.nonFilterArgumentKeys=t=>e.includes(t)}else null!=t&&t.createCollectionFilter&&(this.createCollectionFilter=t.createCollectionFilter.bind(this));this.schema instanceof h?(this.createIfValid=b,this.push=x(this,m),this.unshift=x(this,y)):e instanceof c&&(this.createIfValid=v,this.assign=x(this,g))}get cacheWith(){return this.schema.schema}toJSON(){return{name:`Collection(${this.schema.schema.name})`,schema:this.schema.schema.toJSON(),key:this.key}}pk(e,t,n,r){const s=this.argsKey?this.argsKey(...r):this.nestKey(t,n);for(const e in s)"string"!=typeof s[e]&&void 0!==s[e]&&(s[e]=`${s[e]}`);return function(e){const t=Object.keys(e).sort(),n={};for(const r of t)n[r]=e[r];return JSON.stringify(n)}(s)}normalize(e,t,n,r,s,i,o,a){const c=this.schema.normalize(e,t,n,r,s,i,o,a),u=this.pk(c,t,n,a);return s(this,c,u),u}merge(e,t){return t}shouldReorder(e,t,n,r){return t.fetchedAt<e.fetchedAt}mergeWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?this.merge(r,n):this.merge(n,r)}mergeMetaWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?e:t}queryKey(e,t,n,r){if(this.argsKey){const t=this.pk(void 0,void 0,"",e);if(n(this.key,t))return t}}denormalize(e,t,n){return this.schema.denormalize(e,t,n)}}function x(e,t,n){const r={merge:{value:t},normalize:{value:E},infer:{value:j}};return e.schema instanceof h&&(r.createIfValid={value:w},r.denormalize={value:k}),n&&(r.createCollectionFilter={value:n}),Object.create(e,r)}function j(){}function E(e,t,n,r,s,i,o,a){i[d]={};const c=this.schema.normalize(this.schema instanceof h&&!Array.isArray(e)?[e]:e,t,n,r,s,i,o,a),u=this.createCollectionFilter(...a);return o[this.key]&&Object.keys(o[this.key]).forEach((e=>{u(JSON.parse(e))&&s(this,c,e)})),c}function w(e){return Array.isArray(e)?[...e]:Object.assign({},e)}function k(e,t,n){return Array.isArray(e)?this.schema.denormalize(e,t,n):this.schema.denormalize([e],t,n)[0]}var S=t(n((function(e){e.exports=function(e,t){if(null==e)return{};var n,r,s={},i=Object.keys(e);for(r=0;r<i.length;r++)n=i[r],t.indexOf(n)>=0||(s[n]=e[n]);return s},e.exports.__esModule=!0,e.exports.default=e.exports})));const A=["pk","schema","key"];function _(e,t={}){class n extends e{static toString(){return this.key}static toJSON(){return{name:this.name,schema:this.schema,key:this.key}}static pk(e,t,n,r){return this.prototype.pk.call(e,t,n,r)}static shouldUpdate(e,t,n,r){return!0}static shouldReorder(e,t,n,r){return t.fetchedAt<e.fetchedAt}static merge(e,t){return Object.assign({},e,t)}static mergeWithStore(e,t,n,r){return this.shouldUpdate(e,t,n,r)?typeof r!=typeof n?r:this.shouldReorder(e,t,n,r)?this.merge(r,n):this.merge(n,r):n}static mergeMetaWithStore(e,t,n,r){return this.shouldReorder(e,t,n,r)?e:t}static fromJS(e={}){const t=new this(e);return Object.assign(t,e),t}static createIfValid(e){if(!this.validate(e))return this.fromJS(e)}static process(e,t,n,r){return Object.assign({},e)}static normalize(e,t,n,r,s,i,o,a){const c=this.process(e,t,n,a);let u=this.pk(c,t,n,a);u=void 0===u||""===u||"undefined"===u?`MISS-${Math.random()}`:`${u}`;const h=this.key;if(h in i||(i[h]={}),u in i[h]||(i[h][u]=[]),i[h][u].some((t=>t===e)))return u;return function(e){if(e){const t=new Error(e);throw t.status=400,t}}(this.validate(c)),i[h][u].push(e),Object.keys(this.schema).forEach((e=>{Object.hasOwn(c,e)&&(c[e]=r(c[e],c,e,this.schema[e],s,i,o,a))})),s(this,c,u),u}static validate(e){}static queryKey(e,t,n,r){if(!e[0])return;const s=function(e,t,n){if(["string","number"].includes(typeof t[0]))return`${t[0]}`;const r=e.pk(t[0],void 0,"",t);if(void 0!==r&&""!==r)return r;const s=(i=t[0],o=e.indexes,o?o.find((e=>Object.hasOwn(i,e))):void 0);var i,o;if(!s)return;const a=t[0][s];return n(e.key,s,a)[a]}(this,e,r);return n(this.key,s)?s:void 0}static denormalize(e,t,n){if("symbol"==typeof e)return e;for(const t of Object.keys(this.schema)){const r=this.schema[t],s=n(e[t],r);if("symbol"==typeof s){if(this.defaults[t])return s;e[t]=void 0}else e[t]=s}return e}static get defaults(){return Object.hasOwn(this,"__defaults")||Object.defineProperty(this,"__defaults",{value:new this,writable:!0,configurable:!0}),this.__defaults}}const r=S(t,A);if(Object.assign(n,r),"schema"in t?n.schema=t.schema:e.schema||(n.schema={}),"pk"in t?"function"==typeof t.pk?n.prototype.pk=function(e,n){return t.pk(this,e,n)}:n.prototype.pk=function(){return this[t.pk]}:"function"!=typeof e.prototype.pk&&(n.prototype.pk=function(){return this.id}),"key"in t)Object.defineProperty(n,"key",{value:t.key,configurable:!0,writable:!0,enumerable:!0});else if(!("key"in e)){function s(e){Object.defineProperty(this,"key",{value:e,writable:!0,enumerable:!0,configurable:!0})}const i=function(){return"EntityMixin"===this.name?e.name:this.name},o="undefined"!=typeof document&&document.CLS_MANGLE?function(){return null==document.CLS_MANGLE||document.CLS_MANGLE(this),Object.defineProperty(n,"key",{get:i,set:s,enumerable:!0,configurable:!0}),i.call(this)}:i;Object.defineProperty(n,"key",{get:o,set:s,enumerable:!0,configurable:!0})}return n}var P=Object.freeze({__proto__:null,Union:class extends a{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,o,a){return this.normalizeValue(e,t,n,r,s,i,o,a)}denormalize(e,t,n){return this.denormalizeValue(e,n)}queryKey(e,t,n,r){if(!e[0])return;const s=this.getSchemaAttribute(e[0],void 0,""),i=this.schema[s];if(void 0===i)return;const o=t(i,e,n,r);return void 0!==o?{id:o,schema:s}:void 0}},Values:c,Array:h,All:class extends h{constructor(e,t){super(e,t)}normalize(e,t,n,r,s,i,o,a){super.normalize(e,t,n,r,s,i,o,a)}queryKey(e,t,n,r){if(this.isSingleSchema){const e=n(this.schema.key);return void 0===e?l:Object.values(e).map((e=>e&&this.schema.pk(e)))}let s=!1;const i=Object.values(this.schema).flatMap((e=>{const t=n(e.key);return void 0===t?[]:(s=!0,Object.values(t).map((t=>({id:t&&e.pk(t),schema:this.getSchemaAttribute(t,void 0,void 0)}))))}));return s?i:l}},Object:p,Invalidate:class{constructor(e){this._entity=e}get key(){return this._entity.key}normalize(e,t,n,r,s,i,o,a){const c=this._entity.process(e,t,n,a),u=this._entity.pk(c,t,n,a);return s(this,l,u),u}merge(e,t){return t}mergeWithStore(e,t,n,r){return this.merge(n,r)}mergeMetaWithStore(e,t,n,r){return t}queryKey(e,t,n,r){}denormalize(e,t,n){return n(e,this._entity)}_denormalizeNullable(){return{}}_normalizeNullable(){return{}}},Collection:O,Entity:_,Query:class{constructor(e,t){this.schema=e,this.process=t}normalize(...e){return this.schema.normalize(...e)}denormalize(e,t,n){const r=n(e,this.schema);return"symbol"==typeof r?r:this.process(r,...t)}queryKey(e,t,n,r){return t(this.schema,e,n,r)}}});const R=class{};class C extends(_(R)){static process(e,t,n,r){return super.process(e,t,n,r)}}var T;const z="undefined"!=typeof document;let N=z&&!(null!=(T=document.querySelector("meta[http-equiv='Content-Security-Policy']"))&&T.getAttribute("content"));try{N||Function()}catch(e){N=!0,z&&console.error("Content Security Policy: The previous CSP log can be safely ignored - @data-client/endpoint will use setPrototypeOf instead")}var $;class K extends Function{constructor(e,t){let n,r;return N?(n=(...e)=>n.fetch(...e),Object.setPrototypeOf(n,new.target.prototype)):(super("return arguments.callee.fetch.apply(arguments.callee, arguments)"),n=this),e&&(n.fetch=e),t&&"name"in t||!e||!e.name||"anonymous"===e.name||(r=e.name),Object.defineProperty(n,"name",{get(){return r||this.__name},set(e){this.__name=e}}),Object.assign(n,t),n}key(...e){return`${this.name} ${JSON.stringify(e)}`}testKey(e){return e.startsWith(this.name)}bind(e,...t){const n=this.fetch,r=this.key;return this.extend({fetch(){return n.apply(null!=e?e:this,t)},key(){return r.apply(this,t)}})}extend(e){class t extends this.constructor{}return Object.assign(t.prototype,this),new t(e.fetch,e)}}$=K,(()=>{if("undefined"!=typeof document&&document.FUNC_MANGLE){const e=$.prototype.key;$.prototype.key=function(...t){return null==document.FUNC_MANGLE||document.FUNC_MANGLE(this),this.prototype.key=e,e.call(this,...t)}}})();const M=K;function F(e,t){void 0===t&&(t={});for(var n=function(e){for(var t=[],n=0;n<e.length;){var r=e[n];if("*"!==r&&"+"!==r&&"?"!==r)if("\\"!==r)if("{"!==r)if("}"!==r)if(":"!==r)if("("!==r)t.push({type:"CHAR",index:n,value:e[n++]});else{var s=1,i="";if("?"===e[a=n+1])throw new TypeError('Pattern cannot start with "?" at '.concat(a));for(;a<e.length;)if("\\"!==e[a]){if(")"===e[a]){if(0==--s){a++;break}}else if("("===e[a]&&(s++,"?"!==e[a+1]))throw new TypeError("Capturing groups are not allowed at ".concat(a));i+=e[a++]}else i+=e[a++]+e[a++];if(s)throw new TypeError("Unbalanced pattern at ".concat(n));if(!i)throw new TypeError("Missing pattern at ".concat(n));t.push({type:"PATTERN",index:n,value:i}),n=a}else{for(var o="",a=n+1;a<e.length;){var c=e.charCodeAt(a);if(!(c>=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||95===c))break;o+=e[a++]}if(!o)throw new TypeError("Missing parameter name at ".concat(n));t.push({type:"NAME",index:n,value:o}),n=a}else t.push({type:"CLOSE",index:n,value:e[n++]});else t.push({type:"OPEN",index:n,value:e[n++]});else t.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});else t.push({type:"MODIFIER",index:n,value:e[n++]})}return t.push({type:"END",index:n,value:""}),t}(e),r=t.prefixes,s=void 0===r?"./":r,i="[^".concat(L(t.delimiter||"/#?"),"]+?"),o=[],a=0,c=0,u="",h=function(e){if(c<n.length&&n[c].type===e)return n[c++].value},l=function(e){var t=h(e);if(void 0!==t)return t;var r=n[c],s=r.type,i=r.index;throw new TypeError("Unexpected ".concat(s," at ").concat(i,", expected ").concat(e))},f=function(){for(var e,t="";e=h("CHAR")||h("ESCAPED_CHAR");)t+=e;return t};c<n.length;){var p=h("CHAR"),d=h("NAME"),m=h("PATTERN");if(d||m){var y=p||"";-1===s.indexOf(y)&&(u+=y,y=""),u&&(o.push(u),u=""),o.push({name:d||a++,prefix:y,suffix:"",pattern:m||i,modifier:h("MODIFIER")||""})}else{var g=p||h("ESCAPED_CHAR");if(g)u+=g;else if(u&&(o.push(u),u=""),h("OPEN")){y=f();var b=h("NAME")||"",v=h("PATTERN")||"",O=f();l("CLOSE"),o.push({name:b||(v?a++:""),pattern:b&&!v?i:v,prefix:y,suffix:O,modifier:h("MODIFIER")||""})}else l("END")}}return o}function I(e,t){return function(e,t){void 0===t&&(t={});var n=U(t),r=t.encode,s=void 0===r?function(e){return e}:r,i=t.validate,o=void 0===i||i,a=e.map((function(e){if("object"==typeof e)return new RegExp("^(?:".concat(e.pattern,")$"),n)}));return function(t){for(var n="",r=0;r<e.length;r++){var i=e[r];if("string"!=typeof i){var c=t?t[i.name]:void 0,u="?"===i.modifier||"*"===i.modifier,h="*"===i.modifier||"+"===i.modifier;if(Array.isArray(c)){if(!h)throw new TypeError('Expected "'.concat(i.name,'" to not repeat, but got an array'));if(0===c.length){if(u)continue;throw new TypeError('Expected "'.concat(i.name,'" to not be empty'))}for(var l=0;l<c.length;l++){var f=s(c[l],i);if(o&&!a[r].test(f))throw new TypeError('Expected all "'.concat(i.name,'" to match "').concat(i.pattern,'", but got "').concat(f,'"'));n+=i.prefix+f+i.suffix}}else if("string"!=typeof c&&"number"!=typeof c){if(!u){var p=h?"an array":"a string";throw new TypeError('Expected "'.concat(i.name,'" to be ').concat(p))}}else{f=s(String(c),i);if(o&&!a[r].test(f))throw new TypeError('Expected "'.concat(i.name,'" to match "').concat(i.pattern,'", but got "').concat(f,'"'));n+=i.prefix+f+i.suffix}}else n+=i}return n}}(F(e,t),t)}function L(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function U(e){return e&&e.sensitive?"":"i"}function q(e,t,n){return function(e,t,n){void 0===n&&(n={});for(var r=n.strict,s=void 0!==r&&r,i=n.start,o=void 0===i||i,a=n.end,c=void 0===a||a,u=n.encode,h=void 0===u?function(e){return e}:u,l=n.delimiter,f=void 0===l?"/#?":l,p=n.endsWith,d="[".concat(L(void 0===p?"":p),"]|$"),m="[".concat(L(f),"]"),y=o?"^":"",g=0,b=e;g<b.length;g++){var v=b[g];if("string"==typeof v)y+=L(h(v));else{var O=L(h(v.prefix)),x=L(h(v.suffix));if(v.pattern)if(t&&t.push(v),O||x)if("+"===v.modifier||"*"===v.modifier){var j="*"===v.modifier?"?":"";y+="(?:".concat(O,"((?:").concat(v.pattern,")(?:").concat(x).concat(O,"(?:").concat(v.pattern,"))*)").concat(x,")").concat(j)}else y+="(?:".concat(O,"(").concat(v.pattern,")").concat(x,")").concat(v.modifier);else"+"===v.modifier||"*"===v.modifier?y+="((?:".concat(v.pattern,")").concat(v.modifier,")"):y+="(".concat(v.pattern,")").concat(v.modifier);else y+="(?:".concat(O).concat(x,")").concat(v.modifier)}}if(c)s||(y+="".concat(m,"?")),y+=n.endsWith?"(?=".concat(d,")"):"$";else{var E=e[e.length-1],w="string"==typeof E?m.indexOf(E[E.length-1])>-1:void 0===E;s||(y+="(?:".concat(m,"(?=").concat(d,"))?")),w||(y+="(?=".concat(m,"|").concat(d,")"))}return new RegExp(y,U(n))}(F(e,n),t,n)}function D(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;for(var n=/\((?:\?<(.*?)>)?(?!\?)/g,r=0,s=n.exec(e.source);s;)t.push({name:s[1]||r++,prefix:"",suffix:"",modifier:"",pattern:""}),s=n.exec(e.source);return e}(e,t):Array.isArray(e)?function(e,t,n){var r=e.map((function(e){return D(e,t,n).source}));return new RegExp("(?:".concat(r.join("|"),")"),U(n))}(e,t,n):q(e,t,n)}function V(e,t){if("object"!=typeof e||void 0===e||Array.isArray(e))return;if(e instanceof O)return t(e);const n=Object.assign({},e instanceof p?e.schema:e);for(const e in n){if(!n[e])continue;const r=V(n[e],t);if(r)return r}}function W(e,t){if("object"!=typeof e||void 0===e)return e;if(e instanceof O)return t(e);const n=Object.assign({},e instanceof p?e.schema:e);for(const e in n)n[e]&&(n[e]=W(n[e],t));return n}class J extends Error{constructor(e){super(e.statusText||`Network response not 'ok': ${e.status}`),this.name="NetworkError",this.status=e.status,this.response=e}}function G(e,t){const n=new Set(e),r=[...e];for(const e of t)n.has(e)||r.push(e);return r}const H=Object.create(null);function B(e){return e in H||(H[e]=I(e,{encode:encodeURIComponent,validate:!1})),H[e]}const Q=Object.create(null);function X(e){return e in Q||(Q[e]=new Set(F(e).map((e=>"string"==typeof e?e:`${e.name}`)))),Q[e]}const Y=Object.prototype,Z=Object.getPrototypeOf;var ee=i("hasBody");class te extends K{constructor(e){var t;super(null!=(t=e.fetch)?t:async function(...e){const t=s(this,ee)[ee]&&e.length<2?{}:e[0]||{},n=s(this,ee)[ee]?e[e.length-1]:void 0;return this.fetchResponse(this.url(t),await this.getRequestInit(n)).then((e=>this.parseResponse(e))).then((t=>this.process(t,...e)))},e),Object.defineProperty(this,ee,{writable:!0,value:void 0}),(!("sideEffect"in this)||"method"in e&&!("sideEffect"in e))&&(this.sideEffect="GET"!==e.method&&void 0!==e.method||void 0),void 0===this.method&&(this.method=this.sideEffect?"POST":"GET"),void 0===this.urlPrefix&&(this.urlPrefix=""),s(this,ee)[ee]=!("body"in this&&void 0===this.body||["GET","DELETE"].includes(this.method)),Object.defineProperty(this,"name",{get(){return"__name"in this?this.__name:this.urlPrefix+this.path}})}key(...e){return`${this.method} ${this.url(s(this,ee)[ee]&&e.length<2?{}:e[0]||{})}`}url(e={}){const t=B(this.path)(e),n=X(this.path),r={};return Object.keys(e).forEach((t=>{n.has(t)||(r[t]=e[t])})),Object.keys(r).length?`${this.urlPrefix}${t}?${this.searchToString(r)}`:`${this.urlPrefix}${t}`}searchToString(e){return function(e){const t=new URLSearchParams(e);return t.sort(),t.toString()}(e)}getHeaders(e){return e}async getRequestInit(e){const t=null!==(n=e)&&"object"==typeof n&&Z(n)===Y;var n;t&&(e=JSON.stringify(e));const r=Object.assign({},this.requestInit,{method:this.method,signal:this.signal,body:e});return e&&!t||(r.headers=Object.assign({"Content-Type":"application/json"},r.headers)),r.headers=await this.getHeaders(r.headers),r}fetchResponse(e,t){return fetch(e,t).then((e=>{if(!e.ok)throw new J(e);return e})).catch((e=>{throw e instanceof TypeError&&(e.status=500),e}))}parseResponse(e){var t;return 204===e.status?Promise.resolve(null):null!=(t=e.headers.get("content-type"))&&t.includes("json")?e.json().catch((e=>{throw e.status=400,e})):e.text().then((t=>{if(["string","undefined"].includes(typeof this.schema)||null===this.schema||this.process!==te.prototype.process)return t;const n=new J(e);throw n.status=404,n.message=`Unexpected text response for schema ${this.schema}`,n}))}process(e){return e}errorPolicy(e){return e.status>=500?"soft":void 0}get pathRegex(){return D(this.path)}testKey(e){const t=this.method+" "+this.urlPrefix;if(!e.startsWith(t))return!1;let n=e.lastIndexOf("?");return-1===n&&(n=void 0),this.pathRegex.test(e.substring(t.length,n))}extend(e){class t extends this.constructor{}return Object.assign(t.prototype,this),new t(Object.assign({fetch:this.fetch},e))}paginated(e){if("string"==typeof e){const t=e;e=e=>{let n=Object.assign({},(r(e),e));return delete n[t],[n]}}let t=!1;const n=W(this.schema,(n=>(t=!0,function(e,t){return Object.create(t,{name:{value:`Pagination(${t.schema})`},merge:{value:G},pk:{value:function(n,r,s,i){return t.pk.call(this,n,r,s,e(...i))}}})}(e,n))));if(!t)throw new Error("Missing Collection");const s=this;return this.extend({schema:n,key(...t){return s.key.call(this,...e(...t))},name:this.name+".getPage"})}get getPage(){return this.paginated(this.paginationField)}get push(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.push)),name:this.name+".create"})}get unshift(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.unshift)),name:this.name+".create"})}get assign(){return this.extend({method:"POST",schema:V(this.schema,(e=>e.assign)),name:this.name+".create"})}}const ne=["path","schema","Endpoint","Collection","optimistic","paginationField"],{Invalidate:re,Collection:se}=P;function ie(e,t,n){return Object.assign({},t,ae(n))}function oe(e,t){return t}function ae(e){return e instanceof FormData?Object.fromEntries(e.entries()):e}Object.hasOwn=Object.hasOwn||function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},e.Endpoint=K,e.Entity=C,e.ExtendableEndpoint=M,e.INVALID=l,e.NetworkError=J,e.RestEndpoint=te,e.createResource=function(e){let{path:t,schema:n,Endpoint:r=te,Collection:s=se,optimistic:i,paginationField:o}=e,a=S(e,ne);const c=function(e){const t=e.lastIndexOf(":");if(-1===t)throw new Error("Resource path requires at least one :parameter");let n=e.substring(0,t);return"/"===n[n.length-1]&&(n=n.substring(0,n.length-1)),n}(t),u=e=>`${null==n?void 0:n.name}.${e}`;function h(e,t,n){e[t]=e[t].extend(n)}const l=Object.assign({},a),f=Object.assign({},a),p=new r(Object.assign({},a,{path:t,schema:n,name:u("get")}));i&&(l.getOptimisticResponse=ie,f.getOptimisticResponse=function(e){return function(t,n,r){const s=t.get(e,n);if(!s)throw t.abort;return Object.assign({},n,s,ae(r))}}(n));const d=new r(Object.assign({},l,{paginationField:o,path:c,schema:new s([n]),name:u("getList")}));return{get:p,getList:d,create:d.push.extend({name:u("create")}),update:new r(Object.assign({},l,{path:t,schema:n,method:"PUT",name:u("update")})),partialUpdate:new r(Object.assign({},f,{path:t,schema:n,method:"PATCH",name:u("partialUpdate")})),delete:new r(Object.assign({},l,{path:t,schema:n.process?new re(n):n,method:"DELETE",name:u("delete"),process:(e,t)=>e&&Object.keys(e).length?e:t,getOptimisticResponse:i?oe:void 0})),extend(...e){if("string"==typeof e[0]){const[t,n]=e;if(t in this){const e=Object.assign({},this);return h(e,t,n),e}return Object.assign({},this,{[t]:this.get.extend(n)})}if("function"==typeof e[0]){const t=e[0](this);return Object.assign({},this,t)}const t=e[0],n=Object.assign({},this);for(const e in t)h(n,e,t[e]);return n}}},e.getUrlBase=B,e.getUrlTokens=X,e.hookifyResource=function(e,t){const n={};return Object.keys(e).forEach((r=>{const s=e[r];var i;void 0!==s.extend&&(n[`use${i=r,i.charAt(0).toUpperCase()+i.slice(1)}`]=()=>{const e=t();return s.extend({requestInit:e})})})),n},e.schema=P,e.validateRequired=function(e,t){let n="";if(Object.keys(t).some((t=>!Object.hasOwn(e,t)&&(n=t,!0))))return`Missing key ${n}`},Object.defineProperty(e,"__esModule",{value:!0})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-client/rest",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "description": "Endpoints for REST APIs",
5
5
  "homepage": "https://dataclient.io/rest",
6
6
  "repository": {
@@ -126,7 +126,7 @@
126
126
  "license": "Apache-2.0",
127
127
  "dependencies": {
128
128
  "@babel/runtime": "^7.17.0",
129
- "@data-client/endpoint": "^0.11.1",
129
+ "@data-client/endpoint": "^0.11.3",
130
130
  "path-to-regexp": "^6.2.1"
131
131
  },
132
132
  "devDependencies": {
@@ -1122,7 +1122,7 @@ describe('createResource()', () => {
1122
1122
 
1123
1123
  const queryRemainingTodos = new schema.Query(
1124
1124
  TodoResource.getList.schema,
1125
- entries => entries && entries.filter(todo => !todo.completed).length,
1125
+ entries => entries.filter(todo => !todo.completed).length,
1126
1126
  );
1127
1127
 
1128
1128
  () => useQuery(queryRemainingTodos, { userId: 1 });
@@ -1,10 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
  import { Entity, schema } from '@data-client/endpoint';
3
- import { useController, useLive, useSuspense } from '@data-client/react';
3
+ import { useController, useSuspense } from '@data-client/react';
4
4
  import { User } from '__tests__/new';
5
5
 
6
6
  import createResource from '../createResource';
7
- import { PathArgs, PathKeys } from '../pathTypes';
8
7
  import RestEndpoint, { GetEndpoint, MutateEndpoint } from '../RestEndpoint';
9
8
 
10
9
  it('RestEndpoint construct and extend with typed options', () => {
@@ -430,113 +429,125 @@ it('should precisely type function arguments', () => {
430
429
  });
431
430
 
432
431
  it('should allow sideEffect overrides', () => {
433
- const getEth = new RestEndpoint({
434
- urlPrefix: 'https://rpc.ankr.com',
435
- path: '/eth',
436
- method: 'POST',
437
- body: {} as { jsonrpc: string; id: number; method: string; params: any[] },
438
- pollFrequency: 30 * 1000,
439
- sideEffect: undefined,
440
- });
441
- const getEthExtend = new RestEndpoint({
442
- urlPrefix: 'https://rpc.ankr.com',
443
- path: '/eth',
444
- method: 'POST',
445
- body: {} as { jsonrpc: string; id: number; method: string; params: any[] },
446
- pollFrequency: 30 * 1000,
447
- }).extend({ sideEffect: undefined });
448
-
449
- const a: undefined = getEth.sideEffect;
450
- const b: undefined = getEthExtend.sideEffect;
451
432
  () => {
452
- const ctrl = useController();
453
- ctrl.fetch(getEth, {
454
- jsonrpc: '2.0',
455
- id: 1,
456
- method: 'eth_getBlockByNumber',
457
- params: ['latest', true],
458
- });
459
- useSuspense(getEth, {
460
- jsonrpc: '2.0',
461
- id: 1,
462
- method: 'eth_getBlockByNumber',
463
- params: ['latest', true],
464
- });
465
- // @ts-expect-error
466
- ctrl.fetch(getEth, {
467
- id: 1,
468
- method: 'eth_getBlockByNumber',
469
- params: ['latest', true],
470
- });
471
- // @ts-expect-error
472
- useSuspense(getEth, {
473
- id: 1,
474
- method: 'eth_getBlockByNumber',
475
- params: ['latest', true],
476
- });
477
- // @ts-expect-error
478
- useSuspense(getEth, {
479
- jsonrpc: '2.0',
480
- id: 1,
481
- method: 6,
482
- params: ['latest', true],
483
- });
484
- useSuspense(
485
- getEth,
486
- // @ts-expect-error
487
- {},
488
- {
433
+ const getEth = new RestEndpoint({
434
+ urlPrefix: 'https://rpc.ankr.com',
435
+ path: '/eth',
436
+ method: 'POST',
437
+ body: {} as {
438
+ jsonrpc: string;
439
+ id: number;
440
+ method: string;
441
+ params: any[];
442
+ },
443
+ pollFrequency: 30 * 1000,
444
+ sideEffect: undefined,
445
+ });
446
+ const getEthExtend = new RestEndpoint({
447
+ urlPrefix: 'https://rpc.ankr.com',
448
+ path: '/eth',
449
+ method: 'POST',
450
+ body: {} as {
451
+ jsonrpc: string;
452
+ id: number;
453
+ method: string;
454
+ params: any[];
455
+ },
456
+ pollFrequency: 30 * 1000,
457
+ }).extend({ sideEffect: undefined });
458
+
459
+ const a: undefined = getEth.sideEffect;
460
+ const b: undefined = getEthExtend.sideEffect;
461
+ () => {
462
+ const ctrl = useController();
463
+ ctrl.fetch(getEth, {
489
464
  jsonrpc: '2.0',
490
465
  id: 1,
491
466
  method: 'eth_getBlockByNumber',
492
467
  params: ['latest', true],
493
- },
494
- );
495
- };
496
- () => {
497
- const ctrl = useController();
498
- ctrl.fetch(getEthExtend, {
499
- jsonrpc: '2.0',
500
- id: 1,
501
- method: 'eth_getBlockByNumber',
502
- params: ['latest', true],
503
- });
504
- useSuspense(getEthExtend, {
505
- jsonrpc: '2.0',
506
- id: 1,
507
- method: 'eth_getBlockByNumber',
508
- params: ['latest', true],
509
- });
510
- // @ts-expect-error
511
- ctrl.fetch(getEthExtend, {
512
- id: 1,
513
- method: 'eth_getBlockByNumber',
514
- params: ['latest', true],
515
- });
516
- // @ts-expect-error
517
- useSuspense(getEthExtend, {
518
- id: 1,
519
- method: 'eth_getBlockByNumber',
520
- params: ['latest', true],
521
- });
522
- // @ts-expect-error
523
- useSuspense(getEthExtend, {
524
- jsonrpc: '2.0',
525
- id: 1,
526
- method: 6,
527
- params: ['latest', true],
528
- });
529
- useSuspense(
530
- getEthExtend,
468
+ });
469
+ useSuspense(getEth, {
470
+ jsonrpc: '2.0',
471
+ id: 1,
472
+ method: 'eth_getBlockByNumber',
473
+ params: ['latest', true],
474
+ });
475
+ // @ts-expect-error
476
+ ctrl.fetch(getEth, {
477
+ id: 1,
478
+ method: 'eth_getBlockByNumber',
479
+ params: ['latest', true],
480
+ });
531
481
  // @ts-expect-error
532
- {},
533
- {
482
+ useSuspense(getEth, {
483
+ id: 1,
484
+ method: 'eth_getBlockByNumber',
485
+ params: ['latest', true],
486
+ });
487
+ // @ts-expect-error
488
+ useSuspense(getEth, {
489
+ jsonrpc: '2.0',
490
+ id: 1,
491
+ method: 6,
492
+ params: ['latest', true],
493
+ });
494
+ useSuspense(
495
+ getEth,
496
+ // @ts-expect-error
497
+ {},
498
+ {
499
+ jsonrpc: '2.0',
500
+ id: 1,
501
+ method: 'eth_getBlockByNumber',
502
+ params: ['latest', true],
503
+ },
504
+ );
505
+ };
506
+ () => {
507
+ const ctrl = useController();
508
+ ctrl.fetch(getEthExtend, {
534
509
  jsonrpc: '2.0',
535
510
  id: 1,
536
511
  method: 'eth_getBlockByNumber',
537
512
  params: ['latest', true],
538
- },
539
- );
513
+ });
514
+ useSuspense(getEthExtend, {
515
+ jsonrpc: '2.0',
516
+ id: 1,
517
+ method: 'eth_getBlockByNumber',
518
+ params: ['latest', true],
519
+ });
520
+ // @ts-expect-error
521
+ ctrl.fetch(getEthExtend, {
522
+ id: 1,
523
+ method: 'eth_getBlockByNumber',
524
+ params: ['latest', true],
525
+ });
526
+ // @ts-expect-error
527
+ useSuspense(getEthExtend, {
528
+ id: 1,
529
+ method: 'eth_getBlockByNumber',
530
+ params: ['latest', true],
531
+ });
532
+ // @ts-expect-error
533
+ useSuspense(getEthExtend, {
534
+ jsonrpc: '2.0',
535
+ id: 1,
536
+ method: 6,
537
+ params: ['latest', true],
538
+ });
539
+ useSuspense(
540
+ getEthExtend,
541
+ // @ts-expect-error
542
+ {},
543
+ {
544
+ jsonrpc: '2.0',
545
+ id: 1,
546
+ method: 'eth_getBlockByNumber',
547
+ params: ['latest', true],
548
+ },
549
+ );
550
+ };
540
551
  };
541
552
  });
542
553