mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add nuxt and expo with orpc
This commit is contained in:
49
apps/cli/templates/api/orpc/native/utils/orpc.ts.hbs
Normal file
49
apps/cli/templates/api/orpc/native/utils/orpc.ts.hbs
Normal file
@@ -0,0 +1,49 @@
|
||||
import { createORPCClient } from "@orpc/client";
|
||||
import { RPCLink } from "@orpc/client/fetch";
|
||||
import { createORPCReactQueryUtils } from "@orpc/react-query";
|
||||
import type { RouterUtils } from "@orpc/react-query";
|
||||
import type { RouterClient } from "@orpc/server";
|
||||
import { QueryCache, QueryClient } from "@tanstack/react-query";
|
||||
import { createContext, useContext } from "react";
|
||||
import type { appRouter } from "../../server/src/routers";
|
||||
{{#if auth}}
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
{{/if}}
|
||||
|
||||
type ORPCReactUtils = RouterUtils<RouterClient<typeof appRouter>>;
|
||||
|
||||
export const queryClient = new QueryClient({
|
||||
queryCache: new QueryCache({
|
||||
onError: (error) => {
|
||||
console.log(error)
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
export const link = new RPCLink({
|
||||
url: `${process.env.EXPO_PUBLIC_SERVER_URL}/rpc`,
|
||||
{{#if auth}}
|
||||
headers() {
|
||||
const headers = new Map<string, string>();
|
||||
const cookies = authClient.getCookie();
|
||||
if (cookies) {
|
||||
headers.set("Cookie", cookies);
|
||||
}
|
||||
return Object.fromEntries(headers);
|
||||
},
|
||||
{{/if}}
|
||||
});
|
||||
|
||||
export const client: RouterClient<typeof appRouter> = createORPCClient(link);
|
||||
|
||||
export const orpc = createORPCReactQueryUtils(client);
|
||||
|
||||
export const ORPCContext = createContext<ORPCReactUtils | undefined>(undefined);
|
||||
|
||||
export function useORPC(): ORPCReactUtils {
|
||||
const orpc = useContext(ORPCContext);
|
||||
if (!orpc) {
|
||||
throw new Error("ORPCContext is not set up properly");
|
||||
}
|
||||
return orpc;
|
||||
}
|
||||
35
apps/cli/templates/api/orpc/web/nuxt/app/plugins/orpc.ts.hbs
Normal file
35
apps/cli/templates/api/orpc/web/nuxt/app/plugins/orpc.ts.hbs
Normal file
@@ -0,0 +1,35 @@
|
||||
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
|
||||
import type { RouterClient } from '@orpc/server'
|
||||
import type { appRouter } from "../../../server/src/routers/index";
|
||||
import { createORPCClient } from '@orpc/client'
|
||||
import { RPCLink } from '@orpc/client/fetch'
|
||||
import { createORPCVueQueryUtils } from '@orpc/vue-query'
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
const config = useRuntimeConfig()
|
||||
const serverUrl = config.public.serverURL
|
||||
|
||||
const rpcUrl = `${serverUrl}/rpc`;
|
||||
|
||||
const rpcLink = new RPCLink({
|
||||
url: rpcUrl,
|
||||
{{#if auth}}
|
||||
fetch(url, options) {
|
||||
return fetch(url, {
|
||||
...options,
|
||||
credentials: "include",
|
||||
});
|
||||
},
|
||||
{{/if}}
|
||||
})
|
||||
|
||||
|
||||
const client: RouterClient<typeof appRouter> = createORPCClient(rpcLink)
|
||||
const orpcUtils = createORPCVueQueryUtils(client)
|
||||
|
||||
return {
|
||||
provide: {
|
||||
orpc: orpcUtils
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user