corebasic 1.0.30 → 1.0.32

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/index.js CHANGED
@@ -1,15 +1,17 @@
1
1
 
2
2
 
3
- import Elabase from './libs/elabase.js'
4
- import Kafka from './libs/kafka.js'
5
- import Utils from './libs/utils.js'
6
- import Session from './libs/session.js'
7
- import Auth from './libs/auth.js'
3
+ import * as Elabase from './libs/elabase.js'
4
+ import * as Kafka from './libs/kafka.js'
5
+ import * as Utils from './libs/utils.js'
6
+ import * as Session from './libs/session.js'
7
+ import * as Auth from './libs/auth.js'
8
+ import * as Features from './libs/features.js'
8
9
 
9
10
  export {
10
11
  Elabase,
11
12
  Kafka,
12
13
  Utils,
13
14
  Session,
14
- Auth
15
+ Auth,
16
+ Features
15
17
  }
package/libs/ObjectId.js CHANGED
@@ -121,4 +121,3 @@ ObjectId.prototype.toString = function () {
121
121
  '000000'.substr(0, 6 - increment.length) + increment;
122
122
  };
123
123
 
124
-
package/libs/auth.js CHANGED
@@ -1,9 +1,9 @@
1
- import axios from 'axios'
1
+ import * as axios from 'axios'
2
2
  import otpGenerator from 'otp-generator'
3
3
 
4
- import Elabase from './elabase.js'
5
- import Utils from './utils.js'
6
- import Session from './session.js'
4
+ import * as Elabase from './elabase.js'
5
+ import * as Utils from './utils.js'
6
+ import * as Session from './session.js'
7
7
 
8
8
  let validateFn, validateErrMessage
9
9
  export const validate = (callback, errMessage) => {
package/libs/elabase.js CHANGED
@@ -1,4 +1,4 @@
1
- import axios from 'axios'
1
+ import * as axios from 'axios'
2
2
 
3
3
 
4
4
 
@@ -0,0 +1,97 @@
1
+ import * as Elabase from './elabase.js'
2
+ import * as Kafka from './kafka.js'
3
+ import { createRequire } from 'module';
4
+ const require = createRequire(import.meta.url);
5
+
6
+
7
+ let features = {}
8
+ let apis = {}
9
+
10
+ export function register(app, file) {
11
+ features = require(file)
12
+
13
+ Object.entries(features).forEach(async ([name,feature]) => {
14
+ apis[feature.api] = apis[feature.api] ?? {}
15
+ apis[feature.api][name] = feature
16
+
17
+ let featureName = name
18
+
19
+ name = name.split('.')
20
+ let handler = name.pop()
21
+ name = name.join('/')
22
+ let module = name
23
+
24
+ if (name === 'transactions/query')
25
+ return
26
+
27
+ feature.handler = (await import(`./${name}.js`))[handler]
28
+
29
+ if (feature.api.split(' ')[0].toLowerCase() !== "get") {
30
+ let topic = feature.topic
31
+ Kafka.receive(topic, async (topic, message) => {
32
+
33
+ const timer = ms => new Promise(res => setTimeout(res, ms)) // A promise that resolves after "ms" Milliseconds
34
+
35
+ while (true) {
36
+ try {
37
+ await features[message.feature].handler(topic, message)
38
+ await Elabase.insert("txns", {_id: message.txn, status: "Processed"})
39
+ break
40
+ } catch {
41
+ console.warn(`Feature: ${featureName}, error in executing handler during Kafka.receive(topic: ${topic})`)
42
+ }
43
+ await timer(10000);
44
+ }
45
+ })
46
+ }
47
+
48
+ })
49
+
50
+ Object.entries(apis).forEach(([api,features]) => {
51
+ let method = api.split(' ')[0].toLowerCase()
52
+ let url = api.split(' ')[1]
53
+ if (api === 'GET /transactions/:id')
54
+ return
55
+ app[method](url, async (req, res) => {
56
+ let feature = features[req.body.feature]
57
+ try {
58
+ if (method === "get") {
59
+ try {
60
+ await feature.handler({...req, body: {...req.body, topic: feature.topic } }, res)
61
+ } catch (err) {
62
+ throw {status: 500, message: "Failed to retreive data", ...err}
63
+ }
64
+ } else
65
+ await commandAction(req, res, feature, req.body.feature)
66
+ } catch (err) {
67
+ res.status(err.status ?? 500).json(err)
68
+ }
69
+ })
70
+ })
71
+
72
+
73
+ app.get('/transactions/:id', async (req, res) => {
74
+ try {
75
+ let items = await Elabase.query("txns", {_id: req.params.id})
76
+ if (items.length)
77
+ res.json({ data: { ...items[0], txn: items._id } })
78
+ else
79
+ throw false
80
+ } catch {
81
+ throw {status: 500, message: "Failed to retreive data"}
82
+ }
83
+ })
84
+ }
85
+
86
+ const commandAction = async (req, res, feature, featureName) => {
87
+ let txn = req.body.txn ?? Util.uid()
88
+ req.body.data._id = req.body.data._id ?? txn
89
+ try {
90
+ await Kafka.send(feature.topic, { data: req.body.data, feature: featureName, txn, id: _id }, _id)
91
+
92
+ } catch {
93
+ throw {status: 500, message: "Failed to queue the transaction"}
94
+ }
95
+ res.json({ data: { txn, success: true, status: "Queued" } })
96
+ }
97
+
package/libs/session.js CHANGED
@@ -1,4 +1,4 @@
1
- import jwt from 'jsonwebtoken'
1
+ import * as jwt from 'jsonwebtoken'
2
2
  let app
3
3
 
4
4
 
package/libs/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import ObjectId from './ObjectId.js'
1
+ import * as ObjectId from './ObjectId.js'
2
2
 
3
3
  export let pipeline = { execute: execute }
4
4
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.30",
4
+ "version": "1.0.32",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {