corebasic 1.0.31 → 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 +3 -1
- package/libs/features.js +97 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,11 +5,13 @@ import * as Kafka from './libs/kafka.js'
|
|
|
5
5
|
import * as Utils from './libs/utils.js'
|
|
6
6
|
import * as Session from './libs/session.js'
|
|
7
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/features.js
ADDED
|
@@ -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
|
+
|