@@ -2,7 +2,12 @@ import { initContract, TsRestResponseError } from '@ts-rest/core';
22import { parse as parseMultipart , getBoundary } from 'parse-multipart-data' ;
33import { z } from 'zod' ;
44import { vi } from 'vitest' ;
5- import { fetchRequestHandler , tsr } from './ts-rest-fetch' ;
5+ import {
6+ createFetchHandler ,
7+ FetchHandlerOptions ,
8+ fetchRequestHandler ,
9+ tsr ,
10+ } from './ts-rest-fetch' ;
611import { RequestValidationErrorSchema } from '../types' ;
712
813const c = initContract ( ) ;
@@ -142,22 +147,26 @@ const router = tsr
142147 throw new Error ( 'Test error' ) ;
143148 } ) ;
144149
150+ const options = {
151+ jsonQuery : true ,
152+ responseValidation : true ,
153+ cors : {
154+ origin : [ 'http://localhost' ] ,
155+ credentials : true ,
156+ } ,
157+ } as FetchHandlerOptions < { } , { foo : string } > ;
158+
145159const testFetchRequestHandler = ( request : Request ) => {
146160 return fetchRequestHandler ( {
147161 contract,
148162 router,
149- options : {
150- jsonQuery : true ,
151- responseValidation : true ,
152- cors : {
153- origin : [ 'http://localhost' ] ,
154- credentials : true ,
155- } ,
156- } ,
163+ options,
157164 request,
158165 } ) ;
159166} ;
160167
168+ const newFetchRequestHandler = createFetchHandler ( contract , router , options ) ;
169+
161170describe ( 'fetchRequestHandler' , ( ) => {
162171 afterEach ( ( ) => {
163172 vi . clearAllMocks ( ) ;
@@ -194,7 +203,7 @@ describe('fetchRequestHandler', () => {
194203 body : JSON . stringify ( { ping : 'foo' } ) ,
195204 } ) ;
196205
197- const response = await testFetchRequestHandler ( request ) ;
206+ const response = await newFetchRequestHandler ( request ) ;
198207 const expectedResponse = new Response ( '{"id":123,"pong":"foo"}' , {
199208 headers : {
200209 'access-control-allow-credentials' : 'true' ,
@@ -270,7 +279,7 @@ describe('fetchRequestHandler', () => {
270279 body : JSON . stringify ( { ping : 'foo' } ) ,
271280 } ) ;
272281
273- const response = await testFetchRequestHandler ( request ) ;
282+ const response = await newFetchRequestHandler ( request ) ;
274283 const expectedResponse = new Response ( null , {
275284 status : 500 ,
276285 headers : {
@@ -356,7 +365,7 @@ describe('fetchRequestHandler', () => {
356365 headers : { origin : 'http://localhost' } ,
357366 } ) ;
358367
359- const response = await testFetchRequestHandler ( request ) ;
368+ const response = await newFetchRequestHandler ( request ) ;
360369 const expectedResponse = new Response ( null , {
361370 status : 500 ,
362371 headers : {
0 commit comments