Socket
Socket
Sign inDemoInstall

@fiatconnect/fiatconnect-sdk

Package Overview
Dependencies
Maintainers
5
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fiatconnect/fiatconnect-sdk - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

yarn-error.log

7

CHANGELOG.md

@@ -5,2 +5,9 @@ # Changelog

### [0.2.2](https://github.com/fiatconnect/fiatconnect-sdk/compare/v0.2.1...v0.2.2) (2022-06-09)
### Features
* **result:** Replace Result implementation, simplify errors ([#24](https://github.com/fiatconnect/fiatconnect-sdk/issues/24)) ([0b1a154](https://github.com/fiatconnect/fiatconnect-sdk/commit/0b1a1549f0e7a895367c17683ceb62e4f5f49680))
### [0.2.1](https://github.com/fiatconnect/fiatconnect-sdk/compare/v0.2.0...v0.2.1) (2022-05-23)

@@ -7,0 +14,0 @@

38

dist/fiat-connect-client.d.ts

@@ -1,4 +0,4 @@

import { AddFiatAccountResponse, DeleteFiatAccountRequestParams, GetFiatAccountsResponse, KycRequestParams, KycStatusResponse, QuoteErrorResponse, QuoteRequestQuery, QuoteResponse, TransferResponse, TransferStatusRequestParams, TransferStatusResponse, ClockResponse } from '@fiatconnect/fiatconnect-types';
import { Result } from 'ts-results';
import { AddFiatAccountParams, AddKycParams, ErrorResponse, FiatConnectApiClient, FiatConnectClientConfig, TransferRequestParams, ClockDiffParams, ClockDiffResult } from './types';
import { AddFiatAccountResponse, DeleteFiatAccountRequestParams, GetFiatAccountsResponse, KycRequestParams, KycStatusResponse, QuoteRequestQuery, QuoteResponse, TransferResponse, TransferStatusRequestParams, TransferStatusResponse, ClockResponse } from '@fiatconnect/fiatconnect-types';
import { Result } from '@badrap/result';
import { AddFiatAccountParams, AddKycParams, ResponseError, FiatConnectApiClient, FiatConnectClientConfig, TransferRequestParams, ClockDiffParams, ClockDiffResult } from './types';
export declare class FiatConnectClient implements FiatConnectApiClient {

@@ -25,4 +25,4 @@ config: FiatConnectClientConfig;

*/
login(): Promise<Result<'success', ErrorResponse>>;
_getQuote(params: QuoteRequestQuery, inOrOut: 'in' | 'out'): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>;
login(): Promise<Result<'success', ResponseError>>;
_getQuote(params: QuoteRequestQuery, inOrOut: 'in' | 'out'): Promise<Result<QuoteResponse, ResponseError>>;
/**

@@ -39,51 +39,51 @@ * https://en.wikipedia.org/wiki/Network_Time_Protocol#Clock_synchronization_algorithm

*/
getClockDiffApprox(): Promise<Result<ClockDiffResult, ErrorResponse>>;
getClockDiffApprox(): Promise<Result<ClockDiffResult, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#321-get-clock
*/
getClock(): Promise<Result<ClockResponse, ErrorResponse>>;
getClock(): Promise<Result<ClockResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3311-get-quotein
*/
getQuoteIn(params: QuoteRequestQuery): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>;
getQuoteIn(params: QuoteRequestQuery): Promise<Result<QuoteResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3312-get-quoteout
*/
getQuoteOut(params: QuoteRequestQuery): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>;
getQuoteOut(params: QuoteRequestQuery): Promise<Result<QuoteResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3321-post-kyckycschema
*/
addKyc(params: AddKycParams): Promise<Result<KycStatusResponse, ErrorResponse>>;
addKyc(params: AddKycParams): Promise<Result<KycStatusResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3323-delete-kyckycschema
*/
deleteKyc(params: KycRequestParams): Promise<Result<void, ErrorResponse>>;
deleteKyc(params: KycRequestParams): Promise<Result<void, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3322-get-kyckycschemastatus
*/
getKycStatus(params: KycRequestParams): Promise<Result<KycStatusResponse, ErrorResponse>>;
getKycStatus(params: KycRequestParams): Promise<Result<KycStatusResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3331-post-accountsfiataccountschema
*/
addFiatAccount(params: AddFiatAccountParams): Promise<Result<AddFiatAccountResponse, ErrorResponse>>;
addFiatAccount(params: AddFiatAccountParams): Promise<Result<AddFiatAccountResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3332-get-accounts
*/
getFiatAccounts(): Promise<Result<GetFiatAccountsResponse, ErrorResponse>>;
getFiatAccounts(): Promise<Result<GetFiatAccountsResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3333-delete-accountfiataccountid
*/
deleteFiatAccount(params: DeleteFiatAccountRequestParams): Promise<Result<void, ErrorResponse>>;
deleteFiatAccount(params: DeleteFiatAccountRequestParams): Promise<Result<void, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3341-post-transferin
*/
transferIn(params: TransferRequestParams): Promise<Result<TransferResponse, ErrorResponse>>;
transferIn(params: TransferRequestParams): Promise<Result<TransferResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3342-post-transferout
*/
transferOut(params: TransferRequestParams): Promise<Result<TransferResponse, ErrorResponse>>;
transferOut(params: TransferRequestParams): Promise<Result<TransferResponse, ResponseError>>;
/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3343-get-transfertransferidstatus
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3443-get-transfertransferidstatus
*/
getTransferStatus(params: TransferStatusRequestParams): Promise<Result<TransferStatusResponse, ErrorResponse>>;
getTransferStatus(params: TransferStatusRequestParams): Promise<Result<TransferStatusResponse, ResponseError>>;
}

@@ -9,3 +9,4 @@ "use strict";

const siwe_1 = require("siwe");
const ts_results_1 = require("ts-results");
const result_1 = require("@badrap/result");
const types_1 = require("./types");
const NETWORK_CHAIN_IDS = {

@@ -32,4 +33,4 @@ [fiatconnect_types_1.Network.Alfajores]: 44787,

const loginResult = await this.login();
if (!loginResult.ok) {
throw new Error(`Login failed: ${loginResult.val.error}`);
if (loginResult.isErr) {
throw loginResult.error;
}

@@ -77,6 +78,6 @@ }

const data = await response.json();
return (0, ts_results_1.Err)(data);
return handleError(data);
}
this._sessionExpiry = expirationDate;
return (0, ts_results_1.Ok)('success');
return result_1.Result.ok('success');
}

@@ -96,5 +97,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -125,9 +126,9 @@ catch (error) {

const t3 = Date.now();
if (!clockResponse.ok) {
return clockResponse;
if (!clockResponse.isOk) {
return result_1.Result.err(clockResponse.error);
}
const t1 = new Date(clockResponse.val.time).getTime();
const t1 = new Date(clockResponse.value.time).getTime();
// We can assume that t1 and t2 are sufficiently close to each other
const t2 = t1;
return (0, ts_results_1.Ok)(this._calculateClockDiff({ t0, t1, t2, t3 }));
return result_1.Result.ok(this._calculateClockDiff({ t0, t1, t2, t3 }));
}

@@ -145,5 +146,5 @@ /**

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -179,5 +180,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -200,5 +201,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(undefined);
return result_1.Result.ok(undefined);
}

@@ -221,5 +222,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -243,5 +244,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -264,5 +265,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -285,5 +286,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(undefined);
return result_1.Result.ok(undefined);
}

@@ -307,5 +308,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -329,5 +330,5 @@ catch (error) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -339,3 +340,3 @@ catch (error) {

/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3343-get-transfertransferidstatus
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3443-get-transfertransferidstatus
*/

@@ -351,5 +352,5 @@ async getTransferStatus(params) {

if (!response.ok) {
return (0, ts_results_1.Err)(data);
return handleError(data);
}
return (0, ts_results_1.Ok)(data);
return result_1.Result.ok(data);
}

@@ -362,8 +363,28 @@ catch (error) {

exports.FiatConnectClient = FiatConnectClient;
/**
* handleError accepts three types of inputs:
* * a ResponseError object
* * a built-in Error object
* * A JSON payload from a non-200 FiatConnect API call
*
* handleError converts all of these into ResponseError objects wrapped
* in a Result.err. If handleError is given data of a type not listed above,
* it attempts to cast it to a string, put it in a ResponseError object, and
* wrap it in a Result.err.
**/
function handleError(error) {
if (error instanceof Error) {
return (0, ts_results_1.Err)(Object.assign(Object.assign({}, error), { error: error.message }));
// TODO: expose trace to make these errors more useful for clients
if (error instanceof types_1.ResponseError) {
return result_1.Result.err(error);
}
return (0, ts_results_1.Err)({ error: String(error) });
else if (error instanceof Error) {
return result_1.Result.err(new types_1.ResponseError(error.message));
}
else if (error instanceof Object) {
// We cast to QuoteErrorResponse here since it is a strict superset of all other
// error response objects, allowing us to access all possible error-related fields.
return result_1.Result.err(new types_1.ResponseError('FiatConnect API Error', error));
}
return result_1.Result.err(new types_1.ResponseError(String(error)));
}
//# sourceMappingURL=fiat-connect-client.js.map

@@ -1,22 +0,22 @@

import { AccountNumber, AddFiatAccountResponse, DeleteFiatAccountRequestParams, FiatAccountSchema, FiatConnectError, GetFiatAccountsResponse, KycRequestParams, KycSchema, KycStatusResponse, Network, PersonalDataAndDocumentsKyc, QuoteErrorResponse, QuoteRequestQuery, QuoteResponse, TransferRequestBody, TransferResponse, TransferStatusRequestParams, TransferStatusResponse, ClockResponse } from '@fiatconnect/fiatconnect-types';
import { Result } from 'ts-results';
import { AccountNumber, DuniaWallet, MobileMoney, AddFiatAccountResponse, DeleteFiatAccountRequestParams, FiatAccountSchema, FiatConnectError, GetFiatAccountsResponse, KycRequestParams, KycSchema, KycStatusResponse, Network, PersonalDataAndDocumentsKyc, QuoteErrorResponse, QuoteRequestQuery, QuoteResponse, TransferRequestBody, TransferResponse, TransferStatusRequestParams, TransferStatusResponse, ClockResponse } from '@fiatconnect/fiatconnect-types';
import { Result } from '@badrap/result';
export interface FiatConnectApiClient {
getClockDiffApprox(): Promise<Result<ClockDiffResult, ErrorResponse>>;
getClock(): Promise<Result<ClockResponse, ErrorResponse>>;
login(): Promise<Result<'success', ErrorResponse>>;
getClockDiffApprox(): Promise<Result<ClockDiffResult, ResponseError>>;
getClock(): Promise<Result<ClockResponse, ResponseError>>;
login(): Promise<Result<'success', ResponseError>>;
isLoggedIn(): boolean;
getQuoteIn(params: QuoteRequestQuery, jwt: string): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>;
getQuoteOut(params: QuoteRequestQuery, jwt: string): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>;
addKyc(params: AddKycParams, jwt: string): Promise<Result<KycStatusResponse, ErrorResponse>>;
deleteKyc(params: KycRequestParams, jwt: string): Promise<Result<void, ErrorResponse>>;
getKycStatus(params: KycRequestParams, jwt: string): Promise<Result<KycStatusResponse, ErrorResponse>>;
addFiatAccount(params: AddFiatAccountParams, jwt: string): Promise<Result<AddFiatAccountResponse, ErrorResponse>>;
getFiatAccounts(jwt: string): Promise<Result<GetFiatAccountsResponse, ErrorResponse>>;
deleteFiatAccount(params: DeleteFiatAccountRequestParams, jwt: string): Promise<Result<void, ErrorResponse>>;
transferIn(params: TransferRequestParams, jwt: string): Promise<Result<TransferResponse, ErrorResponse>>;
transferOut(params: TransferRequestParams, jwt: string): Promise<Result<TransferResponse, ErrorResponse>>;
getTransferStatus(params: TransferStatusRequestParams, jwt: string): Promise<Result<TransferStatusResponse, ErrorResponse>>;
getQuoteIn(params: QuoteRequestQuery, jwt: string): Promise<Result<QuoteResponse, ResponseError>>;
getQuoteOut(params: QuoteRequestQuery, jwt: string): Promise<Result<QuoteResponse, ResponseError>>;
addKyc(params: AddKycParams, jwt: string): Promise<Result<KycStatusResponse, ResponseError>>;
deleteKyc(params: KycRequestParams, jwt: string): Promise<Result<void, ResponseError>>;
getKycStatus(params: KycRequestParams, jwt: string): Promise<Result<KycStatusResponse, ResponseError>>;
addFiatAccount(params: AddFiatAccountParams, jwt: string): Promise<Result<AddFiatAccountResponse, ResponseError>>;
getFiatAccounts(jwt: string): Promise<Result<GetFiatAccountsResponse, ResponseError>>;
deleteFiatAccount(params: DeleteFiatAccountRequestParams, jwt: string): Promise<Result<void, ResponseError>>;
transferIn(params: TransferRequestParams, jwt: string): Promise<Result<TransferResponse, ResponseError>>;
transferOut(params: TransferRequestParams, jwt: string): Promise<Result<TransferResponse, ResponseError>>;
getTransferStatus(params: TransferStatusRequestParams, jwt: string): Promise<Result<TransferStatusResponse, ResponseError>>;
}
export declare type KycSchemaData = PersonalDataAndDocumentsKyc;
export declare type FiatAccountSchemaData = AccountNumber;
export declare type FiatAccountSchemaData = AccountNumber | MobileMoney | DuniaWallet;
export interface AddKycParams {

@@ -38,5 +38,2 @@ kycSchemaName: KycSchema;

}
export interface ErrorResponse {
error: FiatConnectError | string;
}
export interface TransferRequestParams {

@@ -56,1 +53,9 @@ idempotencyKey: string;

}
export declare class ResponseError extends Error {
fiatConnectError?: FiatConnectError;
minimumFiatAmount?: string;
maximumFiatAmount?: string;
minimumCryptoAmount?: string;
maximumCryptoAmount?: string;
constructor(message: string, data?: QuoteErrorResponse);
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResponseError = void 0;
// ResponseError is an error object that can act as a general error
// as well as contain details about a FiatConnect-specific error from
// an API.
class ResponseError extends Error {
// Because QuoteErrorResponse contains the `error` field (the only field returned
// by all other endpoints on error) as well as additional quote-specific error
// fields, we use it as the data type here.
constructor(message, data) {
super(message);
Object.setPrototypeOf(this, ResponseError.prototype);
this.fiatConnectError = data === null || data === void 0 ? void 0 : data.error;
this.minimumFiatAmount = data === null || data === void 0 ? void 0 : data.minimumFiatAmount;
this.maximumFiatAmount = data === null || data === void 0 ? void 0 : data.maximumFiatAmount;
this.minimumCryptoAmount = data === null || data === void 0 ? void 0 : data.minimumCryptoAmount;
this.maximumCryptoAmount = data === null || data === void 0 ? void 0 : data.maximumCryptoAmount;
}
}
exports.ResponseError = ResponseError;
//# sourceMappingURL=types.js.map
{
"name": "@fiatconnect/fiatconnect-sdk",
"version": "0.2.1",
"version": "0.2.2",
"description": "A helper libary for wallets to integrate with FiatConnect APIs",

@@ -51,3 +51,4 @@ "scripts": {

"dependencies": {
"@fiatconnect/fiatconnect-types": "^3.2.0",
"@badrap/result": "^0.2.12",
"@fiatconnect/fiatconnect-types": "^4.0.0",
"ethers": "^5.6.4",

@@ -57,5 +58,4 @@ "fetch-cookie": "^2.0.3",

"siwe": "^1.1.6",
"ts-results": "^3.3.0",
"tslib": "^2.4.0"
}
}

@@ -20,7 +20,7 @@ import {

import { generateNonce, SiweMessage } from 'siwe'
import { Ok, Err, Result } from 'ts-results'
import { Result } from '@badrap/result'
import {
AddFiatAccountParams,
AddKycParams,
ErrorResponse,
ResponseError,
FiatConnectApiClient,

@@ -65,4 +65,4 @@ FiatConnectClientConfig,

const loginResult = await this.login()
if (!loginResult.ok) {
throw new Error(`Login failed: ${loginResult.val.error}`)
if (loginResult.isErr) {
throw loginResult.error
}

@@ -86,3 +86,3 @@ }

*/
async login(): Promise<Result<'success', ErrorResponse>> {
async login(): Promise<Result<'success', ResponseError>> {
try {

@@ -118,7 +118,7 @@ const expirationDate = new Date(Date.now() + SESSION_DURATION_MS)

const data = await response.json()
return Err(data as ErrorResponse)
return handleError(data)
}
this._sessionExpiry = expirationDate
return Ok('success')
return Result.ok('success')
} catch (error) {

@@ -132,3 +132,3 @@ return handleError(error)

inOrOut: 'in' | 'out',
): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>> {
): Promise<Result<QuoteResponse, ResponseError>> {
try {

@@ -145,5 +145,5 @@ const queryParams = new URLSearchParams(params).toString()

if (!response.ok) {
return Err(data as QuoteErrorResponse)
return handleError(data)
}
return Ok(data as QuoteResponse)
return Result.ok(data as QuoteResponse)
} catch (error) {

@@ -171,3 +171,3 @@ return handleError(error)

*/
async getClockDiffApprox(): Promise<Result<ClockDiffResult, ErrorResponse>> {
async getClockDiffApprox(): Promise<Result<ClockDiffResult, ResponseError>> {
const t0 = Date.now()

@@ -177,10 +177,10 @@ const clockResponse = await this.getClock()

if (!clockResponse.ok) {
return clockResponse
if (!clockResponse.isOk) {
return Result.err(clockResponse.error)
}
const t1 = new Date(clockResponse.val.time).getTime()
const t1 = new Date(clockResponse.value.time).getTime()
// We can assume that t1 and t2 are sufficiently close to each other
const t2 = t1
return Ok(this._calculateClockDiff({ t0, t1, t2, t3 }))
return Result.ok(this._calculateClockDiff({ t0, t1, t2, t3 }))
}

@@ -191,3 +191,3 @@

*/
async getClock(): Promise<Result<ClockResponse, ErrorResponse>> {
async getClock(): Promise<Result<ClockResponse, ResponseError>> {
try {

@@ -200,5 +200,5 @@ const response = await fetch(`${this.config.baseUrl}/clock`, {

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -214,3 +214,3 @@ return handleError(error)

params: QuoteRequestQuery,
): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>> {
): Promise<Result<QuoteResponse, ResponseError>> {
return this._getQuote(params, 'in')

@@ -224,3 +224,3 @@ }

params: QuoteRequestQuery,
): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>> {
): Promise<Result<QuoteResponse, ResponseError>> {
return this._getQuote(params, 'out')

@@ -234,3 +234,3 @@ }

params: AddKycParams,
): Promise<Result<KycStatusResponse, ErrorResponse>> {
): Promise<Result<KycStatusResponse, ResponseError>> {
try {

@@ -251,5 +251,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -265,3 +265,3 @@ return handleError(error)

params: KycRequestParams,
): Promise<Result<void, ErrorResponse>> {
): Promise<Result<void, ResponseError>> {
try {

@@ -278,5 +278,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(undefined)
return Result.ok(undefined)
} catch (error) {

@@ -292,3 +292,3 @@ return handleError(error)

params: KycRequestParams,
): Promise<Result<KycStatusResponse, ErrorResponse>> {
): Promise<Result<KycStatusResponse, ResponseError>> {
try {

@@ -305,5 +305,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -319,3 +319,3 @@ return handleError(error)

params: AddFiatAccountParams,
): Promise<Result<AddFiatAccountResponse, ErrorResponse>> {
): Promise<Result<AddFiatAccountResponse, ResponseError>> {
try {

@@ -336,5 +336,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -349,3 +349,3 @@ return handleError(error)

async getFiatAccounts(): Promise<
Result<GetFiatAccountsResponse, ErrorResponse>
Result<GetFiatAccountsResponse, ResponseError>
> {

@@ -360,5 +360,5 @@ try {

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -374,3 +374,3 @@ return handleError(error)

params: DeleteFiatAccountRequestParams,
): Promise<Result<void, ErrorResponse>> {
): Promise<Result<void, ResponseError>> {
try {

@@ -387,5 +387,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(undefined)
return Result.ok(undefined)
} catch (error) {

@@ -401,3 +401,3 @@ return handleError(error)

params: TransferRequestParams,
): Promise<Result<TransferResponse, ErrorResponse>> {
): Promise<Result<TransferResponse, ResponseError>> {
try {

@@ -416,5 +416,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -430,3 +430,3 @@ return handleError(error)

params: TransferRequestParams,
): Promise<Result<TransferResponse, ErrorResponse>> {
): Promise<Result<TransferResponse, ResponseError>> {
try {

@@ -445,5 +445,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -455,7 +455,7 @@ return handleError(error)

/**
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3343-get-transfertransferidstatus
* https://github.com/fiatconnect/specification/blob/main/fiatconnect-api.md#3443-get-transfertransferidstatus
*/
async getTransferStatus(
params: TransferStatusRequestParams,
): Promise<Result<TransferStatusResponse, ErrorResponse>> {
): Promise<Result<TransferStatusResponse, ResponseError>> {
try {

@@ -472,5 +472,5 @@ await this._ensureLogin()

if (!response.ok) {
return Err(data)
return handleError(data)
}
return Ok(data)
return Result.ok(data)
} catch (error) {

@@ -482,7 +482,27 @@ return handleError(error)

function handleError(error: unknown): Err<ErrorResponse> {
if (error instanceof Error) {
return Err({ ...error, error: error.message })
/**
* handleError accepts three types of inputs:
* * a ResponseError object
* * a built-in Error object
* * A JSON payload from a non-200 FiatConnect API call
*
* handleError converts all of these into ResponseError objects wrapped
* in a Result.err. If handleError is given data of a type not listed above,
* it attempts to cast it to a string, put it in a ResponseError object, and
* wrap it in a Result.err.
**/
function handleError<T>(error: unknown): Result<T, ResponseError> {
// TODO: expose trace to make these errors more useful for clients
if (error instanceof ResponseError) {
return Result.err(error)
} else if (error instanceof Error) {
return Result.err(new ResponseError(error.message))
} else if (error instanceof Object) {
// We cast to QuoteErrorResponse here since it is a strict superset of all other
// error response objects, allowing us to access all possible error-related fields.
return Result.err(
new ResponseError('FiatConnect API Error', error as QuoteErrorResponse),
)
}
return Err({ error: String(error) })
return Result.err(new ResponseError(String(error)))
}
import {
AccountNumber,
DuniaWallet,
MobileMoney,
AddFiatAccountResponse,

@@ -22,8 +24,8 @@ DeleteFiatAccountRequestParams,

} from '@fiatconnect/fiatconnect-types'
import { Result } from 'ts-results'
import { Result } from '@badrap/result'
export interface FiatConnectApiClient {
getClockDiffApprox(): Promise<Result<ClockDiffResult, ErrorResponse>>
getClock(): Promise<Result<ClockResponse, ErrorResponse>>
login(): Promise<Result<'success', ErrorResponse>>
getClockDiffApprox(): Promise<Result<ClockDiffResult, ResponseError>>
getClock(): Promise<Result<ClockResponse, ResponseError>>
login(): Promise<Result<'success', ResponseError>>
isLoggedIn(): boolean

@@ -33,42 +35,42 @@ getQuoteIn(

jwt: string,
): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>
): Promise<Result<QuoteResponse, ResponseError>>
getQuoteOut(
params: QuoteRequestQuery,
jwt: string,
): Promise<Result<QuoteResponse, QuoteErrorResponse | ErrorResponse>>
): Promise<Result<QuoteResponse, ResponseError>>
addKyc(
params: AddKycParams,
jwt: string,
): Promise<Result<KycStatusResponse, ErrorResponse>>
): Promise<Result<KycStatusResponse, ResponseError>>
deleteKyc(
params: KycRequestParams,
jwt: string,
): Promise<Result<void, ErrorResponse>>
): Promise<Result<void, ResponseError>>
getKycStatus(
params: KycRequestParams,
jwt: string,
): Promise<Result<KycStatusResponse, ErrorResponse>>
): Promise<Result<KycStatusResponse, ResponseError>>
addFiatAccount(
params: AddFiatAccountParams,
jwt: string,
): Promise<Result<AddFiatAccountResponse, ErrorResponse>>
): Promise<Result<AddFiatAccountResponse, ResponseError>>
getFiatAccounts(
jwt: string,
): Promise<Result<GetFiatAccountsResponse, ErrorResponse>>
): Promise<Result<GetFiatAccountsResponse, ResponseError>>
deleteFiatAccount(
params: DeleteFiatAccountRequestParams,
jwt: string,
): Promise<Result<void, ErrorResponse>>
): Promise<Result<void, ResponseError>>
transferIn(
params: TransferRequestParams,
jwt: string,
): Promise<Result<TransferResponse, ErrorResponse>>
): Promise<Result<TransferResponse, ResponseError>>
transferOut(
params: TransferRequestParams,
jwt: string,
): Promise<Result<TransferResponse, ErrorResponse>>
): Promise<Result<TransferResponse, ResponseError>>
getTransferStatus(
params: TransferStatusRequestParams,
jwt: string,
): Promise<Result<TransferStatusResponse, ErrorResponse>>
): Promise<Result<TransferStatusResponse, ResponseError>>
}

@@ -78,3 +80,3 @@

export type KycSchemaData = PersonalDataAndDocumentsKyc // in the future this will be the union of all KYC schema types (currently there is just one)
export type FiatAccountSchemaData = AccountNumber // similarly, this will be the union of all fiat account schema types
export type FiatAccountSchemaData = AccountNumber | MobileMoney | DuniaWallet // similarly, this will be the union of all fiat account schema types

@@ -100,6 +102,2 @@ export interface AddKycParams {

export interface ErrorResponse {
error: FiatConnectError | string
}
export interface TransferRequestParams {

@@ -121,1 +119,27 @@ idempotencyKey: string

}
// ResponseError is an error object that can act as a general error
// as well as contain details about a FiatConnect-specific error from
// an API.
export class ResponseError extends Error {
fiatConnectError?: FiatConnectError
minimumFiatAmount?: string
maximumFiatAmount?: string
minimumCryptoAmount?: string
maximumCryptoAmount?: string
// Because QuoteErrorResponse contains the `error` field (the only field returned
// by all other endpoints on error) as well as additional quote-specific error
// fields, we use it as the data type here.
constructor(message: string, data?: QuoteErrorResponse) {
super(message)
Object.setPrototypeOf(this, ResponseError.prototype)
this.fiatConnectError = data?.error
this.minimumFiatAmount = data?.minimumFiatAmount
this.maximumFiatAmount = data?.maximumFiatAmount
this.minimumCryptoAmount = data?.minimumCryptoAmount
this.maximumCryptoAmount = data?.maximumCryptoAmount
}
}

@@ -1,2 +0,2 @@

import { FiatConnectClient } from '../src/index'
import { FiatConnectClient, ResponseError } from '../src/index'
import {

@@ -26,5 +26,5 @@ mockAddFiatAccountResponse,

import * as siwe from 'siwe'
import { Err, Ok } from 'ts-results'
import { Result } from '@badrap/result'
// Work around from
// work around from
// https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-968853688 for

@@ -78,4 +78,4 @@ // mocking siwe packages

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockClockResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockClockResponse)
expect(getHeadersMock).toHaveBeenCalled()

@@ -87,6 +87,6 @@ })

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message', undefined),
)
})

@@ -126,3 +126,3 @@ })

.spyOn(client, 'getClock')
.mockResolvedValueOnce(Ok(mockClockResponse))
.mockResolvedValueOnce(Result.ok(mockClockResponse))
const expectedClockDiffResult = {

@@ -137,4 +137,4 @@ diff: 1000,

const actualClockDiffResult = await client.getClockDiffApprox()
expect(actualClockDiffResult.ok).toBeTruthy()
expect(actualClockDiffResult.val).toEqual(expectedClockDiffResult)
expect(actualClockDiffResult.isOk).toBeTruthy()
expect(actualClockDiffResult.unwrap()).toEqual(expectedClockDiffResult)
expect(client._calculateClockDiff).toHaveBeenCalledWith({

@@ -151,6 +151,6 @@ t0,

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -188,4 +188,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toEqual('success')
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toEqual('success')
expect(getHeadersMock).toHaveBeenCalled()

@@ -226,12 +226,18 @@ })

)
expect(response.ok).toBeFalsy()
expect(response.val).toEqual({ error: 'InvalidParameters' })
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', {
error: FiatConnectError.InvalidParameters,
}),
)
expect(getHeadersMock).toHaveBeenCalled()
})
it('returns error if login throws', async () => {
signingFunction.mockRejectedValueOnce('sign error')
signingFunction.mockRejectedValueOnce(new Error('some error'))
const response = await client.login()
expect(response.ok).toBeFalsy()
expect(response.val).toEqual({ error: 'sign error' })
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('some error'),
)
expect(getHeadersMock).not.toHaveBeenCalled()

@@ -261,3 +267,3 @@ })

client._sessionExpiry = new Date('2022-04-30T23:00:00Z')
mockLogin.mockResolvedValueOnce(Ok('success'))
mockLogin.mockResolvedValueOnce(Result.ok('success'))
await client._ensureLogin()

@@ -272,6 +278,8 @@ expect(mockLogin).toHaveBeenCalledTimes(1)

it('throws error if login fails', async () => {
mockLogin.mockResolvedValueOnce(Err({ error: 'invalid login' }))
mockLogin.mockResolvedValueOnce(
Result.err(new ResponseError('some error')),
)
await expect(async () => {
await client._ensureLogin()
}).rejects.toThrow('Login failed: invalid login')
}).rejects.toThrow(new ResponseError('some error'))
expect(mockLogin).toHaveBeenCalledTimes(1)

@@ -309,4 +317,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockQuoteResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockQuoteResponse)
expect(getHeadersMock).toHaveBeenCalled()

@@ -320,4 +328,6 @@ })

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(mockQuoteErrorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', mockQuoteErrorResponse),
)
})

@@ -328,6 +338,6 @@ it('handles fetch errors', async () => {

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -343,4 +353,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockQuoteResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockQuoteResponse)
expect(getHeadersMock).toHaveBeenCalled()

@@ -354,4 +364,6 @@ })

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(mockQuoteErrorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', mockQuoteErrorResponse),
)
})

@@ -362,6 +374,6 @@ it('handles fetch errors', async () => {

expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -386,4 +398,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockKycStatusResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockKycStatusResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -409,4 +421,4 @@ expect(getHeadersMock).toHaveBeenCalled()

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockKycStatusResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockKycStatusResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -416,4 +428,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceExists }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceExists }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 409,

@@ -425,4 +437,6 @@ })

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -435,6 +449,6 @@ it('handles fetch errors', async () => {

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -455,4 +469,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toBeUndefined()
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toBeUndefined()
expect(client._ensureLogin).toHaveBeenCalled()

@@ -462,4 +476,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,

@@ -470,4 +484,6 @@ })

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -479,6 +495,6 @@ it('handles fetch errors', async () => {

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -499,4 +515,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockKycStatusResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockKycStatusResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -506,4 +522,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,

@@ -514,4 +530,6 @@ })

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -523,6 +541,6 @@ it('handles fetch errors', async () => {

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -547,4 +565,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockAddFiatAccountResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockAddFiatAccountResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -570,4 +588,4 @@ expect(getHeadersMock).toHaveBeenCalled()

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockAddFiatAccountResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockAddFiatAccountResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -577,4 +595,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceExists }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceExists }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 409,

@@ -586,4 +604,6 @@ })

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -596,6 +616,6 @@ it('handles fetch errors', async () => {

})
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -614,4 +634,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockGetFiatAccountsResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockGetFiatAccountsResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -621,9 +641,11 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,
})
const response = await client.getFiatAccounts()
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -633,6 +655,6 @@ it('handles fetch errors', async () => {

const response = await client.getFiatAccounts()
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -653,4 +675,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toBeUndefined()
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toBeUndefined()
expect(client._ensureLogin).toHaveBeenCalled()

@@ -660,4 +682,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,

@@ -668,4 +690,6 @@ })

)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -677,6 +701,6 @@ it('handles fetch errors', async () => {

)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -700,4 +724,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockTransferResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockTransferResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -720,4 +744,4 @@ expect(getHeadersMock).toHaveBeenCalled()

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockTransferResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockTransferResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -727,9 +751,11 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,
})
const response = await client.transferIn(mockTransferRequestParams)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -739,6 +765,6 @@ it('handles fetch errors', async () => {

const response = await client.transferIn(mockTransferRequestParams)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -763,4 +789,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockTransferResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockTransferResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -783,4 +809,4 @@ expect(getHeadersMock).toHaveBeenCalled()

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockTransferResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockTransferResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -790,9 +816,11 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,
})
const response = await client.transferOut(mockTransferRequestParams)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -802,6 +830,6 @@ it('handles fetch errors', async () => {

const response = await client.transferOut(mockTransferRequestParams)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})

@@ -823,4 +851,4 @@ })

)
expect(response.ok).toBeTruthy()
expect(response.val).toMatchObject(mockTransferStatusResponse)
expect(response.isOk).toBeTruthy()
expect(response.unwrap()).toMatchObject(mockTransferStatusResponse)
expect(client._ensureLogin).toHaveBeenCalled()

@@ -830,4 +858,4 @@ expect(getHeadersMock).toHaveBeenCalled()

it('handles API errors', async () => {
const errorResponse = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorResponse), {
const errorData = { error: FiatConnectError.ResourceNotFound }
fetchMock.mockResponseOnce(JSON.stringify(errorData), {
status: 404,

@@ -838,4 +866,6 @@ })

)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject(errorResponse)
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('FiatConnect API Error', errorData),
)
})

@@ -847,8 +877,8 @@ it('handles fetch errors', async () => {

)
expect(response.ok).toBeFalsy()
expect(response.val).toMatchObject({
error: 'fake error message',
})
expect(response.isOk).toBeFalsy()
expect(response.unwrap.bind(response)).toThrow(
new ResponseError('fake error message'),
)
})
})
})

@@ -120,5 +120,2 @@ import {

data: {
fiatType: FiatType.USD,
cryptoType: CryptoType.cUSD,
amount: '5.0',
fiatAccountId: '12358',

@@ -131,3 +128,3 @@ quoteId: 'mock_quote_id',

transferId: '82938',
transferStatus: TransferStatus.TransferPending,
transferStatus: TransferStatus.TransferReadyForUserToSendCryptoFunds,
transferAddress: '0xCC6DDE7638B2409e120e915adD948069CA619e10',

@@ -148,2 +145,4 @@ }

fiatAccountId: '12358',
transferId: '82938',
transferAddress: '0xCC6DDE7638B2409e120e915adD948069CA619e10',
}

@@ -150,0 +149,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc