@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.
- package/LICENSE.txt +21 -0
- package/README.md +122 -0
- package/dist/client.d.ts +86 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +167 -0
- package/dist/client.js.map +1 -0
- package/dist/connect-api-client.d.ts +82 -0
- package/dist/connect-api-client.d.ts.map +1 -0
- package/dist/connect-api-client.js +106 -0
- package/dist/connect-api-client.js.map +1 -0
- package/dist/constants.d.ts +64 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +68 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors/api.d.ts +79 -0
- package/dist/errors/api.d.ts.map +1 -0
- package/dist/errors/api.js +128 -0
- package/dist/errors/api.js.map +1 -0
- package/dist/errors/auth.d.ts +35 -0
- package/dist/errors/auth.d.ts.map +1 -0
- package/dist/errors/auth.js +50 -0
- package/dist/errors/auth.js.map +1 -0
- package/dist/errors/base.d.ts +103 -0
- package/dist/errors/base.d.ts.map +1 -0
- package/dist/errors/base.js +162 -0
- package/dist/errors/base.js.map +1 -0
- package/dist/errors/hub-org.d.ts +108 -0
- package/dist/errors/hub-org.d.ts.map +1 -0
- package/dist/errors/hub-org.js +144 -0
- package/dist/errors/hub-org.js.map +1 -0
- package/dist/errors/index.d.ts +33 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +42 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/network.d.ts +53 -0
- package/dist/errors/network.d.ts.map +1 -0
- package/dist/errors/network.js +77 -0
- package/dist/errors/network.js.map +1 -0
- package/dist/errors/validation.d.ts +69 -0
- package/dist/errors/validation.d.ts.map +1 -0
- package/dist/errors/validation.js +96 -0
- package/dist/errors/validation.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/org-pref-checker.d.ts +129 -0
- package/dist/org-pref-checker.d.ts.map +1 -0
- package/dist/org-pref-checker.js +193 -0
- package/dist/org-pref-checker.js.map +1 -0
- package/dist/policy-fetcher.d.ts +144 -0
- package/dist/policy-fetcher.d.ts.map +1 -0
- package/dist/policy-fetcher.js +241 -0
- package/dist/policy-fetcher.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/index.d.ts +532 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/result.d.ts +282 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +351 -0
- package/dist/types/result.js.map +1 -0
- package/dist/utils/error-classifier.d.ts +55 -0
- package/dist/utils/error-classifier.d.ts.map +1 -0
- package/dist/utils/error-classifier.js +90 -0
- package/dist/utils/error-classifier.js.map +1 -0
- package/package.json +143 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Terms of Use
|
|
2
|
+
|
|
3
|
+
Copyright 2026 Salesforce, Inc. All rights reserved.
|
|
4
|
+
|
|
5
|
+
These Terms of Use govern the download, installation, and/or use of this software provided by Salesforce, Inc. ("Salesforce") (the "Software"), were last updated on March 24, 2026, and constitute a legally binding agreement between you and Salesforce. If you do not agree to these Terms of Use, do not install or use the Software. The Software may link to third-party software components licensed under various open source licenses ("Open Source Components"). These Terms of Use pertain solely to Salesforce's proprietary code in the Software. It does not alter or extend any rights or obligations regarding the Open Source Components. For clarity, your use of the Open Source Components is governed by the terms of the applicable open source license(s). You are solely responsible for complying with the terms and conditions of those open source licenses.
|
|
6
|
+
|
|
7
|
+
Salesforce grants you a worldwide, non-exclusive, no-charge, royalty-free copyright license to reproduce, revocable, publicly display, publicly perform, sublicense, and distribute the Software and derivative works subject to these Terms. These Terms shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
Subject to the limited rights expressly granted hereunder, Salesforce reserves all rights, title, and interest in and to all intellectual property subsisting in the Software. No rights are granted to you hereunder other than as expressly set forth herein. Users residing in countries on the United States Office of Foreign Assets Control sanction list, or which are otherwise subject to a US export embargo, may not use the Software.
|
|
10
|
+
|
|
11
|
+
Implementation of the Software may require development work, for which you are responsible. The Software may contain bugs, errors and incompatibilities and is made available on an AS IS basis without support, updates, or service level commitments.
|
|
12
|
+
|
|
13
|
+
Salesforce reserves the right at any time to modify, suspend, or discontinue, the Software (or any part thereof) with or without notice. You agree that Salesforce shall not be liable to you or to any third party for any modification, suspension, or discontinuance.
|
|
14
|
+
|
|
15
|
+
You agree to defend Salesforce against any claim, demand, suit or proceeding made or brought against Salesforce by a third party arising out of or accruing from (a) your use of the Software, and (b) any application you develop with the Software that infringes any copyright, trademark, trade secret, trade dress, patent, or other intellectual property right of any person or defames any person or violates their rights of publicity or privacy (each a "Claim Against Salesforce"), and will indemnify Salesforce from any damages, attorney fees, and costs finally awarded against Salesforce as a result of, or for any amounts paid by Salesforce under a settlement approved by you in writing of, a Claim Against Salesforce, provided Salesforce (x) promptly gives you written notice of the Claim Against Salesforce, (y) gives you sole control of the defense and settlement of the Claim Against Salesforce (except that you may not settle any Claim Against Salesforce unless it unconditionally releases Salesforce of all liability), and (z) gives you all reasonable assistance, at your expense.
|
|
16
|
+
|
|
17
|
+
WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE SOFTWARE IS NOT SUPPORTED AND IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL SALESFORCE HAVE ANY LIABILITY FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES, OR DAMAGES BASED ON LOST PROFITS, DATA, OR USE, IN CONNECTION WITH THE SOFTWARE, HOWEVER CAUSED AND WHETHER IN CONTRACT, TORT, OR UNDER ANY OTHER THEORY OF LIABILITY, WHETHER OR NOT YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
18
|
+
|
|
19
|
+
These Terms of Use shall be governed exclusively by the internal laws of the State of California, without regard to its conflicts of laws rules. Each party hereby consents to the exclusive jurisdiction of the state and federal courts located in San Francisco County, California to adjudicate any dispute arising out of or relating to these Terms of Use and the download, installation, and/or use of the Software. Except as expressly stated herein, these Terms of Use constitute the entire agreement between the parties, and supersede all prior and contemporaneous agreements, proposals, or representations, written or oral, concerning their subject matter. No modification, amendment, or waiver of any provision of these Terms of Use shall be effective unless it is by an update to these Terms of Use that Salesforce makes available, or is in writing and signed by the party against whom the modification, amendment, or waiver is to be asserted.
|
|
20
|
+
|
|
21
|
+
Data Privacy: Salesforce may collect, process, and store device, system, and other information related to your use of the Software. This information includes, but is not limited to, IP address, user metrics, and other data ("Usage Data"). Salesforce may use Usage Data for analytics, product development, and marketing purposes. You acknowledge that files generated in conjunction with the Software may contain sensitive or confidential data, and you are solely responsible for anonymizing and protecting such data.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# CAPG Client Library
|
|
2
|
+
|
|
3
|
+
TypeScript client library for fetching governance policies from CAPG (Coding Agent Policy Governance) Hub Org.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The CAPG Client Library provides a simple, type-safe interface for Salesforce coding agents (AFV, VaaS, Codey) to fetch governance policies and tool configurations from the CAPG Hub Org.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Simple unified API via `CAPGClient` class
|
|
12
|
+
- Fetch governance policies from Salesforce orgs via Connect API
|
|
13
|
+
- Check if governance is enabled (first gate for performance)
|
|
14
|
+
- Explicit org alias required for all operations (no auto-detection)
|
|
15
|
+
- `Result<T, E>` pattern for type-safe error handling (no thrown exceptions)
|
|
16
|
+
- AFV pattern support (early return optimization)
|
|
17
|
+
- Comprehensive input validation
|
|
18
|
+
- Credential redaction in logs and errors
|
|
19
|
+
- 96%+ test coverage
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @salesforce/capg-client
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
The library provides a simple unified API via `CAPGClient`. All operations use the `Result<T, E>` pattern for type-safe error handling.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { CAPGClient, isOk, isErr } from '@salesforce/capg-client';
|
|
33
|
+
|
|
34
|
+
const client = new CAPGClient();
|
|
35
|
+
|
|
36
|
+
// Step 1: Check if governance is enabled (FIRST GATE - early return optimization)
|
|
37
|
+
const isEnabled = await client.checkGovernanceEnabled('myHubOrg');
|
|
38
|
+
|
|
39
|
+
if (!isEnabled) {
|
|
40
|
+
console.log('Governance disabled, skipping policy fetch');
|
|
41
|
+
return; // Early return - 75% faster when governance is disabled
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Step 2: Fetch policies (only if governance is enabled)
|
|
45
|
+
const result = await client.fetchPolicies({}, 'myHubOrg');
|
|
46
|
+
|
|
47
|
+
if (isOk(result)) {
|
|
48
|
+
const { policies } = result.value;
|
|
49
|
+
console.log(`Fetched ${policies.length} policies`);
|
|
50
|
+
|
|
51
|
+
// Access policy data
|
|
52
|
+
policies.forEach((policy) => {
|
|
53
|
+
console.log(`${policy.policyCode}: ${policy.name}`);
|
|
54
|
+
console.log(policy.ruleContent); // Markdown rule for LLM
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
console.error(`Error: ${result.error.message}`);
|
|
58
|
+
console.error(`Suggested action: ${result.error.suggestedAction}`);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### AFV Pattern (Recommended)
|
|
63
|
+
|
|
64
|
+
Always check governance first to enable early returns:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const client = new CAPGClient();
|
|
68
|
+
|
|
69
|
+
// FIRST GATE: Check governance (never throws)
|
|
70
|
+
const isEnabled = await client.checkGovernanceEnabled(orgAlias);
|
|
71
|
+
if (!isEnabled) return; // Skip policy fetch if disabled
|
|
72
|
+
|
|
73
|
+
// Only fetch if enabled
|
|
74
|
+
const result = await client.fetchPolicies({}, orgAlias);
|
|
75
|
+
if (isOk(result)) {
|
|
76
|
+
// Use policies
|
|
77
|
+
console.log(result.value.policies);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### With Custom Logger
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const client = new CAPGClient({
|
|
85
|
+
logger: {
|
|
86
|
+
debug: (msg) => console.log(`[DEBUG] ${msg}`),
|
|
87
|
+
error: (msg) => console.error(`[ERROR] ${msg}`),
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Documentation
|
|
93
|
+
|
|
94
|
+
- [API Documentation](docs/api/)
|
|
95
|
+
- [Architecture](docs/architecture/)
|
|
96
|
+
- [Testing Guide](docs/testing/MANUAL_TESTS.md)
|
|
97
|
+
- [Security Assessment](docs/architecture/SECURITY_ASSESSMENT.md)
|
|
98
|
+
- [Epic & Work Items](docs/implementation/EPIC_CAPG_Client_Library.md)
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
See [DEVELOPMENT.md](DEVELOPMENT.md) for detailed setup instructions, workflow, and contributing guidelines.
|
|
103
|
+
|
|
104
|
+
### Running Tests
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# Unit tests (automated, with mocks)
|
|
108
|
+
npm test
|
|
109
|
+
|
|
110
|
+
# Coverage report
|
|
111
|
+
npm run test:coverage
|
|
112
|
+
|
|
113
|
+
# Manual integration tests (requires SF CLI auth)
|
|
114
|
+
npm run build
|
|
115
|
+
node manual-tests/test-org-pref-checker.js
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Manual Tests**: Integration tests that validate library functionality against real Salesforce orgs. See [`docs/testing/MANUAL_TESTS.md`](docs/testing/MANUAL_TESTS.md) for comprehensive documentation.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
See LICENSE file.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Result } from './types/result.js';
|
|
2
|
+
import type { FetchPoliciesOptions, PolicyResponse } from './types/index.js';
|
|
3
|
+
import { CAPGError } from './errors/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Logger interface for optional logging integration
|
|
6
|
+
* All sensitive data (access tokens, credentials) is automatically redacted.
|
|
7
|
+
*/
|
|
8
|
+
export type Logger = {
|
|
9
|
+
debug: (msg: string) => void;
|
|
10
|
+
error: (msg: string) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Options for constructing CAPGClient
|
|
14
|
+
*/
|
|
15
|
+
export type CAPGClientOptions = {
|
|
16
|
+
/** Optional logger for debug and error messages */
|
|
17
|
+
logger?: Logger;
|
|
18
|
+
/** Optional timeout for HTTP requests in milliseconds (default: 30000) */
|
|
19
|
+
timeout?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* CAPGClient - Main client for fetching CAPG governance data
|
|
23
|
+
*
|
|
24
|
+
* Provides two primary operations:
|
|
25
|
+
* 1. checkGovernanceEnabled() - First gate check (returns boolean, never throws)
|
|
26
|
+
* 2. fetchPolicies() - Fetch policies via Connect API (returns Result, never throws)
|
|
27
|
+
*
|
|
28
|
+
* @example Recommended usage pattern
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const client = new CAPGClient();
|
|
31
|
+
* const isEnabled = await client.checkGovernanceEnabled('myHubOrg');
|
|
32
|
+
* if (!isEnabled) return; // Early return if disabled
|
|
33
|
+
*
|
|
34
|
+
* const result = await client.fetchPolicies({}, 'myHubOrg');
|
|
35
|
+
* if (isOk(result)) console.log(result.value.policies);
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @since v0.2.0
|
|
39
|
+
*/
|
|
40
|
+
export declare class CAPGClient {
|
|
41
|
+
private logger?;
|
|
42
|
+
/**
|
|
43
|
+
* Create a new CAPGClient instance
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration options (logger, timeout)
|
|
46
|
+
*/
|
|
47
|
+
constructor(options?: CAPGClientOptions);
|
|
48
|
+
/**
|
|
49
|
+
* Check if CAPG governance is enabled for the specified org
|
|
50
|
+
*
|
|
51
|
+
* **FIRST GATE**: Call this BEFORE fetchPolicies() for 75% faster early return.
|
|
52
|
+
* **NEVER THROWS**: Catches ALL errors and returns false (logs errors if logger provided).
|
|
53
|
+
*
|
|
54
|
+
* @param orgAlias - Org alias or username (e.g., 'myHubOrg', 'devhub@example.com')
|
|
55
|
+
* @returns true if governance enabled, false if disabled or error
|
|
56
|
+
*
|
|
57
|
+
* @since v0.2.0
|
|
58
|
+
*/
|
|
59
|
+
checkGovernanceEnabled(orgAlias: string): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* Fetch governance policies from Connect API
|
|
62
|
+
*
|
|
63
|
+
* Makes HTTP POST to /services/data/v62.0/connect/governance/policies via ConnectAPIClient.
|
|
64
|
+
* Defaults includeDisabled to false (only ENABLED policies). Never throws - returns Result.
|
|
65
|
+
*
|
|
66
|
+
* @param options - Fetch options (includeDisabled, defaults to false)
|
|
67
|
+
* @param orgAlias - Org alias or username (e.g., 'myHubOrg', 'devhub@example.com')
|
|
68
|
+
* @returns Result containing PolicyResponse on success, or CAPGError on failure
|
|
69
|
+
*
|
|
70
|
+
* @since v0.2.0
|
|
71
|
+
*/
|
|
72
|
+
fetchPolicies(options: FetchPoliciesOptions, orgAlias: string): Promise<Result<PolicyResponse, CAPGError>>;
|
|
73
|
+
/**
|
|
74
|
+
* Create a Connection object from an org alias
|
|
75
|
+
*
|
|
76
|
+
* Validates the alias, creates an Org object using Org.create(), and returns
|
|
77
|
+
* the Connection. Returns Result type for consistent error handling.
|
|
78
|
+
*
|
|
79
|
+
* @param orgAlias - Org alias or username
|
|
80
|
+
* @returns Result with Connection or CAPGValidationError
|
|
81
|
+
*
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
private createConnection;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,MAAM,EAAW,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAuB,MAAM,mBAAmB,CAAC;AAInE;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,mDAAmD;IACnD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAC,CAAS;IAExB;;;;OAIG;gBACgB,OAAO,CAAC,EAAE,iBAAiB;IAM9C;;;;;;;;;;OAUG;IACU,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgCvE;;;;;;;;;;;OAWG;IACU,aAAa,CACxB,OAAO,EAAE,oBAAoB,EAC7B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IA4C7C;;;;;;;;;;OAUG;YAEW,gBAAgB;CAgB/B"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, Salesforce, Inc. All rights reserved.
|
|
3
|
+
* See LICENSE.txt for license terms.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* CAPGClient - Main client class for CAPG (Coding Agent Policy Governance)
|
|
7
|
+
*
|
|
8
|
+
* This is the primary entry point for fetching governance policies from Salesforce orgs.
|
|
9
|
+
* Provides a simple, type-safe API that combines org preference checking and policy fetching.
|
|
10
|
+
*
|
|
11
|
+
* Epic: EPIC_CAPG_Client_Library_Refactor_Named_Credential, Work Item: WI-6
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
* @since v0.2.0
|
|
15
|
+
*/
|
|
16
|
+
import { Org } from '@salesforce/core';
|
|
17
|
+
import { ok, err } from './types/result.js';
|
|
18
|
+
import { CAPGValidationError } from './errors/index.js';
|
|
19
|
+
import { ConnectAPIClient } from './connect-api-client.js';
|
|
20
|
+
import { OrgPrefChecker } from './org-pref-checker.js';
|
|
21
|
+
/**
|
|
22
|
+
* CAPGClient - Main client for fetching CAPG governance data
|
|
23
|
+
*
|
|
24
|
+
* Provides two primary operations:
|
|
25
|
+
* 1. checkGovernanceEnabled() - First gate check (returns boolean, never throws)
|
|
26
|
+
* 2. fetchPolicies() - Fetch policies via Connect API (returns Result, never throws)
|
|
27
|
+
*
|
|
28
|
+
* @example Recommended usage pattern
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const client = new CAPGClient();
|
|
31
|
+
* const isEnabled = await client.checkGovernanceEnabled('myHubOrg');
|
|
32
|
+
* if (!isEnabled) return; // Early return if disabled
|
|
33
|
+
*
|
|
34
|
+
* const result = await client.fetchPolicies({}, 'myHubOrg');
|
|
35
|
+
* if (isOk(result)) console.log(result.value.policies);
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @since v0.2.0
|
|
39
|
+
*/
|
|
40
|
+
export class CAPGClient {
|
|
41
|
+
logger;
|
|
42
|
+
/**
|
|
43
|
+
* Create a new CAPGClient instance
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration options (logger, timeout)
|
|
46
|
+
*/
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.logger = options?.logger;
|
|
49
|
+
// Note: timeout option is reserved for future use with ConnectAPIClient
|
|
50
|
+
// Currently not used but kept in the API for forward compatibility
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if CAPG governance is enabled for the specified org
|
|
54
|
+
*
|
|
55
|
+
* **FIRST GATE**: Call this BEFORE fetchPolicies() for 75% faster early return.
|
|
56
|
+
* **NEVER THROWS**: Catches ALL errors and returns false (logs errors if logger provided).
|
|
57
|
+
*
|
|
58
|
+
* @param orgAlias - Org alias or username (e.g., 'myHubOrg', 'devhub@example.com')
|
|
59
|
+
* @returns true if governance enabled, false if disabled or error
|
|
60
|
+
*
|
|
61
|
+
* @since v0.2.0
|
|
62
|
+
*/
|
|
63
|
+
async checkGovernanceEnabled(orgAlias) {
|
|
64
|
+
try {
|
|
65
|
+
// Validate orgAlias
|
|
66
|
+
const trimmedAlias = orgAlias.trim();
|
|
67
|
+
if (!trimmedAlias) {
|
|
68
|
+
// Empty alias - return false, don't call Org.create
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
this.logger?.debug(`Checking governance enabled for org: ${trimmedAlias}`);
|
|
72
|
+
// Create connection
|
|
73
|
+
const connectionResult = await this.createConnection(trimmedAlias);
|
|
74
|
+
if (!connectionResult.ok) {
|
|
75
|
+
// Connection creation failed - return false
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const connection = connectionResult.value;
|
|
79
|
+
// Use OrgPrefChecker to check governance
|
|
80
|
+
const checker = new OrgPrefChecker({ logger: this.logger });
|
|
81
|
+
const isEnabled = await checker.checkGovernanceEnabled(connection);
|
|
82
|
+
return isEnabled;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Catch ALL errors - never throw
|
|
86
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
87
|
+
this.logger?.error(`Failed to check governance enabled: ${errorMessage}`);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Fetch governance policies from Connect API
|
|
93
|
+
*
|
|
94
|
+
* Makes HTTP POST to /services/data/v62.0/connect/governance/policies via ConnectAPIClient.
|
|
95
|
+
* Defaults includeDisabled to false (only ENABLED policies). Never throws - returns Result.
|
|
96
|
+
*
|
|
97
|
+
* @param options - Fetch options (includeDisabled, defaults to false)
|
|
98
|
+
* @param orgAlias - Org alias or username (e.g., 'myHubOrg', 'devhub@example.com')
|
|
99
|
+
* @returns Result containing PolicyResponse on success, or CAPGError on failure
|
|
100
|
+
*
|
|
101
|
+
* @since v0.2.0
|
|
102
|
+
*/
|
|
103
|
+
async fetchPolicies(options, orgAlias) {
|
|
104
|
+
try {
|
|
105
|
+
// Validate orgAlias
|
|
106
|
+
const trimmedAlias = orgAlias.trim();
|
|
107
|
+
if (!trimmedAlias) {
|
|
108
|
+
const validationError = new CAPGValidationError('orgAlias', 'orgAlias cannot be empty');
|
|
109
|
+
this.logger?.error(`Failed to fetch policies: ${validationError.message}`);
|
|
110
|
+
return err(validationError);
|
|
111
|
+
}
|
|
112
|
+
this.logger?.debug(`Fetching policies for org: ${trimmedAlias}`);
|
|
113
|
+
// Create connection
|
|
114
|
+
const connectionResult = await this.createConnection(trimmedAlias);
|
|
115
|
+
if (!connectionResult.ok) {
|
|
116
|
+
this.logger?.error(`Failed to fetch policies: ${connectionResult.error.message}`);
|
|
117
|
+
return err(connectionResult.error);
|
|
118
|
+
}
|
|
119
|
+
const connection = connectionResult.value;
|
|
120
|
+
// Create ConnectAPIClient and fetch policies
|
|
121
|
+
const client = new ConnectAPIClient(connection, { logger: this.logger });
|
|
122
|
+
const fetchOptions = {
|
|
123
|
+
includeDisabled: options.includeDisabled ?? false,
|
|
124
|
+
};
|
|
125
|
+
const result = await client.fetchPolicies(fetchOptions);
|
|
126
|
+
// Log result
|
|
127
|
+
if (result.ok) {
|
|
128
|
+
this.logger?.debug(`Successfully fetched ${result.value.policies.length} policies`);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
this.logger?.error(`Failed to fetch policies: ${result.error.message}`);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
// Catch unexpected errors and convert to CAPGValidationError
|
|
137
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
138
|
+
const validationError = new CAPGValidationError('orgAlias', `Unexpected error: ${errorMessage}`);
|
|
139
|
+
this.logger?.error(`Failed to fetch policies: ${validationError.message}`);
|
|
140
|
+
return err(validationError);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create a Connection object from an org alias
|
|
145
|
+
*
|
|
146
|
+
* Validates the alias, creates an Org object using Org.create(), and returns
|
|
147
|
+
* the Connection. Returns Result type for consistent error handling.
|
|
148
|
+
*
|
|
149
|
+
* @param orgAlias - Org alias or username
|
|
150
|
+
* @returns Result with Connection or CAPGValidationError
|
|
151
|
+
*
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
// eslint-disable-next-line class-methods-use-this
|
|
155
|
+
async createConnection(orgAlias) {
|
|
156
|
+
try {
|
|
157
|
+
const org = await Org.create({ aliasOrUsername: orgAlias });
|
|
158
|
+
const connection = org.getConnection();
|
|
159
|
+
return ok(connection);
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
163
|
+
return err(new CAPGValidationError('orgAlias', `Invalid org alias '${orgAlias}'. Ensure org is authenticated. ${errorMessage}`, orgAlias));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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eAAe,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,GAAG,CAAC,aAAa,EAAE,CAAC;YACvC,OAAO,EAAE,CAAC,UAAU,CAAC,CAAC;QACxB,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACxB,MAAM,YAAY,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC5E,OAAO,GAAG,CACR,IAAI,mBAAmB,CACrB,UAAU,EACV,sBAAsB,QAAQ,mCAAmC,YAAY,EAAE,EAC/E,QAAQ,CACT,CACF,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConnectAPIClient - Connect API Client for fetching CAPG policies
|
|
3
|
+
*
|
|
4
|
+
* REST API client for fetching DevOps Governance policies from Salesforce Connect API endpoints.
|
|
5
|
+
* Handles authentication, error classification, and provides cache metadata for consumers.
|
|
6
|
+
*
|
|
7
|
+
* Epic: EPIC_CAPG_Client_Library_Refactor_Named_Credential, Work Item: WI-4
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
* @since v0.2.0
|
|
11
|
+
*/
|
|
12
|
+
import { Connection } from '@salesforce/core';
|
|
13
|
+
import { Result } from './types/result.js';
|
|
14
|
+
import { FetchPoliciesOptions, PolicyResponse } from './types/index.js';
|
|
15
|
+
import { CAPGError } from './errors/index.js';
|
|
16
|
+
/**
|
|
17
|
+
* Logger interface for optional logging integration
|
|
18
|
+
* All sensitive data (access tokens, credentials) is automatically redacted.
|
|
19
|
+
*/
|
|
20
|
+
export type Logger = {
|
|
21
|
+
debug: (msg: string) => void;
|
|
22
|
+
error: (msg: string) => void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Options for constructing ConnectAPIClient
|
|
26
|
+
*/
|
|
27
|
+
export type ConnectAPIClientOptions = {
|
|
28
|
+
/** Optional logger for debug and error messages */
|
|
29
|
+
logger?: Logger;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* ConnectAPIClient - Client for fetching policies via Connect API
|
|
33
|
+
*
|
|
34
|
+
* @example Basic usage
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const connection = await Connection.create({ authInfo });
|
|
37
|
+
* const client = new ConnectAPIClient(connection);
|
|
38
|
+
* const result = await client.fetchPolicies({ includeDisabled: false });
|
|
39
|
+
* if (isOk(result)) {
|
|
40
|
+
* console.log(`Fetched ${result.value.policies.length} policies`);
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare class ConnectAPIClient {
|
|
45
|
+
private connection;
|
|
46
|
+
private logger?;
|
|
47
|
+
constructor(connection: Connection, options?: ConnectAPIClientOptions);
|
|
48
|
+
/**
|
|
49
|
+
* Fetch DevOps Governance policies from Connect API
|
|
50
|
+
*
|
|
51
|
+
* Makes POST request to: {instanceUrl}/services/data/v62.0/connect/governance/policies
|
|
52
|
+
*
|
|
53
|
+
* @param options - Fetch options (includeDisabled)
|
|
54
|
+
* @returns Result containing PolicyResponse on success, or CAPGError on failure
|
|
55
|
+
*
|
|
56
|
+
* @throws Never throws - all errors returned as Result.err()
|
|
57
|
+
*
|
|
58
|
+
* **Error Types**:
|
|
59
|
+
* - `CAPGAuthError` (401/403) - Authentication/authorization failed, re-login required (non-retryable)
|
|
60
|
+
* - `CAPGNetworkError` (ETIMEDOUT/ECONNREFUSED) - Network issue (retryable)
|
|
61
|
+
* - `CAPGAPIError` (4xx/5xx) - API error with status code
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const result = await client.fetchPolicies({ includeDisabled: false });
|
|
66
|
+
* if (isOk(result)) {
|
|
67
|
+
* console.log(`Fetched ${result.value.policies.length} policies`);
|
|
68
|
+
* } else if (result.error instanceof CAPGAuthError) {
|
|
69
|
+
* console.error('Re-authentication required');
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
fetchPolicies(options: FetchPoliciesOptions): Promise<Result<PolicyResponse, CAPGError>>;
|
|
74
|
+
/**
|
|
75
|
+
* Generate stable cache key for consumer caching
|
|
76
|
+
* Format: connect_policies_{username}_{includeDisabled}
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
private generateCacheKey;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=connect-api-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-api-client.d.ts","sourceRoot":"","sources":["../src/connect-api-client.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAW,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAG9C;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,mDAAmD;IACnD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,MAAM,CAAC,CAAS;gBAEL,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,uBAAuB;IAK5E;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IAgDrG;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;CAMzB"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, Salesforce, Inc. All rights reserved.
|
|
3
|
+
* See LICENSE.txt for license terms.
|
|
4
|
+
*/
|
|
5
|
+
import { ok, err } from './types/result.js';
|
|
6
|
+
import { classifyError } from './utils/error-classifier.js';
|
|
7
|
+
/**
|
|
8
|
+
* ConnectAPIClient - Client for fetching policies via Connect API
|
|
9
|
+
*
|
|
10
|
+
* @example Basic usage
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const connection = await Connection.create({ authInfo });
|
|
13
|
+
* const client = new ConnectAPIClient(connection);
|
|
14
|
+
* const result = await client.fetchPolicies({ includeDisabled: false });
|
|
15
|
+
* if (isOk(result)) {
|
|
16
|
+
* console.log(`Fetched ${result.value.policies.length} policies`);
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class ConnectAPIClient {
|
|
21
|
+
connection;
|
|
22
|
+
logger;
|
|
23
|
+
constructor(connection, options) {
|
|
24
|
+
this.connection = connection;
|
|
25
|
+
this.logger = options?.logger;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Fetch DevOps Governance policies from Connect API
|
|
29
|
+
*
|
|
30
|
+
* Makes POST request to: {instanceUrl}/services/data/v62.0/connect/governance/policies
|
|
31
|
+
*
|
|
32
|
+
* @param options - Fetch options (includeDisabled)
|
|
33
|
+
* @returns Result containing PolicyResponse on success, or CAPGError on failure
|
|
34
|
+
*
|
|
35
|
+
* @throws Never throws - all errors returned as Result.err()
|
|
36
|
+
*
|
|
37
|
+
* **Error Types**:
|
|
38
|
+
* - `CAPGAuthError` (401/403) - Authentication/authorization failed, re-login required (non-retryable)
|
|
39
|
+
* - `CAPGNetworkError` (ETIMEDOUT/ECONNREFUSED) - Network issue (retryable)
|
|
40
|
+
* - `CAPGAPIError` (4xx/5xx) - API error with status code
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const result = await client.fetchPolicies({ includeDisabled: false });
|
|
45
|
+
* if (isOk(result)) {
|
|
46
|
+
* console.log(`Fetched ${result.value.policies.length} policies`);
|
|
47
|
+
* } else if (result.error instanceof CAPGAuthError) {
|
|
48
|
+
* console.error('Re-authentication required');
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
async fetchPolicies(options) {
|
|
53
|
+
try {
|
|
54
|
+
const username = this.connection.getUsername();
|
|
55
|
+
const usernameStr = username ?? 'unknown';
|
|
56
|
+
const includeDisabled = options.includeDisabled ?? false;
|
|
57
|
+
const endpoint = `/services/data/v67.0/connect/governance/policies?includeDisabled=${includeDisabled}`;
|
|
58
|
+
this.logger?.debug(`Fetching policies from Connect API: ${endpoint}`);
|
|
59
|
+
// Make REAL HTTP call to Connect API using Salesforce Connection
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
61
|
+
const response = await this.connection.request({
|
|
62
|
+
method: 'GET',
|
|
63
|
+
url: endpoint,
|
|
64
|
+
});
|
|
65
|
+
// Extract policies from response (empty array if not present)
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
67
|
+
const policies = Array.isArray(response.policies) ? response.policies : [];
|
|
68
|
+
const policyResponse = {
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
70
|
+
policies,
|
|
71
|
+
fetchedAt: new Date(),
|
|
72
|
+
source: 'rest',
|
|
73
|
+
cacheKey: this.generateCacheKey(options),
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
75
|
+
totalCount: policies.length,
|
|
76
|
+
expiresAt: undefined, // Connection.request() doesn't expose headers
|
|
77
|
+
etag: undefined,
|
|
78
|
+
};
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
80
|
+
this.logger?.debug(`Successfully fetched ${policies.length} policies from Connect API for org: ${usernameStr}`);
|
|
81
|
+
return ok(policyResponse);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// Classify and return error
|
|
85
|
+
const capgError = classifyError(error, {
|
|
86
|
+
username: this.connection.getUsername(),
|
|
87
|
+
operation: 'fetching policies via Connect API',
|
|
88
|
+
});
|
|
89
|
+
this.logger?.error(`Failed to fetch policies: ${capgError.message}`);
|
|
90
|
+
return err(capgError);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Generate stable cache key for consumer caching
|
|
95
|
+
* Format: connect_policies_{username}_{includeDisabled}
|
|
96
|
+
*
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
generateCacheKey(options) {
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
101
|
+
const username = this.connection.getUsername() ?? 'unknown';
|
|
102
|
+
const includeDisabled = options.includeDisabled ?? false;
|
|
103
|
+
return `connect_policies_${username}_${includeDisabled}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=connect-api-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-api-client.js","sourceRoot":"","sources":["../src/connect-api-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,EAAU,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAGpD,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAmB5D;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,gBAAgB;IACnB,UAAU,CAAa;IACvB,MAAM,CAAU;IAExB,YAAmB,UAAsB,EAAE,OAAiC;QAC1E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