From: Jérôme Benoit Date: Thu, 10 Aug 2023 18:36:21 +0000 (+0200) Subject: feat: add axios to http-client TS code example X-Git-Tag: v2.6.23~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7e80208e7b0ef86674762cdcbe158dec46666bb5;p=poolifier.git feat: add axios to http-client TS code example reference: #790 Signed-off-by: Jérôme Benoit --- diff --git a/examples/typescript/http-client/package.json b/examples/typescript/http-client/package.json index 04beb8be..27530880 100644 --- a/examples/typescript/http-client/package.json +++ b/examples/typescript/http-client/package.json @@ -13,7 +13,7 @@ "build": "pnpm build:clean && tsc", "build:clean": "tsc --build --clean", "start": "node dist/main.js", - "start:httpd-echo": "node dist/httpd-echo.js", + "start:httpd-echo": "node httpd-echo.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/examples/typescript/http-client/src/main.ts b/examples/typescript/http-client/src/main.ts index a19ed280..067aa2d2 100644 --- a/examples/typescript/http-client/src/main.ts +++ b/examples/typescript/http-client/src/main.ts @@ -5,11 +5,11 @@ import { type WorkerResponse } from './types.js' const parallelism = availableParallelism() const requestUrl = 'http://localhost:8080/' -for (const workerFunction of ['node_fetch', 'fetch']) { +for (const workerFunction of ['node_fetch', 'fetch', 'axios']) { const fetchPoolPromises = new Set>() for (let i = 0; i < availableParallelism(); i++) { fetchPoolPromises.add( - fetchPool.execute({ url: requestUrl }, workerFunction) + fetchPool.execute({ input: requestUrl }, workerFunction) ) } try { diff --git a/examples/typescript/http-client/src/types.ts b/examples/typescript/http-client/src/types.ts index e68e7260..ff0d3262 100644 --- a/examples/typescript/http-client/src/types.ts +++ b/examples/typescript/http-client/src/types.ts @@ -3,10 +3,12 @@ import { type RequestInfo as NodeFetchRequestInfo, type RequestInit as NodeFetchRequestInit } from 'node-fetch' +import { type AxiosRequestConfig } from 'axios' export interface WorkerData { - url: URL | RequestInfo | NodeFetchRequestInfo + input: URL | RequestInfo | NodeFetchRequestInfo init?: RequestInit | NodeFetchRequestInit + axiosRequestConfig?: AxiosRequestConfig } export interface WorkerResponse { diff --git a/examples/typescript/http-client/src/worker.ts b/examples/typescript/http-client/src/worker.ts index b12466d2..3e4c6dd5 100644 --- a/examples/typescript/http-client/src/worker.ts +++ b/examples/typescript/http-client/src/worker.ts @@ -4,6 +4,7 @@ import { type RequestInfo as NodeFetchRequestInfo, type ResponseInit as NodeFetchRequestInit } from 'node-fetch' +import axios from 'axios' import { type WorkerData, type WorkerResponse } from './types.js' class HttpClientWorker extends ThreadWorker { @@ -11,7 +12,7 @@ class HttpClientWorker extends ThreadWorker { super({ node_fetch: async (workerData?: WorkerData) => { const response = await nodeFetch( - (workerData as WorkerData).url as URL | NodeFetchRequestInfo, + (workerData as WorkerData).input as URL | NodeFetchRequestInfo, workerData?.init as NodeFetchRequestInit ) // The response is not structured-cloneable, so we return the response text body instead. @@ -21,13 +22,23 @@ class HttpClientWorker extends ThreadWorker { }, fetch: async (workerData?: WorkerData) => { const response = await fetch( - (workerData as WorkerData).url as URL | RequestInfo, + (workerData as WorkerData).input as URL | RequestInfo, workerData?.init as RequestInit ) // The response is not structured-cloneable, so we return the response text body instead. return { text: await response.text() } + }, + axios: async (workerData?: WorkerData) => { + const response = await axios({ + method: 'get', + url: (workerData as WorkerData).input as string, + ...workerData?.axiosRequestConfig + }) + return { + text: response.data + } } }) }