-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.js
60 lines (54 loc) · 1.53 KB
/
repo.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
import WBEdit from 'wikibase-edit'
import { WBK } from 'wikibase-sdk'
export default class WikibaseRepository {
constructor (origin, opts = {}) {
this.origin = origin
this.read = new WBK({
instance: origin
})
if (opts.oauth) {
this.edit = new WBEdit({
instance: origin,
credentials: {
oauth: opts.oauth
}
})
}
}
getContentLanguages () {
return fetch(`${this.origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json`)
.then(r => r.json())
.then(body => Object.keys(body.query.wbcontentlanguages))
}
createEntities (...entities) {
if (!this.edit) {
return Promise.reject(new Error('Cannot edit a read only instance.'))
}
return Promise.allSettled(entities.map(async entity => {
return this.edit.entity.create(entity)
}))
.then((results) => {
for (const result of results) {
if (result.status === 'rejected') {
throw new Error(result.reason)
}
}
return results.map(v => v.value)
})
}
getEntities (...identifiers) {
return Promise.all(identifiers.map(async identifier => {
const [entityId, revision] = identifier.split('@')
const url = revision
? await this.read.getEntityRevision({
id: entityId,
revision
})
: await this.read.getEntities({
ids: [entityId]
})
const { entities } = await fetch(url).then(res => res.json())
return entities[entityId]
}))
}
}