Skip to content

Commit

Permalink
test(app-check): ensure modular API are exported properly (#7932)
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley authored Jul 29, 2024
1 parent f5a26ee commit 4616396
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 20 deletions.
31 changes: 30 additions & 1 deletion packages/app-check/__tests__/appcheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { describe, expect, it } from '@jest/globals';

import { firebase } from '../lib';
import {
firebase,
initializeAppCheck,
getToken,
getLimitedUseToken,
addTokenListener,
setTokenAutoRefreshEnabled,
} from '../lib';

describe('appCheck()', function () {
describe('namespace', function () {
Expand All @@ -26,4 +33,26 @@ describe('appCheck()', function () {
expect(firebase.appCheck().newReactNativeFirebaseAppCheckProvider()).toBeDefined();
});
});

describe('modular', function () {
it('`initializeAppCheck` function is properly exposed to end user', function () {
expect(initializeAppCheck).toBeDefined();
});

it('`getToken` function is properly exposed to end user', function () {
expect(getToken).toBeDefined();
});

it('`getLimitedUseToken` function is properly exposed to end user', function () {
expect(getLimitedUseToken).toBeDefined();
});

it('`addTokenListener` function is properly exposed to end user', function () {
expect(addTokenListener).toBeDefined();
});

it('`setTokenAutoRefreshEnabled` function is properly exposed to end user', function () {
expect(setTokenAutoRefreshEnabled).toBeDefined();
});
});
});
2 changes: 2 additions & 0 deletions packages/app-check/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ export const firebase: ReactNativeFirebase.Module & {

export default defaultExport;

export * from './modular';

/**
* Attach namespace to `firebase.` and `FirebaseApp.`.
*/
Expand Down
10 changes: 2 additions & 8 deletions packages/app-check/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ import fallBackModule from './web/RNFBAppCheckModule';

import version from './version';

export {
addTokenListener,
getToken,
getLimitedUseToken,
initializeAppCheck,
setTokenAutoRefreshEnabled,
} from './modular/index';

const namespace = 'appCheck';

const nativeModuleName = 'RNFBAppCheckModule';
Expand Down Expand Up @@ -240,6 +232,8 @@ export default createModuleNamespace({
ModuleClass: FirebaseAppCheckModule,
});

export * from './modular';

// import appCheck, { firebase } from '@react-native-firebase/app-check';
// appCheck().X(...);
// firebase.appCheck().X(...);
Expand Down
63 changes: 63 additions & 0 deletions packages/app-check/lib/modular/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseAppCheckTypes } from '..';

import AppCheck = FirebaseAppCheckTypes.Module;
import AppCheckOptions = FirebaseAppCheckTypes.AppCheckOptions;
import AppCheckTokenResult = FirebaseAppCheckTypes.AppCheckTokenResult;
import PartialObserver = FirebaseAppCheckTypes.PartialObserver;
import Unsubscribe = FirebaseAppCheckTypes.Unsubscribe;

/**
* Activate App Check for the given app. Can be called only once per app.
* @param app - FirebaseApp. Optional.
* @param options - AppCheckOptions
* @returns {Promise<{ app: FirebaseApp }>}
*/
export function initializeAppCheck(
app?: FirebaseApp,
options?: AppCheckOptions,
): Promise<{ app: FirebaseApp }>;

/**
* Get the current App Check token. Attaches to the most recent in-flight request if one is present.
* Returns null if no token is present and no token requests are in-flight.
* @param appCheckInstance - AppCheck
* @param forceRefresh - boolean
* @returns {Promise<AppCheckTokenResult>}
*/
export function getToken(
appCheckInstance: AppCheck,
forceRefresh: boolean,
): Promise<AppCheckTokenResult>;

/**
* Get a limited-use (consumable) App Check token.
* For use with server calls to firebase functions or custom backends using the firebase admin SDK
* @param appCheckInstance - AppCheck
* @returns {Promise<AppCheckTokenResult>}
*/
export function getLimitedUseToken(appCheckInstance: AppCheck): Promise<AppCheckTokenResult>;

/**
* Registers a listener to changes in the token state.
* There can be more than one listener registered at the same time for one or more App Check instances.
* The listeners call back on the UI thread whenever the current
* token associated with this App Check instance changes.
* @param appCheckInstance - AppCheck
* @param listener - PartialObserver<AppCheckTokenResult>
* @returns {Unsubscribe}
*/
export function addTokenListener(
appCheckInstance: AppCheck,
listener: PartialObserver<AppCheckTokenResult>,
): Unsubscribe;

/**
* Set whether App Check will automatically refresh tokens as needed.
* @param appCheckInstance - AppCheck
* @param isAutoRefreshEnabled - boolean
*/
export function setTokenAutoRefreshEnabled(
appCheckInstance: AppCheck,
isAutoRefreshEnabled: boolean,
): void;
31 changes: 20 additions & 11 deletions packages/app-check/lib/modular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@

import { firebase } from '..';

/**
* @typedef {import('@firebase/app').FirebaseApp} FirebaseApp
* @typedef {import('..').FirebaseAppCheckTypes.Module} AppCheck
* @typedef {import('..').FirebaseAppCheckTypes.AppCheckTokenResult} AppCheckTokenResult
* @typedef {import('..').FirebaseAppCheckTypes.Unsubscribe} Unsubscribe
* @typedef {import('..').FirebaseAppCheckTypes.PartialObserver} PartialObserver
* @typedef {import('..').FirebaseAppCheckTypes.AppCheckOptions} AppCheckOptions
*/

/**
* Activate App Check for the given app. Can be called only once per app.
* @param app - FirebaseApp. Optional.
* @param options - AppCheckOptions
* @returns {AppCheck}
* @param {FirebaseApp} [app] - The app to initialize App Check for. Optional.
* @param {AppCheckOptions} options - App Check options.
* @returns {Promise<{ app: FirebaseApp }>}
*/
export async function initializeAppCheck(app, options) {
if (app) {
Expand All @@ -37,8 +46,8 @@ export async function initializeAppCheck(app, options) {
/**
* Get the current App Check token. Attaches to the most recent in-flight request if one is present.
* Returns null if no token is present and no token requests are in-flight.
* @param appCheckInstance - AppCheck
* @param forceRefresh - boolean
* @param {AppCheck} appCheckInstance - The App Check instance.
* @param {boolean} forceRefresh - Whether to force refresh the token.
* @returns {Promise<AppCheckTokenResult>}
*/
export function getToken(appCheckInstance, forceRefresh) {
Expand All @@ -47,8 +56,8 @@ export function getToken(appCheckInstance, forceRefresh) {

/**
* Get a limited-use (consumable) App Check token.
* For use with server calls to firebase functions or custom backends using the firebase admin SDK
* @param appCheckInstance - AppCheck
* For use with server calls to firebase functions or custom backends using the firebase admin SDK.
* @param {AppCheck} appCheckInstance - The App Check instance.
* @returns {Promise<AppCheckTokenResult>}
*/
export function getLimitedUseToken(appCheckInstance) {
Expand All @@ -60,8 +69,8 @@ export function getLimitedUseToken(appCheckInstance) {
* There can be more than one listener registered at the same time for one or more App Check instances.
* The listeners call back on the UI thread whenever the current
* token associated with this App Check instance changes.
* @param appCheckInstance - AppCheck
* @param listener - PartialObserver<AppCheckTokenResult>
* @param {AppCheck} appCheckInstance - The App Check instance.
* @param {PartialObserver<AppCheckTokenResult>} listener - The listener to register.
* @returns {Unsubscribe}
*/
export function addTokenListener(appCheckInstance, listener) {
Expand All @@ -72,8 +81,8 @@ export function addTokenListener(appCheckInstance, listener) {

/**
* Set whether App Check will automatically refresh tokens as needed.
* @param appCheckInstance - AppCheck
* @param isAutoRefreshEnabled - boolean
* @param {AppCheck} appCheckInstance - The App Check instance.
* @param {boolean} isAutoRefreshEnabled - Whether to enable auto-refresh.
*/
export function setTokenAutoRefreshEnabled(appCheckInstance, isAutoRefreshEnabled) {
return appCheckInstance.app.appCheck().setTokenAutoRefreshEnabled(isAutoRefreshEnabled);
Expand Down

0 comments on commit 4616396

Please sign in to comment.