@rebasepro/client-firebase 0.0.1-canary.09e5ec5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -0
  3. package/dist/components/FirebaseLoginView.d.ts +72 -0
  4. package/dist/components/RebaseFirebaseApp.d.ts +19 -0
  5. package/dist/components/RebaseFirebaseAppProps.d.ts +144 -0
  6. package/dist/components/index.d.ts +3 -0
  7. package/dist/components/social_icons.d.ts +6 -0
  8. package/dist/hooks/index.d.ts +8 -0
  9. package/dist/hooks/useAppCheck.d.ts +20 -0
  10. package/dist/hooks/useBuildUserManagement.d.ts +46 -0
  11. package/dist/hooks/useFirebaseAuthController.d.ts +15 -0
  12. package/dist/hooks/useFirebaseRealTimeDBDelegate.d.ts +5 -0
  13. package/dist/hooks/useFirebaseStorageSource.d.ts +14 -0
  14. package/dist/hooks/useFirestoreDriver.d.ts +56 -0
  15. package/dist/hooks/useInitialiseFirebase.d.ts +34 -0
  16. package/dist/hooks/useRecaptcha.d.ts +8 -0
  17. package/dist/index.d.ts +4 -0
  18. package/dist/index.es.js +3060 -0
  19. package/dist/index.es.js.map +1 -0
  20. package/dist/index.umd.js +3043 -0
  21. package/dist/index.umd.js.map +1 -0
  22. package/dist/social_icons.d.ts +6 -0
  23. package/dist/types/appcheck.d.ts +10 -0
  24. package/dist/types/auth.d.ts +41 -0
  25. package/dist/types/index.d.ts +3 -0
  26. package/dist/types/text_search.d.ts +39 -0
  27. package/dist/utils/algolia.d.ts +9 -0
  28. package/dist/utils/collections_firestore.d.ts +5 -0
  29. package/dist/utils/database.d.ts +2 -0
  30. package/dist/utils/index.d.ts +7 -0
  31. package/dist/utils/local_text_search_controller.d.ts +2 -0
  32. package/dist/utils/pinecone.d.ts +24 -0
  33. package/dist/utils/rebase_search_controller.d.ts +73 -0
  34. package/dist/utils/text_search_controller.d.ts +13 -0
  35. package/package.json +63 -0
  36. package/src/components/FirebaseLoginView.tsx +693 -0
  37. package/src/components/RebaseFirebaseApp.tsx +291 -0
  38. package/src/components/RebaseFirebaseAppProps.tsx +180 -0
  39. package/src/components/index.ts +3 -0
  40. package/src/components/social_icons.tsx +135 -0
  41. package/src/hooks/index.ts +8 -0
  42. package/src/hooks/useAppCheck.ts +101 -0
  43. package/src/hooks/useBuildUserManagement.tsx +374 -0
  44. package/src/hooks/useFirebaseAuthController.ts +334 -0
  45. package/src/hooks/useFirebaseRealTimeDBDelegate.ts +269 -0
  46. package/src/hooks/useFirebaseStorageSource.ts +207 -0
  47. package/src/hooks/useFirestoreDriver.ts +784 -0
  48. package/src/hooks/useInitialiseFirebase.ts +132 -0
  49. package/src/hooks/useRecaptcha.tsx +28 -0
  50. package/src/index.ts +4 -0
  51. package/src/social_icons.tsx +135 -0
  52. package/src/types/appcheck.ts +11 -0
  53. package/src/types/auth.tsx +74 -0
  54. package/src/types/index.ts +3 -0
  55. package/src/types/text_search.ts +42 -0
  56. package/src/utils/algolia.ts +27 -0
  57. package/src/utils/collections_firestore.ts +148 -0
  58. package/src/utils/database.ts +39 -0
  59. package/src/utils/index.ts +7 -0
  60. package/src/utils/local_text_search_controller.ts +143 -0
  61. package/src/utils/pinecone.ts +75 -0
  62. package/src/utils/rebase_search_controller.ts +357 -0
  63. package/src/utils/text_search_controller.ts +34 -0
@@ -0,0 +1,207 @@
1
+ import { FirebaseApp } from "@firebase/app";
2
+ import {
3
+ deleteObject,
4
+ getDownloadURL,
5
+ getMetadata,
6
+ getStorage,
7
+ list,
8
+ ref,
9
+ uploadBytesResumable
10
+ } from "@firebase/storage";
11
+ import { DownloadConfig, DownloadMetadata, StorageListResult, StorageSource, UploadFileProps } from "@rebasepro/types";
12
+
13
+ /**
14
+ * @group Firebase
15
+ */
16
+ export interface FirebaseStorageSourceProps {
17
+ firebaseApp?: FirebaseApp
18
+ bucketUrl?: string
19
+ }
20
+
21
+ /**
22
+ * Use this hook to build an {@link StorageSource} based on Firebase storage
23
+ * @group Firebase
24
+ */
25
+ export function useFirebaseStorageSource({
26
+ firebaseApp,
27
+ bucketUrl
28
+ }: FirebaseStorageSourceProps): StorageSource {
29
+ const projectId = firebaseApp?.options?.projectId;
30
+ const urlsCache: Record<string, DownloadConfig> = {};
31
+ return {
32
+ putObject({
33
+ file,
34
+ key,
35
+ metadata,
36
+ bucket
37
+ }: UploadFileProps)
38
+ : Promise<any> {
39
+ try {
40
+ if (!firebaseApp) throw Error("useFirebaseStorageSource Firebase not initialised");
41
+ const storageBucketUrl = bucket ?? bucketUrl;
42
+ const storage = getStorage(firebaseApp, storageBucketUrl);
43
+ if (!storage) throw Error("useFirebaseStorageSource Firebase not initialised");
44
+
45
+ const storageRef = ref(storage, key);
46
+ const uploadTask = uploadBytesResumable(storageRef, file, metadata);
47
+
48
+ return new Promise((resolve, reject) => {
49
+ let lastProgress = 0;
50
+ let timeoutId: NodeJS.Timeout | null = null;
51
+
52
+ const clearTimeoutIfExists = () => {
53
+ if (timeoutId) {
54
+ clearTimeout(timeoutId);
55
+ timeoutId = null;
56
+ }
57
+ };
58
+
59
+ const setProgressTimeout = () => {
60
+ clearTimeoutIfExists();
61
+ timeoutId = setTimeout(() => {
62
+ uploadTask.cancel();
63
+ reject(new Error("Upload failed - This is likely a CORS configuration issue. " +
64
+ "Make sure Firebase Storage is enabled in your project: " + `https://console.firebase.google.com/u/0/project/${projectId}/storage` + ". " +
65
+ "If it is, check Firebase Storage CORS settings."));
66
+ }, 5000);
67
+ };
68
+
69
+ setProgressTimeout();
70
+
71
+ uploadTask.on("state_changed",
72
+ (snapshot) => {
73
+ const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
74
+
75
+ if (progress > lastProgress) {
76
+ lastProgress = progress;
77
+ setProgressTimeout();
78
+ }
79
+ },
80
+ (error) => {
81
+ clearTimeoutIfExists();
82
+ console.error("Firebase Storage upload error:", error);
83
+
84
+ let errorMessage = "Unknown upload error";
85
+
86
+ if (error?.message) {
87
+ errorMessage = error.message;
88
+ } else if (typeof error === "string") {
89
+ errorMessage = error;
90
+ } else if (error?.code) {
91
+ errorMessage = error.code;
92
+ }
93
+
94
+ if (error?.code === "storage/unauthorized") {
95
+ reject(new Error("Unauthorized: Check Firebase Storage security rules"));
96
+ } else if (error?.code === "storage/canceled") {
97
+ reject(new Error("Upload canceled"));
98
+ } else if (error?.code === "storage/unknown" || !error?.code) {
99
+ reject(new Error("Upload failed - Check Firebase Storage CORS configuration or network connection"));
100
+ } else if (errorMessage.toLowerCase().includes("network")) {
101
+ reject(new Error("Network error: Check your internet connection"));
102
+ } else {
103
+ const newError = Object.assign(new Error(errorMessage), { code: error?.code });
104
+ reject(newError);
105
+ }
106
+ },
107
+ () => {
108
+ clearTimeoutIfExists();
109
+ const fullPath = uploadTask.snapshot.ref.fullPath;
110
+ const bucketName = uploadTask.snapshot.ref.bucket;
111
+ resolve({
112
+ key: fullPath,
113
+ bucket: bucketName,
114
+ storageUrl: `s3://${bucketName}/${fullPath}`
115
+ });
116
+ }
117
+ );
118
+ });
119
+ } catch (error) {
120
+ return Promise.reject(error);
121
+ }
122
+ },
123
+
124
+ async getObject(path: string, bucket?: string): Promise<File | null> {
125
+ try {
126
+ if (!firebaseApp) throw Error("useFirebaseStorageSource Firebase not initialised");
127
+ const storageBucketUrl = bucket ?? bucketUrl;
128
+ const storage = getStorage(firebaseApp, storageBucketUrl);
129
+ if (!storage) throw Error("useFirebaseStorageSource Firebase not initialised");
130
+ const fileRef = ref(storage, path);
131
+ const url = await getDownloadURL(fileRef);
132
+ const response = await fetch(url);
133
+ const blob = await response.blob();
134
+ return new File([blob], path);
135
+ } catch (e: any) {
136
+ if (e?.code === "storage/object-not-found") return null;
137
+ throw e;
138
+ }
139
+ },
140
+
141
+ async getSignedUrl(storagePathOrUrl: string, bucket?: string): Promise<DownloadConfig> {
142
+ if (!firebaseApp) throw Error("useFirebaseStorageSource Firebase not initialised");
143
+
144
+ // Support fully-qualified s3:// and gs:// URLs for backward compatibility
145
+ let resolvedPathOrUrl = storagePathOrUrl;
146
+ let resolvedBucket = bucket;
147
+ const match = storagePathOrUrl.match(/^(s3|gs):\/\//);
148
+ if (match) {
149
+ const protocolLength = match[0].length;
150
+ const withoutProtocol = storagePathOrUrl.substring(protocolLength);
151
+ const firstSlash = withoutProtocol.indexOf("/");
152
+ if (firstSlash > 0) {
153
+ resolvedBucket = withoutProtocol.substring(0, firstSlash);
154
+ resolvedPathOrUrl = withoutProtocol.substring(firstSlash + 1);
155
+ }
156
+ }
157
+
158
+ const storageBucketUrl = resolvedBucket ?? bucketUrl;
159
+ const storage = getStorage(firebaseApp, storageBucketUrl);
160
+ if (!storage) throw Error("useFirebaseStorageSource Firebase not initialised");
161
+
162
+ if (urlsCache[storagePathOrUrl])
163
+ return urlsCache[storagePathOrUrl];
164
+ try {
165
+ const fileRef = ref(storage, resolvedPathOrUrl);
166
+ const [url, metadata] = await Promise.all([getDownloadURL(fileRef), getMetadata(fileRef)]);
167
+ const result: DownloadConfig = {
168
+ url,
169
+ metadata: metadata as DownloadMetadata
170
+ }
171
+ urlsCache[storagePathOrUrl] = result;
172
+ return result;
173
+ } catch (e: any) {
174
+ if (e?.code === "storage/object-not-found") return {
175
+ url: null,
176
+ fileNotFound: true
177
+ };
178
+ throw e;
179
+ }
180
+ },
181
+
182
+ async listObjects(prefix: string, options?: {
183
+ bucket?: string,
184
+ maxResults?: number,
185
+ pageToken?: string
186
+ }): Promise<StorageListResult> {
187
+ if (!firebaseApp) throw Error("useFirebaseStorageSource Firebase not initialised");
188
+ const storageBucketUrl = options?.bucket ?? bucketUrl;
189
+ const storage = getStorage(firebaseApp, storageBucketUrl);
190
+ if (!storage) throw Error("useFirebaseStorageSource Firebase not initialised");
191
+ const folderRef = ref(storage, prefix);
192
+ return await list(folderRef, {
193
+ maxResults: options?.maxResults,
194
+ pageToken: options?.pageToken
195
+ });
196
+ },
197
+
198
+ async deleteObject(path: string, bucket?: string): Promise<void> {
199
+ if (!firebaseApp) throw Error("useFirebaseStorageSource Firebase not initialised");
200
+ const storageBucketUrl = bucket ?? bucketUrl;
201
+ const storage = getStorage(firebaseApp, storageBucketUrl);
202
+ if (!storage) throw Error("useFirebaseStorageSource Firebase not initialised");
203
+ const fileRef = ref(storage, path);
204
+ return deleteObject(fileRef);
205
+ }
206
+ };
207
+ }