sfobjects-basic-client 1.2.2 → 1.2.4

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.ts CHANGED
@@ -221,6 +221,8 @@ export type SfObjectsIndex<OI> = {
221
221
  };
222
222
  export type SfObjectFlat<O> = {
223
223
  [K in KeyOf<O>]: NonNullable<O[K]> extends SfPrimitiveType ? O[K] : NonNullable<O[K]> extends ChildTable<infer CO> ? SfObjectFlat<CO> : SfObjectFlat<NonNullable<O[K]>>;
224
+ } & {
225
+ ['{...}']: O;
224
226
  };
225
227
  export declare const getSfObjects: <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions) => SfObjectsIndex<OI>;
226
228
  export declare const sfObject: <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => {
@@ -231,5 +233,8 @@ export declare const sfObject: <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>,
231
233
  asProjection: (v: any) => SfRootSelectProjection<OI, N, S>;
232
234
  asFlatProjection: (v: any) => SfObjectFlat<SfRootSelectProjection<OI, N, S>>;
233
235
  };
236
+ toSuperShortId: (id: string) => string;
237
+ normalizeId: (id: string) => string;
238
+ idEquals: (a: string, b: string) => boolean;
234
239
  };
235
240
  export {};
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ exports.sfObject = exports.getSfObjects = exports.SfBasicClientReadError = expor
25
25
  exports.isPlainObject = isPlainObject;
26
26
  exports.constructSoql = constructSoql;
27
27
  exports.getSfObject = getSfObject;
28
+ const utils_1 = require("./utils");
28
29
  // Where
29
30
  const OP_KEY_AND = '__and';
30
31
  const OP_KEY_OR = '__or';
@@ -322,6 +323,9 @@ const sfObject = (cfg, n) => ({
322
323
  value,
323
324
  asProjection: (v) => v,
324
325
  asFlatProjection: (v) => v
325
- })
326
+ }),
327
+ toSuperShortId: (id) => (0, utils_1.toSuperShortId)(id, cfg[n].objectPrefix),
328
+ normalizeId: (id) => (0, utils_1.normalizeId)(id, cfg[n].objectPrefix),
329
+ idEquals: (a, b) => (0, utils_1.idEquals)(a, b, cfg[n].objectPrefix)
326
330
  });
327
331
  exports.sfObject = sfObject;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Convert an 18-char Salesforce ID into a 12-char "super-short" ID.
3
+ * Removes:
4
+ * - 3-char object prefix
5
+ * - 3-char checksum at the end
6
+ *
7
+ * Structure:
8
+ * 18-char ID = PPP + XXXXXXXXXXXX + CCC
9
+ * Result = 12 chars (XXXXXXXXXXXX)
10
+ */
11
+ export declare function toSuperShortId(id: string, prefix: string): string;
12
+ /**
13
+ * Normalize any Salesforce ID (12, 15, 18 chars) to full 18-char format.
14
+ */
15
+ export declare function normalizeId(id: string, prefix: string): string;
16
+ /**
17
+ * Compare two Salesforce IDs, allowing either 15 or 18 char formats.
18
+ */
19
+ export declare function idEquals(a: string, b: string, prefix: string): boolean;
package/dist/utils.js ADDED
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toSuperShortId = toSuperShortId;
4
+ exports.normalizeId = normalizeId;
5
+ exports.idEquals = idEquals;
6
+ function toLongId(shortId) {
7
+ if (shortId.length !== 15)
8
+ return shortId; // assume it's already 18 or invalid
9
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
10
+ const chunks = [
11
+ shortId.substring(0, 5),
12
+ shortId.substring(5, 10),
13
+ shortId.substring(10, 15)
14
+ ];
15
+ let suffix = "";
16
+ for (const chunk of chunks) {
17
+ let flags = 0;
18
+ for (let i = 0; i < chunk.length; i++) {
19
+ const c = chunk[i];
20
+ if (c >= "A" && c <= "Z") {
21
+ flags |= 1 << i;
22
+ }
23
+ }
24
+ suffix += chars[flags];
25
+ }
26
+ return shortId + suffix;
27
+ }
28
+ /**
29
+ * Convert an 18-char Salesforce ID into a 12-char "super-short" ID.
30
+ * Removes:
31
+ * - 3-char object prefix
32
+ * - 3-char checksum at the end
33
+ *
34
+ * Structure:
35
+ * 18-char ID = PPP + XXXXXXXXXXXX + CCC
36
+ * Result = 12 chars (XXXXXXXXXXXX)
37
+ */
38
+ function toSuperShortId(id, prefix) {
39
+ if (!id) {
40
+ throw new Error("Salesforce ID cannot be empty.");
41
+ }
42
+ const trimmed = id.trim();
43
+ if (trimmed.length !== 18) {
44
+ throw new Error("Input must be an 18-character Salesforce ID.");
45
+ }
46
+ if (trimmed.substring(0, 2) !== prefix) {
47
+ throw new Error(`Id must contain prefix '${prefix}'`);
48
+ }
49
+ // Remove prefix (first 3) and checksum (last 3)
50
+ return trimmed.substring(3, 15); // characters 3 through 14 (12 chars)
51
+ }
52
+ /**
53
+ * Turn a 12-char super-short ID into a full 18-char ID using the given object prefix.
54
+ */
55
+ function expandSuperShort(body12, prefix) {
56
+ if (body12.length !== 12) {
57
+ throw new Error("Super-short ID must be 12 characters.");
58
+ }
59
+ if (prefix.length !== 3) {
60
+ throw new Error("Object prefix must be 3 characters.");
61
+ }
62
+ const id15 = prefix + body12; // prefix + 12-char body = 15-char ID
63
+ return toLongId(id15);
64
+ }
65
+ /**
66
+ * Normalize any Salesforce ID (12, 15, 18 chars) to full 18-char format.
67
+ */
68
+ function normalizeId(id, prefix) {
69
+ if (!id)
70
+ return "";
71
+ const trimmed = id.trim();
72
+ if (trimmed.length === 18) {
73
+ return trimmed;
74
+ }
75
+ if (trimmed.length === 15) {
76
+ return toLongId(trimmed);
77
+ }
78
+ if (trimmed.length === 12) {
79
+ return expandSuperShort(trimmed, prefix);
80
+ }
81
+ throw new Error(`Unsupported Salesforce ID length: ${trimmed.length}`);
82
+ }
83
+ /**
84
+ * Compare two Salesforce IDs, allowing either 15 or 18 char formats.
85
+ */
86
+ function idEquals(a, b, prefix) {
87
+ if (!a || !b)
88
+ return false;
89
+ return normalizeId(a, prefix) === normalizeId(b, prefix);
90
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfobjects-basic-client",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Basic Salesforce Client to use together with sfobjects-to-typescript lib",
5
5
  "license": "ISC",
6
6
  "author": "Murad Aliyev",
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  // utility
2
2
 
3
+ import { idEquals, normalizeId, toSuperShortId } from "./utils";
4
+
3
5
  type WrapNull<T> = T extends null ? null : never;
4
6
  type KeyOf<O> = (keyof O) & string;
5
7
  type SfPrimitiveType = string | number | boolean | bigint;
@@ -712,7 +714,7 @@ export type SfObjectFlat<O> = {
712
714
  NonNullable<O[K]> extends SfPrimitiveType ? O[K] :
713
715
  NonNullable<O[K]> extends ChildTable<infer CO> ? SfObjectFlat<CO> :
714
716
  SfObjectFlat<NonNullable<O[K]>>
715
- }
717
+ } & { ['{...}']: O }
716
718
 
717
719
  export const getSfObjects = <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions): SfObjectsIndex<OI> => {
718
720
 
@@ -731,5 +733,8 @@ export const sfObject = <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N)
731
733
  value,
732
734
  asProjection: (v: any) => v as SfRootSelectProjection<OI, N, S>,
733
735
  asFlatProjection: (v: any) => v as SfObjectFlat<SfRootSelectProjection<OI, N, S>>
734
- })
736
+ }),
737
+ toSuperShortId: (id: string) => toSuperShortId(id, cfg[n].objectPrefix),
738
+ normalizeId: (id: string) => normalizeId(id, cfg[n].objectPrefix),
739
+ idEquals: (a: string, b: string) => idEquals(a, b, cfg[n].objectPrefix)
735
740
  });
package/src/utils.ts ADDED
@@ -0,0 +1,106 @@
1
+ function toLongId(shortId: string): string {
2
+
3
+ if (shortId.length !== 15) return shortId; // assume it's already 18 or invalid
4
+
5
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
6
+
7
+ const chunks = [
8
+ shortId.substring(0, 5),
9
+ shortId.substring(5, 10),
10
+ shortId.substring(10, 15)
11
+ ];
12
+
13
+ let suffix = "";
14
+
15
+ for (const chunk of chunks) {
16
+ let flags = 0;
17
+ for (let i = 0; i < chunk.length; i++) {
18
+ const c = chunk[i];
19
+ if (c >= "A" && c <= "Z") {
20
+ flags |= 1 << i;
21
+ }
22
+ }
23
+
24
+ suffix += chars[flags];
25
+ }
26
+
27
+ return shortId + suffix;
28
+ }
29
+
30
+
31
+ /**
32
+ * Convert an 18-char Salesforce ID into a 12-char "super-short" ID.
33
+ * Removes:
34
+ * - 3-char object prefix
35
+ * - 3-char checksum at the end
36
+ *
37
+ * Structure:
38
+ * 18-char ID = PPP + XXXXXXXXXXXX + CCC
39
+ * Result = 12 chars (XXXXXXXXXXXX)
40
+ */
41
+ export function toSuperShortId(id: string, prefix: string): string {
42
+ if (!id) {
43
+ throw new Error("Salesforce ID cannot be empty.");
44
+ }
45
+
46
+ const trimmed = id.trim();
47
+
48
+ if (trimmed.length !== 18) {
49
+ throw new Error("Input must be an 18-character Salesforce ID.");
50
+ }
51
+
52
+ if (trimmed.substring(0, 2) !== prefix) {
53
+ throw new Error(`Id must contain prefix '${prefix}'`);
54
+ }
55
+
56
+ // Remove prefix (first 3) and checksum (last 3)
57
+ return trimmed.substring(3, 15); // characters 3 through 14 (12 chars)
58
+ }
59
+
60
+ /**
61
+ * Turn a 12-char super-short ID into a full 18-char ID using the given object prefix.
62
+ */
63
+ function expandSuperShort(body12: string, prefix: string): string {
64
+ if (body12.length !== 12) {
65
+ throw new Error("Super-short ID must be 12 characters.");
66
+ }
67
+ if (prefix.length !== 3) {
68
+ throw new Error("Object prefix must be 3 characters.");
69
+ }
70
+
71
+ const id15 = prefix + body12; // prefix + 12-char body = 15-char ID
72
+ return toLongId(id15);
73
+ }
74
+
75
+
76
+
77
+ /**
78
+ * Normalize any Salesforce ID (12, 15, 18 chars) to full 18-char format.
79
+ */
80
+ export function normalizeId(id: string, prefix: string): string {
81
+ if (!id) return "";
82
+
83
+ const trimmed = id.trim();
84
+
85
+ if (trimmed.length === 18) {
86
+ return trimmed;
87
+ }
88
+
89
+ if (trimmed.length === 15) {
90
+ return toLongId(trimmed);
91
+ }
92
+
93
+ if (trimmed.length === 12) {
94
+ return expandSuperShort(trimmed, prefix);
95
+ }
96
+
97
+ throw new Error(`Unsupported Salesforce ID length: ${trimmed.length}`);
98
+ }
99
+
100
+ /**
101
+ * Compare two Salesforce IDs, allowing either 15 or 18 char formats.
102
+ */
103
+ export function idEquals(a: string, b: string, prefix: string): boolean {
104
+ if (!a || !b) return false;
105
+ return normalizeId(a, prefix) === normalizeId(b, prefix);
106
+ }