Skip to content

Commit

Permalink
feat: switch to Hono api
Browse files Browse the repository at this point in the history
  • Loading branch information
BRAVO68WEB committed Sep 21, 2023
1 parent 05f9112 commit b05b584
Show file tree
Hide file tree
Showing 33 changed files with 1,297 additions and 1,871 deletions.
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);
};
}
57 changes: 25 additions & 32 deletions packages/api/controllers/config.controller.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,63 @@
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';

export default class ConfigController
extends ConfigService
implements IConfigController
{
public getAllConfig = async (
req: ModRequest,
res: Response,
next: NextFunction
): Promise<Response | void> => {
ctx: Context
) => {
let config;
try {
config = await this.getAllConfigS();
} 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',
});
throw new Error("Invalid Request");
await this.setConfigS(key, value);
res.status(201).json(
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',
});
throw new Error("Invalid Request");
const config = await this.getConfigS(key);
res.status(200).json(makeResponse(config));
ctx.json(makeResponse(config));
} catch (error) {
next(error);
return ctx.json({
error,
})
}
};
}
Loading

0 comments on commit b05b584

Please sign in to comment.