Skip to content

Commit

Permalink
Merge 30617fc into 787a965
Browse files Browse the repository at this point in the history
  • Loading branch information
mciuchitu committed Oct 6, 2022
2 parents 787a965 + 30617fc commit 3fe14e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
3 changes: 2 additions & 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.6.0",
"version": "1.6.1",
"author": "Coroliov Oleg",
"license": "MIT",
"private": false,
Expand Down Expand Up @@ -113,6 +113,7 @@
"standard-version": "^9.3.2",
"ts-node": "^10.5.0",
"typescript": "^4.5.5",
"utility-types": "^3.10.0",
"uuid": "^8.3.2"
},
"dependencies": {
Expand Down
61 changes: 37 additions & 24 deletions src/client/SquareClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import upperFirst from 'lodash.upperfirst';
import type {
ApiResponse,
Error as SquareError,
ApplePayApi,
CardsApi,
CatalogApi,
CheckoutApi,
EmployeesApi,
Error as SquareError,
GiftCardActivitiesApi,
GiftCardsApi,
InventoryApi,
Expand All @@ -25,6 +25,7 @@ import type {
import { Client, DEFAULT_CONFIGURATION } from 'square';
import { v4 as uuidv4 } from 'uuid';
import type { BaseApi } from 'square/dist/api/baseApi';
import type { FunctionKeys } from 'utility-types';
import { retryableErrorCodes } from '../constants';
import { SquareApiException } from '../exception';
import type { ISquareClientConfig, ISquareClientDefaultConfig, ISquareClientMergedConfig } from '../interface';
Expand Down Expand Up @@ -77,30 +78,38 @@ export class SquareClient {
return this.#client;
}

getApplePayApi(retryableMethods: string[] = []): ApplePayApi {
getApplePayApi(retryableMethods: FunctionKeys<ApplePayApi>[] = []): ApplePayApi {
return this.proxy('applePayApi', retryableMethods);
}

getCatalogApi(
retryableMethods: string[] = ['batchRetrieveCatalogObjects', 'catalogInfo', 'listCatalog', 'retrieveCatalogObject', 'searchCatalogObjects'],
retryableMethods: FunctionKeys<CatalogApi>[] = [
'batchRetrieveCatalogObjects',
'catalogInfo',
'listCatalog',
'retrieveCatalogObject',
'searchCatalogObjects',
],
): CatalogApi {
return this.proxy('catalogApi', retryableMethods);
}

getCheckoutApi(retryableMethods: string[] = []): CheckoutApi {
getCheckoutApi(retryableMethods: FunctionKeys<CheckoutApi>[] = []): CheckoutApi {
return this.proxy('checkoutApi', retryableMethods);
}

getCustomersApi(retryableMethods: string[] = ['listCustomers', 'retrieveCustomer', 'searchCustomers', 'deleteCustomerCard']): CustomerClientApi {
getCustomersApi(
retryableMethods: FunctionKeys<CustomerClientApi>[] = ['listCustomers', 'retrieveCustomer', 'searchCustomers', 'deleteCustomerCard'],
): CustomerClientApi {
return this.proxyWithInstance('customersApi', new CustomerClientApi(this.getOriginClient()), retryableMethods);
}

getEmployeesApi(retryableMethods: string[] = ['listEmployees', 'retrieveEmployee']): EmployeesApi {
getEmployeesApi(retryableMethods: FunctionKeys<EmployeesApi>[] = ['listEmployees', 'retrieveEmployee']): EmployeesApi {
return this.proxy('employeesApi', retryableMethods);
}

getLoyaltyApi(
retryableMethods: string[] = [
retryableMethods: FunctionKeys<LoyaltyApi>[] = [
'listLoyaltyPrograms',
'searchLoyaltyEvents',
'searchLoyaltyAccounts',
Expand All @@ -112,7 +121,7 @@ export class SquareClient {
}

getInventoryApi(
retryableMethods: string[] = [
retryableMethods: FunctionKeys<InventoryApi>[] = [
'batchRetrieveInventoryChanges',
'batchRetrieveInventoryCounts',
'retrieveInventoryAdjustment',
Expand All @@ -125,7 +134,7 @@ export class SquareClient {
}

getLaborApi(
retryableMethods: string[] = [
retryableMethods: FunctionKeys<LaborApi>[] = [
'getBreakType',
'getEmployeeWage',
'getShift',
Expand All @@ -138,32 +147,34 @@ export class SquareClient {
return this.proxy('laborApi', retryableMethods);
}

getLocationsApi(retryableMethods: string[] = ['listLocations']): LocationsApi {
getLocationsApi(retryableMethods: FunctionKeys<LocationsApi>[] = ['listLocations']): LocationsApi {
return this.proxy('locationsApi', retryableMethods);
}

getMerchantsApi(retryableMethods: string[] = ['retrieveMerchant', 'listMerchants']): MerchantsApi {
getMerchantsApi(retryableMethods: FunctionKeys<MerchantsApi>[] = ['retrieveMerchant', 'listMerchants']): MerchantsApi {
return this.proxy('merchantsApi', retryableMethods);
}

getMobileAuthorizationApi(retryableMethods: string[] = []): MobileAuthorizationApi {
getMobileAuthorizationApi(retryableMethods: FunctionKeys<MobileAuthorizationApi>[] = []): MobileAuthorizationApi {
return this.proxy('mobileAuthorizationApi', retryableMethods);
}

getOAuthApi(retryableMethods: string[] = ['obtainToken']): OAuthApi {
getOAuthApi(retryableMethods: FunctionKeys<OAuthApi>[] = ['obtainToken']): OAuthApi {
return this.proxy('oAuthApi', retryableMethods);
}

getOrdersApi(retryableMethods: string[] = ['batchRetrieveOrders', 'searchOrders', 'createOrder', 'payOrder', 'calculateOrder']): OrdersApi {
getOrdersApi(
retryableMethods: FunctionKeys<OrdersApi>[] = ['batchRetrieveOrders', 'searchOrders', 'createOrder', 'payOrder', 'calculateOrder'],
): OrdersApi {
return this.proxy('ordersApi', retryableMethods);
}

getPaymentsApi(retryableMethods: string[] = ['getPayment', 'listPayments', 'createPayment', 'cancelPayment']): PaymentsApi {
getPaymentsApi(retryableMethods: FunctionKeys<PaymentsApi>[] = ['getPayment', 'listPayments', 'createPayment', 'cancelPayment']): PaymentsApi {
return this.proxy('paymentsApi', retryableMethods);
}

getGiftCardsApi(
retryableMethods: string[] = [
retryableMethods: FunctionKeys<GiftCardsApi>[] = [
'listGiftCards',
'createGiftCard',
'retrieveGiftCardFromGAN',
Expand All @@ -176,23 +187,25 @@ export class SquareClient {
return this.proxy('giftCardsApi', retryableMethods);
}

getGiftCardActivitiesApi(retryableMethods: string[] = ['listGiftCardActivities', 'createGiftCardActivity']): GiftCardActivitiesApi {
getGiftCardActivitiesApi(
retryableMethods: FunctionKeys<GiftCardActivitiesApi>[] = ['listGiftCardActivities', 'createGiftCardActivity'],
): GiftCardActivitiesApi {
return this.proxy('giftCardActivitiesApi', retryableMethods);
}

getRefundsApi(retryableMethods: string[] = ['getPaymentRefund', 'listPaymentRefunds', 'refundPayment']): RefundsApi {
getRefundsApi(retryableMethods: FunctionKeys<RefundsApi>[] = ['getPaymentRefund', 'listPaymentRefunds', 'refundPayment']): RefundsApi {
return this.proxy('refundsApi', retryableMethods);
}

getTransactionsApi(retryableMethods: string[] = ['listRefunds', 'listTransactions', 'retrieveTransaction']): TransactionsApi {
getTransactionsApi(retryableMethods: FunctionKeys<TransactionsApi>[] = ['listTransactions', 'retrieveTransaction']): TransactionsApi {
return this.proxy('transactionsApi', retryableMethods);
}

getCardsApi(retryableMethods: string[] = ['listCards', 'retrieveCard', 'disableCard']): CardsApi {
getCardsApi(retryableMethods: FunctionKeys<CardsApi>[] = ['listCards', 'retrieveCard', 'disableCard']): CardsApi {
return this.proxy('cardsApi', retryableMethods);
}

getInvoiceApi(retryableMethods: string[] = ['listInvoices', 'searchInvoices', 'getInvoice']): InvoicesApi {
getInvoiceApi(retryableMethods: FunctionKeys<InvoicesApi>[] = ['listInvoices', 'searchInvoices', 'getInvoice']): InvoicesApi {
return this.proxy('invoicesApi', retryableMethods);
}

Expand All @@ -207,7 +220,7 @@ export class SquareClient {
/**
* @throws SquareApiException
*/
private proxyWithInstance<T extends ApiName, A extends Client[T]>(apiName: T, api: A, retryableMethods: string[]): A {
private proxyWithInstance<T extends ApiName, A extends Client[T]>(apiName: T, api: A, retryableMethods: FunctionKeys<A>[]): A {
const stackError: string = new Error().stack?.slice(6) || '';

const handler: ProxyHandler<A> = {
Expand All @@ -234,7 +247,7 @@ export class SquareClient {
/**
* @throws SquareApiException
*/
private proxy<T extends ApiName>(apiName: T, retryableMethods: string[]): Client[T] {
private proxy<T extends ApiName>(apiName: T, retryableMethods: FunctionKeys<Client[T]>[]): Client[T] {
const api = this.getOriginClient()[apiName];

return this.proxyWithInstance(apiName, api, retryableMethods);
Expand All @@ -244,7 +257,7 @@ export class SquareClient {
apiName: ApiName,
promiseFn: (...arg: unknown[]) => Promise<T>,
apiMethodName: string,
retryableMethods: string[],
retryableMethods: (string | number | symbol)[],
): Promise<T> {
let retries: number = 0;
const { maxRetries, retryCondition = this.retryCondition } = this.#mergedConfig.retry;
Expand Down
25 changes: 16 additions & 9 deletions test/unit/client/SquareClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { expect } from 'chai';
import {
Client,
Environment,
ApplePayApi,
CardsApi,
CatalogApi,
CheckoutApi,
Client,
CustomersApi,
DEFAULT_CONFIGURATION,
EmployeesApi,
Environment,
GiftCardActivitiesApi,
GiftCardsApi,
InventoryApi,
InvoicesApi,
LaborApi,
LocationsApi,
LoyaltyApi,
MerchantsApi,
MobileAuthorizationApi,
OAuthApi,
OrdersApi,
PaymentsApi,
RefundsApi,
TransactionsApi,
DEFAULT_CONFIGURATION,
CardsApi,
LoyaltyApi,
InvoicesApi,
GiftCardsApi,
GiftCardActivitiesApi,
} from 'square';
import { describe } from 'mocha';
import type { ISquareClientConfig } from '../../../src';
import { SquareClient, exponentialDelay } from '../../../src';
import { exponentialDelay, SquareClient } from '../../../src';

describe('SquareClient (unit)', (): void => {
const accessToken: string = 'test';
Expand Down Expand Up @@ -175,6 +176,12 @@ describe('SquareClient (unit)', (): void => {
});
});

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

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

0 comments on commit 3fe14e1

Please sign in to comment.