Skip to content

Commit

Permalink
chore: use correct types for auth and app check
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Jan 31, 2025
1 parent 10bcb29 commit 3514416
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 67 deletions.
2 changes: 1 addition & 1 deletion packages/vertexai/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Top level API', () => {
const app = firebase.app();
const auth = app.auth();
const appCheck = app.appCheck();
// proves our limited internal auth and app check types are correct and allow them to be passed in

getVertexAI(app, undefined, appCheck, auth);
});

Expand Down
11 changes: 4 additions & 7 deletions packages/vertexai/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import './polyfills';
import { getApp, ReactNativeFirebase } from '@react-native-firebase/app';
import { InternalAppCheck, InternalAuth } from './types/internal';
import { ModelParams, RequestOptions, VertexAIErrorCode } from './types';
import { DEFAULT_LOCATION } from './constants';
import { VertexAI, VertexAIOptions } from './public-types';
Expand All @@ -26,15 +25,13 @@ import { GenerativeModel } from './models/generative-model';
import { VertexAIService } from './service';
export { ChatSession } from './methods/chat-session';
export * from './requests/schema-builder';
import { FirebaseAuthTypes } from '@react-native-firebase/auth';
import { FirebaseAppCheckTypes } from '@react-native-firebase/app-check';

export { GenerativeModel };

export { VertexAIError };

// We don't have access to RNFB auth and appCheck so we use only types for what we need (i.e get tokens)
type AppCheck = InternalAppCheck;
type Auth = InternalAuth;

/**
* Returns a <code>{@link VertexAI}</code> instance for the given app.
*
Expand All @@ -48,8 +45,8 @@ type Auth = InternalAuth;
export function getVertexAI(
app: ReactNativeFirebase.FirebaseApp = getApp(),
options?: VertexAIOptions,
appCheck?: AppCheck,
auth?: Auth,
appCheck?: FirebaseAppCheckTypes.Module,
auth?: FirebaseAuthTypes.Module,
): VertexAI {
return {
app,
Expand Down
11 changes: 6 additions & 5 deletions packages/vertexai/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
import { ReactNativeFirebase } from '@react-native-firebase/app';
import { VertexAI, VertexAIOptions } from './public-types';
import { DEFAULT_LOCATION } from './constants';
import { InternalAppCheck, InternalAuth } from './types/internal';
import { FirebaseAuthTypes } from '@react-native-firebase/auth';
import { FirebaseAppCheckTypes } from '@react-native-firebase/app-check';

export class VertexAIService implements VertexAI {
auth: InternalAuth | null;
appCheck: InternalAppCheck | null;
auth: FirebaseAuthTypes.Module | null;
appCheck: FirebaseAppCheckTypes.Module | null;
location: string;

constructor(
public app: ReactNativeFirebase.FirebaseApp,
auth?: InternalAuth,
appCheck?: InternalAppCheck,
auth?: FirebaseAuthTypes.Module,
appCheck?: FirebaseAppCheckTypes.Module,
public options?: VertexAIOptions,
) {
this.auth = auth || null;
Expand Down
54 changes: 2 additions & 52 deletions packages/vertexai/lib/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,12 @@
* limitations under the License.
*
*/
import { FirebaseAppCheckTypes } from '@react-native-firebase/app-check';

export interface ApiSettings {
apiKey: string;
project: string;
location: string;
getAuthToken?: () => Promise<string>;
getAppCheckToken?: () => Promise<AppCheckTokenResult>;
}

export interface InternalAppCheck {
/**
* Requests Firebase App Check token.
* This method should only be used if you need to authorize requests to a non-Firebase backend.
* Requests to Firebase backend are authorized automatically if configured.
*
* @param forceRefresh - If true, a new Firebase App Check token is requested and the token cache is ignored.
* If false, the cached token is used if it exists and has not expired yet.
* In most cases, false should be used. True should only be used if the server explicitly returns an error, indicating a revoked token.
*/
getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult>;
}

interface AppCheckTokenResult {
/**
* The token string in JWT format.
*/
readonly token: string;
}

export interface InternalAuth {
/**
* Returns the currently signed-in user (or null if no user signed in). See the User interface documentation for detailed usage.
*
* #### Example
*
* ```js
* const user = firebase.auth().currentUser;
* ```
*
* > It is recommended to use {@link auth#onAuthStateChanged} to track whether the user is currently signed in.
*/
currentUser: User | null;
}

export interface User {
/**
* Returns the users authentication token.
*
* #### Example
*
* ```js
* // Force a token refresh
* const idToken = await firebase.auth().currentUser.getIdToken(true);
* ```
*
* @param forceRefresh A boolean value which forces Firebase to refresh the token.
*/
getIdToken(forceRefresh?: boolean): Promise<string>;
getAppCheckToken?: () => Promise<FirebaseAppCheckTypes.AppCheckTokenResult>;
}
4 changes: 3 additions & 1 deletion packages/vertexai/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"strict": true,
"baseUrl": ".",
"paths": {
"@react-native-firebase/app": ["../app/lib"]
"@react-native-firebase/app": ["../app/lib"],
"@react-native-firebase/auth": ["../auth/lib"],
"@react-native-firebase/app-check": ["../app-check/lib"],
}
}
}
4 changes: 3 additions & 1 deletion tsconfig-jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@react-native-firebase/app": ["packages/app/lib"]
"@react-native-firebase/app": ["packages/app/lib"],
"@react-native-firebase/auth": ["packages/auth/lib"],
"@react-native-firebase/app-check": ["packages/app-check/lib"],
}
}
}

0 comments on commit 3514416

Please sign in to comment.