Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Revamping #161

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TURBO_API="https://turbo-cache.b68.dev"
TURBO_TEAM="team_shx"
TURBO_TOKEN="TURBO@b68"
TURBO_REMOTE_CACHE_SIGNATURE_KEY="TURBO@b68"
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"author": "Jyotirmoy Bandyopadhyaya [Bravo68] <[email protected]>",
"license": "MIT",
"private": true,
"workspaces": [
"packages/*"
],
"scripts": {
"dev": "dotnev -- turbo dev",
"build": "dotenv -- turbo build",
"start": "dotenv -- turbo start",
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,css,scss,md}\"",
"prepare": "husky install",
"configure-husky": "npx husky install && npx husky add .husky/pre-commit \"npx --no-install lint-staged\""
Expand All @@ -27,10 +27,12 @@
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.62.0",
"eslint-config-prettier": "^9.0.0",
"@typescript-eslint/parser": "^5.62.0",
"dotenv-cli": "^7.3.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"prettier": "^2.8.8"
"prettier": "^2.8.8",
"turbo": "^1.10.14"
}
}
5 changes: 4 additions & 1 deletion packages/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ uploads/*.mp4
uploads/*.mp3
uploads/*.zip
uploads/*.rar
uploads/*.pdf
uploads/*.pdf

# CF secrets
secrets.json
75 changes: 33 additions & 42 deletions packages/api/controllers/apikey.controller.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,80 @@
import { NextFunction, Response } from 'express';
import { Context } from 'hono';
import APIKeyService from '../services/apikey.service';
import { ModRequest } from '../types';
import { makeResponse } from '../libs';
import { IAPIKeyController } from '../interfaces/apikey.interface';
import { CustomError } from '../libs/error';

export default class APIKeyController
extends APIKeyService
implements IAPIKeyController
{
public list = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
let apikeys;
let metadata;
try {
const { masterkey } = req.query as { masterkey: string };
const { masterkey } = ctx.req.query() as { masterkey: string };
const { data, meta } = await this.listS(masterkey);
apikeys = data;
metadata = meta;
} catch (error) {
return next(error);
return ctx.json({
error,
}, 401)
}
return res.status(200).json(makeResponse(apikeys, metadata));
return ctx.json(makeResponse(apikeys, metadata));
};

public generate = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
let apikey;
try {
const { masterkey } = req.query as { masterkey: string };
const { masterkey } = ctx.req.query() as { masterkey: string };
apikey = await this.generateS(masterkey);
} catch (error: any) {
return next(
new CustomError({
message: error.message,
statusCode: 502,
})
);
return ctx.json({
error,
}, 401)
}
return res.status(200).json(makeResponse(apikey));
return ctx.json(makeResponse(apikey), 201);
};

public revoke = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
let delKey;
try {
const { masterkey, apikeyID } = req.query as {
const { masterkey, apikeyID } = ctx.req.query() as {
masterkey: string;
apikeyID: string;
};
delKey = await this.deleteS(apikeyID, masterkey);
} catch (error) {
return next(error);
return ctx.json({
error,
}, 401);
}
if (delKey === 0)
return next(
new CustomError({
message: 'API Key not found',
statusCode: 404,
})
);
return res.status(200).json(makeResponse({ message: 'API Key revoked' }));
return ctx.json({
message: 'API Key not found',
statusCode: 404,
}, 404)
return ctx.json(makeResponse({ message: 'API Key revoked' }), 202);
};

public verify = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
let result;
try {
const { apikey } = req.params as { apikey: string };
const { apikey } = ctx.req.param() as { apikey: string };
result = await this.verifyS(apikey);
} catch (error) {
return next(error);
return ctx.json({
error,
}, 401);
}
return res.status(200).json(makeResponse({ result }));
return ctx.json(makeResponse({ result }), 200);
};
}
67 changes: 32 additions & 35 deletions packages/api/controllers/config.controller.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
import { NextFunction, Response } from 'express';
import { Context } from 'hono';
import ConfigService from '../services/config.service';
import { ModRequest } from '../types';
import { makeResponse } from '../libs';
import {
IConfigController,
ConfigKeysTypes,
} from '../interfaces/config.interface';
import { CustomError } from '../libs/error';
import { Bindings, Variables } from "../types"

export default class ConfigController
extends ConfigService
implements IConfigController
{
public getAllConfig = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context<{
Bindings: Bindings,
Variables: Variables
}>
) => {
let config;
try {
config = await this.getAllConfigS();
config = await this.getAllConfigS(ctx);
} catch (error) {
return next(error);
return ctx.json({
error,
})
}
return res.status(200).json(makeResponse(config));
return ctx.json(makeResponse(config));
};

public setConfig = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
try {
const { key, value } = req.body;
const { key, value } = await ctx.req.json();
if (!key || !value)
throw new CustomError({
statusCode: 400,
message: 'Invalid Request',
});
await this.setConfigS(key, value);
res.status(201).json(
throw new Error("Invalid Request");
await this.setConfigS(ctx, key, value);
return ctx.json(
makeResponse({
message: 'Config updated successfully',
})
}),
201
);
} catch (error) {
next(error);
return ctx.json({
error,
})
}
};

public getConfig = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
try {
const { key } = req.params as { key: ConfigKeysTypes };
const { key } = ctx.req.param() as { key: ConfigKeysTypes };
if (!key)
throw new CustomError({
statusCode: 400,
message: 'Invalid Request',
});
const config = await this.getConfigS(key);
res.status(200).json(makeResponse(config));
throw new Error("Invalid Request");
const config = await this.getConfigS(ctx, key);
ctx.json(makeResponse(config));
} catch (error) {
next(error);
return ctx.json({
error,
})
}
};
}
Loading