Commit | Line | Data |
---|---|---|
49ac634f | 1 | import { ThreadWorker } from 'poolifier' |
2b099bf7 | 2 | import nodeFetch, { |
49ac634f JB |
3 | type RequestInfo as NodeFetchRequestInfo, |
4 | type ResponseInit as NodeFetchRequestInit | |
5 | } from 'node-fetch' | |
7e80208e | 6 | import axios from 'axios' |
49ac634f JB |
7 | import { type WorkerData, type WorkerResponse } from './types.js' |
8 | ||
95e50651 | 9 | class HttpClientWorker extends ThreadWorker<WorkerData, WorkerResponse> { |
49ac634f JB |
10 | public constructor () { |
11 | super({ | |
12 | node_fetch: async (workerData?: WorkerData) => { | |
13 | const response = await nodeFetch( | |
7e80208e | 14 | (workerData as WorkerData).input as URL | NodeFetchRequestInfo, |
49ac634f JB |
15 | workerData?.init as NodeFetchRequestInit |
16 | ) | |
17 | // The response is not structured-cloneable, so we return the response text body instead. | |
18 | return { | |
19 | text: await response.text() | |
20 | } | |
21 | }, | |
22 | fetch: async (workerData?: WorkerData) => { | |
23 | const response = await fetch( | |
7e80208e | 24 | (workerData as WorkerData).input as URL | RequestInfo, |
49ac634f JB |
25 | workerData?.init as RequestInit |
26 | ) | |
27 | // The response is not structured-cloneable, so we return the response text body instead. | |
28 | return { | |
29 | text: await response.text() | |
30 | } | |
7e80208e JB |
31 | }, |
32 | axios: async (workerData?: WorkerData) => { | |
33 | const response = await axios({ | |
34 | method: 'get', | |
35 | url: (workerData as WorkerData).input as string, | |
36 | ...workerData?.axiosRequestConfig | |
37 | }) | |
38 | return { | |
39 | text: response.data | |
40 | } | |
49ac634f JB |
41 | } |
42 | }) | |
43 | } | |
44 | } | |
45 | ||
fac9ce96 | 46 | export const httpClientWorker = new HttpClientWorker() |