@commercetools-frontend/application-shell-connectors
Version:
Contains complementary tools for @commercetools-frontend/application-shell
108 lines (107 loc) • 3.53 kB
TypeScript
/**
* This file contains helper functions to configure an HTTP client
* with the recommended configuration for the Merchant Center API.
*/
export type THeaders = Record<string, string>;
export type TForwardToAudiencePolicy = 'forward-url-full-path' | 'forward-url-origin';
export type TForwardToExchangeTokenClaim = 'permissions';
export type TForwardToConfigVersion = 'v1' | 'v2';
export type TForwardToConfig = {
/**
* The URL of the external API to forward the request to.
*/
uri: string;
/**
* Additional HTTP headers to be included in the request to the external API.
*/
headers?: THeaders;
/**
* The audience policy for verifying the incoming request from the Merchant Center API.
*/
audiencePolicy?: TForwardToAudiencePolicy;
/**
* A list of user permissions to be included in the request to the external API.
*/
includeUserPermissions?: boolean;
/**
* The version of the `/proxy/forward-to` endpoint to use.
*/
version?: TForwardToConfigVersion;
};
export type TConfig = {
/**
* A custom user agent to identify the HTTP client.
* We recommend to use the `@commercetools/http-user-agent` package.
*
* @example
* import createHttpUserAgent from '@commercetools/http-user-agent';
*
* const userAgent = createHttpUserAgent({
* name: 'fetch-client',
* version: '2.6.0',
* libraryName: window.app.applicationName,
* contactEmail: 'support@my-company.com',
* });
*/
userAgent?: string;
/**
* Additional headers to be included in the request.
* The provided recommended headers won't be overwritten.
* See `TOptions.headers`.
*/
headers?: THeaders;
/**
* Configuration for using the `/proxy/forward-to` endpoint
* to connect to an external API.
* {@link https://docs.commercetools.com/merchant-center-customizations/concepts/integrate-with-your-own-api}
*
* @example
* {
* forwardToConfig: {
* uri: 'https://my-api.com/my-endpoint',
* }
* }
*/
forwardToConfig?: TForwardToConfig;
/**
* The project key to be assigned to the `x-project-key` header.
* By default the project key is extracted from the URL.
* We do not recommend to use this option unless you know what you are doing.
*/
projectKey?: string;
};
export type TOptions = {
/**
* Include user credentials (session token).
*/
credentials: 'include';
/**
* The HTTP headers included by default are:
* - Accept
* - Authorization (only in development)
* - X-Application-Id
* - X-Correlation-Id
* - X-Project-Key
* - X-User-Agent
*/
headers: THeaders;
};
export type TFetcherResponse<Data> = {
/**
* The parsed response from the server.
*/
data: Data;
/**
* The HTTP status code from the server response.
*/
statusCode: number;
/**
* Implement a function to access the HTTP headers from the server response.
*/
getHeader: (headerName: string) => string | null;
};
export type TFetcher<Data> = (options: TOptions) => Promise<TFetcherResponse<Data>>;
declare function buildApiUrl(endpoint: string): string;
declare function createHttpClientOptions(config?: TConfig): TOptions;
declare function executeHttpClientRequest<Data>(fetcher: TFetcher<Data>, config?: TConfig): Promise<Data>;
export { buildApiUrl, createHttpClientOptions, executeHttpClientRequest };