build: reenable eslint type checking
[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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16 workerData!.input as URL | NodeFetchRequestInfo,
17 workerData?.init as NodeFetchRequestInit
18 )
19 // The response is not structured-cloneable, so we return the response text body instead.
20 return {
21 text: await response.text(),
22 }
23 },
24 fetch: async (workerData?: WorkerData) => {
25 const response = await fetch(
26 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
27 workerData!.input as URL | RequestInfo,
28 workerData?.init as RequestInit
29 )
30 // The response is not structured-cloneable, so we return the response text body instead.
31 return {
32 text: await response.text(),
33 }
34 },
35 axios: async (workerData?: WorkerData) => {
36 const response = await axios({
37 method: 'get',
38 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39 url: workerData!.input as string,
40 ...workerData?.axiosRequestConfig,
41 })
42 return {
43 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
44 text: response.data,
45 }
46 },
47 })
48 }
49 }
50
51 export const httpClientWorker = new HttpClientWorker()