@thinkingdifferently/core 1.3.1 → 1.4.1

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
@@ -46,7 +46,6 @@ var TDClient = class {
46
46
  this.securityKey = securityKey;
47
47
  this.publicKey = publicKey;
48
48
  this.api = import_axios.default.create({
49
- // baseURL: "http://localhost:3000/api/v1",
50
49
  baseURL: "https://www.thinkingdifferently.dev/api/v1",
51
50
  headers: {
52
51
  "X-API-Key": apiKey,
@@ -218,6 +217,7 @@ var QueryBuilder = class {
218
217
  operation: null,
219
218
  collection,
220
219
  filters: [],
220
+ $set: [],
221
221
  limit: null,
222
222
  sort: null
223
223
  };
@@ -246,6 +246,14 @@ var QueryBuilder = class {
246
246
  this.query.sort = { field, direction };
247
247
  return this;
248
248
  }
249
+ set(field, value) {
250
+ if (!field.trim()) {
251
+ throw new Error("Field name cannot be empty in .set()");
252
+ }
253
+ this.query.$set = this.query.$set || [];
254
+ this.query.$set.push({ field, value });
255
+ return this;
256
+ }
249
257
  // build() {
250
258
  // return this.query;
251
259
  // }
@@ -339,31 +347,46 @@ var QueryBuilder = class {
339
347
  throw error;
340
348
  }
341
349
  }
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 ================");
350
+ async updateById(id) {
351
+ this.query.operation = "updateById";
352
+ this.query.updateById = id;
353
+ console.log("\n================ UPDATE BY ID REQUEST ================");
354
+ console.log("[SDK] Final Query:", this.query);
355
+ try {
356
+ const response = await this.client.sendDataRequest(
357
+ "PATCH",
358
+ this.query
359
+ );
360
+ console.log("[SDK] Update By ID Response:", response);
361
+ return response;
362
+ } catch (error) {
363
+ console.error("[SDK] UPDATE BY ID ERROR");
364
+ console.error(error);
365
+ throw error;
366
+ }
367
+ }
368
+ async updateOne() {
369
+ this.query.operation = "updateOne";
370
+ console.log("\n================ UPDATE ONE REQUEST ================");
347
371
  console.log("[SDK] Final Query:", this.query);
348
372
  try {
349
373
  const response = await this.client.sendDataRequest(
350
374
  "PATCH",
351
375
  this.query
352
376
  );
353
- console.log("[SDK] Update Response:", response);
377
+ console.log("[SDK] Update One Response:", response);
354
378
  return response;
355
379
  } catch (error) {
356
- console.error("[SDK] UPDATE ERROR");
380
+ console.error("[SDK] UPDATE ONE ERROR");
357
381
  console.error(error);
358
382
  throw error;
359
383
  }
360
384
  }
361
385
  //updateMany
362
386
  // 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) {
387
+ //example sdk.collection("animals").where("price", ">", 1000).updateMany() it will update all the animals whose price is greater than 1000
388
+ async updateMany() {
365
389
  this.query.operation = "updateMany";
366
- this.query.data = data;
367
390
  console.log("\n================ UPDATE MANY REQUEST ================");
368
391
  console.log("[SDK] Final Query:", this.query);
369
392
  try {
@@ -420,7 +443,7 @@ var QueryBuilder = class {
420
443
  };
421
444
 
422
445
  // src/auth.ts
423
- var crypto = __toESM(require("crypto"));
446
+ var import_jose = require("jose");
424
447
  var AdminAuth = class {
425
448
  constructor(client) {
426
449
  this.client = client;
@@ -448,77 +471,21 @@ var AdminAuth = class {
448
471
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
449
472
  * Supported in Node.js / Server-side environments.
450
473
  */
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) {
474
+ async verifyJwt(token, publicKeyPem) {
466
475
  const keyToUse = publicKeyPem || this.client.getPublicKey();
467
476
  if (!keyToUse) {
468
477
  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."
478
+ "[ThinkingDifferently SDK Error] Public key is required for JWT verification."
475
479
  );
476
480
  }
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
481
+ const publicKey = await (0, import_jose.importSPKI)(
482
+ keyToUse,
483
+ "EdDSA"
484
+ );
485
+ const { payload } = await (0, import_jose.jwtVerify)(
486
+ token,
487
+ publicKey
505
488
  );
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
489
  return payload;
523
490
  }
524
491
  };
@@ -610,28 +577,6 @@ var ThinkingDifferently = class {
610
577
  throw error;
611
578
  }
612
579
  }
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
580
  async delete(key, id) {
636
581
  console.log("\n================ DELETE REQUEST ================");
637
582
  console.log("[SDK] Collection Key:", key);
package/dist/index.mjs CHANGED
@@ -7,7 +7,6 @@ var TDClient = class {
7
7
  this.securityKey = securityKey;
8
8
  this.publicKey = publicKey;
9
9
  this.api = axios.create({
10
- // baseURL: "http://localhost:3000/api/v1",
11
10
  baseURL: "https://www.thinkingdifferently.dev/api/v1",
12
11
  headers: {
13
12
  "X-API-Key": apiKey,
@@ -179,6 +178,7 @@ var QueryBuilder = class {
179
178
  operation: null,
180
179
  collection,
181
180
  filters: [],
181
+ $set: [],
182
182
  limit: null,
183
183
  sort: null
184
184
  };
@@ -207,6 +207,14 @@ var QueryBuilder = class {
207
207
  this.query.sort = { field, direction };
208
208
  return this;
209
209
  }
210
+ set(field, value) {
211
+ if (!field.trim()) {
212
+ throw new Error("Field name cannot be empty in .set()");
213
+ }
214
+ this.query.$set = this.query.$set || [];
215
+ this.query.$set.push({ field, value });
216
+ return this;
217
+ }
210
218
  // build() {
211
219
  // return this.query;
212
220
  // }
@@ -300,31 +308,46 @@ var QueryBuilder = class {
300
308
  throw error;
301
309
  }
302
310
  }
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 ================");
311
+ async updateById(id) {
312
+ this.query.operation = "updateById";
313
+ this.query.updateById = id;
314
+ console.log("\n================ UPDATE BY ID REQUEST ================");
315
+ console.log("[SDK] Final Query:", this.query);
316
+ try {
317
+ const response = await this.client.sendDataRequest(
318
+ "PATCH",
319
+ this.query
320
+ );
321
+ console.log("[SDK] Update By ID Response:", response);
322
+ return response;
323
+ } catch (error) {
324
+ console.error("[SDK] UPDATE BY ID ERROR");
325
+ console.error(error);
326
+ throw error;
327
+ }
328
+ }
329
+ async updateOne() {
330
+ this.query.operation = "updateOne";
331
+ console.log("\n================ UPDATE ONE REQUEST ================");
308
332
  console.log("[SDK] Final Query:", this.query);
309
333
  try {
310
334
  const response = await this.client.sendDataRequest(
311
335
  "PATCH",
312
336
  this.query
313
337
  );
314
- console.log("[SDK] Update Response:", response);
338
+ console.log("[SDK] Update One Response:", response);
315
339
  return response;
316
340
  } catch (error) {
317
- console.error("[SDK] UPDATE ERROR");
341
+ console.error("[SDK] UPDATE ONE ERROR");
318
342
  console.error(error);
319
343
  throw error;
320
344
  }
321
345
  }
322
346
  //updateMany
323
347
  // 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) {
348
+ //example sdk.collection("animals").where("price", ">", 1000).updateMany() it will update all the animals whose price is greater than 1000
349
+ async updateMany() {
326
350
  this.query.operation = "updateMany";
327
- this.query.data = data;
328
351
  console.log("\n================ UPDATE MANY REQUEST ================");
329
352
  console.log("[SDK] Final Query:", this.query);
330
353
  try {
@@ -381,7 +404,7 @@ var QueryBuilder = class {
381
404
  };
382
405
 
383
406
  // src/auth.ts
384
- import * as crypto from "crypto";
407
+ import { jwtVerify, importSPKI } from "jose";
385
408
  var AdminAuth = class {
386
409
  constructor(client) {
387
410
  this.client = client;
@@ -409,77 +432,21 @@ var AdminAuth = class {
409
432
  * Offline verification of Ed25519 (EdDSA) JWT admin token using public key.
410
433
  * Supported in Node.js / Server-side environments.
411
434
  */
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) {
435
+ async verifyJwt(token, publicKeyPem) {
427
436
  const keyToUse = publicKeyPem || this.client.getPublicKey();
428
437
  if (!keyToUse) {
429
438
  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."
439
+ "[ThinkingDifferently SDK Error] Public key is required for JWT verification."
436
440
  );
437
441
  }
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
442
+ const publicKey = await importSPKI(
443
+ keyToUse,
444
+ "EdDSA"
445
+ );
446
+ const { payload } = await jwtVerify(
447
+ token,
448
+ publicKey
466
449
  );
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
450
  return payload;
484
451
  }
485
452
  };
@@ -571,28 +538,6 @@ var ThinkingDifferently = class {
571
538
  throw error;
572
539
  }
573
540
  }
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
541
  async delete(key, id) {
597
542
  console.log("\n================ DELETE REQUEST ================");
598
543
  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.1",
3
+ "version": "1.4.1",
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",