@thinkingdifferently/core 1.3.0 → 1.4.0

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/index.d.mts CHANGED
@@ -53,12 +53,16 @@ declare class TDClient {
53
53
  }
54
54
 
55
55
  type Operator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "contains";
56
- type Operation = "find" | "count" | "insert" | "update" | "delete" | "updateMany" | "deleteMany";
56
+ type Operation = "find" | "count" | "insert" | "delete" | "deleteMany" | "updateById" | "updateMany" | "updateOne";
57
57
  type Filter = {
58
58
  field: string;
59
59
  operator: Operator;
60
60
  value: unknown;
61
61
  };
62
+ type SetOperation = {
63
+ field: string;
64
+ value: unknown;
65
+ };
62
66
  type Query = {
63
67
  operation: Operation | null;
64
68
  collection: string;
@@ -70,6 +74,8 @@ type Query = {
70
74
  } | null;
71
75
  id?: string;
72
76
  data?: unknown;
77
+ $set?: SetOperation[];
78
+ updateById?: string;
73
79
  };
74
80
  declare class QueryBuilder {
75
81
  private query;
@@ -78,12 +84,14 @@ declare class QueryBuilder {
78
84
  where(field: string, operator: Operator, value: unknown): this;
79
85
  limit(count: number): this;
80
86
  sort(field: string, direction: "asc" | "desc"): this;
87
+ set(field: string, value: unknown): this;
81
88
  count(): Promise<number>;
82
89
  toJSON(): Query;
83
90
  get<T = any>(): Promise<T[]>;
84
91
  insert(data: unknown): Promise<any>;
85
- UpdateById(id: string, data: Record<string, any>): Promise<any>;
86
- updateMany(data: Record<string, any>): Promise<any>;
92
+ updateById(id: string): Promise<any>;
93
+ updateOne(): Promise<any>;
94
+ updateMany(): Promise<any>;
87
95
  DeleteById(id: string): Promise<any>;
88
96
  deleteMany(): Promise<any>;
89
97
  }
@@ -108,7 +116,7 @@ declare class AdminAuth {
108
116
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
109
117
  * Supported in Node.js / Server-side environments.
110
118
  */
111
- verifyJwt(token: string, publicKeyPem?: string): any;
119
+ verifyJwt(token: string, publicKeyPem?: string): Promise<any>;
112
120
  }
113
121
  declare class AuthModule {
114
122
  admin: AdminAuth;
@@ -133,7 +141,6 @@ declare class ThinkingDifferently {
133
141
  verifyJwt(token: string, publicKey?: string): any;
134
142
  collection(name: string): QueryBuilder;
135
143
  insert(key: string, data: any): Promise<any>;
136
- update(key: string, id: string, data: Record<string, any>): Promise<any>;
137
144
  delete(key: string, id: string): Promise<any>;
138
145
  }
139
146
 
package/dist/index.d.ts CHANGED
@@ -53,12 +53,16 @@ declare class TDClient {
53
53
  }
54
54
 
55
55
  type Operator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "contains";
56
- type Operation = "find" | "count" | "insert" | "update" | "delete" | "updateMany" | "deleteMany";
56
+ type Operation = "find" | "count" | "insert" | "delete" | "deleteMany" | "updateById" | "updateMany" | "updateOne";
57
57
  type Filter = {
58
58
  field: string;
59
59
  operator: Operator;
60
60
  value: unknown;
61
61
  };
62
+ type SetOperation = {
63
+ field: string;
64
+ value: unknown;
65
+ };
62
66
  type Query = {
63
67
  operation: Operation | null;
64
68
  collection: string;
@@ -70,6 +74,8 @@ type Query = {
70
74
  } | null;
71
75
  id?: string;
72
76
  data?: unknown;
77
+ $set?: SetOperation[];
78
+ updateById?: string;
73
79
  };
74
80
  declare class QueryBuilder {
75
81
  private query;
@@ -78,12 +84,14 @@ declare class QueryBuilder {
78
84
  where(field: string, operator: Operator, value: unknown): this;
79
85
  limit(count: number): this;
80
86
  sort(field: string, direction: "asc" | "desc"): this;
87
+ set(field: string, value: unknown): this;
81
88
  count(): Promise<number>;
82
89
  toJSON(): Query;
83
90
  get<T = any>(): Promise<T[]>;
84
91
  insert(data: unknown): Promise<any>;
85
- UpdateById(id: string, data: Record<string, any>): Promise<any>;
86
- updateMany(data: Record<string, any>): Promise<any>;
92
+ updateById(id: string): Promise<any>;
93
+ updateOne(): Promise<any>;
94
+ updateMany(): Promise<any>;
87
95
  DeleteById(id: string): Promise<any>;
88
96
  deleteMany(): Promise<any>;
89
97
  }
@@ -108,7 +116,7 @@ declare class AdminAuth {
108
116
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
109
117
  * Supported in Node.js / Server-side environments.
110
118
  */
111
- verifyJwt(token: string, publicKeyPem?: string): any;
119
+ verifyJwt(token: string, publicKeyPem?: string): Promise<any>;
112
120
  }
113
121
  declare class AuthModule {
114
122
  admin: AdminAuth;
@@ -133,7 +141,6 @@ declare class ThinkingDifferently {
133
141
  verifyJwt(token: string, publicKey?: string): any;
134
142
  collection(name: string): QueryBuilder;
135
143
  insert(key: string, data: any): Promise<any>;
136
- update(key: string, id: string, data: Record<string, any>): Promise<any>;
137
144
  delete(key: string, id: string): Promise<any>;
138
145
  }
139
146
 
package/dist/index.js CHANGED
@@ -218,6 +218,7 @@ var QueryBuilder = class {
218
218
  operation: null,
219
219
  collection,
220
220
  filters: [],
221
+ $set: [],
221
222
  limit: null,
222
223
  sort: null
223
224
  };
@@ -246,6 +247,14 @@ var QueryBuilder = class {
246
247
  this.query.sort = { field, direction };
247
248
  return this;
248
249
  }
250
+ set(field, value) {
251
+ if (!field.trim()) {
252
+ throw new Error("Field name cannot be empty in .set()");
253
+ }
254
+ this.query.$set = this.query.$set || [];
255
+ this.query.$set.push({ field, value });
256
+ return this;
257
+ }
249
258
  // build() {
250
259
  // return this.query;
251
260
  // }
@@ -339,31 +348,46 @@ var QueryBuilder = class {
339
348
  throw error;
340
349
  }
341
350
  }
342
- async UpdateById(id, data) {
343
- this.query.operation = "update";
344
- this.query.id = id;
345
- this.query.data = data;
346
- console.log("\n================ UPDATE REQUEST ================");
351
+ async updateById(id) {
352
+ this.query.operation = "updateById";
353
+ this.query.updateById = id;
354
+ console.log("\n================ UPDATE BY ID REQUEST ================");
355
+ console.log("[SDK] Final Query:", this.query);
356
+ try {
357
+ const response = await this.client.sendDataRequest(
358
+ "PATCH",
359
+ this.query
360
+ );
361
+ console.log("[SDK] Update By ID Response:", response);
362
+ return response;
363
+ } catch (error) {
364
+ console.error("[SDK] UPDATE BY ID ERROR");
365
+ console.error(error);
366
+ throw error;
367
+ }
368
+ }
369
+ async updateOne() {
370
+ this.query.operation = "updateOne";
371
+ console.log("\n================ UPDATE ONE REQUEST ================");
347
372
  console.log("[SDK] Final Query:", this.query);
348
373
  try {
349
374
  const response = await this.client.sendDataRequest(
350
375
  "PATCH",
351
376
  this.query
352
377
  );
353
- console.log("[SDK] Update Response:", response);
378
+ console.log("[SDK] Update One Response:", response);
354
379
  return response;
355
380
  } catch (error) {
356
- console.error("[SDK] UPDATE ERROR");
381
+ console.error("[SDK] UPDATE ONE ERROR");
357
382
  console.error(error);
358
383
  throw error;
359
384
  }
360
385
  }
361
386
  //updateMany
362
387
  // its just like the get() it will call hte client
363
- //example sdk.collection("animals").where("price", ">", 1000).updateMany({status : "expensive"}) it will update all the animals whose price is greater than 1000 and set their status to expensive
364
- async updateMany(data) {
388
+ //example sdk.collection("animals").where("price", ">", 1000).updateMany() it will update all the animals whose price is greater than 1000
389
+ async updateMany() {
365
390
  this.query.operation = "updateMany";
366
- this.query.data = data;
367
391
  console.log("\n================ UPDATE MANY REQUEST ================");
368
392
  console.log("[SDK] Final Query:", this.query);
369
393
  try {
@@ -420,7 +444,7 @@ var QueryBuilder = class {
420
444
  };
421
445
 
422
446
  // src/auth.ts
423
- var crypto = __toESM(require("crypto"));
447
+ var import_jose = require("jose");
424
448
  var AdminAuth = class {
425
449
  constructor(client) {
426
450
  this.client = client;
@@ -448,77 +472,21 @@ var AdminAuth = class {
448
472
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
449
473
  * Supported in Node.js / Server-side environments.
450
474
  */
451
- //detailed explaination of the verifyJwt function:
452
- // The verifyJwt function is designed to validate a JWT (JSON Web Token) using
453
- // the Ed25519 signature algorithm (EdDSA). It takes a JWT token as input and an optional public key in PEM format.
454
- // The function performs several steps to ensure the token's integrity and authenticity:
455
- // 1. It first determines which public key to use for verification, either from the argument or from the client's configuration.
456
- // 2. It checks if the runtime environment supports the necessary crypto capabilities for JWT verification.
457
- // 3. It splits the JWT into its three components: header, payload, and signature.
458
- // 4. It decodes the signature from base64url format into a Buffer.
459
- // 5. It imports the public key using Node.js's crypto module.
460
- // 6. It verifies the signature against the message (header + payload) using the Ed25519 algorithm.
461
- // 7. If verification succeeds, it decodes and parses the payload JSON.
462
- // 8. It checks for token expiration based on the "exp" claim in the payload.
463
- // 9. Finally, it returns the decoded payload if all checks pass,
464
- // or throws descriptive errors if any step fails.
465
- verifyJwt(token, publicKeyPem) {
475
+ async verifyJwt(token, publicKeyPem) {
466
476
  const keyToUse = publicKeyPem || this.client.getPublicKey();
467
477
  if (!keyToUse) {
468
478
  throw new Error(
469
- "[ThinkingDifferently SDK Error] Public key is required for JWT verification. Please configure it in SDKConfig or pass it as an argument."
470
- );
471
- }
472
- if (typeof crypto === "undefined" || !crypto.createPublicKey || !crypto.verify) {
473
- throw new Error(
474
- "[ThinkingDifferently SDK Error] JWT verification is only supported in Node.js / Server-side environments."
479
+ "[ThinkingDifferently SDK Error] Public key is required for JWT verification."
475
480
  );
476
481
  }
477
- const parts = token.split(".");
478
- if (parts.length !== 3) {
479
- throw new Error("Invalid JWT format");
480
- }
481
- const [headerB64, payloadB64, signatureB64] = parts;
482
- const message = `${headerB64}.${payloadB64}`;
483
- let signature;
484
- try {
485
- signature = Buffer.from(signatureB64, "base64url");
486
- } catch (err) {
487
- throw new Error("Invalid JWT signature encoding");
488
- }
489
- let publicKey;
490
- try {
491
- publicKey = crypto.createPublicKey({
492
- key: keyToUse,
493
- format: "pem",
494
- type: "spki"
495
- });
496
- } catch (err) {
497
- throw new Error(`Failed to import public key: ${err.message}`);
498
- }
499
- const verified = crypto.verify(
500
- void 0,
501
- // Algorithm must be undefined for Ed25519 (EdDSA) in Node
502
- Buffer.from(message),
503
- publicKey,
504
- signature
482
+ const publicKey = await (0, import_jose.importSPKI)(
483
+ keyToUse,
484
+ "EdDSA"
485
+ );
486
+ const { payload } = await (0, import_jose.jwtVerify)(
487
+ token,
488
+ publicKey
505
489
  );
506
- if (!verified) {
507
- throw new Error("Invalid JWT signature");
508
- }
509
- let payload;
510
- try {
511
- const payloadJson = Buffer.from(payloadB64, "base64url").toString("utf8");
512
- payload = JSON.parse(payloadJson);
513
- } catch (err) {
514
- throw new Error("Invalid JWT payload JSON");
515
- }
516
- if (payload.exp && typeof payload.exp === "number") {
517
- const currentTime = Math.floor(Date.now() / 1e3);
518
- if (payload.exp < currentTime) {
519
- throw new Error("JWT has expired");
520
- }
521
- }
522
490
  return payload;
523
491
  }
524
492
  };
@@ -610,28 +578,6 @@ var ThinkingDifferently = class {
610
578
  throw error;
611
579
  }
612
580
  }
613
- async update(key, id, data) {
614
- console.log("\n================ UPDATE REQUEST ================");
615
- console.log("[SDK] Collection Key:", key);
616
- console.log("[SDK] Document ID:", id);
617
- console.log("[SDK] Update Data:", data);
618
- try {
619
- const response = await this.client.sendDataRequest(
620
- "PATCH",
621
- {
622
- key,
623
- id,
624
- data
625
- }
626
- );
627
- console.log("[SDK] Update Response:", response);
628
- return response;
629
- } catch (error) {
630
- console.error("[SDK] UPDATE ERROR");
631
- console.error(error);
632
- throw error;
633
- }
634
- }
635
581
  async delete(key, id) {
636
582
  console.log("\n================ DELETE REQUEST ================");
637
583
  console.log("[SDK] Collection Key:", key);
package/dist/index.mjs CHANGED
@@ -179,6 +179,7 @@ var QueryBuilder = class {
179
179
  operation: null,
180
180
  collection,
181
181
  filters: [],
182
+ $set: [],
182
183
  limit: null,
183
184
  sort: null
184
185
  };
@@ -207,6 +208,14 @@ var QueryBuilder = class {
207
208
  this.query.sort = { field, direction };
208
209
  return this;
209
210
  }
211
+ set(field, value) {
212
+ if (!field.trim()) {
213
+ throw new Error("Field name cannot be empty in .set()");
214
+ }
215
+ this.query.$set = this.query.$set || [];
216
+ this.query.$set.push({ field, value });
217
+ return this;
218
+ }
210
219
  // build() {
211
220
  // return this.query;
212
221
  // }
@@ -300,31 +309,46 @@ var QueryBuilder = class {
300
309
  throw error;
301
310
  }
302
311
  }
303
- async UpdateById(id, data) {
304
- this.query.operation = "update";
305
- this.query.id = id;
306
- this.query.data = data;
307
- console.log("\n================ UPDATE REQUEST ================");
312
+ async updateById(id) {
313
+ this.query.operation = "updateById";
314
+ this.query.updateById = id;
315
+ console.log("\n================ UPDATE BY ID REQUEST ================");
316
+ console.log("[SDK] Final Query:", this.query);
317
+ try {
318
+ const response = await this.client.sendDataRequest(
319
+ "PATCH",
320
+ this.query
321
+ );
322
+ console.log("[SDK] Update By ID Response:", response);
323
+ return response;
324
+ } catch (error) {
325
+ console.error("[SDK] UPDATE BY ID ERROR");
326
+ console.error(error);
327
+ throw error;
328
+ }
329
+ }
330
+ async updateOne() {
331
+ this.query.operation = "updateOne";
332
+ console.log("\n================ UPDATE ONE REQUEST ================");
308
333
  console.log("[SDK] Final Query:", this.query);
309
334
  try {
310
335
  const response = await this.client.sendDataRequest(
311
336
  "PATCH",
312
337
  this.query
313
338
  );
314
- console.log("[SDK] Update Response:", response);
339
+ console.log("[SDK] Update One Response:", response);
315
340
  return response;
316
341
  } catch (error) {
317
- console.error("[SDK] UPDATE ERROR");
342
+ console.error("[SDK] UPDATE ONE ERROR");
318
343
  console.error(error);
319
344
  throw error;
320
345
  }
321
346
  }
322
347
  //updateMany
323
348
  // its just like the get() it will call hte client
324
- //example sdk.collection("animals").where("price", ">", 1000).updateMany({status : "expensive"}) it will update all the animals whose price is greater than 1000 and set their status to expensive
325
- async updateMany(data) {
349
+ //example sdk.collection("animals").where("price", ">", 1000).updateMany() it will update all the animals whose price is greater than 1000
350
+ async updateMany() {
326
351
  this.query.operation = "updateMany";
327
- this.query.data = data;
328
352
  console.log("\n================ UPDATE MANY REQUEST ================");
329
353
  console.log("[SDK] Final Query:", this.query);
330
354
  try {
@@ -381,7 +405,7 @@ var QueryBuilder = class {
381
405
  };
382
406
 
383
407
  // src/auth.ts
384
- import * as crypto from "crypto";
408
+ import { jwtVerify, importSPKI } from "jose";
385
409
  var AdminAuth = class {
386
410
  constructor(client) {
387
411
  this.client = client;
@@ -409,77 +433,21 @@ var AdminAuth = class {
409
433
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
410
434
  * Supported in Node.js / Server-side environments.
411
435
  */
412
- //detailed explaination of the verifyJwt function:
413
- // The verifyJwt function is designed to validate a JWT (JSON Web Token) using
414
- // the Ed25519 signature algorithm (EdDSA). It takes a JWT token as input and an optional public key in PEM format.
415
- // The function performs several steps to ensure the token's integrity and authenticity:
416
- // 1. It first determines which public key to use for verification, either from the argument or from the client's configuration.
417
- // 2. It checks if the runtime environment supports the necessary crypto capabilities for JWT verification.
418
- // 3. It splits the JWT into its three components: header, payload, and signature.
419
- // 4. It decodes the signature from base64url format into a Buffer.
420
- // 5. It imports the public key using Node.js's crypto module.
421
- // 6. It verifies the signature against the message (header + payload) using the Ed25519 algorithm.
422
- // 7. If verification succeeds, it decodes and parses the payload JSON.
423
- // 8. It checks for token expiration based on the "exp" claim in the payload.
424
- // 9. Finally, it returns the decoded payload if all checks pass,
425
- // or throws descriptive errors if any step fails.
426
- verifyJwt(token, publicKeyPem) {
436
+ async verifyJwt(token, publicKeyPem) {
427
437
  const keyToUse = publicKeyPem || this.client.getPublicKey();
428
438
  if (!keyToUse) {
429
439
  throw new Error(
430
- "[ThinkingDifferently SDK Error] Public key is required for JWT verification. Please configure it in SDKConfig or pass it as an argument."
431
- );
432
- }
433
- if (typeof crypto === "undefined" || !crypto.createPublicKey || !crypto.verify) {
434
- throw new Error(
435
- "[ThinkingDifferently SDK Error] JWT verification is only supported in Node.js / Server-side environments."
440
+ "[ThinkingDifferently SDK Error] Public key is required for JWT verification."
436
441
  );
437
442
  }
438
- const parts = token.split(".");
439
- if (parts.length !== 3) {
440
- throw new Error("Invalid JWT format");
441
- }
442
- const [headerB64, payloadB64, signatureB64] = parts;
443
- const message = `${headerB64}.${payloadB64}`;
444
- let signature;
445
- try {
446
- signature = Buffer.from(signatureB64, "base64url");
447
- } catch (err) {
448
- throw new Error("Invalid JWT signature encoding");
449
- }
450
- let publicKey;
451
- try {
452
- publicKey = crypto.createPublicKey({
453
- key: keyToUse,
454
- format: "pem",
455
- type: "spki"
456
- });
457
- } catch (err) {
458
- throw new Error(`Failed to import public key: ${err.message}`);
459
- }
460
- const verified = crypto.verify(
461
- void 0,
462
- // Algorithm must be undefined for Ed25519 (EdDSA) in Node
463
- Buffer.from(message),
464
- publicKey,
465
- signature
443
+ const publicKey = await importSPKI(
444
+ keyToUse,
445
+ "EdDSA"
446
+ );
447
+ const { payload } = await jwtVerify(
448
+ token,
449
+ publicKey
466
450
  );
467
- if (!verified) {
468
- throw new Error("Invalid JWT signature");
469
- }
470
- let payload;
471
- try {
472
- const payloadJson = Buffer.from(payloadB64, "base64url").toString("utf8");
473
- payload = JSON.parse(payloadJson);
474
- } catch (err) {
475
- throw new Error("Invalid JWT payload JSON");
476
- }
477
- if (payload.exp && typeof payload.exp === "number") {
478
- const currentTime = Math.floor(Date.now() / 1e3);
479
- if (payload.exp < currentTime) {
480
- throw new Error("JWT has expired");
481
- }
482
- }
483
451
  return payload;
484
452
  }
485
453
  };
@@ -571,28 +539,6 @@ var ThinkingDifferently = class {
571
539
  throw error;
572
540
  }
573
541
  }
574
- async update(key, id, data) {
575
- console.log("\n================ UPDATE REQUEST ================");
576
- console.log("[SDK] Collection Key:", key);
577
- console.log("[SDK] Document ID:", id);
578
- console.log("[SDK] Update Data:", data);
579
- try {
580
- const response = await this.client.sendDataRequest(
581
- "PATCH",
582
- {
583
- key,
584
- id,
585
- data
586
- }
587
- );
588
- console.log("[SDK] Update Response:", response);
589
- return response;
590
- } catch (error) {
591
- console.error("[SDK] UPDATE ERROR");
592
- console.error(error);
593
- throw error;
594
- }
595
- }
596
542
  async delete(key, id) {
597
543
  console.log("\n================ DELETE REQUEST ================");
598
544
  console.log("[SDK] Collection Key:", key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinkingdifferently/core",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Official SDK for Thinking Differently API",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -22,7 +22,8 @@
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
24
  "axios": "^1.16.0",
25
- "crypto-js": "^4.2.0"
25
+ "crypto-js": "^4.2.0",
26
+ "jose": "^6.2.3"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@types/node": "^25.6.2",