-
Notifications
You must be signed in to change notification settings - Fork 303
/
Auth0RestClient.js
120 lines (102 loc) · 3.53 KB
/
Auth0RestClient.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
const RestClient = require('rest-facade').Client;
const { ArgumentError } = require('rest-facade');
const utils = require('./utils');
const { SanitizedError } = require('./errors');
class Auth0RestClient {
constructor(resourceUrl, options, provider) {
if (resourceUrl === null || resourceUrl === undefined) {
throw new ArgumentError('Must provide a Resource Url');
}
if ('string' !== typeof resourceUrl || resourceUrl.length === 0) {
throw new ArgumentError('The provided Resource Url is invalid');
}
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide options');
}
options.errorCustomizer = options.errorCustomizer || SanitizedError;
options.errorFormatter = options.errorFormatter || { message: 'message', name: 'error' };
this.options = options;
this.provider = provider;
this.restClient = new RestClient(resourceUrl, options);
this.wrappedProvider = function (method, args) {
if (!this.provider) {
return this._request(method, args);
}
let callback;
if (args && args[args.length - 1] instanceof Function) {
callback = args[args.length - 1];
}
return this.provider
.getAccessToken()
.then((access_token) => {
this.restClient.options.headers['Authorization'] = `Bearer ${access_token}`;
return this._request(method, args);
})
.catch((err) => {
if (callback) {
return callback(err);
}
throw err;
});
};
}
_request(method, args) {
if (!this.options.includeResponseHeaders) {
return this.restClient[method](...args);
}
let callback;
// Handle the case where the promise variant is called without any args
// client.get() -> client.get({}, callback)
if (!args || !args.length) {
args = [{}];
}
// Handle the case where an undefined placeholder is defined for callback
// client.get(params, undefined) -> client.get(params, callback)
if (typeof args[args.length - 1] === 'undefined') {
args.pop();
}
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
}
return new Promise((resolve, reject) => {
this.restClient[method](...args, (err, data, headers) => {
const payload = { data, headers };
if (err) {
(callback && callback(err)) || reject(err);
}
(callback && callback(null, payload)) || resolve(payload);
});
});
}
getAll(...args) {
return this.wrappedProvider('getAll', args);
}
get(params, callback) {
if (typeof params === 'object' && params.id) {
params.id = utils.maybeDecode(`${params.id}`);
}
return this.wrappedProvider('get', [...[params, callback].filter(Boolean)]);
}
create(...args) {
return this.wrappedProvider('create', args);
}
patch(params, ...restOfArgs) {
if (typeof params === 'object' && params.id) {
params.id = utils.maybeDecode(`${params.id}`);
}
return this.wrappedProvider('patch', [params, ...restOfArgs]);
}
update(params, ...restOfArgs) {
if (typeof params === 'object' && params.id) {
params.id = utils.maybeDecode(`${params.id}`);
}
return this.wrappedProvider('update', [params, ...restOfArgs]);
}
delete(params, ...restOfArgs) {
if (typeof params === 'object' && params.id) {
params.id = utils.maybeDecode(`${params.id}`);
}
return this.wrappedProvider('delete', [params, ...restOfArgs]);
}
}
module.exports = Auth0RestClient;