-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathhyperglosae.js
More file actions
134 lines (117 loc) · 3.38 KB
/
Copy pathhyperglosae.js
File metadata and controls
134 lines (117 loc) · 3.38 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const service = '/api';
function Hyperglosae(logger) {
this.getView = ({view, id, options = []}) =>
fetch(`${
service
}/_design/app/_view/${
view
}?${
id ? `startkey=["${id}"]&endkey=["${id}",{}]` : ''
}&${
options.map(x => x + '=true').join('&')
}`)
.then(x => x.json())
.then(x => x.rows);
this.getDocument = (id) =>
fetch(`${service}/${id}`)
.then(x => x.json());
this.putDocument = (doc, uri) =>
fetch(`${service}/${uri || doc._id}`, {
method: 'PUT',
body: JSON.stringify(doc)
})
.then(x => x.json())
.then(x => {
if (x.reason) {
logger(x.reason);
throw new Error(x.reason);
}
return x;
});
this.deleteDocument = ({_id, _rev}) =>
fetch(`${service}/${_id}?rev=${_rev}`, {
method: 'DELETE',
})
.then(x => x.json())
.then(x => {
if (x.reason) {
logger(x.reason);
throw new Error(x.reason);
}
return x;
});
this.getDocumentMetadata = (id) =>
fetch(`${service}/${id}`, {
method: 'HEAD',
});
this.putAttachment = (id, attachment, callback) =>
this.getDocumentMetadata(id).then(x => {
const reader = new FileReader();
reader.readAsArrayBuffer(attachment);
reader.onload = () => {
const arrayBuffer = reader.result;
fetch(`${service}/${id}/${attachment.name}`, {
method: 'PUT',
headers: {
// ETag is the header that carries the current rev.
'If-Match': x.headers.get('ETag'),
'Content-Type': attachment.type
},
body: arrayBuffer
}).then(response => callback(response));
};
});
this.deleteAttachment = (id, attachmentName, callback) =>
this.getDocumentMetadata(id).then(headRes => {
fetch(`${service}/${id}/${encodeURIComponent(attachmentName)}`, {
method: 'DELETE',
headers: {
'If-Match': headRes.headers.get('ETag'),
'Accept': 'application/json'
}
}).then(response => callback(response));
});
this.getSession = () =>
fetch(`${service}/_session`)
.then(x => x.json())
.then(x => x.userCtx?.name);
this.postSession = ({name, password}) =>
fetch(`${service}/_session`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `name=${name}&password=${encodeURIComponent(password)}`
})
.then(x => x.json())
.then(x => x.reason ? logger(x.reason) : name);
this.deleteSession = () =>
fetch(`${service}/_session`, {
method: 'DELETE'
});
this.refreshMetadata = (id, callback) => {
this.getView({view: 'metadata', id, options: ['include_docs']})
.then(
(rows) => {
let documents = rows.map(x => x.doc).filter(x => x);
callback(documents);
}
);
};
this.refreshContent = (id, callback) => {
this.getView({view: 'content', id, options: ['include_docs']})
.then(
(rows) => {
callback(rows);
},
(error) => {
console.log(error.message);
}
);
};
this.getAllDocuments = (user) =>
this.getView({view: 'all_documents', id: user || 'PUBLIC', options: ['include_docs']})
.then((rows) => rows.map(x => x.doc));
return this;
}
export default Hyperglosae;