UNPKG

2.23 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _user_id = Joi.number().min(1);
6const _data = Joi.object();
7
8// Initialize Endpoint
9module.exports = (options = {}) => {
10 const { error } = validate(options);
11 if (error) throw new Error(error.details[0].message);
12
13 const { url, headers } = prepare(options);
14
15 return {
16 /**
17 * Set a User's Password
18 *
19 * POST /api/v2/users/{user_id}/password.json
20 * https://developer.zendesk.com/rest_api/docs/support/user_passwords#set-a-users-password
21 */
22 set: (options = {}) => {
23 const { error } = Joi.object({
24 user_id: _user_id.required(),
25 data: _data.required()
26 }).validate(options);
27 if (error) throw new Error(error.details[0].message);
28
29 const { user_id, data } = options;
30 return {
31 method: 'POST',
32 url: `${url}/api/v2/users/${user_id}/password.json`,
33 headers,
34 data
35 };
36 },
37
38 /**
39 * Change Your Password
40 *
41 * PUT /api/v2/users/{user_id}/password.json
42 * https://developer.zendesk.com/rest_api/docs/support/user_passwords#change-your-password
43 */
44 change: (options = {}) => {
45 const { error } = Joi.object({
46 user_id: _user_id.required(),
47 data: _data.required()
48 }).validate(options);
49 if (error) throw new Error(error.details[0].message);
50
51 const { user_id, data } = options;
52 return {
53 method: 'PUT',
54 url: `${url}/api/v2/users/${user_id}/password.json`,
55 headers,
56 data
57 };
58 },
59
60 /**
61 * Get a list of password requirements
62 *
63 * GET /api/v2/users/{user_id}/password/requirements.json
64 * https://developer.zendesk.com/rest_api/docs/support/user_passwords#get-a-list-of-password-requirements
65 */
66 requirements: (options = {}) => {
67 const { error } = Joi.object({
68 user_id: _user_id.required()
69 }).validate(options);
70 if (error) throw new Error(error.details[0].message);
71
72 const { user_id } = options;
73 return {
74 method: 'GET',
75 url: `${url}/api/v2/users/${user_id}/password/requirements.json`,
76 headers
77 };
78 }
79 };
80};