@data-client/rest 0.12.8 → 0.14.0
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.
- package/README.md +1 -1
- package/dist/index.umd.min.js +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ const getTodo = new RestEndpoint({
|
|
|
25
25
|
});
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
[RestEndpoint](https://dataclient.io/rest/api/RestEndpoint) infers [path-to-
|
|
28
|
+
[RestEndpoint](https://dataclient.io/rest/api/RestEndpoint) infers [path-to-regexp](https://github.com/pillarjs/path-to-regexp#compile-reverse-path-to-regexp)
|
|
29
29
|
argument types, enabling enforcement of function calls
|
|
30
30
|
|
|
31
31
|
```typescript
|
package/dist/index.umd.min.js
CHANGED
|
@@ -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){if("object"==typeof e&&null!==e){const n=this.inferSchema(e,void 0,void 0);if(n)return t(e,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?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 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){if(!e)return e;const i=this.inferSchema(e,t,n);if(!i)return e;const o=s(i,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&&(o(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:o(e)?e.get("id"):e.id;return t(this.isSingleSchema?this.schema:this.schema[n],r||e)}}class c extends a{normalize(e,t,n,r,s,i,o,a){return Object.keys(e).reduce(((t,n,i)=>{const o=e[n];return null!=o?Object.assign({},t,{[n]:this.normalizeValue(o,e,n,r,s)}):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,i)=>this.normalizeValue(e,t,n,r,s)))}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(e[i],t.get(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(e[t],s[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],o=i(r,t[n],t,n,s);void 0===o?delete u[n]:u[n]=o})),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=(e,t)=>[...e,...t],m=(e,t)=>[...t,...e],y=(e,t)=>Object.assign({},e,t),g=e=>[...e],b=e=>Object.assign({},e);class v{addWith(e,t){return O(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=g,this.push=O(this,p),this.unshift=O(this,m)):e instanceof c&&(this.createIfValid=b,this.assign=O(this,y))}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,r);return i(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 O(e,t,n){const r={merge:{value:t},normalize:{value:j},infer:{value:x}};return e.schema instanceof h&&(r.createIfValid={value:E},r.denormalize={value:w}),n&&(r.createCollectionFilter={value:n}),Object.create(e,r)}function x(){}function j(e,t,n,r,s,i,o,a){const c=this.schema.normalize(this.schema instanceof h&&!Array.isArray(e)?[e]:e,t,n,r,s,i,o,a),u=this.createCollectionFilter(...r),l=o(this.key);return l&&Object.keys(l).forEach((e=>{u(JSON.parse(e))&&i(this,c,e)})),c}function E(e){return Array.isArray(e)?[...e]:Object.assign({},e)}function w(e,t,n){return Array.isArray(e)?this.schema.denormalize(e,t,n):this.schema.denormalize([e],t,n)[0]}var k=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 S=["pk","schema","key"];function A(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,r);let u=this.pk(c,t,n,r);if(u=void 0===u||""===u||"undefined"===u?`MISS-${Math.random()}`:`${u}`,a(this.key,u,e))return u;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]=s(this.schema[e],c[e],c,e,r))})),i(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=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 r=k(t,S);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 _=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)}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,r),u=this._entity.pk(c,t,n,r);return i(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(this._entity,e)}_denormalizeNullable(){return{}}_normalizeNullable(){return{}}},Collection:v,Entity:A,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)}}});const P=class{};class R extends(A(P)){static process(e,t,n,r){return super.process(e,t,n,r)}}var C;const T="undefined"!=typeof document;let z=T&&!(null!=(C=document.querySelector("meta[http-equiv='Content-Security-Policy']"))&&C.getAttribute("content"));try{z||Function()}catch(e){z=!0,T&&console.error("Content Security Policy: The previous CSP log can be safely ignored - @data-client/endpoint will use setPrototypeOf instead")}var N;class $ extends Function{constructor(e,t){let n,r;return z?(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)}}N=$,(()=>{if("undefined"!=typeof document&&document.FUNC_MANGLE){const e=N.prototype.key;N.prototype.key=function(...t){return null==document.FUNC_MANGLE||document.FUNC_MANGLE(this),this.prototype.key=e,e.call(this,...t)}}})();const K=$;function M(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(I(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 F(e,t){return function(e,t){void 0===t&&(t={});var n=L(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}}(M(e,t),t)}function I(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function L(e){return e&&e.sensitive?"":"i"}function U(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(I(void 0===d?"":d),"]|$"),m="[".concat(I(f),"]"),y=o?"^":"",g=0,b=e;g<b.length;g++){var v=b[g];if("string"==typeof v)y+=I(h(v));else{var O=I(h(v.prefix)),x=I(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,L(n))}(M(e,n),t,n)}function q(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 q(e,t,n).source}));return new RegExp("(?:".concat(r.join("|"),")"),L(n))}(e,t,n):U(e,t,n)}function D(e,t){if("object"!=typeof e||void 0===e||Array.isArray(e))return;if(e instanceof v)return t(e);const n=Object.assign({},e instanceof d?e.schema:e);for(const e in n){if(!n[e])continue;const r=D(n[e],t);if(r)return r}}function V(e,t){if("object"!=typeof e||void 0===e)return e;if(e instanceof v)return t(e);const n=Object.assign({},e instanceof d?e.schema:e);for(const e in n)n[e]&&(n[e]=V(n[e],t));return n}class W extends Error{constructor(e){super(e.statusText||`Network response not 'ok': ${e.status}`),this.name="NetworkError",this.status=e.status,this.response=e}}function J(e,t){const n=new Set(e),r=[...e];for(const e of t)n.has(e)||r.push(e);return r}const G=Object.create(null);function H(e){return e in G||(G[e]=F(e,{encode:encodeURIComponent,validate:!1})),G[e]}const B=Object.create(null);function Q(e){return e in B||(B[e]=new Set(M(e).map((e=>"string"==typeof e?e:`${e.name}`)))),B[e]}const X=Object.prototype,Y=Object.getPrototypeOf;var Z=i("hasBody");class ee extends ${constructor(e){var t;super(null!=(t=e.fetch)?t:async function(...e){const t=s(this,Z)[Z]&&e.length<2?{}:e[0]||{},n=s(this,Z)[Z]?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,Z,{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,Z)[Z]=!("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,Z)[Z]&&e.length<2?{}:e[0]||{})}`}url(e={}){const t=H(this.path)(e),n=Q(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&&Y(n)===X;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 W(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!==ee.prototype.process)return t;const n=new W(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 q(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=V(this.schema,(n=>(t=!0,function(e,t){return Object.create(t,{name:{value:`Pagination(${t.schema})`},merge:{value:J},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:D(this.schema,(e=>e.push)),name:this.name+".create"})}get unshift(){return this.extend({method:"POST",schema:D(this.schema,(e=>e.unshift)),name:this.name+".create"})}get assign(){return this.extend({method:"POST",schema:D(this.schema,(e=>e.assign)),name:this.name+".create"})}}const te=["path","schema","Endpoint","Collection","optimistic","paginationField"],{Invalidate:ne,Collection:re}=_;function se(e,t,n){return Object.assign({},t,oe(n))}function ie(e,t){return t}function oe(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=$,e.Entity=R,e.ExtendableEndpoint=K,e.INVALID=l,e.NetworkError=W,e.RestEndpoint=ee,e.createResource=function(e){let{path:t,schema:n,Endpoint:r=ee,Collection:s=re,optimistic:i,paginationField:o}=e,a=k(e,te);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=se,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,oe(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 ne(n):n,method:"DELETE",name:u("delete"),process:(e,t)=>e&&Object.keys(e).length?e:t,getOptimisticResponse:i?ie: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=H,e.getUrlTokens=Q,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=_,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.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Endpoints for REST APIs",
|
|
5
5
|
"homepage": "https://dataclient.io/rest",
|
|
6
6
|
"repository": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"resource",
|
|
23
23
|
"typescript",
|
|
24
24
|
"HTTP",
|
|
25
|
-
"path-to-
|
|
25
|
+
"path-to-regexp",
|
|
26
26
|
"path template",
|
|
27
27
|
"data fetching",
|
|
28
28
|
"data cache",
|
|
@@ -37,7 +37,9 @@
|
|
|
37
37
|
"ios",
|
|
38
38
|
"android",
|
|
39
39
|
"web",
|
|
40
|
-
"json"
|
|
40
|
+
"json",
|
|
41
|
+
"async",
|
|
42
|
+
"promise"
|
|
41
43
|
],
|
|
42
44
|
"sideEffects": false,
|
|
43
45
|
"main": "dist/index.js",
|
|
@@ -129,12 +131,11 @@
|
|
|
129
131
|
"license": "Apache-2.0",
|
|
130
132
|
"dependencies": {
|
|
131
133
|
"@babel/runtime": "^7.17.0",
|
|
132
|
-
"@data-client/endpoint": "^0.
|
|
134
|
+
"@data-client/endpoint": "^0.14.0",
|
|
133
135
|
"path-to-regexp": "^6.2.1"
|
|
134
136
|
},
|
|
135
137
|
"devDependencies": {
|
|
136
138
|
"@anansi/browserslist-config": "^1.4.2",
|
|
137
|
-
"@types/node": "^20.0.0"
|
|
138
|
-
"@types/path-to-regexp": "^1.7.0"
|
|
139
|
+
"@types/node": "^20.0.0"
|
|
139
140
|
}
|
|
140
141
|
}
|