sfobjects-basic-client 1.2.0 → 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 +18 -2
- package/dist/index.js +13 -1
- package/dist/utils.d.ts +19 -0
- package/dist/utils.js +90 -0
- package/package.json +1 -1
- package/src/index.ts +23 -2
- package/src/utils.ts +106 -0
package/dist/index.d.ts
CHANGED
|
@@ -214,11 +214,27 @@ export declare class SfBasicClientReadError extends Error {
|
|
|
214
214
|
export declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
215
215
|
export declare function constructSoql<OI>(_cfg: SfObjCfgIndex<OI>): (objName: string, from: string, select: (string | {})[], where?: string | Record<string, any>, orderBy?: Record<string, any>, limit?: number) => string;
|
|
216
216
|
export declare function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions): <N extends KeyOf<OI>>(from: N, _conn: ISfConnection) => SfObjActions<OI, N>;
|
|
217
|
-
export
|
|
218
|
-
|
|
217
|
+
export type SfObjectsIndex<OI> = {
|
|
218
|
+
[N in KeyOf<OI>]: SfObjActions<OI, N>;
|
|
219
|
+
} & {
|
|
220
|
+
__getObject: <N extends KeyOf<OI>>(from: N) => SfObjActions<OI, N>;
|
|
221
|
+
};
|
|
222
|
+
export type SfObjectFlat<O> = {
|
|
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;
|
|
219
226
|
};
|
|
227
|
+
export declare const getSfObjects: <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions) => SfObjectsIndex<OI>;
|
|
220
228
|
export declare const sfObject: <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => {
|
|
221
229
|
info: SfObjCfgIndex<OI>[N];
|
|
222
230
|
select: <S extends SfRootSelect<OI, N>>(s: S[]) => S[];
|
|
231
|
+
wrapSelection: <S extends SfRootSelect<OI, N>>(value: S[]) => {
|
|
232
|
+
value: S[];
|
|
233
|
+
asProjection: (v: any) => SfRootSelectProjection<OI, N, S>;
|
|
234
|
+
asFlatProjection: (v: any) => SfObjectFlat<SfRootSelectProjection<OI, N, S>>;
|
|
235
|
+
};
|
|
236
|
+
toSuperShortId: (id: string) => string;
|
|
237
|
+
normalizeId: (id: string) => string;
|
|
238
|
+
idEquals: (a: string, b: string) => boolean;
|
|
223
239
|
};
|
|
224
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';
|
|
@@ -315,5 +316,16 @@ const getSfObjects = (cfg) => (conn, options) => {
|
|
|
315
316
|
return Object.assign(Object.assign({}, Object.keys(cfg).reduce((p, n) => (Object.assign(Object.assign({}, p), { [n]: __getObject(n) })), {})), { __getObject });
|
|
316
317
|
};
|
|
317
318
|
exports.getSfObjects = getSfObjects;
|
|
318
|
-
const sfObject = (cfg, n) => ({
|
|
319
|
+
const sfObject = (cfg, n) => ({
|
|
320
|
+
info: cfg[n],
|
|
321
|
+
select: (s) => s,
|
|
322
|
+
wrapSelection: (value) => ({
|
|
323
|
+
value,
|
|
324
|
+
asProjection: (v) => v,
|
|
325
|
+
asFlatProjection: (v) => v
|
|
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)
|
|
330
|
+
});
|
|
319
331
|
exports.sfObject = sfObject;
|
package/dist/utils.d.ts
ADDED
|
@@ -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
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;
|
|
@@ -705,8 +707,16 @@ export function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions) {
|
|
|
705
707
|
|
|
706
708
|
}
|
|
707
709
|
|
|
710
|
+
export type SfObjectsIndex<OI> = { [N in KeyOf<OI>]: SfObjActions<OI, N> } & { __getObject: <N extends KeyOf<OI>>(from: N) => SfObjActions<OI, N> };
|
|
711
|
+
|
|
712
|
+
export type SfObjectFlat<O> = {
|
|
713
|
+
[K in KeyOf<O>]:
|
|
714
|
+
NonNullable<O[K]> extends SfPrimitiveType ? O[K] :
|
|
715
|
+
NonNullable<O[K]> extends ChildTable<infer CO> ? SfObjectFlat<CO> :
|
|
716
|
+
SfObjectFlat<NonNullable<O[K]>>
|
|
717
|
+
} & { ['{...}']: O }
|
|
708
718
|
|
|
709
|
-
export const getSfObjects = <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions) => {
|
|
719
|
+
export const getSfObjects = <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions): SfObjectsIndex<OI> => {
|
|
710
720
|
|
|
711
721
|
const __getObject = <N extends KeyOf<OI>>(from: N) => getSfObject<OI>(cfg, options)(from, conn);
|
|
712
722
|
|
|
@@ -716,4 +726,15 @@ export const getSfObjects = <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection
|
|
|
716
726
|
};
|
|
717
727
|
}
|
|
718
728
|
|
|
719
|
-
export const sfObject = <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => ({
|
|
729
|
+
export const sfObject = <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => ({
|
|
730
|
+
info: cfg[n],
|
|
731
|
+
select: <S extends SfRootSelect<OI, N>>(s: S[]) => s,
|
|
732
|
+
wrapSelection: <S extends SfRootSelect<OI, N>>(value: S[]) => ({
|
|
733
|
+
value,
|
|
734
|
+
asProjection: (v: any) => v as SfRootSelectProjection<OI, N, S>,
|
|
735
|
+
asFlatProjection: (v: any) => v as SfObjectFlat<SfRootSelectProjection<OI, N, S>>
|
|
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)
|
|
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
|
+
}
|