From acf527caad2af518dc10b6adafd8496cd5d92b1c Mon Sep 17 00:00:00 2001 From: Huboh <72440222+huboh@users.noreply.github.com> Date: Sun, 9 Jan 2022 00:30:15 +0100 Subject: [PATCH 1/2] remove JSDoc annotations from primary module --- lib/Thepeer.js | 160 ++++++++++++++----------------------------------- 1 file changed, 45 insertions(+), 115 deletions(-) diff --git a/lib/Thepeer.js b/lib/Thepeer.js index 388f507..883d0d8 100644 --- a/lib/Thepeer.js +++ b/lib/Thepeer.js @@ -1,195 +1,125 @@ -const axios = require('axios').default -const crypto = require('crypto') -const Helper = require('./utils/Helper') +const axios = require('axios').default; +const crypto = require('crypto'); +const Helper = require('./utils/Helper'); -/** - * @class ThePeer - */ class ThePeer { - /** - *This is a constructor for creating a Peer Instance - * @param {string} secretkey - Thepeer secret key - * @returns { ThePeer } - An instance of thePeer - */ - constructor (secretkey) { - this.secretKey = secretkey + constructor(secretkey) { + this.secretKey = secretkey; this.request = axios.create({ baseURL: 'https://api.thepeer.co', headers: { 'x-api-key': secretkey, 'Content-Type': 'application/json' } - }) + }); } - /** - * @static - * @param {object} payload - The payload to be verified. - * @param {string} signature - The signature to compare with - * @returns { Boolean } - True if same signature otherwise false - * @memberof ThePeer - */ - validateSignature (payload, signature) { - return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex') + validateSignature(payload, signature) { + return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex'); } - /** - * @param {string} name - The name of the user - * @param {string} identifier - The identifier of the user - * @param {string} email - The email of the user - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async indexUser (name, identifier, email) { + async indexUser(name, identifier, email) { try { const response = await this.request.post('/users', { name: name, identifier: identifier, email: email - }) + }); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * @param {string} reference - The reference returned when the user was indexed - * @param {string} identifier - The identifier of the user - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async updateUser (reference, identifier) { + async updateUser(reference, identifier) { try { const response = await this.request.put(`/users/${reference}`, { identifier: identifier - }) + }); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * @param {string} reference - The reference returned when the user was indexed - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async deleteUser (reference) { + async deleteUser(reference) { try { - const response = await this.request.delete(`/users/${reference}`) + const response = await this.request.delete(`/users/${reference}`); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * @param {string} reference - The reference returned when the user was indexed - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async getUser (reference) { + async getUser(reference) { try { - const response = await this.request.get(`/users/${reference}`) + const response = await this.request.get(`/users/${reference}`); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * - * @param {string} linkid - The id of a user linked account - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async getLink (linkid) { + async getLink(linkid) { try { - const response = await this.request.get(`/link/${linkid}`) + const response = await this.request.get(`/link/${linkid}`); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * A function that charges your user's linked account at any time - * @param {string} linkid - The id of the link - * @param {integer} amount - amount in kobo - * @param {string} remark - The reason for initiating a direct charge - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async chargeLink (linkid, amount, remark) { + async chargeLink(linkid, amount, remark) { try { const response = await this.request.post(`/link/${linkid}/charge`, { amount: amount, remark: remark - }) + }); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * Authorize direct charge request via webhooks - * @param {*} reference - The direct charge reference sent via webhook - * @param {string} event - Event type (success, insufficient_funds) - * @returns {JSON} A JSON response containing the details of the user - * @memberof ThePeer - */ - async authorizeDirectCharge (reference, event) { + async authorizeDirectCharge(reference, event) { try { const response = await this.request.post(`/debit/${reference}`, { event: event - }) + }); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * @memberof ThePeer - * @param {string} receipt - The reference returned to your receiptURL via the Chain SDK - * @param {string} event - Event type (success, insufficient_funds) - * @returns {JSON} A JSON response containing the details of the user - */ - async processSendReceipt (receipt, event) { + async processSendReceipt(receipt, event) { try { const response = await this.request.post(`/send/${receipt}`, { event: event - }) + }); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } - /** - * @memberof ThePeer - * @param {string} receipt - The reference returned to your receiptURL via the Chain SDK - * @returns {JSON} A JSON response containing the details of the user - */ - async getSendReceipt (receipt) { + async getSendReceipt(receipt) { try { - const response = await this.request.get(`/send/${receipt}`) + const response = await this.request.get(`/send/${receipt}`); - return response.data + return response.data; } catch (e) { - Helper.processError(e) + Helper.processError(e); } } } -module.exports = ThePeer +module.exports = ThePeer; From 4e0be3fbe06aa822ebe46fb4ad6047b5ec7da129 Mon Sep 17 00:00:00 2001 From: Huboh <72440222+huboh@users.noreply.github.com> Date: Sun, 9 Jan 2022 02:21:31 +0100 Subject: [PATCH 2/2] add type declaration file & TSDoc annotations --- index.d.ts | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..de37cd1 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,165 @@ +import { BinaryLike } from 'crypto'; +import { AxiosInstance } from "axios"; + +export type Event = 'success' | 'insufficient_funds' | 'business_decline' | 'user_decline'; + +export type Status = 'success' | 'failed' | 'pending'; + +export interface DeleteResponse { + message: 'user deleted'; +} + +export interface IndexedUser { + name: string; + email: string; + identifier: string; + reference: string; + /** + * The primary means of user identification on your platform. There are various type of identifier types which ranges from + * `email`, `username`, `phone number`, `Account ID` etc. However, we currently support `username` and `email`. + */ + identifier_type: string; +} + +export interface User extends IndexedUser { + created_at: string; + updated_at: string; +} + +export interface Business extends Pick { + id: string; + logo: string; + logo_colour: string; +} + +/** + * The Peer object contains objects of the user and business the transaction is being carried out with + */ +export interface Peer { + user: User; + business: Business; +} + +/** + * A connection between your user's account on your platform to another user's account on another platform on Thepeer. + * With links, your users can perform a Direct charge via our APIs or available SDKs. + */ +export interface Link { + id: string; + user: User; + peer: Peer; + created_at: string; + updated_at: string; +} + +/** + * The transaction object contains all the information about a transaction. it is usually returend after calling + * an authorizing endpoint (`process receipt` and `authorize direct charge`) and also usually received via webhook. + */ +export interface Transaction extends Link { + type: string; + remark: string; + refund: boolean; + channel: string; + amount: number; + status: Status; + reference: string; + mode: 'credit' | 'debit'; + /** + * This object should contain additional/optional values passed from the SDK + */ + meta?: { + city: string; + state: string; + }; +} + +declare class Thepeer { + + public secretKey: string; + private request: AxiosInstance; + + /** + * creates Thepeer Instance + * @param secretKey - Thepeer secret key + */ + constructor(secretKey: string); + + /** + * + * @param payload - The payload to be verified. + * @param signature - The signature to compare with + * @returns a boolean indicating if the signature is valid + */ + validateSignature(payload: BinaryLike, signature: string): boolean; + + /** + * This function index your users In order to make your `users` resolvable on the SDK, you need to index them on Thepeer. + * Your `identifier` must match the identifier type registered on your business, at the time of writing + * this we currently support `username` and `email`. + * @param name - The name of the user + * @param identifier - The identifier of the user + * @param email - The email of the user + */ + indexUser(name: string, identifier: string, email: string): Promise; + + /** + * This function allows you update your user's identifier when they make a profile update to their identifier on your platform. + * @param reference - The `reference` returned when the user was indexed + * @param identifier - The identifier of the user + */ + updateUser(reference: string, identifier: string): Promise; + + /** + * This function allows you delete a user in the event that a user deactivates their account on your platform. + * @param reference - The `reference` returned when the user was indexed + */ + deleteUser(reference: string): Promise; + + /** + * + * @param reference - The `reference` returned when the user was indexed + */ + getUser(reference: string): Promise; + + /** + * This function returns a linked account details by passing its `ID`. + * @param linkId - The id of a user linked account + */ + getLink(linkId: string): Promise; + + /** + * This function allows you to charge your user's linked account at any time, as long as the other business in + * which the direct charge is being caried on approves the direct charge request via its webhook. + * @param linkId - The id of the link + * @param amount - amount in kobo + * @param remark - The reason for initiating a direct charge + */ + chargeLink(linkId: string, amount: number, remark: string): Promise; + + /** + * This function authorize direct charge request via webhooks, for every time a direct charge request is made via our APIs or via our SDK; + * We send a webhook with in a format like this. You are expected to extract the reference so you can use it to authorize the direct charge + * request. + * @param reference - The direct charge `reference` sent via webhook + * @param event - Event type `success`, `insufficient_funds` + */ + authorizeDirectCharge(reference: string, event: Event): Promise; + + /** + * This function is used to process receipts generated from Thepeer inline (chain.js). + * Once this function is successfully called we debit both parties involved and send a webhook to the + * receiving party notifying them of the transaction. + * @param receipt - The `reference` returned to your receiptURL via the Chain SDK + * @param event - Event type `success`, `insufficient_funds` + */ + processSendReceipt(receipt: string, event: Event): Promise | undefined>; + + /** + * + * @param receipt - The `reference` returned to your receiptURL via the Chain SDK + */ + getSendReceipt(receipt: string): Promise | undefined>; +} + +export default Thepeer; \ No newline at end of file diff --git a/package.json b/package.json index 99ec5a3..1855672 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.1", "description": "The official node package for Thepeer", "main": "index.js", + "types": "index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -34,4 +35,4 @@ "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^5.1.0" } -} +} \ No newline at end of file