From ee93bd6e51eabcf3507a77d21c5bdc6fe93d5d1c Mon Sep 17 00:00:00 2001 From: Janek Rahrt Date: Tue, 8 Feb 2022 09:59:48 +0100 Subject: [PATCH] fix: review comments --- .eslintrc | 1 + src/provider/default.ts | 65 ++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/.eslintrc b/.eslintrc index f35c2e43d..8e2e4d8bd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -18,6 +18,7 @@ }, "plugins": ["@typescript-eslint"], "rules": { + "class-methods-use-this": 0, "import/prefer-default-export": 0, "@typescript-eslint/naming-convention": 0, "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }] diff --git a/src/provider/default.ts b/src/provider/default.ts index 71b882189..137ba45d7 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -1,4 +1,4 @@ -import axios from 'axios'; +import axios, { AxiosRequestHeaders } from 'axios'; import urljoin from 'url-join'; import { @@ -79,34 +79,65 @@ export class Provider implements ProviderInterface { } } + private getFetchUrl(endpoint: keyof Endpoints) { + const gatewayUrlEndpoints = ['add_transaction']; + + return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl; + } + + private getFetchMethod(endpoint: keyof Endpoints) { + const postMethodEndpoints = ['add_transaction', 'call_contract']; + + return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET'; + } + + private getQueryString(query?: Record): string { + if (isEmptyQueryObject(query)) { + return ''; + } + const queryString = Object.entries(query) + .map(([key, value]) => { + if (key === 'blockIdentifier') { + return `${getFormattedBlockIdentifier(value)}`; + } + return `${key}=${value}`; + }) + .join('&'); + + return `?${queryString}`; + } + + private getHeaders(method: 'POST' | 'GET'): AxiosRequestHeaders | undefined { + if (method === 'POST') { + return { + 'Content-Type': 'application/json', + }; + } + return undefined; + } + // typesafe fetch protected async fetchEndpoint( endpoint: T, + // typescript type magiuc to create a nice fitting function interface ...[query, request]: Endpoints[T]['QUERY'] extends never ? Endpoints[T]['REQUEST'] extends never - ? [] + ? [] // when no query and no request is needed, we can omit the query and request parameters : [undefined, Endpoints[T]['REQUEST']] : Endpoints[T]['REQUEST'] extends never - ? [Endpoints[T]['QUERY']] - : [Endpoints[T]['QUERY'], Endpoints[T]['REQUEST']] + ? [Endpoints[T]['QUERY']] // when no request is needed, we can omit the request parameter + : [Endpoints[T]['QUERY'], Endpoints[T]['REQUEST']] // when both query and request are needed, we cant omit anything ): Promise { - const baseUrl = ['add_transaction'].includes(endpoint) - ? this.gatewayUrl - : this.feederGatewayUrl; - const method = ['add_transaction', 'call_contract'].includes(endpoint) ? 'POST' : 'GET'; - const queryString = - !isEmptyQueryObject(query) && - `?${Object.entries(query) - .map(([key, value]) => - key !== 'blockIdentifier' ? `${key}=${value}` : `${getFormattedBlockIdentifier(value)}` - ) - .join('&')}`; + const baseUrl = this.getFetchUrl(endpoint); + const method = this.getFetchMethod(endpoint); + const queryString = this.getQueryString(query); + const headers = this.getHeaders(method); const { data } = await axios.request({ method, - url: urljoin(baseUrl, endpoint, queryString || ''), + url: urljoin(baseUrl, endpoint, queryString), data: stringify(request), - headers: method === 'POST' ? { 'Content-Type': 'application/json' } : {}, + headers, }); return data;