-
Notifications
You must be signed in to change notification settings - Fork 12
/
AppsRegistryCollection.js
65 lines (55 loc) · 1.74 KB
/
AppsRegistryCollection.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
import { transformRegistryFormatToStackFormat } from 'cozy-client/dist/registry'
import DocumentCollection, { normalizeDoc } from './DocumentCollection'
import { FetchError } from './errors'
export const APPS_REGISTRY_DOCTYPE = 'io.cozy.apps_registry'
export const normalizeAppFromRegistry = (app, doctype) => {
return {
...normalizeDoc(app, doctype)
}
}
/**
* Extends `DocumentCollection` API along with specific methods for `io.cozy.apps_registry`.
*/
class AppsRegistryCollection extends DocumentCollection {
constructor(stackClient) {
super(APPS_REGISTRY_DOCTYPE, stackClient)
this.endpoint = '/registry/'
}
/**
* Fetches an app from the registry.
*
* @param {string} slug - Slug of the app
* @returns {Promise<{data: object}>} JsonAPI response containing normalized document as data attribute
* @throws {FetchError}
*/
async get(slug) {
const resp = await this.stackClient.fetchJSON(
'GET',
`${this.endpoint}${slug}`
)
// The "maintenance" keyword calls a specific route in the stack
// that returns a table of all applications under maintenance.
// We processed it independently so that it could be stored in the cache.
if (slug === 'maintenance') {
return {
data: resp.map(app => ({
_type: APPS_REGISTRY_DOCTYPE,
...app
}))
}
}
const data = transformRegistryFormatToStackFormat(resp)
return { data: normalizeAppFromRegistry(data, this.doctype) }
}
async create() {
throw new Error(
'create() method is not available for AppsRegistryCollection'
)
}
async destroy() {
throw new Error(
'destroy() method is not available for AppsRegistryCollection'
)
}
}
export default AppsRegistryCollection