Skip to content

Commit 441fa81

Browse files
authored
Merge pull request #2 from thepeerstack/initial
v0.0.1
2 parents 2b821d7 + 6905b08 commit 441fa81

17 files changed

+2112
-1
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+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2021 Peerstack Technologies Inc
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Thepeer node
2+
3+
## Installation
4+
5+
```bash
6+
npm i thepeer-node
7+
```
8+
9+
## Usage
10+
11+
```js
12+
const thepeer = new Thepeer(secretKey)
13+
14+
let user = thepeer.indexUser("Thor Odin", "thor", "[email protected]");
15+
```
16+
17+
### Available methods
18+
19+
* validateSiganture
20+
- `accepts`:
21+
- request (object)
22+
- signature (object)
23+
- `returns`: boolean
24+
25+
* getSendReceipt
26+
- `accepts`:
27+
- receipt_id (string)
28+
- `returns`: object
29+
30+
* processSendReceipt
31+
- `accepts`:
32+
- receipt_id (string)
33+
- insufficient_funds (bool)
34+
- `returns`: object
35+
36+
* indexUser
37+
- `accepts`:
38+
- name (string)
39+
- email (string)
40+
- identifier (string)
41+
- `returns`: object
42+
43+
* updateUser
44+
- `accepts`:
45+
- reference (string)
46+
- identifier (string)
47+
- `returns`: object
48+
49+
* deleteUser
50+
- `accepts`:
51+
- reference (string)
52+
- `returns`: boolean
53+
54+
* getLink
55+
- `accepts`:
56+
- lind_id (string)
57+
- `returns`: object
58+
59+
* chargeLink
60+
- `accepts`:
61+
- lind_id (string)
62+
- amount (integer)
63+
- `returns`: object
64+
65+
* authorizaDirectCharge
66+
- `accepts`:
67+
- reference (string)
68+
- insufficient_funds (bool)
69+
- `returns`: object
70+
71+
## Extra
72+
73+
Refer to the [documentation](https://docs.thepeer.co) for more information.

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: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
const axios = require('axios').default
2+
const crypto = require('crypto')
3+
const Helper = require('./utils/Helper')
4+
5+
/**
6+
* @class ThePeer
7+
*/
8+
class ThePeer {
9+
/**
10+
*This is a constructor for creating a Peer Instance
11+
* @param {string} secretkey - Thepeer secret key
12+
* @returns { ThePeer } - An instance of thePeer
13+
*/
14+
constructor (secretkey) {
15+
this.secretKey = secretkey
16+
this.request = axios.create({
17+
baseURL: 'https://api.thepeer.co',
18+
headers: {
19+
'x-api-key': secretkey,
20+
'Content-Type': 'application/json'
21+
}
22+
})
23+
}
24+
25+
/**
26+
* @static
27+
* @param {object} payload - The payload to be verified.
28+
* @param {string} signature - The signature to compare with
29+
* @returns { Boolean } - True if same signature otherwise false
30+
* @memberof ThePeer
31+
*/
32+
validateSignature (payload, signature) {
33+
return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex')
34+
}
35+
36+
/**
37+
* @param {string} name - The name of the user
38+
* @param {string} identifier - The identifier of the user
39+
* @param {string} email - The email of the user
40+
* @returns {JSON} A JSON response containing the details of the user
41+
* @memberof ThePeer
42+
*/
43+
async indexUser (name, identifier, email) {
44+
try {
45+
const response = await this.request.post('/users', {
46+
name: name,
47+
identifier: identifier,
48+
email: email
49+
})
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+
69+
return response.data
70+
} catch (e) {
71+
Helper.processError(e)
72+
}
73+
}
74+
75+
/**
76+
* @param {string} reference - The reference returned when the user was indexed
77+
* @returns {JSON} A JSON response containing the details of the user
78+
* @memberof ThePeer
79+
*/
80+
async deleteUser (reference) {
81+
try {
82+
const response = await this.request.delete(`/users/${reference}`)
83+
84+
return response.data
85+
} catch (e) {
86+
Helper.processError(e)
87+
}
88+
}
89+
90+
/**
91+
* @param {string} reference - The reference returned when the user was indexed
92+
* @returns {JSON} A JSON response containing the details of the user
93+
* @memberof ThePeer
94+
*/
95+
async getUser (reference) {
96+
try {
97+
const response = await this.request.get(`/users/${reference}`)
98+
99+
return response.data
100+
} catch (e) {
101+
Helper.processError(e)
102+
}
103+
}
104+
105+
/**
106+
*
107+
* @param {string} linkid - The id of a user linked account
108+
* @returns {JSON} A JSON response containing the details of the user
109+
* @memberof ThePeer
110+
*/
111+
async getLink (linkid) {
112+
try {
113+
const response = await this.request.get(`/link/${linkid}`)
114+
115+
return response.data
116+
} catch (e) {
117+
Helper.processError(e)
118+
}
119+
}
120+
121+
/**
122+
* A function that charges your user's linked account at any time
123+
* @param {string} linkid - The id of the link
124+
* @param {integer} amount - amount in kobo
125+
* @param {string} remark - The reason for initiating a direct charge
126+
* @returns {JSON} A JSON response containing the details of the user
127+
* @memberof ThePeer
128+
*/
129+
async chargeLink (linkid, amount, remark) {
130+
try {
131+
const response = await this.request.post(`/link/${linkid}/charge`, {
132+
amount: amount,
133+
remark: remark
134+
})
135+
136+
return response.data
137+
} catch (e) {
138+
Helper.processError(e)
139+
}
140+
}
141+
142+
/**
143+
* Authorize direct charge request via webhooks
144+
* @param {*} reference - The direct charge reference sent via webhook
145+
* @param {boolean} insufficientFunds - Status of user funds
146+
* @returns {JSON} A JSON response containing the details of the user
147+
* @memberof ThePeer
148+
*/
149+
async authorizeDirectCharge (reference, insufficientFunds) {
150+
try {
151+
const response = await this.request.post(`/debit/${reference}`, {
152+
insufficient_funds: insufficientFunds
153+
})
154+
155+
return response.data
156+
} catch (e) {
157+
Helper.processError(e)
158+
}
159+
}
160+
161+
/**
162+
* @memberof ThePeer
163+
* @param {string} receipt - The reference returned to your receiptURL via the Chain SDK
164+
* @param {boolean} insufficientFunds - Status of user funds
165+
* @returns {JSON} A JSON response containing the details of the user
166+
*/
167+
async processSendReceipt (receipt, insufficientFunds) {
168+
try {
169+
const response = await this.request.post(`/send/${receipt}`, {
170+
insufficient_funds: insufficientFunds
171+
})
172+
173+
return response.data
174+
} catch (e) {
175+
Helper.processError(e)
176+
}
177+
}
178+
179+
/**
180+
* @memberof ThePeer
181+
* @param {string} receipt - The reference returned to your receiptURL via the Chain SDK
182+
* @returns {JSON} A JSON response containing the details of the user
183+
*/
184+
async getSendReceipt (receipt) {
185+
try {
186+
const response = await this.request.get(`/send/${receipt}`)
187+
188+
return response.data
189+
} catch (e) {
190+
Helper.processError(e)
191+
}
192+
}
193+
}
194+
195+
module.exports = ThePeer

lib/utils/Helper/index.js

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

lib/utils/errors/BaseError.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* A custom error class for handling module related errors.
3+
* @class BaseError
4+
*/
5+
module.exports = class BaseError extends Error {
6+
/**
7+
* The BaseError Constructor.
8+
* @param {Object} options - A configuration object for errors.
9+
* @param {String} options.message - The error message if any.
10+
* @constructor BaseError
11+
*/
12+
constructor (options = {}) {
13+
super()
14+
Error.captureStackTrace(this, this.constructor)
15+
this.name = this.constructor.name
16+
this.message = options.message
17+
}
18+
}

0 commit comments

Comments
 (0)