corebasic 1.0.254 → 1.0.256

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/libs/auth.js CHANGED
@@ -34,15 +34,24 @@ export const start = (app, successCallback) => {
34
34
  }
35
35
  });
36
36
  };
37
+ const Apps = {
38
+ SlypBusiness: "SlypBusiness",
39
+ Slyp: "Slyp",
40
+ DipDive: "DipDive",
41
+ Craft: "Craft",
42
+ Portal: "Portal"
43
+ };
37
44
  async function attemptLogin(req) {
38
45
  let meta = { company: "GLOBAL", outlet: "GLOBAL" };
46
+ let errMessage = { success: false, message: "Login Server Error" };
47
+ if (!req.body?.app || !Apps[req.body.app])
48
+ throw { ...errMessage, mode: 'verify', info: "Invalid app" };
39
49
  let expiry = 300000;
40
50
  let userMob = req.body.mob ?? req.body.phone ?? '';
41
51
  const code = req.body.code ?? '';
42
52
  let mob = userMob.endsWith('123456789') ? '0123456789' : Utils.parseMob(userMob, code);
43
53
  let time = new Date().getTime();
44
- let collection = (req.body.app ? req.body.app + '.' : '') + "auth.login";
45
- let errMessage = { success: false, message: "Login Server Error" };
54
+ const collection = `${req.body.app}.auth.login`;
46
55
  let otpValid = Utils.isEmpty(req.body.otp) ? false : true;
47
56
  let clientId = req.body.clientId;
48
57
  if (Utils.isEmpty(clientId))
@@ -301,6 +301,7 @@ export const insert = async (meta, collection, value, options, extras) => {
301
301
  });
302
302
  };
303
303
  // Ported
304
+ // export const query = async (meta: DipMeta, collection: string | string[], query: any, options?: any, extras?: any) => {
304
305
  export const query = async (meta, collection, query, options, extras) => {
305
306
  if (!(meta instanceof DipMeta))
306
307
  console.warn('Warn: Provided meta is not an instance of DipMeta in Dip.query()'); // TODO: throw error once review completes
@@ -328,7 +329,7 @@ export const query = async (meta, collection, query, options, extras) => {
328
329
  if (!batches[meta.batch])
329
330
  throw new Error('Error: Invalid batch in Dip.query()');
330
331
  batches[meta.batch].push(arg);
331
- return;
332
+ return [];
332
333
  }
333
334
  return new Promise((resolve, _reject) => resolve())
334
335
  .then(() => execute(arg));
@@ -347,7 +347,7 @@ export const start = async (app, url, file) => {
347
347
  try {
348
348
  let items = await Dip.query(globalMeta, "Features.txns", { _id: req.params.id });
349
349
  if (items.length)
350
- res.json({ data: { ...items[0], txn: items._id } });
350
+ res.json({ data: { ...items[0], txn: items[0]._id } });
351
351
  else
352
352
  throw false;
353
353
  }
package/libs/auth.ts CHANGED
@@ -74,16 +74,46 @@ export const start = (app: AuthApp, successCallback?: SuccessCallback) => {
74
74
  })
75
75
  }
76
76
 
77
+
78
+ const Apps = {
79
+ SlypBusiness: "SlypBusiness",
80
+ Slyp: "Slyp",
81
+ DipDive: "DipDive",
82
+ Craft: "Craft",
83
+ Portal: "Portal"
84
+ } as const;
85
+ type Auth = {
86
+ app: string
87
+ // mob: string
88
+ // phone: string
89
+ // code: string
90
+ // otp: string
91
+ // clientId: string
92
+ _id: string
93
+ otp: string
94
+ clientId: string
95
+ time: number
96
+ userId?: string
97
+ loggedIn?: boolean
98
+ }
99
+ declare global {
100
+ interface Schema extends Record<`${keyof typeof Apps}.auth.login`, Auth> {}
101
+ }
102
+
77
103
  async function attemptLogin(req: AuthRequest) {
78
104
  let meta = {company: "GLOBAL", outlet: "GLOBAL"}
105
+ let errMessage = {success: false, message: "Login Server Error"}
106
+
107
+ if (!req.body?.app || !Apps[req.body.app as keyof typeof Apps])
108
+ throw {...errMessage, mode: 'verify', info: "Invalid app"}
109
+
79
110
 
80
111
  let expiry = 300000
81
112
  let userMob = req.body.mob ?? req.body.phone ?? ''
82
113
  const code = req.body.code ?? ''
83
114
  let mob = userMob.endsWith('123456789') ? '0123456789' : Utils.parseMob(userMob, code)
84
115
  let time = new Date().getTime()
85
- let collection = (req.body.app ? req.body.app + '.' : '') + "auth.login"
86
- let errMessage = {success: false, message: "Login Server Error"}
116
+ const collection = `${req.body.app}.auth.login` as `${keyof typeof Apps}.auth.login`;
87
117
  let otpValid = Utils.isEmpty(req.body.otp) ? false : true
88
118
  let clientId = req.body.clientId
89
119
  if (Utils.isEmpty(clientId))
@@ -93,7 +123,7 @@ async function attemptLogin(req: AuthRequest) {
93
123
  let res = await Dip.query(meta, collection, { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
94
124
  if (res.length) {
95
125
  try {await Dip.update(meta, collection, { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
96
- return {...res[0], mode: 'verify', success: true, userId: res[0].userId, phone: res[0]._id, mob: res[0]._id, code}
126
+ return {...res[0], mode: 'verify', success: true, userId: res[0]!.userId!, phone: res[0]!._id, mob: res[0]!._id, code}
97
127
  }
98
128
  throw {...errMessage, mode: 'verify', info: "Invalid/Expired OTP"}
99
129
  } else { // generate login
package/libs/dip.ts CHANGED
@@ -40,6 +40,11 @@ type IdempotentDip = {
40
40
  finish: () => Promise<unknown>
41
41
  }
42
42
 
43
+ declare global {
44
+ interface Schema {
45
+ "txns": {_id: string}
46
+ }
47
+ }
43
48
 
44
49
  export async function insertSync(meta: Elabase.DipMeta, feature: string, date: number, uid: string, ref: unknown, type: "REMOVE" | "UPSERT") {
45
50
  let stamp = new Date(date).toISOString()
@@ -71,7 +76,7 @@ export const IdempotentDip = (): IdempotentDip => {
71
76
  let token = { [state.txn]: true }
72
77
  let idempotent = { [state.txn]: { $exists: rollback ?? false } }
73
78
  let result = await Elabase.update(state.meta!, collection, {...query, ...idempotent}, {...data, $setOnInsert: undefined, $set: {...(data.$set ?? {}), ...token} })
74
- return result.count ? true : (await Elabase.query(state.meta!, collection, {...query, ...idempotent})).length
79
+ return result.count ? true : (await Elabase.query(state.meta!, collection as keyof Schema, {...query, ...idempotent})).length
75
80
  }
76
81
 
77
82
  idip.upsert = async function (collection, query, data, rollback) {
package/libs/elabase.ts CHANGED
@@ -437,7 +437,8 @@ export const insert = async (meta: DipMeta, collection: string | string[], value
437
437
  }
438
438
 
439
439
  // Ported
440
- export const query = async (meta: DipMeta, collection: string | string[], query: any, options?: any, extras?: any) => {
440
+ // export const query = async (meta: DipMeta, collection: string | string[], query: any, options?: any, extras?: any) => {
441
+ export const query = async<K extends keyof Schema> (meta: DipMeta, collection: K | K[], query: any, options?: any, extras?: any): Promise<Schema[K][]> => {
441
442
 
442
443
  if (!(meta instanceof DipMeta)) console.warn('Warn: Provided meta is not an instance of DipMeta in Dip.query()') // TODO: throw error once review completes
443
444
 
@@ -468,7 +469,7 @@ export const query = async (meta: DipMeta, collection: string | string[], query:
468
469
  if (!batches[meta.batch])
469
470
  throw new Error('Error: Invalid batch in Dip.query()')
470
471
  batches[meta.batch]!.push(arg)
471
- return
472
+ return []
472
473
  }
473
474
  return new Promise<void>((resolve, _reject) => resolve())
474
475
  .then(() => execute(arg))
package/libs/features.ts CHANGED
@@ -55,12 +55,12 @@ type FeatureMeta = {
55
55
  version: string,
56
56
  txn: string,
57
57
  // invoiceTxn?: string // TODO: Must also include invoiceTxn
58
- topic: string,
58
+ topic: keyof Schema,
59
59
  date: number,
60
60
  }
61
61
  export type FeatureParams = Record<string, string>
62
- type FeatureMessage<I = unknown> = {
63
- meta: FeatureMeta
62
+ type FeatureMessage<I = unknown, T extends keyof Schema = keyof Schema> = {
63
+ meta: FeatureMeta & { topic: T }
64
64
  data: I
65
65
  params: FeatureParams
66
66
 
@@ -120,13 +120,17 @@ type ExpressApplication = { [Method in HttpMethod]: (path: string, handler: Expr
120
120
  // type Handler = Query | Command
121
121
 
122
122
  declare global {
123
- type Handler<I, O> = (input: FeatureMessage<I>, req?: Req, res?: Res) => Promise<O>
123
+ type Handler<I, O, T extends keyof Schema> = (input: FeatureMessage<I, T>, req?: Req, res?: Res) => Promise<O>
124
124
 
125
125
  interface FeatureTypes {
126
126
  // "privileges.query.check": Handler<{feature: string}, {data: {granted: boolean} }>
127
127
  // "products.query.get": typeof import("./src/products/query.ts").get;
128
128
  // "products.brands.query.get": typeof import("./src/products/brands/query.ts").get;
129
129
  }
130
+
131
+ interface Schema {
132
+ "Features.txns": {_id: string, status: "Queued" | "Processed"}
133
+ }
130
134
  }
131
135
 
132
136
 
@@ -138,7 +142,7 @@ type RemoteFeatureEntry = {
138
142
  headers?: {JWT?: string, service?: boolean, NDCURVE_DEVELOPER_LICENSE_ACCESS_TOKEN?: string}
139
143
  }
140
144
 
141
- type FeatureEntry = { api: string, topic: string, bypass: boolean, featureless: boolean, subscribe: string, handler: Handler<unknown, unknown> }
145
+ type FeatureEntry = { api: string, topic: string, bypass: boolean, featureless: boolean, subscribe: string, handler: Handler<unknown, unknown, keyof Schema> }
142
146
 
143
147
 
144
148
  let features: Record<string, FeatureEntry> = {}
@@ -187,7 +191,7 @@ export const get = (feature: string) => {
187
191
  }
188
192
 
189
193
 
190
- type HandlerInput<T> = T extends Handler<infer I, any> ? I : never
194
+ type HandlerInput<T> = T extends Handler<infer I, any, keyof Schema> ? I : never
191
195
 
192
196
  export const send = async <K extends keyof FeatureTypes> (meta: Partial<FeatureMeta>, feature: K, data?: HandlerInput<FeatureTypes[K]>, params?: FeatureParams): Promise<Awaited<ReturnType<FeatureTypes[K]>>> => {
193
197
  const throwError = () => {throw new Error(`Feature ${feature} not found in internal or external list during inter feature call`)}
@@ -478,7 +482,7 @@ export const start = async (app: ExpressApplication, url: URL, file: string) =>
478
482
  try {
479
483
  let items = await Dip.query(globalMeta, "Features.txns", {_id: req.params!.id})
480
484
  if (items.length)
481
- res.json({ data: { ...items[0], txn: items._id } })
485
+ res.json({ data: { ...items[0], txn: items[0]!._id } })
482
486
  else
483
487
  throw false
484
488
  } catch {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.254",
4
+ "version": "1.0.256",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",