-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
api.js
180 lines (149 loc) · 3.58 KB
/
api.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
var EVALUATOR = {};
var cache = {};
// Registers a new API type
exports.evaluate = function(type, callback) {
if (typeof(type) === 'function') {
callback = type;
type = 'default';
}
if (type.indexOf(',') !== -1) {
var arr = type.split(',').trim();
for (var m of arr)
exports.evaluate(m, callback);
return;
}
// It can be "camel case"
var lower = type.toLowerCase();
cache[type] = lower;
cache[lower] = lower;
if (callback)
EVALUATOR[lower] = callback;
else
delete EVALUATOR[lower];
};
function APICall() {
var t = this;
t.options = {};
}
const APICallProto = APICall.prototype;
APICallProto.output = function(type) {
this.options.output = type;
return this;
};
APICallProto.promise = function($) {
var t = this;
var promise = new Promise(function(resolve, reject) {
t.$callback = function(err, response) {
if (err) {
if ($ && $.invalid) {
$.invalid(err);
} else {
err.name = 'API(' + t.options.name + ' --> ' + t.options.schema + ')';
reject(err);
}
} else
resolve(response);
};
});
return promise;
};
APICallProto.audit = function($, message, type) {
var t = this;
t.$audit = function() {
// Dynamic arguments
if (message)
message = $.variables(message, t.options.data);
$.audit(message, type);
};
return t;
};
APICallProto.done = function($, callback) {
var t = this;
t.$callback = function(err, response) {
if (err)
$.invalid(err);
else
callback && callback(response);
t.free();
};
return t;
};
APICallProto.debug = function() {
this.$debug = true;
return this;
};
APICallProto.fail = function(cb) {
this.$callback_fail = cb;
return this;
};
APICallProto.data = function(cb) {
this.$callback_data = cb;
return this;
};
APICallProto.controller = function($) {
this.options.controller = $.controller || $;
return this;
};
APICallProto.file = function(filename, path, name) {
var t = this;
if (!t.options.files)
t.options.files = [];
var obj = { name: name || ('file' + t.options.files.length), filename: filename, path: path };
if (t.options.files)
t.options.files.push(obj);
else
t.options.files = [obj];
return t;
};
APICallProto.error = APICallProto.err = function(err, reverse) {
this.$error = err + '';
this.$error_reverse = reverse;
return this;
};
APICallProto.callback = function($) {
var t = this;
t.$callback = typeof($) === 'function' ? $ : $.callback;
return t;
};
APICallProto.evaluate = function(err, response) {
var t = this;
if (!err && t.$error) {
if (t.$error_reverse) {
if (response)
err = t.$error;
else if (response instanceof Array && response.length)
err = t.$error;
} else if (!response)
err = t.$error;
else if (response instanceof Array && !response.length)
err = t.$error;
}
if (err) {
t.$callback_fail && t.$callback_fail(err);
} else {
if (t.$audit) {
t.$audit();
t.$audit = null;
}
t.$callback_data && t.$callback_data(response);
}
t.$debug && console.log('--DEBUG-- API: ' + t.options.name + ' --> ' + t.options.schema, '|', 'Error:', err, '|', 'Response:', response);
t.$callback && t.$callback(err, response);
};
function execapi(api) {
var conn = EVALUATOR[cache[api.options.name]] || EVALUATOR['*'];
if (conn)
conn.call(api, api.options, (err, response) => api.evaluate(err, response));
else
api.evaluate('API is not initialized');
}
// Makes a new instances of API call
exports.make = function(name, schema, data, $) {
var api = new APICall();
api.options.name = cache[name] || name;
api.options.schema = schema;
api.options.data = data;
api.options.controller = $;
setImmediate(execapi, api);
return api;
};