corebasic 1.0.238 → 1.0.240

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.
@@ -93,7 +93,7 @@ export const send = async (meta, feature, data, params) => {
93
93
  writableEnded: false,
94
94
  };
95
95
  await apiHandler(req, res);
96
- return response;
96
+ return response; // tsc already checked that the feature returned proper types at the handler site. Cast here due to ts limitations. So no guarantees lost.
97
97
  }
98
98
  // Workaround: https://stackoverflow.com/questions/72343387/axios-not-sending-headers-request-failing-getting-401-error
99
99
  return (await axios({
@@ -102,7 +102,7 @@ export const send = async (meta, feature, data, params) => {
102
102
  headers: { jwt: SERVICE_ACCESS_TOKEN, service: true, NDCURVE_DEVELOPER_LICENSE_ACCESS_TOKEN },
103
103
  data: payload,
104
104
  timeout: 30000 // 30 secs
105
- })).data;
105
+ })).data; // tsc already checked that the feature returned proper types at the handler site. Cast here due to ts limitations. So no guarantees lost.;
106
106
  };
107
107
  let appids = {};
108
108
  async function announce() {
@@ -9,11 +9,29 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
9
9
  import { readdir } from "node:fs/promises";
10
10
  import * as Utils from './utils.js';
11
11
  const SCHEMAS_DIR = `${Utils.PROJECT_ROOT_PATH}/schemas`;
12
+ const TYPES_DIR = `${Utils.PROJECT_ROOT_PATH}/types`;
13
+ const REMOTES_DIR = `${Utils.PROJECT_ROOT_PATH}/types/remotes`;
12
14
  const g_this = globalThis;
13
15
  g_this.Schemas ??= {};
14
- for (const file of await readdir(SCHEMAS_DIR)) {
15
- if (file.endsWith(".ts") && file !== "index.ts") {
16
- const schema = await import(__rewriteRelativeImportExtension(`${SCHEMAS_DIR}/${file}`));
17
- Object.assign(g_this.Schemas, schema.Schemas);
16
+ g_this.Types ??= { Remotes: {} };
17
+ async function load(path) {
18
+ let files;
19
+ try {
20
+ files = await readdir(path);
21
+ }
22
+ catch {
23
+ files = [];
24
+ }
25
+ for (const file of files) {
26
+ if (file.endsWith(".ts")) {
27
+ const imported = await import(__rewriteRelativeImportExtension(`${path}/${file}`));
28
+ Object.assign(g_this.Schemas, imported?.Schemas ?? {});
29
+ const { Remotes: remotes, ...types } = imported?.Types ?? {};
30
+ Object.assign(g_this.Types, types ?? {});
31
+ Object.assign(g_this.Types.Remotes, remotes ?? {});
32
+ }
18
33
  }
19
34
  }
35
+ await load(SCHEMAS_DIR);
36
+ await load(TYPES_DIR);
37
+ await load(REMOTES_DIR);
@@ -20,7 +20,8 @@ export const start = (expressApp, allowedUrls) => {
20
20
  return next();
21
21
  const checkPrivilege = async (req) => {
22
22
  const staff = Utils.isEmpty(req.body.staff) ? 'BLANK_STAFF' : req.body.staff;
23
- const granted = (await Features.send({ company: req.body.company, outlet: req.body.outlet, app: req.body.app }, "privileges.query.check", { feature: req.body.feature }, { id: staff })).data.granted;
23
+ const response = (await Features.send({ company: req.body.company, outlet: req.body.outlet, app: req.body.app }, "privileges.query.check", { feature: req.body.feature }, { id: staff }));
24
+ const granted = response.data.granted;
24
25
  if (!granted)
25
26
  throw { message: "Access Denied" };
26
27
  return granted;
package/libs/elabase.ts CHANGED
@@ -619,9 +619,9 @@ function execute(arg: DipRequest) {
619
619
 
620
620
  result.getSliceAsText = (offset: number, length: number): string => Cpp.getSliceAsText(result.data, offset, length)
621
621
  result.getSliceAsArrayBuffer = (offset: number, length: number): Buffer => Cpp.getSliceAsArrayBuffer(result.data, offset, length)
622
- result["Content-Type"] = result.headers["content-type"]
622
+ result["Content-Type"] = result.headers["content-type"] as string
623
623
  result.body = result.data
624
- if (result.headers["content-type"].includes("application/json"))
624
+ if ((result.headers["content-type"] as string).includes("application/json"))
625
625
  result.body = JSON.parse(result.data)
626
626
 
627
627
  let parsed = parseBinaryResponse(result)
package/libs/features.ts CHANGED
@@ -26,7 +26,7 @@ type FeatureMeta = {
26
26
  txn: string,
27
27
  // invoiceTxn?: string // TODO: Must also include invoiceTxn
28
28
  }
29
- type FeatureParams = Record<string, string>
29
+ export type FeatureParams = Record<string, string>
30
30
  type FeatureMessage = {
31
31
  meta: FeatureMeta
32
32
  data: unknown
@@ -151,7 +151,17 @@ export const get = (feature: string) => {
151
151
  return {api, service, topic}
152
152
  }
153
153
 
154
- export const send = async (meta: Partial<FeatureMeta>, feature: string, data?: unknown, params?: FeatureParams) => {
154
+ declare global {
155
+ type Handler<I, O> = (input: I) => Promise<O>
156
+
157
+ interface FeatureTypes {
158
+ // "privileges.query.check": Handler<{feature: string}, {data: {granted: boolean} }>
159
+ // "products.query.get": typeof import("./src/products/query.ts").get;
160
+ // "products.brands.query.get": typeof import("./src/products/brands/query.ts").get;
161
+ }
162
+ }
163
+
164
+ export const send = async <K extends keyof FeatureTypes> (meta: Partial<FeatureMeta>, feature: K, data?: Parameters<FeatureTypes[K]>[0], params?: FeatureParams): Promise<Awaited<ReturnType<FeatureTypes[K]>>> => {
155
165
  const throwError = () => {throw new Error(`Feature ${feature} not found in internal or external list during inter feature call`)}
156
166
  const baseFeature: FeatureEntry | undefined = getFeature(feature)
157
167
  let {api, service = SERVICE_ADDRESS} = baseFeature ? baseFeature as FeatureEntry & {service: string} : (SLYP_FEATURES_LIST[feature] ?? throwError())
@@ -189,7 +199,7 @@ export const send = async (meta: Partial<FeatureMeta>, feature: string, data?: u
189
199
  writableEnded: false,
190
200
  }
191
201
  await apiHandler(req, res);
192
- return response
202
+ return response as Awaited<ReturnType<FeatureTypes[K]>> // tsc already checked that the feature returned proper types at the handler site. Cast here due to ts limitations. So no guarantees lost.
193
203
  }
194
204
  // Workaround: https://stackoverflow.com/questions/72343387/axios-not-sending-headers-request-failing-getting-401-error
195
205
  return (await axios({
@@ -198,7 +208,7 @@ export const send = async (meta: Partial<FeatureMeta>, feature: string, data?: u
198
208
  headers: { jwt: SERVICE_ACCESS_TOKEN, service: true, NDCURVE_DEVELOPER_LICENSE_ACCESS_TOKEN },
199
209
  data: payload,
200
210
  timeout: 30000 // 30 secs
201
- })).data;
211
+ })).data as Awaited<ReturnType<FeatureTypes[K]>> // tsc already checked that the feature returned proper types at the handler site. Cast here due to ts limitations. So no guarantees lost.;
202
212
  }
203
213
 
204
214
  let appids: Record<string, boolean> = {}
@@ -524,7 +534,7 @@ async function subscribe() {
524
534
 
525
535
  while (true) {
526
536
  try {
527
- await Features.send(message.meta, consumer, message.data, message.params)
537
+ await Features.send(message.meta, consumer as keyof FeatureTypes, message.data as any, message.params as FeatureParams)
528
538
  break
529
539
  } catch {
530
540
  console.warn(`Feature: ${consumer}, error in executing handler during Features.subscribe(topic: ${topic})`)
package/libs/schemas.ts CHANGED
@@ -2,13 +2,35 @@ import { readdir } from "node:fs/promises"
2
2
  import * as Utils from './utils.ts'
3
3
 
4
4
  const SCHEMAS_DIR = `${Utils.PROJECT_ROOT_PATH}/schemas`
5
+ const TYPES_DIR = `${Utils.PROJECT_ROOT_PATH}/types`
6
+ const REMOTES_DIR = `${Utils.PROJECT_ROOT_PATH}/types/remotes`
5
7
 
6
8
  const g_this: any = globalThis as any
7
9
  g_this.Schemas ??= {};
10
+ g_this.Types ??= {Remotes: {}};
8
11
 
9
- for (const file of await readdir(SCHEMAS_DIR)) {
10
- if (file.endsWith(".ts") && file !== "index.ts") {
11
- const schema = await import(`${SCHEMAS_DIR}/${file}`)
12
- Object.assign(g_this.Schemas, schema.Schemas)
12
+ async function load(path: string) {
13
+ let files: string[]
14
+ try {
15
+ files = await readdir(path)
16
+ } catch {
17
+ files = []
18
+ }
19
+
20
+ for (const file of files) {
21
+ if (file.endsWith(".ts")) {
22
+ const imported = await import(`${path}/${file}`)
23
+ Object.assign(g_this.Schemas, imported?.Schemas ?? {})
24
+
25
+ const { Remotes: remotes, ...types } = imported?.Types ?? {};
26
+ Object.assign(g_this.Types, types ?? {})
27
+ Object.assign(g_this.Types.Remotes, remotes ?? {})
28
+ }
13
29
  }
14
30
  }
31
+
32
+
33
+ await load(SCHEMAS_DIR)
34
+ await load(TYPES_DIR)
35
+ await load(REMOTES_DIR)
36
+
package/libs/session.ts CHANGED
@@ -44,7 +44,9 @@ export const start = (expressApp: ExpressApp, allowedUrls: string[]) => {
44
44
 
45
45
  const checkPrivilege = async (req: Req) => {
46
46
  const staff = Utils.isEmpty(req.body.staff) ? 'BLANK_STAFF' : req.body.staff
47
- const granted = (await Features.send({company: req.body.company, outlet: req.body.outlet, app: req.body.app}, "privileges.query.check", {feature: req.body.feature }, {id: staff})).data.granted
47
+ type PrivilegeResponse = {data: {granted: boolean}}
48
+ const response = (await Features.send({company: req.body.company, outlet: req.body.outlet, app: req.body.app}, "privileges.query.check" as keyof FeatureTypes, {feature: req.body.feature } as any, {id: staff} as Features.FeatureParams)) as PrivilegeResponse
49
+ const granted = response.data.granted
48
50
  if (!granted)
49
51
  throw {message: "Access Denied"}
50
52
  return granted
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.238",
4
+ "version": "1.0.240",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",