Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint . --ext .ts --fix",
"build": "rm -rf ./dist && tsc",
"build": "rimraf ./dist && tsc",
"dev": "ts-node ./src/index",
"test": "jest --runInBand --verbose --silent=false",
"doc-gen": "rm -rf ./dist && typedoc --out ./docs src/",
"doc-gen": "rimraf ./dist && typedoc --out ./docs src/",
"prepublish": "npm run build",
"publish-doc": "npm run doc-gen && gh-pages -d ./docs",
"prepare": "husky install",
Expand Down Expand Up @@ -46,6 +46,7 @@
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.0.1",
"rimraf": "^5.0.5",
"standard-version": "^9.5.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
Expand Down
23 changes: 23 additions & 0 deletions src/CoinGeckoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ describe("CoinGeckoClient test", () => {
})
);
});

describe("API BaseURL", ()=> {
it('baseURL should be equal to public api URL when demo key passed', () => {
const fixture = new CoinGeckoClient({},{apiKey: "random_demo_key", isDemo: true});
expect(fixture.baseURL).toEqual("https://api.coingecko.com/api/v3")
});

it('baseURL should be equal to public api URL when no key is passed', () => {
const fixture = new CoinGeckoClient({});
expect(fixture.baseURL).toEqual("https://api.coingecko.com/api/v3")
});

it('baseURL should be equal to pro api URL when pro key is passed', () => {
// isDemo passed explicitly
const fixture = new CoinGeckoClient({}, {apiKey: "random_pro_key", isDemo: false});
// isDemo not passed
const fixture2 = new CoinGeckoClient({}, {apiKey: "random_pro_key"});
expect(fixture.baseURL).toEqual("https://pro-api.coingecko.com/api/v3")
expect(fixture2.baseURL).toEqual("https://pro-api.coingecko.com/api/v3")
});
})

describe("Coins", () => {
it("/coins/list should successful", async () => {
const list = await client.coinList({ include_platform: true });
Expand Down Expand Up @@ -138,6 +160,7 @@ describe("CoinGeckoClient test", () => {
});
});
});

describe("Contract", () => {
it("/coins/{id}/contract/{contract_address} should successful", async () => {
const aave = await client.contract({
Expand Down
23 changes: 16 additions & 7 deletions src/CoinGeckoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Options,
HttpResponse,
SearchResponse,
ApiKeyOptions,
} from "./Interface";

/**
Expand All @@ -44,19 +45,28 @@ export class CoinGeckoClient {

baseURL: string;

apiKey?: string;
apiKeyParam?: {
x_cg_demo_api_key?: string,
x_cg_pro_api_key?: string
}

/**
* Constructor
* @param options the options passed for client library, at the moment only timeout are support
* @param apiKeyOptions options for the API key, including whether it's a demo key.
* *
*/
constructor(options?: Options, apiKey?: string) {
constructor(options?: Options, apiKeyOptions?: ApiKeyOptions) {
this.options = { ...this.options, ...options };
if (!apiKey) {
if (!apiKeyOptions || apiKeyOptions.isDemo) {
this.baseURL = CoinGeckoClient.API_V3_URL;
} else {
this.baseURL = CoinGeckoClient.PRO_API_V3_URL;
this.apiKey = apiKey;
}
if (apiKeyOptions){
this.apiKeyParam = apiKeyOptions.isDemo ? {
x_cg_demo_api_key: apiKeyOptions.apiKey
}: { x_cg_pro_api_key: apiKeyOptions.apiKey }
}
}

Expand Down Expand Up @@ -144,8 +154,8 @@ export class CoinGeckoClient {
action: API_ROUTES,
params: { [key: string]: any } = {}
): Promise<T> {
if (this.apiKey) {
params.x_cg_pro_api_key = this.apiKey;
if (this.apiKeyParam) {
params = {...params, ...this.apiKeyParam}
}
const qs = Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
Expand Down Expand Up @@ -229,7 +239,6 @@ valid values: true, false
ids?: string;
category?: "decentralized_finance_defi" | "stablecoins";
order?:
| "market_cap_desc"
| "gecko_desc"
| "gecko_asc"
| "market_cap_asc"
Expand Down
5 changes: 5 additions & 0 deletions src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ export interface Options {
extraHTTPSOptions?: RequestOptions
}

export interface ApiKeyOptions {
apiKey: string,
isDemo?: boolean
}

export interface HttpResponse<T> {
data: T,
statusCode: number,
Expand Down
Loading