From b58940c79c310f10968fe78246f77cfb67dc6add Mon Sep 17 00:00:00 2001 From: Wojciech Grzebieniowski Date: Fri, 17 Jan 2025 12:39:59 +0100 Subject: [PATCH 1/2] types: Allow to pass any "requestable" undici instance --- types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 5d685cde..0280d356 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -28,7 +28,7 @@ import { AgentOptions as SecureAgentOptions, RequestOptions as SecureRequestOptions } from 'https' -import { Pool, ProxyAgent } from 'undici' +import { Pool, ProxyAgent, Dispatcher } from 'undici' declare module 'fastify' { interface FastifyReply { @@ -104,7 +104,7 @@ declare namespace fastifyReplyFrom { disableCache?: boolean; http?: HttpOptions; http2?: Http2Options | boolean; - undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options }; + undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options } | { request: Dispatcher['request'] }; contentTypesToEncode?: string[]; retryMethods?: (HTTPMethods | 'TRACE')[]; maxRetriesOn503?: number; From 9ce5f664dc966a80a7abcecb38cb96d52c55ccbd Mon Sep 17 00:00:00 2001 From: Wojciech Grzebieniowski Date: Thu, 23 Jan 2025 19:35:08 +0100 Subject: [PATCH 2/2] chore: Add tests --- types/index.test-d.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index cf4e2245..a7e91d94 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -4,6 +4,7 @@ import { IncomingHttpHeaders } from 'http2' import * as https from 'node:https' import { AddressInfo } from 'net' import { expectType } from 'tsd' +import { Agent, Client, Dispatcher, Pool } from 'undici' import replyFrom, { FastifyReplyFromOptions } from '..' // @ts-ignore import tap from 'tap' @@ -158,6 +159,34 @@ async function main () { }) await undiciInstance.ready() + const undiciInstanceAgent = fastify() + undiciInstance.register(replyFrom, { + base: 'http://example2.com', + undici: new Agent() + }) + await undiciInstanceAgent.ready() + + const undiciInstancePool = fastify() + undiciInstance.register(replyFrom, { + base: 'http://example2.com', + undici: new Pool('http://example2.com') + }) + await undiciInstancePool.ready() + + const undiciInstanceClient = fastify() + undiciInstance.register(replyFrom, { + base: 'http://example2.com', + undici: new Client('http://example2.com') + }) + await undiciInstanceClient.ready() + + const undiciInstanceDispatcher = fastify() + undiciInstance.register(replyFrom, { + base: 'http://example2.com', + undici: new Dispatcher() + }) + await undiciInstanceDispatcher.ready() + tap.pass('done') tap.end() }