@purr-react-core/services.firebase 0.0.12
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 +21 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +110 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dinh Thien Phuc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var firestore=require('firebase/firestore'),utils_helpers=require('@purr-react-core/utils.helpers');var R=async(t,o)=>{let e=firestore.collection(t,o),{error:n,result:r}=await utils_helpers.tryDo(firestore.getDocs(e));if(n)return {error:n,result:null};let s=r?.docs?.map?.(u=>({...u.data(),id:u.id}));return {error:null,result:utils_helpers.cast(s)}},h=async(t,o,e)=>{let n=firestore.doc(t,o,e),{error:r,result:s}=await utils_helpers.tryDo(firestore.getDoc(n));return r?{error:r,result:null}:{error:null,result:utils_helpers.cast(s?.data())}},w=async(t,o,e)=>{let n=firestore.collection(t,o),{error:r,result:s}=await utils_helpers.tryDo(firestore.addDoc(n,e));return r?{error:r,result:null}:{error:null,result:utils_helpers.cast(s)}},P=async(t,o,e)=>{let n=firestore.doc(t,o,e.id),{error:r,result:s}=await utils_helpers.tryDo(firestore.setDoc(n,e,{merge:true}));return r?{error:r,result:null}:{error:null,result:s}},I=async(t,o,e)=>{let n=firestore.doc(t,o,e),{error:r,result:s}=await utils_helpers.tryDo(firestore.deleteDoc(n));return r?{error:r,result:null}:{error:null,result:s}};var B=async t=>(await import('firebase/app').then(e=>e.default.initializeApp))(t),Q=async t=>(await import('firebase/firestore').then(e=>e.default.getFirestore))(t),z=async t=>(await import('firebase/storage').then(e=>e.default.getStorage))(t);exports.addDocument=w;exports.deleteDocument=I;exports.getAllDocuments=R;exports.getDocumentById=h;exports.getFireStorage=z;exports.getFireStore=Q;exports.initFirebaseApp=B;exports.setDocument=P;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Firestore, DocumentData } from 'firebase/firestore';
|
|
2
|
+
import { TAsyncBoundary } from '@purr-react-core/utils.definitions';
|
|
3
|
+
import { FirebaseApp } from 'firebase/app';
|
|
4
|
+
import { FirebaseStorage } from 'firebase/storage';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents the configuration for a Firebase project.
|
|
8
|
+
*
|
|
9
|
+
* @interface IFirebaseConfig
|
|
10
|
+
* @property {string} [apiKey] - The API key for the Firebase project.
|
|
11
|
+
* @property {string} [authDomain] - The authentication domain for the Firebase project.
|
|
12
|
+
* @property {string} [projectId] - The project ID for the Firebase project.
|
|
13
|
+
* @property {string} [storageBucket] - The storage bucket for the Firebase project.
|
|
14
|
+
* @property {string} [messagingSenderId] - The messaging sender ID for the Firebase project.
|
|
15
|
+
* @property {string} [appId] - The app ID for the Firebase project.
|
|
16
|
+
* @property {string} [measurementId] - The measurement ID for the Firebase project.
|
|
17
|
+
*/
|
|
18
|
+
interface IFirebaseConfig {
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
authDomain?: string;
|
|
21
|
+
projectId?: string;
|
|
22
|
+
storageBucket?: string;
|
|
23
|
+
messagingSenderId?: string;
|
|
24
|
+
appId?: string;
|
|
25
|
+
measurementId?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Represents the return type of an asynchronous Firebase operation.
|
|
29
|
+
*
|
|
30
|
+
* @interface IAsyncFirebaseReturnType
|
|
31
|
+
* @template T - The type of the result.
|
|
32
|
+
* @property {unknown | null} error - The error encountered during the asynchronous operation, if any.
|
|
33
|
+
* @property {T | null} result - The result of the asynchronous operation, if successful.
|
|
34
|
+
*/
|
|
35
|
+
interface IAsyncFirebaseReturnType<T> {
|
|
36
|
+
error: unknown | null;
|
|
37
|
+
result: T | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Gets all documents from a collection in Firebase Firestore.
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of the documents.
|
|
44
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
45
|
+
* @param {string} c - The collection name.
|
|
46
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
47
|
+
*/
|
|
48
|
+
declare const getAllDocuments: <T>(firestore: Firestore, c: string) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
49
|
+
/**
|
|
50
|
+
* Gets a document from a collection in Firebase Firestore by its ID.
|
|
51
|
+
*
|
|
52
|
+
* @template T - The type of the document.
|
|
53
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
54
|
+
* @param {string} c - The collection name.
|
|
55
|
+
* @param {string} id - The ID of the document.
|
|
56
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
57
|
+
*/
|
|
58
|
+
declare const getDocumentById: <T>(firestore: Firestore, c: string, id: string) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
59
|
+
/**
|
|
60
|
+
* Adds a document to a collection in Firebase Firestore.
|
|
61
|
+
*
|
|
62
|
+
* @template T - The type of the document.
|
|
63
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
64
|
+
* @param {string} c - The collection name.
|
|
65
|
+
* @param {DocumentData} data - The data to add to the document.
|
|
66
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
67
|
+
*/
|
|
68
|
+
declare const addDocument: <T>(firestore: Firestore, c: string, data: DocumentData) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
69
|
+
/**
|
|
70
|
+
* Sets a document in a collection in Firebase Firestore.
|
|
71
|
+
*
|
|
72
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
73
|
+
* @param {string} c - The collection name.
|
|
74
|
+
* @param {DocumentData} data - The data to set in the document.
|
|
75
|
+
* @returns {Promise<TAsyncBoundary<void>>} A promise that resolves to an object containing either the result or an error.
|
|
76
|
+
*/
|
|
77
|
+
declare const setDocument: (firestore: Firestore, c: string, data: DocumentData) => Promise<TAsyncBoundary<void>>;
|
|
78
|
+
/**
|
|
79
|
+
* Deletes a document from a collection in Firebase Firestore.
|
|
80
|
+
*
|
|
81
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
82
|
+
* @param {string} c - The collection name.
|
|
83
|
+
* @param {string} id - The ID of the document to delete.
|
|
84
|
+
* @returns {Promise<TAsyncBoundary<void>>} A promise that resolves to an object containing either the result or an error.
|
|
85
|
+
*/
|
|
86
|
+
declare const deleteDocument: (firestore: Firestore, c: string, id: string) => Promise<TAsyncBoundary<void>>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Initializes a Firebase app.
|
|
90
|
+
*
|
|
91
|
+
* @param {IFirebaseConfig} config - The configuration for the Firebase app.
|
|
92
|
+
* @returns {Promise<FirebaseApp>} A promise that resolves to the initialized Firebase app.
|
|
93
|
+
*/
|
|
94
|
+
declare const initFirebaseApp: (config: IFirebaseConfig) => Promise<FirebaseApp>;
|
|
95
|
+
/**
|
|
96
|
+
* Gets a Firestore instance.
|
|
97
|
+
*
|
|
98
|
+
* @param {FirebaseApp} app - The Firebase app.
|
|
99
|
+
* @returns {Promise<Firestore>} A promise that resolves to the Firestore instance.
|
|
100
|
+
*/
|
|
101
|
+
declare const getFireStore: (app: FirebaseApp) => Promise<Firestore>;
|
|
102
|
+
/**
|
|
103
|
+
* Gets a Firebase Storage instance.
|
|
104
|
+
*
|
|
105
|
+
* @param {FirebaseApp} app - The Firebase app.
|
|
106
|
+
* @returns {Promise<FirebaseStorage>} A promise that resolves to the Firebase Storage instance.
|
|
107
|
+
*/
|
|
108
|
+
declare const getFireStorage: (app: FirebaseApp) => Promise<FirebaseStorage>;
|
|
109
|
+
|
|
110
|
+
export { type IAsyncFirebaseReturnType, type IFirebaseConfig, addDocument, deleteDocument, getAllDocuments, getDocumentById, getFireStorage, getFireStore, initFirebaseApp, setDocument };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Firestore, DocumentData } from 'firebase/firestore';
|
|
2
|
+
import { TAsyncBoundary } from '@purr-react-core/utils.definitions';
|
|
3
|
+
import { FirebaseApp } from 'firebase/app';
|
|
4
|
+
import { FirebaseStorage } from 'firebase/storage';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents the configuration for a Firebase project.
|
|
8
|
+
*
|
|
9
|
+
* @interface IFirebaseConfig
|
|
10
|
+
* @property {string} [apiKey] - The API key for the Firebase project.
|
|
11
|
+
* @property {string} [authDomain] - The authentication domain for the Firebase project.
|
|
12
|
+
* @property {string} [projectId] - The project ID for the Firebase project.
|
|
13
|
+
* @property {string} [storageBucket] - The storage bucket for the Firebase project.
|
|
14
|
+
* @property {string} [messagingSenderId] - The messaging sender ID for the Firebase project.
|
|
15
|
+
* @property {string} [appId] - The app ID for the Firebase project.
|
|
16
|
+
* @property {string} [measurementId] - The measurement ID for the Firebase project.
|
|
17
|
+
*/
|
|
18
|
+
interface IFirebaseConfig {
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
authDomain?: string;
|
|
21
|
+
projectId?: string;
|
|
22
|
+
storageBucket?: string;
|
|
23
|
+
messagingSenderId?: string;
|
|
24
|
+
appId?: string;
|
|
25
|
+
measurementId?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Represents the return type of an asynchronous Firebase operation.
|
|
29
|
+
*
|
|
30
|
+
* @interface IAsyncFirebaseReturnType
|
|
31
|
+
* @template T - The type of the result.
|
|
32
|
+
* @property {unknown | null} error - The error encountered during the asynchronous operation, if any.
|
|
33
|
+
* @property {T | null} result - The result of the asynchronous operation, if successful.
|
|
34
|
+
*/
|
|
35
|
+
interface IAsyncFirebaseReturnType<T> {
|
|
36
|
+
error: unknown | null;
|
|
37
|
+
result: T | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Gets all documents from a collection in Firebase Firestore.
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of the documents.
|
|
44
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
45
|
+
* @param {string} c - The collection name.
|
|
46
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
47
|
+
*/
|
|
48
|
+
declare const getAllDocuments: <T>(firestore: Firestore, c: string) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
49
|
+
/**
|
|
50
|
+
* Gets a document from a collection in Firebase Firestore by its ID.
|
|
51
|
+
*
|
|
52
|
+
* @template T - The type of the document.
|
|
53
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
54
|
+
* @param {string} c - The collection name.
|
|
55
|
+
* @param {string} id - The ID of the document.
|
|
56
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
57
|
+
*/
|
|
58
|
+
declare const getDocumentById: <T>(firestore: Firestore, c: string, id: string) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
59
|
+
/**
|
|
60
|
+
* Adds a document to a collection in Firebase Firestore.
|
|
61
|
+
*
|
|
62
|
+
* @template T - The type of the document.
|
|
63
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
64
|
+
* @param {string} c - The collection name.
|
|
65
|
+
* @param {DocumentData} data - The data to add to the document.
|
|
66
|
+
* @returns {Promise<IAsyncFirebaseReturnType<T>>} A promise that resolves to an object containing either the result or an error.
|
|
67
|
+
*/
|
|
68
|
+
declare const addDocument: <T>(firestore: Firestore, c: string, data: DocumentData) => Promise<IAsyncFirebaseReturnType<T>>;
|
|
69
|
+
/**
|
|
70
|
+
* Sets a document in a collection in Firebase Firestore.
|
|
71
|
+
*
|
|
72
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
73
|
+
* @param {string} c - The collection name.
|
|
74
|
+
* @param {DocumentData} data - The data to set in the document.
|
|
75
|
+
* @returns {Promise<TAsyncBoundary<void>>} A promise that resolves to an object containing either the result or an error.
|
|
76
|
+
*/
|
|
77
|
+
declare const setDocument: (firestore: Firestore, c: string, data: DocumentData) => Promise<TAsyncBoundary<void>>;
|
|
78
|
+
/**
|
|
79
|
+
* Deletes a document from a collection in Firebase Firestore.
|
|
80
|
+
*
|
|
81
|
+
* @param {Firestore} firestore - The Firestore instance.
|
|
82
|
+
* @param {string} c - The collection name.
|
|
83
|
+
* @param {string} id - The ID of the document to delete.
|
|
84
|
+
* @returns {Promise<TAsyncBoundary<void>>} A promise that resolves to an object containing either the result or an error.
|
|
85
|
+
*/
|
|
86
|
+
declare const deleteDocument: (firestore: Firestore, c: string, id: string) => Promise<TAsyncBoundary<void>>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Initializes a Firebase app.
|
|
90
|
+
*
|
|
91
|
+
* @param {IFirebaseConfig} config - The configuration for the Firebase app.
|
|
92
|
+
* @returns {Promise<FirebaseApp>} A promise that resolves to the initialized Firebase app.
|
|
93
|
+
*/
|
|
94
|
+
declare const initFirebaseApp: (config: IFirebaseConfig) => Promise<FirebaseApp>;
|
|
95
|
+
/**
|
|
96
|
+
* Gets a Firestore instance.
|
|
97
|
+
*
|
|
98
|
+
* @param {FirebaseApp} app - The Firebase app.
|
|
99
|
+
* @returns {Promise<Firestore>} A promise that resolves to the Firestore instance.
|
|
100
|
+
*/
|
|
101
|
+
declare const getFireStore: (app: FirebaseApp) => Promise<Firestore>;
|
|
102
|
+
/**
|
|
103
|
+
* Gets a Firebase Storage instance.
|
|
104
|
+
*
|
|
105
|
+
* @param {FirebaseApp} app - The Firebase app.
|
|
106
|
+
* @returns {Promise<FirebaseStorage>} A promise that resolves to the Firebase Storage instance.
|
|
107
|
+
*/
|
|
108
|
+
declare const getFireStorage: (app: FirebaseApp) => Promise<FirebaseStorage>;
|
|
109
|
+
|
|
110
|
+
export { type IAsyncFirebaseReturnType, type IFirebaseConfig, addDocument, deleteDocument, getAllDocuments, getDocumentById, getFireStorage, getFireStore, initFirebaseApp, setDocument };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {collection,getDocs,doc,getDoc,addDoc,setDoc,deleteDoc}from'firebase/firestore';import {tryDo,cast}from'@purr-react-core/utils.helpers';var R=async(t,o)=>{let e=collection(t,o),{error:n,result:r}=await tryDo(getDocs(e));if(n)return {error:n,result:null};let s=r?.docs?.map?.(u=>({...u.data(),id:u.id}));return {error:null,result:cast(s)}},h=async(t,o,e)=>{let n=doc(t,o,e),{error:r,result:s}=await tryDo(getDoc(n));return r?{error:r,result:null}:{error:null,result:cast(s?.data())}},w=async(t,o,e)=>{let n=collection(t,o),{error:r,result:s}=await tryDo(addDoc(n,e));return r?{error:r,result:null}:{error:null,result:cast(s)}},P=async(t,o,e)=>{let n=doc(t,o,e.id),{error:r,result:s}=await tryDo(setDoc(n,e,{merge:true}));return r?{error:r,result:null}:{error:null,result:s}},I=async(t,o,e)=>{let n=doc(t,o,e),{error:r,result:s}=await tryDo(deleteDoc(n));return r?{error:r,result:null}:{error:null,result:s}};var B=async t=>(await import('firebase/app').then(e=>e.default.initializeApp))(t),Q=async t=>(await import('firebase/firestore').then(e=>e.default.getFirestore))(t),z=async t=>(await import('firebase/storage').then(e=>e.default.getStorage))(t);export{w as addDocument,I as deleteDocument,R as getAllDocuments,h as getDocumentById,z as getFireStorage,Q as getFireStore,B as initFirebaseApp,P as setDocument};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@purr-react-core/services.firebase",
|
|
3
|
+
"version": "0.0.12",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"firebase": "^10.0.0"
|
|
25
|
+
},
|
|
26
|
+
"author": "@DinhThienPhuc",
|
|
27
|
+
"license": "ISC",
|
|
28
|
+
"description": "",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@purr-react-core/utils.definitions": "0.0.14",
|
|
32
|
+
"@purr-react-core/utils.helpers": "0.0.14"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"dev": "vite build --watch",
|
|
36
|
+
"build": "tsc && vite build",
|
|
37
|
+
"build:vite": "tsc && vite build",
|
|
38
|
+
"build:tsup": "tsup",
|
|
39
|
+
"lint": "eslint . --ext ts,tsx --max-warnings 0"
|
|
40
|
+
}
|
|
41
|
+
}
|