build: reenable eslint type checking
[poolifier.git] / examples / typescript / http-client-pool / src / worker.ts
CommitLineData
ded253e2 1import axios from 'axios'
2b099bf7 2import nodeFetch, {
49ac634f 3 type RequestInfo as NodeFetchRequestInfo,
3a502712 4 type ResponseInit as NodeFetchRequestInit,
49ac634f 5} from 'node-fetch'
ded253e2
JB
6import { ThreadWorker } from 'poolifier'
7
ef083f7b 8import type { WorkerData, WorkerResponse } from './types.js'
49ac634f 9
95e50651 10class HttpClientWorker extends ThreadWorker<WorkerData, WorkerResponse> {
49ac634f
JB
11 public constructor () {
12 super({
13 node_fetch: async (workerData?: WorkerData) => {
14 const response = await nodeFetch(
3a502712 15 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67f3f2d6 16 workerData!.input as URL | NodeFetchRequestInfo,
49ac634f
JB
17 workerData?.init as NodeFetchRequestInit
18 )
19 // The response is not structured-cloneable, so we return the response text body instead.
20 return {
3a502712 21 text: await response.text(),
49ac634f
JB
22 }
23 },
24 fetch: async (workerData?: WorkerData) => {
25 const response = await fetch(
3a502712 26 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67f3f2d6 27 workerData!.input as URL | RequestInfo,
49ac634f
JB
28 workerData?.init as RequestInit
29 )
30 // The response is not structured-cloneable, so we return the response text body instead.
31 return {
3a502712 32 text: await response.text(),
49ac634f 33 }
7e80208e
JB
34 },
35 axios: async (workerData?: WorkerData) => {
36 const response = await axios({
37 method: 'get',
3a502712 38 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67f3f2d6 39 url: workerData!.input as string,
3a502712 40 ...workerData?.axiosRequestConfig,
7e80208e
JB
41 })
42 return {
6e5d7052 43 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
3a502712 44 text: response.data,
7e80208e 45 }
3a502712 46 },
49ac634f
JB
47 })
48 }
49}
50
fac9ce96 51export const httpClientWorker = new HttpClientWorker()