-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.ts
executable file
·127 lines (106 loc) · 2.96 KB
/
get.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
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
import { flags } from '@oclif/command';
import Command from '../BaseCommand';
import { json, shell } from '../highlight';
import {
OADAifiedJsonArray,
OADAifiedJsonObject,
OADAifiedJsonValue,
oadaify,
_meta,
_id,
} from '@oada/oadaify';
import { output, expandPath } from '../io';
import getConn from '../connections';
/**
* @todo why does TS need this?
*/
function isArray(
oadaified: OADAifiedJsonArray | OADAifiedJsonObject
): oadaified is OADAifiedJsonArray {
return Array.isArray(oadaified);
}
const examples = [
`${shell`$ oada get /bookmarks`}
${json`{
"_id": "resources/default:resources_bookmarks_321",
"_rev": 45,
"_type": "application/vnd.oada.bookmarks.1+json",
"_meta": {
"_id": "resources/default:resources_bookmarks_321/_meta",
"_rev": 45
},
"foo": "bar",
"baz": 700
}`}`,
`${shell`$ oada get /bookmarks/*`}
${json`"bar"`}
${json`700`}`,
];
/**
* OADA GET
*/
export default class Get extends Command {
static description = 'perform an OADA GET (read)';
static aliases = ['g', 'GET'];
static examples = examples;
static flags = {
...Command.flags,
recursive: flags.boolean({ char: 'R', default: false }),
meta: flags.boolean({ char: 'm', default: false }),
out: flags.string({ char: 'o', default: '-' }),
};
static args = [
{ name: 'paths...', required: true, description: 'OADA path(s) to GET' },
];
static strict = false;
async run() {
const {
argv: paths,
flags: { out, meta },
} = this.parse(Get);
const conn = getConn(this.iconfig);
await output(
out,
async function* () {
for (const p of paths) {
const pp = expandPath(conn, p);
for await (const path of pp) {
const { data } = await conn.get({ path });
const oadaified = oadaify(data);
if (meta) {
await getMeta(oadaified);
async function getMeta(
oadaified: OADAifiedJsonValue
): Promise<OADAifiedJsonValue> {
if (!oadaified || typeof oadaified !== 'object') {
return oadaified;
}
if (isArray(oadaified)) {
return Promise.all(oadaified.map(getMeta));
}
for (const key in oadaified) {
oadaified[key] = await getMeta(oadaified[key]);
}
// Check for "empty" meta ?
const meta = oadaified[_meta] as
| OADAifiedJsonObject
| undefined;
if (meta) {
// Fetch meta?
const { data } = await conn.get({
path: meta[_id] as string,
});
// Fill it in
oadaified[_meta] = oadaify(data);
}
return oadaified;
}
}
yield oadaified;
}
}
},
this.iconfig
);
}
}