This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
63 lines (46 loc) · 1.49 KB
/
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
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
const fetch = require('node-fetch')
const querystring = require('querystring')
const utils = require('./utils')
const { trimSlashes } = utils
class Cockpit {
constructor(host, token, headers = {}) {
this.host = host
this.token = token
this.headers = headers
this.params = querystring.stringify({ token, })
}
getApiUrl(path) {
return `${trimSlashes(this.host.trim())}/${trimSlashes(path.trim())}?${this.params}`
}
async makeRequest(path) {
const url = this.getApiUrl(path)
const response = await fetch(url, { method: 'get', headers: this.headers })
return await response.json()
}
async assets() {
const { assets: entries } = await this.makeRequest('/api/cockpit/assets')
return { entries }
}
async listCollections() {
return await this.makeRequest('/api/collections/listCollections')
}
async collection(collection) {
return await this.makeRequest(`/api/collections/collection/${collection}`)
}
async collectionEntries(collection) {
return await this.makeRequest(`/api/collections/get/${collection}`)
}
async listSingletons() {
return await this.makeRequest('/api/singletons/listSingletons')
}
async singleton(singleton) {
return await this.makeRequest(`/api/singletons/singleton/${singleton}`)
}
async singletonEntries(singleton) {
return await {
...(await this.singleton(singleton)),
entries: [await this.makeRequest(`/api/singletons/get/${singleton}`)],
}
}
}
module.exports = Cockpit;