UNPKG

4.4 kBJavaScriptView Raw
1/**
2 * Provides an interface for the Anvil Connect Users CRUD API.
3 * Note: All user operations require an access token passed in `options.token`.
4 * You can get this token either via an admin user (with an `authority` role
5 * assigned to them) login, OR via a Client Credentials Grant request
6 * (see `client.getClientAccessToken()` docs in `../index.js`).
7 * Example Usage:
8 *
9 * ```
10 * client.getClientAccessToken()
11 * .then(function (accessToken) {
12 * var options = { token: accessToken }
13 * // Once you have the access token you can
14 * // call client.users.update(), create(), delete(), etc
15 * return client.users.create(userData, options)
16 * })
17 * ```
18 * @module users
19 */
20
21/**
22 * Module dependencies
23 */
24var request = require('../lib/request')
25
26/**
27 * Retrieves a list of user accounts via a REST request to the AnvilConnect
28 * server's `/users` API.
29 * @method listUsers
30 * @param options {Object} Options hashmap object
31 * @param options.token {String} (Required) Access token. Gets sent as an
32 * Authorization: Bearer <token> header.
33 * @param [options.headers={}] {Object} Optional hashmap of additional headers
34 * @return {Promise<Request>}
35 */
36function listUsers (options) {
37 options = options || {}
38 options.url = '/v1/users'
39 return request.bind(this)(options)
40}
41exports.list = listUsers
42
43/**
44 * Retrieves a user's account details via a REST request to the AnvilConnect
45 * server's `/users` API.
46 * @method getUser
47 * @param id {String} User ID (`sub` in the ID Token)
48 * @param options {Object} Options hashmap object
49 * @param options.token {String} (Required) Access token. Gets sent as an
50 * Authorization: Bearer <token> header.
51 * @param [options.headers={}] {Object} Optional hashmap of additional headers
52 * @return {Promise<Request>}
53 */
54function getUser (id, options) {
55 options = options || {}
56 options.url = '/v1/users/' + id
57 return request.bind(this)(options)
58}
59exports.get = getUser
60
61/**
62 * Creates a user via a REST request to the AnvilConnect server's /users API.
63 * Usage:
64 *
65 * ```
66 * var userData = {
67 * email: 'alice@example.com',
68 * name: 'Alice',
69 * password: 'swordfish'
70 * }
71 * client.getClientAccessToken()
72 * .then(function (accessToken) {
73 * var options = { token: accessToken }
74 * return client.users.create(userData, options)
75 * })
76 * ```
77 * @method createUser
78 * @param data {Object} User details object
79 * @param options {Object} Options hashmap object
80 * @param options.token {String} (Required) Access token. Gets sent as an
81 * Authorization: Bearer <token> header.
82 * @param [options.headers={}] {Object} Optional hashmap of additional headers
83 * @return {Promise<Request>}
84 */
85function createUser (data, options) {
86 options = options || {}
87 options.url = '/v1/users'
88 options.method = 'POST'
89 options.json = data
90 return request.bind(this)(options)
91}
92exports.create = createUser
93
94/**
95 * Updates a user via a REST request to the AnvilConnect server's /users API.
96 * Note: You cannot update a user's password using this call (the field gets
97 * ignored by the /users endpoint).
98 * @method updateUser
99 * @param id {String} User ID (`sub` in the ID Token)
100 * @param data {Object} User details object
101 * @param options {Object} Options hashmap object
102 * @param options.token {String} (Required) Access token. Gets sent as an
103 * Authorization: Bearer <token> header.
104 * @param [options.headers={}] {Object} Optional hashmap of additional headers
105 * @return {Promise<Request>}
106 */
107function updateUser (id, data, options) {
108 options = options || {}
109 options.url = '/v1/users/' + id
110 options.method = 'PATCH'
111 options.json = data
112 return request.bind(this)(options)
113}
114exports.update = updateUser
115
116/**
117 * Deletes a user via a REST request to the AnvilConnect server's /users API.
118 * @method deleteUser
119 * @param id {String} User ID (`sub` in the ID Token).
120 * @param options {Object} Options hashmap object
121 * @param options.token {String} (Required) Access token. Gets sent as an
122 * Authorization: Bearer <token> header.
123 * @param [options.headers={}] {Object} Optional hashmap of additional headers
124 * @return {Promise<Request>}
125 */
126function deleteUser (id, options) {
127 options = options || {}
128 options.url = '/v1/users/' + id
129 options.method = 'DELETE'
130 delete options.json
131 return request.bind(this)(options)
132}
133exports.delete = deleteUser