Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Hyperwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export default class Hyperwallet {
* @param {string} config.password - The API password
* @param {string} [config.programToken] - The program token that is used for some API calls
* @param {string} [config.server=https://api.sandbox.hyperwallet.com] - The API server to connect to
* @param {Object} [config.superagentRequest]
*/
constructor({ username, password, programToken, server = "https://api.sandbox.hyperwallet.com" }) {
constructor({ username, password, programToken, server = "https://api.sandbox.hyperwallet.com", superagentRequest = null }) {
if (!username || !password) {
throw new Error("You need to specify your API username and password!");
}
Expand All @@ -26,7 +27,7 @@ export default class Hyperwallet {
* @type {ApiClient}
* @protected
*/
this.client = new ApiClient(username, password, server);
this.client = new ApiClient(username, password, server, superagentRequest);

/**
* The program token that is used for some API calls
Expand Down
14 changes: 10 additions & 4 deletions src/utils/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export default class ApiClient {
* @param {string} username - The API username
* @param {string} password - The API password
* @param {string} server - The API server to connect to
* @param {Object} superagentRequest - A custom superagent request object
*/
constructor(username, password, server) {
constructor(username, password, server, superagentRequest) {
/**
* The API username
*
Expand Down Expand Up @@ -56,6 +57,11 @@ export default class ApiClient {
* @protected
*/
this.version = packageJson.version;

/**
* The superAgent request object used to make the API calls
*/
this.request = superagentRequest || request;
}

/**
Expand All @@ -67,7 +73,7 @@ export default class ApiClient {
* @param {api-callback} callback - The callback for this call
*/
doPost(partialUrl, data, params, callback) {
request
this.request
.post(`${this.server}/rest/v3/${partialUrl}`)
.auth(this.username, this.password)
.set("User-Agent", `Hyperwallet Node SDK v${this.version}`)
Expand All @@ -87,7 +93,7 @@ export default class ApiClient {
* @param {api-callback} callback - The callback for this call
*/
doPut(partialUrl, data, params, callback) {
request
this.request
.put(`${this.server}/rest/v3/${partialUrl}`)
.auth(this.username, this.password)
.set("User-Agent", `Hyperwallet Node SDK v${this.version}`)
Expand All @@ -106,7 +112,7 @@ export default class ApiClient {
* @param {api-callback} callback - The callback for this call
*/
doGet(partialUrl, params, callback) {
request
this.request
.get(`${this.server}/rest/v3/${partialUrl}`)
.auth(this.username, this.password)
.set("User-Agent", `Hyperwallet Node SDK v${this.version}`)
Expand Down
12 changes: 12 additions & 0 deletions test/Hyperwallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ describe("Hyperwallet", () => {
client.client.server.should.be.equal("test-server");
});

/** @test {Hyperwallet#constructor} */
it("should initialize ApiClient with provided superagent request", () => {
const fakeRequest = { fake: true };
const client = new Hyperwallet({
username: "test-username",
password: "test-password",
superagentRequest: fakeRequest,
});

client.client.request.should.be.equal(fakeRequest);
});

/** @test {Hyperwallet#constructor} */
it("should throw error if username is missing", () => {
expect(() => new Hyperwallet({
Expand Down
7 changes: 7 additions & 0 deletions test/utils/ApiClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe("utils/ApiClient", () => {
const client = new ApiClient("test-username", "test-password", "test-server");
client.version.should.be.equal(packageJson.version);
});

/** @test {ApiClient#constructor} */
it("should be able to set the request with a custom one", () => {
const fakeRequest = { fake: true };
const client = new ApiClient("test-username", "test-password", "test-server", fakeRequest);
client.request.should.be.equal(fakeRequest);
});
});

/** @test {ApiClient#doPost} */
Expand Down