-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRocDocument.ts
More file actions
193 lines (170 loc) · 5.2 KB
/
RocDocument.ts
File metadata and controls
193 lines (170 loc) · 5.2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { AxiosInstance } from 'axios';
import { RocClientError } from '../Error.ts';
import type { FetchAttachmentType, IAttachment } from '../index.ts';
import type {
IEntryDocument,
IEntryDocumentDraft,
INewAttachment,
RocAxiosRequestOptions,
} from '../types.ts';
import { assert } from '../util/assert.ts';
import { addInlineUploads, deleteInlineUploads } from './utils.ts';
export interface RocDocumentOptions {
allowAttachmentOverwrite: boolean;
}
const defaultRocOptions: RocDocumentOptions = {
allowAttachmentOverwrite: true,
};
export default class RocDocument<
ContentType = Record<string, unknown>,
IdType = string,
> {
private request: AxiosInstance;
public uuid: string;
public rev?: string;
protected value?: IEntryDocument<ContentType, IdType>;
public deleted: boolean;
private options: RocDocumentOptions;
public constructor(
data: string | IEntryDocument<ContentType, IdType>,
request: AxiosInstance,
options: RocDocumentOptions = defaultRocOptions,
) {
if (typeof data === 'string') {
this.uuid = data;
} else {
this.uuid = data._id;
this.rev = data._rev;
this.value = data;
}
this.request = request;
this.deleted = false;
this.options = options;
}
public async fetchAttachment(
name: string,
responseType: FetchAttachmentType<'text' | 'arraybuffer' | 'blob'>,
axiosOptions?: RocAxiosRequestOptions,
): Promise<Buffer | string> {
const url = new URL(name, this.getBaseUrl()).href;
const response = await this.request({
url,
responseType,
...axiosOptions,
});
return response.data;
}
public async fetch(
rev?: string,
axiosOptions?: RocAxiosRequestOptions,
): Promise<IEntryDocument<ContentType, IdType>> {
if (rev) {
throw new Error('UNIMPLEMENTED fetch with rev');
}
const response = await this.request.get('/', axiosOptions);
this.value = response.data;
return response.data;
}
public async update(
content: ContentType,
newAttachments?: INewAttachment[],
deleteAttachments?: string[],
axiosOptions?: RocAxiosRequestOptions,
): Promise<IEntryDocument<ContentType, IdType>> {
if (content && typeof content === 'object' && '_id' in content) {
throw new Error(
'Your content contains an _id proprerty. This is probably an error since you should not pass the entire document, only $content',
);
}
await this._fetchIfUnfetched();
assert(this.value, 'Unreachable: fetched value is undefined');
let newDoc: IEntryDocumentDraft<ContentType, IdType> = {
...this.value,
$content: content,
};
if (deleteAttachments !== undefined) {
newDoc = deleteInlineUploads(newDoc, deleteAttachments);
}
if (newAttachments !== undefined) {
if (!this.options.allowAttachmentOverwrite) {
for (const attachment of newAttachments) {
if (newDoc._attachments?.[attachment.name]) {
throw new RocClientError(
`overwriting ${attachment.name}, overwriting attachments is forbidden`,
);
}
}
}
newDoc = await addInlineUploads(newDoc, newAttachments);
}
// Send the new doc
await this.request.put('/', newDoc, axiosOptions);
// Get the new document
// With updated properties ($lastModifification...)
// And new attachment list
await this.fetch();
return this.value;
}
public getAttachmentList(): IAttachment[] {
if (this.value === undefined) {
throw new RocClientError(
'You must fetch the document in order to get the attachment list',
);
}
// value must be defined after fetch
const doc = this.value;
const attachments = doc._attachments || {};
const list = [];
for (const key in attachments) {
list.push(this.getAttachment(key));
}
return list;
}
public async delete(axiosOptions?: RocAxiosRequestOptions) {
const response = await this.request.delete('/', axiosOptions);
if (response.data.ok) {
this.value = undefined;
this.deleted = true;
} else {
throw new Error('document was not deleted');
}
}
public getAttachment(name: string): IAttachment {
if (this.value === undefined) {
throw new RocClientError(
'You must fetch the document in order to get an attachment',
);
}
const doc = this.value;
const attachments = doc._attachments || {};
if (!attachments[name]) {
throw new RocClientError(`attachment ${name} does not exist`);
}
return {
...attachments[name],
name,
url: `${this.getBaseUrl()}${name}`,
};
}
public getValue() {
return this.value;
}
public toJSON() {
return this.getValue();
}
public addGroups(/* groups: string | string[] */): Promise<string[]> {
throw new Error('UNIMPLEMENTED addGroups');
}
public async hasRight(right: string, axiosOptions?: RocAxiosRequestOptions) {
const response = await this.request.get(`_rights/${right}`, axiosOptions);
return response.data;
}
protected getBaseUrl() {
return this.request.defaults.baseURL || '';
}
private async _fetchIfUnfetched() {
if (this.value === undefined) {
await this.fetch();
}
}
}