forked from box/box-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnotationAPI.js
103 lines (91 loc) · 3 KB
/
AnnotationAPI.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
94
95
96
97
98
99
100
101
102
103
// @flow
import API from './API';
import { ERROR_TYPE } from '../constants';
const HTTP_POST = 'POST';
const HTTP_DELETE = 'DELETE';
class AnnotationAPI extends API {
/**
* Construct the URL to create/edit/delete annotations
*
* @param {string} [id] - Optional annotation id
* @return {string} Base API url
*/
getBaseUrl(id?: string): string {
return id ? `${this.apiHost}/2.0/annotations/${id}` : `${this.apiHost}/2.0/annotations`;
}
/**
* HTTP POSTs a URL with annotation data
*
* @param {AnnotationData} annotation - Annotation returned from API
* @return {Promise} HTTP Response
*/
create(annotation: AnnotationData): Promise<StringAnyMap> {
const url = this.getBaseUrl();
const methodRequest = this.axios({
url,
data: annotation,
method: HTTP_POST,
cancelToken: this.axiosSource.token,
headers: this.headers
});
return this.makeRequest(methodRequest, this.createSuccessHandler);
}
/**
* @param {string} id - Annotation ID to be deleted
* @return {Promise} HTTP Response
*/
delete(id: string): Promise<StringAnyMap> {
const url = this.getBaseUrl(id);
const methodRequest = this.axios({
url,
method: HTTP_DELETE,
cancelToken: this.axiosSource.token,
headers: this.headers
});
return this.makeRequest(methodRequest, (data) => this.deleteSuccessHandler(data, id));
}
/**
* @param {Object} data - HTTP response data
* @return {Annotation} Formatted HTTP response data
*/
createSuccessHandler = (data: Object): CommentProps => {
if (data.type === 'error') {
const error = new Error('Could not create annotation');
error.name = ERROR_TYPE.create;
this.errorHandler(error);
return data;
}
const { details, thread: threadNumber } = data;
const { threadID, location, type } = details;
// Corrects any annotation page number to 1 instead of -1
const fixedLocation = location;
if (!fixedLocation.page || fixedLocation.page < 0) {
fixedLocation.page = 1;
}
return {
...this.formatComment(data),
type,
threadID,
threadNumber,
canAnnotate: true,
permissions: {
can_delete: true,
can_edit: true
}
};
};
/**
* @param {Object} data - HTTP response data
* @param {string} id - Annotation ID
* @return {Object} Formatted HTTP response data
*/
deleteSuccessHandler = (data: Object, id: string) => {
if (data.type === 'error' || !data.id) {
const error = new Error(`Could not delete annotation with ID ${id}`);
error.name = ERROR_TYPE.delete;
this.errorHandler(error);
}
return data;
};
}
export default AnnotationAPI;