-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathindex.ts
More file actions
86 lines (78 loc) · 2.77 KB
/
Copy pathindex.ts
File metadata and controls
86 lines (78 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Constants from "expo-constants";
import { generateReactHelpers } from "@uploadthing/react/native";
import type { FetchEsque } from "@uploadthing/shared";
import { warnIfInvalidPeerDependency } from "@uploadthing/shared";
import { version as uploadthingClientVersion } from "uploadthing/client";
import type { FileRouter } from "uploadthing/types";
import { peerDependencies } from "../package.json";
import { GENERATE_useDocumentUploader } from "./document-picker";
import { GENERATE_useImageUploader } from "./image-picker";
export interface GenerateTypedHelpersOptions {
/**
* URL to the UploadThing API endpoint
* @example "/api/uploadthing"
* @example "https://www.example.com/api/uploadthing"
*
* If relative, host will be inferred from either the `EXPO_PUBLIC_SERVER_URL` environment variable or `ExpoConstants.hostUri`
*
* @default (process.env.EXPO_PUBLIC_SERVER_URL ?? ExpoConstants.debuggerHost) + "/api/uploadthing"
*/
url?: URL | string;
/**
* Provide a custom fetch implementation.
* @default `globalThis.fetch`
* @example
* ```ts
* fetch: (input, init) => {
* if (input.toString().startsWith(MY_SERVER_URL)) {
* // Include cookies in the request to your API
* return fetch(input, {
* ...init,
* credentials: "include",
* });
* }
*
* return fetch(input, init);
* }
* ```
*/
fetch?: FetchEsque | undefined;
}
export const generateReactNativeHelpers = <TRouter extends FileRouter>(
initOpts?: GenerateTypedHelpersOptions,
) => {
warnIfInvalidPeerDependency(
"@uploadthing/expo",
peerDependencies.uploadthing,
uploadthingClientVersion,
);
const debuggerHost = Constants.expoConfig?.hostUri;
let url = new URL("http://localhost:8081/api/uploadthing");
try {
url = new URL(
initOpts?.url ?? "/api/uploadthing",
process.env.EXPO_PUBLIC_SERVER_URL ??
(typeof window !== "undefined" && window.location?.origin
? window.location?.origin
: `http://${debuggerHost}`),
);
} catch (err) {
// Can't throw since window.location is undefined in Metro pass
// but may get defined when app mounts.
// eslint-disable-next-line no-console
console.warn(
`Failed to resolve URL from ${initOpts?.url?.toString()} and ${process.env.EXPO_PUBLIC_SERVER_URL} or ${debuggerHost}. Your application may not work as expected.`,
err,
);
}
const fetch = initOpts?.fetch ?? globalThis.fetch;
const opts = {
...initOpts,
url,
fetch,
};
const vanillaHelpers = generateReactHelpers<TRouter>(opts);
const useImageUploader = GENERATE_useImageUploader<TRouter>(opts);
const useDocumentUploader = GENERATE_useDocumentUploader<TRouter>(opts);
return { ...vanillaHelpers, useImageUploader, useDocumentUploader };
};