pesapress-lookup 1.0.0
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/.babelrc +5 -0
- package/LICENSE +21 -0
- package/README.md +79 -0
- package/dist/api.js +118 -0
- package/dist/app.js +164 -0
- package/dist/index.js +12 -0
- package/package.json +25 -0
- package/sammple/index.js +28 -0
- package/src/api.ts +100 -0
- package/src/app.ts +68 -0
- package/src/index.js +1 -0
- package/tsconfig.json +111 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 PesaPress
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
## PesaPress Lookup
|
|
2
|
+
|
|
3
|
+
### Goal
|
|
4
|
+
|
|
5
|
+
Make it easy to integrate [PesaPress](https://www.pesapress.com) number lookup service into any application to enable number hash decoding.
|
|
6
|
+
Easy registration with only an email and get your number hash decoded.
|
|
7
|
+
|
|
8
|
+
### Usage
|
|
9
|
+
|
|
10
|
+
###### Install
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
$ npm install pesapress-lookup
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
###### Setup
|
|
17
|
+
```javascript
|
|
18
|
+
|
|
19
|
+
const { PesaPressLookup } = require('pesapress');
|
|
20
|
+
const pesapress = new PesaPressLookup();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
###### Register email to get API key
|
|
24
|
+
Register a new API key using your email.
|
|
25
|
+
The API key will only be shown **once** and should be saved somewhere
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
|
|
29
|
+
const resp = await pesapress.register('myemail@domain.com')
|
|
30
|
+
|
|
31
|
+
if ( resp ) {
|
|
32
|
+
resp.apikey; // api key
|
|
33
|
+
resp.dailyCredits; // daily credits
|
|
34
|
+
resp.remainingCredits; // remaining daily credits
|
|
35
|
+
} else {
|
|
36
|
+
// Error that email already exists
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
###### Check remaining credits
|
|
41
|
+
Check the remaining account credits
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
|
|
45
|
+
const resp = await pesapress.status('YOURAPIKEY')
|
|
46
|
+
|
|
47
|
+
if ( resp ) {
|
|
48
|
+
resp.dailyCredits; // daily credits
|
|
49
|
+
resp.remainingCredits; // remaining daily credits
|
|
50
|
+
} else {
|
|
51
|
+
// Error that api key is invalid
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
###### Get mobile number from hash
|
|
56
|
+
Check for a mobile number from a hash.
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
|
|
60
|
+
const resp = await pesapress.search('YOURAPIKEY', 'MOBILENUMBER_HASH')
|
|
61
|
+
|
|
62
|
+
if ( resp ) {
|
|
63
|
+
resp.msisdn; // The decoded number
|
|
64
|
+
resp.hashed; // The original hash
|
|
65
|
+
} else {
|
|
66
|
+
// Error that api key is invalid
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Contributing
|
|
71
|
+
|
|
72
|
+
1. Fork this repo and make changes in your own fork.
|
|
73
|
+
2. Commit your changes and push to your fork `git push origin master`
|
|
74
|
+
3. Create a new pull request and submit it back to the project.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Bugs & Issues
|
|
78
|
+
|
|
79
|
+
To report bugs (or any other issues), use the [issues page](https://github.com/pesapress/pesapress-lookup/issues).
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ApiClient = void 0;
|
|
7
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
9
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
10
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
11
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
12
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
14
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
15
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
16
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
17
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
18
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
19
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
20
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
21
|
+
var ApiClient = exports.ApiClient = /*#__PURE__*/function () {
|
|
22
|
+
function ApiClient(baseURL) {
|
|
23
|
+
_classCallCheck(this, ApiClient);
|
|
24
|
+
this.client = _axios["default"].create({
|
|
25
|
+
baseURL: baseURL,
|
|
26
|
+
timout: 10000,
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json"
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Handle get
|
|
35
|
+
* @param {*} endpoint
|
|
36
|
+
* @param {*} customHeaders
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
return _createClass(ApiClient, [{
|
|
40
|
+
key: "getData",
|
|
41
|
+
value: (function () {
|
|
42
|
+
var _getData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(endpoint) {
|
|
43
|
+
var customHeaders,
|
|
44
|
+
response,
|
|
45
|
+
_args = arguments;
|
|
46
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
47
|
+
while (1) switch (_context.prev = _context.next) {
|
|
48
|
+
case 0:
|
|
49
|
+
customHeaders = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
50
|
+
_context.prev = 1;
|
|
51
|
+
_context.next = 4;
|
|
52
|
+
return this.client.get(endpoint, {
|
|
53
|
+
headers: _objectSpread({}, customHeaders)
|
|
54
|
+
});
|
|
55
|
+
case 4:
|
|
56
|
+
response = _context.sent;
|
|
57
|
+
return _context.abrupt("return", response.data);
|
|
58
|
+
case 8:
|
|
59
|
+
_context.prev = 8;
|
|
60
|
+
_context.t0 = _context["catch"](1);
|
|
61
|
+
console.error("API Error:", _context.t0);
|
|
62
|
+
throw _context.t0;
|
|
63
|
+
case 12:
|
|
64
|
+
case "end":
|
|
65
|
+
return _context.stop();
|
|
66
|
+
}
|
|
67
|
+
}, _callee, this, [[1, 8]]);
|
|
68
|
+
}));
|
|
69
|
+
function getData(_x) {
|
|
70
|
+
return _getData.apply(this, arguments);
|
|
71
|
+
}
|
|
72
|
+
return getData;
|
|
73
|
+
}()
|
|
74
|
+
/**
|
|
75
|
+
* Handle post
|
|
76
|
+
* @param {*} endpoint
|
|
77
|
+
* @param {*} data
|
|
78
|
+
* @param {*} customHeaders
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
)
|
|
82
|
+
}, {
|
|
83
|
+
key: "postData",
|
|
84
|
+
value: (function () {
|
|
85
|
+
var _postData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(endpoint, data) {
|
|
86
|
+
var customHeaders,
|
|
87
|
+
response,
|
|
88
|
+
_args2 = arguments;
|
|
89
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
90
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
91
|
+
case 0:
|
|
92
|
+
customHeaders = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {};
|
|
93
|
+
_context2.prev = 1;
|
|
94
|
+
_context2.next = 4;
|
|
95
|
+
return this.client.post(endpoint, data, {
|
|
96
|
+
headers: _objectSpread({}, customHeaders)
|
|
97
|
+
});
|
|
98
|
+
case 4:
|
|
99
|
+
response = _context2.sent;
|
|
100
|
+
return _context2.abrupt("return", response.data);
|
|
101
|
+
case 8:
|
|
102
|
+
_context2.prev = 8;
|
|
103
|
+
_context2.t0 = _context2["catch"](1);
|
|
104
|
+
console.error("API Error:", _context2.t0);
|
|
105
|
+
throw _context2.t0;
|
|
106
|
+
case 12:
|
|
107
|
+
case "end":
|
|
108
|
+
return _context2.stop();
|
|
109
|
+
}
|
|
110
|
+
}, _callee2, this, [[1, 8]]);
|
|
111
|
+
}));
|
|
112
|
+
function postData(_x2, _x3) {
|
|
113
|
+
return _postData.apply(this, arguments);
|
|
114
|
+
}
|
|
115
|
+
return postData;
|
|
116
|
+
}())
|
|
117
|
+
}]);
|
|
118
|
+
}();
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PesaPressLookup = void 0;
|
|
7
|
+
var _api = require("./api");
|
|
8
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
9
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
10
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
11
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
12
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
13
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
14
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
var PesaPressLookup = exports.PesaPressLookup = /*#__PURE__*/function () {
|
|
18
|
+
function PesaPressLookup() {
|
|
19
|
+
_classCallCheck(this, PesaPressLookup);
|
|
20
|
+
this.apiClient = new _api.ApiClient('https://api.pesapress.com');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Register an email
|
|
25
|
+
* @param {string} email
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
return _createClass(PesaPressLookup, [{
|
|
29
|
+
key: "register",
|
|
30
|
+
value: (function () {
|
|
31
|
+
var _register = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(email) {
|
|
32
|
+
var params, response;
|
|
33
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
34
|
+
while (1) switch (_context.prev = _context.next) {
|
|
35
|
+
case 0:
|
|
36
|
+
_context.prev = 0;
|
|
37
|
+
params = new URLSearchParams();
|
|
38
|
+
params.append("email", email);
|
|
39
|
+
_context.next = 5;
|
|
40
|
+
return this.apiClient.postData('/api/numbers/lookup/register', params, {
|
|
41
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
42
|
+
});
|
|
43
|
+
case 5:
|
|
44
|
+
response = _context.sent;
|
|
45
|
+
if (!response) {
|
|
46
|
+
_context.next = 8;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
return _context.abrupt("return", response.data);
|
|
50
|
+
case 8:
|
|
51
|
+
_context.next = 14;
|
|
52
|
+
break;
|
|
53
|
+
case 10:
|
|
54
|
+
_context.prev = 10;
|
|
55
|
+
_context.t0 = _context["catch"](0);
|
|
56
|
+
console.error("API Error:", _context.t0);
|
|
57
|
+
return _context.abrupt("return", false);
|
|
58
|
+
case 14:
|
|
59
|
+
case "end":
|
|
60
|
+
return _context.stop();
|
|
61
|
+
}
|
|
62
|
+
}, _callee, this, [[0, 10]]);
|
|
63
|
+
}));
|
|
64
|
+
function register(_x) {
|
|
65
|
+
return _register.apply(this, arguments);
|
|
66
|
+
}
|
|
67
|
+
return register;
|
|
68
|
+
}()
|
|
69
|
+
/**
|
|
70
|
+
* Check the status on the account
|
|
71
|
+
* @param {string} apikey
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
)
|
|
75
|
+
}, {
|
|
76
|
+
key: "status",
|
|
77
|
+
value: (function () {
|
|
78
|
+
var _status = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(apikey) {
|
|
79
|
+
var response;
|
|
80
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
81
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
82
|
+
case 0:
|
|
83
|
+
_context2.prev = 0;
|
|
84
|
+
_context2.next = 3;
|
|
85
|
+
return this.apiClient.postData('/api/numbers/lookup/status', {}, {
|
|
86
|
+
apikey: apikey
|
|
87
|
+
});
|
|
88
|
+
case 3:
|
|
89
|
+
response = _context2.sent;
|
|
90
|
+
if (!response) {
|
|
91
|
+
_context2.next = 6;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
return _context2.abrupt("return", response.data);
|
|
95
|
+
case 6:
|
|
96
|
+
_context2.next = 12;
|
|
97
|
+
break;
|
|
98
|
+
case 8:
|
|
99
|
+
_context2.prev = 8;
|
|
100
|
+
_context2.t0 = _context2["catch"](0);
|
|
101
|
+
console.error("API Error:", _context2.t0);
|
|
102
|
+
return _context2.abrupt("return", false);
|
|
103
|
+
case 12:
|
|
104
|
+
case "end":
|
|
105
|
+
return _context2.stop();
|
|
106
|
+
}
|
|
107
|
+
}, _callee2, this, [[0, 8]]);
|
|
108
|
+
}));
|
|
109
|
+
function status(_x2) {
|
|
110
|
+
return _status.apply(this, arguments);
|
|
111
|
+
}
|
|
112
|
+
return status;
|
|
113
|
+
}()
|
|
114
|
+
/**
|
|
115
|
+
* Search by api key. Search for the hash
|
|
116
|
+
* @param {string} apikey The API key
|
|
117
|
+
* @param {string} hash The msisdn hashs
|
|
118
|
+
* @returns
|
|
119
|
+
*/
|
|
120
|
+
)
|
|
121
|
+
}, {
|
|
122
|
+
key: "search",
|
|
123
|
+
value: (function () {
|
|
124
|
+
var _search = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(apikey, hash) {
|
|
125
|
+
var params, response;
|
|
126
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
127
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
128
|
+
case 0:
|
|
129
|
+
_context3.prev = 0;
|
|
130
|
+
params = new URLSearchParams();
|
|
131
|
+
params.append("hash", hash);
|
|
132
|
+
_context3.next = 5;
|
|
133
|
+
return this.apiClient.postData('/api/numbers/lookup/search', params, {
|
|
134
|
+
apikey: apikey,
|
|
135
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
136
|
+
});
|
|
137
|
+
case 5:
|
|
138
|
+
response = _context3.sent;
|
|
139
|
+
if (!response) {
|
|
140
|
+
_context3.next = 8;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
return _context3.abrupt("return", response.data);
|
|
144
|
+
case 8:
|
|
145
|
+
_context3.next = 14;
|
|
146
|
+
break;
|
|
147
|
+
case 10:
|
|
148
|
+
_context3.prev = 10;
|
|
149
|
+
_context3.t0 = _context3["catch"](0);
|
|
150
|
+
console.error("API Error:", _context3.t0);
|
|
151
|
+
return _context3.abrupt("return", false);
|
|
152
|
+
case 14:
|
|
153
|
+
case "end":
|
|
154
|
+
return _context3.stop();
|
|
155
|
+
}
|
|
156
|
+
}, _callee3, this, [[0, 10]]);
|
|
157
|
+
}));
|
|
158
|
+
function search(_x3, _x4) {
|
|
159
|
+
return _search.apply(this, arguments);
|
|
160
|
+
}
|
|
161
|
+
return search;
|
|
162
|
+
}())
|
|
163
|
+
}]);
|
|
164
|
+
}();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "PesaPressLookup", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _app.PesaPressLookup;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
var _app = require("./app");
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pesapress-lookup",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Mpesa lookup API by PesaPress",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "babel src --out-dir dist"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "Paul Kevin <paultitude@gmail.com>",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@types/qs": "^6.9.18",
|
|
17
|
+
"axios": "^1.7.9",
|
|
18
|
+
"qs": "^6.14.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@babel/cli": "^7.26.4",
|
|
22
|
+
"@babel/preset-env": "^7.26.9",
|
|
23
|
+
"typescript": "^5.7.3"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/sammple/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { PesaPressLookup } = require('../dist');
|
|
2
|
+
|
|
3
|
+
async function name() {
|
|
4
|
+
const pesapress = new PesaPressLookup();
|
|
5
|
+
const resp = await pesapress.register('thepaultitude@gmail.com');
|
|
6
|
+
|
|
7
|
+
console.log(resp);
|
|
8
|
+
|
|
9
|
+
console.log('---------------------------');
|
|
10
|
+
|
|
11
|
+
const status = await pesapress.status('1e0o6a1m1S0g1C1b8G57810W');
|
|
12
|
+
|
|
13
|
+
console.log(status);
|
|
14
|
+
|
|
15
|
+
console.log('---------------------------');
|
|
16
|
+
|
|
17
|
+
const statusw = await pesapress.status('123456');
|
|
18
|
+
|
|
19
|
+
console.log(statusw);
|
|
20
|
+
|
|
21
|
+
console.log('---------------------------');
|
|
22
|
+
|
|
23
|
+
const lookup = await pesapress.search('1e0o6a1m1S0g1C1b8G57810W', '56772dd63a7009833e50c7bf67ec1a5616cd20fc32221580fa3a521a73b65352')
|
|
24
|
+
|
|
25
|
+
console.log(lookup);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
name();
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosResponse, AxiosError } from "axios";
|
|
2
|
+
import qs from "qs";
|
|
3
|
+
|
|
4
|
+
interface ApiClientOptions {
|
|
5
|
+
baseURL: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
defaultHeaders?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class ApiClient {
|
|
11
|
+
|
|
12
|
+
private client: AxiosInstance;
|
|
13
|
+
|
|
14
|
+
constructor({ baseURL, timeout = 5000, defaultHeaders = {} }: ApiClientOptions) {
|
|
15
|
+
this.client = axios.create({
|
|
16
|
+
baseURL,
|
|
17
|
+
timeout,
|
|
18
|
+
headers: {
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
...defaultHeaders,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Handles API errors and logs them.
|
|
27
|
+
* @param error - Axios error
|
|
28
|
+
*/
|
|
29
|
+
private handleError(error: AxiosError): never {
|
|
30
|
+
if (error.response) {
|
|
31
|
+
console.error(`API Error (${error.response.status}):`, error.response.data);
|
|
32
|
+
} else if (error.request) {
|
|
33
|
+
console.error("No response received from API.");
|
|
34
|
+
} else {
|
|
35
|
+
console.error("Request error:", error.message);
|
|
36
|
+
}
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Sends a GET request.
|
|
42
|
+
* @param endpoint - API endpoint (e.g., "/users")
|
|
43
|
+
* @param params - Query parameters
|
|
44
|
+
* @returns Response data
|
|
45
|
+
s*/
|
|
46
|
+
async getData<T>(endpoint: string, params?: Record<string, any>): Promise<T> {
|
|
47
|
+
try {
|
|
48
|
+
const response: AxiosResponse<T> = await this.client.get(endpoint, { params });
|
|
49
|
+
return response.data;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
this.handleError(error as AxiosError);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Sends a POST request with JSON data.
|
|
57
|
+
* @param endpoint - API endpoint
|
|
58
|
+
* @param data - JSON payload
|
|
59
|
+
* @param headers - Optional extra headers
|
|
60
|
+
* @returns Response data
|
|
61
|
+
*/
|
|
62
|
+
async postData<T>(endpoint: string, data: Record<string, any>, headers?: Record<string, string>): Promise<T> {
|
|
63
|
+
try {
|
|
64
|
+
const response: AxiosResponse<T> = await this.client.post(endpoint, data, { headers });
|
|
65
|
+
return response.data;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
this.handleError(error as AxiosError);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Sends a POST request with URL-encoded form data.
|
|
74
|
+
* @param endpoint - API endpoint
|
|
75
|
+
* @param data - Form data
|
|
76
|
+
* @param headers - Optional extra headers
|
|
77
|
+
* @returns Response data
|
|
78
|
+
*/
|
|
79
|
+
async postForm<T>(endpoint: string, data: Record<string, any>, headers?: Record<string, string>): Promise<T> {
|
|
80
|
+
try {
|
|
81
|
+
const response: AxiosResponse<T> = await this.client.post(endpoint, qs.stringify(data), {
|
|
82
|
+
headers: {
|
|
83
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
84
|
+
...headers,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
return response.data;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
this.handleError(error as AxiosError);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Updates default headers dynamically (e.g., setting auth token).
|
|
95
|
+
* @param newHeaders - Headers to update
|
|
96
|
+
*/
|
|
97
|
+
setHeaders(newHeaders: Record<string, string>) {
|
|
98
|
+
Object.assign(this.client.defaults.headers.common, newHeaders);
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ApiClient } from "./api";
|
|
2
|
+
|
|
3
|
+
export class PesaPressLookup {
|
|
4
|
+
|
|
5
|
+
private apiClient: ApiClient;
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
this.apiClient = new ApiClient({ baseURL : 'https://api.pesapress.com'});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Register an email
|
|
13
|
+
* @param {string} email
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
async register( email : string ){
|
|
17
|
+
try {
|
|
18
|
+
const response = await this.apiClient.postForm<{ data: object }>('/api/numbers/lookup/register', {email : email},
|
|
19
|
+
{"Content-Type": "application/x-www-form-urlencoded"},
|
|
20
|
+
);
|
|
21
|
+
if ( response ) {
|
|
22
|
+
return response.data;
|
|
23
|
+
}
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error("API Error:", error);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check the status on the account
|
|
32
|
+
* @param {string} apikey
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
async status( apikey : string) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await this.apiClient.postForm<{ data: object }>('/api/numbers/lookup/status', {}, { apikey: apikey } );
|
|
38
|
+
if ( response ) {
|
|
39
|
+
return response.data;
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error("API Error:", error);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Search by api key. Search for the hash
|
|
49
|
+
* @param {string} apikey The API key
|
|
50
|
+
* @param {string} hash The msisdn hashs
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
async search( apikey : string, hash : string ) {
|
|
54
|
+
try {
|
|
55
|
+
const response = await this.apiClient.postForm<{ data: object }>(
|
|
56
|
+
'/api/numbers/lookup/search',
|
|
57
|
+
{ hash: hash },
|
|
58
|
+
{ apikey: apikey, "Content-Type": "application/x-www-form-urlencoded" }
|
|
59
|
+
);
|
|
60
|
+
if ( response ) {
|
|
61
|
+
return response.data;
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error("API Error:", error);
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PesaPressLookup } from "./app";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
|
|
40
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
41
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
42
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
43
|
+
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
|
|
44
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
45
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
46
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
47
|
+
|
|
48
|
+
/* JavaScript Support */
|
|
49
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
50
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
51
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
52
|
+
|
|
53
|
+
/* Emit */
|
|
54
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
55
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
56
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
57
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
58
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
59
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
60
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
61
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
62
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
63
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
64
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
65
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
66
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
67
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
68
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
69
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
70
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
71
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
72
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
73
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
74
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
75
|
+
|
|
76
|
+
/* Interop Constraints */
|
|
77
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
78
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
79
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
80
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
81
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
82
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
83
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
84
|
+
|
|
85
|
+
/* Type Checking */
|
|
86
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
87
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
88
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
89
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
90
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
91
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
92
|
+
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
|
|
93
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
94
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
95
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
96
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
97
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
98
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
99
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
100
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
101
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
102
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
103
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
104
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
105
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
106
|
+
|
|
107
|
+
/* Completeness */
|
|
108
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
109
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
110
|
+
}
|
|
111
|
+
}
|