@@ -10,19 +10,27 @@ npm install @salesforce/b2c-tooling-sdk
1010
1111## Quick Start
1212
13- ### From Environment Configuration (Recommended)
13+ ### From Configuration (Recommended)
1414
15- The easiest way to create an instance is from environment configuration files :
15+ Use ` resolveConfig() ` to load configuration from project files (dw.json) and create a B2C instance :
1616
1717``` typescript
18- import { B2CInstance } from ' @salesforce/b2c-tooling-sdk' ;
18+ import { resolveConfig } from ' @salesforce/b2c-tooling-sdk/config ' ;
1919
20- // Load configuration from environment files (dw.json, etc.) , override secrets from environment
21- const instance = B2CInstance . fromEnvironment ({
20+ // Load configuration, override secrets from environment
21+ const config = resolveConfig ({
2222 clientId: process .env .SFCC_CLIENT_ID ,
2323 clientSecret: process .env .SFCC_CLIENT_SECRET ,
2424});
2525
26+ // Validate configuration before use
27+ if (! config .hasB2CInstanceConfig ()) {
28+ throw new Error (' Missing B2C instance configuration' );
29+ }
30+
31+ // Create instance from validated config
32+ const instance = config .createB2CInstance ();
33+
2634// Use typed WebDAV client
2735await instance .webdav .mkcol (' Cartridges/v1' );
2836await instance .webdav .put (' Cartridges/v1/app.zip' , zipBuffer );
@@ -31,11 +39,16 @@ await instance.webdav.put('Cartridges/v1/app.zip', zipBuffer);
3139const { data, error } = await instance .ocapi .GET (' /sites' , {
3240 params: { query: { select: ' (**)' } },
3341});
42+
43+ // Check for configuration warnings
44+ for (const warning of config .warnings ) {
45+ console .warn (warning .message );
46+ }
3447```
3548
3649### Direct Construction
3750
38- You can also construct an instance directly with configuration :
51+ For advanced use cases, you can construct a B2CInstance directly :
3952
4053``` typescript
4154import { B2CInstance } from ' @salesforce/b2c-tooling-sdk' ;
@@ -51,6 +64,70 @@ const instance = new B2CInstance(
5164);
5265```
5366
67+ ## Configuration Resolution
68+
69+ The ` resolveConfig() ` function provides a robust configuration system with multi-source loading and validation.
70+
71+ ### Multi-Source Loading
72+
73+ Configuration is loaded from multiple sources with the following priority (highest to lowest):
74+
75+ 1 . ** Explicit overrides** - Values passed to ` resolveConfig() `
76+ 2 . ** dw.json** - Project configuration file (searched upward from cwd)
77+ 3 . ** ~ /.mobify** - Home directory file for MRT API key
78+
79+ ``` typescript
80+ import { resolveConfig } from ' @salesforce/b2c-tooling-sdk/config' ;
81+
82+ // Override specific values, rest loaded from dw.json
83+ const config = resolveConfig ({
84+ hostname: process .env .SFCC_SERVER , // Override hostname
85+ clientId: process .env .SFCC_CLIENT_ID , // Override from env
86+ clientSecret: process .env .SFCC_CLIENT_SECRET ,
87+ });
88+ ```
89+
90+ ### Validation Helpers
91+
92+ The resolved config provides methods to check what configuration is available:
93+
94+ ``` typescript
95+ const config = resolveConfig ();
96+
97+ // Check for B2C instance configuration
98+ if (config .hasB2CInstanceConfig ()) {
99+ const instance = config .createB2CInstance ();
100+ }
101+
102+ // Check for MRT configuration
103+ if (config .hasMrtConfig ()) {
104+ const mrtClient = config .createMrtClient ({ project: ' my-project' });
105+ }
106+
107+ // Other validation methods
108+ config .hasOAuthConfig (); // OAuth credentials available?
109+ config .hasBasicAuthConfig (); // Basic auth credentials available?
110+ ```
111+
112+ ### Configuration Warnings
113+
114+ The config system detects potential issues and provides warnings:
115+
116+ ``` typescript
117+ const config = resolveConfig ({
118+ hostname: ' staging.demandware.net' , // Different from dw.json
119+ });
120+
121+ // Check warnings
122+ for (const warning of config .warnings ) {
123+ console .warn (` [${warning .code }] ${warning .message } ` );
124+ }
125+ ```
126+
127+ ### Hostname Protection
128+
129+ When you explicitly override the hostname with a value that differs from dw.json, the system protects against credential leakage by ignoring the dw.json credentials. This prevents accidentally using production credentials against a staging server.
130+
54131## Authentication
55132
56133B2CInstance supports multiple authentication methods:
@@ -60,36 +137,28 @@ B2CInstance supports multiple authentication methods:
60137Used for OCAPI and can be used for WebDAV:
61138
62139``` typescript
63- const instance = new B2CInstance (
64- { hostname: ' sandbox.demandware.net' },
65- {
66- oauth: {
67- clientId: ' your-client-id' ,
68- clientSecret: ' your-client-secret' ,
69- scopes: [' SALESFORCE_COMMERCE_API:...:dwsid' ],
70- }
71- }
72- );
140+ const config = resolveConfig ({
141+ clientId: ' your-client-id' ,
142+ clientSecret: ' your-client-secret' ,
143+ scopes: [' SALESFORCE_COMMERCE_API:...:dwsid' ],
144+ });
145+
146+ const instance = config .createB2CInstance ();
73147```
74148
75149### Basic Auth
76150
77151Used for WebDAV operations (Business Manager credentials):
78152
79153``` typescript
80- const instance = new B2CInstance (
81- { hostname: ' sandbox.demandware.net' },
82- {
83- basic: {
84- username: ' admin' ,
85- password: ' your-access-key'
86- },
87- oauth: {
88- clientId: ' your-client-id' ,
89- clientSecret: ' your-client-secret' ,
90- }
91- }
92- );
154+ const config = resolveConfig ({
155+ username: ' admin' ,
156+ password: ' your-access-key' ,
157+ clientId: ' your-client-id' , // Still needed for OCAPI
158+ clientSecret: ' your-client-secret' ,
159+ });
160+
161+ const instance = config .createB2CInstance ();
93162```
94163
95164When both are configured, WebDAV uses Basic auth and OCAPI uses OAuth.
0 commit comments