Improving TypeScript typing/interfaces.

This commit is contained in:
servous 2022-05-28 12:27:17 +02:00
parent 8537fcbded
commit f960ef3cec
8 changed files with 21 additions and 33 deletions

View file

@ -65,6 +65,7 @@
"husky": "8.0.1",
"jest": "28.0.0",
"lint-staged": "^12.3.7",
"nodemon": "^2.0.16",
"prettier": "^2.6.0"
},
"lint-staged": {

View file

@ -1,22 +0,0 @@
// 3rd party dependencies
import mongoose from "mongoose";
// Dependencies
import logger from "@logger";
// Configuration
import { url } from "@config/database";
export default async () => {
await mongoose.connect(url).then(async (connection) => {
logger.info(`Connected to database: ${connection.connection.name}`);
});
mongoose.connection.on("error", async (error) => {
logger.error(`${error}`);
});
mongoose.connection.on("warn", async (warning) => {
logger.warn(warning);
});
};

View file

@ -83,7 +83,7 @@ export default async (interaction: CommandInteraction) => {
`Command: ${commandName} executed in guild: ${guild?.name} (${guild?.id}) by user: ${user?.tag} (${user?.id})`
);
})
.catch(async (error: any) => {
.catch(async (error: string) => {
logger?.error(`${error}`);
return interaction.editReply({

View file

@ -1,12 +1,17 @@
import crypto from "crypto";
// @ts-ignore
import { secretKey, algorithm } from "@config/encryption";
const iv = crypto.randomBytes(16);
const encrypt = (text: any): { iv: any; content: any } => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
interface IEncrypt {
iv: string;
content: string;
}
const encrypt = (text: crypto.BinaryLike): IEncrypt => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
@ -15,7 +20,7 @@ const encrypt = (text: any): { iv: any; content: any } => {
};
};
const decrypt = (hash: any) => {
const decrypt = (hash: IEncrypt) => {
const decipher = crypto.createDecipheriv(
algorithm,
secretKey,

View file

@ -1,6 +1,6 @@
import logger from "@logger";
export default function sleep(milliseconds: any) {
export default function sleep(milliseconds: number) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
logger?.silly(`Sleeping for ${milliseconds} milliseconds`);

View file

@ -45,8 +45,9 @@ export default {
const { options, guild } = interaction;
// Get options
const url = options?.getString("url");
const token = encryption.encrypt(options?.getString("token"));
const tokenData = options.getString("token");
const url = options.getString("url");
const token = tokenData && encryption.encrypt(tokenData);
// Update API credentials
await apiSchema

View file

@ -147,10 +147,12 @@ export default {
guildId: guild?.id,
});
if (!apiCredentials) return;
const api = axios?.create({
baseURL: apiCredentials?.url,
baseURL: apiCredentials.url,
headers: {
Authorization: `Bearer ${encryption.decrypt(apiCredentials?.token)}`,
Authorization: `Bearer ${encryption.decrypt(apiCredentials.token)}`,
},
});
@ -235,7 +237,7 @@ export default {
});
})
.catch(async (error: any) => {
.catch(async (error) => {
logger?.silly(`Error creating voucher. - ${error}`);
return interaction?.editReply({

View file

@ -1,7 +1,8 @@
import { Collection, Client as DJSClient } from "discord.js";
declare module "discord.js" {
export interface Client extends DJSClient {
commands: Collection<unknown, any>;
commands: Collection;
}
}