mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 08:06:56 +00:00
tRPC 초기 설정
This commit is contained in:
23
src/trpc/client.ts
Normal file
23
src/trpc/client.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
||||
import { browser } from "$app/environment";
|
||||
import type { AppRouter } from "./router.server";
|
||||
|
||||
const createClient = (fetch: typeof globalThis.fetch) =>
|
||||
createTRPCClient<AppRouter>({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: "/trpc",
|
||||
fetch,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
let browserClient: ReturnType<typeof createClient>;
|
||||
|
||||
export const trpc = (fetch = globalThis.fetch) => {
|
||||
const client = browserClient ?? createClient(fetch);
|
||||
if (browser) {
|
||||
browserClient ??= client;
|
||||
}
|
||||
return client;
|
||||
};
|
||||
9
src/trpc/init.server.ts
Normal file
9
src/trpc/init.server.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import { initTRPC } from "@trpc/server";
|
||||
|
||||
export const createContext = (event: RequestEvent) => event;
|
||||
|
||||
const t = initTRPC.context<Awaited<ReturnType<typeof createContext>>>().create();
|
||||
|
||||
export const router = t.router;
|
||||
export const publicProcedure = t.procedure;
|
||||
13
src/trpc/router.server.ts
Normal file
13
src/trpc/router.server.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
|
||||
import { createContext, router } from "./init.server";
|
||||
|
||||
export const appRouter = router({
|
||||
// TODO
|
||||
});
|
||||
|
||||
export const createCaller = (event: RequestEvent) => appRouter.createCaller(createContext(event));
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
export type RouterInputs = inferRouterInputs<AppRouter>;
|
||||
export type RouterOutputs = inferRouterOutputs<AppRouter>;
|
||||
Reference in New Issue
Block a user