|
| 1 | +import * as _ from "lodash"; |
| 2 | + |
| 3 | +import * as api from "./api"; |
| 4 | +import * as utils from "./utils"; |
| 5 | +import { FirebaseError } from "./error"; |
| 6 | + |
| 7 | +const POLL_INTERVAL = 10000; // 10 seconds |
| 8 | +const POLLS_BEFORE_RETRY = 12; // Retry enabling the API after 2 minutes |
| 9 | + |
| 10 | +export async function check( |
| 11 | + projectId: string, |
| 12 | + apiName: string, |
| 13 | + prefix: string, |
| 14 | + silent: boolean = false |
| 15 | +): Promise<boolean> { |
| 16 | + const response = await api.request("GET", `/v1/projects/${projectId}/services/${apiName}`, { |
| 17 | + auth: true, |
| 18 | + origin: api.serviceUsageOrigin, |
| 19 | + }); |
| 20 | + |
| 21 | + const isEnabled = _.get(response.body, "state") === "ENABLED"; |
| 22 | + if (isEnabled && !silent) { |
| 23 | + utils.logLabeledSuccess(prefix, "all necessary APIs are enabled"); |
| 24 | + } |
| 25 | + return isEnabled; |
| 26 | +} |
| 27 | + |
| 28 | +export async function enable(projectId: string, apiName: string): Promise<void> { |
| 29 | + return api.request("POST", `/v1/projects/${projectId}/services/${apiName}:enable`, { |
| 30 | + auth: true, |
| 31 | + origin: api.serviceUsageOrigin, |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +export async function ensure( |
| 36 | + projectId: string, |
| 37 | + apiName: string, |
| 38 | + prefix: string, |
| 39 | + silent: boolean = false |
| 40 | +): Promise<void> { |
| 41 | + if (!silent) { |
| 42 | + utils.logLabeledBullet(prefix, "ensuring necessary APIs are enabled..."); |
| 43 | + } |
| 44 | + const isEnabled = await check(projectId, apiName, prefix, silent); |
| 45 | + if (isEnabled) { |
| 46 | + return; |
| 47 | + } |
| 48 | + if (!silent) { |
| 49 | + utils.logLabeledWarning(prefix, "missing necessary APIs. Enabling now..."); |
| 50 | + } |
| 51 | + return enableApiWithRetries(projectId, apiName, prefix, silent); |
| 52 | +} |
| 53 | + |
| 54 | +async function pollCheckEnabled( |
| 55 | + projectId: string, |
| 56 | + apiName: string, |
| 57 | + prefix: string, |
| 58 | + silent: boolean, |
| 59 | + enablementRetries: number, |
| 60 | + pollRetries: number = 0 |
| 61 | +): Promise<void> { |
| 62 | + if (pollRetries > POLLS_BEFORE_RETRY) { |
| 63 | + return enableApiWithRetries(projectId, apiName, prefix, silent, enablementRetries + 1); |
| 64 | + } |
| 65 | + |
| 66 | + await new Promise((resolve) => { |
| 67 | + setTimeout(resolve, POLL_INTERVAL); |
| 68 | + }); |
| 69 | + const isEnabled = await check(projectId, apiName, prefix, silent); |
| 70 | + if (isEnabled) { |
| 71 | + return; |
| 72 | + } |
| 73 | + if (!silent) { |
| 74 | + utils.logLabeledBullet(prefix, "waiting for APIs to activate..."); |
| 75 | + } |
| 76 | + return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries, pollRetries + 1); |
| 77 | +} |
| 78 | + |
| 79 | +async function enableApiWithRetries( |
| 80 | + projectId: string, |
| 81 | + apiName: string, |
| 82 | + prefix: string, |
| 83 | + silent: boolean, |
| 84 | + enablementRetries = 0 |
| 85 | +): Promise<void> { |
| 86 | + if (enablementRetries > 1) { |
| 87 | + throw new FirebaseError( |
| 88 | + "Timed out while waiting for APIs to enable. Please try again in a few minutes." |
| 89 | + ); |
| 90 | + } |
| 91 | + await enable(projectId, apiName); |
| 92 | + return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries); |
| 93 | +} |
0 commit comments