Customize SitecoreClient
The SitecoreClient class implements the BaseSitecoreClient interface, which provides various configuration options to customize its behavior and control how the client interacts with SitecoreAI's GraphQL service.
Customize fetch options
All client methods accept an optional FetchOptions parameter that lets you customize HTTP request behavior. The FetchOptions type defines a set of optional configuration settings you can provide when making GraphQL requests in your application:
-
debugger- a custom debugger instance (from thedebuglibrary) for logging request and response details. By default, the system uses a debugger with the namespace'core:http', but you can override this for more granular or custom logging. -
fetch- an override for the default Node.js fetch implementation. You can provide your own fetch function here, which is useful for testing, mocking, or using a custom HTTP client. -
headers- an object containing custom HTTP headers to be sent with each request. This is useful for adding authentication tokens, custom content types, or any other headers required by your API. -
retries- a number specifying how many times the GraphQL client should retry a request if it encounters an error. This allows you to make your requests more resilient to transient failures. -
retryStrategy- a type implementing theRetryStrategyinterface. This lets you customize the logic for when and how retries should occur, such as exponential backoff or retrying only for certain error types.
Example usage
// Custom fetch options with retries and headers
const fetchOptions = {
retries: 3,
headers: {
'X-Custom-Header': 'value'
}
};
// Using with getPage
const page = await client.getPage(
'/about',
{ site: 'mysite', language: 'en' },
fetchOptions
);
// Using with getDictionary
const dictionary = await client.getDictionary(
{ site: 'mysite', language: 'en' },
fetchOptions
);
Override methods in the SitecoreClient class
The architecture of SitecoreClient is designed for extensibility, letting you extend or override its behavior in two primary ways:
-
Supplying custom service implementations via the constructor
-
Inheriting the class and overriding protected or private factory methods
Supplying custom service implementations via the constructor
You can use the custom property of the SitecoreClient’s configuration object to pass in custom implementations of services to override the ones returned by the default factory methods.
For example:
const client = new SitecoreClient({
sites,
...scConfig,
custom: {
layoutService: new MyCustomLayoutService({ /* custom config */ }),
}
});
Inheriting the class and overriding protected or private factory methods
The SitecoreClient class provides several protected (and some private) factory methods for creating its internal service dependencies. These methods serve as strategic extension or override points, allowing developers to customize service implementations without modifying the core class.
The following example demonstrates how to effectively use this pattern:
-
Extend the base class by creating a
CustomSitecoreClientclass that inherits fromSitecoreClient.RequestResponseclass CustomSitecoreClient extends SitecoreClient { // Overrides go here } -
Replace specific service factories to inject custom logic.
RequestResponseprotected getDictionaryService( baseOptions: BaseServiceOptions ): GraphQLDictionaryService { return new CustomDictionaryService({ ...baseOptions, customOption: true // Extended configuration }); }