Skip to content

Commit 56345bf

Browse files
committed
Release Version - 0.51.0
1 parent d41c6ae commit 56345bf

File tree

12 files changed

+159
-8
lines changed

12 files changed

+159
-8
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@boldcommerce/checkout-frontend-library",
3-
"version": "0.50.0",
3+
"version": "0.51.0",
44
"main": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"license": "MIT",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ export {
5151
getCurrency
5252
} from './state';
5353
export * from './pigi';
54+
export * from './log';

src/log/addLog.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {apiTypeKeys, apiTypes, IApiReturnObject, checkApiResponse, fetchAPI, getApiOptions, getApiUrl} from 'src';
2+
3+
/**
4+
* # addLog
5+
*
6+
* This make requests which contain messages that will be logged through the usual Checkout logging mechanisms.
7+
*
8+
* @param message Message to add to Logs - Maximum of 100 Characters
9+
* @param key Optional Key to add to NR counters
10+
* @param numOfRetries
11+
*
12+
*/
13+
export async function addLog(message: string, key?: string, numOfRetries = 0): Promise<IApiReturnObject> {
14+
const {addLog} = apiTypeKeys;
15+
const {keysToTest} = apiTypes.addLog;
16+
const url = getApiUrl(addLog);
17+
const options = getApiOptions(addLog, { message, key });
18+
const fetchRes = await fetchAPI(url, options, numOfRetries);
19+
return checkApiResponse(fetchRes, keysToTest);
20+
}

src/log/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './addLog';

src/types/apiInterfaces.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export interface IApiSuccessResponse {
1919
IAddPaymentResponse |
2020
IUpdateLineItemQuantityResponse |
2121
IProcessOrderResponse |
22-
IPatchOrderMetaDataResponse;
22+
IPatchOrderMetaDataResponse |
23+
IAddLogResponse;
2324
application_state?: IApplicationState;
2425
}
2526

@@ -202,6 +203,10 @@ export interface IPatchOrderMetaDataResponse {
202203
application_state: IApplicationState | undefined;
203204
}
204205

206+
export interface IAddLogResponse {
207+
success: boolean;
208+
}
209+
205210
export interface IAddPaymentResponse {
206211
payment: IPayment;
207212
application_state: IApplicationState | undefined;
@@ -262,6 +267,7 @@ export interface IApiTypes {
262267
deleteGiftCardPayment: IApiTypesDetail;
263268
patchOrderMetaData: IApiTypesDetail;
264269
batchRequest: IApiTypesDetail;
270+
addLog: IApiTypesDetail;
265271
}
266272

267273
export interface IApiTypeKeys {
@@ -297,6 +303,7 @@ export interface IApiTypeKeys {
297303
deleteGiftCardPayment: keyof IApiTypes;
298304
patchOrderMetaData: keyof IApiTypes;
299305
batchRequest: keyof IApiTypes;
306+
addLog: keyof IApiTypes;
300307
}
301308

302309
export interface IValidateAddress {

src/utils/apiResponse.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {setOrderInitialData} from 'src/state/setOrderInitialData';
1616
export function checkApiResponse(fetchRes: IApiReturnObject, keysToCheck?: Array<string>, checkOnFail = false): IApiReturnObject {
1717
const success = fetchRes.success;
1818
const response = fetchRes.response as IApiResponse;
19+
const originalError = fetchRes.error;
1920
const errorMessages: Array<string> = [];
2021

2122
if (!keysToCheck || (Array.isArray(keysToCheck) && keysToCheck.length <= 0)) {
@@ -57,8 +58,9 @@ export function checkApiResponse(fetchRes: IApiReturnObject, keysToCheck?: Array
5758
});
5859

5960
if (errorMessages.length > 0) {
61+
const metaData = originalError?.metaData ? {...originalError.metaData, fields: errorMessages} : {fields: errorMessages};
6062
fetchRes.success = false;
61-
fetchRes.error = new FetchError(apiErrors.errorsInResponse.status, apiErrors.errorsInResponse.message, undefined, undefined, {fields: errorMessages});
63+
fetchRes.error = new FetchError(apiErrors.errorsInResponse.status, apiErrors.errorsInResponse.message, undefined, undefined, metaData);
6264
}
6365
}
6466
return fetchRes;

src/utils/fetchAPI.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const sleep = (time: number) => new Promise(func => setTimeout(func, time));
55
/**
66
* # FetchAPI
77
*
8-
* This function takes in a url and optional parameters, fetches data from the specified api and returns the data
8+
* This function takes in an url and optional parameters, fetches data from the specified api and returns the data
99
*
1010
* @param url URL to fetch data from
1111
* @param numOfRetries
@@ -38,7 +38,11 @@ export async function callFetch(url: RequestInfo, numOfRetries: number, totalNum
3838
}
3939
} catch(e) {
4040
const { status, message } = apiErrors.general;
41-
returnObject.error = new FetchError(status, `${message} - ${e}`);
41+
if (e instanceof Error) {
42+
returnObject.error = new FetchError(status, `${message} - ${e.message}`, message, null, {error: e});
43+
} else {
44+
returnObject.error = new FetchError(status, `${message} - ${e}`);
45+
}
4246
returnObject.success = false;
4347
return returnObject;
4448
}

src/variables/constants.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ export const apiTypes: IApiTypes = {
290290
method: methods.POST,
291291
useJwt: true,
292292
keysToTest: [...appStateKeysToTest]
293+
},
294+
addLog: {
295+
path: '/log',
296+
method: methods.POST,
297+
useJwt: true,
298+
keysToTest: [keysToTestFromResponse.data]
293299
}
294300
};
295301

@@ -325,7 +331,8 @@ export const apiTypeKeys: IApiTypeKeys = {
325331
deletePayment: 'deletePayment',
326332
deleteGiftCardPayment: 'deleteGiftCardPayment',
327333
patchOrderMetaData: 'patchOrderMetaData',
328-
batchRequest: 'batchRequest'
334+
batchRequest: 'batchRequest',
335+
addLog: 'addLog'
329336
};
330337

331338
export const baseReturnObject: IApiReturnObject = {

tests/log/addLog.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {apiTypeKeys, baseReturnObject, methods, apiTypes, addLog} from 'src';
2+
import * as fetchAPI from 'src/utils/fetchAPI';
3+
import * as getApiOptions from 'src/utils/getApiOptions';
4+
import * as apiUrl from 'src/utils/apiUrl';
5+
import * as apiResponse from 'src/utils/apiResponse';
6+
7+
describe('testing add log api', () => {
8+
const returnObject = {...baseReturnObject};
9+
const timesWhenCalled = 1;
10+
const message = 'some test message';
11+
const key = 'test_key';
12+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/log';
13+
const {keysToTest} = apiTypes.addLog;
14+
let optionsMock: RequestInit;
15+
let getApiOptionsSpy: jest.SpyInstance;
16+
let getApiUrlSpy: jest.SpyInstance;
17+
let fetchApiSpy: jest.SpyInstance;
18+
let checkApiResponseSpy: jest.SpyInstance;
19+
20+
beforeEach(() => {
21+
global.Headers = jest.fn().mockReturnValue({
22+
append: jest.fn(() => null)
23+
});
24+
optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})};
25+
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
26+
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
27+
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
28+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
29+
returnObject.response = { data: {success: true}};
30+
returnObject.success = true;
31+
});
32+
33+
afterEach(() => {
34+
jest.restoreAllMocks();
35+
});
36+
37+
test('calling add log', async () => {
38+
const res = await addLog(message, key);
39+
40+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
41+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
42+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
43+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
44+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.addLog, {message, key});
45+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.addLog);
46+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
47+
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
48+
expect(res).toStrictEqual(returnObject);
49+
});
50+
51+
test('calling add log w/ success = false', async () => {
52+
const tempReturnObject = {...baseReturnObject};
53+
54+
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
55+
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
56+
57+
const res = await addLog(message, key, 1);
58+
59+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
60+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
61+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
62+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
63+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.addLog, {message, key});
64+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.addLog);
65+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
66+
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
67+
expect(res).toStrictEqual(tempReturnObject);
68+
});
69+
70+
});

0 commit comments

Comments
 (0)