@donotdev/firebase 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.
Files changed (51) hide show
  1. package/LICENSE.md +242 -0
  2. package/README.md +386 -0
  3. package/dist/client/firestore.d.ts +147 -0
  4. package/dist/client/firestore.d.ts.map +1 -0
  5. package/dist/client/firestore.js +1 -0
  6. package/dist/client/functions.d.ts +28 -0
  7. package/dist/client/functions.d.ts.map +1 -0
  8. package/dist/client/functions.js +1 -0
  9. package/dist/client/index.d.ts +11 -0
  10. package/dist/client/index.d.ts.map +1 -0
  11. package/dist/client/index.js +1 -0
  12. package/dist/client/sdk.d.ts +968 -0
  13. package/dist/client/sdk.d.ts.map +1 -0
  14. package/dist/client/sdk.js +21 -0
  15. package/dist/index.d.ts +9 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +1 -0
  18. package/dist/server/batch.d.ts +97 -0
  19. package/dist/server/batch.d.ts.map +1 -0
  20. package/dist/server/batch.js +1 -0
  21. package/dist/server/index.d.ts +15 -0
  22. package/dist/server/index.d.ts.map +1 -0
  23. package/dist/server/index.js +1 -0
  24. package/dist/server/init.d.ts +13 -0
  25. package/dist/server/init.d.ts.map +1 -0
  26. package/dist/server/init.js +2 -0
  27. package/dist/server/subscription.d.ts +117 -0
  28. package/dist/server/subscription.d.ts.map +1 -0
  29. package/dist/server/subscription.js +1 -0
  30. package/dist/server/uniqueness.d.ts +69 -0
  31. package/dist/server/uniqueness.d.ts.map +1 -0
  32. package/dist/server/uniqueness.js +1 -0
  33. package/dist/server/utils.d.ts +28 -0
  34. package/dist/server/utils.d.ts.map +1 -0
  35. package/dist/server/utils.js +1 -0
  36. package/dist/server/validation.d.ts +43 -0
  37. package/dist/server/validation.d.ts.map +1 -0
  38. package/dist/server/validation.js +1 -0
  39. package/dist/shared/index.d.ts +11 -0
  40. package/dist/shared/index.d.ts.map +1 -0
  41. package/dist/shared/index.js +1 -0
  42. package/dist/shared/transform.d.ts +96 -0
  43. package/dist/shared/transform.d.ts.map +1 -0
  44. package/dist/shared/transform.js +1 -0
  45. package/dist/shared/types.d.ts +37 -0
  46. package/dist/shared/types.d.ts.map +1 -0
  47. package/dist/shared/types.js +0 -0
  48. package/dist/shared/utils.d.ts +71 -0
  49. package/dist/shared/utils.d.ts.map +1 -0
  50. package/dist/shared/utils.js +1 -0
  51. package/package.json +50 -0
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @fileoverview Firebase data transformation utilities
3
+ * @description Functions for converting data between Firebase and application formats. Handles timestamp conversion, data preparation, and transformation for Firestore operations.
4
+ * @version 0.0.1
5
+ * @since 0.0.1
6
+ * @author AMBROISE PARK Consulting
7
+ */
8
+ import type { DateValue, FirestoreTimestamp } from '@donotdev/core';
9
+ /**
10
+ * Create a client-safe version of the Timestamp creation
11
+ * This is a browser-compatible replacement for the Timestamp.fromDate() method
12
+ * @param date - The Date object to convert
13
+ * @returns A representation of the timestamp
14
+ *
15
+ * @version 0.0.1
16
+ * @since 0.0.1
17
+ * @author AMBROISE PARK Consulting
18
+ */
19
+ export declare function createTimestamp(date: Date): FirestoreTimestamp;
20
+ /**
21
+ * Converts a Date or ISO string to a Firestore Timestamp.
22
+ * @param date - The date to convert (string or Date)
23
+ * @returns The Firestore Timestamp
24
+ * @throws Error if the date is invalid
25
+ *
26
+ * @version 0.0.1
27
+ * @since 0.0.1
28
+ * @author AMBROISE PARK Consulting
29
+ */
30
+ export declare function toTimestamp(date: string | Date): FirestoreTimestamp;
31
+ /**
32
+ * Converts a DateValue to an ISO string.
33
+ * @param date - The date value to convert (Date, Timestamp, or ISO string)
34
+ * @returns The ISO string representation
35
+ * @throws Error if the date is invalid
36
+ *
37
+ * @version 0.0.1
38
+ * @since 0.0.1
39
+ * @author AMBROISE PARK Consulting
40
+ */
41
+ export declare function firestoreToISOString(date: DateValue): string;
42
+ /**
43
+ * Type guard to check if an object is a Firestore Timestamp
44
+ * @param value - The value to check
45
+ * @returns True if the value is a Firestore Timestamp
46
+ *
47
+ * @version 0.0.1
48
+ * @since 0.0.1
49
+ * @author AMBROISE PARK Consulting
50
+ */
51
+ export declare function isTimestamp(value: any): value is FirestoreTimestamp;
52
+ /**
53
+ * Recursively converts ISO date strings to Date objects for schema validation.
54
+ * Schema validation expects Date objects for timestamp fields, not ISO strings.
55
+ * @param data - The data to convert
56
+ * @returns The data with ISO strings converted to Date objects
57
+ *
58
+ * @version 0.0.1
59
+ * @since 0.0.1
60
+ * @author AMBROISE PARK Consulting
61
+ */
62
+ export declare function convertISOStringsToDates<T = any>(data: T): T;
63
+ /**
64
+ * Recursively transforms Firestore data by converting Timestamps to ISO strings.
65
+ * @param data - The data to transform
66
+ * @param includeDocumentIds - Whether to include document IDs from doc.id
67
+ * @returns The transformed data
68
+ *
69
+ * @version 0.0.1
70
+ * @since 0.0.1
71
+ * @author AMBROISE PARK Consulting
72
+ */
73
+ export declare function transformFirestoreData<T = any>(data: T, includeDocumentIds?: boolean): T;
74
+ /**
75
+ * Recursively prepares data for Firestore by converting ISO strings or Date objects to Timestamps.
76
+ * @param data - The data to prepare
77
+ * @param removeFields - Optional array of field names to remove
78
+ * @returns The prepared data
79
+ *
80
+ * @version 0.0.1
81
+ * @since 0.0.1
82
+ * @author AMBROISE PARK Consulting
83
+ */
84
+ export declare function prepareForFirestore<T = any>(data: T, removeFields?: string[]): T;
85
+ /**
86
+ * Creates a data diff between original and updated data for partial updates
87
+ * @param original - Original data
88
+ * @param updated - Updated data
89
+ * @returns Object containing only the changed fields and their new values
90
+ *
91
+ * @version 0.0.1
92
+ * @since 0.0.1
93
+ * @author AMBROISE PARK Consulting
94
+ */
95
+ export declare function createFirestorePartialUpdate<T extends Record<string, any>>(original: T, updated: Partial<T>): Partial<T>;
96
+ //# sourceMappingURL=transform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/shared/transform.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAKpE;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,kBAAkB,CAY9D;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,kBAAkB,CAyBnE;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAoC5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,kBAAkB,CASnE;AAmBD;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,GAAG,GAAG,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,CAoB5D;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,GAAG,GAAG,EAC5C,IAAI,EAAE,CAAC,EACP,kBAAkB,GAAE,OAAe,GAClC,CAAC,CA0CH;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,GAAG,GAAG,EACzC,IAAI,EAAE,CAAC,EACP,YAAY,GAAE,MAAM,EAAO,GAC1B,CAAC,CAiDH;AAED;;;;;;;;;GASG;AACH,wBAAgB,4BAA4B,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxE,QAAQ,EAAE,CAAC,EACX,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAClB,OAAO,CAAC,CAAC,CAAC,CAsBZ"}
@@ -0,0 +1 @@
1
+ import{handleError as o}from"@donotdev/core";function s(e){return{seconds:Math.floor(e.getTime()/1e3),nanoseconds:e.getTime()%1e3*1e6,toDate:()=>new Date(e),toMillis:()=>e.getTime(),isEqual:t=>t.seconds===Math.floor(e.getTime()/1e3)&&t.nanoseconds===e.getTime()%1e3*1e6,valueOf:()=>`Timestamp(seconds=${Math.floor(e.getTime()/1e3)}, nanoseconds=${e.getTime()%1e3*1e6})`}}function c(e){try{if(typeof e=="string"){const t=new Date(e);if(isNaN(t.getTime()))throw o("Invalid date string format",{context:{value:e}});return s(t)}if(e instanceof Date){if(isNaN(e.getTime()))throw o("Invalid Date object",{context:{value:e}});return s(e)}throw o("Invalid date value type",{context:{valueType:typeof e}})}catch(t){throw o(t,"Failed to convert to Timestamp")}}function l(e){try{if(e instanceof Date){if(isNaN(e.getTime()))throw o("Invalid Date object",{context:{value:e}});return e.toISOString()}if(typeof e=="object"&&e!==null&&"toDate"in e&&typeof e.toDate=="function")return e.toDate().toISOString();if(typeof e=="string"){const t=new Date(e);if(isNaN(t.getTime()))throw o("Invalid date string format",{context:{value:e}});return t.toISOString()}throw o("Invalid date value type",{context:{valueType:typeof e}})}catch(t){throw o(t,"Failed to convert to ISO string")}}function y(e){return typeof e=="object"&&e!==null&&"toDate"in e&&typeof e.toDate=="function"&&"seconds"in e&&"nanoseconds"in e}function m(e){if(typeof e!="string"||!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/.test(e))return!1;const r=new Date(e);return!isNaN(r.getTime())}function u(e){if(!e)return e;if(Array.isArray(e))return e.map(t=>u(t));if(typeof e=="string"&&m(e))return new Date(e);if(typeof e=="object"&&e!==null){const t={};for(const[r,n]of Object.entries(e))t[r]=u(n);return t}return e}function f(e,t=!1){if(!e)return e;if(t&&typeof e=="object"&&e!==null&&"id"in e&&"ref"in e&&"data"in e&&typeof e.data=="function"){const r=e,n=r.data();return{id:r.id,...f(n)}}if(Array.isArray(e))return e.map(r=>f(r));if(y(e))return l(e);if(typeof e=="object"&&e!==null){const r={};for(const[n,i]of Object.entries(e))r[n]=f(i);return r}return e}function p(e,t=[]){if(!e)return e;if(Array.isArray(e))return e.map(r=>p(r,t));if(typeof e=="string"&&/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/.test(e))try{return c(e)}catch{return e}if(e instanceof Date)try{return c(e)}catch{return e}if(typeof e=="object"&&e!==null){const r={};for(const[n,i]of Object.entries(e))t.includes(n)||(r[n]=p(i,t));return r}return e}function D(e,t){const r={};for(const[n,i]of Object.entries(t)){if(!(n in e)){r[n]=i;continue}JSON.stringify(e[n])!==JSON.stringify(i)&&(r[n]=i)}return{...r,updatedAt:new Date().toISOString()}}export{u as convertISOStringsToDates,D as createFirestorePartialUpdate,s as createTimestamp,l as firestoreToISOString,y as isTimestamp,p as prepareForFirestore,c as toTimestamp,f as transformFirestoreData};
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @fileoverview Firebase shared types
3
+ * @description Common type definitions for Firebase operations. Defines interfaces and types used across both client and server Firebase implementations for consistency and type safety.
4
+ * @version 0.0.1
5
+ * @since 0.0.1
6
+ * @author AMBROISE PARK Consulting
7
+ */
8
+ /**
9
+ * Firebase configuration interface
10
+ *
11
+ * @version 0.0.1
12
+ * @since 0.0.1
13
+ * @author AMBROISE PARK Consulting
14
+ */
15
+ export interface FirebaseConfig {
16
+ apiKey: string;
17
+ authDomain: string;
18
+ projectId: string;
19
+ storageBucket?: string;
20
+ messagingSenderId?: string;
21
+ appId?: string;
22
+ }
23
+ /**
24
+ * Firebase services interface
25
+ *
26
+ * @version 0.0.1
27
+ * @since 0.0.1
28
+ * @author AMBROISE PARK Consulting
29
+ */
30
+ export interface FirebaseServices {
31
+ app: any | null;
32
+ auth: any | null;
33
+ firestore: any | null;
34
+ functions: any | null;
35
+ isDemoMode: boolean;
36
+ }
37
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IAChB,IAAI,EAAE,GAAG,GAAG,IAAI,CAAC;IACjB,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC;IACtB,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;CACrB"}
File without changes
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @fileoverview Firebase utility functions
3
+ * @description Core utility functions for Firebase integration
4
+ *
5
+ * @version 0.0.1
6
+ * @since 0.0.1
7
+ * @author AMBROISE PARK Consulting
8
+ */
9
+ import type { FirebaseCallOptions } from '@donotdev/core';
10
+ /**
11
+ * Firebase Admin instance (not available in browser)
12
+ *
13
+ * @version 0.0.1
14
+ * @since 0.0.1
15
+ * @author AMBROISE PARK Consulting
16
+ */
17
+ export declare const admin: (() => never) | null;
18
+ /**
19
+ * Get Firebase Admin Firestore instance (not available in browser)
20
+ *
21
+ * @version 0.0.1
22
+ * @since 0.0.1
23
+ * @author AMBROISE PARK Consulting
24
+ */
25
+ export declare const getAdminFirestore: () => never;
26
+ /**
27
+ * Generate Firestore document ID (not available in browser)
28
+ *
29
+ * @version 0.0.1
30
+ * @since 0.0.1
31
+ * @author AMBROISE PARK Consulting
32
+ */
33
+ export declare const generateId: () => never;
34
+ /**
35
+ * Creates an abort controller with timeout
36
+ * @param options Timeout and related options
37
+ * @returns AbortController with appropriate timeout
38
+ *
39
+ * @version 0.0.1
40
+ * @since 0.0.1
41
+ * @author AMBROISE PARK Consulting
42
+ */
43
+ export declare function createAbortController(options?: FirebaseCallOptions): AbortController;
44
+ /**
45
+ * Handles Firebase errors consistently using the central error handler
46
+ * @param error The original Firebase error
47
+ * @param context Optional context about the operation
48
+ * @returns An error handled through the central system
49
+ *
50
+ * @version 0.0.1
51
+ * @since 0.0.1
52
+ * @author AMBROISE PARK Consulting
53
+ */
54
+ export declare function handleFirebaseError(error: any, context?: string): Error;
55
+ /**
56
+ * Executes a Firebase operation with automatic retry for retryable errors
57
+ * @param operation Name of the operation for logging
58
+ * @param fn Function to execute
59
+ * @param options Options for retry behavior
60
+ * @returns Result of the operation
61
+ *
62
+ * @version 0.0.1
63
+ * @since 0.0.1
64
+ * @author AMBROISE PARK Consulting
65
+ */
66
+ export declare function executeFirebaseOperation<T>(operation: string, fn: () => Promise<T>, options?: {
67
+ retry?: boolean;
68
+ maxRetries?: number;
69
+ retryDelay?: number;
70
+ }): Promise<T>;
71
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/shared/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAsB1D;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,sBAAmD,CAAC;AAEtE;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,aAEgB,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,aAEgB,CAAC;AAExC;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,CAAC,EAAE,mBAAmB,GAC5B,eAAe,CAoCjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAUvE;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,wBAAwB,CAAC,CAAC,EAC9C,SAAS,EAAE,MAAM,EACjB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE;IACP,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CAChB,GACL,OAAO,CAAC,CAAC,CAAC,CAoCZ"}
@@ -0,0 +1 @@
1
+ import{handleError as i}from"@donotdev/core";const s=typeof window<"u",a=e=>()=>{throw i(new Error(`Firebase Admin function '${e}' is not available in browser environments`),{userMessage:"This operation requires server-side Firebase access and cannot be used in the browser",severity:"error"})},w=s?null:a("admin"),y=a("getAdminFirestore"),x=a("generateId");function h(e){const t=new AbortController;let r;return e?.timeout&&(r=setTimeout(()=>{const n=new Error("Request timeout exceeded");t.abort(i(n,{userMessage:"The operation timed out",context:{timeoutMs:e.timeout,abortKey:e.abortKey},severity:"warning"}))},e.timeout)),e?.externalSignal&&e.externalSignal.addEventListener("abort",()=>{t.abort(e.externalSignal?.reason||"External abort")}),t.signal.addEventListener("abort",()=>{r!==void 0&&clearTimeout(r)}),t}function v(e,t){return i(e,{userMessage:t?`Error during ${t}: ${e.message||"An error occurred"}`:void 0,context:{operation:t}})}async function E(e,t,r={}){const{retry:n=!1,maxRetries:d=3,retryDelay:l=300}=r;let o=0;for(;;)try{return await t()}catch(u){if(o++,b(u)&&n&&o<=d){const c=l*Math.pow(2,o-1);await new Promise(m=>setTimeout(m,c));continue}throw i(u,{userMessage:`Firebase operation "${e}" failed`,context:{operation:e,attempt:o,maxRetries:d,options:r}})}}function b(e){const t=["permission-denied","invalid-argument","already-exists","not-found","unauthenticated"];return e&&e.name==="DoNotDevError"&&t.includes(e.code)?!1:e&&e.code?["unavailable","deadline","cancel","network","timeout","internal","resource","exhausted"].some(n=>e.code.toLowerCase().includes(n)):!0}export{w as admin,h as createAbortController,E as executeFirebaseOperation,x as generateId,y as getAdminFirestore,v as handleFirebaseError};
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@donotdev/firebase",
3
+ "version": "0.0.1",
4
+ "private": false,
5
+ "type": "module",
6
+ "license": "SEE LICENSE IN LICENSE.md",
7
+ "description": "Firebase utilities for DoNotDev",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./server": {
17
+ "types": "./dist/server/index.d.ts",
18
+ "import": "./dist/server/index.js",
19
+ "default": "./dist/server/index.js"
20
+ }
21
+ },
22
+ "scripts": {
23
+ "dev": "tsc --noEmit --watch --listFiles false --listEmittedFiles false",
24
+ "clean": "rimraf dist tsconfig.tsbuildinfo",
25
+ "type-check": "tsc --noEmit"
26
+ },
27
+ "dependencies": {
28
+ "valibot": "^1.2.0"
29
+ },
30
+ "peerDependencies": {
31
+ "@donotdev/core": "0.0.1",
32
+ "firebase": "^12.5.0",
33
+ "firebase-admin": "^13.6.0"
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "package.json",
38
+ "README.md",
39
+ "LICENSE.md"
40
+ ],
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/donotdev/firebase.git"
44
+ },
45
+ "publishConfig": {
46
+ "registry": "https://registry.npmjs.org",
47
+ "access": "public"
48
+ },
49
+ "peerDependenciesMeta": {}
50
+ }