@ouroboros/body 0.1.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -12
- package/src/Service.d.ts +126 -0
- package/src/Service.js +148 -0
- package/src/Service.ts +166 -0
- package/src/constants.d.ts +13 -0
- package/src/constants.js +15 -0
- package/src/constants.ts +17 -0
- package/src/errors.d.ts +32 -0
- package/src/errors.js +36 -0
- package/src/errors.ts +40 -0
- package/src/index.d.ts +223 -0
- package/src/index.js +401 -0
- package/src/index.ts +514 -0
- package/src/regex.d.ts +11 -0
- package/src/regex.js +13 -0
- package/src/regex.ts +15 -0
- package/index.d.ts +0 -3
- package/index.js +0 -3
- package/jquery.ajax.js +0 -5469
- package/rest.js +0 -408
- package/twoway.js +0 -389
- package/wshelper.js +0 -92
package/rest.js
DELETED
|
@@ -1,408 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rest
|
|
3
|
-
*
|
|
4
|
-
* Handles connecting to and retrieving data from rest services
|
|
5
|
-
*
|
|
6
|
-
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
-
* @copyright Ouroboros Coding Inc.
|
|
8
|
-
* @created 2018-11-24
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// External modules
|
|
12
|
-
import $ from './jquery.ajax';
|
|
13
|
-
|
|
14
|
-
// Generic modules
|
|
15
|
-
import { cookies } from '@ouroboros/browser';
|
|
16
|
-
|
|
17
|
-
// Global data
|
|
18
|
-
let _conf = {
|
|
19
|
-
after: null,
|
|
20
|
-
before: null,
|
|
21
|
-
cookie: '',
|
|
22
|
-
domain: '',
|
|
23
|
-
error: null,
|
|
24
|
-
errors: {},
|
|
25
|
-
session: null,
|
|
26
|
-
success: null,
|
|
27
|
-
use_session: true
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Clear
|
|
32
|
-
*
|
|
33
|
-
* Clears the session from the conf and cookie
|
|
34
|
-
*
|
|
35
|
-
* @name clear
|
|
36
|
-
* access private
|
|
37
|
-
* @return void
|
|
38
|
-
*/
|
|
39
|
-
function clear() {
|
|
40
|
-
|
|
41
|
-
// Clear the session
|
|
42
|
-
_conf.session = null;
|
|
43
|
-
|
|
44
|
-
// Delete the cookie
|
|
45
|
-
cookies.remove('_session', _conf.cookie, '/');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Request
|
|
50
|
-
*
|
|
51
|
-
* Handles actual requests
|
|
52
|
-
*
|
|
53
|
-
* @name request
|
|
54
|
-
* @access private
|
|
55
|
-
* @param string method The method used to send the request
|
|
56
|
-
* @param string url The full URL to the service/noun
|
|
57
|
-
* @param object data The data to send to the service
|
|
58
|
-
* @param object opts Optional flags that can be set
|
|
59
|
-
* @return xhr
|
|
60
|
-
*/
|
|
61
|
-
function request(method, url, data, opts) {
|
|
62
|
-
|
|
63
|
-
if(!window.navigator.onLine) {
|
|
64
|
-
console.error('Not connected to internet');
|
|
65
|
-
|
|
66
|
-
// Create the response object
|
|
67
|
-
let oResponse = {
|
|
68
|
-
_handled: false,
|
|
69
|
-
error: {code: -1}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Call the success callback if there is one
|
|
73
|
-
if(_conf.success) {
|
|
74
|
-
_conf.success(oResponse);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Return an object with a done function
|
|
78
|
-
return {done: callback => callback(oResponse)};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// If session opts is not set
|
|
82
|
-
if(!('session' in opts)) {
|
|
83
|
-
opts['session'] = _conf.use_session;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// If we have a before callback
|
|
87
|
-
if(_conf.before) {
|
|
88
|
-
_conf.before(method, url, data, opts);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Generate the ajax config
|
|
92
|
-
let oConfig = {
|
|
93
|
-
|
|
94
|
-
// Check requests before sending
|
|
95
|
-
beforeSend: (xhr, settings) => {
|
|
96
|
-
|
|
97
|
-
// Add the URL to the request so that on error what failed
|
|
98
|
-
xhr._url = url;
|
|
99
|
-
|
|
100
|
-
// If we have a session, add the authorization token
|
|
101
|
-
if(opts['session'] && _conf.session) {
|
|
102
|
-
xhr.setRequestHeader('Authorization', _conf.session);
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
// Looking for JSON responses
|
|
107
|
-
contentType: "application/json; charset=utf-8",
|
|
108
|
-
|
|
109
|
-
// Called when the requested is completed, whether success or error
|
|
110
|
-
complete: res => {
|
|
111
|
-
|
|
112
|
-
// If we have an after callback
|
|
113
|
-
if(_conf.after) {
|
|
114
|
-
_conf.after(method, url, data, opts);
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
// On error
|
|
119
|
-
error: (xhr, status, error) => {
|
|
120
|
-
|
|
121
|
-
// If we got an Authorization error
|
|
122
|
-
if(xhr.status === 401) {
|
|
123
|
-
|
|
124
|
-
// Clear the current token
|
|
125
|
-
clear();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Put the error in the console
|
|
129
|
-
console.error(method + ' ' + xhr._url + ' returned: ' + error);
|
|
130
|
-
|
|
131
|
-
// Return the xhr to the error callback if there is one
|
|
132
|
-
if(_conf.error) {
|
|
133
|
-
_conf.error(xhr);
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
// Set the method
|
|
138
|
-
method: method,
|
|
139
|
-
|
|
140
|
-
// Called when the request is successful
|
|
141
|
-
success: res => {
|
|
142
|
-
|
|
143
|
-
// Set the default value of the handled flag
|
|
144
|
-
res._handled = false;
|
|
145
|
-
|
|
146
|
-
// Call the success callback if there is one
|
|
147
|
-
if(_conf.success) {
|
|
148
|
-
_conf.success(res);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
// Set the requested URL
|
|
153
|
-
url: url
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// If we have any data
|
|
157
|
-
if(typeof data !== 'undefined' && data !== null) {
|
|
158
|
-
|
|
159
|
-
// If it's a get request
|
|
160
|
-
if(method === 'get') {
|
|
161
|
-
|
|
162
|
-
// And data was passed, add it as a param
|
|
163
|
-
oConfig.data = "d=" + encodeURIComponent(JSON.stringify(data));
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Else it's any other method, stringify the data
|
|
167
|
-
else {
|
|
168
|
-
oConfig.data = JSON.stringify(data);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Make the request and return the xhr
|
|
173
|
-
return $.ajax(oConfig);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Store
|
|
178
|
-
*
|
|
179
|
-
* Stores the session token in the conf and cookie
|
|
180
|
-
*
|
|
181
|
-
* @name store
|
|
182
|
-
* @access private
|
|
183
|
-
* @param string token
|
|
184
|
-
* @return void
|
|
185
|
-
*/
|
|
186
|
-
function store(token) {
|
|
187
|
-
|
|
188
|
-
// Store the session
|
|
189
|
-
_conf.session = token;
|
|
190
|
-
|
|
191
|
-
// Set the session in a cookie
|
|
192
|
-
cookies.set('_session', token, 86400, _conf.cookie, '/');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Init
|
|
197
|
-
*
|
|
198
|
-
* Initialises the modules
|
|
199
|
-
*
|
|
200
|
-
* @name init
|
|
201
|
-
* @access public
|
|
202
|
-
* @param string domain The domain rest services can be reached through
|
|
203
|
-
* @param string cookie The domain to store the cookie on
|
|
204
|
-
* @param Object opts Optional settings
|
|
205
|
-
* after: optional callback to run after all requests
|
|
206
|
-
* before: optional callback to run before all requests
|
|
207
|
-
* cookie: optional domain for storing the session cookie in
|
|
208
|
-
* error: optional callback for when http errors occur
|
|
209
|
-
* errors: optional object of error codes to messages
|
|
210
|
-
* success: optional callback for after successful requests
|
|
211
|
-
* use_session: optional flag to allow for never using sessions
|
|
212
|
-
* @return void
|
|
213
|
-
*/
|
|
214
|
-
function init(domain, opts = {}) {
|
|
215
|
-
|
|
216
|
-
// Store the domains
|
|
217
|
-
_conf.domain = 'https://' + domain + '/';
|
|
218
|
-
_conf.cookie = 'cookie' in opts ? opts['cookie'] : domain;
|
|
219
|
-
|
|
220
|
-
// Check for use_session flag
|
|
221
|
-
if(!('use_session' in opts)) {
|
|
222
|
-
opts['use_session'] = true;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// If we are using sessions
|
|
226
|
-
if(opts['use_session']) {
|
|
227
|
-
|
|
228
|
-
// If we don't already have it
|
|
229
|
-
if(!_conf.session) {
|
|
230
|
-
|
|
231
|
-
// Is it in a cookie?
|
|
232
|
-
let cookie = cookies.get('_session');
|
|
233
|
-
if(cookie) {
|
|
234
|
-
this.session(cookie);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// Store errors
|
|
240
|
-
if('errors' in opts) {
|
|
241
|
-
_conf.errors = opts.errors;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Store callbacks
|
|
245
|
-
if('error' in opts) {
|
|
246
|
-
if(typeof opts['error'] === 'function') {
|
|
247
|
-
_conf.error = opts['error'];
|
|
248
|
-
} else {
|
|
249
|
-
console.error('Rest.init \'error\' param must be a function');
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if('before' in opts) {
|
|
253
|
-
if(typeof opts['before'] === 'function') {
|
|
254
|
-
_conf.before = opts['before'];
|
|
255
|
-
} else {
|
|
256
|
-
console.error('Rest.init \'before\' param must be a function');
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
if('after' in opts) {
|
|
260
|
-
if(typeof opts['after'] === 'function') {
|
|
261
|
-
_conf.after = opts['after'];
|
|
262
|
-
} else {
|
|
263
|
-
console.error('Rest.init \'after\' param must be a function');
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
if('success' in opts) {
|
|
267
|
-
if(typeof opts['success'] === 'function') {
|
|
268
|
-
_conf.success = opts['success'];
|
|
269
|
-
} else {
|
|
270
|
-
console.error('Rest.init \'success\' param must be a function');
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Create
|
|
277
|
-
*
|
|
278
|
-
* Calls the create action on a specific service noune
|
|
279
|
-
*
|
|
280
|
-
* @name create
|
|
281
|
-
* @access public
|
|
282
|
-
* @param string service The name of the service to call
|
|
283
|
-
* @param string noun The noun to call on the service
|
|
284
|
-
* @param object data The data to send to the service
|
|
285
|
-
* @param object opts Optional flags that can be set
|
|
286
|
-
* @return xhr
|
|
287
|
-
*/
|
|
288
|
-
function create(service, noun, data, opts = {}) {
|
|
289
|
-
return request('post', _conf.domain + service + '/' + noun, data, opts);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Delete
|
|
294
|
-
*
|
|
295
|
-
* Calls the delete action on a specific service noune
|
|
296
|
-
*
|
|
297
|
-
* @name delete_
|
|
298
|
-
* @access public
|
|
299
|
-
* @param string service The name of the service to call
|
|
300
|
-
* @param string noun The noun to call on the service
|
|
301
|
-
* @param object data The data to send to the service
|
|
302
|
-
* @param object opts Optional flags that can be set
|
|
303
|
-
* @return xhr
|
|
304
|
-
*/
|
|
305
|
-
function delete_(service, noun, data, opts = {}) {
|
|
306
|
-
return request('delete', _conf.domain + service + '/' + noun, data, opts);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Generic Error Message
|
|
311
|
-
*
|
|
312
|
-
* Returns an error message based on the errors passed to Rest at init
|
|
313
|
-
*
|
|
314
|
-
* @name
|
|
315
|
-
* @access public
|
|
316
|
-
* @param Object error The 'code' and 'msg'
|
|
317
|
-
* @return String
|
|
318
|
-
*/
|
|
319
|
-
function errorMessage(error) {
|
|
320
|
-
|
|
321
|
-
// Convert the code to a string
|
|
322
|
-
let sCode = error.code.toString();
|
|
323
|
-
|
|
324
|
-
// If the code is in the errors
|
|
325
|
-
if(sCode in _conf.errors) {
|
|
326
|
-
return _conf.errors[sCode];
|
|
327
|
-
} else {
|
|
328
|
-
return 'msg' in error ? error.msg + ' (' + sCode + ')' : sCode;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Read
|
|
334
|
-
*
|
|
335
|
-
* Calls the read action on a specific service noune
|
|
336
|
-
*
|
|
337
|
-
* @name read
|
|
338
|
-
* @access public
|
|
339
|
-
* @param string service The name of the service to call
|
|
340
|
-
* @param string noun The noun to call on the service
|
|
341
|
-
* @param object data The data to send to the service
|
|
342
|
-
* @param object opts Optional flags that can be set
|
|
343
|
-
* @return xhr
|
|
344
|
-
*/
|
|
345
|
-
function read(service, noun, data, opts = {}) {
|
|
346
|
-
return request('get', _conf.domain + service + '/' + noun, data, opts);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Session
|
|
351
|
-
*
|
|
352
|
-
* Set or get the session token
|
|
353
|
-
*
|
|
354
|
-
* @name session
|
|
355
|
-
* @access public
|
|
356
|
-
* @param string token The token to store
|
|
357
|
-
* @return void|str
|
|
358
|
-
*/
|
|
359
|
-
function session(token) {
|
|
360
|
-
|
|
361
|
-
// If we are setting the session
|
|
362
|
-
if(typeof token !== 'undefined') {
|
|
363
|
-
|
|
364
|
-
// If null was passed, delete the session
|
|
365
|
-
if(token == null) {
|
|
366
|
-
clear();
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Else, set the session
|
|
370
|
-
else {
|
|
371
|
-
store(token);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// Else we are returning the session
|
|
376
|
-
else {
|
|
377
|
-
return _conf.session;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Update
|
|
383
|
-
*
|
|
384
|
-
* Calls the update action on a specific service noune
|
|
385
|
-
*
|
|
386
|
-
* @name update
|
|
387
|
-
* @access public
|
|
388
|
-
* @param string service The name of the service to call
|
|
389
|
-
* @param string noun The noun to call on the service
|
|
390
|
-
* @param object data The data to send to the service
|
|
391
|
-
* @param object opts Optional flags that can be set
|
|
392
|
-
* @return xhr
|
|
393
|
-
*/
|
|
394
|
-
function update(service, noun, data, opts = {}) {
|
|
395
|
-
return request('put', _conf.domain + service + '/' + noun, data, opts);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// Default export
|
|
399
|
-
const rest = {
|
|
400
|
-
init: init,
|
|
401
|
-
create: create,
|
|
402
|
-
delete: delete_,
|
|
403
|
-
errorMessage: errorMessage,
|
|
404
|
-
read: read,
|
|
405
|
-
session: session,
|
|
406
|
-
update: update
|
|
407
|
-
};
|
|
408
|
-
export default rest;
|