This repository was archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodigaToken.js
More file actions
162 lines (157 loc) · 4.45 KB
/
Copy pathcodigaToken.js
File metadata and controls
162 lines (157 loc) · 4.45 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import inquirer from "inquirer";
import { CHECK_USER } from "../graphql/queries";
import { isTestMode, SAMPLE_TOKEN, TEST_USER } from "../tests/test-utils";
import { codigaApiFetch } from "../utils/api";
import { ACTION_TOKEN_ADD } from "../utils/constants";
import {
printSuggestion,
printFailure,
printInfo,
printSuccess,
printCommandSuggestion,
setPrintToStdErr,
setPrintToStdOut,
} from "../utils/print";
import { setToken, getToken, deleteToken, store } from "../utils/store";
/**
* Returns the user details for a valid API token
* @param {string} apiToken the token string to check
* @returns {Promise<{id: string, accountType: string, username: string}?>} the user if a valid token
*/
export async function getUserInfoFromToken(apiToken) {
// handle test case
if (isTestMode && apiToken === SAMPLE_TOKEN) {
return TEST_USER;
}
// complete the real action
try {
const resp = await codigaApiFetch(CHECK_USER, null, apiToken);
if (resp?.user?.id) {
return resp.user;
} else {
return null;
}
} catch (err) {
return null;
}
}
/**
* Handles the checking and messaging when checking a
* token with the `token-check` command action
*/
export async function checkCodigaToken() {
const token = getToken();
if (token) {
const user = await getUserInfoFromToken(token);
if (!!user) {
printSuccess(
`A valid API token was found for ${user.username} (${user.accountType})`
);
printSuggestion(`Tokens can be found here:`, store.path);
printCommandSuggestion(
" ↳ If you wish to override it, run one of the following commands:",
ACTION_TOKEN_ADD
);
process.exit(0);
} else {
setPrintToStdErr();
printInfo("An invalid token was found");
printSuggestion(`Tokens can be found here:`, store.path);
printCommandSuggestion(
" ↳ To override it, run one of the following commands:",
ACTION_TOKEN_ADD
);
setPrintToStdOut();
process.exit(1);
}
} else {
setPrintToStdErr();
printInfo("No token was found.");
printCommandSuggestion(
" ↳ To set an API token, run one of the following commands:",
ACTION_TOKEN_ADD
);
setPrintToStdOut();
process.exit(1);
}
}
/**
* Handles the answers given for the token-add command
* @param {{apiToken: string}} params
*/
export async function handleAnswers({ apiToken }) {
const user = await getUserInfoFromToken(apiToken);
if (!!user) {
setToken(apiToken);
printSuccess(
`Codiga API token added for ${user.username} (${user.accountType})`
);
printSuggestion(`Tokens can be found here:`, store.path);
} else {
printFailure("That token is not valid");
printSuggestion(
" ↳ Go to Codiga and create a new token:",
"https://app.codiga.io/api-tokens"
);
}
}
/**
* Handles asking, verifying, setting and the messaging
* when the `token-add` command action is run
*/
export async function addCodigaToken() {
printSuggestion(
"Create a Codiga API token:",
"https://app.codiga.io/api-tokens"
);
await inquirer
.prompt([
{
type: "input",
name: "apiToken",
message: "Please enter your API token:",
},
])
.then(async ({ apiToken }) => {
await handleAnswers({ apiToken });
process.exit(0);
});
}
/**
* Handles deleting a token, if there is one, and the messaging
* when the `token-delete` command action is run
*/
export async function deleteCodigaToken() {
if (getToken()) {
deleteToken();
// ensure the token was deleted
if (getToken()) {
setPrintToStdErr();
printFailure("We couldn't delete your Codiga API token");
printSuggestion(`Tokens can be found here:`, store.path);
printSuggestion(
" ↳ If the issue persists, contact us at:",
"https://app.codiga.io/support"
);
setPrintToStdOut();
process.exit(1);
} else {
printSuccess("Codiga API token deleted");
printSuggestion(`Tokens can be found here:`, store.path);
printCommandSuggestion(
" ↳ To set a Codiga API token, run one of the following commands:",
ACTION_TOKEN_ADD
);
process.exit(0);
}
} else {
setPrintToStdErr();
printInfo("No Codiga API token was found to delete");
printCommandSuggestion(
" ↳ To set a Codiga API token, run one of the following commands:",
ACTION_TOKEN_ADD
);
setPrintToStdOut();
process.exit(1);
}
}