Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 119 additions & 1 deletion src/static/helpers/fetchHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {Response} from 'node-fetch';
import * as environment from './environment';
import ClientConfig from '../clientConfig';
import {doFetch} from './fetchHelper';
import {doFetch, transferParams, ParameterKey} from './fetchHelper';

describe('doFetch', () => {
const basePath = 'https://short_code.api.commercecloud.salesforce.com';
Expand Down Expand Up @@ -139,3 +139,121 @@
);
});
});

describe('transferParams', () => {
const optionParams: Record<string, unknown> = {
// organizationId and siteId are not defined here
id: 'id',
inventoryIds: 'inventoryIds',
expand: ['expand1', 'expand2'],
allImages: true,
perPricebook: true,
select: 'select',
currency: 'currency',
locale: 'locale',
};

const configParameters = {
shortCode: 'short_code',
organizationId: 'organization_id',
clientId: 'client_id',
siteId: 'site_id',
};

const getProductRequired = ['organizationId', 'id'];

const currentParamKeys: Array<ParameterKey> = [
{
parameterName: 'organizationId',
isQueryParameter: false,
},
{
parameterName: 'id',
isQueryParameter: false,
},
{
parameterName: 'inventoryIds',
isQueryParameter: true,
},
{
parameterName: 'expand',
isQueryParameter: true,
},
{
parameterName: 'allImages',
isQueryParameter: true,
},
{
parameterName: 'perPricebook',
isQueryParameter: true,
},
{
parameterName: 'select',
isQueryParameter: true,
},
{
parameterName: 'currency',
isQueryParameter: true,
},
{
parameterName: 'locale',
isQueryParameter: true,
},
{
parameterName: 'siteId',
isQueryParameter: true,
},
];

test('transferParams successfully populates the query and path parameters object', () => {
const queryParameters = {};
const pathParameters = {};

transferParams({
optionParams,
configParams: configParameters,
queryParams: queryParameters,
pathParams: pathParameters,
paramKeys: currentParamKeys,
requiredParamKeys: getProductRequired,
});

const expectedQueryParameters = {
allImages: true,
currency: 'currency',
expand: ['expand1', 'expand2'],
inventoryIds: 'inventoryIds',
locale: 'locale',
perPricebook: true,
siteId: 'site_id',
select: 'select',
};

const expectedPathParameters = {
id: 'id',
organizationId: 'organization_id',
};

expect(queryParameters).toEqual(expectedQueryParameters);
expect(pathParameters).toEqual(expectedPathParameters);
});

test('transferParams throws error when required parameter is not passed', () => {
const queryParameters = {};
const pathParameters = {};

// remove id from optionParams
const {id, ...newOptionParams} = optionParams;

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

'id' is assigned a value but never used

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

'id' is assigned a value but never used

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

'id' is assigned a value but never used

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

'id' is assigned a value but never used

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (20)

'id' is assigned a value but never used

Check warning on line 246 in src/static/helpers/fetchHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (22)

'id' is assigned a value but never used

expect(() =>
transferParams({
optionParams: newOptionParams,
configParams: configParameters,
queryParams: queryParameters,
pathParams: pathParameters,
paramKeys: currentParamKeys,
requiredParamKeys: getProductRequired,
})
).toThrowError('Missing required parameter: id');
});
});
48 changes: 48 additions & 0 deletions src/static/helpers/fetchHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,51 @@ export const doFetch = async <Params extends BaseUriParameters>(
return (text ? JSON.parse(text) : {}) as unknown | Response;
}
};

export type ParameterKey = {
parameterName: string;
required?: boolean;
isQueryParameter?: boolean;
isPathParameter?: boolean;
};

/**
* Transfers parameters from option and config objects to the query parameters object
* based on a list of parameter keys
*
* @param options - The options object
* @param options.optionParams - The options parameters object (higher priority)
* @param options.configParams - The config parameters object (lower priority)
* @param options.queryParams - The query parameters object to populate
* @param options.pathParams - The path parameters object to populate
* @param options.paramKeys - Array of parameter keys to process
* @param options.paramKeys - Array of parameter keys to process
*/
export const transferParams = (options: {
optionParams: Record<string, unknown>;
configParams: Record<string, unknown>;
queryParams: Record<string, unknown>;
pathParams: Record<string, unknown>;
paramKeys: Array<ParameterKey>;
requiredParamKeys: readonly string[];
}): void => {
const {
optionParams,
configParams,
queryParams,
paramKeys,
pathParams,
requiredParamKeys,
} = options;
paramKeys.forEach(paramKey => {
const currentParams = paramKey.isQueryParameter ? queryParams : pathParams;
const key = paramKey.parameterName;
if (optionParams[key] !== undefined) {
currentParams[key] = optionParams[key];
} else if (configParams[key] !== undefined) {
currentParams[key] = configParams[key];
} else if (requiredParamKeys.includes(key)) {
throw new Error(`Missing required parameter: ${paramKey.parameterName}`);
}
});
};
Loading