-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-estate.ts
67 lines (58 loc) · 1.82 KB
/
fetch-estate.ts
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
import { Argv } from 'yargs';
import { command as parentCommand, FlowFactFlags } from '../flowfact';
import { BasicAuth, TokenAuth } from '../../classes/Authorization';
import {
storeResponse,
generateOutputName,
loadDictionary,
} from '../../utils/cli-tools';
import { Logger } from '../../utils';
import { GlobalFlags, fetchOptions } from '../../cli';
import { FlowFactV1 } from '../../classes/portals/FlowFact/v1/Aggregator';
import { FlowFactV2 } from '../../classes/portals/FlowFact/v2/Aggregator';
export const command = 'fetch-estate <estate-id>';
export const aliases = ['fe'];
const usage = `
$0 ${parentCommand} ${command} [args]
`;
interface Arguments extends GlobalFlags, FlowFactFlags {
id: string;
}
exports.builder = (yargs: Argv) =>
yargs
.group(Object.keys(fetchOptions), 'Fetch options')
.options(fetchOptions)
.usage(usage)
.positional('estate-id', { alias: ['id'], type: 'string' });
exports.handler = async (argv: Arguments) => {
try {
const flowFact = argv.apiV1
? new FlowFactV1(argv as BasicAuth)
: new FlowFactV2(argv as TokenAuth);
let result;
if (!argv.normalize) {
result = await flowFact.fetchResult(argv.id);
} else {
result = await flowFact.fetchEstate(argv.id);
result = result.getProperties(
argv.detailed,
loadDictionary(argv.dictionary)
);
}
const name = generateOutputName(
parentCommand,
command.replace(' <estate-id>', ''),
argv.apiV1 ? 'v1' : 'v2',
argv.normalize ? 'normalized' : 'original',
argv.detailed ? 'long' : 'short'
);
if (argv.storeResult) {
const fileName = storeResponse(name, result, argv.pretty);
Logger.log(`Result stored at "${fileName}"`);
} else {
Logger.logJSON(result);
}
} catch (error) {
Logger.error(error);
}
};