@@ -3,8 +3,11 @@ import { FetchEvent, ServeFunction } from '../types';
33
44export type GetEndpoint = ( req ?: Request ) => URL ;
55
6+ type CustomHeaders = ( req : Request ) => HeaderChanges ;
7+
68export interface ProxyOptions {
79 host ?: 'original' | 'xforwarded' ;
10+ headers ?: CustomHeaders ;
811}
912
1013const DEFAULT_OPTIONS : ProxyOptions = {
@@ -63,15 +66,22 @@ export function requestToUpstream(
6366 opts . host === 'original' ? original . hostname : upstream . hostname ;
6467 url . protocol = upstream . protocol ;
6568
66- const customHeaders : HeaderChanges =
69+ const hostHeaders =
6770 opts . host === 'xforwarded'
6871 ? {
69- 'X-Forwarded-Host' : original . hostname ,
70- 'X-Forwarded-Proto' : original . protocol
71- }
72+ 'X-Forwarded-Host' : original . hostname ,
73+ 'X-Forwarded-Proto' : original . protocol
74+ }
7275 : {
73- Host : original . hostname
74- } ;
76+ Host : original . hostname
77+ } ;
78+
79+ const reqHeaders = opts . headers ? opts . headers ( request ) : { } ;
80+
81+ const customHeaders : HeaderChanges = {
82+ ...( hostHeaders as any ) ,
83+ ...reqHeaders
84+ } ;
7585
7686 // Copy old headers
7787 const headers = patchHeaders ( request . headers , customHeaders ) ;
0 commit comments