This is an JavaScript/TypeScript SDK for the exchangerate.host. The API responses are based on official API documentation
Because this SDK uses fetch both in Node and the Browser, and ESM, we require the following:
- Node 18.0.0 or higher
- A modern, version infinite, browser
The package contains both an ESM and CommonJS build, so you can use it in both Node and the Browser.
First install the dependencies:
yarn installTo run the app:
yarn run startCreating an instance of the SDK is easy
import {ExchangerateHostApi} from '@checksanity/exchangerate-host-api';
const api = ExchangerateHostApi.create();Each of these factory methods will return a ExchangerateHostApi instance, which you can use to
make
requests to the exchangerate.host API.
const result = await api.getLatest();
console.log(result);All of the constructors support a configuration object that lets you override the default behavior of the SDK.
const defaultConfig: SdkConfiguration = {
fetch: (req: RequestInfo | URL, init: RequestInit | undefined) =>
fetch(req, init),
url: '<your custom url>',
};As a general rule, this options should be overridden when you create your instance of the client, and you probably won't have to change most of them unless you have some very specific requirements.
You can provide the options like this, to any of the constructors or static initialization methods:
const opts = {
fetch: (req, init) => {
console.log("Fire custom fetch first");
return fetch(req, init);
}
}
const sdk = ExchangerateHostApi.create(opts);All the below examples are in TypeScript, but the same method signatures all apply to JavaScript - just without the Type information.
You can run the tests with yarn run test.