svelte-firekit 0.0.24 → 0.1.0
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.
- package/README.md +445 -213
- package/dist/components/Collection.svelte +150 -0
- package/dist/components/Collection.svelte.d.ts +27 -0
- package/dist/components/Ddoc.svelte +131 -0
- package/dist/components/Ddoc.svelte.d.ts +28 -0
- package/dist/components/Node.svelte +97 -0
- package/dist/components/Node.svelte.d.ts +23 -0
- package/dist/components/auth-guard.svelte +89 -0
- package/dist/components/auth-guard.svelte.d.ts +26 -0
- package/dist/components/custom-guard.svelte +122 -0
- package/dist/components/custom-guard.svelte.d.ts +31 -0
- package/dist/components/download-url.svelte +92 -0
- package/dist/components/download-url.svelte.d.ts +19 -0
- package/dist/components/firebase-app.svelte +30 -0
- package/dist/components/firebase-app.svelte.d.ts +7 -0
- package/dist/components/node-list.svelte +102 -0
- package/dist/components/node-list.svelte.d.ts +27 -0
- package/dist/components/signed-in.svelte +42 -0
- package/dist/components/signed-in.svelte.d.ts +11 -0
- package/dist/components/signed-out.svelte +42 -0
- package/dist/components/signed-out.svelte.d.ts +11 -0
- package/dist/components/storage-list.svelte +97 -0
- package/dist/components/storage-list.svelte.d.ts +26 -0
- package/dist/components/upload-task.svelte +108 -0
- package/dist/components/upload-task.svelte.d.ts +24 -0
- package/dist/config.js +17 -39
- package/dist/firebase.d.ts +43 -21
- package/dist/firebase.js +121 -35
- package/dist/index.d.ts +21 -12
- package/dist/index.js +27 -14
- package/dist/services/auth.d.ts +389 -0
- package/dist/services/auth.js +824 -0
- package/dist/services/collection.svelte.d.ts +286 -0
- package/dist/services/collection.svelte.js +871 -0
- package/dist/services/document.svelte.d.ts +288 -0
- package/dist/services/document.svelte.js +555 -0
- package/dist/services/mutations.d.ts +336 -0
- package/dist/services/mutations.js +1079 -0
- package/dist/services/presence.svelte.d.ts +141 -0
- package/dist/services/presence.svelte.js +727 -0
- package/dist/{realtime → services}/realtime.svelte.d.ts +3 -1
- package/dist/{realtime → services}/realtime.svelte.js +13 -7
- package/dist/services/storage.svelte.d.ts +257 -0
- package/dist/services/storage.svelte.js +374 -0
- package/dist/services/user.svelte.d.ts +290 -0
- package/dist/services/user.svelte.js +533 -0
- package/dist/types/auth.d.ts +158 -0
- package/dist/types/auth.js +106 -0
- package/dist/types/collection.d.ts +360 -0
- package/dist/types/collection.js +167 -0
- package/dist/types/document.d.ts +342 -0
- package/dist/types/document.js +148 -0
- package/dist/types/firebase.d.ts +44 -0
- package/dist/types/firebase.js +33 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +4 -0
- package/dist/types/mutations.d.ts +387 -0
- package/dist/types/mutations.js +205 -0
- package/dist/types/presence.d.ts +282 -0
- package/dist/types/presence.js +80 -0
- package/dist/utils/errors.d.ts +21 -0
- package/dist/utils/errors.js +35 -0
- package/dist/utils/firestore.d.ts +9 -0
- package/dist/utils/firestore.js +33 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/providers.d.ts +16 -0
- package/dist/utils/providers.js +30 -0
- package/dist/utils/user.d.ts +8 -0
- package/dist/utils/user.js +29 -0
- package/package.json +65 -65
- package/dist/auth/auth.d.ts +0 -117
- package/dist/auth/auth.js +0 -194
- package/dist/auth/presence.svelte.d.ts +0 -139
- package/dist/auth/presence.svelte.js +0 -373
- package/dist/auth/user.svelte.d.ts +0 -112
- package/dist/auth/user.svelte.js +0 -155
- package/dist/firestore/awaitable-doc.svelte.d.ts +0 -141
- package/dist/firestore/awaitable-doc.svelte.js +0 -183
- package/dist/firestore/batch.svelte.d.ts +0 -140
- package/dist/firestore/batch.svelte.js +0 -218
- package/dist/firestore/collection-group.svelte.d.ts +0 -78
- package/dist/firestore/collection-group.svelte.js +0 -120
- package/dist/firestore/collection.svelte.d.ts +0 -96
- package/dist/firestore/collection.svelte.js +0 -137
- package/dist/firestore/doc.svelte.d.ts +0 -90
- package/dist/firestore/doc.svelte.js +0 -131
- package/dist/firestore/document-mutations.svelte.d.ts +0 -164
- package/dist/firestore/document-mutations.svelte.js +0 -273
- package/dist/storage/download-url.svelte.d.ts +0 -83
- package/dist/storage/download-url.svelte.js +0 -114
- package/dist/storage/storage-list.svelte.d.ts +0 -89
- package/dist/storage/storage-list.svelte.js +0 -123
- package/dist/storage/upload-task.svelte.d.ts +0 -94
- package/dist/storage/upload-task.svelte.js +0 -138
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { UserProfile } from '../types/auth.js';
|
|
2
|
+
import type { Auth } from 'firebase/auth';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
type $$ComponentProps = {
|
|
5
|
+
/**
|
|
6
|
+
* Children content to render when all checks pass
|
|
7
|
+
*/
|
|
8
|
+
children: Snippet<[UserProfile, Auth, () => Promise<void>]>;
|
|
9
|
+
/**
|
|
10
|
+
* Whether authentication is required to view the content
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
requireAuth?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Path to redirect to if checks fail
|
|
16
|
+
* @default '/'
|
|
17
|
+
*/
|
|
18
|
+
redirectTo?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Fallback content to show while checking auth state
|
|
21
|
+
*/
|
|
22
|
+
fallback?: Snippet<[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Array of verification functions that must return true to allow access
|
|
25
|
+
* Each function receives the user profile and auth instance
|
|
26
|
+
*/
|
|
27
|
+
verificationChecks?: ((user: UserProfile, auth: Auth) => boolean | Promise<boolean>)[];
|
|
28
|
+
};
|
|
29
|
+
declare const CustomGuard: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
30
|
+
type CustomGuard = ReturnType<typeof CustomGuard>;
|
|
31
|
+
export default CustomGuard;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ref, type StorageReference } from 'firebase/storage';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { firekitDownloadUrl } from '../services/storage.svelte.js';
|
|
5
|
+
import { browser } from '$app/environment';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for DownloadURL component
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
ref: path,
|
|
13
|
+
children,
|
|
14
|
+
loading
|
|
15
|
+
}: {
|
|
16
|
+
/**
|
|
17
|
+
* Storage reference path string (e.g. 'images/pic.png')
|
|
18
|
+
*/
|
|
19
|
+
ref: string;
|
|
20
|
+
/**
|
|
21
|
+
* Content to render when URL is loaded
|
|
22
|
+
*/
|
|
23
|
+
children: Snippet<[string, StorageReference, any]>;
|
|
24
|
+
/**
|
|
25
|
+
* Content to render while loading
|
|
26
|
+
*/
|
|
27
|
+
loading?: Snippet<[]>;
|
|
28
|
+
} = $props();
|
|
29
|
+
|
|
30
|
+
// Get Storage instance and create references
|
|
31
|
+
let storage: any = $state(null);
|
|
32
|
+
let storageRef: StorageReference | null = $state(null);
|
|
33
|
+
let downloadUrlService: ReturnType<typeof firekitDownloadUrl> | null = $state(null);
|
|
34
|
+
|
|
35
|
+
// Track URL state
|
|
36
|
+
let urlState = $state({
|
|
37
|
+
loading: true,
|
|
38
|
+
url: null as string | null,
|
|
39
|
+
error: null as Error | null
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Subscribe to URL changes
|
|
43
|
+
$effect(() => {
|
|
44
|
+
if (!browser) {
|
|
45
|
+
urlState = {
|
|
46
|
+
loading: false,
|
|
47
|
+
url: null,
|
|
48
|
+
error: new Error('Storage not available in SSR')
|
|
49
|
+
};
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Initialize storage and service
|
|
54
|
+
storage = firebaseService.getStorageInstance();
|
|
55
|
+
if (!storage) {
|
|
56
|
+
urlState = {
|
|
57
|
+
loading: false,
|
|
58
|
+
url: null,
|
|
59
|
+
error: new Error('Storage instance not available')
|
|
60
|
+
};
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Create storage reference
|
|
65
|
+
storageRef = ref(storage, path);
|
|
66
|
+
|
|
67
|
+
// Create download URL service
|
|
68
|
+
downloadUrlService = firekitDownloadUrl(path);
|
|
69
|
+
|
|
70
|
+
// The service state is reactive, so we can directly access it
|
|
71
|
+
// The effect will re-run when the service state changes
|
|
72
|
+
urlState = {
|
|
73
|
+
loading: downloadUrlService.loading,
|
|
74
|
+
url: downloadUrlService.url,
|
|
75
|
+
error: downloadUrlService.error
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
</script>
|
|
79
|
+
|
|
80
|
+
{#if !browser}
|
|
81
|
+
<div>Storage not available in SSR</div>
|
|
82
|
+
{:else if urlState.loading}
|
|
83
|
+
{#if loading}
|
|
84
|
+
{@render loading()}
|
|
85
|
+
{:else}
|
|
86
|
+
<div>Loading...</div>
|
|
87
|
+
{/if}
|
|
88
|
+
{:else if urlState.error}
|
|
89
|
+
<div class="error">Error: {urlState.error.message}</div>
|
|
90
|
+
{:else if urlState.url}
|
|
91
|
+
{@render children(urlState.url, storageRef!, storage)}
|
|
92
|
+
{/if}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type StorageReference } from 'firebase/storage';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Storage reference path string (e.g. 'images/pic.png')
|
|
6
|
+
*/
|
|
7
|
+
ref: string;
|
|
8
|
+
/**
|
|
9
|
+
* Content to render when URL is loaded
|
|
10
|
+
*/
|
|
11
|
+
children: Snippet<[string, StorageReference, any]>;
|
|
12
|
+
/**
|
|
13
|
+
* Content to render while loading
|
|
14
|
+
*/
|
|
15
|
+
loading?: Snippet<[]>;
|
|
16
|
+
};
|
|
17
|
+
declare const DownloadUrl: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
18
|
+
type DownloadUrl = ReturnType<typeof DownloadUrl>;
|
|
19
|
+
export default DownloadUrl;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from 'svelte';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import { browser } from '$app/environment';
|
|
5
|
+
import { firebaseService } from '../firebase.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Props for FirebaseApp component
|
|
9
|
+
*/
|
|
10
|
+
let { children }: { children: Snippet } = $props();
|
|
11
|
+
|
|
12
|
+
// Only initialize Firebase in the browser
|
|
13
|
+
if (browser) {
|
|
14
|
+
// Get Firebase app instance from service
|
|
15
|
+
const app = firebaseService.getFirebaseApp();
|
|
16
|
+
const auth = firebaseService.getAuthInstance();
|
|
17
|
+
const firestore = firebaseService.getDbInstance();
|
|
18
|
+
const storage = firebaseService.getStorageInstance();
|
|
19
|
+
const rtdb = firebaseService.getDatabaseInstance();
|
|
20
|
+
|
|
21
|
+
// Set Firebase instances in context
|
|
22
|
+
setContext('firebase/app', app);
|
|
23
|
+
setContext('firebase/auth', auth);
|
|
24
|
+
setContext('firebase/firestore', firestore);
|
|
25
|
+
setContext('firebase/storage', storage);
|
|
26
|
+
setContext('firebase/rtdb', rtdb);
|
|
27
|
+
}
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
{@render children()}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ref, type DatabaseReference, type Database } from 'firebase/database';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { firekitRealtimeList } from '../services/realtime.svelte.js';
|
|
5
|
+
import { browser } from '$app/environment';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for NodeList component
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
path,
|
|
13
|
+
startWith,
|
|
14
|
+
children,
|
|
15
|
+
loading,
|
|
16
|
+
queryConstraints
|
|
17
|
+
}: {
|
|
18
|
+
/**
|
|
19
|
+
* Database reference path string (e.g. 'users')
|
|
20
|
+
*/
|
|
21
|
+
path: string;
|
|
22
|
+
/**
|
|
23
|
+
* Initial value to use before list is fetched
|
|
24
|
+
*/
|
|
25
|
+
startWith?: any[];
|
|
26
|
+
/**
|
|
27
|
+
* Content to render when list is loaded
|
|
28
|
+
*/
|
|
29
|
+
children: Snippet<[any[], DatabaseReference, Database]>;
|
|
30
|
+
/**
|
|
31
|
+
* Content to render while loading
|
|
32
|
+
*/
|
|
33
|
+
loading?: Snippet<[]>;
|
|
34
|
+
/**
|
|
35
|
+
* Query constraints for the list
|
|
36
|
+
*/
|
|
37
|
+
queryConstraints?: any[];
|
|
38
|
+
} = $props();
|
|
39
|
+
|
|
40
|
+
// Get Database instance and create references
|
|
41
|
+
let database: Database | null = $state(null);
|
|
42
|
+
let listRef: DatabaseReference | null = $state(null);
|
|
43
|
+
let listService: ReturnType<typeof firekitRealtimeList> | null = $state(null);
|
|
44
|
+
|
|
45
|
+
// Track list state
|
|
46
|
+
let listState = $state({
|
|
47
|
+
loading: true,
|
|
48
|
+
data: startWith ?? [],
|
|
49
|
+
error: null as Error | null
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Subscribe to list changes
|
|
53
|
+
$effect(() => {
|
|
54
|
+
if (!browser) {
|
|
55
|
+
listState = {
|
|
56
|
+
loading: false,
|
|
57
|
+
data: startWith ?? [],
|
|
58
|
+
error: new Error('Realtime Database not available in SSR')
|
|
59
|
+
};
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Initialize database and service
|
|
64
|
+
database = firebaseService.getDatabaseInstance();
|
|
65
|
+
if (!database) {
|
|
66
|
+
listState = {
|
|
67
|
+
loading: false,
|
|
68
|
+
data: [],
|
|
69
|
+
error: new Error('Realtime Database instance not available')
|
|
70
|
+
};
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Create database reference
|
|
75
|
+
listRef = ref(database, path);
|
|
76
|
+
|
|
77
|
+
// Create list service
|
|
78
|
+
listService = firekitRealtimeList(path, startWith);
|
|
79
|
+
|
|
80
|
+
// The service state is reactive, so we can directly access it
|
|
81
|
+
// The effect will re-run when the service state changes
|
|
82
|
+
listState = {
|
|
83
|
+
loading: listService.loading,
|
|
84
|
+
data: listService.list, // Use the processed list with IDs
|
|
85
|
+
error: listService.error
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
</script>
|
|
89
|
+
|
|
90
|
+
{#if !browser}
|
|
91
|
+
{@render children(startWith ?? [], null as any, null as any)}
|
|
92
|
+
{:else if listState.loading}
|
|
93
|
+
{#if loading}
|
|
94
|
+
{@render loading()}
|
|
95
|
+
{:else}
|
|
96
|
+
<div>Loading...</div>
|
|
97
|
+
{/if}
|
|
98
|
+
{:else if listState.error}
|
|
99
|
+
<div class="error">Error: {listState.error.message}</div>
|
|
100
|
+
{:else}
|
|
101
|
+
{@render children(listState.data, listRef!, database!)}
|
|
102
|
+
{/if}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type DatabaseReference, type Database } from 'firebase/database';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Database reference path string (e.g. 'users')
|
|
6
|
+
*/
|
|
7
|
+
path: string;
|
|
8
|
+
/**
|
|
9
|
+
* Initial value to use before list is fetched
|
|
10
|
+
*/
|
|
11
|
+
startWith?: any[];
|
|
12
|
+
/**
|
|
13
|
+
* Content to render when list is loaded
|
|
14
|
+
*/
|
|
15
|
+
children: Snippet<[any[], DatabaseReference, Database]>;
|
|
16
|
+
/**
|
|
17
|
+
* Content to render while loading
|
|
18
|
+
*/
|
|
19
|
+
loading?: Snippet<[]>;
|
|
20
|
+
/**
|
|
21
|
+
* Query constraints for the list
|
|
22
|
+
*/
|
|
23
|
+
queryConstraints?: any[];
|
|
24
|
+
};
|
|
25
|
+
declare const NodeList: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
26
|
+
type NodeList = ReturnType<typeof NodeList>;
|
|
27
|
+
export default NodeList;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { firekitAuth } from '../services/auth.js';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { onDestroy } from 'svelte';
|
|
5
|
+
import type { UserProfile } from '../types/auth.js';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for SignedIn component
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
children
|
|
13
|
+
}: {
|
|
14
|
+
/**
|
|
15
|
+
* Children content to render when user is signed in
|
|
16
|
+
*/
|
|
17
|
+
children: Snippet<[UserProfile]>;
|
|
18
|
+
} = $props();
|
|
19
|
+
|
|
20
|
+
// Get Firebase Auth instance
|
|
21
|
+
const auth = firebaseService.getAuthInstance();
|
|
22
|
+
if (!auth) {
|
|
23
|
+
throw new Error('Firebase Auth instance not available');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Reactive auth state
|
|
27
|
+
let authState = $state(firekitAuth.getState());
|
|
28
|
+
|
|
29
|
+
// Subscribe to auth state changes
|
|
30
|
+
const unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
31
|
+
authState = state;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Cleanup subscription on component destruction
|
|
35
|
+
onDestroy(() => {
|
|
36
|
+
unsubscribe();
|
|
37
|
+
});
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
{#if firekitAuth.isAuthenticated() && authState.user}
|
|
41
|
+
{@render children(authState.user)}
|
|
42
|
+
{/if}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { UserProfile } from '../types/auth.js';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Children content to render when user is signed in
|
|
6
|
+
*/
|
|
7
|
+
children: Snippet<[UserProfile]>;
|
|
8
|
+
};
|
|
9
|
+
declare const SignedIn: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
10
|
+
type SignedIn = ReturnType<typeof SignedIn>;
|
|
11
|
+
export default SignedIn;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { firekitAuth } from '../services/auth.js';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { onDestroy } from 'svelte';
|
|
5
|
+
import type { Auth } from 'firebase/auth';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for SignedOut component
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
children
|
|
13
|
+
}: {
|
|
14
|
+
/**
|
|
15
|
+
* Children content to render when user is signed out
|
|
16
|
+
*/
|
|
17
|
+
children: Snippet<[Auth]>;
|
|
18
|
+
} = $props();
|
|
19
|
+
|
|
20
|
+
// Get Firebase Auth instance
|
|
21
|
+
const auth = firebaseService.getAuthInstance();
|
|
22
|
+
if (!auth) {
|
|
23
|
+
throw new Error('Firebase Auth instance not available');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Reactive auth state
|
|
27
|
+
let authState = $state(firekitAuth.getState());
|
|
28
|
+
|
|
29
|
+
// Subscribe to auth state changes
|
|
30
|
+
const unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
31
|
+
authState = state;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Cleanup subscription on component destruction
|
|
35
|
+
onDestroy(() => {
|
|
36
|
+
unsubscribe();
|
|
37
|
+
});
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
{#if !firekitAuth.isAuthenticated()}
|
|
41
|
+
{@render children(auth)}
|
|
42
|
+
{/if}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Auth } from 'firebase/auth';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Children content to render when user is signed out
|
|
6
|
+
*/
|
|
7
|
+
children: Snippet<[Auth]>;
|
|
8
|
+
};
|
|
9
|
+
declare const SignedOut: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
10
|
+
type SignedOut = ReturnType<typeof SignedOut>;
|
|
11
|
+
export default SignedOut;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ref, type StorageReference } from 'firebase/storage';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { firekitStorageList } from '../services/storage.svelte.js';
|
|
5
|
+
import { browser } from '$app/environment';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for StorageList component
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
ref: path,
|
|
13
|
+
children,
|
|
14
|
+
loading
|
|
15
|
+
}: {
|
|
16
|
+
/**
|
|
17
|
+
* Storage reference path string (e.g. 'images/uid')
|
|
18
|
+
*/
|
|
19
|
+
ref: string;
|
|
20
|
+
/**
|
|
21
|
+
* Content to render when list is loaded
|
|
22
|
+
*/
|
|
23
|
+
children: Snippet<
|
|
24
|
+
[{ items: StorageReference[]; prefixes: StorageReference[] }, StorageReference, any]
|
|
25
|
+
>;
|
|
26
|
+
/**
|
|
27
|
+
* Content to render while loading
|
|
28
|
+
*/
|
|
29
|
+
loading?: Snippet<[]>;
|
|
30
|
+
} = $props();
|
|
31
|
+
|
|
32
|
+
// Get Storage instance and create references
|
|
33
|
+
let storage: any = $state(null);
|
|
34
|
+
let storageRef: StorageReference | null = $state(null);
|
|
35
|
+
let storageListService: ReturnType<typeof firekitStorageList> | null = $state(null);
|
|
36
|
+
|
|
37
|
+
// Track list state
|
|
38
|
+
let listState = $state({
|
|
39
|
+
loading: true,
|
|
40
|
+
list: null as { items: StorageReference[]; prefixes: StorageReference[] } | null,
|
|
41
|
+
error: null as Error | null
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Subscribe to list changes
|
|
45
|
+
$effect(() => {
|
|
46
|
+
if (!browser) {
|
|
47
|
+
listState = {
|
|
48
|
+
loading: false,
|
|
49
|
+
list: null,
|
|
50
|
+
error: new Error('Storage not available in SSR')
|
|
51
|
+
};
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Initialize storage and service
|
|
56
|
+
storage = firebaseService.getStorageInstance();
|
|
57
|
+
if (!storage) {
|
|
58
|
+
listState = {
|
|
59
|
+
loading: false,
|
|
60
|
+
list: null,
|
|
61
|
+
error: new Error('Storage instance not available')
|
|
62
|
+
};
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Create storage reference
|
|
67
|
+
storageRef = ref(storage, path);
|
|
68
|
+
|
|
69
|
+
// Create storage list service
|
|
70
|
+
storageListService = firekitStorageList(path);
|
|
71
|
+
|
|
72
|
+
// The service state is reactive, so we can directly access it
|
|
73
|
+
// The effect will re-run when the service state changes
|
|
74
|
+
listState = {
|
|
75
|
+
loading: storageListService.loading,
|
|
76
|
+
list: {
|
|
77
|
+
items: storageListService.items,
|
|
78
|
+
prefixes: storageListService.prefixes
|
|
79
|
+
},
|
|
80
|
+
error: storageListService.error
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
{#if !browser}
|
|
86
|
+
<div>Storage not available in SSR</div>
|
|
87
|
+
{:else if listState.loading}
|
|
88
|
+
{#if loading}
|
|
89
|
+
{@render loading()}
|
|
90
|
+
{:else}
|
|
91
|
+
<div>Loading...</div>
|
|
92
|
+
{/if}
|
|
93
|
+
{:else if listState.error}
|
|
94
|
+
<div class="error">Error: {listState.error.message}</div>
|
|
95
|
+
{:else if listState.list}
|
|
96
|
+
{@render children(listState.list, storageRef!, storage)}
|
|
97
|
+
{/if}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type StorageReference } from 'firebase/storage';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Storage reference path string (e.g. 'images/uid')
|
|
6
|
+
*/
|
|
7
|
+
ref: string;
|
|
8
|
+
/**
|
|
9
|
+
* Content to render when list is loaded
|
|
10
|
+
*/
|
|
11
|
+
children: Snippet<[
|
|
12
|
+
{
|
|
13
|
+
items: StorageReference[];
|
|
14
|
+
prefixes: StorageReference[];
|
|
15
|
+
},
|
|
16
|
+
StorageReference,
|
|
17
|
+
any
|
|
18
|
+
]>;
|
|
19
|
+
/**
|
|
20
|
+
* Content to render while loading
|
|
21
|
+
*/
|
|
22
|
+
loading?: Snippet<[]>;
|
|
23
|
+
};
|
|
24
|
+
declare const StorageList: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
25
|
+
type StorageList = ReturnType<typeof StorageList>;
|
|
26
|
+
export default StorageList;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ref, type StorageReference, type UploadTaskSnapshot } from 'firebase/storage';
|
|
3
|
+
import { firebaseService } from '../firebase.js';
|
|
4
|
+
import { firekitUploadTask } from '../services/storage.svelte.js';
|
|
5
|
+
import { browser } from '$app/environment';
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
import type { UploadMetadata } from 'firebase/storage';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Props for UploadTask component
|
|
11
|
+
*/
|
|
12
|
+
let {
|
|
13
|
+
ref: path,
|
|
14
|
+
data,
|
|
15
|
+
metadata,
|
|
16
|
+
children
|
|
17
|
+
}: {
|
|
18
|
+
/**
|
|
19
|
+
* Storage reference path string (e.g. 'myFile.txt')
|
|
20
|
+
*/
|
|
21
|
+
ref: string;
|
|
22
|
+
/**
|
|
23
|
+
* File data to upload (Blob, Uint8Array, or ArrayBuffer)
|
|
24
|
+
*/
|
|
25
|
+
data: Blob | Uint8Array | ArrayBuffer;
|
|
26
|
+
/**
|
|
27
|
+
* Optional file metadata
|
|
28
|
+
*/
|
|
29
|
+
metadata?: UploadMetadata;
|
|
30
|
+
/**
|
|
31
|
+
* Content to render during upload
|
|
32
|
+
*/
|
|
33
|
+
children: Snippet<[UploadTaskSnapshot | null, any, number, any]>;
|
|
34
|
+
} = $props();
|
|
35
|
+
|
|
36
|
+
// Get Storage instance and create references
|
|
37
|
+
let storage: any = $state(null);
|
|
38
|
+
let uploadTaskService: ReturnType<typeof firekitUploadTask> | null = $state(null);
|
|
39
|
+
|
|
40
|
+
// Track upload state
|
|
41
|
+
let uploadState = $state({
|
|
42
|
+
loading: true,
|
|
43
|
+
snapshot: null as UploadTaskSnapshot | null,
|
|
44
|
+
progress: 0,
|
|
45
|
+
error: null as Error | null,
|
|
46
|
+
task: null as any
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Subscribe to upload changes
|
|
50
|
+
$effect(() => {
|
|
51
|
+
if (!browser) {
|
|
52
|
+
uploadState = {
|
|
53
|
+
loading: false,
|
|
54
|
+
snapshot: null,
|
|
55
|
+
progress: 0,
|
|
56
|
+
error: new Error('Storage not available in SSR'),
|
|
57
|
+
task: null
|
|
58
|
+
};
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Initialize storage and service
|
|
63
|
+
storage = firebaseService.getStorageInstance();
|
|
64
|
+
if (!storage) {
|
|
65
|
+
uploadState = {
|
|
66
|
+
loading: false,
|
|
67
|
+
snapshot: null,
|
|
68
|
+
progress: 0,
|
|
69
|
+
error: new Error('Storage instance not available'),
|
|
70
|
+
task: null
|
|
71
|
+
};
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Convert data to File if it's not already a File
|
|
76
|
+
let file: File;
|
|
77
|
+
if (data instanceof File) {
|
|
78
|
+
file = data;
|
|
79
|
+
} else if (data instanceof Blob) {
|
|
80
|
+
file = new File([data], path.split('/').pop() || 'file', { type: metadata?.contentType });
|
|
81
|
+
} else {
|
|
82
|
+
// For Uint8Array or ArrayBuffer, create a Blob first
|
|
83
|
+
const blob = new Blob([data], { type: metadata?.contentType });
|
|
84
|
+
file = new File([blob], path.split('/').pop() || 'file');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Create upload task service
|
|
88
|
+
uploadTaskService = firekitUploadTask(path, file);
|
|
89
|
+
|
|
90
|
+
// The service state is reactive, so we can directly access it
|
|
91
|
+
// The effect will re-run when the service state changes
|
|
92
|
+
uploadState = {
|
|
93
|
+
loading: !uploadTaskService.completed,
|
|
94
|
+
snapshot: uploadTaskService.snapshot,
|
|
95
|
+
progress: uploadTaskService.progress,
|
|
96
|
+
error: uploadTaskService.error,
|
|
97
|
+
task: uploadTaskService
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
{#if !browser}
|
|
103
|
+
<div>Storage not available in SSR</div>
|
|
104
|
+
{:else if uploadState.error}
|
|
105
|
+
<div class="error">Error: {uploadState.error.message}</div>
|
|
106
|
+
{:else}
|
|
107
|
+
{@render children(uploadState.snapshot, uploadState.task, uploadState.progress, storage)}
|
|
108
|
+
{/if}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type UploadTaskSnapshot } from 'firebase/storage';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { UploadMetadata } from 'firebase/storage';
|
|
4
|
+
type $$ComponentProps = {
|
|
5
|
+
/**
|
|
6
|
+
* Storage reference path string (e.g. 'myFile.txt')
|
|
7
|
+
*/
|
|
8
|
+
ref: string;
|
|
9
|
+
/**
|
|
10
|
+
* File data to upload (Blob, Uint8Array, or ArrayBuffer)
|
|
11
|
+
*/
|
|
12
|
+
data: Blob | Uint8Array | ArrayBuffer;
|
|
13
|
+
/**
|
|
14
|
+
* Optional file metadata
|
|
15
|
+
*/
|
|
16
|
+
metadata?: UploadMetadata;
|
|
17
|
+
/**
|
|
18
|
+
* Content to render during upload
|
|
19
|
+
*/
|
|
20
|
+
children: Snippet<[UploadTaskSnapshot | null, any, number, any]>;
|
|
21
|
+
};
|
|
22
|
+
declare const UploadTask: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
23
|
+
type UploadTask = ReturnType<typeof UploadTask>;
|
|
24
|
+
export default UploadTask;
|