-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathusers.ts
More file actions
108 lines (100 loc) · 2.7 KB
/
users.ts
File metadata and controls
108 lines (100 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Client from '../networking';
import type { Auth0Response } from '../networking';
import { toCamelCase } from '../utils/camel';
import Auth0Error from './error';
import type { Telemetry } from '../networking/telemetry';
import type { GetUserOptions, PatchUserOptions, User } from '../types';
import type { RawUser } from '../internal-types';
function responseHandler<TRawResult = unknown, TResult = unknown>(
response: Auth0Response<TRawResult>,
exceptions = {}
) {
if (response.ok && response.json) {
return toCamelCase(response.json, exceptions) as TResult;
}
throw new Auth0Error(response);
}
const attributes = [
'name',
'nickname',
'picture',
'user_id',
'user_metadata',
'app_metadata',
'email',
'email_verified',
'given_name',
'family_name',
];
interface UsersClientOptions {
baseUrl: string;
telemetry?: Telemetry;
token?: string;
timeout?: number;
acceptLanguage?: string;
}
/**
* Auth0 Management API User endpoints
*
* @export
* @see https://auth0.com/docs/api/management/v2#!/Users/
* @class Users
*/
class Users {
private client: Client;
/**
* @ignore
*/
constructor(options: UsersClientOptions) {
this.client = new Client(options);
if (!options.token) {
throw new Error('Missing token in parameters');
}
}
/**
* Returns the user by identifier
*
* @param {Object} parameters get user by identifier parameters
* @param {String} parameters.id identifier of the user to obtain
* @returns {Promise}
* @see https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id
*
* @memberof Users
*/
getUser(parameters: GetUserOptions): Promise<User> {
return this.client
.get<RawUser>(`/api/v2/users/${encodeURIComponent(parameters.id)}`)
.then((response) =>
responseHandler<RawUser, User>(response, {
attributes,
whitelist: true,
rootOnly: true,
})
);
}
/**
* Patch a user's `user_metadata`
*
* @param {Object} parameters patch user metadata parameters
* @param {String} parameters.id identifier of the user to patch
* @param {Object} parameters.metadata object with attributes to store in user_metadata.
* @returns {Promise}
* @see https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
*
* @memberof Users
*/
patchUser(parameters: PatchUserOptions): Promise<User> {
return this.client
.patch<RawUser>(`/api/v2/users/${encodeURIComponent(parameters.id)}`, {
user_metadata: parameters.metadata,
})
.then((response) =>
responseHandler<RawUser, User>(response, {
attributes,
whitelist: true,
rootOnly: true,
})
);
}
}
export default Users;