UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

2 lines 9.18 kB
'use strict';var _=require('qs');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var ___default=/*#__PURE__*/_interopDefault(_);var K=Object.defineProperty,B=Object.defineProperties;var F=Object.getOwnPropertyDescriptors;var M=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var U=(t,e,n)=>e in t?K(t,e,{enumerable:true,configurable:true,writable:true,value:n}):t[e]=n,h=(t,e)=>{for(var n in e||(e={}))v.call(e,n)&&U(t,n,e[n]);if(M)for(var n of M(e))W.call(e,n)&&U(t,n,e[n]);return t},$=(t,e)=>B(t,F(e));function T(...t){if(t.length===1)return Array.isArray(t[0])?t[0]:[];let e=null;for(var n in t){let r=t[n];if(Array.isArray(r)){if(r.length)return r;e||(e=r);}}return e||[]}function l(t){return !!(t&&typeof t=="string")}function p(t){return typeof t=="number"&&!isNaN(t)}function A(t){return !t||typeof t!="object"?false:t instanceof Date?true:typeof t.getTime!="function"?false:!(Object.prototype.toString.call(t)!=="[object Date]"||isNaN(t.getTime()))}function P(t){return typeof window!="object"||!window||typeof document=="undefined"||typeof HTMLElement=="undefined"?false:t===document?true:"HTMLElement"in window?!!t&&t instanceof HTMLElement:!!t&&typeof t=="object"&&t.nodeType===1&&!!t.nodeName}function x(t){return t==null||typeof t=="string"||typeof t=="number"||typeof t=="boolean"}function R(t){if(t instanceof RegExp)return true;if(!t||typeof t!="object"||!Object.prototype.toString.call(t).includes("RegExp"))return false;try{return new RegExp(t),!0}catch(e){return false}}function u(t){if(t===null||typeof t!="object"||P(t)||A(t)||R(t)||x(t))return false;let e=Object.getPrototypeOf(t);if(e===null)return true;let n=e.constructor;if(typeof n!="function")return false;if(n===Object)return true;let r=n.prototype;return typeof r!="object"?false:r===Object.prototype?true:typeof e.hasOwnProperty=="function"&&e.hasOwnProperty("isPrototypeOf")&&typeof e.isPrototypeOf=="function"}function w(t){if(Array.isArray(t)){let r=[];for(var e=0;e<t.length;e++)r[e]=w(t[e]);return r}else if(u(t)&&t){let r={};for(var n in t)t.hasOwnProperty(n)&&(r[n]=w(t[n]));return r}else return t}Object.getSize=function(t,e=false){if(!t||typeof t!="object")return 0;if(Array.isArray(t))return t.length;typeof e!="boolean"&&(e=false);let n=0;for(let r in t)if(t.hasOwnProperty(r)&&(n++,e===true))return n;return n};function d(t,...e){let n=Array.isArray(t),r=u(t);(t==null||!n&&!r)&&(t={});for(let i=0;i<e.length;i++){let s=e[i];if(s==null)continue;let o=u(s),c=Array.isArray(s);if(!(!o&&!c)){if(n){c&&z(t,s);continue}else if(c)continue;for(let a in s){let f=s[a];if(f==null)continue;if(f===s){t[a]=t;continue}let y=t[a],m=Array.isArray(y),g=Array.isArray(f);if(m){g?z(t[a],f):t[a]=f;continue}else if(!u(y)){t[a]=f;continue}if(g||!u(f)){t[a]=f;continue}t[a]=d({},y,f);}}}return t}var z=(t,e)=>{let n=e.length,r=0;for(let i=0;i<t.length;i++){let s=t[i],o=e[i];if(i<n){let c=Array.isArray(s),a=Array.isArray(o),f=u(s),y=u(o);c&&a||f&&y?(t[r]=d(c?[]:{},s,o),r++):o!=null?(t[r]=o,r++):s!=null&&(t[r]=s,r++);}}for(let i=t.length;i<n;i++)e[i]!==void 0&&e[i]!==null&&t.push(e[i]);return t};function V(t,e){return b(t,"",{},e)}function b(t,e="",n={},r){if(n=u(n)?n:{},x(t)||A(t)||R(t)||Array.isArray(t)&&(r!=null&&r.skipArrays))return e&&(n[e]=t),n;if(typeof t=="function"||typeof t=="object"&&!u(t)&&!H(t))return n;if(t instanceof Map||t instanceof WeakMap)return Array.from(t.entries()).forEach(([i,s])=>{let o=e?`${e}[${String(i)}]`:String(i);b(s,o,n,r);}),n;if(Array.isArray(t)||t instanceof Set||t instanceof WeakSet)return (Array.isArray(t)?t:Array.from(t)).forEach((s,o)=>{let c=e?`${e}[${o}]`:String(o);b(s,c,n,r);}),n;if(u(t))for(let i in t){if(!Object.prototype.hasOwnProperty.call(t,i))continue;let s=t[i],o=e?e.endsWith("]")?`${e}.${i}`:`${e}.${i}`:i;b(s,o,n,r);}return n}function H(t){return Array.isArray(t)||t instanceof Set||t instanceof Map||t instanceof WeakMap||t instanceof WeakSet}function Z(t){return Object.entries(t)}Object.typedEntries=Z;Object.flatten=V;Object.clone=w;var O=function(t,e=""){if(!t||typeof t!="string")return "";if(!e||typeof e!="string")return t.trim();let n=t.length;for(;t.startsWith(e)&&n>=0;)t=t.slice(e.length),--n;return t.toString()},N=function(t,e=""){if(!t||typeof t!="string")return "";if(!e||typeof e!="string")return t.trim();let n=t.length;for(;t.endsWith(e)&&n>=0;)t=t.slice(0,-e.length),--n;return t.toString()},I=(t,e=true)=>typeof t!="string"?false:e!==false?/^\d*\.?\d+$/.test(t):/^\d+$/.test(t);String.prototype.ltrim=function(t){return O(this.toString(),t)};String.prototype.rtrim=function(t){return N(this.toString(),t)};String.prototype.isNumber=function(t=true){return I(this.toString(),t)};String.prototype.replaceAll=function(t,e){return !l(t)||!l(e)?this.toString():this.toString().split(t).join(e)};var G=t=>l(t)?(t=t.trim(),t.replace(/(.)([A-Z][a-z]+)/,"$1_$2").replace(/([a-z0-9])([A-Z])/,"$1_$2").toUpperCase()):"";String.prototype.toSnakeCase=function(){return G(this.toString())};var J=t=>l(t)?(t=t.trim(),t.charAt(0)+t.replace(/(_\w)/g,e=>e[1].toUpperCase()).substring(1)):"";String.prototype.toCamelCase=function(){return J(this.toString())};var X=function(t){return !t||typeof t!="string"?"":(t=t.trim(),t.charAt(0).toUpperCase()+t.slice(1))};String.prototype.upperFirst=function(){return X(this.toString())};var Y=function(t){return !t||typeof t!="string"?"":(t=t.trim(),t.charAt(0).toLowerCase()+t.slice(1))};String.prototype.lowerFirst=function(){return Y(this.toString())};var q=(t,e=true)=>{if(typeof t!="string")return "";let n=tt(t);return t=typeof n.search=="string"?n.search:"",e&&t?"?"+O(t,"?"):(t=N(O(t.trim(),"?"),"?"),t)},Q=function(t,e={}){return typeof t!="string"?{}:___default.default.parse(q(t,false),h({allowSparse:true},Object.assign({},e)))};var tt=t=>{if(typeof t!="string")return {};if(typeof URL!="undefined"&&URL&&et(t))try{return new URL(t)}catch(r){}t=nt(t)?decodeURIComponent(t):t;var e=t.match(/^(([^:\/?#]+:)?(?:\/\/((?:([^\/?#:]*):([^\/?#:]*)@)?([^\/?#:]*)(?::([^\/?#:]*))?)))?([^?#]*)(\?[^#]*)?(#.*)?$/);let n=e?{hash:e[10]||"",host:e[3]||"",hostname:e[6]||"",href:e[0]||"",origin:e[1]||"",pathname:e[8]||(e[1]?"/":""),port:e[7]||"",protocol:e[2]||"",search:e[9]||"",username:e[4]||"",password:e[5]||""}:{};return n.protocol&&n.protocol.length==2&&(n.protocol="file:///"+n.protocol.toUpperCase(),n.origin=n.protocol+"//"+n.host),n.protocol&&(n.href=n.origin+n.pathname+n.search+n.hash),n},et=t=>{if(!l(t)||!t.trim())return false;if(/^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/.test(t))return true;if(typeof URL!="undefined"&&URL)try{let e=new URL(t);return e.protocol==="http:"||e.protocol==="https:"}catch(e){return false}return false},nt=t=>{if(!l(t)||!t.includes("%"))return false;let e=/%[0-9A-Fa-f]{2}/g;if(!t.match(e))return false;try{let r=decodeURIComponent(t);if(r!==t)return !0;try{if(decodeURIComponent(r)!==r)return !0}catch(i){return !0}}catch(r){return false}return false};var L=class t{static normalizePagination(e){let{page:n,skip:r,limit:i}=Object.assign({},e);return p(i)?(p(r)&&r>0?n=Math.floor(r/i)+1:p(n)&&n>0?r=(n-1)*i:(n=1,r=0),{page:n,skip:r,limit:i}):{}}static normalizeOrderBy(e){let n=Array.isArray(e)?e:l(e)?[e]:[],r={};return n.forEach(i=>{if(!l(i)||String(i).trim()==="")return;let s=String(i).startsWith("-"),o=s?"desc":"asc",c=s?String(i).slice(1):i,a=c;r[a]!==void 0&&console.warn(`Duplicate orderBy field: ${c}. Using last occurrence.`),r[a]=o;}),r}static canPaginateResult(e){return u(e)?p(e.limit):false}static getPaginationMetaData(e,n){let{limit:r,page:i,skip:s}=t.normalizePagination(n);e=p(e)?e:0;let o={total:e};return typeof r=="number"&&r>0&&typeof i=="number"&&i>=0&&(o.currentPage=i,o.pageSize=r,o.totalPages=Math.ceil(e/r),o.hasNextPage=o.currentPage<o.totalPages,o.hasPreviousPage=o.currentPage>1,o.nextPage=o.hasNextPage?o.currentPage+1:void 0,o.previousPage=o.hasPreviousPage?o.currentPage-1:void 0,o.lastPage=o.totalPages),o}static paginate(e,n,r){let i=t.getPaginationMetaData(n,r),{currentPage:s,pageSize:o,totalPages:c}=i;if(Array.isArray(e)&&p(s)&&p(o)&&p(c)){let a=Math.max(0,s-1)*o,f=a+o;e=e.slice(a,f);}return {data:e,total:n,meta:i}}static parseQueryOptions(e){var E;let n=d({},e==null?void 0:e.params,Q(e==null?void 0:e.url)),r=d({},n,(E=e==null?void 0:e.headers)==null?void 0:E["x-filters"],e==null?void 0:e.filters),i=D(r.limit),s=D(r.skip),o=D(r.page),c=t.normalizePagination({limit:i,skip:s,page:o}),a=r.distinct;typeof a=="number"&&(a=!!a),(typeof a=="boolean"||Array.isArray(a)&&a.length)&&(c.distinct=a);let f=r.orderBy,y=t.normalizeOrderBy(f);y&&Object.getSize(y,true)>0&&(c.orderBy=y);let m=T(r.include);m.length&&(c.include=m);let g=r.cache;g!==void 0&&(c.cache=!!g);let j=r.cacheTTL;j!==void 0&&(c.cacheTTL=j);let S=rt(r.where);u(S)&&Object.getSize(S,true)&&(c.where=S);let C=r.includeDeleted;typeof C=="boolean"&&(c.includeDeleted=C);let k=T(r.relations);return k.length&&(c.relations=k),$(h({},c),{queryParams:n})}},rt=(...t)=>{for(let e of t)if(Array.isArray(e)&&e.length||l(e)||u(e)&&Object.getSize(e,true))return e},D=t=>{if(I(t))return Number(t);if(typeof t=="number")return t}; exports.ResourcePaginationHelper=L;