@data-client/rest
Version:
Quickly define typed REST resources and endpoints
2 lines (1 loc) • 24 kB
JavaScript
!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(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},t.apply(null,arguments)}function n(e,t){if(!{}.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var r=0;function i(e){return"__private_"+r+++"_"+e}function s(e){return!("function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}class o{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]}schemaKey(){return this.isSingleSchema?this.schema.key:Object.values(this.schema).join(";")}normalizeValue(e,t,n,r,i){if(!e)return e;const s=this.inferSchema(e,t,n);if(!s)return e;const o=i(s,e,t,n,r);return this.isSingleSchema||null==o?o:{id:o,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,t){const n=!this.isSingleSchema&&e&&(s(e)?e.get("schema"):e.schema);if(!this.isSingleSchema&&!n){if("object"==typeof e&&null!==e){const n=this.inferSchema(e,void 0,void 0);if(n)return t(n,e)}return e}const r=this.isSingleSchema?void 0:s(e)?e.get("id"):e.id;return t(this.isSingleSchema?this.schema:this.schema[n],r||e)}}class a extends o{normalize(e,n,r,i,s,o,a,c){return Object.keys(e).reduce(((n,r,o)=>{const a=e[r];return null!=a?t({},n,{[r]:this.normalizeValue(a,e,r,i,s)}):n}),{})}denormalize(e,n,r){return Object.keys(e).reduce(((n,i)=>{const s=e[i],o=this.denormalizeValue(s,r);return o&&"symbol"!=typeof o?t({},n,{[i]:o}):n}),{})}queryKey(e,t,n,r){}}const c=e=>void 0!==e&&"symbol"!=typeof e;class h extends o{normalize(e,t,n,r,i,s,o,a){const c=(e=>Array.isArray(e)?e:Object.keys(e).map((t=>e[t])))(e);return c.map(((e,s)=>this.normalizeValue(e,t,n,r,i)))}denormalize(e,t,n){return e.map?e.map((e=>this.denormalizeValue(e,n))).filter(c):e}queryKey(e,t,n,r){}toJSON(){return[this.schema]}}const u=Symbol("INVALID");function f(e,n,r,i){if(s(n))return function(e,t,n){let r;const i=Object.keys(e).reduce(((t,i)=>{const s=`${i}`,o=n(e[s],t.get(s));return"symbol"==typeof o&&(r=o),t.has(s)?t.set(s,o):t}),t);return null!=r?r:i}(e,n,i);const o=t({},n);for(const t of Object.keys(e)){const n=i(e[t],o[t]);if(void 0!==o[t]&&(o[t]=n),"symbol"==typeof n)return n}return o}class l{constructor(e){this.schema=void 0,this.define(e)}define(e){this.schema=Object.keys(e).reduce(((n,r)=>{const i=e[r];return t({},n,{[r]:i})}),this.schema||{})}normalize(...e){return((e,n,r,i,s,o)=>{const a=t({},n);return Object.keys(e).forEach((t=>{const r=e[t],i=o(r,n[t],n,t,s);void 0===i?delete a[t]:a[t]=i})),a})(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,i){const s={};return Object.keys(e).forEach((o=>{s[o]=n(e[o],t,r,i)})),s}(this.schema,e,t,n,r)}}const m=(e,t)=>[...e,...t],d=(e,t)=>[...t,...e],p=(e,n)=>t({},e,n),y=e=>[...e],g=e=>t({},e);class v{addWith(e,t){return b(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,n){if(this.schema=Array.isArray(e)?new h(e[0]):e,n?"nestKey"in n?this.nestKey=n.nestKey:this.argsKey="argsKey"in n?n.argsKey:e=>t({},e):this.argsKey=e=>t({},e),this.key=function(e){if(e instanceof h)return`[${e.schemaKey()}]`;if(e instanceof a)return`{${e.schemaKey()}}`;return`(${e.schemaKey()})`}(this.schema),null!=n&&n.nonFilterArgumentKeys){const{nonFilterArgumentKeys:e}=n;"function"==typeof e?this.nonFilterArgumentKeys=e:e instanceof RegExp?this.nonFilterArgumentKeys=t=>e.test(t):this.nonFilterArgumentKeys=t=>e.includes(t)}else null!=n&&n.createCollectionFilter&&(this.createCollectionFilter=n.createCollectionFilter.bind(this));this.schema instanceof h?(this.createIfValid=y,this.push=b(this,m),this.unshift=b(this,d)):e instanceof a&&(this.createIfValid=g,this.assign=b(this,p))}get cacheWith(){return this.schema.schema}toString(){return this.key}toJSON(){return{key:this.key,schema:this.schema.schema.toJSON()}}pk(e,t,n,r){const i=this.argsKey?this.argsKey(...r):this.nestKey(t,n);for(const e in i)["number","boolean"].includes(typeof i[e])&&(i[e]=`${i[e]}`);return function(e){const t=Object.keys(e).sort(),n={};for(const r of t)n[r]=e[r];return JSON.stringify(n)}(i)}normalize(e,t,n,r,i,s,o,a){const c=this.schema.normalize(e,t,n,r,i,s,o,a),h=this.pk(c,t,n,r);return s(this,c,h),h}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 b(e,t,n){const r={merge:{value:t},normalize:{value:w},queryKey:{value:O}};return e.schema instanceof h&&(r.createIfValid={value:E},r.denormalize={value:x}),n&&(r.createCollectionFilter={value:n}),Object.create(e,r)}function O(){}function w(e,t,n,r,i,s,o,a){const c=this.schema.normalize(this.schema instanceof h&&!Array.isArray(e)?[e]:e,t,n,r,i,s,o,a),u=this.createCollectionFilter(...r),f=o(this.key);return f&&Object.keys(f).forEach((e=>{u(JSON.parse(e))&&s(this,c,e)})),c}function E(e){return Array.isArray(e)?[...e]:t({},e)}function x(e,t,n){return Array.isArray(e)?this.schema.denormalize(e,t,n):this.schema.denormalize([e],t,n)[0]}function k(e,t){if(null==e)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(-1!==t.indexOf(r))continue;n[r]=e[r]}return n}const S=["pk","schema","key"];function A(e,n={}){class r extends e{static toString(){return this.key}static toJSON(){return{key:this.key,schema:this.schema}}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,n){return t({},e,n)}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,n,r,i){return t({},e)}static normalize(e,t,n,r,i,s,o,a){const c=this.process(e,t,n,r);let h;if(void 0===c)return h=this.pk(e,t,n,r),s(this,u,h),h;if(h=this.pk(c,t,n,r),h=void 0===h||""===h||"undefined"===h?`MISS-${Math.random()}`:`${h}`,a(this.key,h,e))return h;return function(e){if(e){const t=new Error(e);throw t.status=400,t}}(this.validate(c)),Object.keys(this.schema).forEach((e=>{Object.hasOwn(c,e)&&(c[e]=i(this.schema[e],c[e],c,e,r))})),s(this,c,h),h}static validate(e){}static queryKey(e,t,n,r){if(!e[0])return;const i=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 i=(s=t[0],o=e.indexes,o?o.find((e=>Object.hasOwn(s,e))):void 0);var s,o;if(!i)return;const a=t[0][i];return n(e.key,i,a)[a]}(this,e,r);return n(this.key,i)?i:void 0}static denormalize(e,t,n){if("symbol"==typeof e)return e;for(const t of Object.keys(this.schema)){const r=n(this.schema[t],e[t]);if("symbol"==typeof r){if(this.defaults[t])return r;e[t]=void 0}else e[t]=r}return e}static get defaults(){return Object.hasOwn(this,"__defaults")||Object.defineProperty(this,"__defaults",{value:new this,writable:!0,configurable:!0}),this.__defaults}}const i=k(n,S);if(Object.assign(r,i),"schema"in n?r.schema=n.schema:e.schema||(r.schema={}),"pk"in n?"function"==typeof n.pk?r.prototype.pk=function(e,t){return n.pk(this,e,t)}:r.prototype.pk=function(){return this[n.pk]}:"function"!=typeof e.prototype.pk&&(r.prototype.pk=function(){return this.id}),"key"in n)Object.defineProperty(r,"key",{value:n.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 o=function(){return"EntityMixin"===this.name?e.name:this.name},a="undefined"!=typeof document&&document.CLS_MANGLE?function(){return null==document.CLS_MANGLE||document.CLS_MANGLE(this),Object.defineProperty(r,"key",{get:o,set:s,enumerable:!0,configurable:!0}),o.call(this)}:o;Object.defineProperty(r,"key",{get:a,set:s,enumerable:!0,configurable:!0})}return r}var j=Object.freeze({__proto__:null,All:class extends h{constructor(e,t){super(e,t)}normalize(e,t,n,r,i,s,o,a){super.normalize(e,t,n,r,i,s,o,a)}queryKey(e,t,n,r){if(this.isSingleSchema){const e=n(this.schema.key);return void 0===e?u:Object.values(e).map((e=>e&&this.schema.pk(e)))}let i=!1;const s=Object.values(this.schema).flatMap((e=>{const t=n(e.key);return void 0===t?[]:(i=!0,Object.values(t).map((t=>({id:t&&e.pk(t),schema:this.getSchemaAttribute(t,void 0,void 0)}))))}));return i?s:u}},Array:h,Collection:v,Entity:A,EntityMixin:A,Invalidate:class{constructor(e){this._entity=e}get key(){return this._entity.key}normalize(e,t,n,r,i,s,o,a){const c=this._entity.process(e,t,n,r),h=this._entity.pk(c,t,n,r);return s(this,u,h),h}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(this._entity,e)}_denormalizeNullable(){return{}}_normalizeNullable(){return{}}},Object:l,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(this.schema,e);return"symbol"==typeof r?r:this.process(r,...t)}queryKey(e,t,n,r){return t(this.schema,e,n,r)}},Union:class extends o{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,i,s,o,a){return this.normalizeValue(e,t,n,r,i)}denormalize(e,t,n){return this.denormalizeValue(e,n)}queryKey(e,t,n,r){if(!e[0])return;const i=this.getSchemaAttribute(e[0],void 0,""),s=this.schema[i];if(void 0===s)return;const o=t(s,e,n,r);return void 0!==o?{id:o,schema:i}:void 0}},Values:a});const _=class{};class P extends(A(_)){static process(e,t,n,r){return super.process(e,t,n,r)}}var R;const T="undefined"!=typeof document;let C=T&&!(null!=(R=document.querySelector("meta[http-equiv='Content-Security-Policy']"))&&R.getAttribute("content"));try{C||Function()}catch(e){C=!0,T&&console.error("Content Security Policy: The previous CSP log can be safely ignored - @data-client/endpoint will use setPrototypeOf instead")}var z;class K extends Function{constructor(e,t){let n,r;return C?(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)}}z=K,(()=>{if("undefined"!=typeof document&&document.FUNC_MANGLE){const e=z.prototype.key;z.prototype.key=function(...t){return null==document.FUNC_MANGLE||document.FUNC_MANGLE(this),this.prototype.key=e,e.call(this,...t)}}})();const $=K;function N(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 i=1,s="";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==--i){a++;break}}else if("("===e[a]&&(i++,"?"!==e[a+1]))throw new TypeError("Capturing groups are not allowed at ".concat(a));s+=e[a++]}else s+=e[a++]+e[a++];if(i)throw new TypeError("Unbalanced pattern at ".concat(n));if(!s)throw new TypeError("Missing pattern at ".concat(n));t.push({type:"PATTERN",index:n,value:s}),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,i=void 0===r?"./":r,s=t.delimiter,o=void 0===s?"/#?":s,a=[],c=0,h=0,u="",f=function(e){if(h<n.length&&n[h].type===e)return n[h++].value},l=function(e){var t=f(e);if(void 0!==t)return t;var r=n[h],i=r.type,s=r.index;throw new TypeError("Unexpected ".concat(i," at ").concat(s,", expected ").concat(e))},m=function(){for(var e,t="";e=f("CHAR")||f("ESCAPED_CHAR");)t+=e;return t},d=function(e){var t=a[a.length-1],n=e||(t&&"string"==typeof t?t:"");if(t&&!n)throw new TypeError('Must have text between two parameters, missing text after "'.concat(t.name,'"'));return!n||function(e){for(var t=0,n=o;t<n.length;t++){var r=n[t];if(e.indexOf(r)>-1)return!0}return!1}(n)?"[^".concat(F(o),"]+?"):"(?:(?!".concat(F(n),")[^").concat(F(o),"])+?")};h<n.length;){var p=f("CHAR"),y=f("NAME"),g=f("PATTERN");if(y||g){var v=p||"";-1===i.indexOf(v)&&(u+=v,v=""),u&&(a.push(u),u=""),a.push({name:y||c++,prefix:v,suffix:"",pattern:g||d(v),modifier:f("MODIFIER")||""})}else{var b=p||f("ESCAPED_CHAR");if(b)u+=b;else if(u&&(a.push(u),u=""),f("OPEN")){v=m();var O=f("NAME")||"",w=f("PATTERN")||"",E=m();l("CLOSE"),a.push({name:O||(w?c++:""),pattern:O&&!w?d(v):w,prefix:v,suffix:E,modifier:f("MODIFIER")||""})}else l("END")}}return a}function M(e,t){return function(e,t){void 0===t&&(t={});var n=I(t),r=t.encode,i=void 0===r?function(e){return e}:r,s=t.validate,o=void 0===s||s,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 s=e[r];if("string"!=typeof s){var c=t?t[s.name]:void 0,h="?"===s.modifier||"*"===s.modifier,u="*"===s.modifier||"+"===s.modifier;if(Array.isArray(c)){if(!u)throw new TypeError('Expected "'.concat(s.name,'" to not repeat, but got an array'));if(0===c.length){if(h)continue;throw new TypeError('Expected "'.concat(s.name,'" to not be empty'))}for(var f=0;f<c.length;f++){var l=i(c[f],s);if(o&&!a[r].test(l))throw new TypeError('Expected all "'.concat(s.name,'" to match "').concat(s.pattern,'", but got "').concat(l,'"'));n+=s.prefix+l+s.suffix}}else if("string"!=typeof c&&"number"!=typeof c){if(!h){var m=u?"an array":"a string";throw new TypeError('Expected "'.concat(s.name,'" to be ').concat(m))}}else{l=i(String(c),s);if(o&&!a[r].test(l))throw new TypeError('Expected "'.concat(s.name,'" to match "').concat(s.pattern,'", but got "').concat(l,'"'));n+=s.prefix+l+s.suffix}}else n+=s}return n}}(N(e,t),t)}function F(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function I(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,i=void 0!==r&&r,s=n.start,o=void 0===s||s,a=n.end,c=void 0===a||a,h=n.encode,u=void 0===h?function(e){return e}:h,f=n.delimiter,l=void 0===f?"/#?":f,m=n.endsWith,d="[".concat(F(void 0===m?"":m),"]|$"),p="[".concat(F(l),"]"),y=o?"^":"",g=0,v=e;g<v.length;g++){var b=v[g];if("string"==typeof b)y+=F(u(b));else{var O=F(u(b.prefix)),w=F(u(b.suffix));if(b.pattern)if(O||w)if("+"===b.modifier||"*"===b.modifier){var E="*"===b.modifier?"?":"";y+="(?:".concat(O,"((?:").concat(b.pattern,")(?:").concat(w).concat(O,"(?:").concat(b.pattern,"))*)").concat(w,")").concat(E)}else y+="(?:".concat(O,"(").concat(b.pattern,")").concat(w,")").concat(b.modifier);else{if("+"===b.modifier||"*"===b.modifier)throw new TypeError('Can not repeat "'.concat(b.name,'" without a prefix and suffix'));y+="(".concat(b.pattern,")").concat(b.modifier)}else y+="(?:".concat(O).concat(w,")").concat(b.modifier)}}if(c)i||(y+="".concat(p,"?")),y+=n.endsWith?"(?=".concat(d,")"):"$";else{var x=e[e.length-1],k="string"==typeof x?p.indexOf(x[x.length-1])>-1:void 0===x;i||(y+="(?:".concat(p,"(?=").concat(d,"))?")),k||(y+="(?=".concat(p,"|").concat(d,")"))}return new RegExp(y,I(n))}(N(e,n),0,n)}function L(e,t,n){return e instanceof RegExp?function(e){return e}(e):Array.isArray(e)?function(e,t,n){var r=e.map((function(e){return L(e,t,n).source}));return new RegExp("(?:".concat(r.join("|"),")"),I(n))}(e,t,n):q(e,0,n)}function U(e,n){if("object"!=typeof e||void 0===e||Array.isArray(e))return;if(e instanceof v)return n(e);const r=t({},e instanceof l?e.schema:e);for(const e in r){if(!r[e])continue;const t=U(r[e],n);if(t)return t}}function D(e,n){if("object"!=typeof e||void 0===e)return e;if(e instanceof v)return n(e);const r=t({},e instanceof l?e.schema:e);for(const e in r)r[e]&&(r[e]=D(r[e],n));return r}Object.hasOwn=Object.hasOwn||function(e,t){return Object.prototype.hasOwnProperty.call(e,t)};class V extends Error{constructor(e){super(`${e.url}: ${e.statusText||`Status not 'ok': ${e.status}`}`),this.name="NetworkError",this.status=e.status,this.response=e}}function W(e,t){const n=new Set(e),r=[...e];for(const e of t)n.has(e)||r.push(e);return r}const J=new Map;function G(e){return J.has(e)||J.set(e,M(e,{encode:encodeURIComponent,validate:!1})),J.get(e)}const H=new Map;function B(e){return H.has(e)||H.set(e,new Set(N(e).map((e=>"string"==typeof e?e:`${e.name}`)))),H.get(e)}const Q=Object.prototype,X=Object.getPrototypeOf;var Y=i("hasBody");class Z extends K{constructor(e){var t;super(null!=(t=e.fetch)?t:async function(...e){const t=n(this,Y)[Y]&&e.length<2?{}:e[0]||{},r=n(this,Y)[Y]?e[e.length-1]:void 0;return this.fetchResponse(this.url(t),await this.getRequestInit(r)).then((e=>this.parseResponse(e))).then((t=>this.process(t,...e)))},e),Object.defineProperty(this,Y,{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=""),n(this,Y)[Y]=!("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(n(this,Y)[Y]&&e.length<2?{}:e[0]||{})}`}url(e={}){const t=G(this.path)(e),n=B(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 n=null!==(r=e)&&"object"==typeof r&&X(r)===Q;var r;n&&(e=JSON.stringify(e));const i=t({},this.requestInit,{method:this.method,signal:this.signal,body:e});return e&&!n||(i.headers=t({"Content-Type":"application/json"},i.headers)),i.headers=await this.getHeaders(i.headers),i}fetchResponse(e,t){return fetch(e,t).then((e=>{if(!e.ok)throw new V(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!==Z.prototype.process)return t;const n=new V(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 L(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 n extends this.constructor{}return Object.assign(n.prototype,this),new n(t({fetch:this.fetch},e))}paginated(e){if("string"==typeof e){const n=e;e=e=>{let r=t({},(function(e){if(null==e)throw new TypeError("Cannot destructure "+e)}(e),e));return delete r[n],[r]}}let n=!1;const r=D(this.schema,(t=>(n=!0,function(e,t){return Object.create(t,{name:{value:`Pagination(${t.schema})`},merge:{value:W},pk:{value:function(n,r,i,s){return t.pk.call(this,n,r,i,e(...s))}}})}(e,t))));if(!n)throw new Error("Missing Collection");const i=this;return this.extend({schema:r,key(...t){return i.key.call(this,...e(...t))},name:this.name+".getPage"})}get getPage(){return this.paginated(this.paginationField)}get push(){return this.extend({method:"POST",schema:U(this.schema,(e=>e.push)),name:this.name+".create"})}get unshift(){return this.extend({method:"POST",schema:U(this.schema,(e=>e.unshift)),name:this.name+".create"})}get assign(){return this.extend({method:"POST",schema:U(this.schema,(e=>e.assign)),name:this.name+".create"})}}const ee=["path","schema","Endpoint","Collection","optimistic","paginationField"],{Invalidate:te,Collection:ne}=j;function re(e){let{path:n,schema:r,Endpoint:i=Z,Collection:s=ne,optimistic:o,paginationField:a}=e,c=k(e,ee);const h=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}(n),u=e=>`${null==r?void 0:r.name}.${e}`;function f(e,t,n){e[t]=e[t].extend(n)}const l=t({},c),m=t({},c),d=new i(t({},c,{path:n,schema:r,name:u("get")}));o&&(l.getOptimisticResponse=ie,m.getOptimisticResponse=function(e){return function(n,r,i){const s=n.get(e,r);if(!s)throw n.abort;return t({},r,s,oe(i))}}(r));const p=new i(t({},l,{paginationField:a,path:h,schema:new s([r]),name:u("getList")}));return{get:d,getList:p,create:p.push.extend({name:u("create")}),update:new i(t({},l,{path:n,schema:r,method:"PUT",name:u("update")})),partialUpdate:new i(t({},m,{path:n,schema:r,method:"PATCH",name:u("partialUpdate")})),delete:new i(t({},l,{path:n,schema:r.process?new te(r):r,method:"DELETE",name:u("delete"),process:(e,t)=>e&&Object.keys(e).length?e:t,getOptimisticResponse:o?se:void 0})),extend(...e){if("string"==typeof e[0]){const[n,r]=e;if(n in this){const e=t({},this);return f(e,n,r),e}return t({},this,{[n]:this.get.extend(r)})}if("function"==typeof e[0]){return t({},this,e[0](this))}const n=e[0],r=t({},this);for(const e in n)f(r,e,n[e]);return r}}}function ie(e,n,r){return t({},n,oe(r))}function se(e,t){return t}function oe(e){return e instanceof FormData?Object.fromEntries(e.entries()):e}e.Endpoint=K,e.Entity=P,e.EntityMixin=A,e.ExtendableEndpoint=$,e.INVALID=u,e.NetworkError=V,e.RestEndpoint=Z,e.createResource=re,e.getUrlBase=G,e.getUrlTokens=B,e.hookifyResource=function(e,t){const n={};return Object.keys(e).forEach((r=>{const i=e[r];var s;void 0!==i.extend&&(n[`use${s=r,s.charAt(0).toUpperCase()+s.slice(1)}`]=()=>{const e=t();return i.extend({requestInit:e})})})),n},e.resource=re,e.schema=j,e.validateRequired=function(e,t){let n="";if(Object.keys(t).some((t=>!Object.hasOwn(e,t)&&(n=t,!0))))return`Missing key ${n}`}}));