-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathservices.ts
More file actions
408 lines (365 loc) · 12.1 KB
/
services.ts
File metadata and controls
408 lines (365 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Services module providing dependency injection for MCP tools.
*
* The {@link Services} class is the central dependency container for tools,
* providing:
* - Pre-resolved B2CInstance for WebDAV/OCAPI operations
* - Pre-resolved MRT authentication for Managed Runtime operations
* - MRT project/environment configuration
* - File system utilities for local operations
*
* ## Creating Services
*
* Use {@link Services.fromResolvedConfig} with an already-resolved configuration:
*
* ```typescript
* // In a command that extends BaseCommand
* const services = Services.fromResolvedConfig(this.resolvedConfig);
* ```
*
* ## Resolution Pattern
*
* Both B2CInstance and MRT auth are resolved once at server startup (not on each tool call).
* This provides fail-fast behavior and consistent performance.
*
* **B2C Instance** (for WebDAV/OCAPI tools):
* - Flags (highest priority) merged with dw.json (auto-discovered or via --config)
*
* **MRT Auth** (for Managed Runtime tools):
* 1. `--api-key` flag (oclif also checks `SFCC_MRT_API_KEY` env var)
* 2. `~/.mobify` config file (or `~/.mobify--[hostname]` if `--cloud-origin` is set)
*
* **MRT Origin** (for Managed Runtime API URL):
* 1. `--cloud-origin` flag (oclif also checks `SFCC_MRT_CLOUD_ORIGIN` env var)
* 2. `mrtOrigin` field in dw.json
* 3. Default: `https://cloud.mobify.com`
*
* @module services
*/
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import type {B2CInstance} from '@salesforce/b2c-tooling-sdk';
import type {AuthStrategy} from '@salesforce/b2c-tooling-sdk/auth';
import type {ResolvedB2CConfig} from '@salesforce/b2c-tooling-sdk/config';
import {
createCustomApisClient,
createScapiSchemasClient,
toOrganizationId,
WebDavClient,
type CustomApisClient,
type ScapiSchemasClient,
} from '@salesforce/b2c-tooling-sdk/clients';
/**
* MRT (Managed Runtime) configuration.
* Groups auth, project, environment, and origin settings.
*/
export interface MrtConfig {
/** Pre-resolved auth strategy for MRT API operations */
auth?: AuthStrategy;
/** MRT project slug from --project flag or SFCC_MRT_PROJECT env var */
project?: string;
/** MRT environment from --environment flag or SFCC_MRT_ENVIRONMENT env var */
environment?: string;
/** MRT API origin URL from --cloud-origin flag, SFCC_MRT_CLOUD_ORIGIN env var, or mrtOrigin in dw.json */
origin?: string;
}
/**
* Options for Services constructor (internal).
*/
export interface ServicesOptions {
/** Pre-resolved B2C instance (if configured) */
b2cInstance?: B2CInstance;
/** Pre-resolved MRT configuration (auth, project, environment) */
mrtConfig?: MrtConfig;
/** Resolved configuration for access to SCAPI settings */
resolvedConfig: ResolvedB2CConfig;
}
/**
* Services class that provides utilities for MCP tools.
*
* Use the static `Services.fromResolvedConfig()` factory method to create
* an instance from an already-resolved configuration.
*
* @example
* ```typescript
* // In a command that extends BaseCommand
* const services = Services.fromResolvedConfig(this.resolvedConfig);
*
* // Access resolved config
* services.b2cInstance; // B2CInstance | undefined
* services.mrtConfig.auth; // AuthStrategy | undefined
* services.mrtConfig.project; // string | undefined
* ```
*/
export class Services {
/**
* Pre-resolved B2C instance for WebDAV/OCAPI operations.
* Resolved once at server startup from InstanceCommand flags and dw.json.
* Undefined if no B2C instance configuration was available.
*/
public readonly b2cInstance?: B2CInstance;
/**
* Pre-resolved MRT configuration (auth, project, environment, origin).
* Resolved once at server startup from MrtCommand flags and ~/.mobify.
*/
public readonly mrtConfig: MrtConfig;
/**
* Resolved configuration for accessing SCAPI settings.
* Provides access to shortCode, tenantId, and OAuth credentials.
* @private
*/
private readonly resolvedConfig: ResolvedB2CConfig;
public constructor(opts: ServicesOptions) {
this.b2cInstance = opts.b2cInstance;
this.mrtConfig = opts.mrtConfig ?? {};
this.resolvedConfig = opts.resolvedConfig;
}
/**
* Creates a Services instance from an already-resolved configuration.
*
* @param config - Already-resolved configuration from BaseCommand.resolvedConfig
* @returns Services instance with resolved config
*
* @example
* ```typescript
* // In a command that extends BaseCommand
* const services = Services.fromResolvedConfig(this.resolvedConfig);
* ```
*/
public static fromResolvedConfig(config: ResolvedB2CConfig): Services {
// Build MRT config using factory methods
const mrtConfig: MrtConfig = {
auth: config.hasMrtConfig() ? config.createMrtAuth() : undefined,
project: config.values.mrtProject,
environment: config.values.mrtEnvironment,
origin: config.values.mrtOrigin,
};
// Build B2C instance using factory method
const b2cInstance = config.hasB2CInstanceConfig() ? config.createB2CInstance() : undefined;
return new Services({
b2cInstance,
mrtConfig,
resolvedConfig: config,
});
}
// ============================================
// Internal OS Resource Access Methods
// These are for internal use by tools, not exposed to AI assistants
// ============================================
/**
* Check if a file or directory exists.
*
* @param targetPath - Path to check
* @returns True if exists, false otherwise
*/
public exists(targetPath: string): boolean {
return fs.existsSync(targetPath);
}
/**
* Get Custom APIs client for managing custom SCAPI endpoints.
* Requires shortCode, tenantId, and OAuth credentials to be configured.
*
* @throws Error if shortCode, tenantId, or OAuth credentials are missing
* @returns Typed Custom APIs client
*/
public getCustomApisClient(): CustomApisClient {
const {shortCode, tenantId} = this.resolvedConfig.values;
if (!shortCode) {
throw new Error(
'SCAPI short code required. Provide --short-code, set SFCC_SHORTCODE, or configure short-code in dw.json.',
);
}
if (!tenantId) {
throw new Error(
'Tenant ID required. Provide --tenant-id, set SFCC_TENANT_ID, or configure tenant-id in dw.json.',
);
}
// This will throw if OAuth credentials are missing
const oauthStrategy = this.getOAuthStrategy();
return createCustomApisClient({shortCode, tenantId}, oauthStrategy);
}
/**
* Get the current working directory.
*/
public getCwd(): string {
return process.cwd();
}
/**
* Get the user's home directory.
*/
public getHomeDir(): string {
return os.homedir();
}
/**
* Get organization ID for SCAPI API calls.
* Ensures the tenant ID has the required f_ecom_ prefix.
*
* @throws Error if tenantId is not configured
* @returns Organization ID with f_ecom_ prefix
*/
public getOrganizationId(): string {
const {tenantId} = this.resolvedConfig.values;
if (!tenantId) {
throw new Error(
'Tenant ID required. Provide --tenant-id, set SFCC_TENANT_ID, or configure tenant-id in dw.json.',
);
}
return toOrganizationId(tenantId);
}
/**
* Get OS platform information.
*/
public getPlatform(): NodeJS.Platform {
return os.platform();
}
/**
* Get SCAPI Schemas client for discovering available SCAPI APIs.
* Requires shortCode, tenantId, and OAuth credentials to be configured.
*
* @throws Error if shortCode, tenantId, or OAuth credentials are missing
* @returns Typed SCAPI Schemas client
*/
public getScapiSchemasClient(): ScapiSchemasClient {
const {shortCode, tenantId} = this.resolvedConfig.values;
if (!shortCode) {
throw new Error(
'SCAPI short code required. Provide --short-code, set SFCC_SHORTCODE, or configure short-code in dw.json.',
);
}
if (!tenantId) {
throw new Error(
'Tenant ID required. Provide --tenant-id, set SFCC_TENANT_ID, or configure tenant-id in dw.json.',
);
}
// This will throw if OAuth credentials are missing
const oauthStrategy = this.getOAuthStrategy();
return createScapiSchemasClient({shortCode, tenantId}, oauthStrategy);
}
/**
* Get SCAPI shortCode from configuration.
* Returns undefined if not configured.
*
* @returns shortCode or undefined
*/
public getShortCode(): string | undefined {
return this.resolvedConfig.values.shortCode;
}
/**
* Get tenant ID from configuration.
* Returns undefined if not configured.
*
* @returns tenantId or undefined
*/
public getTenantId(): string | undefined {
return this.resolvedConfig.values.tenantId;
}
/**
* Get system temporary directory.
*/
public getTmpDir(): string {
return os.tmpdir();
}
/**
* Get WebDAV client for file operations on B2C instances.
* Requires hostname and WebDAV credentials to be configured.
*
* @throws Error if hostname or B2C instance is missing
* @returns WebDAV client instance
*/
public getWebDavClient(): WebDavClient {
if (!this.b2cInstance) {
throw new Error('B2C instance required for WebDAV operations. Configure hostname and authentication in dw.json.');
}
return this.b2cInstance.webdav;
}
/**
* Join path segments.
*
* @param segments - Path segments to join
* @returns Joined path
*/
public joinPath(...segments: string[]): string {
return path.join(...segments);
}
/**
* List directory contents.
*
* @param dirPath - Directory path to list
* @returns Array of directory entries
*/
public listDirectory(dirPath: string): fs.Dirent[] {
return fs.readdirSync(dirPath, {withFileTypes: true});
}
// ============================================
// SCAPI Helper Methods
// ============================================
/**
* Read a file from the filesystem.
*
* @param filePath - Path to the file
* @param encoding - File encoding (default: utf8)
* @returns File contents as a string
*/
public readFile(filePath: string, encoding: 'ascii' | 'base64' | 'hex' | 'latin1' | 'utf8' = 'utf8'): string {
return fs.readFileSync(filePath, {encoding});
}
/**
* Resolve a path relative to the current working directory.
*
* @param segments - Path segments to join and resolve
* @returns Absolute path
*/
public resolvePath(...segments: string[]): string {
return path.resolve(...segments);
}
/**
* Resolve a path relative to the project directory.
* If path is not supplied, returns the project directory.
* If path is absolute, returns it as-is.
* If path is relative, resolves it relative to the project directory.
*
* @param pathArg - Optional path to resolve
* @returns Resolved absolute path
*/
public resolveWithProjectDirectory(pathArg?: string): string {
const projectDir = this.resolvedConfig.values.projectDirectory ?? process.cwd();
if (!pathArg) {
return projectDir;
}
if (path.isAbsolute(pathArg)) {
return pathArg;
}
return path.resolve(projectDir, pathArg);
}
/**
* Get file or directory stats.
*
* @param targetPath - Path to get stats for
* @returns File stats object
*/
public stat(targetPath: string): fs.Stats {
return fs.statSync(targetPath);
}
/**
* Get OAuth strategy from resolved configuration.
* Mirrors the pattern from OAuthCommand.getOAuthStrategy().
*
* @throws Error if OAuth credentials are not configured
* @returns OAuth auth strategy
* @private
*/
private getOAuthStrategy(): AuthStrategy {
if (!this.resolvedConfig.hasOAuthConfig()) {
throw new Error('OAuth client ID required. Provide --client-id, set SFCC_CLIENT_ID, or configure in dw.json.');
}
// Use resolvedConfig factory to create OAuth strategy
// This handles client-credentials vs implicit flow automatically
return this.resolvedConfig.createOAuth();
}
}