UNPKG

@frames.js/debugger

Version:

Debug your Frames.js applications

44 lines (43 loc) 4.04 kB
"use strict";exports.id=9842,exports.ids=[9842],exports.modules={79842:(e,t,r)=>{r.r(t),r.d(t,{SQLiteStorage:()=>u,getStorage:()=>z});var i=r(85890),n=r.n(i),s=r(12725),a=r(6005),o=r.n(a),d=r(49411),l=r.n(d),f=r(53797),c=r(49974);let p=f.z.object({id:f.z.string(),fid:f.z.number(),frameAppUrl:f.z.string(),namespaceUrl:f.z.string(),webhookUrl:f.z.string(),frame:f.z.preprocess(e=>"string"==typeof e?JSON.parse(e):e,f.z.discriminatedUnion("status",[f.z.object({status:f.z.literal("added"),notificationDetails:f.z.nullable(f.z.object({url:f.z.string(),token:f.z.string()}))}),f.z.object({status:f.z.literal("removed")})]))}),m=f.z.preprocess(e=>"string"==typeof e?JSON.parse(e):e,f.z.discriminatedUnion("type",[f.z.object({type:f.z.literal("notification"),notification:c.M,id:f.z.string()}),f.z.object({type:f.z.literal("event"),event:s._G,id:f.z.string()}),f.z.object({type:f.z.literal("event_success"),event:s._G,id:f.z.string(),eventId:f.z.string()}),f.z.object({type:f.z.literal("event_failure"),event:s._G,id:f.z.string(),eventId:f.z.string(),message:f.z.string(),response:f.z.optional(f.z.object({status:f.z.number(),headers:f.z.record(f.z.union([f.z.string(),f.z.array(f.z.string())])),body:f.z.string()}))})])),E=f.z.object({id:f.z.string(),namespaceId:f.z.string(),event:m}),g=null;async function z(e){if(!g){let t=l().resolve(process.cwd(),"./notifications.db"),r=new(n())(t);r.exec(` CREATE TABLE IF NOT EXISTS notification_settings ( id TEXT PRIMARY KEY, fid INTEGER, frameAppUrl TEXT, namespaceUrl TEXT, webhookUrl TEXT, frame TEXT ); CREATE TABLE IF NOT EXISTS event_log ( id TEXT PRIMARY KEY, namespaceId TEXT, event TEXT ); `),g=new u(r,e)}return g}class u{constructor(e,t){this.db=e,this.serverUrl=t}async registerNamespace(e,t){let r=new URL(`/notifications/${e}`,this.serverUrl).toString(),i={id:e,fid:t.fid,frameAppUrl:t.frameAppUrl,namespaceUrl:r,webhookUrl:t.webhookUrl,frame:{status:"removed"}};return this.db.prepare(` INSERT INTO notification_settings (id, fid, frameAppUrl, namespaceUrl, webhookUrl, frame) VALUES (?, ?, ?, ?, ?, ?) `).run(e,t.fid,t.frameAppUrl,i.namespaceUrl,t.webhookUrl,JSON.stringify(i.frame)),i}async getNamespace(e){let t=this.db.prepare(` SELECT * FROM notification_settings WHERE id = ? `).get(e);return t?p.parse(t):null}async addFrame(e){if("added"===e.frame.status)throw Error("Frame is already added");let t=o().randomUUID(),r={url:new URL(`/notifications/${e.id}/send`,this.serverUrl).toString(),token:t};return e.frame={status:"added",notificationDetails:r},this.db.prepare(` UPDATE notification_settings SET frame = ? WHERE id = ? `).run(JSON.stringify(e.frame),e.id),r}async removeFrame(e){if("removed"===e.frame.status)throw Error("Frame is already removed");e.frame={status:"removed"},this.db.prepare(` UPDATE notification_settings SET frame = ? WHERE id = ? `).run(JSON.stringify(e.frame),e.id)}async enableNotifications(e){if("removed"===e.frame.status)throw Error("Frame is not added");let t={token:o().randomUUID(),url:new URL(`/notifications/${e.id}/send`,this.serverUrl).toString()};return e.frame.notificationDetails=t,this.db.prepare(` UPDATE notification_settings SET frame = ? WHERE id = ? `).run(JSON.stringify(e.frame),e.id),t}async disableNotifications(e){if("removed"===e.frame.status)throw Error("Frame is not added");e.frame.notificationDetails=null,this.db.prepare(` UPDATE notification_settings SET frame = ? WHERE id = ? `).run(JSON.stringify(e.frame),e.id)}async recordEvent(e,t){this.db.prepare(` INSERT INTO event_log (id, namespaceId, event) VALUES (?, ?, ?) `).run(t.id,e,JSON.stringify(t))}async listEvents(e){let t=this.db.prepare(` SELECT * FROM event_log WHERE namespaceId = ? `).all(e);return this.db.prepare(` DELETE FROM event_log WHERE namespaceId = ? `).run(e),t.map(e=>E.parse(e).event)}}}};