|
| 1 | +import { ClientTypes } from '@requestnetwork/types'; |
| 2 | +import { retry } from '@requestnetwork/utils'; |
| 3 | +import httpConfigDefaults from './http-config-defaults'; |
| 4 | +import { stringify } from 'qs'; |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 7 | +const packageJson = require('../package.json'); |
| 8 | +export type NodeConnectionConfig = { baseURL: string; headers: Record<string, string> }; |
| 9 | + |
| 10 | +export class HttpDataAccessConfig { |
| 11 | + /** |
| 12 | + * Configuration that overrides http-config-defaults, |
| 13 | + * @see httpConfigDefaults for the default configuration. |
| 14 | + */ |
| 15 | + public httpConfig: ClientTypes.IHttpDataAccessConfig; |
| 16 | + |
| 17 | + /** |
| 18 | + * Configuration that will be sent at each request. |
| 19 | + */ |
| 20 | + public nodeConnectionConfig: NodeConnectionConfig; |
| 21 | + |
| 22 | + constructor( |
| 23 | + { |
| 24 | + httpConfig, |
| 25 | + nodeConnectionConfig, |
| 26 | + }: { |
| 27 | + httpConfig?: Partial<ClientTypes.IHttpDataAccessConfig>; |
| 28 | + nodeConnectionConfig?: Partial<NodeConnectionConfig>; |
| 29 | + } = { |
| 30 | + httpConfig: {}, |
| 31 | + nodeConnectionConfig: {}, |
| 32 | + }, |
| 33 | + ) { |
| 34 | + const requestClientVersion = packageJson.version; |
| 35 | + this.httpConfig = { |
| 36 | + ...httpConfigDefaults, |
| 37 | + ...httpConfig, |
| 38 | + }; |
| 39 | + this.nodeConnectionConfig = { |
| 40 | + baseURL: 'http://localhost:3000', |
| 41 | + headers: { |
| 42 | + [this.httpConfig.requestClientVersionHeader]: requestClientVersion, |
| 43 | + }, |
| 44 | + ...nodeConnectionConfig, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sends an HTTP GET request to the node and retries until it succeeds. |
| 50 | + * Throws when the retry count reaches a maximum. |
| 51 | + * |
| 52 | + * @param url HTTP GET request url |
| 53 | + * @param params HTTP GET request parameters |
| 54 | + * @param retryConfig Maximum retry count, delay between retries, exponential backoff delay, and maximum exponential backoff delay |
| 55 | + */ |
| 56 | + public async fetchAndRetry<T = unknown>( |
| 57 | + path: string, |
| 58 | + params: Record<string, unknown>, |
| 59 | + retryConfig: { |
| 60 | + maxRetries?: number; |
| 61 | + retryDelay?: number; |
| 62 | + exponentialBackoffDelay?: number; |
| 63 | + maxExponentialBackoffDelay?: number; |
| 64 | + } = {}, |
| 65 | + ): Promise<T> { |
| 66 | + retryConfig.maxRetries = retryConfig.maxRetries ?? this.httpConfig.httpRequestMaxRetry; |
| 67 | + retryConfig.retryDelay = retryConfig.retryDelay ?? this.httpConfig.httpRequestRetryDelay; |
| 68 | + retryConfig.exponentialBackoffDelay = |
| 69 | + retryConfig.exponentialBackoffDelay ?? this.httpConfig.httpRequestExponentialBackoffDelay; |
| 70 | + retryConfig.maxExponentialBackoffDelay = |
| 71 | + retryConfig.maxExponentialBackoffDelay ?? |
| 72 | + this.httpConfig.httpRequestMaxExponentialBackoffDelay; |
| 73 | + return await retry(async () => await this.fetch<T>('GET', path, params), retryConfig)(); |
| 74 | + } |
| 75 | + |
| 76 | + public async fetch<T = unknown>( |
| 77 | + method: 'GET' | 'POST', |
| 78 | + path: string, |
| 79 | + params: Record<string, unknown> | undefined, |
| 80 | + body?: Record<string, unknown>, |
| 81 | + ): Promise<T> { |
| 82 | + const { baseURL, headers, ...options } = this.nodeConnectionConfig; |
| 83 | + const url = new URL(path, baseURL); |
| 84 | + if (params) { |
| 85 | + // qs.parse doesn't handle well mixes of string and object params |
| 86 | + for (const [key, value] of Object.entries(params)) { |
| 87 | + if (typeof value === 'object') { |
| 88 | + params[key] = JSON.stringify(value); |
| 89 | + } |
| 90 | + } |
| 91 | + url.search = stringify(params); |
| 92 | + } |
| 93 | + const r = await fetch(url, { |
| 94 | + method, |
| 95 | + body: body ? JSON.stringify(body) : undefined, |
| 96 | + headers: { |
| 97 | + 'Content-Type': 'application/json', |
| 98 | + ...headers, |
| 99 | + }, |
| 100 | + ...options, |
| 101 | + }); |
| 102 | + if (r.ok) { |
| 103 | + return await r.json(); |
| 104 | + } |
| 105 | + |
| 106 | + throw Object.assign(new Error(r.statusText), { |
| 107 | + status: r.status, |
| 108 | + statusText: r.statusText, |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
0 commit comments