-
Notifications
You must be signed in to change notification settings - Fork 5
/
truevault.js
292 lines (261 loc) · 9.86 KB
/
truevault.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var tvExplorer = {
apiKey: "",
accountId: "",
endpoint: "https://api.truevault.com",
apiVersion: "v1",
timeout: 60000, //milliseconds
useragent: 'TrueVault JavaScript Explorer by MichaelApproved',
/**
* Function as a stub. This should be changed by your own code to
* work with the individual requirements of a website.
*/
errorDisplay: function(message) {
//set this to whatever you need done when an error is displayed to a user.
},
apiRequest: {
/**
* Function as a stub. This should be changed by your own code to
* execute something after an API call has been successfully completed.
*/
Done: function() {
//set this to whatever you need done when an API request has completed.
},
/**
* Function as a stub. This should be changed by your own code to
* execute something after an API call has been completed.
*/
Always: function() {
//set this to whatever you need done when an API request has completed.
},
/**
* Function as a stub. This should be changed by your own code to
* execute something before an API call has been sent.
*/
BeforeSend: function(jqXHR, settings) {
//set this to whatever you need done when an API request has completed.
},
/**
* Builds the API URL used to make the request.
* @param {string} path must NOT have a leading forward slash /
* @returns {string} the complete URL with endpoint, apiversion and path
*/
createUrl: function(path) {
$url = tvExplorer.endpoint + '/' + tvExplorer.apiVersion + '/' + path;
return $url;
},
/**
* Makes the api request to a given path using the method along with any post vars and file transfers.
* @param {string} path The TrueVault path to communicate with
* @param {function} callback The function to run on success
* @param {string} method GET, POST, DELETE, PUT
* @param {array} params Post fields
* @param {array} transfer Instructions and data for file transfers.
* @returns {undefined}
*/
send: function(path, callback, method, params, transfer) {
//set defaults
//An action could be handled globally with the
//tvExplorer.apiRequestDone function of or via this callback
//for more specific action.
if (typeof (callback) === "undefined" || typeof (callback) === "object") {
callback = function() {
};
}
//default method to GET
method = (typeof (method) === "undefined" || typeof (percentage) === "object") ? "GET" : method;
if (typeof (params) !== "undefined" && typeof (params) !== "object") {
//do post param work here
params = '';
}
if (typeof (transfer) !== "undefined" && typeof (transfer) !== "object") {
//do transfer work here
}
//Make the request
jQuery.ajax({
type: method,
url: tvExplorer.apiRequest.createUrl(path),
async: false,
data: params,
timeout: 1,
headers: {"Authorization": "Basic " + btoa(tvExplorer.apiKey + ":")},
beforeSend: function(jqXHR, settings) {
tvExplorer.apiRequest.BeforeSend(jqXHR, settings);
},
})
.done(function(data, textStatus, jqXHR) {
console.log('tvExplorer.apiRequest() ajax done. Details start.');
console.log(data);
console.log(textStatus);
console.log(jqXHR);
console.log(jqXHR.getAllResponseHeaders());
console.log('tvExplorer.apiRequest() ajax done. Details end.');
//Is this a JSON response or is this a file?
if (jqXHR.getResponseHeader('content-type') === 'application/json') {
if (data.result === 'success') {
callback(data);
} else {
//display an error to the visitor
tvExplorer.errorDisplay('Record Request error', data);
}
} else {
//Likely 'application/octet-stream'
//Pass the data object to the callback function.
//Allow it to processing the file.
callback(data);
}
tvExplorer.apiRequest.Done(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
//log the details to console.
console.log('tvExplorer.apiRequest() ajax fail. Details start.');
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
console.log('tvExplorer.apiRequest() ajax fail. Details end.');
//display an error to the visitor
displayMessage = (jqXHR.responseJSON === undefined) ? jqXHR.statusText : jqXHR.responseJSON.error.message + "\n(" + jqXHR.statusText + ")";
tvExplorer.errorDisplay('Record Request failed: ' + displayMessage);
})
.always(function() {
tvExplorer.apiRequest.Always();
});
},
},
vaults: {
/**
* Finds a list of vaults for the account Id
*
* @param {function} callback The function to run on success.
* @returns {undefined}
*/
findAll: function(callback) {
path = 'accounts/' + tvExplorer.accountId + '/vaults';
tvExplorer.apiRequest.send(path, callback, "GET");
},
},
schemas: {
/**
* Find all schemas for a given vault
* @param {type} vaultId The ID of the vault to search.
* @param {type} callback The function to execute on a successful request.
* @returns {undefined}
*/
findAll: function(vaultId, callback) {
var path = 'vaults/' + vaultId + '/schemas';
tvExplorer.apiRequest.send(path, callback, "GET");
},
/**
* Get schema details
* @param {type} vaultId The ID of the vault to search.
* @param {type} schemaId The ID of the schema to get details of.
* @param {type} callback The function to execute on a successful request.
* @returns {undefined}
*/
get: function(vaultId, schemaId, callback) {
var path = 'vaults/' + vaultId + '/schemas/' + schemaId;
tvExplorer.apiRequest.send(path, callback, "GET");
},
delete: function(vaultId, schemaId, callback) {
var path = 'vaults/' + vaultId + '/schemas/' + schemaId;
tvExplorer.apiRequest.send(path, callback, "DELETE");
},
update: function(vaultId, schemaId, schemaContent, callback) {
var path = 'vaults/' + vaultId + '/schemas/' + schemaId;
//Base64 (btoa) encode the string.
documentContentBase64 = btoa(schemaContent);
tvExplorer.apiRequest.send(path, callback, "PUT", {"schema": documentContentBase64});
},
create: function(vaultId, schemaContent, callback) {
var path = 'vaults/' + vaultId + '/schemas';
//Base64 (btoa) encode the string.
documentContentBase64 = btoa(schemaContent);
tvExplorer.apiRequest.send(path, callback, "POST", {"schema": documentContentBase64});
},
},
documents: {
/**
* Will return all documentIds in a vault.
*
* Documents come paged with 100 per page. To get another page, pass the
* next page number into the request.
*
* @param {type} vaultId The ID of the vault to return all data from.
* @param {integer} page The page number to get.
* @param {type} callback The function to execute on a successful request.
* @returns {undefined}
*/
findAll: function(vaultId, page, callback) {
var path = 'vaults/' + vaultId + '/documents';
//send the request
tvExplorer.apiRequest.send(path + '?page=' + page, callback, "GET");
},
/**
* Return a list of documents filtered by searchOption
*
* Documents come paged with 100 per page. To get another page, pass the
* next page number into the request.
*
* @param {type} vaultId The ID of the vault to return all data from.
* @param {object} filterOption The filter options to include in the request.
* Options include: schema_id, filter, case_sensitive, page, per_page, filter_type, full_document, and sort.
* @param {type} callback The function to execute on a successful request.
* @returns {undefined}
*/
filter: function(vaultId, filterOption, callback) {
var path = 'vaults/' + vaultId + '/';
//convert the serachOption array into a string and then base64 encode it.
searchOptionBase64 = btoa(JSON.stringify(filterOption));
//send the request
tvExplorer.apiRequest.send(path + '?search_option=' + searchOptionBase64, callback, "GET");
},
/**
* Returns the contents of a document.
* TODO - Extended this to accept an array for documentd(s) for multiple documents in one request.
* @param {array} documentId Either a string with a single documentId or an array of documentIds.
* @returns {undefined}
*/
get: function(vaultId, documentId, callback) {
var path = 'vaults/' + vaultId + '/documents/' + documentId;
tvExplorer.apiRequest.send(path, callback, "GET");
},
delete: function(vaultId, documentId, callback) {
var path = 'vaults/' + vaultId + '/documents/' + documentId;
tvExplorer.apiRequest.send(path, callback, "DELETE");
},
update: function(vaultId, schemaId, documentContent, documentId, callback) {
var path = 'vaults/' + vaultId + '/documents/' + documentId;
//Base64 (btoa) encode the string.
documentContentBase64 = btoa(documentContent);
//Only include the schema in the request if it was sent.
if (schemaId === '') {
tvExplorer.apiRequest.send(path, callback, "PUT", {"document": documentContentBase64});
} else {
tvExplorer.apiRequest.send(path, callback, "PUT", {"document": documentContentBase64, "schema_id": schemaId});
}
},
/**
*
* @param {type} vaultId
* @param {type} schemaId
* @param {type} documentContent
* @param {type} callback
* @returns {undefined}
*/
create: function(vaultId, schemaId, documentContent, callback) {
var path = 'vaults/' + vaultId + '/documents';
//Base64 (btoa) encode the string.
documentContentBase64 = btoa(documentContent);
//Only include the schema in the request if it was sent.
if (schemaId === '') {
tvExplorer.apiRequest.send(path, callback, "POST", {"document": documentContentBase64});
} else {
tvExplorer.apiRequest.send(path, callback, "POST", {"document": documentContentBase64, "schema_id": schemaId});
}
},
},
}