Skip to content

Commit

Permalink
feat: added type checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Moore committed Jul 13, 2023
1 parent fce1b30 commit cb41aea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
40 changes: 40 additions & 0 deletions backend/UserConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
import fs from 'fs-extra';
import { UserConfiguration, UserConfigTypeChecker } from 'ass';

/**
* Returns a boolean if the provided value is a number
*/
const numChecker = (val: string) => {
try { parseInt(val); return true; }
catch (err) { return false; }
}

/**
* User-config property type checker functions
*/
const Checkers: UserConfigTypeChecker = {
uploadsDir: (val) => {
try { fs.accessSync(val); return true; }
catch (err) { return false; }
},
idType: (val) => {
const options = ['random', 'original', 'gfycat', 'timestamp', 'zws'];
return options.includes(val);
},
idSize: numChecker,
gfySize: numChecker,
maximumFileSize: numChecker,
}

export class UserConfig {
private config: UserConfiguration;
public getConfig = () => this.config;

constructor(config?: UserConfiguration) {
if (config != null) this.config = this.parseConfig(config);
}

private parseConfig(config: UserConfiguration) {
if (!Checkers.uploadsDir(config.uploadsDir)) throw new Error(`Unable to access uploads directory: ${config.uploadsDir}`);
if (!Checkers.idType(config.idType)) throw new Error(`Invalid ID type: ${config.idType}`);
if (!Checkers.idSize(config.idSize)) throw new Error(`Invalid ID size: ${config.idSize}`);
if (!Checkers.gfySize(config.gfySize)) throw new Error(`Invalid Gfy size: ${config.gfySize}`);
if (!Checkers.maximumFileSize(config.maximumFileSize)) throw new Error(`Invalid maximum file size: ${config.maximumFileSize}`);

// All is fine, carry on!
return config;
}
}
3 changes: 2 additions & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"lib": [
"ES2022",
],
"moduleResolution": "Node"
"moduleResolution": "Node",
"strictPropertyInitialization": false
},
"include": [
"./**/*.ts",
Expand Down
8 changes: 8 additions & 0 deletions common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ declare module 'ass' {
gfySize: number;
maximumFileSize: number;
}

interface UserConfigTypeChecker {
uploadsDir: (val: any) => boolean;
idType: (val: any) => boolean;
idSize: (val: any) => boolean;
gfySize: (val: any) => boolean;
maximumFileSize: (val: any) => boolean;
}
}

//#region Dummy modules
Expand Down

0 comments on commit cb41aea

Please sign in to comment.