From ae3ccfdc03b3f09979447de6b014766b7649234d Mon Sep 17 00:00:00 2001 From: DellaBitta Date: Mon, 18 Nov 2024 11:27:47 -0500 Subject: [PATCH] Throw in fetch in non browser environments --- packages/remote-config/src/client/rest_client.ts | 6 ++++++ packages/remote-config/src/errors.ts | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/remote-config/src/client/rest_client.ts b/packages/remote-config/src/client/rest_client.ts index 87fdae3c3d6..773824e916c 100644 --- a/packages/remote-config/src/client/rest_client.ts +++ b/packages/remote-config/src/client/rest_client.ts @@ -24,6 +24,7 @@ import { import { ERROR_FACTORY, ErrorCode } from '../errors'; import { getUserLanguage } from '../language'; import { _FirebaseInstallationsInternal } from '@firebase/installations'; +import { isBrowser } from '@firebase/util'; /** * Defines request body parameters required to call the fetch API: @@ -65,8 +66,13 @@ export class RestClient implements RemoteConfigFetchClient { * @throws a {@link ErrorCode.FETCH_PARSE} error if {@link Response#json} can't parse the * fetch response. * @throws a {@link ErrorCode.FETCH_STATUS} error if the service returns an HTTP error status. + * @throws a {@link ErrorCode.REQUIRES_BROWSER_ENVIRONMENT} error if the invoked in a non browser + * environment. */ async fetch(request: FetchRequest): Promise { + if (!isBrowser()) { + throw ERROR_FACTORY.create(ErrorCode.REQUIRES_BROWSER_ENVIRONMENT); + } const [installationId, installationToken] = await Promise.all([ this.firebaseInstallations.getId(), this.firebaseInstallations.getToken() diff --git a/packages/remote-config/src/errors.ts b/packages/remote-config/src/errors.ts index eac9a25657b..21f035dc823 100644 --- a/packages/remote-config/src/errors.ts +++ b/packages/remote-config/src/errors.ts @@ -31,7 +31,8 @@ export const enum ErrorCode { FETCH_THROTTLE = 'fetch-throttle', FETCH_PARSE = 'fetch-client-parse', FETCH_STATUS = 'fetch-status', - INDEXED_DB_UNAVAILABLE = 'indexed-db-unavailable' + INDEXED_DB_UNAVAILABLE = 'indexed-db-unavailable', + REQUIRES_BROWSER_ENVIRONMENT = 'requires-browser-environment' } const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = { @@ -67,7 +68,9 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = { [ErrorCode.FETCH_STATUS]: 'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.', [ErrorCode.INDEXED_DB_UNAVAILABLE]: - 'Indexed DB is not supported by current browser' + 'Indexed DB is not supported by current browser', + [ErrorCode.REQUIRES_BROWSER_ENVIRONMENT]: + 'The requested operation must be executed in a browser environment' }; // Note this is effectively a type system binding a code to params. This approach overlaps with the