Skip to content

Commit cd7e85a

Browse files
committed
Replace interface for individual args
1 parent b617205 commit cd7e85a

2 files changed

Lines changed: 30 additions & 48 deletions

File tree

src/deploy/functions/prepare.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as events from "../../functions/events/v1";
1616
import { getDatabase } from "./services/firestore";
1717
import { getBucket } from "./services/storage";
1818
import { getDatabaseInstanceDetails } from "./services/database";
19-
import { isGlobalAILogicEndpoint } from "./services/ailogic";
19+
import { isGlobalAILogicTrigger } from "./services/ailogic";
2020
import { parseServiceName, parseConnectorName } from "../../dataconnect/names";
2121
import {
2222
functionsOrigin,
@@ -66,22 +66,6 @@ import * as prompt from "../../prompt";
6666
export const EVENTARC_SOURCE_ENV = "EVENTARC_CLOUD_EVENT_SOURCE";
6767
export const DEFAULT_FUNCTION_REGION = "us-central1";
6868

69-
/**
70-
* A subset of the backend.Endpoint interface for the purposes of resolving regions.
71-
*/
72-
interface TriggerResolutionTarget {
73-
project: string;
74-
eventTrigger?: {
75-
eventType: string;
76-
eventFilters?: Record<string, string | build.Expression<string>>;
77-
region?: string | build.Expression<string>;
78-
};
79-
blockingTrigger?: {
80-
eventType: string;
81-
options?: Record<string, unknown>;
82-
};
83-
}
84-
8569
/**
8670
* Prepare functions codebases for deploy.
8771
*/
@@ -398,9 +382,12 @@ export async function resolveDefaultRegionsForBuild(
398382
// Match triggers.
399383
try {
400384
if (build.isBlockingTriggered(endpoint)) {
401-
resolvedRegion = resolveRegionForBlockingTrigger(endpoint);
385+
resolvedRegion = resolveRegionForBlockingTrigger(endpoint.blockingTrigger);
402386
} else if (build.isEventTriggered(endpoint)) {
403-
resolvedRegion = await resolveRegionForEventTrigger(endpoint);
387+
resolvedRegion = await resolveRegionForEventTrigger(
388+
endpoint.project,
389+
endpoint.eventTrigger,
390+
);
404391
}
405392
} catch (err: any) {
406393
logger.debug(
@@ -415,27 +402,23 @@ export async function resolveDefaultRegionsForBuild(
415402
}
416403
}
417404

418-
function resolveRegionForBlockingTrigger(endpoint: TriggerResolutionTarget): string {
419-
if (!endpoint.blockingTrigger) {
420-
return DEFAULT_FUNCTION_REGION;
421-
}
422-
const eventType = endpoint.blockingTrigger.eventType;
405+
function resolveRegionForBlockingTrigger(blockingTrigger: build.BlockingTrigger): string {
406+
const eventType = blockingTrigger.eventType;
423407
if ((events.AUTH_BLOCKING_EVENTS as readonly string[]).includes(eventType)) {
424408
return "us-east1";
425409
}
426410

427-
if (isGlobalAILogicEndpoint(endpoint)) {
411+
if (isGlobalAILogicTrigger(blockingTrigger)) {
428412
return "us-east1";
429413
}
430414

431415
return DEFAULT_FUNCTION_REGION;
432416
}
433417

434-
async function resolveRegionForEventTrigger(endpoint: TriggerResolutionTarget): Promise<string> {
435-
if (!endpoint.eventTrigger) {
436-
return DEFAULT_FUNCTION_REGION;
437-
}
438-
const eventTrigger = endpoint.eventTrigger;
418+
async function resolveRegionForEventTrigger(
419+
project: string,
420+
eventTrigger: build.EventTrigger,
421+
): Promise<string> {
439422
const eventType = eventTrigger.eventType;
440423

441424
// Global functions should be deployed to us-east1.
@@ -458,7 +441,7 @@ async function resolveRegionForEventTrigger(endpoint: TriggerResolutionTarget):
458441
if (eventType.startsWith("google.cloud.firestore.")) {
459442
try {
460443
const databaseId = eventTrigger.eventFilters?.database || "(default)";
461-
const db = await getDatabase(endpoint.project, databaseId);
444+
const db = await getDatabase(project, databaseId);
462445
const locationId = db.locationId.toLowerCase();
463446

464447
if (locationId === "nam5" || locationId === "nam7") return "us-central1";
@@ -498,7 +481,7 @@ async function resolveRegionForEventTrigger(endpoint: TriggerResolutionTarget):
498481
try {
499482
const instanceName = eventTrigger.eventFilters?.instance;
500483
if (instanceName) {
501-
const details = await getDatabaseInstanceDetails(endpoint.project, instanceName);
484+
const details = await getDatabaseInstanceDetails(project, instanceName);
502485
if (details.location && details.location !== "-") {
503486
return details.location.toLowerCase();
504487
}

src/deploy/functions/services/ailogic.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as backend from "../backend";
2+
import * as build from "../build";
23
import { FirebaseError, getErrStatus } from "../../../error";
34
import { Name, Service } from "./index";
45
import * as ailogicApi from "../../../gcp/ailogic";
@@ -15,15 +16,7 @@ export const AI_LOGIC_EVENTS = [
1516
AI_LOGIC_AFTER_GENERATE_CONTENT,
1617
] as const;
1718

18-
export interface AILogicTriggerTarget {
19-
blockingTrigger?: {
20-
eventType: string;
21-
options?: Record<string, unknown>;
22-
};
23-
project?: string;
24-
}
25-
26-
export type AILogicTrigger = {
19+
export type AILogicEndpoint = backend.Endpoint & {
2720
blockingTrigger: {
2821
eventType: (typeof AI_LOGIC_EVENTS)[number];
2922
options?: {
@@ -32,26 +25,32 @@ export type AILogicTrigger = {
3225
};
3326
};
3427

35-
export type AILogicEndpoint = backend.Endpoint & AILogicTrigger;
36-
3728
/**
3829
* Type guard to check if a trigger target is an AI Logic event trigger.
3930
*/
40-
export function isAILogicEvent<T extends AILogicTriggerTarget>(
41-
endpoint: T,
42-
): endpoint is T & AILogicTrigger {
43-
if (!endpoint.blockingTrigger) {
31+
export function isAILogicEvent(endpoint: backend.Endpoint): endpoint is AILogicEndpoint {
32+
if (!backend.isBlockingTriggered(endpoint)) {
4433
return false;
4534
}
4635
return AI_LOGIC_EVENTS.includes(
4736
endpoint.blockingTrigger.eventType as (typeof AI_LOGIC_EVENTS)[number],
4837
);
4938
}
5039

40+
/**
41+
* Check if a blocking trigger is a global AI Logic trigger (not a regional webhook).
42+
*/
43+
export function isGlobalAILogicTrigger(blockingTrigger: build.BlockingTrigger): boolean {
44+
return (
45+
AI_LOGIC_EVENTS.includes(blockingTrigger.eventType as any) &&
46+
!blockingTrigger.options?.regionalWebhook
47+
);
48+
}
49+
5150
/**
5251
* Check if an AI Logic trigger target is global (i.e. not a regional webhook).
5352
*/
54-
export function isGlobalAILogicEndpoint(endpoint: AILogicTriggerTarget): boolean {
53+
export function isGlobalAILogicEndpoint(endpoint: backend.Endpoint): boolean {
5554
if (!isAILogicEvent(endpoint)) {
5655
return false;
5756
}

0 commit comments

Comments
 (0)