| 16 | return v |
| 17 | }) |
| 18 | class CloudDBAdapter { |
| 19 | constructor(url, { serialize = stringify, deserialize = parse, fetchOptions = {} } = {}) { |
| 20 | this.url = url |
| 21 | this.serialize = serialize |
| 22 | this.deserialize = deserialize |
| 23 | this.fetchOptions = fetchOptions |
| 24 | } |
| 25 | |
| 26 | async read() { |
| 27 | try { |
| 28 | let res = await got(this.url, { |
| 29 | method: 'GET', |
| 30 | headers: { |
| 31 | Accept: 'application/json;q=0.9,text/plain', |
| 32 | }, |
| 33 | ...this.fetchOptions, |
| 34 | }) |
| 35 | if (res.statusCode !== 200) throw res.statusMessage |
| 36 | return this.deserialize(res.body) |
| 37 | } catch (e) { |
| 38 | return null |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | async write(obj) { |
| 43 | let res = await got(this.url, { |
| 44 | method: 'POST', |
| 45 | headers: { |
| 46 | 'Content-Type': 'application/json', |
| 47 | }, |
| 48 | ...this.fetchOptions, |
| 49 | body: this.serialize(obj), |
| 50 | }) |
| 51 | if (res.statusCode !== 200) throw res.statusMessage |
| 52 | return res.body |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export default CloudDBAdapter |
nothing calls this directly
no outgoing calls
no test coverage detected