@aghamdi/shared-types 0.2.1 → 0.2.2

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,4 @@ 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
+ export { type DeviceNotification, MiaNotifications, MiaServerResponse, type Notification, type UserNotification };
package/dist/index.d.ts CHANGED
@@ -25,4 +25,4 @@ 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
+ export { type DeviceNotification, 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,6 +39,7 @@ 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 = {};
@@ -73,31 +77,61 @@ MiaServerResponse.sendError = (error) => {
73
77
  return response;
74
78
  };
75
79
 
76
- // src/notifications.ts
80
+ // src/MessagingBus.ts
77
81
  var import_nats = require("nats");
78
- var MiaNotifications = class {
79
- static sendtoDevice(deviceId, body) {
82
+ var MessagingBus = class {
83
+ static subscribe(channel, queue, onMessage) {
84
+ return __async(this, null, function* () {
85
+ const nc = yield (0, import_nats.connect)({ servers: process.env.NATS_HOST });
86
+ const sc = (0, import_nats.JSONCodec)();
87
+ const subscription = nc.subscribe(channel, { queue });
88
+ ((sub) => __async(this, null, function* () {
89
+ console.log(`listening for ${sub.getSubject()} requests...`);
90
+ try {
91
+ for (var iter = __forAwait(sub), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
92
+ const m = temp.value;
93
+ const msg = sc.decode(m.data);
94
+ onMessage(msg);
95
+ }
96
+ } catch (temp) {
97
+ error = [temp];
98
+ } finally {
99
+ try {
100
+ more && (temp = iter.return) && (yield temp.call(iter));
101
+ } finally {
102
+ if (error)
103
+ throw error[0];
104
+ }
105
+ }
106
+ console.log(`subscription ${sub.getSubject()} drained.`);
107
+ }))(subscription);
108
+ });
109
+ }
110
+ static publish(channel, message) {
80
111
  return __async(this, null, function* () {
81
112
  if (!process.env.NATS_HOST) {
82
113
  throw new Error("NATS_HOST ENV is not defined");
83
114
  }
84
115
  const nc = yield (0, import_nats.connect)({ servers: process.env.NATS_HOST });
85
116
  const jc = (0, import_nats.JSONCodec)();
86
- const message = { deviceId, body };
87
- nc.publish("mia.notifications.test", jc.encode({ message }));
117
+ nc.publish(channel, jc.encode({ message }));
88
118
  yield nc.drain();
89
119
  });
90
120
  }
121
+ };
122
+
123
+ // src/notifications.ts
124
+ var MiaNotifications = class {
125
+ static sendtoDevice(deviceId, body) {
126
+ return __async(this, null, function* () {
127
+ const message = { deviceId, body };
128
+ yield MessagingBus.publish("mia.notifications.test", message);
129
+ });
130
+ }
91
131
  static sendtoUserDevices(userId, body) {
92
132
  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
133
  const message = { userId, body };
99
- nc.publish("mia.notifications.test", jc.encode({ message }));
100
- yield nc.drain();
134
+ yield MessagingBus.publish("mia.notifications.test", message);
101
135
  });
102
136
  }
103
137
  };
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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0B,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/MessagingBus.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\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 \"./MessagingBus\";\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;;;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,31 +51,61 @@ MiaServerResponse.sendError = (error) => {
47
51
  return response;
48
52
  };
49
53
 
50
- // src/notifications.ts
54
+ // src/MessagingBus.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
  };
@@ -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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/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 \"./MessagingBus\";\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,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B,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.2",
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",