Skip to content

Commit

Permalink
Merge pull request #97 from mwarzybok-sumoheavy/feature/SP-731
Browse files Browse the repository at this point in the history
Feature/sp 731
  • Loading branch information
bobbrodie authored Dec 1, 2023
2 parents 59bd96b + 08f59da commit e6a8dda
Show file tree
Hide file tree
Showing 101 changed files with 4,482 additions and 1,678 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"rules": {
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off"
}
}
4,006 changes: 3,104 additions & 902 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"dependencies": {
"bs58": "4.0.1",
"elliptic": "6.5.4",
"lodash": "4.17.21"
"lodash": "4.17.21",
"zod": "3.22.4"
},
"devDependencies": {
"@types/bs58": "4.0.1",
Expand All @@ -69,4 +70,4 @@
"ts-jest": "29.1.1",
"typescript": "5.2.2"
}
}
}
26 changes: 16 additions & 10 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class Client {
configFilePath: string | null,
privateKey: PrivateKey | null,
tokenContainer: TokenContainer | null,
identity: string | null,
posToken: PosToken | null,
environment?: Environment,
bitPayClient?: BitPayClient, // using for tests
Expand All @@ -68,6 +67,9 @@ export class Client {

if (bitPayClient !== undefined && bitPayClient !== null) {
// using for tests
if (guidGenerator == undefined) {
guidGenerator = new GuidGenerator();
}
this.initForTests(bitPayClient, guidGenerator, tokenContainer);
return;
}
Expand Down Expand Up @@ -100,7 +102,7 @@ export class Client {
* @param environment
*/
public static createPosClient(posToken: string, environment?: Environment): Client {
return new Client(null, null, null, null, new PosToken(posToken), environment);
return new Client(null, null, null, new PosToken(posToken), environment);
}

/**
Expand All @@ -109,7 +111,7 @@ export class Client {
* @param configFilePath
*/
public static createClientByConfig(configFilePath: string): Client {
return new Client(configFilePath, null, null, null, null, null);
return new Client(configFilePath, null, null, null);
}

/**
Expand All @@ -123,7 +125,7 @@ export class Client {
tokenContainer: TokenContainer,
environment?: Environment
) {
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, null, environment);
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, environment);
}

public getToken(facade: Facade) {
Expand All @@ -148,7 +150,7 @@ export class Client {
* Current supported values are BTC and BCH.
* @return A Rates object populated with the BitPay exchange rate table.
*/
public async getRates(currency: string = null): Promise<Rates> {
public async getRates(currency: string | null): Promise<Rates> {
return this.createRateClient().getRates(currency);
}

Expand Down Expand Up @@ -626,7 +628,7 @@ export class Client {
* @param status The status to filter the bills.
* @return BillInterface A list of BitPay Bill objects.
*/
public async getBills(status: string | null): Promise<BillInterface> {
public async getBills(status: string | null): Promise<BillInterface[]> {
return this.createBillClient().getBills(status);
}

Expand Down Expand Up @@ -707,9 +709,9 @@ export class Client {
* Gets info for specific currency.
*
* @param currencyCode String Currency code for which the info will be retrieved.
* @return CurrencyInterface Currency info.
* @return CurrencyInterface|null Currency info.
*/
public async getCurrencyInfo(currencyCode: string): Promise<CurrencyInterface> {
public async getCurrencyInfo(currencyCode: string): Promise<CurrencyInterface | null> {
return this.getCurrencyClient().getCurrencyInfo(currencyCode);
}

Expand All @@ -735,6 +737,7 @@ export class Client {
}

BitPayExceptionProvider.throwGenericExceptionWithMessage('Missing ECKey');
throw new Error();
}

private static getBaseUrl(environment: string) {
Expand Down Expand Up @@ -801,7 +804,10 @@ export class Client {
return facade !== Facade.Pos;
}

private static getDateAsString(date: Date): string {
private static getDateAsString(date: Date | null): string | null {
if (date === null) {
return null;
}
return date.toISOString().split('T')[0];
}

Expand All @@ -819,7 +825,7 @@ export class Client {
const ecKey = this.getEcKeyByConfig(envConfig);
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
this.guidGenerator = new GuidGenerator();
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwGenericExceptionWithMessage('Error when reading configuration file');
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/Client/BillClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export class BillClient {

try {
return <BillInterface>JSON.parse(result);
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwSerializeResourceException('Bill', e.message);
throw new Error();
}
}

Expand All @@ -50,8 +51,9 @@ export class BillClient {

try {
return <BillInterface>JSON.parse(result);
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
throw new Error();
}
}

Expand All @@ -63,7 +65,7 @@ export class BillClient {
* @throws BitPayGenericException BitPayGenericException
* @throws BitPayApiException BitPayApiException
*/
public async getBills(status: string | null): Promise<BillInterface> {
public async getBills(status: string | null): Promise<BillInterface[]> {
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
if (status) {
params['status'] = status;
Expand All @@ -72,9 +74,10 @@ export class BillClient {
const result = await this.bitPayClient.get('bills', params, true);

try {
return <BillInterface>JSON.parse(result);
} catch (e) {
return <BillInterface[]>JSON.parse(result);
} catch (e: any) {
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
throw new Error();
}
}

Expand All @@ -92,8 +95,9 @@ export class BillClient {

try {
return <BillInterface>JSON.parse(result);
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
throw new Error();
}
}

Expand All @@ -113,8 +117,9 @@ export class BillClient {

try {
return <string>JSON.parse(result) == 'Success';
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
throw new Error();
}
}
}
28 changes: 18 additions & 10 deletions src/Client/BitPayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { LoggerProvider } from '../Logger/LoggerProvider';

export class BitPayClient {
private readonly ecKey: KeyPair;
private readonly identity: string;
private readonly ecKey: KeyPair | null;
private readonly identity: string | null;
private readonly baseUrl: string;
private readonly defaultHeaders: Record<string, string>;
private readonly keyUtils: KeyUtils;
private readonly responseParser: BitPayResponseParser;

public constructor(baseUrl: string, ecKey: KeyPair, identity: string) {
public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null) {
this.ecKey = ecKey;
this.baseUrl = baseUrl;
this.identity = identity;
Expand Down Expand Up @@ -67,7 +67,7 @@ export class BitPayClient {
LoggerProvider.getLogger().logResponse(method, fullUrl, JSON.stringify(jsonObject));

return this.responseParser.getJsonDataFromJsonResponse(jsonObject);
} catch (e) {
} catch (e: any) {
if (e instanceof BitPayException) {
throw e;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ export class BitPayClient {
LoggerProvider.getLogger().logResponse(method, fullUrl, JSON.stringify(jsonObject));

return this.responseParser.getJsonDataFromJsonResponse(jsonObject);
} catch (e) {
} catch (e: any) {
if (e instanceof BitPayException) {
throw e;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export class BitPayClient {
LoggerProvider.getLogger().logResponse(method, fullUrl, JSON.stringify(jsonObject));

return this.responseParser.getJsonDataFromJsonResponse(jsonObject);
} catch (e) {
} catch (e: any) {
if (e instanceof BitPayException) {
throw e;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ export class BitPayClient {
LoggerProvider.getLogger().logResponse(method, fullUrl, JSON.stringify(jsonObject));

return this.responseParser.getJsonDataFromJsonResponse(jsonObject);
} catch (e) {
} catch (e: any) {
if (e instanceof BitPayException) {
throw e;
}
Expand All @@ -200,17 +200,25 @@ export class BitPayClient {
* @param jsonData
* @throws BitPayApiExtension
*/
private getSignatureHeaders(fullUrl: string, headers: Record<string, string>, jsonData: string) {
private getSignatureHeaders(fullUrl: string, headers: Record<string, string>, jsonData: string | null) {
if (jsonData !== null) {
fullUrl = fullUrl + jsonData;
}

if (this.ecKey == null) {
BitPayExceptionProvider.throwGenericExceptionWithMessage('Missing ecKey');
throw new Error();
}

try {
headers['X-Signature'] = this.keyUtils.sign(fullUrl, this.ecKey);
} catch (e) {
} catch (e: any) {
BitPayExceptionProvider.throwGenericExceptionWithMessage('Wrong ecKey. ' + e.message);
}
headers['X-Identity'] = this.identity;

if (this.identity !== null) {
headers['X-Identity'] = this.identity;
}

return headers;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Client/CurrencyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class CurrencyClient {
* Retrieve the Currency Info
*
* @param currencyCode
* @returns Currency
* @returns CurrencyInterface|null
*/
public async getCurrencyInfo(currencyCode: string): Promise<CurrencyInterface> {
public async getCurrencyInfo(currencyCode: string): Promise<CurrencyInterface | null> {
let currencyInfo = null;

await this.bitPayClient.get('currencies', null, false).then((ratesData) => {
Expand Down
Loading

0 comments on commit e6a8dda

Please sign in to comment.