Skip to content

Commit

Permalink
better rnw support
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed May 22, 2024
1 parent 4bf669e commit 7a5e12b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/minimal-expo/app/api/trpc/[trpc]+api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { z } from "zod";
import { UTApi } from "uploadthing/server";

const createContext = () => ({
utapi: new UTApi({ logLevel: "debug" }),
utapi: new UTApi({ logLevel: "info" }),
userId: "abc123",
});
const t = initTRPC.context<typeof createContext>().create();
Expand Down
4 changes: 3 additions & 1 deletion examples/minimal-expo/lib/trpc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const resolveUrl = () => {
try {
return new URL(
"/api/trpc",
process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? `http://${debuggerHost}`,
typeof window.location !== "undefined"
? window.location.origin
: process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? `http://${debuggerHost}`,
);
} catch (e) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions examples/minimal-expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"react-native-reanimated": "~3.10.0",
"react-native-safe-area-context": "4.10.1",
"react-native-screens": "~3.31.1",
"react-native-web": "~0.19.12",
"uploadthing": "6.10.4",
"zod": "^3.22.4"
},
Expand Down
12 changes: 7 additions & 5 deletions packages/expo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export const generateReactNativeHelpers = <TRouter extends FileRouter>(
initOpts?: GenerateTypedHelpersOptions,
) => {
const debuggerHost = Constants.expoConfig?.hostUri;
let url: URL;
let url = new URL("http://localhost:8081/api/uploadthing")
try {
url = new URL(
initOpts?.url ?? "/api/uploadthing",
process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? `http://${debuggerHost}`,
typeof window.location !== "undefined"
? window.location.origin
: process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? `http://${debuggerHost}`,
);
} catch (e) {
throw new Error(
`Failed to resolve URL from ${initOpts?.url?.toString()} and ${process.env.EXPO_PUBLIC_SERVER_ORIGIN} or ${debuggerHost}`,
);
// Can't throw since window.location is undefined in Metro pass
// but may get defined when app mounts.
console.warn(`Failed to resolve URL from ${initOpts?.url?.toString()} and ${process.env.EXPO_PUBLIC_SERVER_ORIGIN} or ${debuggerHost}. Your application may not work as expected.`);
}

const vanillaHelpers = generateReactHelpers<TRouter>({ ...initOpts, url });
Expand Down
52 changes: 52 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a5e12b

Please sign in to comment.