cosmjs-util/packages/faucet-client/src/faucetclient.ts
Milan Steiner 2b6e30d48a
Fix LGTM Warnings (#1069)
* Removed unused variable

* Improve Regex
2022-03-02 18:26:57 +01:00

34 lines
844 B
TypeScript

import axios from "axios";
export class FaucetClient {
private readonly baseUrl: string;
public constructor(baseUrl: string) {
if (!baseUrl.match(/^https?:\/\//)) {
throw new Error("Expected base url to start with http:// or https://");
}
// Strip trailing /
const strippedBaseUrl = baseUrl.replace(/(\/+)$/, "");
this.baseUrl = strippedBaseUrl;
}
public async credit(address: string, denom: string): Promise<void> {
const body = {
address: address,
denom: denom,
};
try {
await axios.post(this.baseUrl + "/credit", body);
} catch (error: any) {
if (error.response) {
// append response body to error message
throw new Error(`${error}; response body: ${JSON.stringify(error.response.data)}`);
} else {
throw error;
}
}
}
}