-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
38 lines (32 loc) · 859 Bytes
/
api.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
'use strict';
const React = require('react');
const shell = require('shelljs');
const { useState, useEffect } = React;
const callDynamo = command =>
new Promise((resolve, reject) =>
shell.exec(
`aws dynamodb ${command} --output json`,
{ silent: true },
(code, stdout, stderr) => {
if (code) {
reject(stderr);
} else resolve(JSON.parse(stdout));
}
)
);
const useDynamo = (command, { skip } = {}) => {
const [{ loading, error, data }, setResult] = useState({ loading: true });
useEffect(() => {
if (!skip) {
setResult({ loading: true });
callDynamo(command)
.then(data => setResult({ data }))
.catch(error_ => setResult({ error: error_ }));
}
}, [command, skip]);
return { loading, error, data };
};
module.exports = {
useDynamo,
callDynamo,
};