@data-client/rest 0.14.21 → 0.14.25
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/dist/index.js +9 -9
- package/dist/index.umd.min.js +1 -1
- package/legacy/RestHelpers.js +10 -10
- package/lib/RestHelpers.d.ts.map +1 -1
- package/lib/RestHelpers.js +10 -10
- package/package.json +2 -2
- package/src/RestHelpers.ts +18 -12
package/dist/index.js
CHANGED
|
@@ -76,22 +76,22 @@ function paramsToString(searchParams) {
|
|
|
76
76
|
return params.toString();
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
const urlBaseCache =
|
|
79
|
+
const urlBaseCache = new Map();
|
|
80
80
|
function getUrlBase(path) {
|
|
81
|
-
if (!(path
|
|
82
|
-
urlBaseCache
|
|
81
|
+
if (!urlBaseCache.has(path)) {
|
|
82
|
+
urlBaseCache.set(path, pathToRegexp.compile(path, {
|
|
83
83
|
encode: encodeURIComponent,
|
|
84
84
|
validate: false
|
|
85
|
-
});
|
|
85
|
+
}));
|
|
86
86
|
}
|
|
87
|
-
return urlBaseCache
|
|
87
|
+
return urlBaseCache.get(path);
|
|
88
88
|
}
|
|
89
|
-
const urlTokensCache =
|
|
89
|
+
const urlTokensCache = new Map();
|
|
90
90
|
function getUrlTokens(path) {
|
|
91
|
-
if (!(path
|
|
92
|
-
urlTokensCache
|
|
91
|
+
if (!urlTokensCache.has(path)) {
|
|
92
|
+
urlTokensCache.set(path, new Set(pathToRegexp.parse(path).map(t => typeof t === 'string' ? t : `${t['name']}`)));
|
|
93
93
|
}
|
|
94
|
-
return urlTokensCache
|
|
94
|
+
return urlTokensCache.get(path);
|
|
95
95
|
}
|
|
96
96
|
const proto = Object.prototype;
|
|
97
97
|
const gpo = Object.getPrototypeOf;
|
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(){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 l(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 f{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 l(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),l=o(this.key);return l&&Object.keys(l).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=this.pk(c,t,n,r);if(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:f,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="",l=function(e){if(h<n.length&&n[h].type===e)return n[h++].value},f=function(e){var t=l(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=l("CHAR")||l("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(I(o),"]+?"):"(?:(?!".concat(I(n),")[^").concat(I(o),"])+?")};h<n.length;){var p=l("CHAR"),y=l("NAME"),g=l("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:l("MODIFIER")||""})}else{var b=p||l("ESCAPED_CHAR");if(b)u+=b;else if(u&&(a.push(u),u=""),l("OPEN")){v=m();var O=l("NAME")||"",w=l("PATTERN")||"",E=m();f("CLOSE"),a.push({name:O||(w?c++:""),pattern:O&&!w?d(v):w,prefix:v,suffix:E,modifier:l("MODIFIER")||""})}else f("END")}}return a}function F(e,t){return function(e,t){void 0===t&&(t={});var n=M(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 l=0;l<c.length;l++){var f=i(c[l],s);if(o&&!a[r].test(f))throw new TypeError('Expected all "'.concat(s.name,'" to match "').concat(s.pattern,'", but got "').concat(f,'"'));n+=s.prefix+f+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{f=i(String(c),s);if(o&&!a[r].test(f))throw new TypeError('Expected "'.concat(s.name,'" to match "').concat(s.pattern,'", but got "').concat(f,'"'));n+=s.prefix+f+s.suffix}}else n+=s}return n}}(N(e,t),t)}function I(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function M(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,l=n.delimiter,f=void 0===l?"/#?":l,m=n.endsWith,d="[".concat(I(void 0===m?"":m),"]|$"),p="[".concat(I(f),"]"),y=o?"^":"",g=0,v=e;g<v.length;g++){var b=v[g];if("string"==typeof b)y+=I(u(b));else{var O=I(u(b.prefix)),w=I(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,M(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("|"),")"),M(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 f?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 f?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=Object.create(null);function G(e){return e in J||(J[e]=F(e,{encode:encodeURIComponent,validate:!1})),J[e]}const H=Object.create(null);function B(e){return e in H||(H[e]=new Set(N(e).map((e=>"string"==typeof e?e:`${e.name}`)))),H[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 l(e,t,n){e[t]=e[t].extend(n)}const f=t({},c),m=t({},c),d=new i(t({},c,{path:n,schema:r,name:u("get")}));o&&(f.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({},f,{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({},f,{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({},f,{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 l(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)l(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}`}}));
|
|
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(){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}`}}));
|
package/legacy/RestHelpers.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { compile, parse } from 'path-to-regexp';
|
|
2
|
-
const urlBaseCache =
|
|
2
|
+
const urlBaseCache = new Map();
|
|
3
3
|
export function getUrlBase(path) {
|
|
4
|
-
if (!(path
|
|
5
|
-
urlBaseCache
|
|
4
|
+
if (!urlBaseCache.has(path)) {
|
|
5
|
+
urlBaseCache.set(path, compile(path, {
|
|
6
6
|
encode: encodeURIComponent,
|
|
7
7
|
validate: false
|
|
8
|
-
});
|
|
8
|
+
}));
|
|
9
9
|
}
|
|
10
|
-
return urlBaseCache
|
|
10
|
+
return urlBaseCache.get(path);
|
|
11
11
|
}
|
|
12
|
-
const urlTokensCache =
|
|
12
|
+
const urlTokensCache = new Map();
|
|
13
13
|
export function getUrlTokens(path) {
|
|
14
|
-
if (!(path
|
|
15
|
-
urlTokensCache
|
|
14
|
+
if (!urlTokensCache.has(path)) {
|
|
15
|
+
urlTokensCache.set(path, new Set(parse(path).map(t => typeof t === 'string' ? t : `${t['name']}`)));
|
|
16
16
|
}
|
|
17
|
-
return urlTokensCache
|
|
17
|
+
return urlTokensCache.get(path);
|
|
18
18
|
}
|
|
19
19
|
const proto = Object.prototype;
|
|
20
20
|
const gpo = Object.getPrototypeOf;
|
|
@@ -32,4 +32,4 @@ export function shortenPath(path) {
|
|
|
32
32
|
if (shortUrlRoot[shortUrlRoot.length - 1] === '/') shortUrlRoot = shortUrlRoot.substring(0, shortUrlRoot.length - 1);
|
|
33
33
|
return shortUrlRoot;
|
|
34
34
|
}
|
|
35
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
35
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJjb21waWxlIiwicGFyc2UiLCJ1cmxCYXNlQ2FjaGUiLCJNYXAiLCJnZXRVcmxCYXNlIiwicGF0aCIsImhhcyIsInNldCIsImVuY29kZSIsImVuY29kZVVSSUNvbXBvbmVudCIsInZhbGlkYXRlIiwiZ2V0IiwidXJsVG9rZW5zQ2FjaGUiLCJnZXRVcmxUb2tlbnMiLCJTZXQiLCJtYXAiLCJ0IiwicHJvdG8iLCJPYmplY3QiLCJwcm90b3R5cGUiLCJncG8iLCJnZXRQcm90b3R5cGVPZiIsImlzUG9qbyIsIm9iaiIsInNob3J0ZW5QYXRoIiwibGFzdENvbG9uSW5kZXgiLCJsYXN0SW5kZXhPZiIsIkVycm9yIiwic2hvcnRVcmxSb290Iiwic3Vic3RyaW5nIiwibGVuZ3RoIl0sInNvdXJjZXMiOlsiLi4vc3JjL1Jlc3RIZWxwZXJzL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ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAE9D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,
|
|
1
|
+
{"version":3,"file":"RestHelpers.d.ts","sourceRoot":"","sources":["../src/RestHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAE9D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAWrD;AAGD,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAUtD;AAKD,wBAAgB,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAK/D;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAYrE"}
|
package/lib/RestHelpers.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { compile, parse } from 'path-to-regexp';
|
|
2
|
-
const urlBaseCache =
|
|
2
|
+
const urlBaseCache = new Map();
|
|
3
3
|
export function getUrlBase(path) {
|
|
4
|
-
if (!(path
|
|
5
|
-
urlBaseCache
|
|
4
|
+
if (!urlBaseCache.has(path)) {
|
|
5
|
+
urlBaseCache.set(path, compile(path, {
|
|
6
6
|
encode: encodeURIComponent,
|
|
7
7
|
validate: false
|
|
8
|
-
});
|
|
8
|
+
}));
|
|
9
9
|
}
|
|
10
|
-
return urlBaseCache
|
|
10
|
+
return urlBaseCache.get(path);
|
|
11
11
|
}
|
|
12
|
-
const urlTokensCache =
|
|
12
|
+
const urlTokensCache = new Map();
|
|
13
13
|
export function getUrlTokens(path) {
|
|
14
|
-
if (!(path
|
|
15
|
-
urlTokensCache
|
|
14
|
+
if (!urlTokensCache.has(path)) {
|
|
15
|
+
urlTokensCache.set(path, new Set(parse(path).map(t => typeof t === 'string' ? t : `${t['name']}`)));
|
|
16
16
|
}
|
|
17
|
-
return urlTokensCache
|
|
17
|
+
return urlTokensCache.get(path);
|
|
18
18
|
}
|
|
19
19
|
const proto = Object.prototype;
|
|
20
20
|
const gpo = Object.getPrototypeOf;
|
|
@@ -32,4 +32,4 @@ export function shortenPath(path) {
|
|
|
32
32
|
if (shortUrlRoot[shortUrlRoot.length - 1] === '/') shortUrlRoot = shortUrlRoot.substring(0, shortUrlRoot.length - 1);
|
|
33
33
|
return shortUrlRoot;
|
|
34
34
|
}
|
|
35
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
35
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJjb21waWxlIiwicGFyc2UiLCJ1cmxCYXNlQ2FjaGUiLCJNYXAiLCJnZXRVcmxCYXNlIiwicGF0aCIsImhhcyIsInNldCIsImVuY29kZSIsImVuY29kZVVSSUNvbXBvbmVudCIsInZhbGlkYXRlIiwiZ2V0IiwidXJsVG9rZW5zQ2FjaGUiLCJnZXRVcmxUb2tlbnMiLCJTZXQiLCJtYXAiLCJ0IiwicHJvdG8iLCJPYmplY3QiLCJwcm90b3R5cGUiLCJncG8iLCJnZXRQcm90b3R5cGVPZiIsImlzUG9qbyIsIm9iaiIsInNob3J0ZW5QYXRoIiwibGFzdENvbG9uSW5kZXgiLCJsYXN0SW5kZXhPZiIsIkVycm9yIiwic2hvcnRVcmxSb290Iiwic3Vic3RyaW5nIiwibGVuZ3RoIl0sInNvdXJjZXMiOlsiLi4vc3JjL1Jlc3RIZWxwZXJzLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNvbXBpbGUsIFBhdGhGdW5jdGlvbiwgcGFyc2Ugf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UFTO0FBQzlCLE1BQU1DLEdBQUcsR0FBR0YsTUFBTSxDQUFDRyxjQUFjO0FBRWpDLE9BQU8sU0FBU0MsTUFBTUEsQ0FBQ0MsR0FBWSxFQUE4QjtFQUMvRCxJQUFJQSxHQUFHLEtBQUssSUFBSSxJQUFJLE9BQU9BLEdBQUcsS0FBSyxRQUFRLEVBQUU7SUFDM0MsT0FBTyxLQUFLO0VBQ2Q7RUFDQSxPQUFPSCxHQUFHLENBQUNHLEdBQUcsQ0FBQyxLQUFLTixLQUFLO0FBQzNCO0FBRUEsT0FBTyxTQUFTTyxXQUFXQSxDQUFtQm5CLElBQU8sRUFBa0I7RUFDckUsTUFBTW9CLGNBQWMsR0FBR3BCLElBQUksQ0FBQ3FCLFdBQVcsQ0FBQyxHQUFHLENBQUM7RUFDNUMsSUFBSUQsY0FBYyxLQUFLLENBQUMsQ0FBQyxFQUN2QixNQUFNLElBQUlFLEtBQUssQ0FBQyxnREFBZ0QsQ0FBQztFQUNuRTtFQUNBLElBQUlDLFlBQTRCLEdBQUd2QixJQUFJLENBQUN3QixTQUFTLENBQUMsQ0FBQyxFQUFFSixjQUFjLENBQVE7RUFDM0UsSUFBSUcsWUFBWSxDQUFDQSxZQUFZLENBQUNFLE1BQU0sR0FBRyxDQUFDLENBQUMsS0FBSyxHQUFHLEVBQy9DRixZQUFZLEdBQUdBLFlBQVksQ0FBQ0MsU0FBUyxDQUNuQyxDQUFDLEVBQ0RELFlBQVksQ0FBQ0UsTUFBTSxHQUFHLENBQ3hCLENBQW1CO0VBQ3JCLE9BQU9GLFlBQVk7QUFDckIiLCJpZ25vcmVMaXN0IjpbXX0=
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-client/rest",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.25",
|
|
4
4
|
"description": "Quickly define typed REST resources and endpoints",
|
|
5
5
|
"homepage": "https://dataclient.io/rest",
|
|
6
6
|
"repository": {
|
|
@@ -135,7 +135,7 @@
|
|
|
135
135
|
"license": "Apache-2.0",
|
|
136
136
|
"dependencies": {
|
|
137
137
|
"@babel/runtime": "^7.20.0",
|
|
138
|
-
"@data-client/endpoint": "^0.14.
|
|
138
|
+
"@data-client/endpoint": "^0.14.25",
|
|
139
139
|
"path-to-regexp": "^6.3.0"
|
|
140
140
|
},
|
|
141
141
|
"devDependencies": {
|
package/src/RestHelpers.ts
CHANGED
|
@@ -2,25 +2,31 @@ import { compile, PathFunction, parse } from 'path-to-regexp';
|
|
|
2
2
|
|
|
3
3
|
import { ShortenPath } from './pathTypes.js';
|
|
4
4
|
|
|
5
|
-
const urlBaseCache:
|
|
5
|
+
const urlBaseCache: Map<string, PathFunction<object>> = new Map();
|
|
6
6
|
export function getUrlBase(path: string): PathFunction {
|
|
7
|
-
if (!(path
|
|
8
|
-
urlBaseCache
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
if (!urlBaseCache.has(path)) {
|
|
8
|
+
urlBaseCache.set(
|
|
9
|
+
path,
|
|
10
|
+
compile(path, {
|
|
11
|
+
encode: encodeURIComponent,
|
|
12
|
+
validate: false,
|
|
13
|
+
}),
|
|
14
|
+
);
|
|
12
15
|
}
|
|
13
|
-
return urlBaseCache
|
|
16
|
+
return urlBaseCache.get(path) as PathFunction;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
const urlTokensCache:
|
|
19
|
+
const urlTokensCache: Map<string, Set<string>> = new Map();
|
|
17
20
|
export function getUrlTokens(path: string): Set<string> {
|
|
18
|
-
if (!(path
|
|
19
|
-
urlTokensCache
|
|
20
|
-
|
|
21
|
+
if (!urlTokensCache.has(path)) {
|
|
22
|
+
urlTokensCache.set(
|
|
23
|
+
path,
|
|
24
|
+
new Set(
|
|
25
|
+
parse(path).map(t => (typeof t === 'string' ? t : `${t['name']}`)),
|
|
26
|
+
),
|
|
21
27
|
);
|
|
22
28
|
}
|
|
23
|
-
return urlTokensCache
|
|
29
|
+
return urlTokensCache.get(path) as Set<string>;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
const proto = Object.prototype;
|