Skip to content

Commit 1756f98

Browse files
authored
types: Allow to pass any requestable undici instance (#405)
* types: Allow to pass any "requestable" undici instance * chore: Add tests
1 parent 1de0d8b commit 1756f98

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
AgentOptions as SecureAgentOptions,
2929
RequestOptions as SecureRequestOptions
3030
} from 'https'
31-
import { Pool, ProxyAgent } from 'undici'
31+
import { Pool, ProxyAgent, Dispatcher } from 'undici'
3232

3333
declare module 'fastify' {
3434
interface FastifyReply {
@@ -104,7 +104,7 @@ declare namespace fastifyReplyFrom {
104104
disableCache?: boolean;
105105
http?: HttpOptions;
106106
http2?: Http2Options | boolean;
107-
undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options };
107+
undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options } | { request: Dispatcher['request'] };
108108
contentTypesToEncode?: string[];
109109
retryMethods?: (HTTPMethods | 'TRACE')[];
110110
maxRetriesOn503?: number;

types/index.test-d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { IncomingHttpHeaders } from 'http2'
44
import * as https from 'node:https'
55
import { AddressInfo } from 'net'
66
import { expectType } from 'tsd'
7+
import { Agent, Client, Dispatcher, Pool } from 'undici'
78
import replyFrom, { FastifyReplyFromOptions } from '..'
89
// @ts-ignore
910
import tap from 'tap'
@@ -158,6 +159,34 @@ async function main () {
158159
})
159160
await undiciInstance.ready()
160161

162+
const undiciInstanceAgent = fastify()
163+
undiciInstance.register(replyFrom, {
164+
base: 'http://example2.com',
165+
undici: new Agent()
166+
})
167+
await undiciInstanceAgent.ready()
168+
169+
const undiciInstancePool = fastify()
170+
undiciInstance.register(replyFrom, {
171+
base: 'http://example2.com',
172+
undici: new Pool('http://example2.com')
173+
})
174+
await undiciInstancePool.ready()
175+
176+
const undiciInstanceClient = fastify()
177+
undiciInstance.register(replyFrom, {
178+
base: 'http://example2.com',
179+
undici: new Client('http://example2.com')
180+
})
181+
await undiciInstanceClient.ready()
182+
183+
const undiciInstanceDispatcher = fastify()
184+
undiciInstance.register(replyFrom, {
185+
base: 'http://example2.com',
186+
undici: new Dispatcher()
187+
})
188+
await undiciInstanceDispatcher.ready()
189+
161190
tap.pass('done')
162191
tap.end()
163192
}

0 commit comments

Comments
 (0)