From 90cc9826b90fec8d1fc79d6d5b5194a108b3a2c6 Mon Sep 17 00:00:00 2001 From: Alexandr Kazachenko Date: Thu, 16 May 2024 19:13:35 +0500 Subject: [PATCH] feat: allow changing backoff and limiter per request (#208) * feat: allow changing backoff and limiter per request * fix: use custom RateLimiter is limiterOpts are overridden (cherry picked from commit 436884497f6fc1765a044f5ee3c88d12d0bbfd23) --- src/common/configs.ts | 2 ++ src/order-book/api.ts | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/common/configs.ts b/src/common/configs.ts index 2142eb0..488f783 100644 --- a/src/common/configs.ts +++ b/src/common/configs.ts @@ -67,6 +67,8 @@ export interface ApiContext { chainId: SupportedChainId env: CowEnv baseUrls?: ApiBaseUrls + limiterOpts?: RateLimiterOpts + backoffOpts?: BackoffOptions } /** diff --git a/src/order-book/api.ts b/src/order-book/api.ts index f1566e8..3183b99 100644 --- a/src/order-book/api.ts +++ b/src/order-book/api.ts @@ -23,7 +23,6 @@ import { DEFAULT_COW_API_CONTEXT, ENVS_LIST, PartialApiContext, - RequestOptions, } from '../common/configs' import { transformOrder } from './transformOrder' import { EnrichedOrder } from './types' @@ -126,7 +125,7 @@ export type GetOrdersRequest = { * @see {@link OrderBook API https://github.com/cowprotocol/services} */ export class OrderBookApi { - public context: ApiContext & RequestOptions + public context: ApiContext private rateLimiter: RateLimiter @@ -134,7 +133,7 @@ export class OrderBookApi { * Creates a new instance of the CoW Protocol OrderBook API client. * @param context - The API context to use. If not provided, the default context will be used. */ - constructor(context: PartialApiContext & RequestOptions = {}) { + constructor(context: PartialApiContext = {}) { this.context = { ...DEFAULT_COW_API_CONTEXT, ...context } this.rateLimiter = new RateLimiter(context.limiterOpts || DEFAULT_LIMITER_OPTIONS) } @@ -386,7 +385,7 @@ export class OrderBookApi { * @param contextOverride Optional context override for this request. * @returns New context with the override applied. */ - private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext & RequestOptions { + private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext { return { ...this.context, ...contextOverride } } @@ -408,10 +407,11 @@ export class OrderBookApi { * @returns The response from the API. */ private fetch(params: FetchParams, contextOverride: PartialApiContext = {}): Promise { - const { chainId, env } = this.getContextWithOverride(contextOverride) + const { chainId, env, backoffOpts: _backoffOpts } = this.getContextWithOverride(contextOverride) const baseUrl = this.getApiBaseUrls(env)[chainId] - const backoffOpts = this.context.backoffOpts || DEFAULT_BACKOFF_OPTIONS + const backoffOpts = _backoffOpts || DEFAULT_BACKOFF_OPTIONS + const rateLimiter = contextOverride.limiterOpts ? new RateLimiter(contextOverride.limiterOpts) : this.rateLimiter - return request(baseUrl, params, this.rateLimiter, backoffOpts) + return request(baseUrl, params, rateLimiter, backoffOpts) } }