Skip to content

Commit ca7a441

Browse files
committed
initial commit
1 parent da2f6ca commit ca7a441

File tree

2 files changed

+167
-1
lines changed

2 files changed

+167
-1
lines changed

src/static/helpers/fetchHelper.test.ts

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import nock from 'nock';
88
import {Response} from 'node-fetch';
99
import * as environment from './environment';
1010
import ClientConfig from '../clientConfig';
11-
import {doFetch} from './fetchHelper';
11+
import {doFetch, transferParams, ParameterKey} from './fetchHelper';
1212

1313
describe('doFetch', () => {
1414
const basePath = 'https://short_code.api.commercecloud.salesforce.com';
@@ -139,3 +139,121 @@ describe('doFetch', () => {
139139
);
140140
});
141141
});
142+
143+
describe('transferParams', () => {
144+
const optionParams = {
145+
// organizationId and siteId are not defined here
146+
id: 'id',
147+
inventoryIds: 'inventoryIds',
148+
expand: ['expand1', 'expand2'],
149+
allImages: true,
150+
perPricebook: true,
151+
select: 'select',
152+
currency: 'currency',
153+
locale: 'locale',
154+
};
155+
156+
const configParameters = {
157+
shortCode: 'short_code',
158+
organizationId: 'organization_id',
159+
clientId: 'client_id',
160+
siteId: 'site_id',
161+
};
162+
163+
const getProductRequired = ['organizationId', 'id'];
164+
165+
const currentParamKeys: Array<ParameterKey> = [
166+
{
167+
parameterName: 'organizationId',
168+
isQueryParameter: false,
169+
},
170+
{
171+
parameterName: 'id',
172+
isQueryParameter: false,
173+
},
174+
{
175+
parameterName: 'inventoryIds',
176+
isQueryParameter: true,
177+
},
178+
{
179+
parameterName: 'expand',
180+
isQueryParameter: true,
181+
},
182+
{
183+
parameterName: 'allImages',
184+
isQueryParameter: true,
185+
},
186+
{
187+
parameterName: 'perPricebook',
188+
isQueryParameter: true,
189+
},
190+
{
191+
parameterName: 'select',
192+
isQueryParameter: true,
193+
},
194+
{
195+
parameterName: 'currency',
196+
isQueryParameter: true,
197+
},
198+
{
199+
parameterName: 'locale',
200+
isQueryParameter: true,
201+
},
202+
{
203+
parameterName: 'siteId',
204+
isQueryParameter: true,
205+
},
206+
];
207+
208+
test('transferParams successfully populates the query and path parameters object', () => {
209+
const queryParameters = {};
210+
const pathParameters = {};
211+
212+
transferParams({
213+
optionParams,
214+
configParams: configParameters,
215+
queryParams: queryParameters,
216+
pathParams: pathParameters,
217+
paramKeys: currentParamKeys,
218+
requiredParamKeys: getProductRequired,
219+
});
220+
221+
const expectedQueryParameters = {
222+
allImages: true,
223+
currency: 'currency',
224+
expand: ['expand1', 'expand2'],
225+
inventoryIds: 'inventoryIds',
226+
locale: 'locale',
227+
perPricebook: true,
228+
siteId: 'site_id',
229+
select: 'select',
230+
};
231+
232+
const expectedPathParameters = {
233+
id: 'id',
234+
organizationId: 'organization_id',
235+
};
236+
237+
expect(queryParameters).toEqual(expectedQueryParameters);
238+
expect(pathParameters).toEqual(expectedPathParameters);
239+
});
240+
241+
test('transferParams throws error when required parameter is not passed', () => {
242+
const queryParameters = {};
243+
const pathParameters = {};
244+
245+
// remove id from optionParams
246+
const {id, ...newOptionParams} = optionParams;
247+
248+
expect(() =>
249+
transferParams({
250+
optionParams: newOptionParams,
251+
configParams: configParameters,
252+
queryParams: queryParameters,
253+
pathParams: pathParameters,
254+
paramKeys: currentParamKeys,
255+
requiredParamKeys: getProductRequired,
256+
})
257+
).toThrowError('Missing required parameter: id');
258+
});
259+
});

src/static/helpers/fetchHelper.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,51 @@ export const doFetch = async <Params extends BaseUriParameters>(
6868
return (text ? JSON.parse(text) : {}) as unknown | Response;
6969
}
7070
};
71+
72+
export type ParameterKey = {
73+
parameterName: string;
74+
required?: boolean;
75+
isQueryParameter?: boolean;
76+
isPathParameter?: boolean;
77+
};
78+
79+
/**
80+
* Transfers parameters from option and config objects to the query parameters object
81+
* based on a list of parameter keys
82+
*
83+
* @param options - The options object
84+
* @param options.optionParams - The options parameters object (higher priority)
85+
* @param options.configParams - The config parameters object (lower priority)
86+
* @param options.queryParams - The query parameters object to populate
87+
* @param options.pathParams - The path parameters object to populate
88+
* @param options.paramKeys - Array of parameter keys to process
89+
* @param options.paramKeys - Array of parameter keys to process
90+
*/
91+
export const transferParams = (options: {
92+
optionParams: Record<string, any>;
93+
configParams: Record<string, any>;
94+
queryParams: Record<string, any>;
95+
pathParams: Record<string, any>;
96+
paramKeys: Array<ParameterKey>;
97+
requiredParamKeys: readonly string[];
98+
}): void => {
99+
const {
100+
optionParams,
101+
configParams,
102+
queryParams,
103+
paramKeys,
104+
pathParams,
105+
requiredParamKeys,
106+
} = options;
107+
paramKeys.forEach(paramKey => {
108+
const currentParams = paramKey.isQueryParameter ? queryParams : pathParams;
109+
const key = paramKey.parameterName;
110+
if (optionParams[key] !== undefined) {
111+
currentParams[key] = optionParams[key];
112+
} else if (configParams[key] !== undefined) {
113+
currentParams[key] = configParams[key];
114+
} else if (requiredParamKeys.includes(key)) {
115+
throw new Error(`Missing required parameter: ${paramKey.parameterName}`);
116+
}
117+
});
118+
};

0 commit comments

Comments
 (0)