svelte-firekit 0.0.21 → 0.0.22

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.
@@ -1,15 +1,141 @@
1
+ /**
2
+ * @module FirekitAwaitableDoc
3
+ */
1
4
  import { type DocumentReference, type DocumentData } from "firebase/firestore";
5
+ /**
6
+ * Provides real-time document subscription with state management.
7
+ * Automatically handles document data updates, loading states, and error handling.
8
+ *
9
+ * @class
10
+ * @template T Type of document data
11
+ * @example
12
+ * ```typescript
13
+ * interface UserProfile {
14
+ * id: string;
15
+ * name: string;
16
+ * email: string;
17
+ * }
18
+ *
19
+ * // Create document subscription
20
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
21
+ * 'users/123',
22
+ * { id: '123', name: 'Loading...', email: '' }
23
+ * );
24
+ *
25
+ * // Access reactive state
26
+ * if (userDoc.loading) {
27
+ * console.log('Loading user data...');
28
+ * } else if (userDoc.error) {
29
+ * console.error('Error:', userDoc.error);
30
+ * } else {
31
+ * console.log('User data:', userDoc.data);
32
+ * }
33
+ *
34
+ * // Get fresh data
35
+ * const freshData = await userDoc.getData();
36
+ * ```
37
+ */
2
38
  declare class FirekitAwaitableDoc<T> {
39
+ /**
40
+ * Current document data.
41
+ * Uses SvelteKit's $state for reactivity.
42
+ * @private
43
+ */
3
44
  private _data;
45
+ /**
46
+ * Loading state indicator.
47
+ * Uses SvelteKit's $state for reactivity.
48
+ * @private
49
+ */
4
50
  private _loading;
51
+ /**
52
+ * Error state container.
53
+ * Uses SvelteKit's $state for reactivity.
54
+ * @private
55
+ */
5
56
  private _error;
57
+ /**
58
+ * Firestore document reference.
59
+ * @private
60
+ */
6
61
  private docRef;
62
+ /**
63
+ * Creates a document subscription.
64
+ * Initializes document subscription if in browser environment.
65
+ *
66
+ * @param {string | DocumentReference<T>} ref - Document path or reference
67
+ * @param {T} [startWith] - Initial data before fetch completes
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const doc = new FirekitAwaitableDoc('users/123', defaultUser);
72
+ * ```
73
+ */
7
74
  constructor(ref: string | DocumentReference<T>, startWith?: T);
75
+ /**
76
+ * Initializes document subscription.
77
+ * Sets up real-time updates and handles initial data fetch.
78
+ *
79
+ * @private
80
+ * @param {string | DocumentReference<T>} ref - Document path or reference
81
+ * @returns {Promise<void>}
82
+ *
83
+ * @throws {Error} If document initialization fails
84
+ */
8
85
  private initializeDoc;
86
+ /**
87
+ * Fetches fresh document data.
88
+ * Makes a new request to Firestore instead of using cached data.
89
+ *
90
+ * @returns {Promise<T | null>} Document data or null if not found
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const freshData = await doc.getData();
95
+ * if (freshData) {
96
+ * console.log('Fresh data:', freshData);
97
+ * }
98
+ * ```
99
+ */
9
100
  getData(): Promise<T | null>;
101
+ /**
102
+ * Gets current document data.
103
+ * @returns {T | null} Current document data or null if not loaded
104
+ */
10
105
  get data(): T | null;
106
+ /**
107
+ * Gets current loading state.
108
+ * @returns {boolean} True if document is loading
109
+ */
11
110
  get loading(): boolean;
111
+ /**
112
+ * Gets current error state.
113
+ * @returns {Error | null} Error object if an error occurred, null otherwise
114
+ */
12
115
  get error(): Error | null;
13
116
  }
117
+ /**
118
+ * Creates a document subscription.
119
+ * Factory function for creating FirekitAwaitableDoc instances.
120
+ *
121
+ * @template T Document data type
122
+ * @param {string} path Document path
123
+ * @param {T} [startWith] Initial data before fetch completes
124
+ * @returns {FirekitAwaitableDoc<T>} Document subscription instance
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * interface UserProfile {
129
+ * id: string;
130
+ * name: string;
131
+ * email: string;
132
+ * }
133
+ *
134
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
135
+ * 'users/123',
136
+ * { id: '123', name: 'Loading...', email: '' }
137
+ * );
138
+ * ```
139
+ */
14
140
  export declare function firekitAwaitableDoc<T extends DocumentData>(path: string, startWith?: T): FirekitAwaitableDoc<T>;
15
141
  export {};
@@ -1,17 +1,94 @@
1
+ /**
2
+ * @module FirekitAwaitableDoc
3
+ */
1
4
  import { doc, getDoc, onSnapshot } from "firebase/firestore";
2
5
  import { firebaseService } from "../firebase.js";
3
6
  import { browser } from "$app/environment";
7
+ /**
8
+ * Provides real-time document subscription with state management.
9
+ * Automatically handles document data updates, loading states, and error handling.
10
+ *
11
+ * @class
12
+ * @template T Type of document data
13
+ * @example
14
+ * ```typescript
15
+ * interface UserProfile {
16
+ * id: string;
17
+ * name: string;
18
+ * email: string;
19
+ * }
20
+ *
21
+ * // Create document subscription
22
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
23
+ * 'users/123',
24
+ * { id: '123', name: 'Loading...', email: '' }
25
+ * );
26
+ *
27
+ * // Access reactive state
28
+ * if (userDoc.loading) {
29
+ * console.log('Loading user data...');
30
+ * } else if (userDoc.error) {
31
+ * console.error('Error:', userDoc.error);
32
+ * } else {
33
+ * console.log('User data:', userDoc.data);
34
+ * }
35
+ *
36
+ * // Get fresh data
37
+ * const freshData = await userDoc.getData();
38
+ * ```
39
+ */
4
40
  class FirekitAwaitableDoc {
41
+ /**
42
+ * Current document data.
43
+ * Uses SvelteKit's $state for reactivity.
44
+ * @private
45
+ */
5
46
  _data = $state(null);
47
+ /**
48
+ * Loading state indicator.
49
+ * Uses SvelteKit's $state for reactivity.
50
+ * @private
51
+ */
6
52
  _loading = $state(true);
53
+ /**
54
+ * Error state container.
55
+ * Uses SvelteKit's $state for reactivity.
56
+ * @private
57
+ */
7
58
  _error = $state(null);
59
+ /**
60
+ * Firestore document reference.
61
+ * @private
62
+ */
8
63
  docRef = null;
64
+ /**
65
+ * Creates a document subscription.
66
+ * Initializes document subscription if in browser environment.
67
+ *
68
+ * @param {string | DocumentReference<T>} ref - Document path or reference
69
+ * @param {T} [startWith] - Initial data before fetch completes
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const doc = new FirekitAwaitableDoc('users/123', defaultUser);
74
+ * ```
75
+ */
9
76
  constructor(ref, startWith) {
10
77
  this._data = startWith ?? null;
11
78
  if (browser) {
12
79
  this.initializeDoc(ref);
13
80
  }
14
81
  }
82
+ /**
83
+ * Initializes document subscription.
84
+ * Sets up real-time updates and handles initial data fetch.
85
+ *
86
+ * @private
87
+ * @param {string | DocumentReference<T>} ref - Document path or reference
88
+ * @returns {Promise<void>}
89
+ *
90
+ * @throws {Error} If document initialization fails
91
+ */
15
92
  async initializeDoc(ref) {
16
93
  try {
17
94
  const firestore = firebaseService.getDbInstance();
@@ -36,22 +113,71 @@ class FirekitAwaitableDoc {
36
113
  this._loading = false;
37
114
  }
38
115
  }
116
+ /**
117
+ * Fetches fresh document data.
118
+ * Makes a new request to Firestore instead of using cached data.
119
+ *
120
+ * @returns {Promise<T | null>} Document data or null if not found
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const freshData = await doc.getData();
125
+ * if (freshData) {
126
+ * console.log('Fresh data:', freshData);
127
+ * }
128
+ * ```
129
+ */
39
130
  async getData() {
40
131
  if (!this.docRef)
41
132
  return null;
42
133
  const snapshot = await getDoc(this.docRef);
43
134
  return snapshot.exists() ? { id: snapshot.id, ...snapshot.data() } : null;
44
135
  }
136
+ /**
137
+ * Gets current document data.
138
+ * @returns {T | null} Current document data or null if not loaded
139
+ */
45
140
  get data() {
46
141
  return this._data;
47
142
  }
143
+ /**
144
+ * Gets current loading state.
145
+ * @returns {boolean} True if document is loading
146
+ */
48
147
  get loading() {
49
148
  return this._loading;
50
149
  }
150
+ /**
151
+ * Gets current error state.
152
+ * @returns {Error | null} Error object if an error occurred, null otherwise
153
+ */
51
154
  get error() {
52
155
  return this._error;
53
156
  }
54
157
  }
158
+ /**
159
+ * Creates a document subscription.
160
+ * Factory function for creating FirekitAwaitableDoc instances.
161
+ *
162
+ * @template T Document data type
163
+ * @param {string} path Document path
164
+ * @param {T} [startWith] Initial data before fetch completes
165
+ * @returns {FirekitAwaitableDoc<T>} Document subscription instance
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * interface UserProfile {
170
+ * id: string;
171
+ * name: string;
172
+ * email: string;
173
+ * }
174
+ *
175
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
176
+ * 'users/123',
177
+ * { id: '123', name: 'Loading...', email: '' }
178
+ * );
179
+ * ```
180
+ */
55
181
  export function firekitAwaitableDoc(path, startWith) {
56
182
  return new FirekitAwaitableDoc(path, startWith);
57
183
  }
@@ -1,17 +1,96 @@
1
+ /**
2
+ * @module FirekitCollection
3
+ */
1
4
  import { type CollectionReference, type DocumentData, type QueryConstraint } from "firebase/firestore";
5
+ /**
6
+ * Manages real-time Firestore collection subscriptions with reactive state
7
+ * @class
8
+ * @template T Collection document type
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * interface User {
13
+ * id: string;
14
+ * name: string;
15
+ * email: string;
16
+ * }
17
+ *
18
+ * // Create collection subscription
19
+ * const users = firekitCollection<User>('users',
20
+ * where('active', '==', true),
21
+ * orderBy('name')
22
+ * );
23
+ *
24
+ * // Access reactive state
25
+ * console.log(users.data); // Array of documents
26
+ * console.log(users.loading); // Loading state
27
+ * console.log(users.error); // Error state
28
+ * console.log(users.empty); // Whether collection is empty
29
+ * console.log(users.size); // Number of documents
30
+ * ```
31
+ */
2
32
  declare class FirekitCollection<T> {
33
+ /** Current collection data */
3
34
  private _data;
35
+ /** Loading state */
4
36
  private _loading;
37
+ /** Error state */
5
38
  private _error;
39
+ /** Collection reference */
6
40
  private colRef;
41
+ /** Query reference */
7
42
  private queryRef;
43
+ /**
44
+ * Creates a collection subscription
45
+ * @param {string} path Collection path
46
+ * @param {...QueryConstraint[]} queryConstraints Query constraints (where, orderBy, limit, etc.)
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const collection = new FirekitCollection('users',
51
+ * where('age', '>=', 18),
52
+ * orderBy('name', 'asc'),
53
+ * limit(10)
54
+ * );
55
+ * ```
56
+ */
8
57
  constructor(path: string, ...queryConstraints: QueryConstraint[]);
58
+ /** Gets current collection data */
9
59
  get data(): T[];
60
+ /** Gets loading state */
10
61
  get loading(): boolean;
62
+ /** Gets error state */
11
63
  get error(): Error | null;
64
+ /** Checks if collection is empty */
12
65
  get empty(): boolean;
66
+ /** Gets number of documents in collection */
13
67
  get size(): number;
14
- get ref(): CollectionReference<T, DocumentData>;
68
+ /**
69
+ * Gets collection reference
70
+ * @throws {Error} If collection reference is not available
71
+ */
72
+ get ref(): CollectionReference<T>;
15
73
  }
74
+ /**
75
+ * Creates a collection subscription
76
+ * @template T Collection document type
77
+ * @param {string} path Collection path
78
+ * @param {...QueryConstraint[]} queryConstraints Query constraints
79
+ * @returns {FirekitCollection<T>} Collection subscription instance
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * interface Post {
84
+ * id: string;
85
+ * title: string;
86
+ * authorId: string;
87
+ * }
88
+ *
89
+ * const posts = firekitCollection<Post>('posts',
90
+ * where('authorId', '==', currentUserId),
91
+ * orderBy('createdAt', 'desc')
92
+ * );
93
+ * ```
94
+ */
16
95
  export declare function firekitCollection<T extends DocumentData>(path: string, ...queryConstraints: QueryConstraint[]): FirekitCollection<T>;
17
96
  export {};
@@ -1,12 +1,61 @@
1
+ /**
2
+ * @module FirekitCollection
3
+ */
1
4
  import { collection, query, onSnapshot } from "firebase/firestore";
2
5
  import { firebaseService } from "../firebase.js";
3
6
  import { browser } from "$app/environment";
7
+ /**
8
+ * Manages real-time Firestore collection subscriptions with reactive state
9
+ * @class
10
+ * @template T Collection document type
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface User {
15
+ * id: string;
16
+ * name: string;
17
+ * email: string;
18
+ * }
19
+ *
20
+ * // Create collection subscription
21
+ * const users = firekitCollection<User>('users',
22
+ * where('active', '==', true),
23
+ * orderBy('name')
24
+ * );
25
+ *
26
+ * // Access reactive state
27
+ * console.log(users.data); // Array of documents
28
+ * console.log(users.loading); // Loading state
29
+ * console.log(users.error); // Error state
30
+ * console.log(users.empty); // Whether collection is empty
31
+ * console.log(users.size); // Number of documents
32
+ * ```
33
+ */
4
34
  class FirekitCollection {
35
+ /** Current collection data */
5
36
  _data = $state([]);
37
+ /** Loading state */
6
38
  _loading = $state(true);
39
+ /** Error state */
7
40
  _error = $state(null);
41
+ /** Collection reference */
8
42
  colRef = null;
43
+ /** Query reference */
9
44
  queryRef = null;
45
+ /**
46
+ * Creates a collection subscription
47
+ * @param {string} path Collection path
48
+ * @param {...QueryConstraint[]} queryConstraints Query constraints (where, orderBy, limit, etc.)
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const collection = new FirekitCollection('users',
53
+ * where('age', '>=', 18),
54
+ * orderBy('name', 'asc'),
55
+ * limit(10)
56
+ * );
57
+ * ```
58
+ */
10
59
  constructor(path, ...queryConstraints) {
11
60
  if (browser) {
12
61
  try {
@@ -31,21 +80,30 @@ class FirekitCollection {
31
80
  }
32
81
  }
33
82
  }
83
+ /** Gets current collection data */
34
84
  get data() {
35
85
  return this._data;
36
86
  }
87
+ /** Gets loading state */
37
88
  get loading() {
38
89
  return this._loading;
39
90
  }
91
+ /** Gets error state */
40
92
  get error() {
41
93
  return this._error;
42
94
  }
95
+ /** Checks if collection is empty */
43
96
  get empty() {
44
97
  return this._data.length === 0;
45
98
  }
99
+ /** Gets number of documents in collection */
46
100
  get size() {
47
101
  return this._data.length;
48
102
  }
103
+ /**
104
+ * Gets collection reference
105
+ * @throws {Error} If collection reference is not available
106
+ */
49
107
  get ref() {
50
108
  if (!this.colRef) {
51
109
  throw new Error("Collection reference is not available");
@@ -53,6 +111,27 @@ class FirekitCollection {
53
111
  return this.colRef;
54
112
  }
55
113
  }
114
+ /**
115
+ * Creates a collection subscription
116
+ * @template T Collection document type
117
+ * @param {string} path Collection path
118
+ * @param {...QueryConstraint[]} queryConstraints Query constraints
119
+ * @returns {FirekitCollection<T>} Collection subscription instance
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * interface Post {
124
+ * id: string;
125
+ * title: string;
126
+ * authorId: string;
127
+ * }
128
+ *
129
+ * const posts = firekitCollection<Post>('posts',
130
+ * where('authorId', '==', currentUserId),
131
+ * orderBy('createdAt', 'desc')
132
+ * );
133
+ * ```
134
+ */
56
135
  export function firekitCollection(path, ...queryConstraints) {
57
136
  return new FirekitCollection(path, ...queryConstraints);
58
137
  }
@@ -1,16 +1,90 @@
1
+ /**
2
+ * @module FirekitDoc
3
+ */
1
4
  import { DocumentReference } from "firebase/firestore";
5
+ /**
6
+ * Manages real-time Firestore document subscriptions with reactive state
7
+ * @class
8
+ * @template T Document data type
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * interface User {
13
+ * id: string;
14
+ * name: string;
15
+ * email: string;
16
+ * }
17
+ *
18
+ * // Create document subscription
19
+ * const userDoc = firekitDoc<User>('users/123', {
20
+ * id: '123',
21
+ * name: 'Loading...',
22
+ * email: ''
23
+ * });
24
+ * ```
25
+ */
2
26
  declare class FirekitDoc<T> {
27
+ /** Current document data */
3
28
  private _data;
29
+ /** Loading state */
4
30
  private _loading;
31
+ /** Error state */
5
32
  private _error;
33
+ /** Document reference */
6
34
  private docRef;
35
+ /**
36
+ * Creates a document subscription
37
+ * @param {string | DocumentReference<T>} ref Document path or reference
38
+ * @param {T} [startWith] Initial data before fetch completes
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const doc = new FirekitDoc('users/123', defaultUser);
43
+ * // or
44
+ * const doc = new FirekitDoc(docRef, defaultUser);
45
+ * ```
46
+ */
7
47
  constructor(ref: string | DocumentReference<T>, startWith?: T);
48
+ /** Gets current document data */
8
49
  get data(): T | null;
50
+ /** Gets document ID */
9
51
  get id(): string;
52
+ /** Gets loading state */
10
53
  get loading(): boolean;
54
+ /** Gets error state */
11
55
  get error(): Error | null;
12
- get ref(): DocumentReference<T, import("@firebase/firestore").DocumentData>;
56
+ /**
57
+ * Gets document reference
58
+ * @throws {Error} If document reference is not available
59
+ */
60
+ get ref(): DocumentReference<T>;
61
+ /** Checks if document exists */
13
62
  get exists(): boolean;
14
63
  }
64
+ /**
65
+ * Creates a document subscription
66
+ * @template T Document data type
67
+ * @param {string | DocumentReference<T>} ref Document path or reference
68
+ * @param {T} [startWith] Initial data before fetch completes
69
+ * @returns {FirekitDoc<T>} Document subscription instance
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const userDoc = firekitDoc<User>('users/123', {
74
+ * id: '123',
75
+ * name: 'Loading...',
76
+ * email: ''
77
+ * });
78
+ *
79
+ * // Access reactive state
80
+ * if (userDoc.loading) {
81
+ * console.log('Loading...');
82
+ * } else if (userDoc.error) {
83
+ * console.error(userDoc.error);
84
+ * } else if (userDoc.exists) {
85
+ * console.log(userDoc.data);
86
+ * }
87
+ * ```
88
+ */
15
89
  export declare function firekitDoc<T>(ref: string | DocumentReference<T>, startWith?: T): FirekitDoc<T>;
16
90
  export {};