From 104f6439dddca3016c1858ef5a5b5cfb56dc76aa Mon Sep 17 00:00:00 2001 From: Sidemen19 Date: Tue, 20 Oct 2020 17:24:12 +1100 Subject: [PATCH] typescript rewrite --- .gitignore | 13 ++++- package.json | 12 +++-- src/index.ts | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 28 ++++++++++ 4 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index d35bbf7..fb22d77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,11 @@ -node_modules/ -.idea/ \ No newline at end of file +# deps +node_modules + +# ide settings +.idea + +# compiled +dist + +# dep tree +pnpm-lock.yaml \ No newline at end of file diff --git a/package.json b/package.json index fc2140a..2640175 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "vacefron", - "version": "1.1.0", - "description": "A wrapper for the VACEfron API in JavaScript.", - "main": "src/index.js", + "version": "2.0.0", + "description": "A wrapper for the VACEfron API in TypeScript.", + "main": "dist/index.js", + "types": "dist/index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -10,7 +11,8 @@ "api", "wrapper", "api-wrapper", - "vacefron" + "vacefron", + "typescript" ], "homepage": "https://github.com/Sidemen19/VACEfron.js", "repository": { @@ -23,6 +25,8 @@ "author": "Sidemen19", "license": "MIT", "dependencies": { + "@types/node": "^14.11.10", + "@types/node-fetch": "^2.5.7", "node-fetch": "^2.6.1" } } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..f8fe85a --- /dev/null +++ b/src/index.ts @@ -0,0 +1,143 @@ +import fetch from 'node-fetch'; + +class VACEFronJS { + baseURL: string; + crewmateColors: string[]; + + public constructor() { + this.baseURL = `https://vacefron.nl/api`; + + this.crewmateColors = [ + 'black', + 'blue', + 'brown', + 'cyan', + 'darkgreen', + 'lime', + 'orange', + 'pink', + 'purple', + 'red', + 'white', + 'yellow' + ]; + } + + protected async api(endpoint: string, parameters: object): Promise { + for (const [key, value] of Object.entries(parameters)) { + if (value === undefined) { + throw new Error(`Param "${key}" of VACEfron endpoint "${endpoint}" is undefined.`); + } + } + + // @ts-ignore + const params = new URLSearchParams(parameters); + const response = await fetch(`${this.baseURL}/${endpoint}?${params}`); + + if (response.status !== 200) { + const body = await response.json(); + throw new Error(`"${endpoint}" endpoint: ${body.message} (status ${body.code})`); + } + + return response.buffer(); + } + + public carReverse(text: string): Promise { + return this.api('carreverse', { text: text }); + } + + distractedBoyfriend(boyfriendAvatarURL: string, womanAvatarURL: string, girlfriendAvatarURL: string): Promise { + return this.api('distractedbf', { + boyfriend: boyfriendAvatarURL, + woman: womanAvatarURL, + girlfriend: girlfriendAvatarURL + }); + } + + ejected(name: string, wasImposter?: boolean, color?: string): Promise { + if (!wasImposter) { + wasImposter = Math.random() >= 0.5; + } + + if (!color || !this.crewmateColors.includes(color.toLowerCase())) { + color = this.crewmateColors[Math.floor(Math.random() * this.crewmateColors.length)]; + } + + return this.api('ejected', { + name: name, + imposter: wasImposter, + crewmate: color.toLowerCase() + }); + } + + emergencyMeeting(text: string): Promise { + return this.api('emergencymeeting', { text: text }); + } + + firstTime(avatarURL: string): Promise { + return this.api('firsttime', { user: avatarURL }); + } + + grave(avatarURL: string): Promise { + return this.api('grave', { user: avatarURL }); + } + + iAmSpeed(avatarURL: string): Promise { + return this.api('icanmilkyou', { user: avatarURL }); + } + + iCanMilkYou(faceAvatarURL: string, cowAvatarURL: string): Promise { + return this.api('iamspeed', { + user1: faceAvatarURL, + user2: cowAvatarURL + }); + } + + heaven(avatarURL: string): Promise { + return this.api('heaven', { user: avatarURL }); + } + + rankCard( + username: string, + avatarURL: string, + customBackgroundURL: string, + level: number, + rank: number, + currentXP: number, + nextLevelXP: number, + previousLevelXP: number, + xpColor: string, + isBoosting: boolean + ): Promise { + return this.api('rankcard', { + username: encodeURIComponent(username), + avatar: avatarURL, + custombg: customBackgroundURL, + level: level, + rank: rank, + currentxp: currentXP, + nextlevelxp: nextLevelXP, + previouslevelxp: previousLevelXP, + xpcolor: xpColor.replace('#', ''), + isboosting: isBoosting + }); + } + + stonks(avatarURL: string): Promise { + return this.api('stonks', { user: avatarURL }); + } + + tableFlip(avatarURL: string): Promise { + return this.api('tableflip', { user: avatarURL }); + } + + water(text: string): Promise { + return this.api('water', { text: text }); + } + + wide(imageURL: string): Promise { + return this.api('wide', { image: imageURL }); + } +} + +export = new VACEFronJS(); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f9fce0e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "strict": true, + "moduleResolution": "node", + "target": "esnext", + "lib": [ + "esnext", + "dom" + ], + "declaration": true, + "removeComments": false, + "alwaysStrict": true, + "pretty": true, + "noEmitHelpers": true, + "importHelpers": true, + "skipLibCheck": true, + "esModuleInterop": true, + "module": "commonjs", + "outDir": "dist", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "allowSyntheticDefaultImports": true, + "skipDefaultLibCheck": true, + "resolveJsonModule": true + }, + "include": ["src"], + "exclude": ["node_modules"] +}