From b2a568050e8275223b41f54a388f92a284841d8a Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 22 Feb 2024 11:18:41 +0800 Subject: [PATCH] scan api pattern --- apps/whale-api/src/e2e.defid.module.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/apps/whale-api/src/e2e.defid.module.ts b/apps/whale-api/src/e2e.defid.module.ts index df7773b8c3..55baaec74d 100644 --- a/apps/whale-api/src/e2e.defid.module.ts +++ b/apps/whale-api/src/e2e.defid.module.ts @@ -78,19 +78,16 @@ class DefidOceanApiClient { constructor (protected readonly options: WhaleApiClientOptions) { this.options = { // default - url: '', + url: '`http://127.0.0.1:3002', // DEFAULT_OCEAN_ARCHIVE_PORT: 3002 timeout: 60000, - version: '0', + version: 'v0', + network: 'regtest', ...options } } async get (path: string): Promise { - // TODO(canonbrother): endpoint should include `version` and `network` - // const { url: urlString, version, network, timeout } = this.options - // const url = `${urlString as string}/${version as string}/${network as string}/${path}` - - const res = await this.fetchTimeout(`${this.options.url}/${path}`, { + const res = await this.fetchTimeout(path, { method: 'GET', headers: { connection: 'open' @@ -105,7 +102,7 @@ class DefidOceanApiClient { } async post (path: string, body?: any): Promise { - const res = await this.fetchTimeout(`${this.options.url}/${path}`, { + const res = await this.fetchTimeout(path, { method: 'POST', headers: { 'content-type': 'application/json', @@ -117,11 +114,13 @@ class DefidOceanApiClient { } private async fetchTimeout (path: string, init: RequestInit): Promise { - const timeout = this.options.timeout ?? 60000 + const { url: endpoint, version, network, timeout } = this.options + const url = `${endpoint}/${version}/${network}/${path}` + const controller = new AbortController() const id = setTimeout(() => controller.abort(), timeout) - const req = fetch(path, { + const req = fetch(url, { cache: 'no-cache', signal: controller.signal, keepalive: true,