- 06 Jan 2025
- 4 Minutes to read
- Print
- DarkLight
ApiDecorator
- Updated on 06 Jan 2025
- 4 Minutes to read
- Print
- DarkLight
A developer can use the IApiResponseDecorator to modify or enhance the ApiResponseContext before it is sent back to the client. This interface allows developers to add additional data or modify existing data in the response in a flexible and modular way. By implementing the IApiResponseDecorator interface, developers can create custom decorators that interact with the ApiResponseContext to tailor the response data according to specific requirements. This is particularly useful for adding custom properties, transforming data, or injecting additional information into the API response.
Create and register a welisacommerce.IApiPlugin implementation
To be able to register new decorators you will need to create an ApiPlugin. This can be done by implementing the welisacommerce.IApiPlugin interface. This interface has one method called initialize that accepts an instance of welisacommerce.ApiPluginParams. Once you have done so you can register this class as a plugin in the Custom Metadata Type "Plugin".
- Browse to Setup -> Custom Metadata Types -> Plugin
- Manage record and click ' New'
- Select Type Api Plugin
- Enter the name of the class in the Default Class Name field.
- Set name and label
- Save
Create a IApiResponseDecorator implementation
You need to create a new class that implements the welisacommerce.IApiResponseDecorator
interface. This interface has one method called decorate that accepts and instance of welisacommerce.ApiResponseContext
. This class has the following methods.
Method signature | Description |
---|---|
String getRequestParam(String paramName | Can be used to retrieve a request paramater |
Id getContextId() | Will provide the request path element representing a specific record id (like web product) |
void addData(String propertyName, object propertyValue) | Register the additional data that needs to be return by the api |
To use this decorator, you would typically instantiate it and call the decorate method, passing in an instance of ApiResponseContext.
ApiResponseContext
The ApiResponseContext interface provides methods to interact with the response data that will be sent back to the client. It allows for retrieving request parameters, getting the context ID, and adding custom data to the response.
Methods:
- String getRequestParam(String paramName): Retrieves the value of a request parameter.
- Id getContextId(): Returns the context ID associated with the response.
- void addData(String propertyName, Object propertyValue): Adds custom data to the response.
Example usage
global class MyResponseDecorator implements welisacommerce.IApiResponseDecorator {
public void decorate(welisacommerce.ApiResponseContext context) {
// Retrieve a request parameter
String paramValue = context.getRequestParam('paramName');
// Add custom data to the response context
context.addData('customProperty', 'customValue');
}
}
Register decorator in decorator registry
A new decorator needs to be registered in the decorator registry. For us to be able to use the decorator for specific requests you need to specify the Resource and Operation Type. For this you call the welisacommerce.ApiDecoratorRegistry.registerDecorator()
method.
This method accepts the folllowing three parameters:
Parameter Name | Type | Description |
---|---|---|
resourceType | welisacommerce.ApiConstants.ResourceType | The resource to use the decorator for like PRODUCT or SHOPPING_CART |
operationType | welisacommerce. ApiConstants.OperationType | The operation to use the decorator for, like GET or POST |
decoratorType | Type | The type representation the created api decorator |
ApiDecoratorRegistry
The ApiDecoratorRegistry class provides a flexible and centralized way to manage API response decorators in Salesforce. Developers can register custom decorators for specific resources and operations, retrieve these decorators, and apply them to API responses to customize the behavior of their APIs.
registerDecorator method
The registerDecorator method is used to register a custom decorator for a specific resource type and operation type. This allows developers to customize API responses by applying the registered decorators.
Method Signature
Parameters
Name | Type | Description |
---|---|---|
resourceType | welisacommerce.ApiConstants.ResourceType | The type of resource for which the decorator is being registered. This is an enum value defined in ApiConstants |
operationType | welisacommerce.ApiConstants.OperationType | The type of operation for which the decorator is being registered. This is an enum value defined in ApiConstants |
decoratorType | System.Type | The Apex class type of the decorator being registered. The class must implement the IApiResponseDecorator interface |
Usage
1. Define a Decorator Class
Implement the welisacommerce.IApiResponseDecorator interface in a custom class.
public class CheckoutDecorator implements welisacommerce.IApiResponseDecorator {
public void decorate(welisacommerce.ApiResponseContext response) {
// Custom logic to modify the API response for Account creation
response.addData('message', 'Order created successfully.');
}
}
2. Register the Decorator:
Call the registerDecorator method to register the custom decorator for a specific resource type and operation type.
Example:
welisacommerce.ApiDecoratorRegistry.registerDecorator(
welisacommerce.ApiConstants.ResourceType.CHECKOUT,
welisacommerce.ApiConstants.OperationType.POST,
CheckoutDecorator.class
);
Notes
- The decoratorType must implement the IApiResponseDecorator interface. If it does not, a System.TypeException will be thrown.
- The registerDecorator method is a static method, so it can be called without instantiating the ApiDecoratorRegistry class.
- The decorators are stored in a map within the ApiDecoratorRegistry singleton instance, ensuring that they are managed centrally.
Example class - Combining the ApiPlugin, Response Decorator and the TranslationService
global class TestApiPlugin implements welisacommerce.IApiPlugin {
global void initialize(List<object> params) {
welisacommerce.ApiDecoratorRegistry.registerDecorator(
welisacommerce.ApiConstants.ResourceType.Product,
welisacommerce.ApiConstants.OperationType.Get,
TestApiDecorator.class
);
}
global class TestApiDecorator implements welisacommerce.IApiResponseDecorator {
global void decorate(welisacommerce.ApiResponseContext context) {
welisacommerce.TranslationService.GetLocalizedRecordsRequest request = new welisacommerce.TranslationService.GetLocalizedRecordsRequest();
request.recordIds = new List<Id>{ context.getContextId()};
request.fields = new List<Schema.SObjectField>{welisacommerce__Web_Product__c.welisacommerce__Title__c };
request.languages = new List<String>{ 'nl_NL' };
context.addData('relatedProducts', welisacommerce.TranslationService.getLocalizedRecords(request).results);
}
}
}