@dereekb/firebase-server 13.2.2 → 13.3.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 (166) hide show
  1. package/index.cjs.js +7506 -1884
  2. package/index.esm.js +7505 -1890
  3. package/mailgun/index.cjs.js +333 -31
  4. package/mailgun/index.esm.js +333 -31
  5. package/mailgun/package.json +8 -8
  6. package/mailgun/src/lib/auth.mailgun.d.ts +22 -3
  7. package/model/index.cjs.js +11334 -3830
  8. package/model/index.esm.js +11338 -3834
  9. package/model/package.json +10 -10
  10. package/model/src/lib/mailgun/notification.send.service.mailgun.d.ts +27 -1
  11. package/model/src/lib/notification/notification.action.init.service.d.ts +93 -1
  12. package/model/src/lib/notification/notification.action.service.d.ts +151 -2
  13. package/model/src/lib/notification/notification.config.d.ts +19 -7
  14. package/model/src/lib/notification/notification.config.service.d.ts +44 -6
  15. package/model/src/lib/notification/notification.create.run.d.ts +33 -5
  16. package/model/src/lib/notification/notification.error.d.ts +63 -0
  17. package/model/src/lib/notification/notification.module.d.ts +11 -0
  18. package/model/src/lib/notification/notification.send.d.ts +8 -1
  19. package/model/src/lib/notification/notification.send.service.d.ts +7 -1
  20. package/model/src/lib/notification/notification.send.service.notificationsummary.d.ts +32 -2
  21. package/model/src/lib/notification/notification.send.service.text.d.ts +11 -3
  22. package/model/src/lib/notification/notification.task.service.d.ts +13 -1
  23. package/model/src/lib/notification/notification.task.service.handler.d.ts +27 -3
  24. package/model/src/lib/notification/notification.task.service.util.d.ts +16 -4
  25. package/model/src/lib/notification/notification.task.subtask.handler.d.ts +31 -1
  26. package/model/src/lib/notification/notification.util.d.ts +70 -0
  27. package/model/src/lib/storagefile/storagefile.action.init.service.d.ts +62 -1
  28. package/model/src/lib/storagefile/storagefile.action.server.d.ts +124 -2
  29. package/model/src/lib/storagefile/storagefile.error.d.ts +44 -0
  30. package/model/src/lib/storagefile/storagefile.module.d.ts +11 -0
  31. package/model/src/lib/storagefile/storagefile.task.service.handler.d.ts +4 -1
  32. package/model/src/lib/storagefile/storagefile.upload.service.d.ts +13 -1
  33. package/model/src/lib/storagefile/storagefile.upload.service.initializer.d.ts +43 -1
  34. package/model/src/lib/storagefile/storagefile.util.d.ts +8 -0
  35. package/oidc/index.cjs.default.js +1 -0
  36. package/oidc/index.cjs.js +5607 -0
  37. package/oidc/index.cjs.mjs +2 -0
  38. package/oidc/index.d.ts +1 -0
  39. package/oidc/index.esm.js +5560 -0
  40. package/oidc/package.json +26 -0
  41. package/oidc/src/index.d.ts +1 -0
  42. package/oidc/src/lib/controller/index.d.ts +3 -0
  43. package/oidc/src/lib/controller/oidc.interaction.controller.d.ts +54 -0
  44. package/oidc/src/lib/controller/oidc.provider.controller.d.ts +18 -0
  45. package/oidc/src/lib/controller/oidc.wellknown.controller.d.ts +36 -0
  46. package/oidc/src/lib/index.d.ts +6 -0
  47. package/oidc/src/lib/middleware/index.d.ts +3 -0
  48. package/oidc/src/lib/middleware/oauth-auth.decorator.d.ts +14 -0
  49. package/oidc/src/lib/middleware/oauth-auth.middleware.d.ts +21 -0
  50. package/oidc/src/lib/middleware/oauth-auth.module.d.ts +50 -0
  51. package/oidc/src/lib/model/index.d.ts +3 -0
  52. package/oidc/src/lib/model/jwks/index.d.ts +3 -0
  53. package/oidc/src/lib/model/jwks/jwks.d.ts +107 -0
  54. package/oidc/src/lib/model/jwks/jwks.id.d.ts +9 -0
  55. package/oidc/src/lib/model/jwks/jwks.query.d.ts +18 -0
  56. package/oidc/src/lib/model/model.d.ts +12 -0
  57. package/oidc/src/lib/model/oidc/index.d.ts +2 -0
  58. package/oidc/src/lib/model/oidc/oidcmodel.action.server.d.ts +62 -0
  59. package/oidc/src/lib/model/oidc/oidcmodel.module.d.ts +23 -0
  60. package/oidc/src/lib/oidc.config.d.ts +175 -0
  61. package/oidc/src/lib/oidc.module.d.ts +100 -0
  62. package/oidc/src/lib/service/index.d.ts +10 -0
  63. package/oidc/src/lib/service/oidc.account.d.ts +36 -0
  64. package/oidc/src/lib/service/oidc.account.service.d.ts +104 -0
  65. package/oidc/src/lib/service/oidc.adapter.service.d.ts +20 -0
  66. package/oidc/src/lib/service/oidc.auth.d.ts +26 -0
  67. package/oidc/src/lib/service/oidc.client.service.d.ts +57 -0
  68. package/oidc/src/lib/service/oidc.config.service.d.ts +100 -0
  69. package/oidc/src/lib/service/oidc.encryption.service.d.ts +53 -0
  70. package/oidc/src/lib/service/oidc.interaction.service.d.ts +44 -0
  71. package/oidc/src/lib/service/oidc.jwks.service.d.ts +105 -0
  72. package/oidc/src/lib/service/oidc.service.d.ts +55 -0
  73. package/package.json +21 -14
  74. package/src/lib/auth/auth.context.d.ts +22 -2
  75. package/src/lib/auth/auth.service.d.ts +5 -0
  76. package/src/lib/auth/auth.service.error.d.ts +2 -1
  77. package/src/lib/auth/auth.util.d.ts +15 -3
  78. package/src/lib/env/env.config.d.ts +42 -0
  79. package/src/lib/env/env.service.d.ts +17 -0
  80. package/src/lib/env/index.d.ts +1 -0
  81. package/src/lib/firestore/array.d.ts +11 -3
  82. package/src/lib/firestore/driver.accessor.batch.d.ts +27 -4
  83. package/src/lib/firestore/driver.accessor.d.ts +43 -0
  84. package/src/lib/firestore/driver.accessor.default.d.ts +20 -0
  85. package/src/lib/firestore/driver.accessor.transaction.d.ts +28 -4
  86. package/src/lib/firestore/driver.d.ts +14 -0
  87. package/src/lib/firestore/driver.query.d.ts +25 -0
  88. package/src/lib/firestore/increment.d.ts +13 -3
  89. package/src/lib/firestore/snapshot/index.d.ts +1 -1
  90. package/src/lib/firestore/snapshot/{snapshot.field.d.ts → snapshot.field.encrypt.d.ts} +13 -27
  91. package/src/lib/function/assert.d.ts +32 -16
  92. package/src/lib/function/context.d.ts +26 -0
  93. package/src/lib/function/error.auth.d.ts +15 -0
  94. package/src/lib/function/error.d.ts +68 -4
  95. package/src/lib/nest/app.d.ts +53 -9
  96. package/src/lib/nest/app.module.d.ts +90 -0
  97. package/src/lib/nest/auth/auth.module.d.ts +30 -4
  98. package/src/lib/nest/auth/auth.util.d.ts +38 -0
  99. package/src/lib/nest/controller/auth.context.server.d.ts +27 -0
  100. package/src/lib/nest/controller/index.d.ts +1 -0
  101. package/src/lib/nest/development/development.app.function.d.ts +45 -0
  102. package/src/lib/nest/development/development.assert.function.d.ts +19 -1
  103. package/src/lib/nest/development/development.function.d.ts +42 -3
  104. package/src/lib/nest/development/development.schedule.function.d.ts +29 -0
  105. package/src/lib/nest/development/development.schedule.function.error.d.ts +21 -0
  106. package/src/lib/nest/env/env.service.d.ts +9 -0
  107. package/src/lib/nest/env/env.util.d.ts +16 -0
  108. package/src/lib/nest/firebase/firebase.module.d.ts +13 -1
  109. package/src/lib/nest/firestore/firestore.module.d.ts +24 -8
  110. package/src/lib/nest/function/call.d.ts +75 -6
  111. package/src/lib/nest/function/context.d.ts +85 -2
  112. package/src/lib/nest/function/nest.d.ts +46 -0
  113. package/src/lib/nest/function/schedule.d.ts +49 -0
  114. package/src/lib/nest/function/v2/blocking.d.ts +92 -8
  115. package/src/lib/nest/function/v2/call.d.ts +38 -8
  116. package/src/lib/nest/function/v2/event.d.ts +67 -4
  117. package/src/lib/nest/function/v2/schedule.d.ts +36 -5
  118. package/src/lib/nest/index.d.ts +3 -0
  119. package/src/lib/nest/middleware/appcheck.d.ts +5 -0
  120. package/src/lib/nest/middleware/appcheck.middleware.d.ts +44 -14
  121. package/src/lib/nest/middleware/appcheck.module.d.ts +10 -1
  122. package/src/lib/nest/middleware/globalprefix.d.ts +31 -3
  123. package/src/lib/nest/middleware/index.d.ts +2 -1
  124. package/src/lib/nest/middleware/rawbody.middleware.d.ts +6 -0
  125. package/src/lib/nest/model/call.model.function.d.ts +69 -3
  126. package/src/lib/nest/model/create.model.function.d.ts +66 -0
  127. package/src/lib/nest/model/crud.assert.function.d.ts +27 -1
  128. package/src/lib/nest/model/delete.model.function.d.ts +55 -0
  129. package/src/lib/nest/model/permission.error.d.ts +15 -0
  130. package/src/lib/nest/model/read.model.function.d.ts +57 -0
  131. package/src/lib/nest/model/specifier.function.d.ts +58 -1
  132. package/src/lib/nest/model/update.model.function.d.ts +58 -0
  133. package/src/lib/nest/nest.provider.d.ts +24 -11
  134. package/src/lib/nest/nest.provider.server.d.ts +12 -0
  135. package/src/lib/nest/storage/storage.module.d.ts +30 -4
  136. package/src/lib/storage/driver.accessor.d.ts +33 -0
  137. package/src/lib/storage/driver.d.ts +14 -0
  138. package/src/lib/storage/storage.d.ts +11 -4
  139. package/src/lib/storage/storage.service.d.ts +10 -1
  140. package/src/lib/type.d.ts +7 -2
  141. package/test/index.cjs.js +4072 -757
  142. package/test/index.esm.js +4073 -761
  143. package/test/package.json +10 -10
  144. package/test/src/lib/firebase/firebase.admin.auth.d.ts +82 -1
  145. package/test/src/lib/firebase/firebase.admin.collection.d.ts +55 -3
  146. package/test/src/lib/firebase/firebase.admin.d.ts +64 -0
  147. package/test/src/lib/firebase/firebase.admin.function.d.ts +84 -0
  148. package/test/src/lib/firebase/firebase.admin.nest.d.ts +125 -17
  149. package/test/src/lib/firebase/firebase.admin.nest.function.callable.context.d.ts +44 -0
  150. package/test/src/lib/firebase/firebase.admin.nest.function.cloud.context.d.ts +41 -0
  151. package/test/src/lib/firebase/firebase.admin.nest.function.d.ts +98 -0
  152. package/test/src/lib/firebase/firebase.admin.test.server.d.ts +26 -0
  153. package/test/src/lib/firebase/firebase.d.ts +57 -2
  154. package/test/src/lib/firebase/firebase.function.d.ts +138 -0
  155. package/test/src/lib/firestore/firestore.admin.d.ts +6 -0
  156. package/test/src/lib/firestore/firestore.d.ts +30 -0
  157. package/test/src/lib/storage/storage.admin.d.ts +9 -3
  158. package/test/src/lib/storage/storage.d.ts +30 -0
  159. package/zoho/index.cjs.js +406 -73
  160. package/zoho/index.esm.js +406 -73
  161. package/zoho/package.json +8 -8
  162. package/zoho/src/lib/zoho.accounts.firebase.d.ts +13 -2
  163. package/zoho/src/lib/zoho.accounts.firebase.system.d.ts +29 -6
  164. package/index.cjs.js.map +0 -1
  165. package/index.esm.js.map +0 -1
  166. /package/src/lib/nest/middleware/{webhook.d.ts → webhook.module.d.ts} +0 -0
@@ -2,7 +2,11 @@ import { type DocumentReference, type DocumentSnapshot, type Transaction as Goog
2
2
  import { type Observable } from 'rxjs';
3
3
  import { type WithFieldValue, type UpdateData, type FirestoreDocumentDataAccessor, type FirestoreDocumentDataAccessorFactory, type FirestoreDocumentContext, FirestoreDocumentContextType, type FirestoreDocumentUpdateParams, type FirestoreDataConverter, type DocumentData, type FirestoreAccessorIncrementUpdate, type FirestoreAccessorArrayUpdate } from '@dereekb/firebase';
4
4
  /**
5
- * FirestoreDocumentDataAccessor implementation for a transaction.
5
+ * Google Cloud Firestore implementation of {@link FirestoreDocumentDataAccessor} that executes
6
+ * all operations within a Firestore {@link Transaction}.
7
+ *
8
+ * Reads performed through this accessor are included in the transaction's read set, ensuring
9
+ * consistency. Writes are committed atomically when the transaction completes.
6
10
  */
7
11
  export declare class TransactionFirestoreDocumentDataAccessor<T> implements FirestoreDocumentDataAccessor<T> {
8
12
  private readonly _transaction;
@@ -22,12 +26,29 @@ export declare class TransactionFirestoreDocumentDataAccessor<T> implements Fire
22
26
  update(data: UpdateData<object>, params?: FirestoreDocumentUpdateParams): Promise<void>;
23
27
  }
24
28
  /**
25
- * Creates a new FirestoreDocumentDataAccessorFactory for a Transaction.
29
+ * Creates a {@link FirestoreDocumentDataAccessorFactory} that produces transaction-backed accessors.
30
+ *
31
+ * All accessors share the same transaction, so reads and writes participate in the same
32
+ * atomic operation.
26
33
  *
27
- * @param transaction
28
- * @returns
34
+ * @param transaction - The Google Cloud Transaction to execute operations within.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * await firestore.runTransaction(async (transaction) => {
39
+ * const factory = transactionAccessorFactory<User>(transaction);
40
+ * const accessor = factory.accessorFor(userDocRef);
41
+ * const snapshot = await accessor.get();
42
+ * await accessor.update({ name: 'Updated' });
43
+ * });
44
+ * ```
29
45
  */
30
46
  export declare function transactionAccessorFactory<T>(transaction: GoogleCloudTransaction): FirestoreDocumentDataAccessorFactory<T>;
47
+ /**
48
+ * A {@link FirestoreDocumentContext} backed by a Google Cloud {@link Transaction}.
49
+ *
50
+ * All document accessors created from this context operate within the same transaction.
51
+ */
31
52
  export declare class TransactionFirestoreDocumentContext<T> implements FirestoreDocumentContext<T> {
32
53
  private readonly _transaction;
33
54
  readonly contextType = FirestoreDocumentContextType.TRANSACTION;
@@ -35,4 +56,7 @@ export declare class TransactionFirestoreDocumentContext<T> implements Firestore
35
56
  constructor(transaction: GoogleCloudTransaction);
36
57
  get transaction(): GoogleCloudTransaction;
37
58
  }
59
+ /**
60
+ * Creates a {@link TransactionFirestoreDocumentContext} wrapping the given transaction.
61
+ */
38
62
  export declare function transactionDocumentContext<T>(transaction: GoogleCloudTransaction): TransactionFirestoreDocumentContext<T>;
@@ -1,3 +1,17 @@
1
1
  import { type FirestoreDrivers } from '@dereekb/firebase';
2
+ /**
3
+ * Alias for {@link FirestoreDrivers} specific to the Google Cloud Firestore (server) implementation.
4
+ */
2
5
  export type GoogleCloudFirestoreDrivers = FirestoreDrivers;
6
+ /**
7
+ * Creates a complete set of {@link FirestoreDrivers} for Google Cloud Firestore (Admin SDK).
8
+ *
9
+ * Bundles the server-side accessor driver and query driver, identified as `@google-cloud/firestore`.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const drivers = googleCloudFirestoreDrivers();
14
+ * const context = firestoreContextFactory(drivers);
15
+ * ```
16
+ */
3
17
  export declare function googleCloudFirestoreDrivers(): GoogleCloudFirestoreDrivers;
@@ -1,6 +1,31 @@
1
1
  import { type Query as GoogleCloudQuery } from '@google-cloud/firestore';
2
2
  import { type FullFirestoreQueryConstraintHandlersMapping, type FirestoreQueryConstraintFunctionsDriver, type FirestoreQueryDriver } from '@dereekb/firebase';
3
+ /**
4
+ * Server-side query builder type, aliasing the Google Cloud Firestore {@link Query}.
5
+ */
3
6
  export type FirestoreServerQueryBuilder<T = any> = GoogleCloudQuery<T>;
7
+ /**
8
+ * Maps each abstract query constraint type to its Google Cloud Firestore implementation.
9
+ *
10
+ * Used by {@link firestoreClientQueryConstraintFunctionsDriver} to build the server-side query driver.
11
+ */
4
12
  export declare const FIRESTORE_CLIENT_QUERY_CONSTRAINT_HANDLER_MAPPING: FullFirestoreQueryConstraintHandlersMapping<FirestoreServerQueryBuilder>;
13
+ /**
14
+ * Creates a {@link FirestoreQueryConstraintFunctionsDriver} for the Google Cloud Firestore server SDK.
15
+ *
16
+ * Translates abstract query constraints into Google Cloud Firestore query builder calls.
17
+ */
5
18
  export declare function firestoreClientQueryConstraintFunctionsDriver(): FirestoreQueryConstraintFunctionsDriver;
19
+ /**
20
+ * Creates a complete {@link FirestoreQueryDriver} for Google Cloud Firestore (Admin SDK).
21
+ *
22
+ * Supports query execution (getDocs), document counting (countDocs), and real-time
23
+ * streaming (streamDocs) via `onSnapshot`. Transaction-aware reads are supported
24
+ * through the optional transaction parameter in `getDocs`.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const queryDriver = googleCloudFirestoreQueryDriver();
29
+ * ```
30
+ */
6
31
  export declare function googleCloudFirestoreQueryDriver(): FirestoreQueryDriver;
@@ -1,8 +1,18 @@
1
1
  import { type UpdateData, type FirestoreAccessorIncrementUpdate } from '@dereekb/firebase';
2
2
  /**
3
- * Creates UpdateData corresponding to the input increment update.
3
+ * Converts a {@link FirestoreAccessorIncrementUpdate} into Firestore {@link UpdateData} using
4
+ * Google Cloud Firestore's {@link FieldValue.increment}.
4
5
  *
5
- * @param input
6
- * @returns
6
+ * Each field in the input maps to an atomic increment operation. Null/undefined values default to 0.
7
+ *
8
+ * @param input - The increment specification mapping field names to numeric deltas.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const updateData = firestoreServerIncrementUpdateToUpdateData<Stats>({
13
+ * viewCount: 1,
14
+ * likeCount: -1
15
+ * });
16
+ * ```
7
17
  */
8
18
  export declare function firestoreServerIncrementUpdateToUpdateData<T extends object>(input: FirestoreAccessorIncrementUpdate<T>): UpdateData<T>;
@@ -1 +1 @@
1
- export * from './snapshot.field';
1
+ export * from './snapshot.field.encrypt';
@@ -1,44 +1,30 @@
1
- import { type Maybe, type Getter, type GetterOrValue } from '@dereekb/util';
1
+ import { type GetterOrValue, type Maybe } from '@dereekb/util';
2
2
  import { type FirestoreModelFieldMapFunctionsConfig } from '@dereekb/firebase';
3
3
  /**
4
- * The source for the encryption secret.
4
+ * Configuration for a required encrypted Firestore field.
5
5
  *
6
- * - If a string, it is used directly as the hex-encoded key (64 hex chars = 32 bytes).
7
- * - If a Getter, it is called each time to retrieve the key (useful for rotation or lazy loading).
8
- * - If an object with `env`, the key is read from the specified environment variable.
9
- */
10
- export type FirestoreEncryptedFieldSecretSource = string | Getter<string> | {
11
- env: string;
12
- };
13
- /**
14
- * Configuration for encrypted Firestore fields.
15
- *
16
- * @template T - The JSON-serializable value type stored in the model.
6
+ * @template T - The JSON-serializable value type.
17
7
  */
18
8
  export interface FirestoreEncryptedFieldConfig<T> {
19
9
  /**
20
- * The encryption secret source.
21
- *
22
- * Expects a 64-character hex string representing a 32-byte AES-256 key.
10
+ * Secret source for the encryption key.
23
11
  */
24
- readonly secret: FirestoreEncryptedFieldSecretSource;
12
+ readonly secret: GetterOrValue<string>;
25
13
  /**
26
- * Default model value when the Firestore data is null/undefined.
14
+ * Default value when the field is missing from Firestore.
27
15
  */
28
- readonly default?: GetterOrValue<T>;
16
+ readonly default: GetterOrValue<T>;
29
17
  }
30
18
  /**
31
- * Configuration for optional encrypted Firestore fields.
19
+ * Configuration for an optional encrypted Firestore field.
32
20
  *
33
- * @template T - The JSON-serializable value type stored in the model.
21
+ * @template T - The JSON-serializable value type.
34
22
  */
35
23
  export interface OptionalFirestoreEncryptedFieldConfig<T> {
36
24
  /**
37
- * The encryption secret source.
38
- *
39
- * Expects a 64-character hex string representing a 32-byte AES-256 key.
25
+ * Secret source for the encryption key.
40
26
  */
41
- readonly secret: FirestoreEncryptedFieldSecretSource;
27
+ readonly secret: GetterOrValue<string>;
42
28
  }
43
29
  /**
44
30
  * Creates a Firestore field mapping that encrypts/decrypts a JSON-serializable value
@@ -50,7 +36,7 @@ export interface OptionalFirestoreEncryptedFieldConfig<T> {
50
36
  * @example
51
37
  * ```typescript
52
38
  * const jwksField = firestoreEncryptedField<JWKSet>({
53
- * secret: { env: 'FIRESTORE_ENCRYPTION_KEY' },
39
+ * secret: process.env['FIRESTORE_ENCRYPTION_KEY']!,
54
40
  * default: () => ({ keys: [] })
55
41
  * });
56
42
  * ```
@@ -69,7 +55,7 @@ export declare function firestoreEncryptedField<T>(config: FirestoreEncryptedFie
69
55
  * @example
70
56
  * ```typescript
71
57
  * const optionalSecretField = optionalFirestoreEncryptedField<OAuthClientSecret>({
72
- * secret: { env: 'FIRESTORE_ENCRYPTION_KEY' }
58
+ * secret: process.env['FIRESTORE_ENCRYPTION_KEY']!
73
59
  * });
74
60
  * ```
75
61
  *
@@ -1,35 +1,51 @@
1
1
  import { type DocumentDataWithIdAndKey, type FirestoreDocument, type FirestoreDocumentData } from '@dereekb/firebase';
2
2
  import { type CallableContext } from '../type';
3
+ /**
4
+ * Asserts that the callable context contains auth data with a valid UID.
5
+ *
6
+ * @throws {HttpsError} Throws unauthenticated error if no auth data is present.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * assertContextHasAuth(context);
11
+ * // Safe to access context.auth.uid
12
+ * ```
13
+ */
3
14
  export declare function assertContextHasAuth(context: CallableContext): void;
4
15
  /**
5
- * Attempts to load data from the document. A modelNotAvailableError is thrown if the snapshot data is null/undefined (the document does not exist).
16
+ * Loads the snapshot data from a Firestore document, throwing if the document does not exist.
17
+ *
18
+ * @param document - The Firestore document to load data from.
19
+ * @param message - Optional custom error message.
20
+ * @throws {HttpsError} Throws a {@link modelNotAvailableError} (404) if the document has no data.
6
21
  *
7
- * @param document
8
- * @param message
9
- * @returns
22
+ * @example
23
+ * ```typescript
24
+ * const userData = await assertSnapshotData(userDocument);
25
+ * ```
10
26
  */
11
27
  export declare function assertSnapshotData<D extends FirestoreDocument<any>>(document: D, message?: string): Promise<FirestoreDocumentData<D>>;
12
28
  /**
13
- * Convenience function for assertSnapshotData that also attaches the id and key of the document to the data.
29
+ * Loads snapshot data and attaches the document's `id` and `key` to the result.
30
+ *
31
+ * Combines {@link assertSnapshotData} with {@link setIdAndKeyFromKeyIdRefOnDocumentData}.
14
32
  *
15
- * @param document
16
- * @param message
17
- * @returns
33
+ * @param document - The Firestore document to load data from.
34
+ * @param message - Optional custom error message.
35
+ * @throws {HttpsError} Throws a {@link modelNotAvailableError} (404) if the document has no data.
18
36
  */
19
37
  export declare function assertSnapshotDataWithKey<D extends FirestoreDocument<any>>(document: D, message?: string): Promise<DocumentDataWithIdAndKey<FirestoreDocumentData<D>>>;
20
38
  /**
21
- * Asserts that the document exists. A modelNotAvailableError is thrown if the document does not exist.
39
+ * Asserts that the Firestore document exists without loading its data.
22
40
  *
23
- * @param document
24
- * @param message
25
- * @returns
41
+ * @param document - The Firestore document to check.
42
+ * @param message - Optional custom error message.
43
+ * @throws {HttpsError} Throws a {@link modelNotAvailableError} (404) if the document does not exist.
26
44
  */
27
45
  export declare function assertDocumentExists<D extends FirestoreDocument<any>>(document: D, message?: string): Promise<void>;
28
46
  /**
29
- * Error thrown by assertDocumentExists().
47
+ * Creates a {@link modelNotAvailableError} for the given document's model type.
30
48
  *
31
- * @param document
32
- * @param message
33
- * @returns
49
+ * Used by {@link assertDocumentExists} and other assertion functions.
34
50
  */
35
51
  export declare function documentModelNotAvailableError(document: Pick<FirestoreDocument<any>, 'modelType'>, message?: string): import("firebase-functions/https").HttpsError;
@@ -1,4 +1,30 @@
1
1
  import { type CallableContext } from '../type';
2
+ /**
3
+ * A {@link CallableContext} narrowed to guarantee that `auth` data is present.
4
+ *
5
+ * Used as the base context type for authenticated function handlers throughout `@dereekb/firebase-server`.
6
+ */
2
7
  export type CallableContextWithAuthData<R extends CallableContext = CallableContext> = Omit<R, 'auth'> & Required<Pick<R, 'auth'>>;
8
+ /**
9
+ * Type guard that checks whether the given callable context contains authenticated user data (non-null auth with a uid).
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * if (isContextWithAuthData(context)) {
14
+ * console.log(context.auth.uid);
15
+ * }
16
+ * ```
17
+ */
3
18
  export declare function isContextWithAuthData<R extends CallableContext>(context: CallableContext): context is CallableContextWithAuthData<R>;
19
+ /**
20
+ * Asserts that the callable context contains authenticated user data.
21
+ *
22
+ * @throws {HttpsError} Throws an unauthenticated error if auth data is missing.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * assertIsContextWithAuthData(context);
27
+ * // context.auth is now guaranteed to be present
28
+ * ```
29
+ */
4
30
  export declare function assertIsContextWithAuthData<R extends CallableContext>(context: CallableContext): asserts context is CallableContextWithAuthData<R>;
@@ -1,5 +1,20 @@
1
1
  import { type ThrowErrorFunction } from '@dereekb/util';
2
2
  import type * as admin from 'firebase-admin';
3
+ /**
4
+ * Error code for when a phone number is already registered in Firebase Auth.
5
+ */
3
6
  export declare const PHONE_NUMBER_ALREADY_EXISTS_ERROR_CODE = "PHONE_NUMBER_ALREADY_EXISTS";
7
+ /**
8
+ * Creates a precondition conflict (409) error indicating the phone number already exists.
9
+ */
4
10
  export declare function phoneNumberAlreadyExistsError(): import("firebase-functions/https").HttpsError;
11
+ /**
12
+ * Handles Firebase Auth errors by mapping known error codes to typed HTTP errors.
13
+ *
14
+ * Currently maps `auth/phone-number-already-exists` to a 409 conflict error.
15
+ * Unknown error codes are forwarded to the optional handler.
16
+ *
17
+ * @param e - The caught error.
18
+ * @param handleUnknownCode - Optional handler for Firebase errors with unrecognized codes.
19
+ */
5
20
  export declare function handleFirebaseAuthError(e: unknown, handleUnknownCode?: ThrowErrorFunction<admin.FirebaseError>): never | void;
@@ -2,33 +2,62 @@ import { type ErrorMessageOrPartialServerError, type ServerError, type StringErr
2
2
  import type * as admin from 'firebase-admin';
3
3
  import { type FirebaseErrorCode } from '@dereekb/firebase';
4
4
  import { HttpsError } from 'firebase-functions/https';
5
+ /**
6
+ * Creates an unauthenticated {@link HttpsError} indicating the request context has no auth data.
7
+ */
5
8
  export declare function unauthenticatedContextHasNoAuthData(): HttpsError;
9
+ /**
10
+ * Creates an unauthenticated {@link HttpsError} indicating the request context has no user UID.
11
+ */
6
12
  export declare function unauthenticatedContextHasNoUidError(): HttpsError;
13
+ /**
14
+ * Standard error code constants and factory functions for creating typed {@link HttpsError} instances.
15
+ *
16
+ * Each factory wraps the Firebase `HttpsError` with a consistent shape: an HTTP status code,
17
+ * a string error code, and an optional {@link ServerError} detail object.
18
+ */
7
19
  export declare const UNAUTHENTICATED_ERROR_CODE = "UNAUTHENTICATED";
20
+ /** Creates an unauthenticated (401) {@link HttpsError}. */
8
21
  export declare function unauthenticatedError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
9
22
  export declare const FORBIDDEN_ERROR_CODE = "FORBIDDEN";
23
+ /** Creates a forbidden (403) {@link HttpsError}. */
10
24
  export declare function forbiddenError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
11
25
  export declare const PERMISSION_DENIED_ERROR_CODE = "PERMISSION_DENIED";
26
+ /** Creates a permission-denied (403) {@link HttpsError}. */
12
27
  export declare function permissionDeniedError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
13
28
  export declare const NOT_FOUND_ERROR_CODE = "NOT_FOUND";
29
+ /** Creates a not-found (404) {@link HttpsError}. */
14
30
  export declare function notFoundError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
15
31
  export declare const MODEL_NOT_AVAILABLE_ERROR_CODE = "MODEL_NOT_AVAILABLE";
32
+ /** Creates a model-not-available (404) {@link HttpsError}, used when a Firestore document does not exist. */
16
33
  export declare function modelNotAvailableError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
17
34
  export declare const BAD_REQUEST_ERROR_CODE = "BAD_REQUEST";
35
+ /** Creates a bad-request (400) {@link HttpsError}. */
18
36
  export declare function badRequestError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
19
37
  export declare const CONFLICT_ERROR_CODE = "CONFLICT";
38
+ /** Creates a precondition-conflict (409) {@link HttpsError}. */
20
39
  export declare function preconditionConflictError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
21
40
  export declare const ALREADY_EXISTS_ERROR_CODE = "ALREADY_EXISTS";
41
+ /** Creates an already-exists (409) {@link HttpsError}. */
22
42
  export declare function alreadyExistsError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
23
43
  export declare const UNAVAILABLE_ERROR_CODE = "UNAVAILABLE";
44
+ /** Creates an unavailable (503) {@link HttpsError}. */
24
45
  export declare function unavailableError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
25
46
  export declare const UNAVAILABLE_OR_DEACTIVATED_FUNCTION_ERROR_CODE = "UNAVAILABLE_OR_DEACTIVATED_FUNCTION";
47
+ /** Creates an unimplemented (501) {@link HttpsError} for deactivated or unavailable functions. */
26
48
  export declare function unavailableOrDeactivatedFunctionError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
27
49
  export declare const INTERNAL_SERVER_ERROR_CODE = "INTERNAL_ERROR";
50
+ /** Creates an internal-error (500) {@link HttpsError}. */
28
51
  export declare function internalServerError(messageOrError?: ErrorMessageOrPartialServerError): HttpsError;
52
+ /**
53
+ * Discriminator for the type of Firebase server error encountered.
54
+ */
29
55
  export type FirebaseServerErrorInfoType = 'httpsError' | 'firebaseError' | 'unknown';
30
56
  /**
31
- * Server error information
57
+ * Structured information extracted from a caught Firebase server error.
58
+ *
59
+ * Provides typed access to the original error, Firebase error codes, and any
60
+ * embedded {@link ServerError} details from {@link HttpsError} instances.
32
61
  */
33
62
  export interface FirebaseServerErrorInfo {
34
63
  /**
@@ -60,16 +89,51 @@ export interface FirebaseServerErrorInfo {
60
89
  */
61
90
  readonly firebaseError?: admin.FirebaseError;
62
91
  }
92
+ /**
93
+ * Type guard for Firebase {@link HttpsError} instances.
94
+ */
63
95
  export declare function isFirebaseHttpsError(input: unknown | HttpsError): input is HttpsError;
96
+ /**
97
+ * Type guard for Firebase Admin {@link admin.FirebaseError} instances.
98
+ */
64
99
  export declare function isFirebaseError(input: unknown | admin.FirebaseError): input is admin.FirebaseError;
65
100
  /**
66
- * Creates a FirebaseServerErrorInfo from the input.
101
+ * Analyzes a caught error and extracts structured Firebase error information.
102
+ *
103
+ * Classifies the error as an {@link HttpsError}, {@link admin.FirebaseError}, or unknown,
104
+ * and extracts any embedded error codes and {@link ServerError} details.
67
105
  *
68
- * @param e
69
- * @returns
106
+ * @param e - The caught error to analyze.
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * try {
111
+ * await someFirebaseOperation();
112
+ * } catch (e) {
113
+ * const info = firebaseServerErrorInfo(e);
114
+ * if (info.serverErrorCode === 'MODEL_NOT_AVAILABLE') { ... }
115
+ * }
116
+ * ```
70
117
  */
71
118
  export declare function firebaseServerErrorInfo(e: unknown): FirebaseServerErrorInfo;
119
+ /**
120
+ * Returns a tuple of [firebaseErrorCode, errorInfo] for pattern-matching on Firebase error codes.
121
+ */
72
122
  export declare function firebaseServerErrorInfoCodePair(e: unknown): [FirebaseErrorCode | undefined, FirebaseServerErrorInfo];
123
+ /**
124
+ * Returns a tuple of [serverError, errorInfo] for pattern-matching on embedded server error details.
125
+ */
73
126
  export declare function firebaseServerErrorInfoServerErrorPair(e: unknown): [ServerError | undefined, FirebaseServerErrorInfo];
127
+ /**
128
+ * Returns a tuple of [serverErrorCode, errorInfo] for pattern-matching on server error string codes.
129
+ */
74
130
  export declare function firebaseServerErrorInfoServerErrorCodePair(e: unknown): [StringErrorCode | undefined, FirebaseServerErrorInfo];
131
+ /**
132
+ * Handles a caught error if it is a Firebase Admin error, passing it to the given handler function.
133
+ *
134
+ * If the error is not a Firebase error (no `code` property), this is a no-op.
135
+ *
136
+ * @param e - The caught error.
137
+ * @param handleFirebaseErrorFn - Handler that receives the typed {@link admin.FirebaseError}.
138
+ */
75
139
  export declare function handleFirebaseError(e: unknown, handleFirebaseErrorFn: ThrowErrorFunction<admin.FirebaseError>): never | void;
@@ -1,14 +1,26 @@
1
- import { type ClassType, type Getter } from '@dereekb/util';
1
+ import { type ClassType, type Getter, type WebsitePath } from '@dereekb/util';
2
2
  import { type INestApplication, type INestApplicationContext, type NestApplicationOptions, type Provider } from '@nestjs/common';
3
3
  import express from 'express';
4
4
  import type * as admin from 'firebase-admin';
5
5
  import { type StorageBucketId } from '@dereekb/firebase';
6
- import { type ServerEnvironmentConfig } from '@dereekb/nestjs';
6
+ import { type FirebaseServerEnvironmentConfig } from '../env/env.config';
7
+ import { type GlobalRoutePrefixConfig } from './middleware/globalprefix';
8
+ /**
9
+ * A running NestJS server instance backed by Express, paired with a lazy promise getter for the NestJS application context.
10
+ */
7
11
  export interface NestServer {
8
12
  readonly server: express.Express;
9
13
  readonly nest: NestAppPromiseGetter;
10
14
  }
15
+ /**
16
+ * Lazy getter that returns a promise resolving to the initialized NestJS application context.
17
+ */
11
18
  export type NestAppPromiseGetter = Getter<Promise<INestApplicationContext>>;
19
+ /**
20
+ * Manages the lifecycle of a NestJS server instance for Firebase Cloud Functions.
21
+ *
22
+ * Caches the server per Firebase app name so repeated invocations reuse the same instance.
23
+ */
12
24
  export interface NestServerInstance<T> {
13
25
  /**
14
26
  * Root module class of the app.
@@ -19,7 +31,7 @@ export interface NestServerInstance<T> {
19
31
  *
20
32
  * If already initialized the server will not be initialized again.
21
33
  */
22
- initNestServer(firebaseApp: admin.app.App, env?: NestServerEnvironmentConfig): NestServer;
34
+ initNestServer(firebaseApp: admin.app.App, env?: NestFirebaseServerEnvironmentConfig): NestServer;
23
35
  /**
24
36
  * Terminates the nest server for the input app.
25
37
  *
@@ -27,9 +39,26 @@ export interface NestServerInstance<T> {
27
39
  */
28
40
  removeNestServer(firebaseApp: admin.app.App): Promise<boolean>;
29
41
  }
30
- export declare class FirebaseNestServerRootModule {
31
- }
42
+ /** @deprecated Use `FirebaseNestServerRootModule` from `./app.module` instead. */
43
+ export { FirebaseNestServerRootModule } from './app.module';
44
+ /**
45
+ * Optional hook to customize the NestJS application after creation but before initialization.
46
+ */
32
47
  export type ConfigureNestServerInstanceFunction = (nestApp: INestApplication) => INestApplication | void;
48
+ /**
49
+ * Configuration for creating a {@link NestServerInstance}, including the root module class,
50
+ * global providers, middleware toggles, storage defaults, and app-level options.
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const instance = nestServerInstance({
55
+ * moduleClass: AppModule,
56
+ * appCheckEnabled: true,
57
+ * globalApiRoutePrefix: '/api',
58
+ * configureWebhooks: true
59
+ * });
60
+ * ```
61
+ */
33
62
  export interface NestServerInstanceConfig<T> {
34
63
  /**
35
64
  * Module to instantiate.
@@ -66,17 +95,32 @@ export interface NestServerInstanceConfig<T> {
66
95
  */
67
96
  readonly applicationOptions?: NestApplicationOptions;
68
97
  /**
69
- * Global routing prefix.
98
+ * Global routing prefix or options.
70
99
  *
71
100
  * Example: '/api'
72
101
  */
73
- readonly globalApiRoutePrefix?: string;
102
+ readonly globalApiRoutePrefix?: WebsitePath | GlobalRoutePrefixConfig;
74
103
  /**
75
104
  * Optional configuration function
76
105
  */
77
106
  readonly configureNestServerInstance?: ConfigureNestServerInstanceFunction;
78
107
  }
79
- export interface NestServerEnvironmentConfig {
80
- readonly environment: ServerEnvironmentConfig;
108
+ export interface NestFirebaseServerEnvironmentConfig {
109
+ readonly environment: FirebaseServerEnvironmentConfig;
81
110
  }
111
+ /** @deprecated Use NestFirebaseServerEnvironmentConfig instead. */
112
+ export type NestServerEnvironmentConfig = NestFirebaseServerEnvironmentConfig;
113
+ /**
114
+ * Creates a {@link NestServerInstance} that manages NestJS server lifecycle within Firebase Cloud Functions.
115
+ *
116
+ * The returned instance caches servers by Firebase app name, so calling `initNestServer` multiple
117
+ * times with the same app reuses the existing server. The factory wires up Firebase Admin,
118
+ * environment config, storage, AppCheck middleware, and webhook routes based on the config.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const instance = nestServerInstance({ moduleClass: AppModule, appCheckEnabled: true });
123
+ * const { server } = instance.initNestServer(firebaseApp, { environment: envConfig });
124
+ * ```
125
+ */
82
126
  export declare function nestServerInstance<T>(config: NestServerInstanceConfig<T>): NestServerInstance<T>;
@@ -0,0 +1,90 @@
1
+ import { type ArrayOrValue, type ClassType, type Getter, type Maybe, type WebsitePath } from '@dereekb/util';
2
+ import { type DynamicModule, type Provider } from '@nestjs/common';
3
+ import { type StorageBucketId } from '@dereekb/firebase';
4
+ import { type FirebaseServerEnvironmentConfig } from '../env/env.config';
5
+ import { GlobalRoutePrefixConfig } from './middleware/globalprefix';
6
+ import type * as admin from 'firebase-admin';
7
+ export declare class FirebaseNestServerRootModule {
8
+ }
9
+ /**
10
+ * Configuration for building the shared NestJS root module used by both
11
+ * production (`nestServerInstance`) and test (`firebaseAdminNestContextWithFixture`) setups.
12
+ *
13
+ * This ensures both environments use the same provider assembly logic so they don't drift apart.
14
+ */
15
+ export interface NestServerRootModuleConfig {
16
+ /**
17
+ * Module(s) to import into the root module.
18
+ */
19
+ readonly modules: ArrayOrValue<ClassType | DynamicModule>;
20
+ /**
21
+ * Getter for the Firebase Admin app instance.
22
+ * When provided, the `FIREBASE_APP_TOKEN` is made available globally.
23
+ */
24
+ readonly firebaseAppGetter?: Maybe<Getter<admin.app.App>>;
25
+ /**
26
+ * Additional providers to include globally.
27
+ */
28
+ readonly additionalProviders?: Provider[];
29
+ /**
30
+ * Environment configuration. When provided, injects env tokens via `firebaseServerEnvTokenProviders`.
31
+ */
32
+ readonly envConfig?: FirebaseServerEnvironmentConfig;
33
+ /**
34
+ * Whether to configure `FirebaseServerEnvService` / `ServerEnvironmentService`.
35
+ * Defaults to true when `envConfig` is provided.
36
+ */
37
+ readonly configureEnvService?: boolean;
38
+ /**
39
+ * Default storage bucket ID.
40
+ */
41
+ readonly defaultStorageBucket?: StorageBucketId;
42
+ /**
43
+ * Whether to force using the storage bucket.
44
+ */
45
+ readonly forceStorageBucket?: boolean;
46
+ /**
47
+ * Global route prefix configuration.
48
+ * The `GlobalRoutePrefixConfig` token is always provided (empty object when no config given).
49
+ */
50
+ readonly globalApiRoutePrefix?: WebsitePath | GlobalRoutePrefixConfig;
51
+ /**
52
+ * Whether to add the webhook middleware module.
53
+ */
54
+ readonly configureWebhooks?: boolean;
55
+ /**
56
+ * Whether to add the AppCheck middleware module.
57
+ * Defaults to false (production code sets this to true via `appCheckEnabled`).
58
+ */
59
+ readonly appCheckEnabled?: boolean;
60
+ }
61
+ /**
62
+ * Result of {@link buildNestServerRootModule}.
63
+ */
64
+ export interface NestServerRootModuleResult {
65
+ /**
66
+ * The fully configured DynamicModule (global: true) containing
67
+ * all providers and imports.
68
+ */
69
+ readonly rootModule: DynamicModule;
70
+ /**
71
+ * The resolved `GlobalRoutePrefixConfig`. Callers that create
72
+ * an `INestApplication` can use this to call `setGlobalPrefix()`.
73
+ */
74
+ readonly globalApiRoutePrefixConfig: Maybe<GlobalRoutePrefixConfig>;
75
+ }
76
+ /**
77
+ * Builds the shared root `DynamicModule` used by both production and test setups.
78
+ *
79
+ * Assembles:
80
+ * - Firebase app token provider
81
+ * - Environment token providers and env service bindings
82
+ * - Additional providers
83
+ * - Storage bucket provider
84
+ * - `GlobalRoutePrefixConfig` token
85
+ * - Optional webhook and AppCheck middleware modules
86
+ *
87
+ * @param config - Shared configuration
88
+ * @returns The root module and resolved prefix config
89
+ */
90
+ export declare function buildNestServerRootModule(config: NestServerRootModuleConfig): NestServerRootModuleResult;