diff --git a/src/main.ts b/src/main.ts index b6b2cb6..6534067 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,9 @@ // Written by Brendan Lane - https://brndnln.dev/ import User from './modules/User' +import Group from './modules/Group' export { - User + User, + Group } diff --git a/src/modules/Group.ts b/src/modules/Group.ts index 7e26e8f..cc22e75 100644 --- a/src/modules/Group.ts +++ b/src/modules/Group.ts @@ -2,6 +2,7 @@ // Written by Brendan Lane - https://brndnln.dev/ import axios from 'axios' +import { AuthEntity, CreditAmount, GroupMember, PaymentEntity } from './types/Types' const groupURL = 'https://api.spookvooper.com/group' const ecoURL = 'https://api.spookvooper.com/eco' @@ -10,6 +11,150 @@ class Group { private accountid: string constructor (svid: string) { + if (!svid.startsWith('g-')) { + throw new Error('A group SVID must start with a \'g-\' to comply with the latest API changes') + } + + this.accountid = svid + } + + public async getGroup (): Promise { + return await new Promise((resolve, reject) => { + axios.get(`${groupURL}/getGroup`, { + params: { + svid: this.accountid + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } + + public async sendCredits (amount: CreditAmount, to: PaymentEntity, reason: string, auth: AuthEntity): Promise { + if (typeof to === 'string') { + return await new Promise((resolve, reject) => { + axios.get(`${ecoURL}/sendTransactionByIds`, { + params: { + from: this.accountid, + to: to, + amount: amount, + auth: auth.apikey, + detail: reason + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } else if (typeof to === 'object') { + return await new Promise((resolve, reject) => { + axios.get(`${ecoURL}/sendTransactionByIds`, { + params: { + from: this.accountid, + to: to.svid, + amount: amount, + auth: auth.apikey, + detail: reason + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } else { + throw new Error('The \'to\' parameter must be a string or an object!') + } + } + + public async doesGroupExist (): Promise { + return await new Promise((resolve, reject) => { + axios.get(`${groupURL}/doesGroupExist`, { + params: { + svid: this.accountid + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } + + public async getGroupMembers (): Promise { + return await new Promise((resolve, reject) => { + axios.get(`${groupURL}/getGroupMembers`, { + params: { + svid: this.accountid + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } + + public async hasGroupPermission (user: GroupMember, permission: string): Promise { + if (typeof user === 'string') { + return await new Promise((resolve, reject) => { + axios.get(`${groupURL}/hasGroupPermission`, { + params: { + svid: this.accountid, + usersvid: user, + permission: permission + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } else if (typeof user === 'object') { + return await new Promise((resolve, reject) => { + axios.get(`${groupURL}/hasGroupPermission`, { + params: { + svid: this.accountid, + usersvid: user.svid, + permission: permission + } + }) + .then((response) => { + resolve(response.data) + }) + .catch((error) => { + reject(error) + }) + }) + } else { + throw new Error('Argument \'user\' must either be a string or an object!') + } + } + + public get svid (): string { + return this.accountid + } + + public set svid (svid: string) { + if (!svid.startsWith('g-')) { + throw new Error('A group SVID must start with a \'g-\' to comply with the latest API changes') + } + this.accountid = svid } } diff --git a/src/modules/types/Types.ts b/src/modules/types/Types.ts index 4c074ee..aaede01 100644 --- a/src/modules/types/Types.ts +++ b/src/modules/types/Types.ts @@ -6,3 +6,5 @@ import { EntityUser } from './../interfaces/Interfaces' export type CreditAmount = string | number export type PaymentEntity = string | EntityUser export type SVStockTicker = 'B' | 'IDE' | 'NEWS' | 'POT' | 'TECH' | 'TYCO' | 'VC' | 'VNB' | 'VU' | 'X' +export type AuthEntity = EntityUser +export type GroupMember = string | EntityUser diff --git a/typings/main.d.ts b/typings/main.d.ts index e69de29..52573e1 100644 --- a/typings/main.d.ts +++ b/typings/main.d.ts @@ -0,0 +1,5 @@ +// SpookVooper API - src/main.ts +// Written by Brendan Lane - https://brndnln.dev/ +import User from './modules/User'; +import Group from './modules/Group'; +export { User, Group }; \ No newline at end of file diff --git a/typings/modules/Group.d.ts b/typings/modules/Group.d.ts new file mode 100644 index 0000000..d17f051 --- /dev/null +++ b/typings/modules/Group.d.ts @@ -0,0 +1,14 @@ +import { AuthEntity, CreditAmount, GroupMember, PaymentEntity } from './types/Types' +declare class Group { + // eslint-disable-next-line @typescript-eslint/prefer-readonly + private accountid + constructor (svid: string) + getGroup (): Promise + sendCredits (amount: CreditAmount, to: PaymentEntity, reason: string, auth: AuthEntity): Promise + doesGroupExist (): Promise + getGroupMembers (): Promise + hasGroupPermission (user: GroupMember, permission: string): Promise + get svid (): string + set svid (svid: string) +} +export default Group diff --git a/typings/modules/types/Types.d.ts b/typings/modules/types/Types.d.ts index 13a6975..423239b 100644 --- a/typings/modules/types/Types.d.ts +++ b/typings/modules/types/Types.d.ts @@ -2,3 +2,5 @@ import { EntityUser } from './../interfaces/Interfaces' export declare type CreditAmount = string | number export declare type PaymentEntity = string | EntityUser export declare type SVStockTicker = 'B' | 'IDE' | 'NEWS' | 'POT' | 'TECH' | 'TYCO' | 'VC' | 'VNB' | 'VU' | 'X' +export declare type AuthEntity = EntityUser +export declare type GroupMember = string | EntityUser