-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
155 lines (148 loc) · 3.96 KB
/
script.js
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
#!/usr/bin/node
const axios = require('axios');
var chalk = require('chalk');
var inquirer = require('inquirer');
const readline = require('readline');
const urls = require('./urls');
var fs = require('fs');
const printf = console.log;
const green = chalk.green;
const red = chalk.red;
async function initProcess() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(chalk.green('Please enter Access Token : '), (accessToken) => {
const apiUrl = `${urls.baseUrl}${urls.resource.auth.getToken}`;
axios
.post(apiUrl, {
accessToken,
})
.then((response) => {
const authToken = response.data.data.authToken;
try {
fs.writeFileSync('token.json', JSON.stringify({ authToken }));
} catch (err) {
printf(red(err));
}
printf(green('Authentication Successfull.'));
})
.catch((err) => {
printf(
red('Authentication Failed ! Please enter correct Access Token.')
);
});
rl.close();
});
}
function constructProjectsFormatting(projects) {
var choices = [];
projects.forEach(function (project, index) {
var choice = {
name: `${project.id} ----- ${project.title}`,
short: '',
};
choices.push(choice);
new inquirer.Separator();
});
return choices;
}
async function projectListing() {
if (fs.existsSync('token.json')) {
const { authToken } = JSON.parse(fs.readFileSync('token.json'));
var axios = require('axios');
var config = {
method: 'get',
url: `${urls.baseUrl}${urls.resource.project.listing}`,
headers: {
Authorization: `Bearer ${authToken}`,
},
};
axios(config)
.then((response) => {
if (response.data.data.length > 0) {
const projects = response.data.data;
const choices = constructProjectsFormatting(projects);
inquirer
.prompt([
{
type: 'list',
name: 'url',
message: 'Assigned Project',
choices: choices,
pageSize: 20,
},
])
.then((answers) => {
printf('this is the answers', answers);
})
.catch((err) => {
printf(err);
});
} else {
console.log('No Projects Assigned to you.');
}
})
.catch((err) => {
if (err.response.status === 401) {
printf(red('Error : Please authenticate yourself !'));
} else {
printf(red('Error !', err));
}
});
} else {
printf(red('Please authenticate yourself first !'));
}
}
function constructChoices(posts) {
var choices = [];
var space = ' ',
separator = '- ';
posts.forEach(function (post, index) {
var choice = {
name: `${post.id} ----- ${post.title}`,
value: `https://jsonplaceholder.typicode.com/posts/${post.id}`,
};
choices.push(choice);
new inquirer.Separator();
});
return choices;
}
async function listAllData(pageSize) {
let posts = null;
const response = await axios.get(
'https://jsonplaceholder.typicode.com/posts'
);
var choices = constructChoices(response.data);
// var open = options.open || false;
inquirer
.prompt([
{
type: 'list',
name: 'url',
message: 'Select the Post to read :',
choices: choices,
pageSize,
},
])
.then((answers) => {
printf(answers);
})
.catch((err) => {
printf(err);
});
}
async function PostDetail(uuid) {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/posts/${uuid}`
);
const { title, body, id } = response.data;
const output = `
${chalk.green.bold('Id')} : ${id}
${chalk.green.bold('Title')} : ${title}
${chalk.green.bold('Body')} : ${body}
`;
console.log(output);
}
module.exports = { listAllData, PostDetail, initProcess, projectListing };