TypeError: cache is not a function
Trying to run vitest integration testing on a procedure but i keep running into errors with a "cache" function not existing
I'm assuming this is a side effect since i call
flashcards.test.ts:
i'm getting an error from
uncachedValidateRequest
every time i create a TRPC context.
portion of trpc.ts
export async function createTRPCContext(opts: { headers: Headers }) {
// Grab the source of the request
const source = opts.headers.get("x-trpc-source") ?? "unknown";
// Get latest session and user data
const { session, user } = await uncachedValidateRequest();
console.log(">>> tRPC Request from", source, "by", user?.email);
return {
db,
session,
user,
...opts,
};
}
type TRPCContext = Awaited<ReturnType<typeof createTRPCContext>>;
export async function createTRPCContext(opts: { headers: Headers }) {
// Grab the source of the request
const source = opts.headers.get("x-trpc-source") ?? "unknown";
// Get latest session and user data
const { session, user } = await uncachedValidateRequest();
console.log(">>> tRPC Request from", source, "by", user?.email);
return {
db,
session,
user,
...opts,
};
}
type TRPCContext = Awaited<ReturnType<typeof createTRPCContext>>;
import type { inferProcedureInput } from "@trpc/server";
import { expect, suite, test } from "vitest";
import type { AppRouter } from "../root";
import { createCaller, createTRPCContext } from "../index";
suite("Flashcard Testing", async () => {
const ctx = await createTRPCContext({ headers: new Headers() });
const caller = createCaller(ctx);
test("Create Card Pack", async () => {
const input: inferProcedureInput<AppRouter["flashcards"]["createPack"]> =
{
name: "Testing Card Pack",
userId: "abc123def123"
}
const res = await caller.flashcards.createPack(input);
expect(res).toHaveProperty("id");
expect(res).toBeDefined();
});
});
import type { inferProcedureInput } from "@trpc/server";
import { expect, suite, test } from "vitest";
import type { AppRouter } from "../root";
import { createCaller, createTRPCContext } from "../index";
suite("Flashcard Testing", async () => {
const ctx = await createTRPCContext({ headers: new Headers() });
const caller = createCaller(ctx);
test("Create Card Pack", async () => {
const input: inferProcedureInput<AppRouter["flashcards"]["createPack"]> =
{
name: "Testing Card Pack",
userId: "abc123def123"
}
const res = await caller.flashcards.createPack(input);
expect(res).toHaveProperty("id");
expect(res).toBeDefined();
});
});
/auth/src/validate-request.ts
import type { Session, User } from "lucia";
import { cache } from "react";
import { cookies, headers } from "next/headers";
import { lucia } from ".";
export const uncachedValidateRequest = async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
> => {
const headersList = headers();
const source = headersList.get("x-trpc-source") ?? "unknown";
if (source === "expo-react") {
const authorization = headersList.get("Authorization");
if (!authorization) {
return { user: null, session: null };
}
const sessionId = lucia.readBearerToken(authorization);
if (!sessionId) {
return { user: null, session: null };
}
const result = await lucia.validateSession(sessionId);
return result;
} else {
const cookiesList = cookies();
const sessionId = cookiesList.get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return { user: null, session: null };
}
const result = await lucia.validateSession(sessionId);
try {
if (result.session?.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id);
cookiesList.set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookiesList.set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);
}
} catch {
console.error("Failed to set session cookie");
}
return result;
}
};
export const validateRequest = cache(uncachedValidateRequest);
import type { Session, User } from "lucia";
import { cache } from "react";
import { cookies, headers } from "next/headers";
import { lucia } from ".";
export const uncachedValidateRequest = async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
> => {
const headersList = headers();
const source = headersList.get("x-trpc-source") ?? "unknown";
if (source === "expo-react") {
const authorization = headersList.get("Authorization");
if (!authorization) {
return { user: null, session: null };
}
const sessionId = lucia.readBearerToken(authorization);
if (!sessionId) {
return { user: null, session: null };
}
const result = await lucia.validateSession(sessionId);
return result;
} else {
const cookiesList = cookies();
const sessionId = cookiesList.get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return { user: null, session: null };
}
const result = await lucia.validateSession(sessionId);
try {
if (result.session?.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id);
cookiesList.set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookiesList.set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);
}
} catch {
console.error("Failed to set session cookie");
}
return result;
}
};
export const validateRequest = cache(uncachedValidateRequest);
0 Replies