Skip to content

Commit

Permalink
Merge 33a8a93 into 7da2528
Browse files Browse the repository at this point in the history
  • Loading branch information
endihunter committed Sep 13, 2022
2 parents 7da2528 + 33a8a93 commit 8777dfc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@goparrot/square-connect-plus",
"description": "Extends the official Square Node.js SDK library with additional functionality",
"version": "1.3.0",
"version": "1.3.0-dev.7073.0",
"author": "Coroliov Oleg",
"license": "MIT",
"private": false,
Expand Down
60 changes: 45 additions & 15 deletions src/client/SquareClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
Client,
DEFAULT_CONFIGURATION,
EmployeesApi,
GiftCardActivitiesApi,
GiftCardsApi,
InventoryApi,
InvoicesApi,
LaborApi,
LocationsApi,
LoyaltyApi,
MerchantsApi,
MobileAuthorizationApi,
OAuthApi,
OrdersApi,
Expand Down Expand Up @@ -130,6 +133,10 @@ export class SquareClient {
return this.proxy(new LocationsApi(this.getOriginClient()), retryableMethods);
}

getMerchantsApi(retryableMethods: string[] = ['retrieveMerchant', 'listMerchants']): MerchantsApi {
return this.proxy(new MerchantsApi(this.getOriginClient()), retryableMethods);
}

getMobileAuthorizationApi(retryableMethods: string[] = []): MobileAuthorizationApi {
return this.proxy(new MobileAuthorizationApi(this.getOriginClient()), retryableMethods);
}
Expand All @@ -146,6 +153,24 @@ export class SquareClient {
return this.proxy(new PaymentsApi(this.getOriginClient()), retryableMethods);
}

getGiftCardsApi(
retryableMethods: string[] = [
'listGiftCards',
'createGiftCard',
'retrieveGiftCardFromGAN',
'retrieveGiftCardFromNonce',
'linkCustomerToGiftCard',
'unlinkCustomerFromGiftCard',
'retrieveGiftCard',
],
): GiftCardsApi {
return this.proxy(new GiftCardsApi(this.getOriginClient()), retryableMethods);
}

getGiftCardActivitiesApi(retryableMethods: string[] = ['listGiftCardActivities', 'createGiftCardActivity']): GiftCardActivitiesApi {
return this.proxy(new GiftCardActivitiesApi(this.getOriginClient()), retryableMethods);
}

getRefundsApi(retryableMethods: string[] = ['getPaymentRefund', 'listPaymentRefunds', 'refundPayment']): RefundsApi {
return this.proxy(new RefundsApi(this.getOriginClient()), retryableMethods);
}
Expand Down Expand Up @@ -181,16 +206,8 @@ export class SquareClient {
return async (...args: unknown[]): Promise<ApiResponse<T>> => {
const requestFn: (...arg: unknown[]) => Promise<ApiResponse<T>> = target[apiMethodName].bind(target, ...args);

this.getLogger().debug(
`Square api request: ${JSON.stringify({ apiMethodName, args }, (_, value) =>
typeof value === 'bigint' ? value.toString() + 'n' : value,
)}`,
);

try {
const response = await this.makeRetryable<ApiResponse<T>>(requestFn, apiMethodName, retryableMethods);

return response;
return await this.makeRetryable<ApiResponse<T>>(api, requestFn, apiMethodName, retryableMethods);
} catch (err) {
if (err instanceof Error) {
err.stack += stackError;
Expand All @@ -205,27 +222,30 @@ export class SquareClient {
return new Proxy<T>(api, handler);
}

private async makeRetryable<T>(promiseFn: (...arg: unknown[]) => Promise<T>, apiMethodName: string, retryableMethods: string[]): Promise<T> {
private async makeRetryable<T>(api, promiseFn: (...arg: unknown[]) => Promise<T>, apiMethodName: string, retryableMethods: string[]): Promise<T> {
let retries: number = 0;
const { maxRetries, retryCondition = this.retryCondition } = this.#mergedConfig.retry;
const logger = this.getLogger();

async function retry(): Promise<T> {
const requestStartTime = new Date();

const startedAt = Date.now();
try {
return await promiseFn();
} catch (error) {
// @ts-expect-error
const squareException: SquareApiException = new SquareApiException(error, retries, new Date() - requestStartTime);
const finishedAt = Date.now();
const execTime = finishedAt - startedAt;
const squareException: SquareApiException = new SquareApiException(error, retries, execTime);

if (retryableMethods.includes(apiMethodName) && (await retryCondition(squareException, maxRetries, retries))) {
logger.info('Square api retry', {
retries,
maxRetries,
apiName: api.constructor.name,
apiMethodName,
startedAt,
finishedAt,
execTime,
exception: squareException.toObject(),
responseTime: squareException.responseTime,
});

retries++;
Expand All @@ -236,6 +256,16 @@ export class SquareClient {
}

throw squareException;
} finally {
const finishedAt = Date.now();
const execTime = finishedAt - startedAt;
logger.info(`Square api request: ${apiMethodName} executed in ${execTime}ms`, {
apiName: api.constructor.name,
apiMethodName,
startedAt,
finishedAt,
execTime,
});
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/client/SquareClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
CardsApi,
LoyaltyApi,
InvoicesApi,
GiftCardsApi,
GiftCardActivitiesApi,
} from 'square';
import { describe } from 'mocha';
import type { ISquareClientConfig } from '../../../src';
Expand Down Expand Up @@ -190,6 +192,18 @@ describe('SquareClient (unit)', (): void => {
});
});

describe('#getGiftCardsApi', (): void => {
it('should return GiftCardsApi', (): void => {
expect(new SquareClient(accessToken).getGiftCardsApi()).to.be.instanceOf(GiftCardsApi);
});
});

describe('#getGiftCardActivitiesApi', (): void => {
it('should return GiftCardActivitiesApi', (): void => {
expect(new SquareClient(accessToken).getGiftCardActivitiesApi()).to.be.instanceOf(GiftCardActivitiesApi);
});
});

describe('#getRefundsApi', (): void => {
it('should return RefundsApi', (): void => {
expect(new SquareClient(accessToken).getRefundsApi()).to.be.instanceOf(RefundsApi);
Expand Down
25 changes: 24 additions & 1 deletion test/unit/client/SquareClientFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { expect } from 'chai';
import { Client, CustomersApi, LocationsApi, OrdersApi, PaymentsApi, RefundsApi, Environment, DEFAULT_CONFIGURATION } from 'square';
import {
Client,
CustomersApi,
LocationsApi,
OrdersApi,
PaymentsApi,
RefundsApi,
Environment,
DEFAULT_CONFIGURATION,
GiftCardsApi,
GiftCardActivitiesApi,
} from 'square';
import type { ISquareClientConfig } from '../../../src';
import { SquareClient, SquareClientFactory, exponentialDelay } from '../../../src';

Expand Down Expand Up @@ -117,6 +128,18 @@ describe('SquareClientFactory (unit)', (): void => {
});
});

describe('#getGiftCardsApi', (): void => {
it('should return GiftCardsApi', (): void => {
expect(new SquareClient(accessToken).getGiftCardsApi()).to.be.instanceOf(GiftCardsApi);
});
});

describe('#getGiftCardActivitiesApi', (): void => {
it('should return GiftCardActivitiesApi', (): void => {
expect(new SquareClient(accessToken).getGiftCardActivitiesApi()).to.be.instanceOf(GiftCardActivitiesApi);
});
});

describe('#getRefundsApi', (): void => {
it('should return RefundsApi', (): void => {
expect(new SquareClient(accessToken).getRefundsApi()).to.be.instanceOf(RefundsApi);
Expand Down

0 comments on commit 8777dfc

Please sign in to comment.