Skip to content

Commit e84d3b8

Browse files
Code Refactoring develop
1 parent e5cf5ce commit e84d3b8

15 files changed

+1938
-179
lines changed

.eslintrc.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es2021: true
6+
},
7+
extends: [
8+
'standard'
9+
],
10+
parserOptions: {
11+
ecmaVersion: 12
12+
},
13+
rules: {
14+
camelcase: 0,
15+
'valid-jsdoc': [
16+
'error',
17+
{
18+
requireReturn: true,
19+
requireReturnType: true,
20+
requireParamDescription: false,
21+
requireReturnDescription: true
22+
}
23+
],
24+
'require-jsdoc': [
25+
'error',
26+
{
27+
require: {
28+
FunctionDeclaration: true,
29+
MethodDefinition: true,
30+
ClassDeclaration: true
31+
}
32+
}
33+
]
34+
}
35+
}

Thepeer.js

Lines changed: 0 additions & 142 deletions
This file was deleted.

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Thepeer = require('./lib/Thepeer')
2+
3+
module.exports = Thepeer

lib/Thepeer.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
const axios = require('axios').default
2+
const crypto = require('crypto')
3+
const Helper = require('./utils/Helper')
4+
5+
6+
/**
7+
* @class ThePeer
8+
*/
9+
class ThePeer {
10+
/**
11+
*This is a constructor for creating a Peer Instance
12+
* @param {string} secretkey - Thepeer secret key
13+
* @returns { ThePeer } - An instance of thePeer
14+
*/
15+
constructor (secretkey) {
16+
this.secretKey = secretkey
17+
this.request = axios.create({
18+
baseURL: 'https://api.thepeer.co',
19+
headers: {
20+
'x-api-key': secretkey,
21+
'Content-Type': 'application/json'
22+
}
23+
})
24+
}
25+
26+
/**
27+
* @static
28+
* @param {object} payload - The payload to be verified.
29+
* @param {string} signature - The signature to compare with
30+
* @returns { Boolean } - True if same signature otherwise false
31+
* @memberof ThePeer
32+
*/
33+
validateSignature (payload, signature) {
34+
return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex')
35+
}
36+
37+
/**
38+
* @param {string} name - The name of the user
39+
* @param {string} identifier - The identifier of the user
40+
* @param {string} email - The email of the user
41+
* @returns {JSON} A JSON response containing the details of the user
42+
* @memberof ThePeer
43+
*/
44+
async indexUser (name, identifier, email) {
45+
try {
46+
const response = await this.request.post('/users', {
47+
name: name,
48+
identifier: identifier,
49+
email: email
50+
})
51+
return response.data
52+
} catch (e) {
53+
Helper.processError(e)
54+
}
55+
}
56+
57+
/**
58+
* @param {string} reference - The reference returned when the user was indexed
59+
* @param {string} identifier - The identifier of the user
60+
* @returns {JSON} A JSON response containing the details of the user
61+
* @memberof ThePeer
62+
*/
63+
async updateUser (reference, identifier) {
64+
try {
65+
const response = await this.request.put(`/users/${reference}`, {
66+
identifier: identifier
67+
})
68+
return response.data
69+
} catch (e) {
70+
Helper.processError(e)
71+
}
72+
}
73+
74+
/**
75+
* @param {string} reference - The reference returned when the user was indexed
76+
* @returns {JSON} A JSON response containing the details of the user
77+
* @memberof ThePeer
78+
*/
79+
async deleteUser (reference) {
80+
try {
81+
const response = await this.request.delete(`/users/${reference}`)
82+
return response.data
83+
} catch (e) {
84+
Helper.processError(e)
85+
}
86+
}
87+
88+
/**
89+
* @param {string} reference - The reference returned when the user was indexed
90+
* @returns {JSON} A JSON response containing the details of the user
91+
* @memberof ThePeer
92+
*/
93+
async getUser (reference) {
94+
try {
95+
const response = await this.request.get(`/users/${reference}`)
96+
return response.data
97+
} catch (e) {
98+
Helper.processError(e)
99+
}
100+
}
101+
102+
/**
103+
*
104+
* @param {string} linkid - The id of a user linked account
105+
* @returns {JSON} A JSON response containing the details of the user
106+
* @memberof ThePeer
107+
*/
108+
async getLink (linkid) {
109+
try {
110+
const response = await this.request.get(`/link/${linkid}`)
111+
return response.data
112+
} catch (e) {
113+
Helper.processError(e)
114+
}
115+
}
116+
117+
/**
118+
* A function that charges your user's linked account at any time
119+
* @param {string} linkid - The id of the link
120+
* @param {integer} amount - amount in kobo
121+
* @param {string} remark - The reason for initiating a direct charge
122+
* @returns {JSON} A JSON response containing the details of the user
123+
* @memberof ThePeer
124+
*/
125+
async chargeLink (linkid, amount, remark) {
126+
try {
127+
const response = await this.request.post(`/link/${linkid}/charge`, {
128+
amount: amount,
129+
remark: remark
130+
})
131+
return response.data
132+
} catch (e) {
133+
Helper.processError(e)
134+
}
135+
}
136+
137+
/**
138+
* Authorize direct charge request via webhooks
139+
* @param {*} reference - The direct charge reference sent via webhook
140+
* @param {boolean} insufficientFunds - Status of user funds
141+
* @returns {JSON} A JSON response containing the details of the user
142+
* @memberof ThePeer
143+
*/
144+
async authorizeDirectCharge (reference, insufficientFunds) {
145+
try {
146+
const response = await this.request.post(`/debit/${reference}`, {
147+
insufficient_funds: insufficientFunds
148+
})
149+
return response.data
150+
} catch (e) {
151+
Helper.processError(e)
152+
}
153+
}
154+
}
155+
module.exports = ThePeer

lib/utils/Helper/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const crypto = require('crypto')
2+
const ForbiddenError = require('../errors/ForbiddenError')
3+
const InvalidResourceError = require('../errors/InvalidResourceError')
4+
const NotAcceptableError = require('../errors/NotAcceptableError')
5+
const ServerError = require('../errors/ServerError')
6+
const ServiceUnavailableError = require('../errors/ServiceUnavailableError')
7+
const UnauthorizedError = require('../errors/UnauthorizedError')
8+
9+
/**
10+
* @class Helper
11+
*/
12+
class Helper {
13+
/**
14+
* @static
15+
* @param {*} payload - The payload to be verified.
16+
* @param {*} signature - The signature to compare with
17+
* @returns { Boolean } - True if same signature otherwise false
18+
* @memberof Helper
19+
*/
20+
validateSignature (payload, signature) {
21+
return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex')
22+
}
23+
24+
/**
25+
*
26+
* @param {object} error - The error object
27+
* @returns {Object} - The an error instance
28+
*/
29+
static processError (error) {
30+
switch (error.response.status) {
31+
case 401:
32+
throw new UnauthorizedError({ ...error.response.data.message })
33+
case 403:
34+
throw new ForbiddenError({ ...error.response.data.message })
35+
case 404:
36+
throw new InvalidResourceError({ ...error.response.data.message })
37+
case 406:
38+
throw new NotAcceptableError({ ...error.response.data.message })
39+
case 503:
40+
throw new ServiceUnavailableError({ ...error.response.data.message })
41+
default:
42+
throw new ServerError({ ...error.response.data.message })
43+
}
44+
}
45+
}
46+
47+
module.exports = Helper

0 commit comments

Comments
 (0)