( route: string, info: RouteInfo<Params, Search>, getInfo: GetInfo<Result> )
| 337 | } |
| 338 | |
| 339 | export function makeGetRoute< |
| 340 | Params extends z.ZodSchema, |
| 341 | Search extends z.ZodSchema, |
| 342 | Result extends z.ZodSchema |
| 343 | >( |
| 344 | route: string, |
| 345 | info: RouteInfo<Params, Search>, |
| 346 | getInfo: GetInfo<Result> |
| 347 | ): GetRouteBuilder<Params, Search, Result> { |
| 348 | const urlBuilder = createRouteBuilder(route, info); |
| 349 | |
| 350 | const routeBuilder: GetRouteBuilder<Params, Search, Result> = ( |
| 351 | p?: z.input<Params>, |
| 352 | search?: z.input<Search>, |
| 353 | options?: FetchOptions |
| 354 | ): Promise<z.output<Result>> => { |
| 355 | return fetch(urlBuilder(p, search), options) |
| 356 | .then((res) => { |
| 357 | if (!res.ok) { |
| 358 | throw new Error(`Failed to fetch ${info.name}: ${res.statusText}`); |
| 359 | } |
| 360 | return res.json() as Promise<z.output<Result>>; |
| 361 | }) |
| 362 | .then((data) => { |
| 363 | const result = getInfo.result.safeParse(data); |
| 364 | if (!result.success) { |
| 365 | throw new Error( |
| 366 | `Invalid response for route ${info.name}: ${result.error.message}` |
| 367 | ); |
| 368 | } |
| 369 | return result.data; |
| 370 | }); |
| 371 | }; |
| 372 | |
| 373 | routeBuilder.params = undefined as z.output<Params>; |
| 374 | routeBuilder.paramsSchema = info.params; |
| 375 | routeBuilder.search = undefined as z.output<Search>; |
| 376 | routeBuilder.searchSchema = info.search; |
| 377 | routeBuilder.result = undefined as z.output<Result>; |
| 378 | routeBuilder.resultSchema = getInfo.result; |
| 379 | |
| 380 | return routeBuilder; |
| 381 | } |
| 382 | |
| 383 | export function makeDeleteRoute< |
| 384 | Params extends z.ZodSchema, |
nothing calls this directly
no test coverage detected