From 461639661dc3a55592cea65ce958683b84cf96ae Mon Sep 17 00:00:00 2001 From: Russell Wheatley Date: Mon, 29 Jul 2024 13:49:24 +0100 Subject: [PATCH] test(app-check): ensure modular API are exported properly (#7932) --- packages/app-check/__tests__/appcheck.test.ts | 31 ++++++++- packages/app-check/lib/index.d.ts | 2 + packages/app-check/lib/index.js | 10 +-- packages/app-check/lib/modular/index.d.ts | 63 +++++++++++++++++++ packages/app-check/lib/modular/index.js | 31 +++++---- 5 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 packages/app-check/lib/modular/index.d.ts diff --git a/packages/app-check/__tests__/appcheck.test.ts b/packages/app-check/__tests__/appcheck.test.ts index c0ae5110f4..aba3e65fd4 100644 --- a/packages/app-check/__tests__/appcheck.test.ts +++ b/packages/app-check/__tests__/appcheck.test.ts @@ -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 () { @@ -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(); + }); + }); }); diff --git a/packages/app-check/lib/index.d.ts b/packages/app-check/lib/index.d.ts index f83908c9fb..7af987bfad 100644 --- a/packages/app-check/lib/index.d.ts +++ b/packages/app-check/lib/index.d.ts @@ -341,6 +341,8 @@ export const firebase: ReactNativeFirebase.Module & { export default defaultExport; +export * from './modular'; + /** * Attach namespace to `firebase.` and `FirebaseApp.`. */ diff --git a/packages/app-check/lib/index.js b/packages/app-check/lib/index.js index 8e8f8339f2..3eb79e899f 100644 --- a/packages/app-check/lib/index.js +++ b/packages/app-check/lib/index.js @@ -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'; @@ -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(...); diff --git a/packages/app-check/lib/modular/index.d.ts b/packages/app-check/lib/modular/index.d.ts new file mode 100644 index 0000000000..516da2f211 --- /dev/null +++ b/packages/app-check/lib/modular/index.d.ts @@ -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} + */ +export function getToken( + appCheckInstance: AppCheck, + forceRefresh: boolean, +): Promise; + +/** + * 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} + */ +export function getLimitedUseToken(appCheckInstance: AppCheck): Promise; + +/** + * 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 + * @returns {Unsubscribe} + */ +export function addTokenListener( + appCheckInstance: AppCheck, + listener: PartialObserver, +): 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; diff --git a/packages/app-check/lib/modular/index.js b/packages/app-check/lib/modular/index.js index 928c77f2fa..d882204e4d 100644 --- a/packages/app-check/lib/modular/index.js +++ b/packages/app-check/lib/modular/index.js @@ -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) { @@ -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} */ export function getToken(appCheckInstance, forceRefresh) { @@ -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} */ export function getLimitedUseToken(appCheckInstance) { @@ -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 + * @param {AppCheck} appCheckInstance - The App Check instance. + * @param {PartialObserver} listener - The listener to register. * @returns {Unsubscribe} */ export function addTokenListener(appCheckInstance, listener) { @@ -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);