createRequestOptions
Helper adding query parameters and headers to requests made to SAP Commerce Cloud from API methods.
Appended query parameters will include lang and curr (i.e. language and currency). Their values will be taken from (in order of relevance): - API method props, - vsf-locale and vsf-currency cookies, - defaultLanguage and defaultCurrency set in middleware.config.js.
Appended headers will vary depending on the tokenMode parameter. - if set to customer, the helper will inject a customer-specific access token (obtained through Resource Owner Password Flow), - if set to application, the helper will inject a generic application token (obtained through Client Credentials Flow), - if set to customerOrApplication, the helper will inject a customer-specific access token and only use the generic application token as a fallback, - if set to none, the helper will not inject any authorization headers.
Signature
createRequestOptions: <T extends BaseProps>(
input: {
context: SapccIntegrationContext;
props?: T;
tokenMode?: TokenMode;
}
) => AxiosRequestConfigParameters
| Name | Required | Type | Description |
|---|---|---|---|
{ context, props, tokenMode } | Required | ||
input | Required | { context: SapccIntegrationContext; props?: T; tokenMode?: TokenMode; } |
Returns
The helper returns an AxiosRequestConfig object.
Referenced Types
- BaseProps
- SapccIntegrationContext
- TokenMode
AxiosRequestConfig
Examples
Building options for a customer-specific request.
const options = createRequestOptions({
context,
props,
tokenMode: 'customer',
});Building options for a generic application request.
const options = createRequestOptions({
context,
props,
tokenMode: 'application',
});Building options for a request which can use either a customer token (when present) or a generic application token. Keep in mind the tokenMode parameter defaults to this value making it the default behaviour.
const options = createRequestOptions({
context,
props,
tokenMode: 'customerOrApplication',
});Building options for a request which needs no authorization headers.
const options = createRequestOptions({
context,
props,
tokenMode: 'none',
});