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