sveltedfire 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2025 Michael Madison
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # SveltedFire
2
+
3
+ Inspired by Sveltefire, but taking a slightly different approach. Due to several changes in Svelte 5, I decided to build this package from the ground up.
4
+
5
+ SveltedFire is a client-side library intended to facilitate communication with Firebase services (currently Firestore and Firebase Auth). It consists of helper utilities for common tasks and components to handle authentication states. SveltedFire has *not* been tested for use as a server-side library.
6
+
7
+ This is still a work in progress. Suggestions are welcome. Note that tests are currently *not* configured.
8
+
9
+ ## Usage
10
+
11
+ ### Initializing
12
+
13
+ In your `hooks.client.ts` file, import initFirebase and initialize with your Firebase configuration:
14
+
15
+ ```typescript
16
+ import type { ClientInit } from "@sveltejs/kit";
17
+ import { initFirebase } from "sveltedfire";
18
+ import firebaseConfig from './config.json'
19
+
20
+ export const init: ClientInit = async () => {
21
+ await initFirebase(firebaseConfig)
22
+ }
23
+ ```
24
+
25
+ This guarantees that firebase has been initialized prior to any data loading calls.
26
+
27
+ ### Authentication
28
+
29
+ You can use the SignedIn/SignoutOut components to selectively render based on the user's authentication state. In addition, the `signOut` and `signInWithGoogle` helpers are available. Note that the signInWithGoogle automatically performs a signInWithPopup. If you would rather use signInWithRedirect, you will need to implement that manually.
30
+
31
+ ### Fetching/Querying
32
+
33
+ To fetch a single document, use the `fetchDoc` method which accepts the document path as arguments. E.g.,
34
+
35
+ ```typescript
36
+ fetchDoc('pages', params.slug)
37
+ fetchDoc('pages', 'home', 'posts', params.postId)
38
+ ```
39
+
40
+ To perform a query for one or more documents, use the `fetchDocs` method. This is a double-invoked function. The first invocation sets the collection path. The second invocation accepts either three arguments (field name, comparator, value), no arguments for a simple collection fetch, or a single argument of an already formed QueryFieldFilterConstraint or a QueryCompositeFilterConstraint. E.g.,
41
+ ```typescript
42
+ fetchDocs('pages')() // fetch all pages
43
+ fetchDocs('pages')('public', '==', true) // fetch all public pages
44
+ fetchDocs('pages')(and(where('published_at', '>=', '2025-01-01'), where('published_at', '<=', '2025-01-31'))) // fetch all pages published in the month of January
45
+ ```
46
+
47
+ In the case of both `fetchDoc` and `fetchDocs`, the documents are returned as bare objects with the ID field injected into both the `id` and `_id` fields. If your data already defines an `id` field, it will be returned as-is; the `_id` field will always reference the actual ID of the object.
48
+
49
+
50
+ --------------
51
+
52
+ ## Building
53
+
54
+ To build your library:
55
+
56
+ ```bash
57
+ npm run package
58
+ ```
59
+
60
+ To create a production version of your showcase app:
61
+
62
+ ```bash
63
+ npm run build
64
+ ```
65
+
66
+ You can preview the production build with `npm run preview`.
67
+
68
+ > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
69
+
70
+ ## Publishing
71
+
72
+ Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
73
+
74
+ To publish your library to [npm](https://www.npmjs.com):
75
+
76
+ ```bash
77
+ npm publish
78
+ ```
@@ -0,0 +1,8 @@
1
+ export { initFirebase } from "./sveltedfire/init.js";
2
+ export { fetchDoc } from './sveltedfire/utilities/fetchDoc.js';
3
+ export { fetchDocs } from './sveltedfire/utilities/fetchDocs.js';
4
+ export { signInWithGoogle } from './sveltedfire/auth/signInWithGoogle.js';
5
+ export { signOut } from './sveltedfire/auth/signOut.js';
6
+ import SignedIn from './sveltedfire/components/SignedIn.svelte';
7
+ import SignedOut from './sveltedfire/components/SignedOut.svelte';
8
+ export { SignedIn, SignedOut };
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ // Reexport your entry components here
2
+ export { initFirebase } from "./sveltedfire/init.js";
3
+ export { fetchDoc } from './sveltedfire/utilities/fetchDoc.js';
4
+ export { fetchDocs } from './sveltedfire/utilities/fetchDocs.js';
5
+ export { signInWithGoogle } from './sveltedfire/auth/signInWithGoogle.js';
6
+ export { signOut } from './sveltedfire/auth/signOut.js';
7
+ import SignedIn from './sveltedfire/components/SignedIn.svelte';
8
+ import SignedOut from './sveltedfire/components/SignedOut.svelte';
9
+ export { SignedIn, SignedOut };
@@ -0,0 +1 @@
1
+ export declare const signInWithGoogle: () => void;
@@ -0,0 +1,6 @@
1
+ import { getAuth, GoogleAuthProvider, signInWithPopup } from "firebase/auth";
2
+ export const signInWithGoogle = () => {
3
+ const auth = getAuth();
4
+ const provider = new GoogleAuthProvider();
5
+ signInWithPopup(auth, provider);
6
+ };
@@ -0,0 +1 @@
1
+ export declare const signOut: () => void;
@@ -0,0 +1,5 @@
1
+ import { getAuth } from "firebase/auth";
2
+ export const signOut = () => {
3
+ const auth = getAuth();
4
+ auth.signOut();
5
+ };
@@ -0,0 +1,15 @@
1
+ <script lang="ts">
2
+ const { children } = $props()
3
+ import { getAuth } from 'firebase/auth'
4
+
5
+ const auth = getAuth()
6
+ let user = $state(null)
7
+ auth.onAuthStateChanged(u => {
8
+ console.log('User is', u)
9
+ user = u
10
+ })
11
+ </script>
12
+
13
+ {#if user}
14
+ {@render children()}
15
+ {/if}
@@ -0,0 +1,5 @@
1
+ declare const SignedIn: import("svelte").Component<{
2
+ children: any;
3
+ }, {}, "">;
4
+ type SignedIn = ReturnType<typeof SignedIn>;
5
+ export default SignedIn;
@@ -0,0 +1,15 @@
1
+ <script lang="ts">
2
+ const { children } = $props()
3
+ import { getAuth } from 'firebase/auth'
4
+
5
+ const auth = getAuth()
6
+ let user = $state(null)
7
+ auth.onAuthStateChanged(u => {
8
+ console.log('User is', u)
9
+ user = u
10
+ })
11
+ </script>
12
+
13
+ {#if !user}
14
+ {@render children()}
15
+ {/if}
@@ -0,0 +1,5 @@
1
+ declare const SignedOut: import("svelte").Component<{
2
+ children: any;
3
+ }, {}, "">;
4
+ type SignedOut = ReturnType<typeof SignedOut>;
5
+ export default SignedOut;
@@ -0,0 +1,2 @@
1
+ import { type FirebaseOptions } from "firebase/app";
2
+ export declare const initFirebase: (firebaseConfig: FirebaseOptions) => Promise<void>;
@@ -0,0 +1,7 @@
1
+ import { initializeApp } from "firebase/app";
2
+ export const initFirebase = async (firebaseConfig) => {
3
+ await new Promise((res, rej) => {
4
+ initializeApp(firebaseConfig);
5
+ res(null);
6
+ });
7
+ };
@@ -0,0 +1,4 @@
1
+ export declare const fetchDoc: (...docPath: Array<string>) => Promise<{
2
+ _id: string;
3
+ id: string;
4
+ } | null>;
@@ -0,0 +1,20 @@
1
+ import { getFirestore, collection, doc, getDoc } from "firebase/firestore";
2
+ export const fetchDoc = async (...docPath) => {
3
+ const db = getFirestore();
4
+ let theRef;
5
+ if (docPath.length > 2) {
6
+ theRef = doc(collection(db, docPath[0], ...docPath.slice(1, -1)), docPath[-1]);
7
+ }
8
+ else {
9
+ theRef = doc(collection(db, docPath[0]), docPath[-1]);
10
+ }
11
+ const theDoc = await getDoc(theRef);
12
+ if (!theDoc.exists()) {
13
+ return null;
14
+ }
15
+ return {
16
+ id: theDoc.id,
17
+ ...theDoc.data(),
18
+ _id: theDoc.id
19
+ };
20
+ };
@@ -0,0 +1,8 @@
1
+ import { QueryCompositeFilterConstraint } from "firebase/firestore";
2
+ export declare const fetchDocs: (...collectionPath: Array<string>) => (qOrField?: QueryCompositeFilterConstraint | string | null, comparator?: string | null, value?: string | boolean | number | null) => Promise<{
3
+ docs: {
4
+ _id: string;
5
+ id: string;
6
+ }[];
7
+ count: number;
8
+ }>;
@@ -0,0 +1,30 @@
1
+ import { getFirestore, collection, doc, getDocs, Query, QueryFieldFilterConstraint, where, query, QueryCompositeFilterConstraint } from "firebase/firestore";
2
+ export const fetchDocs = (...collectionPath) => async (qOrField = null, comparator = null, value = null) => {
3
+ const db = getFirestore();
4
+ let theRef;
5
+ if (collectionPath.length > 1) {
6
+ theRef = collection(db, collectionPath[0], ...collectionPath.slice(1));
7
+ }
8
+ else {
9
+ theRef = collection(db, collectionPath[0]);
10
+ }
11
+ let theQuery = query(theRef);
12
+ if (qOrField) {
13
+ if (comparator) {
14
+ theQuery = query(theRef, where(qOrField, comparator, value));
15
+ }
16
+ else {
17
+ if (qOrField instanceof QueryFieldFilterConstraint) {
18
+ theQuery = query(theRef, qOrField);
19
+ }
20
+ else if (qOrField instanceof QueryCompositeFilterConstraint) {
21
+ theQuery = query(theRef, qOrField);
22
+ }
23
+ }
24
+ }
25
+ const theDocs = await getDocs(theQuery);
26
+ return {
27
+ docs: theDocs.docs.map(d => ({ id: d.id, ...d.data(), _id: d.id })),
28
+ count: theDocs.docs.length
29
+ };
30
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "sveltedfire",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev": "vite dev",
6
+ "build": "vite build && npm run prepack",
7
+ "preview": "vite preview",
8
+ "prepare": "svelte-kit sync || echo ''",
9
+ "prepack": "svelte-kit sync && svelte-package && publint",
10
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12
+ "format": "prettier --write .",
13
+ "lint": "prettier --check . && eslint .",
14
+ "test:unit": "vitest",
15
+ "test": "npm run test:unit -- --run && npm run test:e2e",
16
+ "test:e2e": "playwright test"
17
+ },
18
+ "license": "MIT",
19
+ "files": [
20
+ "dist",
21
+ "!dist/**/*.test.*",
22
+ "!dist/**/*.spec.*"
23
+ ],
24
+ "sideEffects": [
25
+ "**/*.css"
26
+ ],
27
+ "svelte": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "type": "module",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "svelte": "./dist/index.js"
34
+ }
35
+ },
36
+ "peerDependencies": {
37
+ "svelte": "^5.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@eslint/compat": "^1.2.5",
41
+ "@eslint/js": "^9.18.0",
42
+ "@playwright/test": "^1.49.1",
43
+ "@sveltejs/adapter-auto": "^4.0.0",
44
+ "@sveltejs/kit": "^2.16.0",
45
+ "@sveltejs/package": "^2.0.0",
46
+ "@sveltejs/vite-plugin-svelte": "^5.0.0",
47
+ "@testing-library/jest-dom": "^6.6.3",
48
+ "@testing-library/svelte": "^5.2.4",
49
+ "eslint": "^9.18.0",
50
+ "eslint-config-prettier": "^10.0.1",
51
+ "eslint-plugin-svelte": "^2.46.1",
52
+ "globals": "^15.14.0",
53
+ "jsdom": "^25.0.1",
54
+ "prettier": "^3.4.2",
55
+ "prettier-plugin-svelte": "^3.3.3",
56
+ "publint": "^0.3.2",
57
+ "svelte": "^5.0.0",
58
+ "svelte-check": "^4.0.0",
59
+ "typescript": "^5.0.0",
60
+ "typescript-eslint": "^8.20.0",
61
+ "vite": "^6.0.0",
62
+ "vitest": "^3.0.0"
63
+ },
64
+ "dependencies": {
65
+ "firebase": "^11.3.1"
66
+ }
67
+ }