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

1 lines 11.3 kB
'use strict';var fields=require('../fields'),index=require('../utils/index'),observable=require('../observable'),K=require('../auth'),c=require('../i18n'),ResourcePaginationHelper=require('./ResourcePaginationHelper'),S=require('../logger');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var K__default=/*#__PURE__*/_interopDefault(K);var c__default=/*#__PURE__*/_interopDefault(c);var S__default=/*#__PURE__*/_interopDefault(S);var N=Object.defineProperty;var m=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var I=(s,e,t)=>e in s?N(s,e,{enumerable:true,configurable:true,writable:true,value:t}):s[e]=t,T=(s,e)=>{for(var t in e||(e={}))M.call(e,t)&&I(s,t,e[t]);if(m)for(var t of m(e))v.call(e,t)&&I(s,t,e[t]);return s};var n=(s,e,t)=>new Promise((r,a)=>{var d=l=>{try{g(t.next(l));}catch(R){a(R);}},A=l=>{try{g(t.throw(l));}catch(R){a(R);}},g=l=>l.done?r(l.value):Promise.resolve(l.value).then(d,A);g((t=t.apply(s,e)).next());});const D=Symbol("resources"),P=Symbol("resourceFromClassName"),p=class p extends observable.ObservableClass{constructor(){super(),this._onDictionaryChangedListener=c__default.default.on("translations-changed",this.onI18nChange.bind(this)),this._onLocaleChangeListener=c__default.default.on("locale-changed",this.onI18nChange.bind(this)),this.init();}getMetaData(){return Object.assign({},Reflect.getMetadata(h.resourceMetaData,this.constructor))}onI18nChange(){this.resolveTranslations();}resolveTranslations(e){return c__default.default.resolveTranslations(this)}destroy(){var e,t;(e=this._onDictionaryChangedListener)==null||e.remove(),(t=this._onLocaleChangeListener)==null||t.remove(),this.offAll();}getTranslateParams(e){return T({resourceLabel:this.getLabel(),resourceName:this.getName()},Object.assign({},e))}get INVALID_DATA_PROVIDER_ERROR(){return c__default.default.t("resources.invalidDataProvider",this.getTranslateParams())}hasDataService(){const e=this.getDataService();return e!=null&&typeof(e==null?void 0:e.update)=="function"&&typeof(e==null?void 0:e.create)=="function"&&typeof(e==null?void 0:e.find)=="function"&&typeof(e==null?void 0:e.update)=="function"&&typeof(e==null?void 0:e.delete)=="function"}_trigger(e,...t){return p.events.trigger(e,Object.assign({},this.getTranslateParams()),...t),this.trigger(e,...t)}checkPermissionAction(e,t){return this.hasDataService()?Promise.resolve():Promise.reject(new Error(this.INVALID_DATA_PROVIDER_ERROR))}find(e){return this.checkPermissionAction(this.canUserRead.bind(this),"resources.readForbiddenError").then(()=>{var t;return (t=this.getDataService())==null?void 0:t.find(e).then(r=>(this._trigger("find",r),r))})}findOne(e){return this.checkPermissionAction(this.canUserRead.bind(this),"resources.readForbiddenError").then(()=>this.getDataService().findOne(e).then(t=>(this._trigger("findOne",t),t)))}findOneOrFail(e){return n(this,null,function*(){const t=yield this.findOne(e);if(!index.isObj(t)||!t)throw new Error(c__default.default.t("resources.notFoundError",Object.assign({},{options:JSON.stringify(e)},this.getTranslateParams())));return t})}beforeCreate(e){return n(this,null,function*(){})}afterCreate(e){return n(this,null,function*(){})}beforeUpdate(e,t){return n(this,null,function*(){})}afterUpdate(e,t,r){return n(this,null,function*(){})}beforeDelete(e){return n(this,null,function*(){})}afterDelete(e,t){return n(this,null,function*(){})}beforeCreateMany(e){return n(this,null,function*(){})}afterCreateMany(e,t){return n(this,null,function*(){})}beforeUpdateMany(e,t){return n(this,null,function*(){})}afterUpdateMany(e,t,r){return n(this,null,function*(){})}beforeDeleteMany(e){return n(this,null,function*(){})}afterDeleteMany(e,t){return n(this,null,function*(){})}create(e){return this.checkPermissionAction(this.canUserCreate.bind(this),"resources.createForbiddenError").then(()=>this.beforeCreate(e).then(()=>this.getDataService().create(e).then(t=>this.afterCreate(t).then(()=>(this._trigger("create",t),t)))))}update(e,t){return this.checkPermissionAction(this.canUserUpdate.bind(this),"resources.updateForbiddenError").then(()=>n(this,null,function*(){return this.beforeUpdate(e,t).then(()=>{var r;return (r=this.getDataService())==null?void 0:r.update(e,t).then(a=>this.afterUpdate(a,e,t).then(()=>(this._trigger("update",a,e,t),a)))})}))}delete(e){return this.checkPermissionAction(this.canUserDelete.bind(this),"resources.deleteForbiddenError").then(()=>this.beforeDelete(e).then(()=>{var t;return (t=this.getDataService())==null?void 0:t.delete(e).then(r=>this.afterDelete(r,e).then(()=>(this._trigger("delete",r,e),r)))}))}findAndCount(e){return this.checkPermissionAction(this.canUserRead.bind(this),"resources.readForbiddenError").then(()=>this.getDataService().findAndCount(e).then(t=>(this._trigger("findAndCount",t),t)))}findAndPaginate(e){return n(this,null,function*(){e=Object.assign({},e);const[t,r]=yield this.findAndCount(e);return ResourcePaginationHelper.ResourcePaginationHelper.paginate(t,r,e)})}createMany(e){return this.checkPermissionAction(this.canUserCreate.bind(this),"resources.createForbiddenError").then(()=>this.beforeCreateMany(e).then(()=>this.getDataService().createMany(e).then(t=>this.afterCreateMany(t,e).then(()=>(this._trigger("createMany",t,e),t)))))}updateMany(e,t){return this.checkPermissionAction(this.canUserUpdate.bind(this),"resources.updateForbiddenError").then(()=>this.beforeUpdateMany(e,t).then(()=>this.getDataService().updateMany(e,t).then(r=>this.afterUpdateMany(r,e,t).then(()=>(this._trigger("updateMany",r,e,t),r)))))}deleteMany(e){return this.checkPermissionAction(this.canUserDelete.bind(this),"resources.deleteForbiddenError").then(()=>this.beforeDeleteMany(e).then(()=>this.getDataService().deleteMany(e).then(t=>this.afterDeleteMany(t,e).then(()=>(this._trigger("deleteMany",t,e),t)))))}count(e){return this.checkPermissionAction(this.canUserRead.bind(this),"resources.readForbiddenError").then(()=>this.getDataService().count(e).then(t=>(this._trigger("read",t),t)))}exists(e){return this.checkPermissionAction(this.canUserRead.bind(this),"resources.readForbiddenError").then(()=>this.getDataService().exists(e).then(t=>(this._trigger("exits",t),t)))}updateMetadata(e){e=Object.assign({},e);const t=index.extendObj({},this.getMetaData(),e);return Reflect.defineMetadata(h.resourceMetaData,t,this.constructor),t}init(){this.resolveTranslations(),this.getFields();}getTranslations(e){e=index.defaultStr(e,c__default.default.getLocale());const t=String(this.getName()).trim();if(!index.isNonNullString(t))return {};const r=c__default.default.getNestedTranslation(["resources",t],e);return index.isObj(r)&&r?r:{}}translate(e,t){const r=index.isNonNullString(e)?e.trim().split("."):Array.isArray(e)?e:[];return r[0]!=="resources"&&!h.hasResource(r[1])&&r.unshift(this.getName(),"resources"),c__default.default.translate(r,t)}getName(){return this.getResourceName()}getResourceName(){return index.defaultStr(this.getMetaData().name,this.name)}getActions(){return (!index.isObj(this.actions)||!this.actions)&&(this.actions={}),this.actions}hasAction(e){if(!index.isNonNullString(e))return false;const t=this.getActions();return index.isObj(t[e])&&t[e]!==void 0}isAllowed(e,t){const r=[];return index.isNonNullString(e)?r.push([this.getName(),e]):Array.isArray(e)&&e.forEach(a=>{index.isNonNullString(a)&&r.push([this.getName(),a]);}),K__default.default.isAllowed(r,t)}canUserRead(e){return this.isAllowed("read",e)}canUserCreate(e){return this.isAllowed("create",e)}canUserUpdate(e){return this.isAllowed("update",e)}canUserDelete(e){return this.isAllowed("delete",e)}translateProperty(e,t,r){e=index.defaultStr(e).trim(),r=Object.assign({},{resourceName:this.getName()},r);const a=this.getTranslations();if(index.isNonNullString(e)&&a[e]){const d=index.stringify(c__default.default.t(e,r));if(index.isNonNullString(d)&&d.includes("."+e.ltrim(".")))return d}return index.defaultStr(t,e)}getLabel(){const e=this.getMetaData().label,t=this.translateProperty("label",index.defaultStr(e,this.label,this.getName()));return String(t).toLowerCase().trim()!="label"?t:index.defaultStr(this.getName())}getTitle(){return this.translateProperty("title",index.defaultStr(this.getMetaData().title,this.title,this.getLabel()))}getFields(){try{return this.resolveTranslations(),this.fields=fields.getFields(this),this.fields}catch(e){S__default.default.log(e," getting resources fieldss");}return {}}sprintf(e,t){let r=index.defaultStr(e);if(e&&index.isObj(t)&&t)for(let a in t)index.isEmpty(t[a])||(r=r.replaceAll(`{${a}}`,index.stringify(t[a])));return r}getActionLabel(e,t){var r;return this.sprintf((r=this.getAction(e))==null?void 0:r.label,t)}getActionTitle(e,t){var r;return this.sprintf((r=this.getAction(e))==null?void 0:r.title,t)}getAction(e){if(!index.isNonNullString(e))return {};const t=this.getActions();return index.isObj(t[e])&&t[e]||{}}getPrimaryKeys(){const e=[];if(index.isObj(this.fields))for(let t in this.fields)index.isObj(this.fields[t])&&this.fields[t].primaryKey&&e.push(this.fields[t]);return e}};p.events=observable.observableFactory();let f=p;const o=class o{static getAllMetaData(){return Object.assign({},Reflect.getMetadata(D,o))}static addMetaData(e,t){const r=this.getAllMetaData();if(!(!index.isNonNullString(e)||!e)&&(t=Object.assign({},t),t.name=index.isNonNullString(t==null?void 0:t.name)?t.name:e,r[e]=t,Reflect.defineMetadata(D,r,o),index.isNonNullString(t.className))){const a=o.getAllClassNames();a[t.className]=t.name,Reflect.defineMetadata(P,a,o);}}static getAllClassNames(){return Object.assign({},Reflect.getMetadata(P,o))}static getNameFromClassName(e){return index.isNonNullString(e)?this.getAllClassNames()[e]:void 0}static getMetaDataFromName(e){const t=this.getAllMetaData();if(!(!index.isNonNullString(e)||!e))return t[e]}static getMetaDataFromTarget(e){return Object.assign({},Reflect.getMetadata(o.resourceMetaData,e.prototype))}static getMetaDataByClassName(e){const t=this.getNameFromClassName(e);if(t)return this.getMetaDataFromName(t)}static getAllNames(){return Object.keys(this.resources)}static getResource(e){return typeof e=="string"&&e?this.resources[e]:null}static hasResource(e){if(!index.isNonNullString(e)||!e)return false;const t=o.getMetaDataFromName(e);return index.isObj(t)&&(t==null?void 0:t.name)==e?true:!!this.getResource(e)}static addResource(e,t){typeof e=="string"&&e&&t&&t instanceof f&&(this.resources[e]=t);}static removeResource(e){return typeof e=="string"&&delete this.resources[e],this.resources}static getResources(){return this.resources}};o.resourceMetaData=Symbol("resource"),o.resources={};let h=o;function pe(s){return function(e){if(s=Object.assign({},s),s.className=index.defaultStr(s.className,e==null?void 0:e.name),typeof e=="function"&&s!=null&&s.instanciate)try{const t=new e;t.updateMetadata(s),h.addResource(s.name,t);}catch(t){}Reflect.defineMetadata(h.resourceMetaData,s,e),h.addMetaData(s.name,s);}}exports.Resource=f;exports.ResourceMetadata=pe;exports.ResourcesManager=h;Object.keys(ResourcePaginationHelper).forEach(function(k){if(k!=='default'&&!Object.prototype.hasOwnProperty.call(exports,k))Object.defineProperty(exports,k,{enumerable:true,get:function(){return ResourcePaginationHelper[k]}})});