All files / apps/web/src/pages/api utils.test.ts

0% Statements 0/14
0% Branches 0/6
0% Functions 0/3
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
import type {
	Expand,
	ResolveAllParamsFromParent,
	RouteConstraints,
} from "@tanstack/react-router";
import type { RouteMethod } from "@tanstack/react-start";
import type { Route } from "@tanstack/router-core";
import { assert } from "vitest";
 
export const getServerRouteMethod = <
	TRegister,
	TParentRoute extends RouteConstraints["TParentRoute"],
	TPath extends RouteConstraints["TPath"],
	TFullPath extends RouteConstraints["TFullPath"],
	TCustomId extends RouteConstraints["TCustomId"],
	TId extends RouteConstraints["TId"],
	TSearchValidator,
	TParams,
	TRouterContext,
	TRouteContextFn,
	TBeforeLoadFn,
	TLoaderDeps extends Record<string, unknown>,
	TLoaderFn,
	TChildren,
	TFileRouteTypes,
	TSSR,
	TServerMiddlewares,
	THandlers,
>(
	route: Route<
		TRegister,
		TParentRoute,
		TPath,
		TFullPath,
		TCustomId,
		TId,
		TSearchValidator,
		TParams,
		TRouterContext,
		TRouteContextFn,
		TBeforeLoadFn,
		TLoaderDeps,
		TLoaderFn,
		TChildren,
		TFileRouteTypes,
		TSSR,
		TServerMiddlewares,
		THandlers
	>,
	methodName: RouteMethod,
) => {
	const methods = route.options.server?.handlers;
	if (typeof methods === "function") {
		throw new TypeError("Expected to have static methods!");
	}
	const method = methods?.[methodName];
	assert(method);
	return async (props: {
		pathname: TPath;
		request: Request;
		params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>;
	}) => {
		const response = await method({
			// oxlint-disable-next-line typescript/no-explicit-any typescript/no-unsafe-assignment
			context: undefined as any,
			next: () => {
				throw new Error("Next function should not be run in tests!");
			},
			...props,
		});
		if (!response) {
			throw new Error("Expected to have response in tests!");
		}
		if ("isNext" in response) {
			throw new Error("Didn't expect to have isNext in tests!");
		}
		return response;
	};
};