Vue Storefront is now Alokai! Learn More
Overriding API Methods

Overriding API Methods

Learn how to override the Unified Methods, defined within the Unified Data Layer.

The following guide is intended for @vsf-enterprise/unified-api-<integration> version 1.0.0 and above. Refer to the changelog (for example @vsf-enterprise/unified-api-sapcc) if you are using an older version.

Unified Methods are the core of the Unified Data Layer. They define a set of methods that should be implemented by all eCommerce integrations, supported by Alokai.

The following guide will show you how to override the default Unified Methods implementation for your integration.

Presented solutions require to maintain the Unified Data Layer contract. If you'd like to introduce new methods, refer to the guidance provided on the Creating New API Methods page.

Unified Data Layer Contract

Unified Data Layer Contract is an agreement that defines the structure of the data and methods that are available in the Unified Data Layer. In order to maintain the compatibility between the Storefront and the Unified Data Layer, it is important to follow the contract.

The contract means that the data structure and methods must be the same as the default implementation. This includes API method arguments and return types. This way, you can maintain the compatibility with the Storefront and other extensions. Check the Unified Methods page to see, what is the contract for the Unified Methods.

Override API Methods

If you want to override an existing API method, you can provide a new implementation with a key that matches an existing API method. This will completely replace the default logic of the method.

integrations/<integration>/extensions/unified.ts
import { createUnifiedExtension } from "@vsf-enterprise/unified-api-<integration>";

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [{}],
  },
  methods: {
    override: {
      getProductDetails: async (context, args) => {
        // your code here
      },
      // ...
    }
  },
});

Your custom API implementation must have the same parameters as the original

When overwriting an API method, the context and args parameters must be the same types as the original method. If you're using the createUnifiedExtension function, the types will be inferred automatically and any errors will be thrown if you incorrectly change a type.

You can also use the defineApi helper to define the API method outside of the createUnifiedExtension function.

integrations/<integration>/extensions/unified.ts
import { defineApi } from "@vsf-enterprise/unified-api-<integration>";

const getProductDetails = defineApi.getProductDetails(async (context, args) => {
  // your code here
});

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [{}],
  },
  methods: {
    override: {
      getProductDetails,
      // ...
    }
  },
});