Skip to content

Commit

Permalink
Updating JSDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
erdimaden committed May 14, 2024
1 parent fc1202e commit c5da5d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
28 changes: 18 additions & 10 deletions src/coinbase/address.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { AxiosError } from "axios";
import { Address as AddressModel } from "../client";
import { InternalError } from "./errors";
import { FaucetTransaction } from "./faucet_transaction";
import { AddressClient } from "./types";
import { AddressAPIClient } from "./types";

/**
* Class representing an Address in the Coinbase SDK.
* A representation of a blockchain Address, which is a user-controlled account on a Network. Addresses are used to
* send and receive Assets, and should be created using Wallet#create_address. Addresses require an
* Eth::Key to sign transaction data.
*/
export class Address {
private model: AddressModel;
private client: AddressClient;
private client: AddressAPIClient;

/**
* Creates an instance of Address.
* Initializes a new Address instance.
* @param {AddressModel} model - The address model data.
* @param {AddressClient} client - The API client to interact with address-related endpoints.
* @param {AddressAPIClient} client - The API client to interact with address-related endpoints.
* @throws {InternalError} If the model or client is empty.
*/
constructor(model: AddressModel, client: AddressClient) {
constructor(model: AddressModel, client: AddressAPIClient) {
if (!model) {
throw new InternalError("Address model cannot be empty");
}
Expand All @@ -29,6 +33,7 @@ export class Address {

/**
* Requests faucet funds for the address.
* note: only supported on testnet networks
* @returns {Promise<FaucetTransaction>} The faucet transaction object.
* @throws {InternalError} If the request does not return a transaction hash.
* @throws {Error} If the request fails.
Expand All @@ -41,36 +46,39 @@ export class Address {
);
return new FaucetTransaction(response.data);
} catch (e) {
throw new Error(`Failed to request faucet funds`);
if (e instanceof AxiosError) {
throw e;
}
throw new Error(`Failed to complete faucet request`);
}
}

/**
* Gets the address ID.
* Returns the address ID.
* @returns {string} The address ID.
*/
public getId(): string {
return this.model.address_id;
}

/**
* Gets the network ID.
* Returns the network ID.
* @returns {string} The network ID.
*/
public getNetworkId(): string {
return this.model.network_id;
}

/**
* Gets the public key.
* Returns the public key.
* @returns {string} The public key.
*/
public getPublicKey(): string {
return this.model.public_key;
}

/**
* Gets the wallet ID.
* Returns the wallet ID.
* @returns {string} The wallet ID.
*/
public getWalletId(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/faucet_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class FaucetTransaction {
* @throws {InternalError} If the model does not exist.
*/
constructor(model: FaucetTransactionModel) {
if (!model) {
if (!model?.transaction_hash) {
throw new InternalError("FaucetTransaction model cannot be empty");
}
this.model = model;
Expand Down
13 changes: 11 additions & 2 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const VALID_ADDRESS_MODEL: AddressModel = {
describe("Address", () => {
const client = AddressesApiFactory();

afterEach(() => {
axiosMock.reset();
});

it("should create an Address instance", () => {
const address = new Address(VALID_ADDRESS_MODEL, client);
expect(address).toBeInstanceOf(Address);
Expand All @@ -44,11 +48,16 @@ describe("Address", () => {
expect(faucetTransaction).toBeInstanceOf(FaucetTransaction);
expect(faucetTransaction.getTransactionHash()).toBe("mocked_transaction_hash");
});
it("should request faucet funds and return a FaucetTransactionaaa", async () => {
axiosMock.onPost().reply(200, {});
const address = new Address(VALID_ADDRESS_MODEL, client);
await expect(address.faucet()).rejects.toThrow("Failed to complete faucet request");
});

it("should throw an error if faucet request fails", async () => {
it("should throw an AxiosError if faucet request fails", async () => {
axiosMock.onPost().reply(400);
const address = new Address(VALID_ADDRESS_MODEL, client);
await expect(address.faucet()).rejects.toThrow("Failed to request faucet funds");
await expect(address.faucet()).rejects.toThrow("Request failed with status code 400");
});

it("should return the correct string representation", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User as UserModel } from "./../client/api";
/**
* AddressAPI client type definition.
*/
export type AddressClient = {
export type AddressAPIClient = {
/**
* Requests faucet funds for the address.
* @param {string} walletId - The wallet ID.
Expand Down

0 comments on commit c5da5d2

Please sign in to comment.