From 0fbea77850014b343861efe91cda62a025889770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Wed, 3 Apr 2024 07:29:27 +0200 Subject: [PATCH] feat: specify timeout for api requests --- package.json | 2 +- src/extended-map-popup.user.ts | 10 +++++++--- src/extended-player-profile.user.ts | 16 ++++++++++++---- src/extended-tribe-profile.user.ts | 17 +++++++++++++---- src/extended-village-profile.user.ts | 14 ++++++++++++-- src/lib/twhelp/.gitignore | 4 +--- src/lib/twhelp/index.ts | 2 ++ src/lib/twhelp/timeout.ts | 20 ++++++++++++++++++++ 8 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/lib/twhelp/index.ts create mode 100644 src/lib/twhelp/timeout.ts diff --git a/package.json b/package.json index b15fc46..9a4bf1e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "license": "MIT", "scripts": { "prepare": "husky install", - "generate-client": "openapi --input src/lib/twhelp/openapi3.json --output src/lib/twhelp --name TWHelpClient --useOptions --client axios", + "generate-client": "openapi --input src/lib/twhelp/openapi3.json --output src/lib/twhelp/generated --name TWHelpClient --useOptions --client axios", "build": "rm -rf dist && yarn generate-client && parcel build ./src/*.user.ts ./src/*.quickbar.ts", "postbuild": "PUBLIC_URL=https://scripts.twhelp.app node postbuild.js", "lint": "eslint src/**/*.ts" diff --git a/src/extended-map-popup.user.ts b/src/extended-map-popup.user.ts index 99123ab..2734c8e 100644 --- a/src/extended-map-popup.user.ts +++ b/src/extended-map-popup.user.ts @@ -7,6 +7,7 @@ import { Ennoblement, UnitInfo, ServerConfig, + AxiosHttpRequestWithTimeout, } from './lib/twhelp'; declare global { @@ -59,9 +60,12 @@ class TWHelpConnector { private readonly version: string, private readonly server: string ) { - this.client = new TWHelpClient({ - BASE: baseUrl, - }); + this.client = new TWHelpClient( + { + BASE: baseUrl, + }, + AxiosHttpRequestWithTimeout + ); } serverConfig() { diff --git a/src/extended-player-profile.user.ts b/src/extended-player-profile.user.ts index bfa008d..a6963c0 100644 --- a/src/extended-player-profile.user.ts +++ b/src/extended-player-profile.user.ts @@ -3,7 +3,12 @@ import { InADayClient } from './lib/tw'; import { createTranslationFunc } from './utils'; import { buildURL } from './lib/twstats'; -import { TWHelpClient, PlayerSnapshot, Player } from './lib/twhelp'; +import { + TWHelpClient, + PlayerSnapshot, + Player, + AxiosHttpRequestWithTimeout, +} from './lib/twhelp'; import { DialogTable } from './common/dialog-table'; import { Cache } from './lib/cache'; @@ -68,9 +73,12 @@ class TWHelpConnector { private readonly server: string, private readonly id: number ) { - this.client = new TWHelpClient({ - BASE: baseUrl, - }); + this.client = new TWHelpClient( + { + BASE: baseUrl, + }, + AxiosHttpRequestWithTimeout + ); } async player() { diff --git a/src/extended-tribe-profile.user.ts b/src/extended-tribe-profile.user.ts index 15a8deb..f342e88 100644 --- a/src/extended-tribe-profile.user.ts +++ b/src/extended-tribe-profile.user.ts @@ -1,4 +1,10 @@ -import { Player, Tribe, TribeSnapshot, TWHelpClient } from './lib/twhelp'; +import { + AxiosHttpRequestWithTimeout, + Player, + Tribe, + TribeSnapshot, + TWHelpClient, +} from './lib/twhelp'; import { createTranslationFunc } from './utils'; import { buildURL } from './lib/twstats'; import { DialogTable } from './common/dialog-table'; @@ -52,9 +58,12 @@ class TWHelpConnector { private readonly server: string, private readonly id: number ) { - this.client = new TWHelpClient({ - BASE: baseUrl, - }); + this.client = new TWHelpClient( + { + BASE: baseUrl, + }, + AxiosHttpRequestWithTimeout + ); } async tribe() { diff --git a/src/extended-village-profile.user.ts b/src/extended-village-profile.user.ts index f1a365c..6351725 100644 --- a/src/extended-village-profile.user.ts +++ b/src/extended-village-profile.user.ts @@ -1,7 +1,12 @@ import { createTranslationFunc } from './utils'; import { calcLoyalty } from './lib/tw'; import { Cache } from './lib/cache'; -import { TWHelpClient, Ennoblement, ServerConfig } from './lib/twhelp'; +import { + TWHelpClient, + Ennoblement, + ServerConfig, + AxiosHttpRequestWithTimeout, +} from './lib/twhelp'; import { DialogTable } from './common/dialog-table'; const t = createTranslationFunc({ @@ -34,7 +39,12 @@ class TWHelpConnector { private readonly server: string, private readonly id: number ) { - this.client = new TWHelpClient({ BASE: baseUrl }); + this.client = new TWHelpClient( + { + BASE: baseUrl, + }, + AxiosHttpRequestWithTimeout + ); } serverConfig() { diff --git a/src/lib/twhelp/.gitignore b/src/lib/twhelp/.gitignore index 4026c08..86d4c2d 100644 --- a/src/lib/twhelp/.gitignore +++ b/src/lib/twhelp/.gitignore @@ -1,3 +1 @@ -* -!.gitignore -!openapi3.json +generated diff --git a/src/lib/twhelp/index.ts b/src/lib/twhelp/index.ts new file mode 100644 index 0000000..c31c54f --- /dev/null +++ b/src/lib/twhelp/index.ts @@ -0,0 +1,2 @@ +export * from './generated'; +export * from './timeout'; diff --git a/src/lib/twhelp/timeout.ts b/src/lib/twhelp/timeout.ts new file mode 100644 index 0000000..c8bb433 --- /dev/null +++ b/src/lib/twhelp/timeout.ts @@ -0,0 +1,20 @@ +import axios from 'axios'; +import { request as __request } from './generated/core/request'; +import { CancelablePromise } from './generated/core/CancelablePromise'; +import { BaseHttpRequest } from './generated/core/BaseHttpRequest'; +import { ApiRequestOptions } from './generated/core/ApiRequestOptions'; +import type { OpenAPIConfig } from './generated/core/OpenAPI'; + +export class AxiosHttpRequestWithTimeout extends BaseHttpRequest { + axiosInstance = axios.create({ + timeout: 5000, + }); + + constructor(config: OpenAPIConfig) { + super(config); + } + + public override request(options: ApiRequestOptions): CancelablePromise { + return __request(this.config, options, this.axiosInstance); + } +}