Skip to content

Commit 5a0f2fb

Browse files
authored
fix: Updated documentation for app and instance-id entry points (#1168)
* fix: Added API ref docs to firebase-admin/app * fix: Added API docs to firebase-admin/instance-id
1 parent 82d6dc1 commit 5a0f2fb

9 files changed

+195
-28
lines changed

etc/firebase-admin.api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { FirebaseDatabase } from '@firebase/database-types';
1010
import * as _firestore from '@google-cloud/firestore';
1111
import * as rtdb from '@firebase/database-types';
1212

13-
// @public (undocumented)
13+
// @public
1414
export interface App {
1515
name: string;
1616
options: AppOptions;
@@ -46,7 +46,7 @@ export namespace app {
4646
}
4747
}
4848

49-
// @public (undocumented)
49+
// @public
5050
export function applicationDefault(httpAgent?: Agent): Credential;
5151

5252
// @public
@@ -274,10 +274,10 @@ export namespace auth {
274274
export type UserRecord = UserRecord;
275275
}
276276

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

280-
// @public (undocumented)
280+
// @public
281281
export interface Credential {
282282
getAccessToken(): Promise<GoogleOAuthAccessToken>;
283283
}
@@ -316,7 +316,7 @@ export namespace database {
316316
const ServerValue: rtdb.ServerValue;
317317
}
318318

319-
// @public (undocumented)
319+
// @public
320320
export function deleteApp(app: App): Promise<void>;
321321

322322
// @public
@@ -618,7 +618,7 @@ export namespace projectManagement {
618618
export type ShaCertificate = ShaCertificate;
619619
}
620620

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

624624
// @public

etc/firebase-admin.app.api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
import { Agent } from 'http';
88

9-
// @public (undocumented)
9+
// @public
1010
export interface App {
1111
name: string;
1212
options: AppOptions;
1313
}
1414

15-
// @public (undocumented)
15+
// @public
1616
export function applicationDefault(httpAgent?: Agent): Credential;
1717

1818
// @public
@@ -26,15 +26,15 @@ export interface AppOptions {
2626
storageBucket?: string;
2727
}
2828

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

32-
// @public (undocumented)
32+
// @public
3333
export interface Credential {
3434
getAccessToken(): Promise<GoogleOAuthAccessToken>;
3535
}
3636

37-
// @public (undocumented)
37+
// @public
3838
export function deleteApp(app: App): Promise<void>;
3939

4040
// @public
@@ -68,7 +68,7 @@ export interface GoogleOAuthAccessToken {
6868
// @public (undocumented)
6969
export function initializeApp(options?: AppOptions, name?: string): App;
7070

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

7474
// @public (undocumented)

etc/firebase-admin.instance-id.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Agent } from 'http';
88

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

1414
// @public

src/app/core.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ export interface AppOptions {
8484
httpAgent?: Agent;
8585
}
8686

87+
/**
88+
* A Firebase app holds the initialization information for a collection of
89+
* services.
90+
*
91+
* Do not call this constructor directly. Instead, use
92+
* {@link
93+
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
94+
* `admin.initializeApp()`}
95+
* to create an app.
96+
*/
8797
export interface App {
8898

8999
/**
@@ -94,14 +104,14 @@ export interface App {
94104
* @example
95105
* ```javascript
96106
* // The default app's name is "[DEFAULT]"
97-
* admin.initializeApp(defaultAppConfig);
107+
* initializeApp(defaultAppConfig);
98108
* console.log(admin.app().name); // "[DEFAULT]"
99109
* ```
100110
*
101111
* @example
102112
* ```javascript
103113
* // A named app's name is what you provide to initializeApp()
104-
* var otherApp = admin.initializeApp(otherAppConfig, "other");
114+
* const otherApp = initializeApp(otherAppConfig, "other");
105115
* console.log(otherApp.name); // "other"
106116
* ```
107117
*/
@@ -116,7 +126,7 @@ export interface App {
116126
*
117127
* @example
118128
* ```javascript
119-
* var app = admin.initializeApp(config);
129+
* const app = initializeApp(config);
120130
* console.log(app.options.credential === config.credential); // true
121131
* console.log(app.options.databaseURL === config.databaseURL); // true
122132
* ```

src/app/credential-factory.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,92 @@ let globalAppDefaultCred: Credential | undefined;
2626
const globalCertCreds: { [key: string]: ServiceAccountCredential } = {};
2727
const globalRefreshTokenCreds: { [key: string]: RefreshTokenCredential } = {};
2828

29+
/**
30+
* Returns a credential created from the
31+
* {@link
32+
* https://developers.google.com/identity/protocols/application-default-credentials
33+
* Google Application Default Credentials}
34+
* that grants admin access to Firebase services. This credential can be used
35+
* in the call to
36+
* {@link
37+
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
38+
* `admin.initializeApp()`}.
39+
*
40+
* Google Application Default Credentials are available on any Google
41+
* infrastructure, such as Google App Engine and Google Compute Engine.
42+
*
43+
* See
44+
* {@link
45+
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
46+
* Initialize the SDK}
47+
* for more details.
48+
*
49+
* @example
50+
* ```javascript
51+
* initializeApp({
52+
* credential: applicationDefault(),
53+
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
54+
* });
55+
* ```
56+
*
57+
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
58+
* to be used when retrieving access tokens from Google token servers.
59+
*
60+
* @return A credential authenticated via Google
61+
* Application Default Credentials that can be used to initialize an app.
62+
*/
2963
export function applicationDefault(httpAgent?: Agent): Credential {
3064
if (typeof globalAppDefaultCred === 'undefined') {
3165
globalAppDefaultCred = getApplicationDefault(httpAgent);
3266
}
3367
return globalAppDefaultCred;
3468
}
3569

70+
/**
71+
* Returns a credential created from the provided service account that grants
72+
* admin access to Firebase services. This credential can be used in the call
73+
* to
74+
* {@link
75+
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
76+
* `admin.initializeApp()`}.
77+
*
78+
* See
79+
* {@link
80+
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
81+
* Initialize the SDK}
82+
* for more details.
83+
*
84+
* @example
85+
* ```javascript
86+
* // Providing a path to a service account key JSON file
87+
* const serviceAccount = require("path/to/serviceAccountKey.json");
88+
* initializeApp({
89+
* credential: cert(serviceAccount),
90+
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
91+
* });
92+
* ```
93+
*
94+
* @example
95+
* ```javascript
96+
* // Providing a service account object inline
97+
* initializeApp({
98+
* credential: cert({
99+
* projectId: "<PROJECT_ID>",
100+
* clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
101+
* privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n"
102+
* }),
103+
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
104+
* });
105+
* ```
106+
*
107+
* @param serviceAccountPathOrObject The path to a service
108+
* account key JSON file or an object representing a service account key.
109+
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
110+
* to be used when retrieving access tokens from Google token servers.
111+
*
112+
* @return A credential authenticated via the
113+
* provided service account that can be used to initialize an app.
114+
*/
36115
export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAgent?: Agent): Credential {
37116
const stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
38117
if (!(stringifiedServiceAccount in globalCertCreds)) {
@@ -42,6 +121,39 @@ export function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAg
42121
return globalCertCreds[stringifiedServiceAccount];
43122
}
44123

124+
/**
125+
* Returns a credential created from the provided refresh token that grants
126+
* admin access to Firebase services. This credential can be used in the call
127+
* to
128+
* {@link
129+
* https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
130+
* `admin.initializeApp()`}.
131+
*
132+
* See
133+
* {@link
134+
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
135+
* Initialize the SDK}
136+
* for more details.
137+
*
138+
* @example
139+
* ```javascript
140+
* // Providing a path to a refresh token JSON file
141+
* const refreshToken = require("path/to/refreshToken.json");
142+
* initializeApp({
143+
* credential: refreshToken(refreshToken),
144+
* databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
145+
* });
146+
* ```
147+
*
148+
* @param refreshTokenPathOrObject The path to a Google
149+
* OAuth2 refresh token JSON file or an object representing a Google OAuth2
150+
* refresh token.
151+
* @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
152+
* to be used when retrieving access tokens from Google token servers.
153+
*
154+
* @return A credential authenticated via the
155+
* provided service account that can be used to initialize an app.
156+
*/
45157
export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential {
46158
const stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
47159
if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {

src/app/credential.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export interface GoogleOAuthAccessToken {
2929
expires_in: number;
3030
}
3131

32+
/**
33+
* Interface that provides Google OAuth2 access tokens used to authenticate
34+
* with Firebase services.
35+
*
36+
* In most cases, you will not need to implement this yourself and can instead
37+
* use the default implementations provided by
38+
* {@link credential `admin.credential`}.
39+
*/
3240
export interface Credential {
3341
/**
3442
* Returns a Google OAuth2 access token object used to authenticate with

src/app/lifecycle.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ export function getApps(): App[] {
4141
return defaultNamespace.apps;
4242
}
4343

44+
/**
45+
* Renders this given `App` unusable and frees the resources of
46+
* all associated services (though it does *not* clean up any backend
47+
* resources). When running the SDK locally, this method
48+
* must be called to ensure graceful termination of the process.
49+
*
50+
* @example
51+
* ```javascript
52+
* deleteApp(app)
53+
* .then(function() {
54+
* console.log("App deleted successfully");
55+
* })
56+
* .catch(function(error) {
57+
* console.log("Error deleting app:", error);
58+
* });
59+
* ```
60+
*/
4461
export function deleteApp(app: App): Promise<void> {
4562
if (typeof app !== 'object' || app === null || !('options' in app)) {
4663
throw new FirebaseAppError(AppErrorCodes.INVALID_ARGUMENT, 'Invalid app argument.');

src/instance-id/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@ import { InstanceId } from './instance-id';
2020

2121
export { InstanceId };
2222

23+
/**
24+
* Gets the {@link InstanceId `InstanceId`} service for the
25+
* default app or a given app.
26+
*
27+
* `getInstanceId()` can be called with no arguments to access the default
28+
* app's {@link InstanceId `InstanceId`} service or as
29+
* `getInstanceId(app)` to access the
30+
* {@link InstanceId `InstanceId`} service associated with a
31+
* specific app.
32+
*
33+
* @example
34+
* ```javascript
35+
* // Get the Instance ID service for the default app
36+
* const defaultInstanceId = getInstanceId();
37+
* ```
38+
*
39+
* @example
40+
* ```javascript
41+
* // Get the Instance ID service for a given app
42+
* const otherInstanceId = getInstanceId(otherApp);
43+
*```
44+
*
45+
* @param app Optional app whose `InstanceId` service to
46+
* return. If not provided, the default `InstanceId` service will be
47+
* returned.
48+
*
49+
* @return The default `InstanceId` service if
50+
* no app is provided or the `InstanceId` service associated with the
51+
* provided app.
52+
*/
2353
export function getInstanceId(app?: App): InstanceId {
2454
if (typeof app === 'undefined') {
2555
app = getApp();

src/instance-id/instance-id.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,8 @@ import { FirebaseInstanceIdRequestHandler } from './instance-id-request-internal
2020
import * as validator from '../utils/validator';
2121

2222
/**
23-
* Gets the {@link InstanceId `InstanceId`} service for the
24-
* current app.
25-
*
26-
* @example
27-
* ```javascript
28-
* var instanceId = app.instanceId();
29-
* // The above is shorthand for:
30-
* // var instanceId = admin.instanceId(app);
31-
* ```
32-
*
33-
* @return The `InstanceId` service for the
34-
* current app.
23+
* The `InstanceId` service enables deleting the Firebase instance IDs
24+
* associated with Firebase client app instances.
3525
*/
3626
export class InstanceId {
3727

0 commit comments

Comments
 (0)