diff --git a/dist/browser-store.cjs.js b/dist/browser-store.cjs.js index 48338a6..c5fd650 100644 --- a/dist/browser-store.cjs.js +++ b/dist/browser-store.cjs.js @@ -1,2 +1,2 @@ -"use strict";function y(t){let e=this,o=e.export("entity/init"),u=t.handleResponse,s=["save","load","list","remove"].reduce((n,a)=>(n[a]=u[a]||u.any,n),{});const b=[];function m(n,a,d){let r={},i=d.apimsg;for(let l in i){let f=i[l];typeof f=="function"?r[l]=f(n,a,d):r[l]=JSON.parse(JSON.stringify(f))}return r}let c={name:"BrowserStore",save:function(n,a,d){let r=t.debug&&v(arguments),i=t.prepareCtx(n),l=m(n,i,t);r&&w(r,i,l),this.act(l,function(g,_,p){return r&&h(r,arguments),s.save(this,i,a,g,_,p,r)})},load:function(n,a,d){let r=t.debug&&v(arguments),i=t.prepareCtx(n),l=m(n,i,t);r&&w(r,i,l),this.act(l,function(g,_,p){return r&&h(r,arguments),s.load(this,i,a,g,_,p,r)})},list:function(n,a,d){let r=t.debug&&v(arguments),i=t.prepareCtx(n),l=m(n,i,t);r&&w(r,i,l),this.act(l,function(g,_,p){return r&&h(r,arguments),s.list(this,i,a,g,_,p,r)})},remove:function(n,a,d){let r=t.debug&&v(arguments),i=t.prepareCtx(n),l=m(n,i,t);r&&w(r,i,l),this.act(l,function(g,_,p){return r&&h(r,arguments),s.remove(this,i,a,g,_,p,r)})},close:function(n,a){a()},native:function(n,a){a()}},$=o(e,t,c);function v(n){let a=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return a&&b.push(a),a}function w(n,a,d){return n.apistart=Date.now(),n.ctx=a,n.apimsg=d,n}function h(n,a){return n.apiend=Date.now(),n.err=a[0],n.res=a[1],n.apimeta=a[2],n}return{name:c.name,tag:$.tag,exports:{makeApiMsg:m,msglog:b}}}function S(t,e){return t.entity(e.zone,e.base,e.name).canon$()}y.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let o=t.q;e.store=o.store$!==!1,delete o.store$,e.cmd=t.cmd;let u=t.ent||t.qent;if(u){if(u.canon$)Object.assign(e,u.canon$({object:!0}));else if(u.entity$){let s=u.entity$.split("/");Object.assign(e,{zone:s[0]==="-"?null:s[0],base:s[1]==="-"?null:s[1],name:s[2]==="-"?null:s[2]})}}return e},handleResponse:{any:function(t,e,o,u,s,b,m){if(m&&(m.end=Date.now()),u)return o(u);if(s&&s.ok)return o(s.ent);{let c=s&&s.err;return c=c||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown error`),o(c)}},list:function(t,e,o,u,s,b,m){if(m&&(m.end=Date.now()),u&&o(u),s&&s.ok&&s.list){let c=t.entity({zone:e.zone,base:e.base,name:e.name}),$=s.list.map(v=>c.make$().data$(v));m&&(m.end=Date.now()),o($)}else{let c=s&&s.err;c=c||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown list error`),o(c)}}}};Object.defineProperty(y,"name",{value:"BrowserStore"});module.exports=y; +"use strict";function y(t){let e=this,m=e.export("entity/init"),u=t.handleResponse,a=["save","load","list","remove"].reduce((n,i)=>(n[i]=u[i]||u.any,n),{});const w=[];function c(n,i,f){let r={},l=f.apimsg;for(let s in l){let g=l[s];typeof g=="function"?r[s]=g(n,i,f):r[s]=JSON.parse(JSON.stringify(g))}return r}let d={name:"BrowserStore",save:function(n,i,f){let r=t.debug&&v(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&$(r,l,s),this.act(s,function(_,p,b){return r&&h(r,arguments),a.save(this,l,i,_,p,s,b,r)})},load:function(n,i,f){let r=t.debug&&v(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&$(r,l,s),this.act(s,function(_,p,b){return r&&h(r,arguments),a.load(this,l,i,_,p,s,b,r)})},list:function(n,i,f){let r=t.debug&&v(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&$(r,l,s),this.act(s,function(_,p,b){return r&&h(r,arguments),a.list(this,l,i,_,p,s,b,r)})},remove:function(n,i,f){let r=t.debug&&v(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&$(r,l,s),this.act(s,function(_,p,b){return r&&h(r,arguments),a.remove(this,l,i,_,p,s,b,r)})},close:function(n,i){i()},native:function(n,i){i()}},o=m(e,t,d);function v(n){let i=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return i&&w.push(i),i}function $(n,i,f){return n.apistart=Date.now(),n.ctx=i,n.apimsg=f,n}function h(n,i){return n.apiend=Date.now(),n.err=i[0],n.res=i[1],n.apimeta=i[2],n}return{name:d.name,tag:o.tag,exports:{makeApiMsg:c,msglog:w}}}function k(t,e){return t.entity(e.zone,e.base,e.name).canon$()}y.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let m=t.q;e.store=m.store$!==!1,delete m.store$,e.cmd=t.cmd;let u=t.ent||t.qent;if(u){if(u.canon$)Object.assign(e,u.canon$({object:!0}));else if(u.entity$){let a=u.entity$.split("/");Object.assign(e,{zone:a[0]==="-"?null:a[0],base:a[1]==="-"?null:a[1],name:a[2]==="-"?null:a[2]})}}return e},handleResponse:{any:function(t,e,m,u,a,w,c,d){if(d&&(d.end=Date.now()),u)return m(u);if(w.load==="entity"&&(a==null||a.ok==null))return m(null);if(a&&a.ok&&a.item){let o=t.entity({zone:e.zone,base:e.base,name:e.name});return m(o.make$().data$(a.item))}else{let o=a&&a.err;return o=o||new Error(`BrowserStore: ${e.cmd} ${k(t,e)}: unknown error`),m(o)}},list:function(t,e,m,u,a,w,c,d){if(d&&(d.end=Date.now()),u&&m(u),a&&a.ok&&a.list){let o=t.entity({zone:e.zone,base:e.base,name:e.name}),v=a.list.map($=>o.make$().data$($));d&&(d.end=Date.now()),m(v)}else{let o=a&&a.err;o=o||new Error(`BrowserStore: ${e.cmd} ${k(t,e)}: unknown list error`),m(o)}}}};Object.defineProperty(y,"name",{value:"BrowserStore"});module.exports=y; //# sourceMappingURL=browser-store.cjs.js.map diff --git a/dist/browser-store.cjs.js.map b/dist/browser-store.cjs.js.map index 68c562b..4a9fc06 100644 --- a/dist/browser-store.cjs.js.map +++ b/dist/browser-store.cjs.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-store.cjs.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n if (res && res.ok) {\n return reply(res.ent)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","list","item"],"mappings":"aAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ,OAAQ,QAAQ,EAAE,OACtD,CAACC,EAAQC,KAAQD,EAAEC,CAAC,EAAIH,EAAIG,CAAC,GAAKH,EAAI,IAAME,GAC5C,CAAC,CAAA,EAGH,MAAME,EAWA,CAAA,EAEG,SAAAC,EAAWC,EAAUC,EAAUV,EAAc,CACpD,IAAIW,EAAc,CAAA,EACdC,EAAWZ,EAAQ,OAEvB,QAASa,KAAMD,EAAU,CACnB,IAAAE,EAAKF,EAASC,CAAE,EACD,OAAOC,GAAtB,WACFH,EAAOE,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AAC,CAAA,CACxC,CACH,EAGK,OAAAtB,CACT,EAEA,eAAgB,CACd,IAAK,SACHT,EACAS,EACAM,EACAK,EACAC,EACAW,EACAf,EACA,CAGA,GAFSA,IAAAA,EAAK,IAAM,KAAK,IAAI,GAEzBG,EACF,OAAOL,EAAMK,CAAG,EAGd,GAAAC,GAAOA,EAAI,GACN,OAAAN,EAAMM,EAAI,GAAG,EACf,CACDD,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,OAAAA,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAAS3B,EAAQS,CAAG,CAAC,iBAAA,EAE9CM,EAAMK,CAAG,CAClB,CACF,EAEA,KAAM,SACJpB,EACAS,EACAM,EACAK,EACAC,EACAW,EACAf,EACA,CAOA,GANSA,IAAAA,EAAK,IAAM,KAAK,IAAI,GAEzBG,GACFL,EAAMK,CAAG,EAGPC,GAAOA,EAAI,IAAMA,EAAI,KAAM,CACzB,IAAAS,EAAM9B,EAAO,OAAO,CACtB,KAAMS,EAAI,KACV,KAAMA,EAAI,KACV,KAAMA,EAAI,IAAA,CACX,EACGwB,EAAOZ,EAAI,KAAK,IAAKa,GAAcJ,EAAI,MAAM,EAAE,MAAMI,CAAI,CAAC,EACrDjB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMkB,CAAI,CAAA,KACL,CACDb,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file +{"version":3,"file":"browser-store.cjs.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023-2024 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n // TODO: debounce response could be empty object - review\n if ('entity' === apimsg.load && (null == res || null == res.ok)) {\n return reply(null)\n }\n\n if (res && res.ok && res.item) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n return reply(ent.make$().data$(res.item))\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","_apimsg","list","item"],"mappings":"aAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ,OAAQ,QAAQ,EAAE,OACtD,CAACC,EAAQC,KAAQD,EAAEC,CAAC,EAAIH,EAAIG,CAAC,GAAKH,EAAI,IAAME,GAC5C,CAAC,CAAA,EAGH,MAAME,EAWA,CAAA,EAEG,SAAAC,EAAWC,EAAUC,EAAUV,EAAc,CACpD,IAAIW,EAAc,CAAA,EACdC,EAAWZ,EAAQ,OAEvB,QAASa,KAAMD,EAAU,CACnB,IAAAE,EAAKF,EAASC,CAAE,EACD,OAAOC,GAAtB,WACFH,EAAOE,CAAE,EAAIC,EAAGL,EAAKC,EAAKV,CAAO,EAEjCW,EAAOE,CAAE,EAAI,KAAK,MAAM,KAAK,UAAUC,CAAE,CAAC,CAE9C,CAEO,OAAAH,CACT,CAEA,IAAII,EAAQ,CACV,KAAM,eAEN,KAAM,SAAqBN,EAAUO,EAAYC,EAAY,CAC3D,IAAIC,EAAOlB,EAAQ,OAASmB,EAAS,SAAS,EAC1CT,EAAMV,EAAQ,WAAWS,CAAG,EAC5BE,EAASH,EAAWC,EAAKC,EAAKV,CAAO,EAEjCkB,GAAAE,EAAOF,EAAMR,EAAKC,CAAM,EAC3B,KAAA,IACHA,EACA,SAAgCU,EAAYC,EAAUC,EAAc,CAC1D,O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A,CACX,EACD,OAAOM,EAAMe,EAAI,MAAA,EAAQ,MAAMT,EAAI,IAAI,CAAC,CAAA,KACnC,CACDD,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,OAAAA,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAAS3B,EAAQS,CAAG,CAAC,iBAAA,EAE9CM,EAAMK,CAAG,CAClB,CACF,EAEA,KAAM,SACJpB,EACAS,EACAM,EACAK,EACAC,EACAY,EACAD,EACAf,EACA,CAOA,GANSA,IAAAA,EAAK,IAAM,KAAK,IAAI,GAEzBG,GACFL,EAAMK,CAAG,EAGPC,GAAOA,EAAI,IAAMA,EAAI,KAAM,CACzB,IAAAS,EAAM9B,EAAO,OAAO,CACtB,KAAMS,EAAI,KACV,KAAMA,EAAI,KACV,KAAMA,EAAI,IAAA,CACX,EACGyB,EAAOb,EAAI,KAAK,IAAKc,GAAcL,EAAI,MAAM,EAAE,MAAMK,CAAI,CAAC,EACrDlB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMmB,CAAI,CAAA,KACL,CACDd,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file diff --git a/dist/browser-store.d.ts b/dist/browser-store.d.ts index 3f89498..205600f 100644 --- a/dist/browser-store.d.ts +++ b/dist/browser-store.d.ts @@ -38,8 +38,8 @@ declare namespace BrowserStore { }; prepareCtx: (msg: any, ctx: any) => any; handleResponse: { - any: (seneca: any, ctx: any, reply: any, err: Error, res: any, _apimeta: any, logn: any) => any; - list: (seneca: any, ctx: any, reply: any, err: Error, res: any, _apimeta: any, logn: any) => void; + any: (seneca: any, ctx: any, reply: any, err: Error, res: any, apimsg: any, _apimeta: any, logn: any) => any; + list: (seneca: any, ctx: any, reply: any, err: Error, res: any, _apimsg: any, _apimeta: any, logn: any) => void; }; }; } diff --git a/dist/browser-store.es.js b/dist/browser-store.es.js index d36925a..148a58b 100644 --- a/dist/browser-store.es.js +++ b/dist/browser-store.es.js @@ -1,97 +1,125 @@ -function S(t) { - let e = this, o = e.export("entity/init"), u = t.handleResponse, i = ["save", "load", "list", "remove"].reduce( - (n, r) => (n[r] = u[r] || u.any, n), +function k(t) { + let e = this, m = e.export("entity/init"), u = t.handleResponse, a = ["save", "load", "list", "remove"].reduce( + (n, i) => (n[i] = u[i] || u.any, n), {} ); - const b = []; - function m(n, r, c) { - let a = {}, s = c.apimsg; + const w = []; + function c(n, i, f) { + let r = {}, s = f.apimsg; for (let l in s) { - let f = s[l]; - typeof f == "function" ? a[l] = f(n, r, c) : a[l] = JSON.parse(JSON.stringify(f)); + let g = s[l]; + typeof g == "function" ? r[l] = g(n, i, f) : r[l] = JSON.parse(JSON.stringify(g)); } - return a; + return r; } let d = { name: "BrowserStore", - save: function(n, r, c) { - let a = t.debug && v(arguments), s = t.prepareCtx(n), l = m(n, s, t); - a && w(a, s, l), this.act( + save: function(n, i, f) { + let r = t.debug && v(arguments), s = t.prepareCtx(n), l = c(n, s, t); + r && $(r, s, l), this.act( l, - function(g, _, p) { - return a && h(a, arguments), i.save(this, s, r, g, _, p, a); + function(_, p, b) { + return r && h(r, arguments), a.save( + this, + s, + i, + _, + p, + l, + b, + r + ); } ); }, - load: function(n, r, c) { - let a = t.debug && v(arguments), s = t.prepareCtx(n), l = m(n, s, t); - a && w(a, s, l), this.act( + load: function(n, i, f) { + let r = t.debug && v(arguments), s = t.prepareCtx(n), l = c(n, s, t); + r && $(r, s, l), this.act( l, - function(g, _, p) { - return a && h(a, arguments), i.load(this, s, r, g, _, p, a); + function(_, p, b) { + return r && h(r, arguments), a.load( + this, + s, + i, + _, + p, + l, + b, + r + ); } ); }, - list: function(n, r, c) { - let a = t.debug && v(arguments), s = t.prepareCtx(n), l = m(n, s, t); - a && w(a, s, l), this.act( + list: function(n, i, f) { + let r = t.debug && v(arguments), s = t.prepareCtx(n), l = c(n, s, t); + r && $(r, s, l), this.act( l, - function(g, _, p) { - return a && h(a, arguments), i.list(this, s, r, g, _, p, a); + function(_, p, b) { + return r && h(r, arguments), a.list( + this, + s, + i, + _, + p, + l, + b, + r + ); } ); }, - remove: function(n, r, c) { - let a = t.debug && v(arguments), s = t.prepareCtx(n), l = m(n, s, t); - a && w(a, s, l), this.act( + remove: function(n, i, f) { + let r = t.debug && v(arguments), s = t.prepareCtx(n), l = c(n, s, t); + r && $(r, s, l), this.act( l, - function(g, _, p) { - return a && h(a, arguments), i.remove( + function(_, p, b) { + return r && h(r, arguments), a.remove( this, s, - r, - g, + i, _, p, - a + l, + b, + r ); } ); }, - close: function(n, r) { - r(); + close: function(n, i) { + i(); }, - native: function(n, r) { - r(); + native: function(n, i) { + i(); } - }, $ = o(e, t, d); + }, o = m(e, t, d); function v(n) { - let r = t.debug && { + let i = t.debug && { msg: n[0], meta: n[2], start: Date.now() }; - return r && b.push(r), r; + return i && w.push(i), i; } - function w(n, r, c) { - return n.apistart = Date.now(), n.ctx = r, n.apimsg = c, n; + function $(n, i, f) { + return n.apistart = Date.now(), n.ctx = i, n.apimsg = f, n; } - function h(n, r) { - return n.apiend = Date.now(), n.err = r[0], n.res = r[1], n.apimeta = r[2], n; + function h(n, i) { + return n.apiend = Date.now(), n.err = i[0], n.res = i[1], n.apimeta = i[2], n; } return { name: d.name, - tag: $.tag, + tag: o.tag, exports: { - makeApiMsg: m, - msglog: b + makeApiMsg: c, + msglog: w } }; } function y(t, e) { return t.entity(e.zone, e.base, e.name).canon$(); } -S.defaults = { +k.defaults = { debug: !1, apimsg: { aim: "req", @@ -111,58 +139,65 @@ S.defaults = { }, prepareCtx: (t, e) => { e = e || {}; - let o = t.q; - e.store = o.store$ !== !1, delete o.store$, e.cmd = t.cmd; + let m = t.q; + e.store = m.store$ !== !1, delete m.store$, e.cmd = t.cmd; let u = t.ent || t.qent; if (u) { if (u.canon$) Object.assign(e, u.canon$({ object: !0 })); else if (u.entity$) { - let i = u.entity$.split("/"); + let a = u.entity$.split("/"); Object.assign(e, { - zone: i[0] === "-" ? null : i[0], - base: i[1] === "-" ? null : i[1], - name: i[2] === "-" ? null : i[2] + zone: a[0] === "-" ? null : a[0], + base: a[1] === "-" ? null : a[1], + name: a[2] === "-" ? null : a[2] }); } } return e; }, handleResponse: { - any: function(t, e, o, u, i, b, m) { - if (m && (m.end = Date.now()), u) - return o(u); - if (i && i.ok) - return o(i.ent); - { - let d = i && i.err; - return d = d || new Error( + any: function(t, e, m, u, a, w, c, d) { + if (d && (d.end = Date.now()), u) + return m(u); + if (w.load === "entity" && (a == null || a.ok == null)) + return m(null); + if (a && a.ok && a.item) { + let o = t.entity({ + zone: e.zone, + base: e.base, + name: e.name + }); + return m(o.make$().data$(a.item)); + } else { + let o = a && a.err; + return o = o || new Error( `BrowserStore: ${e.cmd} ${y(t, e)}: unknown error` - ), o(d); + ), m(o); } }, - list: function(t, e, o, u, i, b, m) { - if (m && (m.end = Date.now()), u && o(u), i && i.ok && i.list) { - let d = t.entity({ + list: function(t, e, m, u, a, w, c, d) { + if (d && (d.end = Date.now()), u && m(u), a && a.ok && a.list) { + let o = t.entity({ zone: e.zone, base: e.base, name: e.name - }), $ = i.list.map((v) => d.make$().data$(v)); - m && (m.end = Date.now()), o($); + }), v = a.list.map(($) => o.make$().data$($)); + d && (d.end = Date.now()), m(v); } else { - let d = i && i.err; - d = d || new Error( + let o = a && a.err; + o = o || new Error( `BrowserStore: ${e.cmd} ${y( t, e )}: unknown list error` - ), o(d); + ), m(o); } } } }; -Object.defineProperty(S, "name", { value: "BrowserStore" }); +Object.defineProperty(k, "name", { value: "BrowserStore" }); export { - S as default + k as default }; //# sourceMappingURL=browser-store.es.js.map diff --git a/dist/browser-store.es.js.map b/dist/browser-store.es.js.map index 358a88a..e486fbd 100644 --- a/dist/browser-store.es.js.map +++ b/dist/browser-store.es.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-store.es.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n if (res && res.ok) {\n return reply(res.ent)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","list","item"],"mappings":"AAEA,SAASA,EAAwBC,GAAc;AAC7C,MAAIC,IAAc,MAEdC,IAAOD,EAAO,OAAO,aAAa,GAElCE,IAAMH,EAAQ,gBACdI,IAAiB,CAAC,QAAQ,QAAQ,QAAQ,QAAQ,EAAE;AAAA,IACtD,CAACC,GAAQC,OAAQD,EAAEC,CAAC,IAAIH,EAAIG,CAAC,KAAKH,EAAI,KAAME;AAAA,IAC5C,CAAC;AAAA,EAAA;AAGH,QAAME,IAWA,CAAA;AAEG,WAAAC,EAAWC,GAAUC,GAAUV,GAAc;AACpD,QAAIW,IAAc,CAAA,GACdC,IAAWZ,EAAQ;AAEvB,aAASa,KAAMD,GAAU;AACnB,UAAAE,IAAKF,EAASC,CAAE;AAChB,MAAe,OAAOC,KAAtB,aACFH,EAAOE,CAAE,IAAIC,EAAGL,GAAKC,GAAKV,CAAO,IAEjCW,EAAOE,CAAE,IAAI,KAAK,MAAM,KAAK,UAAUC,CAAE,CAAC;AAAA,IAE9C;AAEO,WAAAH;AAAA,EACT;AAEA,MAAII,IAAQ;AAAA,IACV,MAAM;AAAA,IAEN,MAAM,SAAqBN,GAAUO,GAAYC,GAAY;AAC3D,UAAIC,IAAOlB,EAAQ,SAASmB,EAAS,SAAS,GAC1CT,IAAMV,EAAQ,WAAWS,CAAG,GAC5BE,IAASH,EAAWC,GAAKC,GAAKV,CAAO;AAEjC,MAAAkB,KAAAE,EAAOF,GAAMR,GAAKC,CAAM,GAC3B,KAAA;AAAA,QACHA;AAAA,QACA,SAAgCU,GAAYC,GAAUC,GAAc;AAC1D,iBAAA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BAAgB;"} \ No newline at end of file +{"version":3,"file":"browser-store.es.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023-2024 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n // TODO: debounce response could be empty object - review\n if ('entity' === apimsg.load && (null == res || null == res.ok)) {\n return reply(null)\n }\n\n if (res && res.ok && res.item) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n return reply(ent.make$().data$(res.item))\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","_apimsg","list","item"],"mappings":"AAEA,SAASA,EAAwBC,GAAc;AAC7C,MAAIC,IAAc,MAEdC,IAAOD,EAAO,OAAO,aAAa,GAElCE,IAAMH,EAAQ,gBACdI,IAAiB,CAAC,QAAQ,QAAQ,QAAQ,QAAQ,EAAE;AAAA,IACtD,CAACC,GAAQC,OAAQD,EAAEC,CAAC,IAAIH,EAAIG,CAAC,KAAKH,EAAI,KAAME;AAAA,IAC5C,CAAC;AAAA,EAAA;AAGH,QAAME,IAWA,CAAA;AAEG,WAAAC,EAAWC,GAAUC,GAAUV,GAAc;AACpD,QAAIW,IAAc,CAAA,GACdC,IAAWZ,EAAQ;AAEvB,aAASa,KAAMD,GAAU;AACnB,UAAAE,IAAKF,EAASC,CAAE;AAChB,MAAe,OAAOC,KAAtB,aACFH,EAAOE,CAAE,IAAIC,EAAGL,GAAKC,GAAKV,CAAO,IAEjCW,EAAOE,CAAE,IAAI,KAAK,MAAM,KAAK,UAAUC,CAAE,CAAC;AAAA,IAE9C;AAEO,WAAAH;AAAA,EACT;AAEA,MAAII,IAAQ;AAAA,IACV,MAAM;AAAA,IAEN,MAAM,SAAqBN,GAAUO,GAAYC,GAAY;AAC3D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pB,GACAS,GACAM,GACAK,GACAC,GACAY,GACAD,GACAf,GACA;AAOA,UANSA,MAAAA,EAAK,MAAM,KAAK,IAAI,IAEzBG,KACFL,EAAMK,CAAG,GAGPC,KAAOA,EAAI,MAAMA,EAAI,MAAM;AACzB,YAAAS,IAAM9B,EAAO,OAAO;AAAA,UACtB,MAAMS,EAAI;AAAA,UACV,MAAMA,EAAI;AAAA,UACV,MAAMA,EAAI;AAAA,QAAA,CACX,GACGyB,IAAOb,EAAI,KAAK,IAAI,CAACc,MAAcL,EAAI,MAAM,EAAE,MAAMK,CAAI,CAAC;AACrD,QAAAlB,MAAAA,EAAK,MAAM,KAAK,IAAI,IAC7BF,EAAMmB,CAAI;AAAA,MAAA,OACL;AACDd,YAAAA,IAAMC,KAAOA,EAAI;AACrBD,QAAAA,IACEA,KACA,IAAI;AAAA,UACF,iBAAiBX,EAAI,GAAG,IAAIkB;AAAA,YAC1B3B;AAAA,YACAS;AAAA,UACD,CAAA;AAAA,QAAA,GAELM,EAAMK,CAAG;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;AAGA,OAAO,eAAetB,GAAc,QAAQ,EAAE,OAAO,gBAAgB;"} \ No newline at end of file diff --git a/dist/browser-store.iife.js b/dist/browser-store.iife.js index 5d139f5..3dadc8f 100644 --- a/dist/browser-store.iife.js +++ b/dist/browser-store.iife.js @@ -1,2 +1,2 @@ -var BrowserStore=function(){"use strict";function $(t){let e=this,o=e.export("entity/init"),l=t.handleResponse,i=["save","load","list","remove"].reduce((n,a)=>(n[a]=l[a]||l.any,n),{});const b=[];function m(n,a,d){let r={},s=d.apimsg;for(let u in s){let f=s[u];typeof f=="function"?r[u]=f(n,a,d):r[u]=JSON.parse(JSON.stringify(f))}return r}let c={name:"BrowserStore",save:function(n,a,d){let r=t.debug&&v(arguments),s=t.prepareCtx(n),u=m(n,s,t);r&&w(r,s,u),this.act(u,function(g,_,p){return r&&h(r,arguments),i.save(this,s,a,g,_,p,r)})},load:function(n,a,d){let r=t.debug&&v(arguments),s=t.prepareCtx(n),u=m(n,s,t);r&&w(r,s,u),this.act(u,function(g,_,p){return r&&h(r,arguments),i.load(this,s,a,g,_,p,r)})},list:function(n,a,d){let r=t.debug&&v(arguments),s=t.prepareCtx(n),u=m(n,s,t);r&&w(r,s,u),this.act(u,function(g,_,p){return r&&h(r,arguments),i.list(this,s,a,g,_,p,r)})},remove:function(n,a,d){let r=t.debug&&v(arguments),s=t.prepareCtx(n),u=m(n,s,t);r&&w(r,s,u),this.act(u,function(g,_,p){return r&&h(r,arguments),i.remove(this,s,a,g,_,p,r)})},close:function(n,a){a()},native:function(n,a){a()}},y=o(e,t,c);function v(n){let a=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return a&&b.push(a),a}function w(n,a,d){return n.apistart=Date.now(),n.ctx=a,n.apimsg=d,n}function h(n,a){return n.apiend=Date.now(),n.err=a[0],n.res=a[1],n.apimeta=a[2],n}return{name:c.name,tag:y.tag,exports:{makeApiMsg:m,msglog:b}}}function S(t,e){return t.entity(e.zone,e.base,e.name).canon$()}return $.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let o=t.q;e.store=o.store$!==!1,delete o.store$,e.cmd=t.cmd;let l=t.ent||t.qent;if(l){if(l.canon$)Object.assign(e,l.canon$({object:!0}));else if(l.entity$){let i=l.entity$.split("/");Object.assign(e,{zone:i[0]==="-"?null:i[0],base:i[1]==="-"?null:i[1],name:i[2]==="-"?null:i[2]})}}return e},handleResponse:{any:function(t,e,o,l,i,b,m){if(m&&(m.end=Date.now()),l)return o(l);if(i&&i.ok)return o(i.ent);{let c=i&&i.err;return c=c||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown error`),o(c)}},list:function(t,e,o,l,i,b,m){if(m&&(m.end=Date.now()),l&&o(l),i&&i.ok&&i.list){let c=t.entity({zone:e.zone,base:e.base,name:e.name}),y=i.list.map(v=>c.make$().data$(v));m&&(m.end=Date.now()),o(y)}else{let c=i&&i.err;c=c||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown list error`),o(c)}}}},Object.defineProperty($,"name",{value:"BrowserStore"}),$}(); +var BrowserStore=function(){"use strict";function y(t){let e=this,m=e.export("entity/init"),u=t.handleResponse,a=["save","load","list","remove"].reduce((n,i)=>(n[i]=u[i]||u.any,n),{});const $=[];function c(n,i,f){let r={},l=f.apimsg;for(let s in l){let g=l[s];typeof g=="function"?r[s]=g(n,i,f):r[s]=JSON.parse(JSON.stringify(g))}return r}let d={name:"BrowserStore",save:function(n,i,f){let r=t.debug&&b(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&w(r,l,s),this.act(s,function(_,p,v){return r&&h(r,arguments),a.save(this,l,i,_,p,s,v,r)})},load:function(n,i,f){let r=t.debug&&b(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&w(r,l,s),this.act(s,function(_,p,v){return r&&h(r,arguments),a.load(this,l,i,_,p,s,v,r)})},list:function(n,i,f){let r=t.debug&&b(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&w(r,l,s),this.act(s,function(_,p,v){return r&&h(r,arguments),a.list(this,l,i,_,p,s,v,r)})},remove:function(n,i,f){let r=t.debug&&b(arguments),l=t.prepareCtx(n),s=c(n,l,t);r&&w(r,l,s),this.act(s,function(_,p,v){return r&&h(r,arguments),a.remove(this,l,i,_,p,s,v,r)})},close:function(n,i){i()},native:function(n,i){i()}},o=m(e,t,d);function b(n){let i=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return i&&$.push(i),i}function w(n,i,f){return n.apistart=Date.now(),n.ctx=i,n.apimsg=f,n}function h(n,i){return n.apiend=Date.now(),n.err=i[0],n.res=i[1],n.apimeta=i[2],n}return{name:d.name,tag:o.tag,exports:{makeApiMsg:c,msglog:$}}}function S(t,e){return t.entity(e.zone,e.base,e.name).canon$()}return y.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let m=t.q;e.store=m.store$!==!1,delete m.store$,e.cmd=t.cmd;let u=t.ent||t.qent;if(u){if(u.canon$)Object.assign(e,u.canon$({object:!0}));else if(u.entity$){let a=u.entity$.split("/");Object.assign(e,{zone:a[0]==="-"?null:a[0],base:a[1]==="-"?null:a[1],name:a[2]==="-"?null:a[2]})}}return e},handleResponse:{any:function(t,e,m,u,a,$,c,d){if(d&&(d.end=Date.now()),u)return m(u);if($.load==="entity"&&(a==null||a.ok==null))return m(null);if(a&&a.ok&&a.item){let o=t.entity({zone:e.zone,base:e.base,name:e.name});return m(o.make$().data$(a.item))}else{let o=a&&a.err;return o=o||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown error`),m(o)}},list:function(t,e,m,u,a,$,c,d){if(d&&(d.end=Date.now()),u&&m(u),a&&a.ok&&a.list){let o=t.entity({zone:e.zone,base:e.base,name:e.name}),b=a.list.map(w=>o.make$().data$(w));d&&(d.end=Date.now()),m(b)}else{let o=a&&a.err;o=o||new Error(`BrowserStore: ${e.cmd} ${S(t,e)}: unknown list error`),m(o)}}}},Object.defineProperty(y,"name",{value:"BrowserStore"}),y}(); //# sourceMappingURL=browser-store.iife.js.map diff --git a/dist/browser-store.iife.js.map b/dist/browser-store.iife.js.map index 479ccb1..d64c605 100644 --- a/dist/browser-store.iife.js.map +++ b/dist/browser-store.iife.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-store.iife.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n if (res && res.ok) {\n return reply(res.ent)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","list","item"],"mappings":"yCAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ,OAAQ,QAAQ,EAAE,OACtD,CAACC,EAAQC,KAAQD,EAAEC,CAAC,EAAIH,EAAIG,CAAC,GAAKH,EAAI,IAAME,GAC5C,CAAC,CAAA,EAGH,MAAME,EAWA,CAAA,EAEG,SAAAC,EAAWC,EAAUC,EAAUV,EAAc,CACpD,IAAIW,EAAc,CAAA,EACdC,EAAWZ,EAAQ,OAEvB,QAASa,KAAMD,EAAU,CACnB,IAAAE,EAAKF,EAASC,CAAE,EACD,OAAOC,GAAtB,WACFH,EAAOE,CAAE,EAAIC,EAAGL,EAAKC,EAAKV,CAAO,EAEjCW,EAAOE,CAAE,EAAI,KAAK,MAAM,KAAK,UAAUC,CAAE,CAAC,CAE9C,CAEO,OAAAH,CACT,CAEA,IAAII,EAAQ,CACV,KAAM,eAEN,KAAM,SAAqBN,EAAUO,EAAYC,EAAY,CAC3D,IAAIC,EAAOlB,EAAQ,OAASmB,EAAS,SAAS,EAC1CT,EAAMV,EAAQ,WAAWS,CAAG,EAC5BE,EAASH,EAAWC,EAAKC,EAAKV,CAAO,EAEjCkB,GAAAE,EAAOF,EAAMR,EAAKC,CAAM,EAC3B,KAAA,IACHA,EACA,SAAgCU,EAAYC,EAAUC,EAAc,CAC1D,OAAAL,GAAAM,EAAON,EAAM,SAAS,EACvBd,EAA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Af,EACA,CAOA,GANSA,IAAAA,EAAK,IAAM,KAAK,IAAI,GAEzBG,GACFL,EAAMK,CAAG,EAGPC,GAAOA,EAAI,IAAMA,EAAI,KAAM,CACzB,IAAAS,EAAM9B,EAAO,OAAO,CACtB,KAAMS,EAAI,KACV,KAAMA,EAAI,KACV,KAAMA,EAAI,IAAA,CACX,EACGwB,EAAOZ,EAAI,KAAK,IAAKa,GAAcJ,EAAI,MAAM,EAAE,MAAMI,CAAI,CAAC,EACrDjB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMkB,CAAI,CAAA,KACL,CACDb,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file +{"version":3,"file":"browser-store.iife.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023-2024 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n // TODO: debounce response could be empty object - review\n if ('entity' === apimsg.load && (null == res || null == res.ok)) {\n return reply(null)\n }\n\n if (res && res.ok && res.item) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n return reply(ent.make$().data$(res.item))\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","_apimsg","list","item"],"mappings":"yCAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ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,KAAMS,EAAI,KACV,KAAMA,EAAI,KACV,KAAMA,EAAI,IAAA,CACX,EACGyB,EAAOb,EAAI,KAAK,IAAKc,GAAcL,EAAI,MAAM,EAAE,MAAMK,CAAI,CAAC,EACrDlB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMmB,CAAI,CAAA,KACL,CACDd,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file diff --git a/dist/browser-store.umd.js b/dist/browser-store.umd.js index 15311fb..005caf6 100644 --- a/dist/browser-store.umd.js +++ b/dist/browser-store.umd.js @@ -1,2 +1,2 @@ -(function(c,b){typeof exports=="object"&&typeof module<"u"?module.exports=b():typeof define=="function"&&define.amd?define(b):(c=typeof globalThis<"u"?globalThis:c||self,c.BrowserStore=b())})(this,function(){"use strict";function c(t){let e=this,m=e.export("entity/init"),o=t.handleResponse,i=["save","load","list","remove"].reduce((n,a)=>(n[a]=o[a]||o.any,n),{});const w=[];function l(n,a,f){let r={},s=f.apimsg;for(let u in s){let g=s[u];typeof g=="function"?r[u]=g(n,a,f):r[u]=JSON.parse(JSON.stringify(g))}return r}let d={name:"BrowserStore",save:function(n,a,f){let r=t.debug&&h(arguments),s=t.prepareCtx(n),u=l(n,s,t);r&&$(r,s,u),this.act(u,function(p,_,v){return r&&y(r,arguments),i.save(this,s,a,p,_,v,r)})},load:function(n,a,f){let r=t.debug&&h(arguments),s=t.prepareCtx(n),u=l(n,s,t);r&&$(r,s,u),this.act(u,function(p,_,v){return r&&y(r,arguments),i.load(this,s,a,p,_,v,r)})},list:function(n,a,f){let r=t.debug&&h(arguments),s=t.prepareCtx(n),u=l(n,s,t);r&&$(r,s,u),this.act(u,function(p,_,v){return r&&y(r,arguments),i.list(this,s,a,p,_,v,r)})},remove:function(n,a,f){let r=t.debug&&h(arguments),s=t.prepareCtx(n),u=l(n,s,t);r&&$(r,s,u),this.act(u,function(p,_,v){return r&&y(r,arguments),i.remove(this,s,a,p,_,v,r)})},close:function(n,a){a()},native:function(n,a){a()}},S=m(e,t,d);function h(n){let a=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return a&&w.push(a),a}function $(n,a,f){return n.apistart=Date.now(),n.ctx=a,n.apimsg=f,n}function y(n,a){return n.apiend=Date.now(),n.err=a[0],n.res=a[1],n.apimeta=a[2],n}return{name:d.name,tag:S.tag,exports:{makeApiMsg:l,msglog:w}}}function b(t,e){return t.entity(e.zone,e.base,e.name).canon$()}return c.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let m=t.q;e.store=m.store$!==!1,delete m.store$,e.cmd=t.cmd;let o=t.ent||t.qent;if(o){if(o.canon$)Object.assign(e,o.canon$({object:!0}));else if(o.entity$){let i=o.entity$.split("/");Object.assign(e,{zone:i[0]==="-"?null:i[0],base:i[1]==="-"?null:i[1],name:i[2]==="-"?null:i[2]})}}return e},handleResponse:{any:function(t,e,m,o,i,w,l){if(l&&(l.end=Date.now()),o)return m(o);if(i&&i.ok)return m(i.ent);{let d=i&&i.err;return d=d||new Error(`BrowserStore: ${e.cmd} ${b(t,e)}: unknown error`),m(d)}},list:function(t,e,m,o,i,w,l){if(l&&(l.end=Date.now()),o&&m(o),i&&i.ok&&i.list){let d=t.entity({zone:e.zone,base:e.base,name:e.name}),S=i.list.map(h=>d.make$().data$(h));l&&(l.end=Date.now()),m(S)}else{let d=i&&i.err;d=d||new Error(`BrowserStore: ${e.cmd} ${b(t,e)}: unknown list error`),m(d)}}}},Object.defineProperty(c,"name",{value:"BrowserStore"}),c}); +(function(c,h){typeof exports=="object"&&typeof module<"u"?module.exports=h():typeof define=="function"&&define.amd?define(h):(c=typeof globalThis<"u"?globalThis:c||self,c.BrowserStore=h())})(this,function(){"use strict";function c(t){let e=this,o=e.export("entity/init"),l=t.handleResponse,a=["save","load","list","remove"].reduce((n,i)=>(n[i]=l[i]||l.any,n),{});const y=[];function g(n,i,f){let r={},u=f.apimsg;for(let s in u){let p=u[s];typeof p=="function"?r[s]=p(n,i,f):r[s]=JSON.parse(JSON.stringify(p))}return r}let d={name:"BrowserStore",save:function(n,i,f){let r=t.debug&&w(arguments),u=t.prepareCtx(n),s=g(n,u,t);r&&$(r,u,s),this.act(s,function(_,b,v){return r&&S(r,arguments),a.save(this,u,i,_,b,s,v,r)})},load:function(n,i,f){let r=t.debug&&w(arguments),u=t.prepareCtx(n),s=g(n,u,t);r&&$(r,u,s),this.act(s,function(_,b,v){return r&&S(r,arguments),a.load(this,u,i,_,b,s,v,r)})},list:function(n,i,f){let r=t.debug&&w(arguments),u=t.prepareCtx(n),s=g(n,u,t);r&&$(r,u,s),this.act(s,function(_,b,v){return r&&S(r,arguments),a.list(this,u,i,_,b,s,v,r)})},remove:function(n,i,f){let r=t.debug&&w(arguments),u=t.prepareCtx(n),s=g(n,u,t);r&&$(r,u,s),this.act(s,function(_,b,v){return r&&S(r,arguments),a.remove(this,u,i,_,b,s,v,r)})},close:function(n,i){i()},native:function(n,i){i()}},m=o(e,t,d);function w(n){let i=t.debug&&{msg:n[0],meta:n[2],start:Date.now()};return i&&y.push(i),i}function $(n,i,f){return n.apistart=Date.now(),n.ctx=i,n.apimsg=f,n}function S(n,i){return n.apiend=Date.now(),n.err=i[0],n.res=i[1],n.apimeta=i[2],n}return{name:d.name,tag:m.tag,exports:{makeApiMsg:g,msglog:y}}}function h(t,e){return t.entity(e.zone,e.base,e.name).canon$()}return c.defaults={debug:!1,apimsg:{aim:"req",on:"entity",debounce$:!0,q:(t,e)=>t.q,ent:(t,e)=>t.ent,save:(t,e)=>e.cmd==="save"?"entity":void 0,load:(t,e)=>e.cmd==="load"?"entity":void 0,list:(t,e)=>e.cmd==="list"?"entity":void 0,remove:(t,e)=>e.cmd==="remove"?"entity":void 0,store:(t,e)=>e.store,name:(t,e)=>e.name,base:(t,e)=>e.base,zone:(t,e)=>e.zone},prepareCtx:(t,e)=>{e=e||{};let o=t.q;e.store=o.store$!==!1,delete o.store$,e.cmd=t.cmd;let l=t.ent||t.qent;if(l){if(l.canon$)Object.assign(e,l.canon$({object:!0}));else if(l.entity$){let a=l.entity$.split("/");Object.assign(e,{zone:a[0]==="-"?null:a[0],base:a[1]==="-"?null:a[1],name:a[2]==="-"?null:a[2]})}}return e},handleResponse:{any:function(t,e,o,l,a,y,g,d){if(d&&(d.end=Date.now()),l)return o(l);if(y.load==="entity"&&(a==null||a.ok==null))return o(null);if(a&&a.ok&&a.item){let m=t.entity({zone:e.zone,base:e.base,name:e.name});return o(m.make$().data$(a.item))}else{let m=a&&a.err;return m=m||new Error(`BrowserStore: ${e.cmd} ${h(t,e)}: unknown error`),o(m)}},list:function(t,e,o,l,a,y,g,d){if(d&&(d.end=Date.now()),l&&o(l),a&&a.ok&&a.list){let m=t.entity({zone:e.zone,base:e.base,name:e.name}),w=a.list.map($=>m.make$().data$($));d&&(d.end=Date.now()),o(w)}else{let m=a&&a.err;m=m||new Error(`BrowserStore: ${e.cmd} ${h(t,e)}: unknown list error`),o(m)}}}},Object.defineProperty(c,"name",{value:"BrowserStore"}),c}); //# sourceMappingURL=browser-store.umd.js.map diff --git a/dist/browser-store.umd.js.map b/dist/browser-store.umd.js.map index 38b805a..fea88c9 100644 --- a/dist/browser-store.umd.js.map +++ b/dist/browser-store.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-store.umd.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(this, ctx, reply, err, res, apimeta, logn)\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n if (res && res.ok) {\n return reply(res.ent)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","list","item"],"mappings":"6NAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ,OAAQ,QAAQ,EAAE,OACtD,CAACC,EAAQC,KAAQD,EAAEC,CAAC,EAAIH,EAAIG,CAAC,GAAKH,EAAI,IAAME,GAC5C,CAAC,CAAA,EAGH,MAAME,EAWA,CAAA,EAEG,SAAAC,EAAWC,EAAUC,EAAUV,EAAc,CACpD,IAAIW,EAAc,CAAA,EACdC,EAAWZ,EAAQ,OAEvB,QAASa,KAAMD,EAAU,CACnB,IAAAE,EAAKF,EAASC,CAAE,EACD,OAAOC,GAAtB,WACFH,EAAOE,CAAE,EAAIC,EAAGL,EAAKC,EAAKV,CAAO,EAEjCW,EAAOE,CAAE,EAAI,KAAK,MAAM,KAAK,UAAUC,CAAE,CAAC,CAE9C,CAEO,OAAAH,CACT,CAEA,IAAII,EAAQ,CACV,KAAM,eAEN,KAAM,SAAqBN,EAAUO,EAAYC,EAAY,CAC3D,IAAIC,EAAOlB,EAAQ,OAASmB,EAAS,SAAS,EAC1CT,EAAMV,EAAQ,WAAWS,CAAG,EAC5BE,EAASH,EAAWC,EAAKC,EAAKV,CAAO,EAEjCkB,GAAAE,EAAOF,EAAMR,EAAKC,CAAM,EAC3B,KAAA,IACHA,EACA,SAAgCU,EAAYC,EAAUC,EAAc,CAC1D,OAAAL,GAAAM,EAAON,EAAM,SAAS,EACvBd,EAAe,KAAK,KAAMM,EAAKM,EAAOK,EAAKC,EAAKC,EAASL,CAAI,CACtE,CAAA,CAEJ,EAEA,KAAM,SAAqBT,EAAU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AAMA,EAAI,KAAM,CACzB,IAAAS,EAAM9B,EAAO,OAAO,CACtB,KAAMS,EAAI,KACV,KAAMA,EAAI,KACV,KAAMA,EAAI,IAAA,CACX,EACGwB,EAAOZ,EAAI,KAAK,IAAKa,GAAcJ,EAAI,MAAM,EAAE,MAAMI,CAAI,CAAC,EACrDjB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMkB,CAAI,CAAA,KACL,CACDb,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file +{"version":3,"file":"browser-store.umd.js","sources":["../src/browser-store.ts"],"sourcesContent":["/* Copyright (c) 2023-2024 Richard Rodger and other contributors, MIT License */\n\nfunction BrowserStore(this: any, options: any) {\n let seneca: any = this\n\n let init = seneca.export('entity/init')\n\n let ohr = options.handleResponse\n let handleResponse = ['save', 'load', 'list', 'remove'].reduce(\n (a: any, n) => ((a[n] = ohr[n] || ohr.any), a),\n {},\n )\n\n const msglog: {\n msg: any\n meta: any\n start: number\n ctx?: any\n apimsg?: any\n res?: any\n err?: any\n apimeta?: any\n apiend?: number\n end?: number\n }[] = []\n\n function makeApiMsg(msg: any, ctx: any, options: any) {\n let apimsg: any = {}\n let apimsgtm = options.apimsg\n\n for (let pn in apimsgtm) {\n let pv = apimsgtm[pn]\n if ('function' === typeof pv) {\n apimsg[pn] = pv(msg, ctx, options)\n } else {\n apimsg[pn] = JSON.parse(JSON.stringify(pv))\n }\n }\n\n return apimsg\n }\n\n let store = {\n name: 'BrowserStore',\n\n save: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function save_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.save(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n load: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function load_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.load(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n list: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function list_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.list(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n remove: function (this: any, msg: any, reply: any, _meta: any) {\n let logn = options.debug && logstart(arguments)\n let ctx = options.prepareCtx(msg)\n let apimsg = makeApiMsg(msg, ctx, options)\n\n logn && logreq(logn, ctx, apimsg)\n this.act(\n apimsg,\n function remove_result(this: any, err: Error, res: any, apimeta: any) {\n logn && logres(logn, arguments)\n return handleResponse.remove(\n this,\n ctx,\n reply,\n err,\n res,\n apimsg,\n apimeta,\n logn,\n )\n },\n )\n },\n\n close: function (this: any, _msg: any, reply: any) {\n reply()\n },\n\n native: function (this: any, _msg: any, reply: any) {\n reply()\n },\n }\n\n let meta = init(seneca, options, store)\n\n function logstart(args: any) {\n let logn = options.debug && {\n msg: args[0],\n meta: args[2],\n start: Date.now(),\n }\n logn && msglog.push(logn)\n return logn\n }\n\n function logreq(logn: any, ctx: any, apimsg: any) {\n logn.apistart = Date.now()\n logn.ctx = ctx\n logn.apimsg = apimsg\n return logn\n }\n\n function logres(logn: any, args: any) {\n logn.apiend = Date.now()\n logn.err = args[0]\n logn.res = args[1]\n logn.apimeta = args[2]\n return logn\n }\n\n return {\n name: store.name,\n tag: meta.tag,\n exports: {\n makeApiMsg,\n msglog,\n },\n }\n}\n\nfunction canonStr(seneca: any, ctx: any) {\n return seneca.entity(ctx.zone, ctx.base, ctx.name).canon$()\n}\n\nBrowserStore.defaults = {\n debug: false,\n\n apimsg: {\n aim: 'req',\n on: 'entity',\n debounce$: true,\n q: (msg: any, _ctx: any) => msg.q,\n ent: (msg: any, _ctx: any) => msg.ent,\n // cmd: (_msg: any, ctx: any) => ctx.cmd,\n save: (_msg: any, ctx: any) => ('save' === ctx.cmd ? 'entity' : undefined),\n load: (_msg: any, ctx: any) => ('load' === ctx.cmd ? 'entity' : undefined),\n list: (_msg: any, ctx: any) => ('list' === ctx.cmd ? 'entity' : undefined),\n remove: (_msg: any, ctx: any) =>\n 'remove' === ctx.cmd ? 'entity' : undefined,\n store: (_msg: any, ctx: any) => ctx.store,\n name: (_msg: any, ctx: any) => ctx.name,\n base: (_msg: any, ctx: any) => ctx.base,\n zone: (_msg: any, ctx: any) => ctx.zone,\n },\n\n prepareCtx: (msg: any, ctx: any) => {\n ctx = ctx || {}\n\n let q = msg.q\n ctx.store = false !== q.store$\n delete q.store$\n\n ctx.cmd = msg.cmd\n\n let ent = msg.ent || msg.qent\n\n if (ent) {\n if (ent.canon$) {\n Object.assign(ctx, ent.canon$({ object: true }))\n } else if (ent.entity$) {\n let parts = ent.entity$.split('/')\n Object.assign(ctx, {\n zone: '-' === parts[0] ? null : parts[0],\n base: '-' === parts[1] ? null : parts[1],\n name: '-' === parts[2] ? null : parts[2],\n })\n }\n }\n\n return ctx\n },\n\n handleResponse: {\n any: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n return reply(err)\n }\n\n // TODO: debounce response could be empty object - review\n if ('entity' === apimsg.load && (null == res || null == res.ok)) {\n return reply(null)\n }\n\n if (res && res.ok && res.item) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n return reply(ent.make$().data$(res.item))\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(seneca, ctx)}: unknown error`,\n )\n return reply(err)\n }\n },\n\n list: function (\n seneca: any,\n ctx: any,\n reply: any,\n err: Error,\n res: any,\n _apimsg: any,\n _apimeta: any,\n logn: any,\n ) {\n logn && (logn.end = Date.now())\n\n if (err) {\n reply(err)\n }\n\n if (res && res.ok && res.list) {\n let ent = seneca.entity({\n zone: ctx.zone,\n base: ctx.base,\n name: ctx.name,\n })\n let list = res.list.map((item: any) => ent.make$().data$(item))\n logn && (logn.end = Date.now())\n reply(list)\n } else {\n let err = res && res.err\n err =\n err ||\n new Error(\n `BrowserStore: ${ctx.cmd} ${canonStr(\n seneca,\n ctx,\n )}: unknown list error`,\n )\n reply(err)\n }\n },\n },\n}\n\n// Prevent name mangling\nObject.defineProperty(BrowserStore, 'name', { value: 'BrowserStore' })\n\nexport default BrowserStore\n"],"names":["BrowserStore","options","seneca","init","ohr","handleResponse","a","n","msglog","makeApiMsg","msg","ctx","apimsg","apimsgtm","pn","pv","store","reply","_meta","logn","logstart","logreq","err","res","apimeta","logres","_msg","meta","args","canonStr","_ctx","q","ent","parts","_apimeta","_apimsg","list","item"],"mappings":"6NAEA,SAASA,EAAwBC,EAAc,CAC7C,IAAIC,EAAc,KAEdC,EAAOD,EAAO,OAAO,aAAa,EAElCE,EAAMH,EAAQ,eACdI,EAAiB,CAAC,OAAQ,OAAQ,OAAQ,QAAQ,EAAE,OACtD,CAACC,EAAQC,KAAQD,EAAEC,CAAC,EAAIH,EAAIG,CAAC,GAAKH,EAAI,IAAME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,EAAI,MAAM,EAAE,MAAMK,CAAI,CAAC,EACrDlB,IAAAA,EAAK,IAAM,KAAK,IAAI,GAC7BF,EAAMmB,CAAI,CAAA,KACL,CACDd,IAAAA,EAAMC,GAAOA,EAAI,IACrBD,EACEA,GACA,IAAI,MACF,iBAAiBX,EAAI,GAAG,IAAIkB,EAC1B3B,EACAS,CACD,CAAA,sBAAA,EAELM,EAAMK,CAAG,CACX,CACF,CACF,CACF,EAGA,OAAO,eAAetB,EAAc,OAAQ,CAAE,MAAO,eAAgB"} \ No newline at end of file diff --git a/package.json b/package.json index 9ccfef2..47bf6a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seneca/browser-store", - "version": "0.0.5", + "version": "0.0.6", "description": "Seneca browwer storage plugin.", "main": "dist/browser-store.cjs.js", "module": "dist/browser-store.es.js", @@ -46,13 +46,13 @@ "seneca-entity": ">=25" }, "devDependencies": { - "@typescript-eslint/eslint-plugin": "^6.18.1", - "@typescript-eslint/parser": "^6.18.1", - "dts-bundle-generator": "^9.2.3", + "@typescript-eslint/eslint-plugin": "^7.0.1", + "@typescript-eslint/parser": "^7.0.1", + "dts-bundle-generator": "^9.3.1", "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-prettier": "^5.1.3", - "prettier": "^3.1.1", + "prettier": "^3.2.5", "seneca": "^3.33.0", "seneca-entity": "^25.1.1", "seneca-promisify": "^3.7.1", @@ -62,8 +62,8 @@ "ts-node": "^10.9.2", "tslib": "^2.6.2", "typescript": "^5.3.3", - "vite": "^5.0.11", - "vite-plugin-dts": "^3.7.0" + "vite": "^5.1.3", + "vite-plugin-dts": "^3.7.2" }, "files": [ "README.md", diff --git a/src/browser-store.ts b/src/browser-store.ts index 8311beb..8403b62 100644 --- a/src/browser-store.ts +++ b/src/browser-store.ts @@ -1,4 +1,4 @@ -/* Copyright (c) 2023 Richard Rodger and other contributors, MIT License */ +/* Copyright (c) 2023-2024 Richard Rodger and other contributors, MIT License */ function BrowserStore(this: any, options: any) { let seneca: any = this @@ -53,7 +53,16 @@ function BrowserStore(this: any, options: any) { apimsg, function save_result(this: any, err: Error, res: any, apimeta: any) { logn && logres(logn, arguments) - return handleResponse.save(this, ctx, reply, err, res, apimeta, logn) + return handleResponse.save( + this, + ctx, + reply, + err, + res, + apimsg, + apimeta, + logn, + ) }, ) }, @@ -68,7 +77,16 @@ function BrowserStore(this: any, options: any) { apimsg, function load_result(this: any, err: Error, res: any, apimeta: any) { logn && logres(logn, arguments) - return handleResponse.load(this, ctx, reply, err, res, apimeta, logn) + return handleResponse.load( + this, + ctx, + reply, + err, + res, + apimsg, + apimeta, + logn, + ) }, ) }, @@ -83,7 +101,16 @@ function BrowserStore(this: any, options: any) { apimsg, function list_result(this: any, err: Error, res: any, apimeta: any) { logn && logres(logn, arguments) - return handleResponse.list(this, ctx, reply, err, res, apimeta, logn) + return handleResponse.list( + this, + ctx, + reply, + err, + res, + apimsg, + apimeta, + logn, + ) }, ) }, @@ -104,6 +131,7 @@ function BrowserStore(this: any, options: any) { reply, err, res, + apimsg, apimeta, logn, ) @@ -216,6 +244,7 @@ BrowserStore.defaults = { reply: any, err: Error, res: any, + apimsg: any, _apimeta: any, logn: any, ) { @@ -225,8 +254,18 @@ BrowserStore.defaults = { return reply(err) } - if (res && res.ok) { - return reply(res.ent) + // TODO: debounce response could be empty object - review + if ('entity' === apimsg.load && (null == res || null == res.ok)) { + return reply(null) + } + + if (res && res.ok && res.item) { + let ent = seneca.entity({ + zone: ctx.zone, + base: ctx.base, + name: ctx.name, + }) + return reply(ent.make$().data$(res.item)) } else { let err = res && res.err err = @@ -244,6 +283,7 @@ BrowserStore.defaults = { reply: any, err: Error, res: any, + _apimsg: any, _apimeta: any, logn: any, ) {