corebasic 1.0.137 → 1.0.139
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/libs/features.js +61 -53
- package/package.json +1 -1
package/libs/features.js
CHANGED
|
@@ -65,7 +65,14 @@ export const send = async (meta, feature, data, params) => {
|
|
|
65
65
|
|
|
66
66
|
// Used to work. But request to 127.0.0.1 fails to send the specified headers along with the request.
|
|
67
67
|
// return (await axios[method](`${service}${url}`, payload, {headers: {jwt: SERVICE_ACCESS_TOKEN, service: true}, timeout: 1000 })).data // Worked Earlier, but issue spotted
|
|
68
|
-
|
|
68
|
+
if (getFeature(feature)) { // Local call
|
|
69
|
+
let response;
|
|
70
|
+
let req = {meta,body: payload, params,method}
|
|
71
|
+
req = JSON.parse(JSON.stringify(req))
|
|
72
|
+
let res = {json: payload => response = payload, end: _ => true }
|
|
73
|
+
await apiHandler(req, res);
|
|
74
|
+
return response
|
|
75
|
+
}
|
|
69
76
|
// Workaround: https://stackoverflow.com/questions/72343387/axios-not-sending-headers-request-failing-getting-401-error
|
|
70
77
|
return (await axios({
|
|
71
78
|
method,
|
|
@@ -94,7 +101,59 @@ async function announce() {
|
|
|
94
101
|
})
|
|
95
102
|
await Messaging.produce(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, JSON.stringify({...exp_features, uid: appId }))
|
|
96
103
|
}
|
|
104
|
+
const apiHandler = async (req, res) => {
|
|
105
|
+
let method = req.method.toLowerCase()
|
|
106
|
+
try {
|
|
107
|
+
if (!req.body.feature) {
|
|
108
|
+
console.warn(`Feature: feature not sent by client`)
|
|
109
|
+
throw { status: 404, message: "Resource not found. Feature not sent by client." }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let feature = getFeature(req.body.feature)
|
|
113
|
+
if (!feature) {
|
|
114
|
+
console.warn(`Feature: ${req.body.feature} not available`)
|
|
115
|
+
throw { status: 404, message: `Resource not found. Feature ${req.body.feature} not available.` }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let topic = feature.topic
|
|
119
|
+
let params = getFeatureUrl(feature.api).split("/").filter(item => item.startsWith(":")).map(item => item.replace(":", ""))
|
|
120
|
+
for (let param of params)
|
|
121
|
+
if (!req.params[param])
|
|
122
|
+
throw { status: 404, message: "Resource not found. One or more url parameter not specified." }
|
|
123
|
+
|
|
124
|
+
let meta = {...req.body, data: undefined}
|
|
125
|
+
req.meta = meta
|
|
126
|
+
if (process.env.USE_DEFAULT_COMPANY) {
|
|
127
|
+
req.meta.company = 'DEFAULT_COMPANY'
|
|
128
|
+
req.meta.outlet = 'DEFAULT_OUTLET'
|
|
129
|
+
}
|
|
97
130
|
|
|
131
|
+
if (method === "get") {
|
|
132
|
+
try {
|
|
133
|
+
await feature.handler({...req, headers: req.headers, body: {...req.body, topic} }, res)
|
|
134
|
+
} catch (err) {
|
|
135
|
+
if (process.env.DEBUG_MODE)
|
|
136
|
+
console.log('Error: Feature: ', req.body?.feature, err)
|
|
137
|
+
throw {status: 500, message: "Failed to GET feature", ...err}
|
|
138
|
+
}
|
|
139
|
+
} else if (method !== "get" && feature.bypass) {
|
|
140
|
+
try {
|
|
141
|
+
await feature.handler(topic, prepareMessage(req), req, res)
|
|
142
|
+
} catch (err) {
|
|
143
|
+
if (process.env.DEBUG_MODE)
|
|
144
|
+
console.log('Error: Feature: ', req.body?.feature, err)
|
|
145
|
+
throw {status: 500, message: "Failed to POST feature", ...err}
|
|
146
|
+
}
|
|
147
|
+
} else
|
|
148
|
+
await commandAction(req, res, topic)
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (process.env.DEBUG_MODE)
|
|
151
|
+
console.log('Error: Feature: ', req.body?.feature, err)
|
|
152
|
+
try { // Sometimes error occurs when disconnecting stream from front end
|
|
153
|
+
res.status(err.status ?? 500).json(err)
|
|
154
|
+
} catch (_) { }
|
|
155
|
+
}
|
|
156
|
+
}
|
|
98
157
|
export const start = async (app, url, file) => {
|
|
99
158
|
features = await Utils.fileToJson(url, file)
|
|
100
159
|
await announce()
|
|
@@ -133,58 +192,7 @@ export const start = async (app, url, file) => {
|
|
|
133
192
|
if (api === 'GET /transactions/:id')
|
|
134
193
|
continue
|
|
135
194
|
|
|
136
|
-
app[method](url,
|
|
137
|
-
try {
|
|
138
|
-
if (!req.body.feature) {
|
|
139
|
-
console.warn(`Feature: feature not sent by client`)
|
|
140
|
-
throw { status: 404, message: "Resource not found. Feature not sent by client." }
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
let feature = getFeature(req.body.feature)
|
|
144
|
-
if (!feature) {
|
|
145
|
-
console.warn(`Feature: ${req.body.feature} not available`)
|
|
146
|
-
throw { status: 404, message: `Resource not found. Feature ${req.body.feature} not available.` }
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
let topic = feature.topic
|
|
150
|
-
let params = getFeatureUrl(feature.api).split("/").filter(item => item.startsWith(":")).map(item => item.replace(":", ""))
|
|
151
|
-
for (let param of params)
|
|
152
|
-
if (!req.params[param])
|
|
153
|
-
throw { status: 404, message: "Resource not found. One or more url parameter not specified." }
|
|
154
|
-
|
|
155
|
-
let meta = {...req.body, data: undefined}
|
|
156
|
-
req.meta = meta
|
|
157
|
-
if (process.env.USE_DEFAULT_COMPANY) {
|
|
158
|
-
req.meta.company = 'DEFAULT_COMPANY'
|
|
159
|
-
req.meta.outlet = 'DEFAULT_OUTLET'
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (method === "get") {
|
|
163
|
-
try {
|
|
164
|
-
await feature.handler({...req, headers: req.headers, body: {...req.body, topic} }, res)
|
|
165
|
-
} catch (err) {
|
|
166
|
-
if (process.env.DEBUG_MODE)
|
|
167
|
-
console.log(err)
|
|
168
|
-
throw {status: 500, message: "Failed to GET feature", ...err}
|
|
169
|
-
}
|
|
170
|
-
} else if (method !== "get" && feature.bypass) {
|
|
171
|
-
try {
|
|
172
|
-
await feature.handler(topic, prepareMessage(req), req, res)
|
|
173
|
-
} catch (err) {
|
|
174
|
-
if (process.env.DEBUG_MODE)
|
|
175
|
-
console.log(err)
|
|
176
|
-
throw {status: 500, message: "Failed to POST feature", ...err}
|
|
177
|
-
}
|
|
178
|
-
} else
|
|
179
|
-
await commandAction(req, res, topic)
|
|
180
|
-
} catch (err) {
|
|
181
|
-
if (process.env.DEBUG_MODE)
|
|
182
|
-
console.log(err)
|
|
183
|
-
try { // Sometimes error occurs when disconnecting stream from front end
|
|
184
|
-
res.status(err.status ?? 500).json(err)
|
|
185
|
-
} catch (_) { }
|
|
186
|
-
}
|
|
187
|
-
})
|
|
195
|
+
app[method](url, apiHandler)
|
|
188
196
|
}
|
|
189
197
|
|
|
190
198
|
|