Skip to content

fix: Updated documentation for app and instance-id entry points #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions etc/firebase-admin.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FirebaseDatabase } from '@firebase/database-types';
import * as _firestore from '@google-cloud/firestore';
import * as rtdb from '@firebase/database-types';

// @public (undocumented)
// @public
export interface App {
name: string;
options: AppOptions;
Expand Down Expand Up @@ -46,7 +46,7 @@ export namespace app {
}
}

// @public (undocumented)
// @public
export function applicationDefault(httpAgent?: Agent): Credential;

// @public
Expand Down Expand Up @@ -274,10 +274,10 @@ export namespace auth {
export type UserRecord = UserRecord;
}

// @public (undocumented)
// @public
export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAgent?: Agent): Credential;

// @public (undocumented)
// @public
export interface Credential {
getAccessToken(): Promise<GoogleOAuthAccessToken>;
}
Expand Down Expand Up @@ -316,7 +316,7 @@ export namespace database {
const ServerValue: rtdb.ServerValue;
}

// @public (undocumented)
// @public
export function deleteApp(app: App): Promise<void>;

// @public
Expand Down Expand Up @@ -618,7 +618,7 @@ export namespace projectManagement {
export type ShaCertificate = ShaCertificate;
}

// @public (undocumented)
// @public
export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential;

// @public
Expand Down
12 changes: 6 additions & 6 deletions etc/firebase-admin.app.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { Agent } from 'http';

// @public (undocumented)
// @public
export interface App {
name: string;
options: AppOptions;
}

// @public (undocumented)
// @public
export function applicationDefault(httpAgent?: Agent): Credential;

// @public
Expand All @@ -26,15 +26,15 @@ export interface AppOptions {
storageBucket?: string;
}

// @public (undocumented)
// @public
export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAgent?: Agent): Credential;

// @public (undocumented)
// @public
export interface Credential {
getAccessToken(): Promise<GoogleOAuthAccessToken>;
}

// @public (undocumented)
// @public
export function deleteApp(app: App): Promise<void>;

// @public
Expand Down Expand Up @@ -68,7 +68,7 @@ export interface GoogleOAuthAccessToken {
// @public (undocumented)
export function initializeApp(options?: AppOptions, name?: string): App;

// @public (undocumented)
// @public
export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential;

// @public (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion etc/firebase-admin.instance-id.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Agent } from 'http';

// Warning: (ae-forgotten-export) The symbol "App" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
// @public
export function getInstanceId(app?: App): InstanceId;

// @public
Expand Down
16 changes: 13 additions & 3 deletions src/app/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ export interface AppOptions {
httpAgent?: Agent;
}

/**
* A Firebase app holds the initialization information for a collection of
* services.
*
* Do not call this constructor directly. Instead, use
* {@link
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
* `admin.initializeApp()`}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be just initializeApp(), and below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is shared between both old and modularized APIs. So we can go either way here. For now I'm going to keep the old documentation here.

* to create an app.
*/
export interface App {

/**
Expand All @@ -94,14 +104,14 @@ export interface App {
* @example
* ```javascript
* // The default app's name is "[DEFAULT]"
* admin.initializeApp(defaultAppConfig);
* initializeApp(defaultAppConfig);
* console.log(admin.app().name); // "[DEFAULT]"
* ```
*
* @example
* ```javascript
* // A named app's name is what you provide to initializeApp()
* var otherApp = admin.initializeApp(otherAppConfig, "other");
* const otherApp = initializeApp(otherAppConfig, "other");
* console.log(otherApp.name); // "other"
* ```
*/
Expand All @@ -116,7 +126,7 @@ export interface App {
*
* @example
* ```javascript
* var app = admin.initializeApp(config);
* const app = initializeApp(config);
* console.log(app.options.credential === config.credential); // true
* console.log(app.options.databaseURL === config.databaseURL); // true
* ```
Expand Down
112 changes: 112 additions & 0 deletions src/app/credential-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,92 @@ let globalAppDefaultCred: Credential | undefined;
const globalCertCreds: { [key: string]: ServiceAccountCredential } = {};
const globalRefreshTokenCreds: { [key: string]: RefreshTokenCredential } = {};

/**
* Returns a credential created from the
* {@link
* https://developers.google.com/identity/protocols/application-default-credentials
* Google Application Default Credentials}
* that grants admin access to Firebase services. This credential can be used
* in the call to
* {@link
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
* `admin.initializeApp()`}.
*
* Google Application Default Credentials are available on any Google
* infrastructure, such as Google App Engine and Google Compute Engine.
*
* See
* {@link
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
* Initialize the SDK}
* for more details.
*
* @example
* ```javascript
* initializeApp({
* credential: applicationDefault(),
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
* });
* ```
*
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
* to be used when retrieving access tokens from Google token servers.
*
* @return A credential authenticated via Google
* Application Default Credentials that can be used to initialize an app.
*/
export function applicationDefault(httpAgent?: Agent): Credential {
if (typeof globalAppDefaultCred === 'undefined') {
globalAppDefaultCred = getApplicationDefault(httpAgent);
}
return globalAppDefaultCred;
}

/**
* Returns a credential created from the provided service account that grants
* admin access to Firebase services. This credential can be used in the call
* to
* {@link
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
* `admin.initializeApp()`}.
*
* See
* {@link
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
* Initialize the SDK}
* for more details.
*
* @example
* ```javascript
* // Providing a path to a service account key JSON file
* const serviceAccount = require("path/to/serviceAccountKey.json");
* initializeApp({
* credential: cert(serviceAccount),
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
* });
* ```
*
* @example
* ```javascript
* // Providing a service account object inline
* initializeApp({
* credential: cert({
* projectId: "<PROJECT_ID>",
* clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
* privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n"
* }),
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
* });
* ```
*
* @param serviceAccountPathOrObject The path to a service
* account key JSON file or an object representing a service account key.
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
* to be used when retrieving access tokens from Google token servers.
*
* @return A credential authenticated via the
* provided service account that can be used to initialize an app.
*/
export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAgent?: Agent): Credential {
const stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
if (!(stringifiedServiceAccount in globalCertCreds)) {
Expand All @@ -42,6 +121,39 @@ export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAg
return globalCertCreds[stringifiedServiceAccount];
}

/**
* Returns a credential created from the provided refresh token that grants
* admin access to Firebase services. This credential can be used in the call
* to
* {@link
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
* `admin.initializeApp()`}.
*
* See
* {@link
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
* Initialize the SDK}
* for more details.
*
* @example
* ```javascript
* // Providing a path to a refresh token JSON file
* const refreshToken = require("path/to/refreshToken.json");
* initializeApp({
* credential: refreshToken(refreshToken),
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
* });
* ```
*
* @param refreshTokenPathOrObject The path to a Google
* OAuth2 refresh token JSON file or an object representing a Google OAuth2
* refresh token.
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
* to be used when retrieving access tokens from Google token servers.
*
* @return A credential authenticated via the
* provided service account that can be used to initialize an app.
*/
export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential {
const stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export interface GoogleOAuthAccessToken {
expires_in: number;
}

/**
* Interface that provides Google OAuth2 access tokens used to authenticate
* with Firebase services.
*
* In most cases, you will not need to implement this yourself and can instead
* use the default implementations provided by
* {@link credential `admin.credential`}.
*/
export interface Credential {
/**
* Returns a Google OAuth2 access token object used to authenticate with
Expand Down
17 changes: 17 additions & 0 deletions src/app/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ export function getApps(): App[] {
return defaultNamespace.apps;
}

/**
* Renders this given `App` unusable and frees the resources of
* all associated services (though it does *not* clean up any backend
* resources). When running the SDK locally, this method
* must be called to ensure graceful termination of the process.
*
* @example
* ```javascript
* deleteApp(app)
* .then(function() {
* console.log("App deleted successfully");
* })
* .catch(function(error) {
* console.log("Error deleting app:", error);
* });
* ```
*/
export function deleteApp(app: App): Promise<void> {
if (typeof app !== 'object' || app === null || !('options' in app)) {
throw new FirebaseAppError(AppErrorCodes.INVALID_ARGUMENT, 'Invalid app argument.');
Expand Down
30 changes: 30 additions & 0 deletions src/instance-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ import { InstanceId } from './instance-id';

export { InstanceId };

/**
* Gets the {@link InstanceId `InstanceId`} service for the
* default app or a given app.
*
* `getInstanceId()` can be called with no arguments to access the default
* app's {@link InstanceId `InstanceId`} service or as
* `getInstanceId(app)` to access the
* {@link InstanceId `InstanceId`} service associated with a
* specific app.
*
* @example
* ```javascript
* // Get the Instance ID service for the default app
* const defaultInstanceId = getInstanceId();
* ```
*
* @example
* ```javascript
* // Get the Instance ID service for a given app
* const otherInstanceId = getInstanceId(otherApp);
*```
*
* @param app Optional app whose `InstanceId` service to
* return. If not provided, the default `InstanceId` service will be
* returned.
*
* @return The default `InstanceId` service if
* no app is provided or the `InstanceId` service associated with the
* provided app.
*/
export function getInstanceId(app?: App): InstanceId {
if (typeof app === 'undefined') {
app = getApp();
Expand Down
14 changes: 2 additions & 12 deletions src/instance-id/instance-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,8 @@ import { FirebaseInstanceIdRequestHandler } from './instance-id-request-internal
import * as validator from '../utils/validator';

/**
* Gets the {@link InstanceId `InstanceId`} service for the
* current app.
*
* @example
* ```javascript
* var instanceId = app.instanceId();
* // The above is shorthand for:
* // var instanceId = admin.instanceId(app);
* ```
*
* @return The `InstanceId` service for the
* current app.
* The `InstanceId` service enables deleting the Firebase instance IDs
* associated with Firebase client app instances.
*/
export class InstanceId {

Expand Down