-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
93 lines (75 loc) · 2.3 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const qs = require(`query-string`)
const fetch = require('isomorphic-fetch')
const defaultOptions = {
mode: 'cors',
cache: 'default'
}
class Cockpit {
constructor ({ host, accessToken }) {
this.host = host
this.accessToken = accessToken
this.queryParams = {
token: this.accessToken
}
}
// @param {string} apiPath
// @param {object} options
async fetchData (apiPath, options) {
const requestInit = {
...options,
...defaultOptions
}
const hostWithToken = `${this.host}${apiPath}?${qs.stringify(this.queryParams)}`
const response = await fetch(hostWithToken, requestInit)
const json = await response.json()
return json
}
// @param {string} apiPath
async fetchDataText (apiPath, options, additionalOptions) {
const requestInit = {
...options,
...defaultOptions
}
const hostWithToken = `${this.host}${apiPath}?${qs.stringify(this.queryParams)}&${qs.stringify(additionalOptions)}`
const response = await fetch(hostWithToken, requestInit)
const result = await response.text()
return result
}
// @param {string} collection
async collectionSchema (collection) {
return this.fetchData(`/api/collections/collection/${collection}`, { method: 'GET' })
}
// @param {string} collection
async collectionEntries (collection) {
return this.fetchData(`/api/collections/get/${collection}`, { method: 'GET' })
}
// @param {string} collection
// @param {Request} options
async collectionEntriesFiltered (collection, options) {
return this.fetchData(`/api/collections/get/${collection}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
}
// @param {string} region
async regionRenderedTemplate (region) {
return this.fetchData(`/api/regions/get/${region}`, { method: 'GET' })
}
// @param {string} region
async regionFormData (region) {
return this.fetchData(`/api/regions/data/${region}`, { method: 'GET' })
}
async assets () {
return this.fetchData(`/api/cockpit/assets`, { method: 'GET' })
}
async image (assetId, {width, height}) {
return this.fetchDataText(`/api/cockpit/image`, { method: 'GET' }, {
src: assetId,
w: width,
h: height,
d: 1
})
}
}
exports.Cockpit = Cockpit