@dereekb/firebase-server 13.2.0 → 13.2.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/index.cjs.js +340 -24
- package/index.cjs.js.map +1 -1
- package/index.esm.js +332 -25
- package/index.esm.js.map +1 -1
- package/mailgun/package.json +8 -8
- package/model/package.json +8 -8
- package/package.json +9 -9
- package/src/lib/auth/auth.service.d.ts +370 -85
- package/src/lib/nest/model/api.details.d.ts +249 -0
- package/src/lib/nest/model/call.model.function.d.ts +3 -2
- package/src/lib/nest/model/index.d.ts +1 -0
- package/src/lib/nest/model/specifier.function.d.ts +2 -1
- package/test/package.json +8 -8
- package/zoho/package.json +8 -8
package/index.esm.js
CHANGED
|
@@ -270,7 +270,34 @@ class FirebaseServerAuthNewUserSendSetupDetailsSendOnceError extends BaseError {
|
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Generates a random 6-digit number for use as a temporary password or reset token.
|
|
275
|
+
*
|
|
276
|
+
* Used internally by {@link AbstractFirebaseServerAuthUserContext.beginResetPassword} and
|
|
277
|
+
* {@link AbstractFirebaseServerNewUserService.generateRandomSetupPassword} for one-time codes.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* const pin = DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR(); // e.g. 482910
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
const DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR = randomNumberFactory({ min: 100000, max: 1000000, round: 'floor' });
|
|
285
|
+
/**
|
|
286
|
+
* Base implementation of {@link FirebaseServerAuthUserContext} that manages a single user's
|
|
287
|
+
* auth state (record, claims, roles, password) through the Firebase Admin Auth API.
|
|
288
|
+
*
|
|
289
|
+
* Caches the user record on first load and resets the cache automatically when claims are modified
|
|
290
|
+
* via {@link setClaims}. Subclass this to bind it to a specific {@link FirebaseServerAuthService} type.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* export class MyAuthUserContext extends AbstractFirebaseServerAuthUserContext<MyAuthService> {}
|
|
295
|
+
*
|
|
296
|
+
* const ctx = new MyAuthUserContext(authService, 'some-uid');
|
|
297
|
+
* const roles = await ctx.loadRoles();
|
|
298
|
+
* await ctx.addRoles(AUTH_ADMIN_ROLE);
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
274
301
|
class AbstractFirebaseServerAuthUserContext {
|
|
275
302
|
_service;
|
|
276
303
|
_uid;
|
|
@@ -294,6 +321,9 @@ class AbstractFirebaseServerAuthUserContext {
|
|
|
294
321
|
loadDetails() {
|
|
295
322
|
return this.loadRecord().then((record) => this.service.authDetailsForRecord(record));
|
|
296
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Generates a random numeric string for use as a temporary reset password.
|
|
326
|
+
*/
|
|
297
327
|
_generateResetPasswordKey() {
|
|
298
328
|
return String(DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR());
|
|
299
329
|
}
|
|
@@ -318,9 +348,6 @@ class AbstractFirebaseServerAuthUserContext {
|
|
|
318
348
|
return undefined;
|
|
319
349
|
}
|
|
320
350
|
}
|
|
321
|
-
/**
|
|
322
|
-
* Sets the user's password.
|
|
323
|
-
*/
|
|
324
351
|
async setPassword(password) {
|
|
325
352
|
const record = await this.updateUser({ password });
|
|
326
353
|
// clear password reset claims
|
|
@@ -353,15 +380,19 @@ class AbstractFirebaseServerAuthUserContext {
|
|
|
353
380
|
return this.updateClaims(claims);
|
|
354
381
|
}
|
|
355
382
|
/**
|
|
356
|
-
*
|
|
383
|
+
* Replaces all role-based claims with those derived from the given roles.
|
|
357
384
|
*
|
|
358
|
-
* All
|
|
385
|
+
* All existing claims are cleared first. Use `claimsToRetain` to preserve non-role claims
|
|
386
|
+
* (e.g., setup or application-specific claims) through the replacement.
|
|
359
387
|
*
|
|
360
|
-
*
|
|
388
|
+
* @param roles - The complete set of roles to assign.
|
|
389
|
+
* @param claimsToRetain - Additional claims to merge in alongside the role-derived claims.
|
|
361
390
|
*
|
|
362
|
-
* @
|
|
363
|
-
*
|
|
364
|
-
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```typescript
|
|
393
|
+
* // Set roles while preserving a custom claim
|
|
394
|
+
* await userCtx.setRoles([AUTH_ADMIN_ROLE], { customFlag: 1 });
|
|
395
|
+
* ```
|
|
365
396
|
*/
|
|
366
397
|
async setRoles(roles, claimsToRetain) {
|
|
367
398
|
const claims = {
|
|
@@ -370,8 +401,11 @@ class AbstractFirebaseServerAuthUserContext {
|
|
|
370
401
|
};
|
|
371
402
|
return this.setClaims(claims);
|
|
372
403
|
}
|
|
404
|
+
/**
|
|
405
|
+
* Converts roles to their corresponding claim keys, filtering out null/undefined entries
|
|
406
|
+
* that represent unrelated claims in the service's {@link FirebaseServerAuthService.claimsForRoles} output.
|
|
407
|
+
*/
|
|
373
408
|
_claimsForRolesChange(roles) {
|
|
374
|
-
// filter null/undefined since the claims will contain null values for claims that are not related.
|
|
375
409
|
return filterNullAndUndefinedValues(this.service.claimsForRoles(asSet(roles)));
|
|
376
410
|
}
|
|
377
411
|
loadClaims() {
|
|
@@ -401,6 +435,15 @@ class AbstractFirebaseServerAuthUserContext {
|
|
|
401
435
|
});
|
|
402
436
|
}
|
|
403
437
|
}
|
|
438
|
+
/**
|
|
439
|
+
* Base implementation of {@link FirebaseServerAuthContext} with cached getters for roles, admin status,
|
|
440
|
+
* and ToS status to avoid redundant computation within a single request.
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* ```typescript
|
|
444
|
+
* export class MyAuthContext extends AbstractFirebaseServerAuthContext<MyAuthContext, MyUserContext, MyAuthService> {}
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
404
447
|
class AbstractFirebaseServerAuthContext {
|
|
405
448
|
_service;
|
|
406
449
|
_context;
|
|
@@ -442,15 +485,52 @@ class AbstractFirebaseServerAuthContext {
|
|
|
442
485
|
}
|
|
443
486
|
}
|
|
444
487
|
/**
|
|
445
|
-
* 1 hour
|
|
488
|
+
* Default throttle duration (1 hour) between setup content sends to prevent spam.
|
|
489
|
+
*
|
|
490
|
+
* Used by {@link AbstractFirebaseServerNewUserService.sendSetupContent} to rate-limit delivery.
|
|
446
491
|
*/
|
|
447
492
|
const DEFAULT_SETUP_COM_THROTTLE_TIME = hoursToMs(1);
|
|
493
|
+
/**
|
|
494
|
+
* Resolves a {@link UserContextOrUid} to a concrete user context instance.
|
|
495
|
+
*
|
|
496
|
+
* If a string UID is provided, creates a new user context via the auth service.
|
|
497
|
+
* If an existing context is provided, returns it as-is.
|
|
498
|
+
*
|
|
499
|
+
* @param authService - The auth service to create a context from if needed.
|
|
500
|
+
* @param userContextOrUid - A user context or UID string.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```typescript
|
|
504
|
+
* const ctx = userContextFromUid(authService, 'some-uid');
|
|
505
|
+
* const sameCtx = userContextFromUid(authService, ctx); // returns ctx unchanged
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
448
508
|
function userContextFromUid(authService, userContextOrUid) {
|
|
449
509
|
const userContext = typeof userContextOrUid === 'string' ? authService.userContext(userContextOrUid) : userContextOrUid;
|
|
450
510
|
return userContext;
|
|
451
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* Base implementation of {@link FirebaseServerNewUserService} that handles user creation,
|
|
514
|
+
* setup claims management, throttled setup content delivery, and setup completion.
|
|
515
|
+
*
|
|
516
|
+
* Subclasses must implement {@link sendSetupContentToUser} to define how setup content
|
|
517
|
+
* (e.g., invitation email, SMS) is delivered to the user.
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```typescript
|
|
521
|
+
* export class MyNewUserService extends AbstractFirebaseServerNewUserService<MyUserContext> {
|
|
522
|
+
* protected async sendSetupContentToUser(details: FirebaseServerAuthNewUserSetupDetails<MyUserContext>): Promise<void> {
|
|
523
|
+
* await this.emailService.sendInvite(details.userContext.uid, details.claims.setupPassword);
|
|
524
|
+
* }
|
|
525
|
+
* }
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
452
528
|
class AbstractFirebaseServerNewUserService {
|
|
453
529
|
_authService;
|
|
530
|
+
/**
|
|
531
|
+
* Minimum time between setup content sends. Defaults to {@link DEFAULT_SETUP_COM_THROTTLE_TIME} (1 hour).
|
|
532
|
+
* Override in subclasses to customize the throttle window.
|
|
533
|
+
*/
|
|
454
534
|
setupThrottleTime = DEFAULT_SETUP_COM_THROTTLE_TIME;
|
|
455
535
|
constructor(authService) {
|
|
456
536
|
this._authService = authService;
|
|
@@ -562,19 +642,15 @@ class AbstractFirebaseServerNewUserService {
|
|
|
562
642
|
}
|
|
563
643
|
return details;
|
|
564
644
|
}
|
|
645
|
+
/**
|
|
646
|
+
* Records the current timestamp as the last setup content communication date in the user's claims.
|
|
647
|
+
*/
|
|
565
648
|
async updateSetupContentSentTime(details) {
|
|
566
649
|
const setupCommunicationAt = toISODateString(new Date());
|
|
567
650
|
await details.userContext.updateClaims({
|
|
568
651
|
setupCommunicationAt
|
|
569
652
|
});
|
|
570
653
|
}
|
|
571
|
-
/**
|
|
572
|
-
* Update a user's claims to clear any setup-related content.
|
|
573
|
-
*
|
|
574
|
-
* Returns true if a user was updated.
|
|
575
|
-
*
|
|
576
|
-
* @param uid
|
|
577
|
-
*/
|
|
578
654
|
async markUserSetupAsComplete(uid) {
|
|
579
655
|
const userContext = this.authService.userContext(uid);
|
|
580
656
|
const userExists = await userContext.exists();
|
|
@@ -583,6 +659,13 @@ class AbstractFirebaseServerNewUserService {
|
|
|
583
659
|
}
|
|
584
660
|
return userExists;
|
|
585
661
|
}
|
|
662
|
+
/**
|
|
663
|
+
* Creates a new Firebase Auth user from the initialization input.
|
|
664
|
+
*
|
|
665
|
+
* Generates a random setup password if none is provided. Override to customize user creation behavior.
|
|
666
|
+
*
|
|
667
|
+
* @throws Throws if the Firebase Admin SDK rejects the user creation.
|
|
668
|
+
*/
|
|
586
669
|
async createNewUser(input) {
|
|
587
670
|
const { uid, displayName, email, phone: phoneNumber, setupPassword: inputPassword } = input;
|
|
588
671
|
const password = inputPassword ?? this.generateRandomSetupPassword();
|
|
@@ -601,6 +684,9 @@ class AbstractFirebaseServerNewUserService {
|
|
|
601
684
|
generateRandomSetupPassword() {
|
|
602
685
|
return `${DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR()}`;
|
|
603
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* Clears setup-related claims (setup password and last communication date) from the user.
|
|
689
|
+
*/
|
|
604
690
|
async updateClaimsToClearUser(userContext) {
|
|
605
691
|
await userContext.updateClaims({
|
|
606
692
|
[FIREBASE_SERVER_AUTH_CLAIMS_SETUP_PASSWORD_KEY]: null,
|
|
@@ -608,18 +694,65 @@ class AbstractFirebaseServerNewUserService {
|
|
|
608
694
|
});
|
|
609
695
|
}
|
|
610
696
|
}
|
|
697
|
+
/**
|
|
698
|
+
* No-op implementation of {@link AbstractFirebaseServerNewUserService} that skips sending setup content.
|
|
699
|
+
*
|
|
700
|
+
* Used as the default {@link FirebaseServerNewUserService} when no custom delivery mechanism is configured.
|
|
701
|
+
*/
|
|
611
702
|
class NoSetupContentFirebaseServerNewUserService extends AbstractFirebaseServerNewUserService {
|
|
612
|
-
async sendSetupContentToUser(
|
|
703
|
+
async sendSetupContentToUser(_details) {
|
|
613
704
|
// send nothing.
|
|
614
705
|
}
|
|
615
706
|
}
|
|
616
707
|
/**
|
|
617
|
-
*
|
|
708
|
+
* Abstract contract for a Firebase Server authentication service.
|
|
709
|
+
*
|
|
710
|
+
* Provides the core API for creating auth contexts from callable requests, managing user contexts,
|
|
711
|
+
* checking admin/ToS status, converting between roles and claims, and creating new users.
|
|
712
|
+
*
|
|
713
|
+
* Implement this by extending {@link AbstractFirebaseServerAuthService}, which provides default
|
|
714
|
+
* implementations for most methods and only requires `readRoles`, `claimsForRoles`,
|
|
715
|
+
* `userContext`, and `_context` to be defined.
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* class MyAuthService extends AbstractFirebaseServerAuthService<MyUserContext, MyAuthContext> {
|
|
720
|
+
* readRoles(claims: AuthClaims): AuthRoleSet { ... }
|
|
721
|
+
* claimsForRoles(roles: AuthRoleSet): AuthClaimsUpdate { ... }
|
|
722
|
+
* userContext(uid: string): MyUserContext { ... }
|
|
723
|
+
* protected _context(ctx: CallableContextWithAuthData): MyAuthContext { ... }
|
|
724
|
+
* }
|
|
725
|
+
* ```
|
|
618
726
|
*/
|
|
619
727
|
class FirebaseServerAuthService {
|
|
620
728
|
}
|
|
621
729
|
/**
|
|
622
|
-
*
|
|
730
|
+
* Base implementation of {@link FirebaseServerAuthService} providing standard admin/ToS checks,
|
|
731
|
+
* auth context creation with assertion, and a default no-op new user service.
|
|
732
|
+
*
|
|
733
|
+
* Subclasses must implement:
|
|
734
|
+
* - {@link _context} - to create the concrete auth context type.
|
|
735
|
+
* - {@link userContext} - to create the concrete user context type.
|
|
736
|
+
* - {@link readRoles} - to define the claims-to-roles mapping.
|
|
737
|
+
* - {@link claimsForRoles} - to define the roles-to-claims mapping.
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```typescript
|
|
741
|
+
* export class MyAuthService extends AbstractFirebaseServerAuthService<MyUserContext, MyAuthContext> {
|
|
742
|
+
* protected _context(context: CallableContextWithAuthData): MyAuthContext {
|
|
743
|
+
* return new MyAuthContext(this, context);
|
|
744
|
+
* }
|
|
745
|
+
* userContext(uid: string): MyUserContext {
|
|
746
|
+
* return new MyUserContext(this, uid);
|
|
747
|
+
* }
|
|
748
|
+
* readRoles(claims: AuthClaims): AuthRoleSet {
|
|
749
|
+
* return MY_CLAIMS_SERVICE.toRoles(claims);
|
|
750
|
+
* }
|
|
751
|
+
* claimsForRoles(roles: AuthRoleSet): AuthClaimsUpdate {
|
|
752
|
+
* return MY_CLAIMS_SERVICE.toClaims(roles);
|
|
753
|
+
* }
|
|
754
|
+
* }
|
|
755
|
+
* ```
|
|
623
756
|
*/
|
|
624
757
|
class AbstractFirebaseServerAuthService {
|
|
625
758
|
_auth;
|
|
@@ -2025,6 +2158,163 @@ ConfigureFirebaseWebhookMiddlewareModule = __decorate([
|
|
|
2025
2158
|
Module({})
|
|
2026
2159
|
], ConfigureFirebaseWebhookMiddlewareModule);
|
|
2027
2160
|
|
|
2161
|
+
// MARK: Type Guards
|
|
2162
|
+
/**
|
|
2163
|
+
* Whether the details are specifier-level (has specifiers map).
|
|
2164
|
+
*/
|
|
2165
|
+
function isOnCallSpecifierApiDetails(details) {
|
|
2166
|
+
return details != null && 'specifiers' in details;
|
|
2167
|
+
}
|
|
2168
|
+
/**
|
|
2169
|
+
* Whether the details are CRUD-model-level (has modelTypes map).
|
|
2170
|
+
*/
|
|
2171
|
+
function isOnCallCrudModelApiDetails(details) {
|
|
2172
|
+
return details != null && 'modelTypes' in details;
|
|
2173
|
+
}
|
|
2174
|
+
/**
|
|
2175
|
+
* Whether the details are handler-level (leaf node — no specifiers or modelTypes).
|
|
2176
|
+
*/
|
|
2177
|
+
function isOnCallHandlerApiDetails(details) {
|
|
2178
|
+
return details != null && !('specifiers' in details) && !('modelTypes' in details);
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Attaches API details metadata to a handler function.
|
|
2182
|
+
*
|
|
2183
|
+
* The handler function is provided in the config object alongside its metadata.
|
|
2184
|
+
* The function is returned unchanged but with the _apiDetails property set.
|
|
2185
|
+
* Compatible with all handler types (create, read, update, delete, specifier).
|
|
2186
|
+
*
|
|
2187
|
+
* When `optionalAuth: true` is set, also marks the function as not requiring auth
|
|
2188
|
+
* (same effect as optionalAuthContext). This avoids the composition issue where
|
|
2189
|
+
* optionalAuthContext(withApiDetails(...)) would lose the _apiDetails.
|
|
2190
|
+
*
|
|
2191
|
+
* @example
|
|
2192
|
+
* ```typescript
|
|
2193
|
+
* // Handler with api details (auth required by default)
|
|
2194
|
+
* export const createGuestbook: DemoCreateModelFunction<CreateGuestbookParams> = withApiDetails({
|
|
2195
|
+
* inputType: createGuestbookParamsType,
|
|
2196
|
+
* fn: async (request) => {
|
|
2197
|
+
* const { nest, auth, data } = request;
|
|
2198
|
+
* const result = await nest.guestbookActions.createGuestbook(data);
|
|
2199
|
+
* return onCallCreateModelResultWithDocs(await result());
|
|
2200
|
+
* }
|
|
2201
|
+
* });
|
|
2202
|
+
*
|
|
2203
|
+
* // Handler with optional auth
|
|
2204
|
+
* export const profileCreate: DemoCreateModelFunction<{}> = withApiDetails({
|
|
2205
|
+
* optionalAuth: true,
|
|
2206
|
+
* fn: async (request) => { ... }
|
|
2207
|
+
* });
|
|
2208
|
+
* ```
|
|
2209
|
+
*/
|
|
2210
|
+
function withApiDetails(config) {
|
|
2211
|
+
const { optionalAuth, fn, ...apiDetails } = config;
|
|
2212
|
+
fn._apiDetails = apiDetails;
|
|
2213
|
+
if (optionalAuth) {
|
|
2214
|
+
fn._requireAuth = false;
|
|
2215
|
+
}
|
|
2216
|
+
return fn;
|
|
2217
|
+
}
|
|
2218
|
+
// MARK: Aggregation Utilities
|
|
2219
|
+
/**
|
|
2220
|
+
* Reads _apiDetails from a function if present.
|
|
2221
|
+
*/
|
|
2222
|
+
function readApiDetails(fn) {
|
|
2223
|
+
return fn?._apiDetails;
|
|
2224
|
+
}
|
|
2225
|
+
/**
|
|
2226
|
+
* Aggregates _apiDetails from a specifier handler config object.
|
|
2227
|
+
*
|
|
2228
|
+
* Returns OnCallSpecifierApiDetails if any handlers have _apiDetails, otherwise undefined.
|
|
2229
|
+
*/
|
|
2230
|
+
function aggregateSpecifierApiDetails(config) {
|
|
2231
|
+
const specifiers = {};
|
|
2232
|
+
let hasAny = false;
|
|
2233
|
+
for (const [key, handler] of Object.entries(config)) {
|
|
2234
|
+
const details = readApiDetails(handler);
|
|
2235
|
+
if (details != null) {
|
|
2236
|
+
// At the specifier level, details should be handler-level (OnCallModelFunctionApiDetails)
|
|
2237
|
+
specifiers[key] = details;
|
|
2238
|
+
hasAny = true;
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
return hasAny ? { specifiers } : undefined;
|
|
2242
|
+
}
|
|
2243
|
+
/**
|
|
2244
|
+
* Aggregates _apiDetails from a model type map (used by onCallCreateModel, etc.).
|
|
2245
|
+
*
|
|
2246
|
+
* Returns OnCallCrudModelApiDetails if any handlers have _apiDetails, otherwise undefined.
|
|
2247
|
+
*/
|
|
2248
|
+
function aggregateCrudModelApiDetails(map) {
|
|
2249
|
+
const modelTypes = {};
|
|
2250
|
+
let hasAny = false;
|
|
2251
|
+
for (const [key, handler] of Object.entries(map)) {
|
|
2252
|
+
const details = readApiDetails(handler);
|
|
2253
|
+
if (details != null) {
|
|
2254
|
+
modelTypes[key] = details;
|
|
2255
|
+
hasAny = true;
|
|
2256
|
+
}
|
|
2257
|
+
}
|
|
2258
|
+
return hasAny ? { modelTypes } : undefined;
|
|
2259
|
+
}
|
|
2260
|
+
/**
|
|
2261
|
+
* Aggregates _apiDetails from the top-level call model map.
|
|
2262
|
+
*
|
|
2263
|
+
* Returns OnCallModelApiDetails if any CRUD handlers have _apiDetails, otherwise undefined.
|
|
2264
|
+
*/
|
|
2265
|
+
function aggregateModelApiDetails(map) {
|
|
2266
|
+
const result = {};
|
|
2267
|
+
let hasAny = false;
|
|
2268
|
+
for (const [call, handler] of Object.entries(map)) {
|
|
2269
|
+
const details = readApiDetails(handler);
|
|
2270
|
+
if (details != null) {
|
|
2271
|
+
result[call] = details;
|
|
2272
|
+
hasAny = true;
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
return hasAny ? result : undefined;
|
|
2276
|
+
}
|
|
2277
|
+
/**
|
|
2278
|
+
* Extracts and pivots API details from a call model function into a model-first view.
|
|
2279
|
+
*
|
|
2280
|
+
* The internal aggregation tree is organized as CRUD → modelType. This function
|
|
2281
|
+
* pivots it to modelType → CRUD, which is the natural shape for MCP tool generation
|
|
2282
|
+
* and schema introspection.
|
|
2283
|
+
*
|
|
2284
|
+
* @param callModelFn The function returned by onCallModel(), or any object with _apiDetails.
|
|
2285
|
+
* @returns Model-first API details, or undefined if no _apiDetails are present.
|
|
2286
|
+
*
|
|
2287
|
+
* @example
|
|
2288
|
+
* ```typescript
|
|
2289
|
+
* const details = getModelApiDetails(demoCallModel);
|
|
2290
|
+
* // details.models['guestbook'].calls.create => { inputType: createGuestbookParamsType }
|
|
2291
|
+
* // details.models['profile'].calls.update => { specifiers: { _: {...}, username: {...} } }
|
|
2292
|
+
* ```
|
|
2293
|
+
*/
|
|
2294
|
+
function getModelApiDetails(callModelFn) {
|
|
2295
|
+
const topDetails = readApiDetails(callModelFn);
|
|
2296
|
+
if (topDetails == null) {
|
|
2297
|
+
return undefined;
|
|
2298
|
+
}
|
|
2299
|
+
const models = {};
|
|
2300
|
+
// Pivot: iterate CRUD types, then model types within each
|
|
2301
|
+
for (const [callType, crudDetails] of Object.entries(topDetails)) {
|
|
2302
|
+
if (crudDetails == null) {
|
|
2303
|
+
continue;
|
|
2304
|
+
}
|
|
2305
|
+
for (const [modelType, modelDetails] of Object.entries(crudDetails.modelTypes)) {
|
|
2306
|
+
if (modelDetails == null) {
|
|
2307
|
+
continue;
|
|
2308
|
+
}
|
|
2309
|
+
if (!models[modelType]) {
|
|
2310
|
+
models[modelType] = { calls: {} };
|
|
2311
|
+
}
|
|
2312
|
+
models[modelType].calls[callType] = modelDetails;
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
return Object.keys(models).length > 0 ? { models } : undefined;
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2028
2318
|
const nestFirebaseDoesNotExistError = (firebaseContextGrantedModelRoles) => {
|
|
2029
2319
|
return modelNotAvailableError({
|
|
2030
2320
|
data: {
|
|
@@ -2057,6 +2347,11 @@ function onCallSpecifierHandler(config) {
|
|
|
2057
2347
|
}
|
|
2058
2348
|
};
|
|
2059
2349
|
fn._requireAuth = false;
|
|
2350
|
+
// Aggregate _apiDetails from handler functions in the config
|
|
2351
|
+
const specifierApiDetails = aggregateSpecifierApiDetails(config);
|
|
2352
|
+
if (specifierApiDetails != null) {
|
|
2353
|
+
fn._apiDetails = specifierApiDetails;
|
|
2354
|
+
}
|
|
2060
2355
|
return fn;
|
|
2061
2356
|
}
|
|
2062
2357
|
function unknownModelCrudFunctionSpecifierError(specifier) {
|
|
@@ -2078,7 +2373,7 @@ function unknownModelCrudFunctionSpecifierError(specifier) {
|
|
|
2078
2373
|
*/
|
|
2079
2374
|
function onCallModel(map, config = {}) {
|
|
2080
2375
|
const { preAssert = () => undefined } = config;
|
|
2081
|
-
|
|
2376
|
+
const fn = (request) => {
|
|
2082
2377
|
const call = request.data?.call;
|
|
2083
2378
|
if (call) {
|
|
2084
2379
|
const callFn = map[call];
|
|
@@ -2095,6 +2390,12 @@ function onCallModel(map, config = {}) {
|
|
|
2095
2390
|
throw onCallModelMissingCallTypeError();
|
|
2096
2391
|
}
|
|
2097
2392
|
};
|
|
2393
|
+
// Aggregate _apiDetails from CRUD handlers in the map
|
|
2394
|
+
const modelApiDetails = aggregateModelApiDetails(map);
|
|
2395
|
+
if (modelApiDetails != null) {
|
|
2396
|
+
fn._apiDetails = modelApiDetails;
|
|
2397
|
+
}
|
|
2398
|
+
return fn;
|
|
2098
2399
|
}
|
|
2099
2400
|
function onCallModelMissingCallTypeError() {
|
|
2100
2401
|
return badRequestError(serverError({
|
|
@@ -2115,7 +2416,7 @@ function onCallModelUnknownCallTypeError(call) {
|
|
|
2115
2416
|
}
|
|
2116
2417
|
function _onCallWithCallTypeFunction(map, config) {
|
|
2117
2418
|
const { callType, crudType, preAssert = () => undefined, throwOnUnknownModelType } = config;
|
|
2118
|
-
|
|
2419
|
+
const fn = (request) => {
|
|
2119
2420
|
const modelType = request.data?.modelType;
|
|
2120
2421
|
const crudFn = map[modelType];
|
|
2121
2422
|
if (crudFn) {
|
|
@@ -2132,6 +2433,12 @@ function _onCallWithCallTypeFunction(map, config) {
|
|
|
2132
2433
|
throw throwOnUnknownModelType(modelType);
|
|
2133
2434
|
}
|
|
2134
2435
|
};
|
|
2436
|
+
// Aggregate _apiDetails from model type handlers in the map
|
|
2437
|
+
const crudModelApiDetails = aggregateCrudModelApiDetails(map);
|
|
2438
|
+
if (crudModelApiDetails != null) {
|
|
2439
|
+
fn._apiDetails = crudModelApiDetails;
|
|
2440
|
+
}
|
|
2441
|
+
return fn;
|
|
2135
2442
|
}
|
|
2136
2443
|
|
|
2137
2444
|
function onCallCreateModel(map, config = {}) {
|
|
@@ -2817,5 +3124,5 @@ class AbstractFirebaseNestContext extends AbstractNestContext {
|
|
|
2817
3124
|
}
|
|
2818
3125
|
}
|
|
2819
3126
|
|
|
2820
|
-
export { ALREADY_EXISTS_ERROR_CODE, AbstractFirebaseNestContext, AbstractFirebaseServerActionsContext, AbstractFirebaseServerAuthContext, AbstractFirebaseServerAuthService, AbstractFirebaseServerAuthUserContext, AbstractFirebaseServerNewUserService, AbstractNestContext, BAD_REQUEST_ERROR_CODE, CONFLICT_ERROR_CODE, ConfigureFirebaseAppCheckMiddlewareModule, ConfigureFirebaseWebhookMiddlewareModule, DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR, DEFAULT_SETUP_COM_THROTTLE_TIME, DefaultFirebaseServerEnvService, FIREBASE_APP_TOKEN, FIREBASE_AUTH_TOKEN, FIREBASE_FIRESTORE_CONTEXT_TOKEN, FIREBASE_FIRESTORE_TOKEN, FIREBASE_SERVER_VALIDATION_ERROR_CODE, FIREBASE_STORAGE_CONTEXT_FACTORY_CONFIG_TOKEN, FIREBASE_STORAGE_CONTEXT_TOKEN, FIREBASE_STORAGE_TOKEN, FIRESTORE_CLIENT_QUERY_CONSTRAINT_HANDLER_MAPPING, FORBIDDEN_ERROR_CODE, FirebaseAppCheckMiddleware, FirebaseNestServerRootModule, FirebaseRawBodyMiddleware, FirebaseServerAuthModule, FirebaseServerAuthNewUserSendSetupDetailsNoSetupConfigError, FirebaseServerAuthNewUserSendSetupDetailsSendOnceError, FirebaseServerAuthNewUserSendSetupDetailsThrottleError, FirebaseServerAuthService, FirebaseServerEnvService, FirebaseServerFirestoreContextModule, FirebaseServerFirestoreModule, FirebaseServerStorageContextModule, FirebaseServerStorageModule, FirebaseServerStorageService, INTERNAL_SERVER_ERROR_CODE, MODEL_NOT_AVAILABLE_ERROR_CODE, NOT_FOUND_ERROR_CODE, NO_RUN_NAME_SPECIFIED_FOR_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_CODE, NoSetupContentFirebaseServerNewUserService, PERMISSION_DENIED_ERROR_CODE, PHONE_NUMBER_ALREADY_EXISTS_ERROR_CODE, SkipAppCheck, UNAUTHENTICATED_ERROR_CODE, UNAVAILABLE_ERROR_CODE, UNAVAILABLE_OR_DEACTIVATED_FUNCTION_ERROR_CODE, UNKNOWN_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_NAME_CODE, UNKNOWN_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_TYPE_CODE, _onCallWithCallTypeFunction, alreadyExistsError, appFirestoreModuleMetadata, assertContextHasAuth, assertDocumentExists, assertHasRolesInRequest, assertHasSignedTosInRequest, assertIsAdminInRequest, assertIsAdminOrTargetUserInRequestData, assertIsContextWithAuthData, assertRequestRequiresAuthForFunction, assertSnapshotData, assertSnapshotDataWithKey, badRequestError, blockingFunctionHandlerWithNestContextFactory, cloudEventHandlerWithNestContextFactory, collectionRefForPath, createModelUnknownModelTypeError, defaultFirebaseServerActionsTransformFactoryLogErrorFunction, defaultProvideFirebaseServerStorageServiceSimple, deleteModelUnknownModelTypeError, developmentUnknownSpecifierError, docRefForPath, documentModelNotAvailableError, firebaseAuthTokenFromDecodedIdToken, firebaseServerActionsContext, firebaseServerActionsTransformContext, firebaseServerActionsTransformFactory, firebaseServerAppTokenProvider, firebaseServerAuthModuleMetadata, firebaseServerDevFunctions, firebaseServerErrorInfo, firebaseServerErrorInfoCodePair, firebaseServerErrorInfoServerErrorCodePair, firebaseServerErrorInfoServerErrorPair, firebaseServerStorageDefaultBucketIdTokenProvider, firebaseServerStorageModuleMetadata, firebaseServerValidationError, firebaseServerValidationServerError, firestoreClientQueryConstraintFunctionsDriver, firestoreEncryptedField, firestoreServerIncrementUpdateToUpdateData, forbiddenError, getAuthUserOrUndefined, googleCloudFileMetadataToStorageMetadata, googleCloudFirebaseStorageContextFactory, googleCloudFirebaseStorageDrivers, googleCloudFirestoreAccessorDriver, googleCloudFirestoreContextFactory, googleCloudFirestoreDrivers, googleCloudFirestoreQueryDriver, googleCloudStorageAccessorFile, googleCloudStorageAccessorFolder, googleCloudStorageBucketForStorageFilePath, googleCloudStorageFileForStorageFilePath, googleCloudStorageFirebaseStorageAccessorDriver, googleCloudStorageFromFirebaseAdminStorage, googleCloudStorageListFilesResultFactory, handleFirebaseAuthError, handleFirebaseError, hasAuthRolesInRequest, hasNewUserSetupPasswordInRequest, hasSignedTosInRequest, inAuthContext, injectNestApplicationContextIntoRequest, injectNestIntoRequest, internalServerError, isAdminInRequest, isAdminOrTargetUserInRequestData, isContextWithAuthData, isFirebaseError, isFirebaseHttpsError, makeBlockingFunctionWithHandler, makeOnScheduleHandlerWithNestApplicationRequest, makeScheduledFunctionDevelopmentFunction, modelNotAvailableError, nestAppHasDevelopmentSchedulerEnabled, nestAppIsProductionEnvironment, nestFirebaseDoesNotExistError, nestFirebaseForbiddenPermissionError, nestServerInstance, noRunNameSpecifiedForScheduledFunctionDevelopmentFunction, notFoundError, onCallCreateModel, onCallDeleteModel, onCallDevelopmentFunction, onCallHandlerWithNestApplicationFactory, onCallHandlerWithNestContextFactory, onCallModel, onCallModelMissingCallTypeError, onCallModelUnknownCallTypeError, onCallReadModel, onCallSpecifierHandler, onCallUpdateModel, onScheduleHandlerWithNestApplicationFactory, onScheduleHandlerWithNestContextFactory, optionalAuthContext, optionalFirestoreEncryptedField, permissionDeniedError, phoneNumberAlreadyExistsError, preconditionConflictError, provideAppFirestoreCollections, provideFirebaseServerAuthService, provideFirebaseServerStorageService, readModelUnknownModelTypeError, setNestContextOnRequest, setNestContextOnScheduleRequest, taskQueueFunctionHandlerWithNestContextFactory, unauthenticatedContextHasNoAuthData, unauthenticatedContextHasNoUidError, unauthenticatedError, unavailableError, unavailableOrDeactivatedFunctionError, unknownModelCrudFunctionSpecifierError, unknownScheduledFunctionDevelopmentFunctionName, unknownScheduledFunctionDevelopmentFunctionType, updateModelUnknownModelTypeError, userContextFromUid, verifyAppCheckInRequest };
|
|
3127
|
+
export { ALREADY_EXISTS_ERROR_CODE, AbstractFirebaseNestContext, AbstractFirebaseServerActionsContext, AbstractFirebaseServerAuthContext, AbstractFirebaseServerAuthService, AbstractFirebaseServerAuthUserContext, AbstractFirebaseServerNewUserService, AbstractNestContext, BAD_REQUEST_ERROR_CODE, CONFLICT_ERROR_CODE, ConfigureFirebaseAppCheckMiddlewareModule, ConfigureFirebaseWebhookMiddlewareModule, DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR, DEFAULT_SETUP_COM_THROTTLE_TIME, DefaultFirebaseServerEnvService, FIREBASE_APP_TOKEN, FIREBASE_AUTH_TOKEN, FIREBASE_FIRESTORE_CONTEXT_TOKEN, FIREBASE_FIRESTORE_TOKEN, FIREBASE_SERVER_VALIDATION_ERROR_CODE, FIREBASE_STORAGE_CONTEXT_FACTORY_CONFIG_TOKEN, FIREBASE_STORAGE_CONTEXT_TOKEN, FIREBASE_STORAGE_TOKEN, FIRESTORE_CLIENT_QUERY_CONSTRAINT_HANDLER_MAPPING, FORBIDDEN_ERROR_CODE, FirebaseAppCheckMiddleware, FirebaseNestServerRootModule, FirebaseRawBodyMiddleware, FirebaseServerAuthModule, FirebaseServerAuthNewUserSendSetupDetailsNoSetupConfigError, FirebaseServerAuthNewUserSendSetupDetailsSendOnceError, FirebaseServerAuthNewUserSendSetupDetailsThrottleError, FirebaseServerAuthService, FirebaseServerEnvService, FirebaseServerFirestoreContextModule, FirebaseServerFirestoreModule, FirebaseServerStorageContextModule, FirebaseServerStorageModule, FirebaseServerStorageService, INTERNAL_SERVER_ERROR_CODE, MODEL_NOT_AVAILABLE_ERROR_CODE, NOT_FOUND_ERROR_CODE, NO_RUN_NAME_SPECIFIED_FOR_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_CODE, NoSetupContentFirebaseServerNewUserService, PERMISSION_DENIED_ERROR_CODE, PHONE_NUMBER_ALREADY_EXISTS_ERROR_CODE, SkipAppCheck, UNAUTHENTICATED_ERROR_CODE, UNAVAILABLE_ERROR_CODE, UNAVAILABLE_OR_DEACTIVATED_FUNCTION_ERROR_CODE, UNKNOWN_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_NAME_CODE, UNKNOWN_SCHEDULED_FUNCTION_DEVELOPMENT_FUNCTION_TYPE_CODE, _onCallWithCallTypeFunction, aggregateCrudModelApiDetails, aggregateModelApiDetails, aggregateSpecifierApiDetails, alreadyExistsError, appFirestoreModuleMetadata, assertContextHasAuth, assertDocumentExists, assertHasRolesInRequest, assertHasSignedTosInRequest, assertIsAdminInRequest, assertIsAdminOrTargetUserInRequestData, assertIsContextWithAuthData, assertRequestRequiresAuthForFunction, assertSnapshotData, assertSnapshotDataWithKey, badRequestError, blockingFunctionHandlerWithNestContextFactory, cloudEventHandlerWithNestContextFactory, collectionRefForPath, createModelUnknownModelTypeError, defaultFirebaseServerActionsTransformFactoryLogErrorFunction, defaultProvideFirebaseServerStorageServiceSimple, deleteModelUnknownModelTypeError, developmentUnknownSpecifierError, docRefForPath, documentModelNotAvailableError, firebaseAuthTokenFromDecodedIdToken, firebaseServerActionsContext, firebaseServerActionsTransformContext, firebaseServerActionsTransformFactory, firebaseServerAppTokenProvider, firebaseServerAuthModuleMetadata, firebaseServerDevFunctions, firebaseServerErrorInfo, firebaseServerErrorInfoCodePair, firebaseServerErrorInfoServerErrorCodePair, firebaseServerErrorInfoServerErrorPair, firebaseServerStorageDefaultBucketIdTokenProvider, firebaseServerStorageModuleMetadata, firebaseServerValidationError, firebaseServerValidationServerError, firestoreClientQueryConstraintFunctionsDriver, firestoreEncryptedField, firestoreServerIncrementUpdateToUpdateData, forbiddenError, getAuthUserOrUndefined, getModelApiDetails, googleCloudFileMetadataToStorageMetadata, googleCloudFirebaseStorageContextFactory, googleCloudFirebaseStorageDrivers, googleCloudFirestoreAccessorDriver, googleCloudFirestoreContextFactory, googleCloudFirestoreDrivers, googleCloudFirestoreQueryDriver, googleCloudStorageAccessorFile, googleCloudStorageAccessorFolder, googleCloudStorageBucketForStorageFilePath, googleCloudStorageFileForStorageFilePath, googleCloudStorageFirebaseStorageAccessorDriver, googleCloudStorageFromFirebaseAdminStorage, googleCloudStorageListFilesResultFactory, handleFirebaseAuthError, handleFirebaseError, hasAuthRolesInRequest, hasNewUserSetupPasswordInRequest, hasSignedTosInRequest, inAuthContext, injectNestApplicationContextIntoRequest, injectNestIntoRequest, internalServerError, isAdminInRequest, isAdminOrTargetUserInRequestData, isContextWithAuthData, isFirebaseError, isFirebaseHttpsError, isOnCallCrudModelApiDetails, isOnCallHandlerApiDetails, isOnCallSpecifierApiDetails, makeBlockingFunctionWithHandler, makeOnScheduleHandlerWithNestApplicationRequest, makeScheduledFunctionDevelopmentFunction, modelNotAvailableError, nestAppHasDevelopmentSchedulerEnabled, nestAppIsProductionEnvironment, nestFirebaseDoesNotExistError, nestFirebaseForbiddenPermissionError, nestServerInstance, noRunNameSpecifiedForScheduledFunctionDevelopmentFunction, notFoundError, onCallCreateModel, onCallDeleteModel, onCallDevelopmentFunction, onCallHandlerWithNestApplicationFactory, onCallHandlerWithNestContextFactory, onCallModel, onCallModelMissingCallTypeError, onCallModelUnknownCallTypeError, onCallReadModel, onCallSpecifierHandler, onCallUpdateModel, onScheduleHandlerWithNestApplicationFactory, onScheduleHandlerWithNestContextFactory, optionalAuthContext, optionalFirestoreEncryptedField, permissionDeniedError, phoneNumberAlreadyExistsError, preconditionConflictError, provideAppFirestoreCollections, provideFirebaseServerAuthService, provideFirebaseServerStorageService, readApiDetails, readModelUnknownModelTypeError, setNestContextOnRequest, setNestContextOnScheduleRequest, taskQueueFunctionHandlerWithNestContextFactory, unauthenticatedContextHasNoAuthData, unauthenticatedContextHasNoUidError, unauthenticatedError, unavailableError, unavailableOrDeactivatedFunctionError, unknownModelCrudFunctionSpecifierError, unknownScheduledFunctionDevelopmentFunctionName, unknownScheduledFunctionDevelopmentFunctionType, updateModelUnknownModelTypeError, userContextFromUid, verifyAppCheckInRequest, withApiDetails };
|
|
2821
3128
|
//# sourceMappingURL=index.esm.js.map
|