-
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use tRPC
queryOptions
API (#1305)
* queryOptions API * cool
- Loading branch information
1 parent
8589f6f
commit 4690233
Showing
15 changed files
with
251 additions
and
194 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
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 |
---|---|---|
@@ -1,57 +1,49 @@ | ||
import { useState } from "react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { httpBatchLink, loggerLink } from "@trpc/client"; | ||
import { createTRPCReact } from "@trpc/react-query"; | ||
import { QueryClient } from "@tanstack/react-query"; | ||
import { createTRPCClient, httpBatchLink, loggerLink } from "@trpc/client"; | ||
import { createTRPCOptionsProxy } from "@trpc/tanstack-react-query"; | ||
import superjson from "superjson"; | ||
|
||
import type { AppRouter } from "@acme/api"; | ||
|
||
import { getBaseUrl } from "./base-url"; | ||
import { getToken } from "./session-store"; | ||
|
||
/** | ||
* A set of typesafe hooks for consuming your API. | ||
*/ | ||
export const api = createTRPCReact<AppRouter>(); | ||
export { type RouterInputs, type RouterOutputs } from "@acme/api"; | ||
export const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
// ... | ||
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* A wrapper for your app that provides the TRPC context. | ||
* Use only in _app.tsx | ||
* A set of typesafe hooks for consuming your API. | ||
*/ | ||
export function TRPCProvider(props: { children: React.ReactNode }) { | ||
const [queryClient] = useState(() => new QueryClient()); | ||
const [trpcClient] = useState(() => | ||
api.createClient({ | ||
links: [ | ||
loggerLink({ | ||
enabled: (opts) => | ||
process.env.NODE_ENV === "development" || | ||
(opts.direction === "down" && opts.result instanceof Error), | ||
colorMode: "ansi", | ||
}), | ||
httpBatchLink({ | ||
transformer: superjson, | ||
url: `${getBaseUrl()}/api/trpc`, | ||
headers() { | ||
const headers = new Map<string, string>(); | ||
headers.set("x-trpc-source", "expo-react"); | ||
export const trpc = createTRPCOptionsProxy<AppRouter>({ | ||
client: createTRPCClient({ | ||
links: [ | ||
loggerLink({ | ||
enabled: (opts) => | ||
process.env.NODE_ENV === "development" || | ||
(opts.direction === "down" && opts.result instanceof Error), | ||
colorMode: "ansi", | ||
}), | ||
httpBatchLink({ | ||
transformer: superjson, | ||
url: `${getBaseUrl()}/api/trpc`, | ||
headers() { | ||
const headers = new Map<string, string>(); | ||
headers.set("x-trpc-source", "expo-react"); | ||
|
||
const token = getToken(); | ||
if (token) headers.set("Authorization", `Bearer ${token}`); | ||
const token = getToken(); | ||
if (token) headers.set("Authorization", `Bearer ${token}`); | ||
|
||
return Object.fromEntries(headers); | ||
}, | ||
}), | ||
], | ||
}), | ||
); | ||
return Object.fromEntries(headers); | ||
}, | ||
}), | ||
], | ||
}), | ||
queryClient, | ||
}); | ||
|
||
return ( | ||
<api.Provider client={trpcClient} queryClient={queryClient}> | ||
<QueryClientProvider client={queryClient}> | ||
{props.children} | ||
</QueryClientProvider> | ||
</api.Provider> | ||
); | ||
} | ||
export { type RouterInputs, type RouterOutputs } from "@acme/api"; |
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
Oops, something went wrong.