-
Notifications
You must be signed in to change notification settings - Fork 2
/
oa.js
115 lines (96 loc) · 4.05 KB
/
oa.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
import util from './src/util.js';
import { local_schema } from './src/util.js';
import run from './src/run.js';
import inquire from 'inquirer';
/**
* @class
* @param {Object} api Global API Settings Object
* @param {string} api.url URL of OA API instance to interact with
* @param {string} api.username OpenAddresses Username
* @param {string} api.password OpenAddresses Password
* @param {string} api.secret OpenAddresses SharedSecret
* @param {string} api.token OpenAddresses Token
*/
export default class OA {
constructor(api = {}) {
this.url = api.url ? new URL(api.url).toString() : 'https://batch.openaddresses.io';
this.user = {
username: api.username ? api.username : process.env.OA_USERNAME,
password: api.password ? api.password : process.env.OA_PASSWORD,
token: api.token ? api.token : process.env.OA_TOKEN,
secret: api.secret ? api.secret : process.env.OA_SECRET
};
this.schema = local_schema();
this.argv = api;
}
/**
* Run an OpenAddresses Command
*
* @param {String} cmd - Command to run
* @param {String} subcmd - Subcommand to run
*
* @param {Object} defaults - Optional API Payload Defaults
* @param {Object} opts - Options
* @param {Boolean} [opts.stream=false] - Return a streamable response
*/
async cmd(cmd, subcmd, defaults = {}, opts = {}) {
if (process.env.UPDATE) this.schema = await util(this.url);
if (!this.schema.cli[cmd]) throw new Error('Command Not Found');
if (!this.schema.cli[cmd].cmds[subcmd]) throw new Error('Subcommand Not Found');
if (!this.schema.schema[this.schema.cli[cmd].cmds[subcmd]]) throw new Error('API not found for Subcommand');
const payload = {};
let url = this.schema.cli[cmd].cmds[subcmd];
const matches = url.match(/:[a-z]+/g);
if (matches) {
for (const match of matches) {
if (this.argv.cli && !this.argv.script && matches.length) {
const res = await inquire.prompt([{
name: match,
message: `${match} to fetch`,
type: 'string',
required: 'true',
default: defaults[match]
}]);
payload[match] = res[match];
} else {
payload[match] = defaults[match];
}
if (!payload[match]) throw new Error(`"${match}" is required in body`);
url = url.replace(match, payload[match]);
delete payload[match];
}
}
const schema = this.schema.schema[this.schema.cli[cmd].cmds[subcmd]];
if (schema.body) {
const body = (await util(this.url, ...this.schema.cli[cmd].cmds[subcmd].split(' '))).body;
for (const prop of Object.keys(body.properties)) {
const p = body.properties[prop];
if (this.argv.cli && !this.argv.script) {
const ask = {
name: prop,
message: `${prop} to populate`,
type: p.type,
required: 'true'
};
if (p.enum) {
ask.type = 'list';
ask.choices = p.enum;
}
const res = await inquire.prompt([ask]);
payload[prop] = res[prop];
} else if (defaults[prop] !== undefined) {
payload[prop] = defaults[prop];
}
}
}
if (schema.query) {
const query = (await util(this.url, ...this.schema.cli[cmd].cmds[subcmd].split(' '))).query;
for (const prop of Object.keys(query.properties)) {
if (defaults[prop] !== undefined) {
payload[prop] = defaults[prop];
}
}
}
return await run(this, schema, url, payload, opts);
}
}