Skip to content

Commit

Permalink
Throw in fetch in non browser environments
Browse files Browse the repository at this point in the history
  • Loading branch information
DellaBitta committed Nov 18, 2024
1 parent e1913e3 commit ae3ccfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/remote-config/src/client/rest_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<FetchResponse> {
if (!isBrowser()) {
throw ERROR_FACTORY.create(ErrorCode.REQUIRES_BROWSER_ENVIRONMENT);
}
const [installationId, installationToken] = await Promise.all([
this.firebaseInstallations.getId(),
this.firebaseInstallations.getToken()
Expand Down
7 changes: 5 additions & 2 deletions packages/remote-config/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae3ccfd

Please sign in to comment.