-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this is getting more and more complex every day
- Loading branch information
Showing
12 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createContext } from "$lib/trpc/context"; | ||
import { router } from "$lib/trpc/router"; | ||
import type { Handle } from "@sveltejs/kit"; | ||
import { createTRPCHandle } from "trpc-sveltekit"; | ||
|
||
export const handle: Handle = createTRPCHandle({ router, createContext }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts" generics="T, E"> | ||
import type { CreateQueryResult } from '@tanstack/svelte-query'; | ||
import type { Snippet } from "svelte"; | ||
interface Props { | ||
query: CreateQueryResult<T, E>, | ||
onpending?: Snippet, | ||
onerror?: Snippet<[E]>, | ||
ondone: Snippet<[T | undefined, boolean]> | ||
}; | ||
const { | ||
query, | ||
onpending, | ||
onerror, | ||
ondone | ||
}: Props = $props(); | ||
</script> | ||
|
||
{#if $query.isPending && onpending} | ||
{@render onpending()} | ||
{:else if $query.isError && onerror} | ||
{@render onerror($query.error)} | ||
{:else} | ||
{@render ondone($query.data, $query.isPlaceholderData)} | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// type imported for TSDoc, ignore this. | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import type { page } from "$app/stores"; | ||
import type { Router } from "$lib/trpc/router"; | ||
|
||
import { createTRPCClient, type TRPCClientInit } from "trpc-sveltekit"; | ||
import type { QueryClient } from "@tanstack/svelte-query"; | ||
import { svelteQueryWrapper } from "trpc-svelte-query-adapter"; | ||
|
||
let browserClient: ReturnType<typeof svelteQueryWrapper<Router>>; | ||
|
||
/** | ||
* allows access to a client-side tRPC client | ||
* | ||
* @param init it's easier to just set this to {@link page | `$page`} | ||
* @returns a client-side client for tRPC with this server | ||
*/ | ||
export const trpc = (init?: TRPCClientInit, queryClient?: QueryClient) => { | ||
const isBrowser = typeof window !== "undefined"; | ||
if (isBrowser && browserClient) return browserClient; | ||
const client = svelteQueryWrapper({ | ||
client: createTRPCClient<Router>({ init }), | ||
queryClient, | ||
}); | ||
if (isBrowser) browserClient = client; | ||
return client; | ||
}; | ||
|
||
export type trpcClient = typeof browserClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { RequestEvent } from "@sveltejs/kit"; | ||
|
||
export const createContext = async (event: RequestEvent) => { | ||
return { | ||
event, | ||
}; | ||
}; | ||
|
||
export type Context = Awaited<ReturnType<typeof createContext>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import z from "zod"; | ||
|
||
import type { Context } from "$lib/trpc/context"; | ||
import { initTRPC } from "@trpc/server"; | ||
|
||
export const t = initTRPC.context<Context>().create(); | ||
|
||
export const router = t.router({ | ||
greeting: t.procedure | ||
.input( | ||
z.object({ | ||
name: z.string().min(10).max(20).describe("The name to greet"), | ||
}), | ||
) | ||
.query(async (opts) => { | ||
await new Promise((res) => setTimeout(res, 1000)); | ||
return `Hello ${opts.input.name}`; | ||
}), | ||
}); | ||
|
||
export const createCaller = t.createCallerFactory(router); | ||
|
||
export type Router = typeof router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,31 @@ | ||
<script lang="ts"> | ||
import { page } from "$app/stores"; | ||
import { trpc } from "$lib/trpc/client"; | ||
import Main from "@/main.svelte"; | ||
import Suspense from "@/suspense.svelte"; | ||
const query = trpc($page).greeting.createQuery({name: "123456789"}) | ||
</script> | ||
|
||
<Main> | ||
<a href="/app" class="hover:text-blue-300"> | ||
Go to <code>/app</code> | ||
</a> | ||
<div class="flex flex-col items-center justify-start"> | ||
<h1 class="text-hero">Jail Bird</h1> | ||
|
||
{#snippet onpending()} | ||
<span>Loading...</span> | ||
{/snippet} | ||
|
||
{#snippet onerror(error: (typeof $query)['error'])} | ||
<span>Error</span> | ||
<pre><code>{error?.message}</code></pre> | ||
{/snippet} | ||
|
||
{#snippet ondone(data: string | undefined, is_placeholder: boolean)} | ||
<pre>is_placeholder: {is_placeholder}</pre> | ||
<span>{data}</span> | ||
{/snippet} | ||
|
||
<Suspense {query} {ondone} {onerror} {onpending} /> | ||
</div> | ||
</Main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { browser } from "$app/environment"; | ||
import { QueryClient } from "@tanstack/svelte-query"; | ||
|
||
import type { LayoutLoad } from "./$types"; | ||
|
||
export const load = (async () => { | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
enabled: browser | ||
} | ||
} | ||
}); | ||
|
||
return { queryClient }; | ||
}) satisfies LayoutLoad; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters