-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
145 lines (126 loc) · 3.94 KB
/
index.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
/*
Paystack API wrapper
@author Pariola Blessing <@pariola>
*/
const request = require("request-promise");
const endpoint = "https://api.paystack.co";
const Events = require("./resources/events");
function Paystack(key) {
if (!(this instanceof Paystack)) {
return new Paystack(key);
}
this.endpoint = endpoint;
this.key = key;
this.import();
// Setup Events
this.Events = new Events(this.key);
}
const resources = {
customer: require("./resources/customer"),
bulk_charge: require("./resources/bulk_charge"),
charge: require("./resources/charge"),
control_panel: require("./resources/control_panel"),
invoice: require("./resources/invoice"),
nuban: require("./resources/nuban"),
page: require("./resources/page"),
plan: require("./resources/plan"),
refund: require("./resources/refund"),
settlement: require("./resources/settlement"),
subaccount: require("./resources/subaccount"),
subscription: require("./resources/subscription"),
transaction: require("./resources/transaction"),
transfer_control: require("./resources/transfer_control"),
transfer_recipient: require("./resources/transfer_recipient"),
transfer: require("./resources/transfer"),
verification: require("./resources/verification"),
misc: require("./resources/misc")
};
Paystack.prototype = {
extend: function(func) {
const me = this;
return function() {
const data = arguments[0] || {};
// check method
const method = ["post", "get", "put", "delete"].includes(func.method)
? func.method
: (function() {
throw new Error("Method not Allowed! - Resource declaration error");
})();
var endpoint = me.endpoint + func.route,
qs = {};
// Highest priority should go to path variables parsing and validation
var argsInEndpoint = endpoint.match(/{[^}]+}/g);
if (argsInEndpoint) {
argsInEndpoint.map(arg => {
arg = arg.replace(/\W/g, "");
if (!(arg in data)) {
throw new Error(`Argument '${arg}' is required`);
} else {
endpoint = endpoint.replace(`{${arg}}`, data[`${arg}`]);
// to avoid error, remove the path arg from body | qs params
// by deleting it from the data object before body | qs params are set
delete data[arg];
}
});
}
// incase of endpoints with no params requirement
if (func.params) {
// check args
func.params.filter(param => {
if (!param.includes("*")) return;
param = param.replace("*", "");
if (!(param in data)) {
throw new Error(`Parameter '${param}' is required`);
}
return;
});
}
// incase of endpoints with no args requirement
if (func.args) {
// check args
func.args.filter(a => {
// remove unwanted properties
if (!a.includes("*")) {
if (a in data) {
qs[`${a}`] = data[`${a}`];
}
return;
}
a = a.replace("*", "");
if (!(a in data)) {
throw new Error(`Argument '${a}' is required`);
} else {
qs[`${a}`] = data[`${a}`];
}
return;
});
}
// Create request
const options = {
url: endpoint,
json: true,
method: method.toUpperCase(),
headers: {
Authorization: `Bearer ${me.key}`
}
};
if (method == "post" || method == "put") {
options.body = data;
} else {
options.qs = qs;
}
return request(options);
};
},
import: function() {
for (var i in resources) {
const anon = function() {};
for (var j in resources[i]) {
anon.prototype[j] = this.extend(resources[i][j]);
}
Paystack.prototype[i] = new anon();
}
},
FeeHelper: require("./resources/fee_helper")
};
module.exports = Paystack;