@salesforce/capg-client 0.1.0-beta.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.
Files changed (68) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +122 -0
  3. package/dist/client.d.ts +86 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +167 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/connect-api-client.d.ts +82 -0
  8. package/dist/connect-api-client.d.ts.map +1 -0
  9. package/dist/connect-api-client.js +106 -0
  10. package/dist/connect-api-client.js.map +1 -0
  11. package/dist/constants.d.ts +64 -0
  12. package/dist/constants.d.ts.map +1 -0
  13. package/dist/constants.js +68 -0
  14. package/dist/constants.js.map +1 -0
  15. package/dist/errors/api.d.ts +79 -0
  16. package/dist/errors/api.d.ts.map +1 -0
  17. package/dist/errors/api.js +128 -0
  18. package/dist/errors/api.js.map +1 -0
  19. package/dist/errors/auth.d.ts +35 -0
  20. package/dist/errors/auth.d.ts.map +1 -0
  21. package/dist/errors/auth.js +50 -0
  22. package/dist/errors/auth.js.map +1 -0
  23. package/dist/errors/base.d.ts +103 -0
  24. package/dist/errors/base.d.ts.map +1 -0
  25. package/dist/errors/base.js +162 -0
  26. package/dist/errors/base.js.map +1 -0
  27. package/dist/errors/hub-org.d.ts +108 -0
  28. package/dist/errors/hub-org.d.ts.map +1 -0
  29. package/dist/errors/hub-org.js +144 -0
  30. package/dist/errors/hub-org.js.map +1 -0
  31. package/dist/errors/index.d.ts +33 -0
  32. package/dist/errors/index.d.ts.map +1 -0
  33. package/dist/errors/index.js +42 -0
  34. package/dist/errors/index.js.map +1 -0
  35. package/dist/errors/network.d.ts +53 -0
  36. package/dist/errors/network.d.ts.map +1 -0
  37. package/dist/errors/network.js +77 -0
  38. package/dist/errors/network.js.map +1 -0
  39. package/dist/errors/validation.d.ts +69 -0
  40. package/dist/errors/validation.d.ts.map +1 -0
  41. package/dist/errors/validation.js +96 -0
  42. package/dist/errors/validation.js.map +1 -0
  43. package/dist/index.d.ts +16 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +28 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/org-pref-checker.d.ts +129 -0
  48. package/dist/org-pref-checker.d.ts.map +1 -0
  49. package/dist/org-pref-checker.js +193 -0
  50. package/dist/org-pref-checker.js.map +1 -0
  51. package/dist/policy-fetcher.d.ts +144 -0
  52. package/dist/policy-fetcher.d.ts.map +1 -0
  53. package/dist/policy-fetcher.js +241 -0
  54. package/dist/policy-fetcher.js.map +1 -0
  55. package/dist/tsconfig.tsbuildinfo +1 -0
  56. package/dist/types/index.d.ts +532 -0
  57. package/dist/types/index.d.ts.map +1 -0
  58. package/dist/types/index.js +6 -0
  59. package/dist/types/index.js.map +1 -0
  60. package/dist/types/result.d.ts +282 -0
  61. package/dist/types/result.d.ts.map +1 -0
  62. package/dist/types/result.js +351 -0
  63. package/dist/types/result.js.map +1 -0
  64. package/dist/utils/error-classifier.d.ts +55 -0
  65. package/dist/utils/error-classifier.d.ts.map +1 -0
  66. package/dist/utils/error-classifier.js +90 -0
  67. package/dist/utils/error-classifier.js.map +1 -0
  68. package/package.json +143 -0
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Validation Error Class
3
+ *
4
+ * Thrown when client-side input validation fails.
5
+ * This error indicates that the user provided invalid input that must be corrected.
6
+ *
7
+ * Properties:
8
+ * - code: 'VALIDATION_ERROR'
9
+ * - isRetryable: false (bad input won't succeed on retry)
10
+ * - isUserActionable: true (user can fix by providing valid input)
11
+ * - fieldName: The field that failed validation
12
+ * - invalidValue: The invalid value that was provided (optional)
13
+ * - suggestedAction: Guidance on how to fix the validation error
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * throw new CAPGValidationError('surface', 'Invalid surface type', 'invalid_surface');
18
+ * ```
19
+ */
20
+ import { CAPGError } from './base.js';
21
+ export declare class CAPGValidationError extends CAPGError {
22
+ /**
23
+ * The field name that failed validation
24
+ *
25
+ * @readonly
26
+ */
27
+ readonly fieldName: string;
28
+ /**
29
+ * The invalid value that was provided (optional)
30
+ * Can be any type (string, number, object, etc.)
31
+ *
32
+ * @readonly
33
+ */
34
+ readonly invalidValue?: unknown;
35
+ /**
36
+ * Creates a new CAPGValidationError instance
37
+ *
38
+ * @param fieldName - The field name that failed validation
39
+ * @param message - Human-readable error message
40
+ * @param invalidValue - Optional invalid value that was provided
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * // Simple validation error
45
+ * const error1 = new CAPGValidationError('surface', 'Invalid surface type');
46
+ *
47
+ * // With invalid value
48
+ * const error2 = new CAPGValidationError(
49
+ * 'surface',
50
+ * 'Surface must be one of: vibes, vaas, codey',
51
+ * 'invalid_surface'
52
+ * );
53
+ *
54
+ * // With complex invalid value
55
+ * const error3 = new CAPGValidationError(
56
+ * 'options',
57
+ * 'Invalid options object',
58
+ * { surface: 'vibes', scope: { type: 'INVALID' } }
59
+ * );
60
+ * ```
61
+ */
62
+ constructor(fieldName: string, message: string, invalidValue?: unknown);
63
+ /**
64
+ * Serializes the validation error to JSON
65
+ * Includes fieldName and invalidValue properties
66
+ */
67
+ toJSON(): Record<string, unknown>;
68
+ }
69
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/errors/validation.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,qBAAa,mBAAoB,SAAQ,SAAS;IAChD;;;;OAIG;IACH,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC;;;;;OAKG;IACH,SAAgB,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;gBACgB,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,OAAO;IA0B7E;;;OAGG;IACI,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAazC"}
@@ -0,0 +1,96 @@
1
+ /*
2
+ * Copyright 2026, Salesforce, Inc. All rights reserved.
3
+ * See LICENSE.txt for license terms.
4
+ */
5
+ /**
6
+ * Validation Error Class
7
+ *
8
+ * Thrown when client-side input validation fails.
9
+ * This error indicates that the user provided invalid input that must be corrected.
10
+ *
11
+ * Properties:
12
+ * - code: 'VALIDATION_ERROR'
13
+ * - isRetryable: false (bad input won't succeed on retry)
14
+ * - isUserActionable: true (user can fix by providing valid input)
15
+ * - fieldName: The field that failed validation
16
+ * - invalidValue: The invalid value that was provided (optional)
17
+ * - suggestedAction: Guidance on how to fix the validation error
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * throw new CAPGValidationError('surface', 'Invalid surface type', 'invalid_surface');
22
+ * ```
23
+ */
24
+ import { CAPGError } from './base.js';
25
+ export class CAPGValidationError extends CAPGError {
26
+ /**
27
+ * The field name that failed validation
28
+ *
29
+ * @readonly
30
+ */
31
+ fieldName;
32
+ /**
33
+ * The invalid value that was provided (optional)
34
+ * Can be any type (string, number, object, etc.)
35
+ *
36
+ * @readonly
37
+ */
38
+ invalidValue;
39
+ /**
40
+ * Creates a new CAPGValidationError instance
41
+ *
42
+ * @param fieldName - The field name that failed validation
43
+ * @param message - Human-readable error message
44
+ * @param invalidValue - Optional invalid value that was provided
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * // Simple validation error
49
+ * const error1 = new CAPGValidationError('surface', 'Invalid surface type');
50
+ *
51
+ * // With invalid value
52
+ * const error2 = new CAPGValidationError(
53
+ * 'surface',
54
+ * 'Surface must be one of: vibes, vaas, codey',
55
+ * 'invalid_surface'
56
+ * );
57
+ *
58
+ * // With complex invalid value
59
+ * const error3 = new CAPGValidationError(
60
+ * 'options',
61
+ * 'Invalid options object',
62
+ * { surface: 'vibes', scope: { type: 'INVALID' } }
63
+ * );
64
+ * ```
65
+ */
66
+ constructor(fieldName, message, invalidValue) {
67
+ super('VALIDATION_ERROR', message, `Check the '${fieldName}' field and provide a valid value`, undefined, false, // Not retryable - bad input won't succeed on retry
68
+ true);
69
+ // Set error name to class name for better stack traces
70
+ this.name = 'CAPGValidationError';
71
+ // Store field name and invalid value
72
+ this.fieldName = fieldName;
73
+ this.invalidValue = invalidValue;
74
+ // Maintain proper prototype chain for instanceof checks
75
+ Object.setPrototypeOf(this, CAPGValidationError.prototype);
76
+ // Capture stack trace for debugging
77
+ if (Error.captureStackTrace) {
78
+ Error.captureStackTrace(this, this.constructor);
79
+ }
80
+ }
81
+ /**
82
+ * Serializes the validation error to JSON
83
+ * Includes fieldName and invalidValue properties
84
+ */
85
+ toJSON() {
86
+ const json = super.toJSON();
87
+ // Always include fieldName
88
+ json.fieldName = this.fieldName;
89
+ // Include invalidValue if present (even if null or undefined)
90
+ if (this.invalidValue !== undefined) {
91
+ json.invalidValue = this.invalidValue;
92
+ }
93
+ return json;
94
+ }
95
+ }
96
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/errors/validation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,OAAO,mBAAoB,SAAQ,SAAS;IAChD;;;;OAIG;IACa,SAAS,CAAS;IAElC;;;;;OAKG;IACa,YAAY,CAAW;IAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,YAAmB,SAAiB,EAAE,OAAe,EAAE,YAAsB;QAC3E,KAAK,CACH,kBAAkB,EAClB,OAAO,EACP,cAAc,SAAS,mCAAmC,EAC1D,SAAS,EACT,KAAK,EAAE,mDAAmD;QAC1D,IAAI,CACL,CAAC;QAEF,uDAAuD;QACvD,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAElC,qCAAqC;QACrC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QAEjC,wDAAwD;QACxD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,mBAAmB,CAAC,SAAS,CAAC,CAAC;QAE3D,oCAAoC;QACpC,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,MAAM;QACX,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QAE5B,2BAA2B;QAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAEhC,8DAA8D;QAC9D,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;YACpC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;QACxC,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @salesforce/capg-client
3
+ *
4
+ * TypeScript client library for CAPG (Coding Agent Policy Governance) data fetching
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export { CAPGClient } from './client.js';
9
+ export type { CAPGClientOptions } from './client.js';
10
+ export * from './types/index.js';
11
+ export * from './errors/index.js';
12
+ export { OrgPrefChecker } from './org-pref-checker.js';
13
+ export { PolicyFetcher } from './policy-fetcher.js';
14
+ export { ConnectAPIClient } from './connect-api-client.js';
15
+ export { BPO_NAMES, SOQL_QUERIES, SALESFORCE_ERROR_CODES } from './constants.js';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGrD,cAAc,kBAAkB,CAAC;AAGjC,cAAc,mBAAmB,CAAC;AAGlC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAGvD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAG3D,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright 2026, Salesforce, Inc. All rights reserved.
3
+ * See LICENSE.txt for license terms.
4
+ */
5
+ /**
6
+ * @salesforce/capg-client
7
+ *
8
+ * TypeScript client library for CAPG (Coding Agent Policy Governance) data fetching
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+ // Export main client class
13
+ export { CAPGClient } from './client.js';
14
+ // Export types
15
+ export * from './types/index.js';
16
+ // Export error classes
17
+ export * from './errors/index.js';
18
+ // Export OrgPrefChecker
19
+ export { OrgPrefChecker } from './org-pref-checker.js';
20
+ // Export PolicyFetcher
21
+ export { PolicyFetcher } from './policy-fetcher.js';
22
+ // Export ConnectAPIClient
23
+ export { ConnectAPIClient } from './connect-api-client.js';
24
+ // Export constants (BPO names, SOQL queries)
25
+ export { BPO_NAMES, SOQL_QUERIES, SALESFORCE_ERROR_CODES } from './constants.js';
26
+ // TODO: Export interfaces
27
+ // export * from './interfaces/index.js';
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;GAMG;AAEH,2BAA2B;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,eAAe;AACf,cAAc,kBAAkB,CAAC;AAEjC,uBAAuB;AACvB,cAAc,mBAAmB,CAAC;AAElC,wBAAwB;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,uBAAuB;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,0BAA0B;AAC1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,6CAA6C;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAEjF,0BAA0B;AAC1B,yCAAyC"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * OrgPrefChecker - Check if CAPG governance is enabled for an org
3
+ *
4
+ * This module provides the first gate in the governance flow - checking if
5
+ * CAPG governance is enabled by querying for the DevopsGovernancePolicy BPO.
6
+ * When governance is disabled (BPO does not exist), this enables a 75% faster
7
+ * early return path.
8
+ *
9
+ * @packageDocumentation
10
+ * @since v1.0.0
11
+ */
12
+ import { Connection } from '@salesforce/core';
13
+ /**
14
+ * OrgPrefChecker - Check governance preferences for a Salesforce org
15
+ *
16
+ * Queries for the DevopsGovernancePolicy BPO to determine if governance is
17
+ * enabled. This is the FIRST GATE in the governance flow - when governance
18
+ * is disabled, consumers can skip policy/tool fetching (75% faster).
19
+ *
20
+ * @example Basic usage with org alias
21
+ * ```typescript
22
+ * const checker = new OrgPrefChecker();
23
+ * const enabled = await checker.checkGovernanceEnabled('myHubOrg');
24
+ *
25
+ * if (!enabled) {
26
+ * console.log('Governance disabled, skipping policy fetch');
27
+ * return;
28
+ * }
29
+ *
30
+ * // Only fetch if enabled
31
+ * const policies = await client.fetchPolicies(...);
32
+ * ```
33
+ *
34
+ * @example Usage with Connection object
35
+ * ```typescript
36
+ * import { Connection, Org } from '@salesforce/core';
37
+ *
38
+ * const org = await Org.create({ aliasOrUsername: 'myHubOrg' });
39
+ * const connection = org.getConnection();
40
+ *
41
+ * const checker = new OrgPrefChecker();
42
+ * const enabled = await checker.checkGovernanceEnabled(connection);
43
+ * ```
44
+ *
45
+ * @example With custom logger
46
+ * ```typescript
47
+ * const checker = new OrgPrefChecker({
48
+ * logger: {
49
+ * debug: (msg) => console.log('[DEBUG]', msg),
50
+ * error: (msg) => console.error('[ERROR]', msg)
51
+ * }
52
+ * });
53
+ *
54
+ * const enabled = await checker.checkGovernanceEnabled('myHubOrg');
55
+ * ```
56
+ *
57
+ * @since v1.0.0
58
+ */
59
+ export declare class OrgPrefChecker {
60
+ /**
61
+ * Optional logger for debug/error output
62
+ */
63
+ private logger?;
64
+ /**
65
+ * Create a new OrgPrefChecker instance
66
+ *
67
+ * @param options - Configuration options
68
+ * @param options.logger - Optional logger with debug/error methods
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // Without logger
73
+ * const checker = new OrgPrefChecker();
74
+ *
75
+ * // With logger
76
+ * const checker = new OrgPrefChecker({
77
+ * logger: {
78
+ * debug: (msg) => console.log(msg),
79
+ * error: (msg) => console.error(msg)
80
+ * }
81
+ * });
82
+ * ```
83
+ */
84
+ constructor(options?: {
85
+ logger?: {
86
+ debug: (message: string) => void;
87
+ error: (message: string) => void;
88
+ };
89
+ });
90
+ /**
91
+ * Check if CAPG governance is enabled for the specified org
92
+ *
93
+ * Queries for the DevopsGovernancePolicy BPO:
94
+ * - Returns true if query succeeds (BPO exists, governance enabled)
95
+ * - Returns false if INVALID_TYPE error (BPO does not exist, governance disabled)
96
+ * - Throws for auth/connection/API errors (never for governance state)
97
+ *
98
+ * **Performance**: This is the FIRST GATE - when governance is disabled,
99
+ * consumers can skip policy/tool fetching (75% faster session initialization).
100
+ *
101
+ * **NO HUB ORG AUTO-DETECTION**: This method requires an explicit org alias or
102
+ * Connection object. There is no automatic Hub Org resolution. This design aligns
103
+ * with Connect API architecture where org resolution is managed by the consumer
104
+ * framework, not the library.
105
+ *
106
+ * @param orgAliasOrConnection - Explicit org alias (string) or Connection object.
107
+ * REQUIRED - no auto-detection or Hub Org resolution performed.
108
+ * @returns true if governance enabled, false if disabled (BPO does not exist)
109
+ *
110
+ * @throws {@link CAPGAuthError} - Authentication failure (isRetryable = false)
111
+ * @throws {@link CAPGNetworkError} - Network/connection error (isRetryable = true)
112
+ * @throws {@link CAPGAPIError} - API error (isRetryable depends on status code)
113
+ * @throws {@link CAPGValidationError} - Invalid org alias or org not found
114
+ *
115
+ * @since v1.0.0
116
+ */
117
+ checkGovernanceEnabled(orgAliasOrConnection: string | Connection): Promise<boolean>;
118
+ /**
119
+ * Resolve org alias or Connection to a Connection object
120
+ *
121
+ * @param orgAliasOrConnection - Org alias (string) or Connection object
122
+ * @returns Connection object
123
+ * @throws CAPGValidationError if alias is invalid or org not found
124
+ *
125
+ * @internal
126
+ */
127
+ private resolveConnection;
128
+ }
129
+ //# sourceMappingURL=org-pref-checker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org-pref-checker.d.ts","sourceRoot":"","sources":["../src/org-pref-checker.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AAIH,OAAO,EAAE,UAAU,EAAO,MAAM,kBAAkB,CAAC;AAmBnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,qBAAa,cAAc;IACzB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,CAGb;IAEF;;;;;;;;;;;;;;;;;;;OAmBG;gBACgB,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE;YAAE,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;YAAC,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;SAAE,CAAA;KAAE;IAIhH;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,sBAAsB,CAAC,oBAAoB,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC;IAgChG;;;;;;;;OAQG;YACW,iBAAiB;CAsBhC"}
@@ -0,0 +1,193 @@
1
+ /*
2
+ * Copyright 2026, Salesforce, Inc. All rights reserved.
3
+ * See LICENSE.txt for license terms.
4
+ */
5
+ /**
6
+ * OrgPrefChecker - Check if CAPG governance is enabled for an org
7
+ *
8
+ * This module provides the first gate in the governance flow - checking if
9
+ * CAPG governance is enabled by querying for the DevopsGovernancePolicy BPO.
10
+ * When governance is disabled (BPO does not exist), this enables a 75% faster
11
+ * early return path.
12
+ *
13
+ * @packageDocumentation
14
+ * @since v1.0.0
15
+ */
16
+ /* eslint-disable class-methods-use-this, @typescript-eslint/no-unsafe-call, @typescript-eslint/member-ordering */
17
+ import { Org } from '@salesforce/core';
18
+ import { CAPGValidationError } from './errors/index.js';
19
+ import { classifyError } from './utils/error-classifier.js';
20
+ import { SOQL_QUERIES, SALESFORCE_ERROR_CODES } from './constants.js';
21
+ /**
22
+ * Check if an error represents an INVALID_TYPE SOQL error
23
+ * (indicates the BPO object does not exist in the org)
24
+ *
25
+ * @internal
26
+ */
27
+ function isInvalidTypeError(error) {
28
+ if (error == null || typeof error !== 'object') {
29
+ return false;
30
+ }
31
+ const err = error;
32
+ return err.errorCode === SALESFORCE_ERROR_CODES.INVALID_TYPE || err.name === SALESFORCE_ERROR_CODES.INVALID_TYPE;
33
+ }
34
+ /**
35
+ * OrgPrefChecker - Check governance preferences for a Salesforce org
36
+ *
37
+ * Queries for the DevopsGovernancePolicy BPO to determine if governance is
38
+ * enabled. This is the FIRST GATE in the governance flow - when governance
39
+ * is disabled, consumers can skip policy/tool fetching (75% faster).
40
+ *
41
+ * @example Basic usage with org alias
42
+ * ```typescript
43
+ * const checker = new OrgPrefChecker();
44
+ * const enabled = await checker.checkGovernanceEnabled('myHubOrg');
45
+ *
46
+ * if (!enabled) {
47
+ * console.log('Governance disabled, skipping policy fetch');
48
+ * return;
49
+ * }
50
+ *
51
+ * // Only fetch if enabled
52
+ * const policies = await client.fetchPolicies(...);
53
+ * ```
54
+ *
55
+ * @example Usage with Connection object
56
+ * ```typescript
57
+ * import { Connection, Org } from '@salesforce/core';
58
+ *
59
+ * const org = await Org.create({ aliasOrUsername: 'myHubOrg' });
60
+ * const connection = org.getConnection();
61
+ *
62
+ * const checker = new OrgPrefChecker();
63
+ * const enabled = await checker.checkGovernanceEnabled(connection);
64
+ * ```
65
+ *
66
+ * @example With custom logger
67
+ * ```typescript
68
+ * const checker = new OrgPrefChecker({
69
+ * logger: {
70
+ * debug: (msg) => console.log('[DEBUG]', msg),
71
+ * error: (msg) => console.error('[ERROR]', msg)
72
+ * }
73
+ * });
74
+ *
75
+ * const enabled = await checker.checkGovernanceEnabled('myHubOrg');
76
+ * ```
77
+ *
78
+ * @since v1.0.0
79
+ */
80
+ export class OrgPrefChecker {
81
+ /**
82
+ * Optional logger for debug/error output
83
+ */
84
+ logger;
85
+ /**
86
+ * Create a new OrgPrefChecker instance
87
+ *
88
+ * @param options - Configuration options
89
+ * @param options.logger - Optional logger with debug/error methods
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // Without logger
94
+ * const checker = new OrgPrefChecker();
95
+ *
96
+ * // With logger
97
+ * const checker = new OrgPrefChecker({
98
+ * logger: {
99
+ * debug: (msg) => console.log(msg),
100
+ * error: (msg) => console.error(msg)
101
+ * }
102
+ * });
103
+ * ```
104
+ */
105
+ constructor(options) {
106
+ this.logger = options?.logger;
107
+ }
108
+ /**
109
+ * Check if CAPG governance is enabled for the specified org
110
+ *
111
+ * Queries for the DevopsGovernancePolicy BPO:
112
+ * - Returns true if query succeeds (BPO exists, governance enabled)
113
+ * - Returns false if INVALID_TYPE error (BPO does not exist, governance disabled)
114
+ * - Throws for auth/connection/API errors (never for governance state)
115
+ *
116
+ * **Performance**: This is the FIRST GATE - when governance is disabled,
117
+ * consumers can skip policy/tool fetching (75% faster session initialization).
118
+ *
119
+ * **NO HUB ORG AUTO-DETECTION**: This method requires an explicit org alias or
120
+ * Connection object. There is no automatic Hub Org resolution. This design aligns
121
+ * with Connect API architecture where org resolution is managed by the consumer
122
+ * framework, not the library.
123
+ *
124
+ * @param orgAliasOrConnection - Explicit org alias (string) or Connection object.
125
+ * REQUIRED - no auto-detection or Hub Org resolution performed.
126
+ * @returns true if governance enabled, false if disabled (BPO does not exist)
127
+ *
128
+ * @throws {@link CAPGAuthError} - Authentication failure (isRetryable = false)
129
+ * @throws {@link CAPGNetworkError} - Network/connection error (isRetryable = true)
130
+ * @throws {@link CAPGAPIError} - API error (isRetryable depends on status code)
131
+ * @throws {@link CAPGValidationError} - Invalid org alias or org not found
132
+ *
133
+ * @since v1.0.0
134
+ */
135
+ async checkGovernanceEnabled(orgAliasOrConnection) {
136
+ // Step 1: Resolve to Connection
137
+ const connection = await this.resolveConnection(orgAliasOrConnection);
138
+ const username = connection.getUsername() ?? 'unknown';
139
+ this.logger?.debug(`Checking governance via SOQL on org ${username}`);
140
+ try {
141
+ // Query for DevopsGovernancePolicy BPO existence
142
+ const result = await connection.query(SOQL_QUERIES.CHECK_GOVERNANCE_ENABLED);
143
+ // BPO exists - governance is enabled (even if no records, the object is present)
144
+ this.logger?.debug(`BPO exists (totalSize=${result?.totalSize ?? 0}), governance enabled`);
145
+ return true;
146
+ }
147
+ catch (error) {
148
+ // INVALID_TYPE means BPO does not exist - governance is disabled (not an error)
149
+ if (isInvalidTypeError(error)) {
150
+ this.logger?.debug('BPO does not exist (INVALID_TYPE), governance disabled');
151
+ return false;
152
+ }
153
+ // Real error - classify and throw
154
+ // Note: classifyError automatically redacts credentials from username
155
+ const classified = classifyError(error, {
156
+ username,
157
+ operation: 'checking governance preferences',
158
+ });
159
+ throw classified;
160
+ }
161
+ }
162
+ /**
163
+ * Resolve org alias or Connection to a Connection object
164
+ *
165
+ * @param orgAliasOrConnection - Org alias (string) or Connection object
166
+ * @returns Connection object
167
+ * @throws CAPGValidationError if alias is invalid or org not found
168
+ *
169
+ * @internal
170
+ */
171
+ async resolveConnection(orgAliasOrConnection) {
172
+ // If already a Connection, return it
173
+ if (typeof orgAliasOrConnection === 'object' && orgAliasOrConnection !== null) {
174
+ // Duck-type check - if it has query and getUsername, treat as Connection
175
+ if ('query' in orgAliasOrConnection && 'getUsername' in orgAliasOrConnection) {
176
+ return orgAliasOrConnection;
177
+ }
178
+ }
179
+ // Validate alias
180
+ const alias = orgAliasOrConnection.trim();
181
+ if (!alias) {
182
+ throw new CAPGValidationError('orgAlias', 'Org alias cannot be empty');
183
+ }
184
+ try {
185
+ const org = await Org.create({ aliasOrUsername: alias });
186
+ return org.getConnection();
187
+ }
188
+ catch {
189
+ throw new CAPGValidationError('orgAlias', `Invalid org alias '${alias}'. Ensure org is authenticated.`, alias);
190
+ }
191
+ }
192
+ }
193
+ //# sourceMappingURL=org-pref-checker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org-pref-checker.js","sourceRoot":"","sources":["../src/org-pref-checker.ts"],"names":[],"mappings":"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"}