ApiDecoratorRegistry
  • 06 Jan 2025
  • 1 Minute to read
  • Dark
    Light

ApiDecoratorRegistry

  • Dark
    Light

Article summary

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 Documentation

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

NameTypeDescription
resourceTypewelisacommerce.ApiConstants.ResourceTypeThe type of resource for which the decorator is being registered. This is an enum value defined in ApiConstants
operationTypewelisacommerce.ApiConstants.OperationTypeThe type of operation for which the decorator is being registered. This is an enum value defined in ApiConstants
decoratorTypeSystem.TypeThe 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.