-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhelpers.js
More file actions
138 lines (124 loc) · 3.5 KB
/
Copy pathhelpers.js
File metadata and controls
138 lines (124 loc) · 3.5 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
const readlineSync = require('readline-sync');
const chalk = require('chalk');
const url = require('url');
const APIs = require('./paystack/apis')
const axios = require('axios')
function prompt(question, mute = false) {
return readlineSync.question(question, { hideEchoBack: mute })
}
const promiseWrapper = promise => (
promise
.then(data => ([null, data]))
.catch(error => ([error]))
);
function jsonLog(json) {
infoLog(JSON.stringify(json, null, 2));
}
function successLog(error) {
const eLog = chalk.green(error)
console.log(eLog)
}
function errorLog(error) {
const eLog = chalk.red(error)
console.log(eLog)
}
function isJson(val) {
return val instanceof Array || val instanceof Object ? true : false;
}
function loader() {
let progress = require('progressbar').create().step('. .')
return new Promise((resolve, reject) => {
[
() => progress.setTotal(3),
() => progress.addTick(),
() => progress.addTick(),
() => progress.addTick(),
() => progress.finish() // remove and destroy the progress bar
].forEach(function (step, index) {
setTimeout(step, index * 1000)
})
resolve('hello');
})
}
function infoLog(error) {
const eLog = chalk.blueBright(error)
console.log(eLog)
}
function parseURL(uri) {
if (!uri.startsWith('http')) uri = 'http://' + uri
return url.parse(uri)
}
function findSchema(command, args) {
let schema;
APIs[command].forEach((f) => {
if (f.api == args.command) {
schema = f;
}
})
return schema;
}
function getKeys(token, type = 'secret', domain = 'test') {
return new Promise((resolve, reject) => {
axios.get('https://api.paystack.co/integration/keys', { headers: { Authorization: 'Bearer ' + token, 'jwt-auth': true } }).then(response => {
let key = {};
let keys = response.data.data;
if (keys.length) {
for (let i = 0; i < keys.length; i++) {
if (keys[i].domain === domain && keys[i].type === type) {
key = keys[i]
break
}
}
}
resolve(key.key)
}).catch(error => {
if (error.response) {
reject(error.response.data.message)
return
}
reject(error)
})
})
}
async function executeSchema(schema, args) {
let domain = 'test';
if (args.options.domain) {
domain = args.options.domain
}
let token = db.read('token');
let key = await getKeys(token, 'secret', domain);
let instance = axios.create({
baseURL: 'https://api.paystack.co',
timeout: 3000,
headers: { 'Authorization': 'Bearer ' + key }
});
return new Promise((resolve, reject) => {
let query, data;
if (schema.method == 'GET') query = args.options;
if (schema.method == 'POST') data = args.options;
if (schema.endpoint.indexOf('{')) {
let path = schema.endpoint.slice(schema.endpoint.indexOf('{') + 1, schema.endpoint.indexOf('}'))
schema.endpoint = schema.endpoint.replace('{' + path + '}', args.options[path]);
}
instance({
url: schema.endpoint,
method: schema.method,
query,
data
}).then((resp) => {
resolve(resp.data)
}).catch((err) => {
reject(err.response.data.message)
})
})
}
function getDescription(section, title) {
let desc = ''
section.forEach((f) => {
desc = desc + ', ' + f.api
})
desc = desc + ' ' + title;
desc = desc.slice(1);
return desc;
}
module.exports = { prompt, promiseWrapper, successLog, jsonLog , errorLog, infoLog, isJson, loader, parseURL, findSchema, executeSchema, getDescription}