generated from BRAVO68WEB/typescript-express-hasura-pgsql-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05f9112
commit b05b584
Showing
33 changed files
with
1,297 additions
and
1,871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
}; | ||
} |
Oops, something went wrong.