Skip to content

Commit 2697420

Browse files
authored
Type definitions for createUser() and updateUser() parameters (#64)
* Type definitions for createUser() and updateUser() parameters * Defining new types as interfaces * Updated type definitions
1 parent 9fe378d commit 2697420

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/auth/auth-api-request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {AuthClientErrorCode, FirebaseAuthError} from '../utils/error';
2323
import {
2424
HttpMethod, SignedApiRequestHandler, ApiSettings,
2525
} from '../utils/api-request';
26+
import {CreateRequest, UpdateRequest} from './user-record';
2627

2728

2829
/** Firebase Auth backend host. */
@@ -300,7 +301,7 @@ export class FirebaseAuthRequestHandler {
300301
* @return {Promise<string>} A promise that resolves when the operation completes
301302
* with the user id that was edited.
302303
*/
303-
public updateExistingAccount(uid: string, properties: Object): Promise<string> {
304+
public updateExistingAccount(uid: string, properties: UpdateRequest): Promise<string> {
304305
if (!validator.isUid(uid)) {
305306
return Promise.reject(new FirebaseAuthError(AuthClientErrorCode.INVALID_UID));
306307
} else if (!validator.isNonNullObject(properties)) {
@@ -376,7 +377,7 @@ export class FirebaseAuthRequestHandler {
376377
* @return {Promise<string>} A promise that resolves when the operation completes
377378
* with the user id that was created.
378379
*/
379-
public createNewAccount(properties: Object): Promise<string> {
380+
public createNewAccount(properties: CreateRequest): Promise<string> {
380381
if (!validator.isNonNullObject(properties)) {
381382
return Promise.reject(
382383
new FirebaseAuthError(

src/auth/auth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {UserRecord} from './user-record';
17+
import {UserRecord, CreateRequest, UpdateRequest} from './user-record';
1818
import {Certificate} from './credential';
1919
import {FirebaseApp} from '../firebase-app';
2020
import {FirebaseTokenGenerator} from './token-generator';
@@ -181,10 +181,10 @@ class Auth implements FirebaseServiceInterface {
181181
/**
182182
* Creates a new user with the properties provided.
183183
*
184-
* @param {Object} properties The properties to set on the new user record to be created.
184+
* @param {CreateRequest} properties The properties to set on the new user record to be created.
185185
* @return {Promise<UserRecord>} A promise that resolves with the newly created user record.
186186
*/
187-
public createUser(properties: Object): Promise<UserRecord> {
187+
public createUser(properties: CreateRequest): Promise<UserRecord> {
188188
return this.authRequestHandler.createNewAccount(properties)
189189
.then((uid) => {
190190
// Return the corresponding user record.
@@ -219,10 +219,10 @@ class Auth implements FirebaseServiceInterface {
219219
* Updates an existing user with the properties provided.
220220
*
221221
* @param {string} uid The uid identifier of the user to update.
222-
* @param {Object} properties The properties to update on the existing user.
222+
* @param {UpdateRequest} properties The properties to update on the existing user.
223223
* @return {Promise<UserRecord>} A promise that resolves with the modified user record.
224224
*/
225-
public updateUser(uid: string, properties: Object): Promise<UserRecord> {
225+
public updateUser(uid: string, properties: UpdateRequest): Promise<UserRecord> {
226226
return this.authRequestHandler.updateExistingAccount(uid, properties)
227227
.then((existingUid) => {
228228
// Return the corresponding user record.

src/auth/user-record.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ function parseDate(time: any): string {
3535
return null;
3636
}
3737

38+
/** Parameters for update user operation */
39+
export interface UpdateRequest {
40+
displayName?: string;
41+
email?: string;
42+
emailVerified?: boolean;
43+
phoneNumber?: string;
44+
photoURL?: string;
45+
disabled?: boolean;
46+
password?: string;
47+
}
48+
49+
/** Parameters for create user operation */
50+
export interface CreateRequest extends UpdateRequest {
51+
uid?: string;
52+
}
3853

3954
/**
4055
* User metadata class that provides metadata information like user account creation

src/index.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ declare namespace admin.auth {
101101
toJSON(): Object;
102102
}
103103

104+
interface UpdateRequest {
105+
displayName?: string;
106+
email?: string;
107+
emailVerified?: boolean;
108+
phoneNumber?: string;
109+
photoURL?: string;
110+
disabled?: boolean;
111+
password?: string;
112+
}
113+
114+
interface CreateRequest extends UpdateRequest {
115+
uid?: string;
116+
}
117+
104118
interface DecodedIdToken {
105119
aud: string;
106120
auth_time: number;
@@ -123,12 +137,12 @@ declare namespace admin.auth {
123137
app: admin.app.App;
124138

125139
createCustomToken(uid: string, developerClaims?: Object): Promise<string>;
126-
createUser(properties: Object): Promise<admin.auth.UserRecord>;
140+
createUser(properties: admin.auth.CreateRequest): Promise<admin.auth.UserRecord>;
127141
deleteUser(uid: string): Promise<void>;
128142
getUser(uid: string): Promise<admin.auth.UserRecord>;
129143
getUserByEmail(email: string): Promise<admin.auth.UserRecord>;
130144
getUserByPhoneNumber(phoneNumber: string): Promise<admin.auth.UserRecord>;
131-
updateUser(uid: string, properties: Object): Promise<admin.auth.UserRecord>;
145+
updateUser(uid: string, properties: admin.auth.UpdateRequest): Promise<admin.auth.UserRecord>;
132146
verifyIdToken(idToken: string): Promise<admin.auth.DecodedIdToken>;
133147
}
134148
}

0 commit comments

Comments
 (0)