mstate-cli 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,274 +1,274 @@
1
- import { MSTATE_DOMAIN, MSTATE_URL } from '../common/constant';
2
- import {
3
- customLog,
4
- getSecretKey,
5
- handleUpdateSecretKey,
6
- } from '../common/utils';
7
- import { getValueFromArgs } from '../common/helpers';
8
-
9
- import open from 'open';
10
- import express from 'express';
11
- import cors from 'cors';
12
-
13
- enum Key {
14
- PERMISSIONS = 'permissions=',
15
- SECRET_KEY = 'secret=',
16
- USER_ID = 'user=',
17
- NAME = 'name=',
18
- EMAIL = 'email=',
19
- PHONE = 'phone=',
20
- COMPANY_ID = 'company=',
21
- }
22
-
23
- class CompanyHandler {
24
- constructor() {}
25
-
26
- async addToken() {
27
- const args = process.argv;
28
-
29
- const pString = getValueFromArgs(args, Key.PERMISSIONS).trim();
30
- const permissions = pString.split(',').map((s) => s.trim());
31
- const secretKey = getSecretKey();
32
- const user = getValueFromArgs(args, Key.USER_ID);
33
- const companyID = getValueFromArgs(args, Key.COMPANY_ID);
34
-
35
- if (!permissions.length && permissions[0]?.length) {
36
- customLog.changeAndThrow(`Parameter permissions is required`);
37
- return;
38
- }
39
-
40
- if (!secretKey) {
41
- customLog.changeAndThrow(`Parameter secret is required`);
42
- return;
43
- }
44
-
45
- if (!user) {
46
- customLog.changeAndThrow(`Parameter user is required`);
47
- return;
48
- }
49
-
50
- if (!companyID) {
51
- customLog.changeAndThrow(`Parameter company is required`);
52
- return;
53
- }
54
-
55
- const url = `${MSTATE_URL}/company/permission`;
56
- const responseJSON = await fetch(url, {
57
- method: 'POST',
58
- headers: {
59
- 'secret-key': secretKey,
60
- 'Content-Type': 'application/json',
61
- },
62
- body: JSON.stringify({
63
- user,
64
- permissions,
65
- company: companyID,
66
- }),
67
- });
68
-
69
- const response = await responseJSON.json();
70
- if (response?.errors) {
71
- customLog.changeAndThrow(
72
- 'Invalid Parameters for Company: ',
73
- ...response?.errors,
74
- );
75
- } else {
76
- customLog.success('Company updated successfully \n', response?.data);
77
- }
78
- }
79
-
80
- async addUser() {
81
- const args = process.argv;
82
-
83
- const secretKey = getSecretKey();
84
- const name = getValueFromArgs(args, Key.NAME);
85
- const email = getValueFromArgs(args, Key.EMAIL);
86
- const phone = getValueFromArgs(args, Key.PHONE);
87
- const companyID = getValueFromArgs(args, Key.COMPANY_ID);
88
-
89
- if (!secretKey) {
90
- customLog.changeAndThrow(`Parameter secret is required`);
91
- return;
92
- }
93
-
94
- if (!name) {
95
- customLog.changeAndThrow(`Parameter name is required`);
96
- return;
97
- }
98
-
99
- if (!email) {
100
- customLog.changeAndThrow(`Parameter email is required`);
101
- return;
102
- }
103
-
104
- if (!phone) {
105
- customLog.changeAndThrow(`Parameter phone is required`);
106
- return;
107
- }
108
-
109
- if (!companyID) {
110
- customLog.changeAndThrow(`Parameter company is required`);
111
- return;
112
- }
113
-
114
- const url = `${MSTATE_URL}/um/user`;
115
- const responseJSON = await fetch(url, {
116
- method: 'POST',
117
- headers: {
118
- 'secret-key': secretKey,
119
- 'Content-Type': 'application/json',
120
- },
121
- body: JSON.stringify({
122
- companyNickName: companyID,
123
- name,
124
- username: name,
125
- email,
126
- password: '123',
127
- phone,
128
- }),
129
- });
130
-
131
- const response = await responseJSON.json();
132
- if (response?.errors) {
133
- customLog.changeAndThrow(
134
- 'Invalid Parameters for Company: ',
135
- ...response?.errors,
136
- );
137
- } else {
138
- customLog.success('User Updated successfully \n', response?.data);
139
- }
140
- }
141
-
142
- async revokePermission() {
143
- const args = process.argv;
144
-
145
- const pString = getValueFromArgs(args, Key.PERMISSIONS);
146
- const permissions = pString.split(',').map((s) => s.trim());
147
- const secretKey = getSecretKey();
148
- const user = getValueFromArgs(args, Key.USER_ID);
149
- const companyID = getValueFromArgs(args, Key.COMPANY_ID);
150
-
151
- if (!permissions.length && permissions[0]?.length) {
152
- customLog.changeAndThrow(`Parameter permissions is required`);
153
- return;
154
- }
155
-
156
- if (!secretKey) {
157
- customLog.changeAndThrow(`Parameter secret is required`);
158
- return;
159
- }
160
-
161
- if (!user) {
162
- customLog.changeAndThrow(`Parameter user is required`);
163
- return;
164
- }
165
-
166
- if (!companyID) {
167
- customLog.changeAndThrow(`Parameter company is required`);
168
- return;
169
- }
170
-
171
- const url = `${MSTATE_URL}/company/permission`;
172
- const responseJSON = await fetch(url, {
173
- method: 'DELETE',
174
- headers: {
175
- 'secret-key': secretKey,
176
- 'Content-Type': 'application/json',
177
- },
178
- body: JSON.stringify({
179
- company: companyID,
180
- user,
181
- permissions,
182
- }),
183
- });
184
-
185
- const response = await responseJSON.json();
186
- if (response?.errors) {
187
- customLog.changeAndThrow(
188
- 'Invalid Parameters for Company: ',
189
- ...response?.errors,
190
- );
191
- } else {
192
- customLog.success('Company updated successfully \n', response?.data);
193
- }
194
- }
195
-
196
- async handleLogin() {
197
- // Start the login process
198
- let port = 49152;
199
- const app = express();
200
- app.use(cors({ origin: '*' }));
201
-
202
- async function login() {
203
- console.log('Opening browser for authentication...');
204
-
205
- // Start a local server to capture the token
206
-
207
- const startServer = (port: number) => {
208
- return app.listen(port, () => {
209
- console.log(`Waiting for browser response...`);
210
- });
211
- };
212
-
213
- const server = startServer(port);
214
-
215
- server.on('error', (err: any) => {
216
- if (err.code === 'EADDRINUSE') {
217
- console.log(`Port ${port} is in use. Trying another port...`);
218
-
219
- startServer(++port);
220
- } else {
221
- console.error('Server error:', err);
222
- }
223
- });
224
-
225
- // Capture the OAuth callback
226
- app.get('/cli/mstate/login', (req, res) => {
227
- try {
228
- const { token } = req.query;
229
-
230
- if (token) {
231
- console.log('Authentication successful!');
232
-
233
- // Save the token locally
234
- handleUpdateSecretKey(token as string);
235
-
236
- res.send('<h1>Login successful! You can close this window.</h1>');
237
- } else {
238
- console.error('Authentication failed.');
239
- res.send('<h1>Authentication failed. Please try again.</h1>');
240
- }
241
- } catch (error) {
242
- } finally {
243
- server.close(); // Stop the server after handling the response
244
- }
245
- });
246
- }
247
-
248
- // Command handling
249
- login()
250
- .then(async () => {
251
- await open(
252
- MSTATE_DOMAIN +
253
- `oauth?redirect=${encodeURIComponent(
254
- 'cli/mstate/login',
255
- )}&open=${port}&scope=profile`,
256
- );
257
- })
258
- .catch((err) => {
259
- console.error('Error during login:', err.message);
260
- process.exit(1);
261
- });
262
- }
263
-
264
- handleUpdateSavedSecretKey() {
265
- handleUpdateSecretKey(getSecretKey());
266
- }
267
-
268
- logToken() {
269
- customLog.info('Token', getSecretKey());
270
- customLog.info('MSTATE_URL', MSTATE_URL);
271
- }
272
- }
273
-
274
- export const companyHandler = new CompanyHandler();
1
+ import { MSTATE_DOMAIN, MSTATE_URL } from '../common/constant';
2
+ import {
3
+ customLog,
4
+ getSecretKey,
5
+ handleUpdateSecretKey,
6
+ } from '../common/utils';
7
+ import { getValueFromArgs } from '../common/helpers';
8
+
9
+ import open from 'open';
10
+ import express from 'express';
11
+ import cors from 'cors';
12
+
13
+ enum Key {
14
+ PERMISSIONS = 'permissions=',
15
+ SECRET_KEY = 'secret=',
16
+ USER_ID = 'user=',
17
+ NAME = 'name=',
18
+ EMAIL = 'email=',
19
+ PHONE = 'phone=',
20
+ COMPANY_ID = 'company=',
21
+ }
22
+
23
+ class CompanyHandler {
24
+ constructor() {}
25
+
26
+ async addToken() {
27
+ const args = process.argv;
28
+
29
+ const pString = getValueFromArgs(args, Key.PERMISSIONS).trim();
30
+ const permissions = pString.split(',').map((s) => s.trim());
31
+ const secretKey = getSecretKey();
32
+ const user = getValueFromArgs(args, Key.USER_ID);
33
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
34
+
35
+ if (!permissions.length && permissions[0]?.length) {
36
+ customLog.changeAndThrow(`Parameter permissions is required`);
37
+ return;
38
+ }
39
+
40
+ if (!secretKey) {
41
+ customLog.changeAndThrow(`Parameter secret is required`);
42
+ return;
43
+ }
44
+
45
+ if (!user) {
46
+ customLog.changeAndThrow(`Parameter user is required`);
47
+ return;
48
+ }
49
+
50
+ if (!companyID) {
51
+ customLog.changeAndThrow(`Parameter company is required`);
52
+ return;
53
+ }
54
+
55
+ const url = `${MSTATE_URL}/company/permission`;
56
+ const responseJSON = await fetch(url, {
57
+ method: 'POST',
58
+ headers: {
59
+ 'secret-key': secretKey,
60
+ 'Content-Type': 'application/json',
61
+ },
62
+ body: JSON.stringify({
63
+ user,
64
+ permissions,
65
+ company: companyID,
66
+ }),
67
+ });
68
+
69
+ const response = await responseJSON.json();
70
+ if (response?.errors) {
71
+ customLog.changeAndThrow(
72
+ 'Invalid Parameters for Company: ',
73
+ ...response?.errors,
74
+ );
75
+ } else {
76
+ customLog.success('Company updated successfully \n', response?.data);
77
+ }
78
+ }
79
+
80
+ async addUser() {
81
+ const args = process.argv;
82
+
83
+ const secretKey = getSecretKey();
84
+ const name = getValueFromArgs(args, Key.NAME);
85
+ const email = getValueFromArgs(args, Key.EMAIL);
86
+ const phone = getValueFromArgs(args, Key.PHONE);
87
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
88
+
89
+ if (!secretKey) {
90
+ customLog.changeAndThrow(`Parameter secret is required`);
91
+ return;
92
+ }
93
+
94
+ if (!name) {
95
+ customLog.changeAndThrow(`Parameter name is required`);
96
+ return;
97
+ }
98
+
99
+ if (!email) {
100
+ customLog.changeAndThrow(`Parameter email is required`);
101
+ return;
102
+ }
103
+
104
+ if (!phone) {
105
+ customLog.changeAndThrow(`Parameter phone is required`);
106
+ return;
107
+ }
108
+
109
+ if (!companyID) {
110
+ customLog.changeAndThrow(`Parameter company is required`);
111
+ return;
112
+ }
113
+
114
+ const url = `${MSTATE_URL}/um/user`;
115
+ const responseJSON = await fetch(url, {
116
+ method: 'POST',
117
+ headers: {
118
+ 'secret-key': secretKey,
119
+ 'Content-Type': 'application/json',
120
+ },
121
+ body: JSON.stringify({
122
+ companyNickName: companyID,
123
+ name,
124
+ username: name,
125
+ email,
126
+ password: '123',
127
+ phone,
128
+ }),
129
+ });
130
+
131
+ const response = await responseJSON.json();
132
+ if (response?.errors) {
133
+ customLog.changeAndThrow(
134
+ 'Invalid Parameters for Company: ',
135
+ ...response?.errors,
136
+ );
137
+ } else {
138
+ customLog.success('User Updated successfully \n', response?.data);
139
+ }
140
+ }
141
+
142
+ async revokePermission() {
143
+ const args = process.argv;
144
+
145
+ const pString = getValueFromArgs(args, Key.PERMISSIONS);
146
+ const permissions = pString.split(',').map((s) => s.trim());
147
+ const secretKey = getSecretKey();
148
+ const user = getValueFromArgs(args, Key.USER_ID);
149
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
150
+
151
+ if (!permissions.length && permissions[0]?.length) {
152
+ customLog.changeAndThrow(`Parameter permissions is required`);
153
+ return;
154
+ }
155
+
156
+ if (!secretKey) {
157
+ customLog.changeAndThrow(`Parameter secret is required`);
158
+ return;
159
+ }
160
+
161
+ if (!user) {
162
+ customLog.changeAndThrow(`Parameter user is required`);
163
+ return;
164
+ }
165
+
166
+ if (!companyID) {
167
+ customLog.changeAndThrow(`Parameter company is required`);
168
+ return;
169
+ }
170
+
171
+ const url = `${MSTATE_URL}/company/permission`;
172
+ const responseJSON = await fetch(url, {
173
+ method: 'DELETE',
174
+ headers: {
175
+ 'secret-key': secretKey,
176
+ 'Content-Type': 'application/json',
177
+ },
178
+ body: JSON.stringify({
179
+ company: companyID,
180
+ user,
181
+ permissions,
182
+ }),
183
+ });
184
+
185
+ const response = await responseJSON.json();
186
+ if (response?.errors) {
187
+ customLog.changeAndThrow(
188
+ 'Invalid Parameters for Company: ',
189
+ ...response?.errors,
190
+ );
191
+ } else {
192
+ customLog.success('Company updated successfully \n', response?.data);
193
+ }
194
+ }
195
+
196
+ async handleLogin() {
197
+ // Start the login process
198
+ let port = 49152;
199
+ const app = express();
200
+ app.use(cors({ origin: '*' }));
201
+
202
+ async function login() {
203
+ console.log('Opening browser for authentication...');
204
+
205
+ // Start a local server to capture the token
206
+
207
+ const startServer = (port: number) => {
208
+ return app.listen(port, () => {
209
+ console.log(`Waiting for browser response...`);
210
+ });
211
+ };
212
+
213
+ const server = startServer(port);
214
+
215
+ server.on('error', (err: any) => {
216
+ if (err.code === 'EADDRINUSE') {
217
+ console.log(`Port ${port} is in use. Trying another port...`);
218
+
219
+ startServer(++port);
220
+ } else {
221
+ console.error('Server error:', err);
222
+ }
223
+ });
224
+
225
+ // Capture the OAuth callback
226
+ app.get('/cli/mstate/login', (req, res) => {
227
+ try {
228
+ const { token } = req.query;
229
+
230
+ if (token) {
231
+ console.log('Authentication successful!');
232
+
233
+ // Save the token locally
234
+ handleUpdateSecretKey(token as string);
235
+
236
+ res.send('<h1>Login successful! You can close this window.</h1>');
237
+ } else {
238
+ console.error('Authentication failed.');
239
+ res.send('<h1>Authentication failed. Please try again.</h1>');
240
+ }
241
+ } catch (error) {
242
+ } finally {
243
+ server.close(); // Stop the server after handling the response
244
+ }
245
+ });
246
+ }
247
+
248
+ // Command handling
249
+ login()
250
+ .then(async () => {
251
+ await open(
252
+ MSTATE_DOMAIN +
253
+ `/oauth?redirect=${encodeURIComponent(
254
+ 'cli/mstate/login',
255
+ )}&open=${port}&scope=profile`,
256
+ );
257
+ })
258
+ .catch((err) => {
259
+ console.error('Error during login:', err.message);
260
+ process.exit(1);
261
+ });
262
+ }
263
+
264
+ handleUpdateSavedSecretKey() {
265
+ handleUpdateSecretKey(getSecretKey());
266
+ }
267
+
268
+ logToken() {
269
+ customLog.info('Token', getSecretKey());
270
+ customLog.info('MSTATE_URL', MSTATE_URL);
271
+ }
272
+ }
273
+
274
+ export const companyHandler = new CompanyHandler();