@aghamdi/shared-types 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -25,4 +25,9 @@ declare class MiaNotifications {
25
25
  static sendtoUserDevices(userId: string, body: string): Promise<void>;
26
26
  }
27
27
 
28
- export { type DeviceNotification, MiaNotifications, MiaServerResponse, type UserNotification };
28
+ declare class MessagingBus {
29
+ static subscribe(channel: string, queue: string, onMessage: Function): Promise<void>;
30
+ static publish(channel: string, message: any): Promise<void>;
31
+ }
32
+
33
+ export { type DeviceNotification, MessagingBus, MiaNotifications, MiaServerResponse, type Notification, type UserNotification };
package/dist/index.d.ts CHANGED
@@ -25,4 +25,9 @@ declare class MiaNotifications {
25
25
  static sendtoUserDevices(userId: string, body: string): Promise<void>;
26
26
  }
27
27
 
28
- export { type DeviceNotification, MiaNotifications, MiaServerResponse, type UserNotification };
28
+ declare class MessagingBus {
29
+ static subscribe(channel: string, queue: string, onMessage: Function): Promise<void>;
30
+ static publish(channel: string, message: any): Promise<void>;
31
+ }
32
+
33
+ export { type DeviceNotification, MessagingBus, MiaNotifications, MiaServerResponse, type Notification, type UserNotification };
package/dist/index.js CHANGED
@@ -3,6 +3,9 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __knownSymbol = (name, symbol) => {
7
+ return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
8
+ };
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
8
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -36,10 +39,12 @@ var __async = (__this, __arguments, generator) => {
36
39
  step((generator = generator.apply(__this, __arguments)).next());
37
40
  });
38
41
  };
42
+ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
39
43
 
40
44
  // src/index.ts
41
45
  var src_exports = {};
42
46
  __export(src_exports, {
47
+ MessagingBus: () => MessagingBus,
43
48
  MiaNotifications: () => MiaNotifications,
44
49
  MiaServerResponse: () => MiaServerResponse
45
50
  });
@@ -73,36 +78,67 @@ MiaServerResponse.sendError = (error) => {
73
78
  return response;
74
79
  };
75
80
 
76
- // src/notifications.ts
81
+ // src/messaging-bus.ts
77
82
  var import_nats = require("nats");
78
- var MiaNotifications = class {
79
- static sendtoDevice(deviceId, body) {
83
+ var MessagingBus = class {
84
+ static subscribe(channel, queue, onMessage) {
85
+ return __async(this, null, function* () {
86
+ const nc = yield (0, import_nats.connect)({ servers: process.env.NATS_HOST });
87
+ const sc = (0, import_nats.JSONCodec)();
88
+ const subscription = nc.subscribe(channel, { queue });
89
+ ((sub) => __async(this, null, function* () {
90
+ console.log(`listening for ${sub.getSubject()} requests...`);
91
+ try {
92
+ for (var iter = __forAwait(sub), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
93
+ const m = temp.value;
94
+ const msg = sc.decode(m.data);
95
+ onMessage(msg);
96
+ }
97
+ } catch (temp) {
98
+ error = [temp];
99
+ } finally {
100
+ try {
101
+ more && (temp = iter.return) && (yield temp.call(iter));
102
+ } finally {
103
+ if (error)
104
+ throw error[0];
105
+ }
106
+ }
107
+ console.log(`subscription ${sub.getSubject()} drained.`);
108
+ }))(subscription);
109
+ });
110
+ }
111
+ static publish(channel, message) {
80
112
  return __async(this, null, function* () {
81
113
  if (!process.env.NATS_HOST) {
82
114
  throw new Error("NATS_HOST ENV is not defined");
83
115
  }
84
116
  const nc = yield (0, import_nats.connect)({ servers: process.env.NATS_HOST });
85
117
  const jc = (0, import_nats.JSONCodec)();
86
- const message = { deviceId, body };
87
- nc.publish("mia.notifications.test", jc.encode({ message }));
118
+ nc.publish(channel, jc.encode({ message }));
88
119
  yield nc.drain();
89
120
  });
90
121
  }
122
+ };
123
+
124
+ // src/notifications.ts
125
+ var MiaNotifications = class {
126
+ static sendtoDevice(deviceId, body) {
127
+ return __async(this, null, function* () {
128
+ const message = { deviceId, body };
129
+ yield MessagingBus.publish("mia.notifications.test", message);
130
+ });
131
+ }
91
132
  static sendtoUserDevices(userId, body) {
92
133
  return __async(this, null, function* () {
93
- if (!process.env.NATS_HOST) {
94
- throw new Error("NATS_HOST ENV is not defined");
95
- }
96
- const nc = yield (0, import_nats.connect)({ servers: process.env.NATS_HOST });
97
- const jc = (0, import_nats.JSONCodec)();
98
134
  const message = { userId, body };
99
- nc.publish("mia.notifications.test", jc.encode({ message }));
100
- yield nc.drain();
135
+ yield MessagingBus.publish("mia.notifications.test", message);
101
136
  });
102
137
  }
103
138
  };
104
139
  // Annotate the CommonJS export names for ESM import in node:
105
140
  0 && (module.exports = {
141
+ MessagingBus,
106
142
  MiaNotifications,
107
143
  MiaServerResponse
108
144
  });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/mia-server-response.ts","../src/notifications.ts"],"sourcesContent":["export * from './mia-server-response'\nexport * from './notifications'","export class MiaServerResponse {\n status?: number;\n message?: string;\n data: any;\n\n static send = (status: number, message: string, data: any) => {\n const response: MiaServerResponse = {\n status: status,\n message: message,\n data: data\n };\n return response;\n }\n static sendData = (data: any) => {\n const response: MiaServerResponse = {\n status: 200,\n message: \"Success\",\n data: data\n };\n return response;\n }\n static sendError = (error: any) => {\n const response: MiaServerResponse = {\n status: 400,\n message: \"Failed\",\n data: error\n };\n return response;\n }\n}\n","import { connect, JSONCodec } from \"nats\";\n\ninterface Notification {\n title?: string,\n body: string,\n badge?: number,\n topic?: string,\n payload?: any\n}\nexport interface UserNotification extends Notification {\n userId: string\n}\nexport interface DeviceNotification extends Notification {\n deviceId: string\n}\n\nexport class MiaNotifications {\n static async sendtoDevice(deviceId: string, body: string) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n const message: DeviceNotification = { deviceId, body };\n nc.publish(\"mia.notifications.test\", jc.encode({ message }));\n await nc.drain();\n }\n static async sendtoUserDevices(userId: string, body: string) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n const message: UserNotification = { userId, body };\n nc.publish(\"mia.notifications.test\", jc.encode({ message }));\n await nc.drain();\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,oBAAN,MAAwB;AA6B/B;AA7Ba,kBAKJ,OAAO,CAAC,QAAgB,SAAiB,SAAc;AAC5D,QAAM,WAA8B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,SAAO;AACT;AAZW,kBAaJ,WAAW,CAAC,SAAc;AAC/B,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AApBW,kBAqBJ,YAAY,CAAC,UAAe;AACjC,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AACA,SAAO;AACT;;;AC5BF,kBAAmC;AAgB5B,IAAM,mBAAN,MAAuB;AAAA,EAC5B,OAAa,aAAa,UAAkB,MAAc;AAAA;AACxD,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,UAAM,qBAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,SAAK,uBAAU;AACrB,YAAM,UAA8B,EAAE,UAAU,KAAK;AACrD,SAAG,QAAQ,0BAA0B,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3D,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AAAA,EACA,OAAa,kBAAkB,QAAgB,MAAc;AAAA;AAC3D,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,UAAM,qBAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,SAAK,uBAAU;AACrB,YAAM,UAA4B,EAAE,QAAQ,KAAK;AACjD,SAAG,QAAQ,0BAA0B,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3D,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/mia-server-response.ts","../src/messaging-bus.ts","../src/notifications.ts"],"sourcesContent":["export * from './mia-server-response'\nexport * from './notifications'\nexport * from './messaging-bus'","export class MiaServerResponse {\n status?: number;\n message?: string;\n data: any;\n\n static send = (status: number, message: string, data: any) => {\n const response: MiaServerResponse = {\n status: status,\n message: message,\n data: data\n };\n return response;\n }\n static sendData = (data: any) => {\n const response: MiaServerResponse = {\n status: 200,\n message: \"Success\",\n data: data\n };\n return response;\n }\n static sendError = (error: any) => {\n const response: MiaServerResponse = {\n status: 400,\n message: \"Failed\",\n data: error\n };\n return response;\n }\n}\n","import { connect, JSONCodec } from \"nats\";\n\nexport class MessagingBus {\n static async subscribe(channel: string, queue: string, onMessage: Function) {\n const nc = await connect({ servers: process.env.NATS_HOST });\n const sc = JSONCodec();\n const subscription = nc.subscribe(channel, { queue: queue });\n\n (async (sub) => {\n\n console.log(`listening for ${sub.getSubject()} requests...`);\n\n for await (const m of sub) {\n const msg = sc.decode(m.data);\n onMessage(msg);\n }\n\n console.log(`subscription ${sub.getSubject()} drained.`);\n })(subscription);\n }\n\n static async publish(channel: string, message: any) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n nc.publish(channel, jc.encode({ message }));\n await nc.drain();\n }\n}\n","import { connect, JSONCodec } from \"nats\";\nimport { MessagingBus } from \"./messaging-bus\";\nexport interface Notification {\n title?: string,\n body: string,\n badge?: number,\n topic?: string,\n payload?: any\n}\nexport interface UserNotification extends Notification {\n userId: string\n}\nexport interface DeviceNotification extends Notification {\n deviceId: string\n}\n\nexport class MiaNotifications {\n static async sendtoDevice(deviceId: string, body: string) {\n const message: DeviceNotification = { deviceId, body };\n await MessagingBus.publish(\"mia.notifications.test\", message);\n }\n static async sendtoUserDevices(userId: string, body: string) {\n const message: UserNotification = { userId, body };\n await MessagingBus.publish(\"mia.notifications.test\", message);\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,oBAAN,MAAwB;AA6B/B;AA7Ba,kBAKJ,OAAO,CAAC,QAAgB,SAAiB,SAAc;AAC5D,QAAM,WAA8B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,SAAO;AACT;AAZW,kBAaJ,WAAW,CAAC,SAAc;AAC/B,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AApBW,kBAqBJ,YAAY,CAAC,UAAe;AACjC,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AACA,SAAO;AACT;;;AC5BF,kBAAmC;AAE5B,IAAM,eAAN,MAAmB;AAAA,EACxB,OAAa,UAAU,SAAiB,OAAe,WAAqB;AAAA;AAC1E,YAAM,KAAK,UAAM,qBAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,SAAK,uBAAU;AACrB,YAAM,eAAe,GAAG,UAAU,SAAS,EAAE,MAAa,CAAC;AAE3D,OAAC,CAAO,QAAQ;AAEd,gBAAQ,IAAI,iBAAiB,IAAI,WAAW,CAAC,cAAc;AAE3D;AAAA,qCAAsB,MAAtB,0EAA2B;AAAhB,kBAAM,IAAjB;AACE,kBAAM,MAAM,GAAG,OAAO,EAAE,IAAI;AAC5B,sBAAU,GAAG;AAAA,UACf;AAAA,iBAHA,MAZN;AAYM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,gBAAQ,IAAI,gBAAgB,IAAI,WAAW,CAAC,WAAW;AAAA,MACzD,IAAG,YAAY;AAAA,IACjB;AAAA;AAAA,EAEA,OAAa,QAAQ,SAAiB,SAAc;AAAA;AAClD,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,UAAM,qBAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,SAAK,uBAAU;AACrB,SAAG,QAAQ,SAAS,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC1C,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AACF;;;ACdO,IAAM,mBAAN,MAAuB;AAAA,EAC5B,OAAa,aAAa,UAAkB,MAAc;AAAA;AACxD,YAAM,UAA8B,EAAE,UAAU,KAAK;AACrD,YAAM,aAAa,QAAQ,0BAA0B,OAAO;AAAA,IAC9D;AAAA;AAAA,EACA,OAAa,kBAAkB,QAAgB,MAAc;AAAA;AAC3D,YAAM,UAA4B,EAAE,QAAQ,KAAK;AACjD,YAAM,aAAa,QAAQ,0BAA0B,OAAO;AAAA,IAC9D;AAAA;AACF;","names":[]}
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ var __knownSymbol = (name, symbol) => {
2
+ return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
3
+ };
1
4
  var __async = (__this, __arguments, generator) => {
2
5
  return new Promise((resolve, reject) => {
3
6
  var fulfilled = (value) => {
@@ -18,6 +21,7 @@ var __async = (__this, __arguments, generator) => {
18
21
  step((generator = generator.apply(__this, __arguments)).next());
19
22
  });
20
23
  };
24
+ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
21
25
 
22
26
  // src/mia-server-response.ts
23
27
  var MiaServerResponse = class {
@@ -47,35 +51,66 @@ MiaServerResponse.sendError = (error) => {
47
51
  return response;
48
52
  };
49
53
 
50
- // src/notifications.ts
54
+ // src/messaging-bus.ts
51
55
  import { connect, JSONCodec } from "nats";
52
- var MiaNotifications = class {
53
- static sendtoDevice(deviceId, body) {
56
+ var MessagingBus = class {
57
+ static subscribe(channel, queue, onMessage) {
58
+ return __async(this, null, function* () {
59
+ const nc = yield connect({ servers: process.env.NATS_HOST });
60
+ const sc = JSONCodec();
61
+ const subscription = nc.subscribe(channel, { queue });
62
+ ((sub) => __async(this, null, function* () {
63
+ console.log(`listening for ${sub.getSubject()} requests...`);
64
+ try {
65
+ for (var iter = __forAwait(sub), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
66
+ const m = temp.value;
67
+ const msg = sc.decode(m.data);
68
+ onMessage(msg);
69
+ }
70
+ } catch (temp) {
71
+ error = [temp];
72
+ } finally {
73
+ try {
74
+ more && (temp = iter.return) && (yield temp.call(iter));
75
+ } finally {
76
+ if (error)
77
+ throw error[0];
78
+ }
79
+ }
80
+ console.log(`subscription ${sub.getSubject()} drained.`);
81
+ }))(subscription);
82
+ });
83
+ }
84
+ static publish(channel, message) {
54
85
  return __async(this, null, function* () {
55
86
  if (!process.env.NATS_HOST) {
56
87
  throw new Error("NATS_HOST ENV is not defined");
57
88
  }
58
89
  const nc = yield connect({ servers: process.env.NATS_HOST });
59
90
  const jc = JSONCodec();
60
- const message = { deviceId, body };
61
- nc.publish("mia.notifications.test", jc.encode({ message }));
91
+ nc.publish(channel, jc.encode({ message }));
62
92
  yield nc.drain();
63
93
  });
64
94
  }
95
+ };
96
+
97
+ // src/notifications.ts
98
+ var MiaNotifications = class {
99
+ static sendtoDevice(deviceId, body) {
100
+ return __async(this, null, function* () {
101
+ const message = { deviceId, body };
102
+ yield MessagingBus.publish("mia.notifications.test", message);
103
+ });
104
+ }
65
105
  static sendtoUserDevices(userId, body) {
66
106
  return __async(this, null, function* () {
67
- if (!process.env.NATS_HOST) {
68
- throw new Error("NATS_HOST ENV is not defined");
69
- }
70
- const nc = yield connect({ servers: process.env.NATS_HOST });
71
- const jc = JSONCodec();
72
107
  const message = { userId, body };
73
- nc.publish("mia.notifications.test", jc.encode({ message }));
74
- yield nc.drain();
108
+ yield MessagingBus.publish("mia.notifications.test", message);
75
109
  });
76
110
  }
77
111
  };
78
112
  export {
113
+ MessagingBus,
79
114
  MiaNotifications,
80
115
  MiaServerResponse
81
116
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mia-server-response.ts","../src/notifications.ts"],"sourcesContent":["export class MiaServerResponse {\n status?: number;\n message?: string;\n data: any;\n\n static send = (status: number, message: string, data: any) => {\n const response: MiaServerResponse = {\n status: status,\n message: message,\n data: data\n };\n return response;\n }\n static sendData = (data: any) => {\n const response: MiaServerResponse = {\n status: 200,\n message: \"Success\",\n data: data\n };\n return response;\n }\n static sendError = (error: any) => {\n const response: MiaServerResponse = {\n status: 400,\n message: \"Failed\",\n data: error\n };\n return response;\n }\n}\n","import { connect, JSONCodec } from \"nats\";\n\ninterface Notification {\n title?: string,\n body: string,\n badge?: number,\n topic?: string,\n payload?: any\n}\nexport interface UserNotification extends Notification {\n userId: string\n}\nexport interface DeviceNotification extends Notification {\n deviceId: string\n}\n\nexport class MiaNotifications {\n static async sendtoDevice(deviceId: string, body: string) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n const message: DeviceNotification = { deviceId, body };\n nc.publish(\"mia.notifications.test\", jc.encode({ message }));\n await nc.drain();\n }\n static async sendtoUserDevices(userId: string, body: string) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n const message: UserNotification = { userId, body };\n nc.publish(\"mia.notifications.test\", jc.encode({ message }));\n await nc.drain();\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAO,IAAM,oBAAN,MAAwB;AA6B/B;AA7Ba,kBAKJ,OAAO,CAAC,QAAgB,SAAiB,SAAc;AAC5D,QAAM,WAA8B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,SAAO;AACT;AAZW,kBAaJ,WAAW,CAAC,SAAc;AAC/B,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AApBW,kBAqBJ,YAAY,CAAC,UAAe;AACjC,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AACA,SAAO;AACT;;;AC5BF,SAAS,SAAS,iBAAiB;AAgB5B,IAAM,mBAAN,MAAuB;AAAA,EAC5B,OAAa,aAAa,UAAkB,MAAc;AAAA;AACxD,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,MAAM,QAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,KAAK,UAAU;AACrB,YAAM,UAA8B,EAAE,UAAU,KAAK;AACrD,SAAG,QAAQ,0BAA0B,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3D,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AAAA,EACA,OAAa,kBAAkB,QAAgB,MAAc;AAAA;AAC3D,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,MAAM,QAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,KAAK,UAAU;AACrB,YAAM,UAA4B,EAAE,QAAQ,KAAK;AACjD,SAAG,QAAQ,0BAA0B,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3D,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/mia-server-response.ts","../src/messaging-bus.ts","../src/notifications.ts"],"sourcesContent":["export class MiaServerResponse {\n status?: number;\n message?: string;\n data: any;\n\n static send = (status: number, message: string, data: any) => {\n const response: MiaServerResponse = {\n status: status,\n message: message,\n data: data\n };\n return response;\n }\n static sendData = (data: any) => {\n const response: MiaServerResponse = {\n status: 200,\n message: \"Success\",\n data: data\n };\n return response;\n }\n static sendError = (error: any) => {\n const response: MiaServerResponse = {\n status: 400,\n message: \"Failed\",\n data: error\n };\n return response;\n }\n}\n","import { connect, JSONCodec } from \"nats\";\n\nexport class MessagingBus {\n static async subscribe(channel: string, queue: string, onMessage: Function) {\n const nc = await connect({ servers: process.env.NATS_HOST });\n const sc = JSONCodec();\n const subscription = nc.subscribe(channel, { queue: queue });\n\n (async (sub) => {\n\n console.log(`listening for ${sub.getSubject()} requests...`);\n\n for await (const m of sub) {\n const msg = sc.decode(m.data);\n onMessage(msg);\n }\n\n console.log(`subscription ${sub.getSubject()} drained.`);\n })(subscription);\n }\n\n static async publish(channel: string, message: any) {\n if (!process.env.NATS_HOST) {\n throw new Error(\"NATS_HOST ENV is not defined\");\n }\n const nc = await connect({ servers: process.env.NATS_HOST });\n const jc = JSONCodec();\n nc.publish(channel, jc.encode({ message }));\n await nc.drain();\n }\n}\n","import { connect, JSONCodec } from \"nats\";\nimport { MessagingBus } from \"./messaging-bus\";\nexport interface Notification {\n title?: string,\n body: string,\n badge?: number,\n topic?: string,\n payload?: any\n}\nexport interface UserNotification extends Notification {\n userId: string\n}\nexport interface DeviceNotification extends Notification {\n deviceId: string\n}\n\nexport class MiaNotifications {\n static async sendtoDevice(deviceId: string, body: string) {\n const message: DeviceNotification = { deviceId, body };\n await MessagingBus.publish(\"mia.notifications.test\", message);\n }\n static async sendtoUserDevices(userId: string, body: string) {\n const message: UserNotification = { userId, body };\n await MessagingBus.publish(\"mia.notifications.test\", message);\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,IAAM,oBAAN,MAAwB;AA6B/B;AA7Ba,kBAKJ,OAAO,CAAC,QAAgB,SAAiB,SAAc;AAC5D,QAAM,WAA8B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,SAAO;AACT;AAZW,kBAaJ,WAAW,CAAC,SAAc;AAC/B,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AApBW,kBAqBJ,YAAY,CAAC,UAAe;AACjC,QAAM,WAA8B;AAAA,IAClC,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AACA,SAAO;AACT;;;AC5BF,SAAS,SAAS,iBAAiB;AAE5B,IAAM,eAAN,MAAmB;AAAA,EACxB,OAAa,UAAU,SAAiB,OAAe,WAAqB;AAAA;AAC1E,YAAM,KAAK,MAAM,QAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,KAAK,UAAU;AACrB,YAAM,eAAe,GAAG,UAAU,SAAS,EAAE,MAAa,CAAC;AAE3D,OAAC,CAAO,QAAQ;AAEd,gBAAQ,IAAI,iBAAiB,IAAI,WAAW,CAAC,cAAc;AAE3D;AAAA,qCAAsB,MAAtB,0EAA2B;AAAhB,kBAAM,IAAjB;AACE,kBAAM,MAAM,GAAG,OAAO,EAAE,IAAI;AAC5B,sBAAU,GAAG;AAAA,UACf;AAAA,iBAHA,MAZN;AAYM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,gBAAQ,IAAI,gBAAgB,IAAI,WAAW,CAAC,WAAW;AAAA,MACzD,IAAG,YAAY;AAAA,IACjB;AAAA;AAAA,EAEA,OAAa,QAAQ,SAAiB,SAAc;AAAA;AAClD,UAAI,CAAC,QAAQ,IAAI,WAAW;AAC1B,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,YAAM,KAAK,MAAM,QAAQ,EAAE,SAAS,QAAQ,IAAI,UAAU,CAAC;AAC3D,YAAM,KAAK,UAAU;AACrB,SAAG,QAAQ,SAAS,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC1C,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA;AACF;;;ACdO,IAAM,mBAAN,MAAuB;AAAA,EAC5B,OAAa,aAAa,UAAkB,MAAc;AAAA;AACxD,YAAM,UAA8B,EAAE,UAAU,KAAK;AACrD,YAAM,aAAa,QAAQ,0BAA0B,OAAO;AAAA,IAC9D;AAAA;AAAA,EACA,OAAa,kBAAkB,QAAgB,MAAc;AAAA;AAC3D,YAAM,UAA4B,EAAE,QAAQ,KAAK;AACjD,YAAM,aAAa,QAAQ,0BAA0B,OAAO;AAAA,IAC9D;AAAA;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aghamdi/shared-types",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Provide typescript interfaces for common types and some classes",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.js",