1 | /*
|
2 | * Copyright 2019 Adobe. All rights reserved.
|
3 | * This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
4 | * you may not use this file except in compliance with the License. You may obtain a copy
|
5 | * of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6 | *
|
7 | * Unless required by applicable law or agreed to in writing, software distributed under
|
8 | * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | * governing permissions and limitations under the License.
|
11 | */
|
12 | const axios = require('./httpclient');
|
13 | const config = require('./config');
|
14 |
|
15 | /**
|
16 | * The Fastly Account API.
|
17 | *
|
18 | * @see https://docs.fastly.com/api/account#top
|
19 | * @type {AccountAPI}
|
20 | */
|
21 | class AccountAPI {
|
22 | constructor(base) {
|
23 | this.base = base;
|
24 | this.defaultOptions = {
|
25 | baseURL: config.mainEntryPoint,
|
26 | timeout: 15000,
|
27 | headers: {},
|
28 | };
|
29 | }
|
30 |
|
31 | get request() {
|
32 | return this.base.request;
|
33 | }
|
34 |
|
35 | /**
|
36 | * Get the currently logged in user.
|
37 | *
|
38 | * @see https://docs.fastly.com/api/account#user_91db9d9178f3f4c7597899942bd3f941
|
39 | * @returns {Promise} The response object representing the completion or failure.
|
40 | */
|
41 | async readCurrentUser() {
|
42 | if (!this._currentUser) {
|
43 | this._currentUser = await this.request.get('/current_user');
|
44 | }
|
45 | return this._currentUser;
|
46 | }
|
47 |
|
48 | /**
|
49 | * Get a list of all users from the current customer.
|
50 | *
|
51 | * @see https://docs.fastly.com/api/account#customer_12f4a69627ba3bbb1c8668aae03a60ad
|
52 | * @returns {Promise} The response object representing the completion or failure.
|
53 | */
|
54 | async readUsers() {
|
55 | const id = (await this.readCurrentUser()).data.customer_id;
|
56 | return this.request.get(`/customer/${id}/users`);
|
57 | }
|
58 |
|
59 | /**
|
60 | * Get the the user with the specific id.
|
61 | *
|
62 | * @see https://docs.fastly.com/api/account#user_15a6c72980b9434ebb8253c7e882c26c
|
63 | * @param {string} id - The User ID.
|
64 | * @returns {Promise} The response object representing the completion or failure.
|
65 | */
|
66 | async readUser(id) {
|
67 | return this.request.get(`/users/${id}`);
|
68 | }
|
69 |
|
70 | /**
|
71 | * Create a user.
|
72 | *
|
73 | * @see https://docs.fastly.com/api/account#user_00b606002596bac1c652614de98bd260
|
74 | * @param {string} name - The user name.
|
75 | * @param {string} login - The user login.
|
76 | * @returns {Promise} The response object representing the completion or failure.
|
77 | */
|
78 | async createUser(name, login) {
|
79 | return this.request.post('/user', {
|
80 | name,
|
81 | login,
|
82 | });
|
83 | }
|
84 |
|
85 | /**
|
86 | * List all invitations.
|
87 | *
|
88 | * @see https://docs.fastly.com/api/account#invitations_6d8623de97ed7e50b7b6498e374bb657
|
89 | * @returns {Promise} The response object representing the completion or failure.
|
90 | */
|
91 | async readInvitations() {
|
92 | return this.request.get('/invitations');
|
93 | }
|
94 |
|
95 | /**
|
96 | * Create an invitation.
|
97 | *
|
98 | * @see https://docs.fastly.com/api/account#invitations_8c4da3ca11c75facd36cfaad024bd891
|
99 | * @param {string} email - The email address for the invitation.
|
100 | * @param {string} role - The user role. Defaults to {@code engineer}.
|
101 | * @returns {Promise} The response object representing the completion or failure.
|
102 | */
|
103 | async createInvitation(email, role = 'engineer') {
|
104 | const id = (await this.readCurrentUser()).data.customer_id;
|
105 | return this.request.post('/invitations', {
|
106 | data: {
|
107 | type: 'invitation',
|
108 | attributes: {
|
109 | email,
|
110 | limit_services: true,
|
111 | role,
|
112 | },
|
113 | relationships: {
|
114 | customer: {
|
115 | data: {
|
116 | id,
|
117 | type: 'customer',
|
118 | },
|
119 | },
|
120 | },
|
121 | },
|
122 | }, {
|
123 | headers: {
|
124 | 'content-type': 'application/json',
|
125 | },
|
126 | });
|
127 | }
|
128 |
|
129 | /**
|
130 | * Accept an invitation.
|
131 | *
|
132 | * @param {string} acceptCode - The accept code retrieved in the email.
|
133 | * @param {string} name - Name for the new user.
|
134 | * @param {string} password - Password for the new user.
|
135 | * @returns {Promise} The response object representing the completion or failure.
|
136 | */
|
137 | async acceptInvitation(acceptCode, name, password) {
|
138 | // send PUT w/o authentication.
|
139 | const rp = axios.create({ ...this.defaultOptions });
|
140 | return rp.put(`/invitation/accept/${acceptCode}`, {
|
141 | marketing_opt_in: false,
|
142 | name,
|
143 | password,
|
144 | }, {
|
145 | headers: {
|
146 | 'content-type': 'application/json',
|
147 | },
|
148 | });
|
149 | }
|
150 |
|
151 | /**
|
152 | * Delete an invitation.
|
153 | *
|
154 | * @see https://docs.fastly.com/api/account#invitations_d70a7460c7e1bd8dd660c6f5b3558c2e
|
155 | * @param {string} id - The invitation id.
|
156 | * @returns {Promise} The response object representing the completion or failure.
|
157 | */
|
158 | async deleteInvitation(id) {
|
159 | return this.request.delete(`/invitations/${id}`);
|
160 | }
|
161 | }
|
162 |
|
163 | module.exports = AccountAPI;
|