Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Feb 9, 2022
1 parent ed021dc commit ee93bd6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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": "^_" }]
Expand Down
65 changes: 48 additions & 17 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { AxiosRequestHeaders } from 'axios';
import urljoin from 'url-join';

import {
Expand Down Expand Up @@ -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, any>): 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<T extends keyof Endpoints>(
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<Endpoints[T]['RESPONSE']> {
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<Endpoints[T]['RESPONSE']>({
method,
url: urljoin(baseUrl, endpoint, queryString || ''),
url: urljoin(baseUrl, endpoint, queryString),
data: stringify(request),
headers: method === 'POST' ? { 'Content-Type': 'application/json' } : {},
headers,
});

return data;
Expand Down

0 comments on commit ee93bd6

Please sign in to comment.