-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
list.js
70 lines (60 loc) · 1.61 KB
/
list.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
'use strict';
const url = require('url');
const fetch = require('node-fetch');
const utils = require('./utils.js');
const config = require('./config.js');
/** @typedef {import('./view.js')} View */
/**
* List constructor. Create an instance of a list associated with a couchdb list in the npm registry.
*
* ```js
* let list = new List('dependedUpon', view);
* ```
*
* @param {String} `name` Name of couchdb list to use.
* @param {View} `view` Instance of a View to use with the list.
* @returns {List} instance of `List`
* @name List
* @api public
*/
class List {
constructor(name, view) {
this.name = name;
this.view = view;
this.config = utils.clone(config);
this.config.pathname += '/_list/' + this.view.name + '/' + this.name;
}
/**
* Query the couchdb list with the provided parameters.
*
* ```js
* let results = await list.query({ key: JSON.stringify(['micromatch']) })
* ```
* @param {Object} `params` URL query parameters to pass along to the couchdb list.
* @return {Promise} Results of the query when promise is resolved.
* @name .query
* @api public
*/
async query(params = {}) {
const response = await fetch(this.url(params));
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
}
/**
* Build a formatted url with the provided parameters.
*
* @param {Object} `query` URL query parameters.
* @return {String} formatted url string
* @name .url
* @api public
*/
url(query = {}) {
return url.format({ ...this.config, query });
}
}
/**
* Exposes `List`
*/
module.exports = List;