New API route format to make code cleaner

This commit is contained in:
Jesse Wierzbinski 2023-10-15 17:51:29 -10:00
parent c7b2f5b741
commit ca7d325cb1
36 changed files with 600 additions and 237 deletions

18
utils/api.ts Normal file
View file

@ -0,0 +1,18 @@
import { getConfig } from "@config";
import { APIRouteMeta } from "~types/api";
export const applyConfig = (routeMeta: APIRouteMeta) => {
const config = getConfig();
const newMeta = routeMeta;
// Apply ratelimits from config
newMeta.ratelimits.duration *= config.ratelimits.duration_coeff;
newMeta.ratelimits.max *= config.ratelimits.max_coeff;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (config.custom_ratelimits[routeMeta.route]) {
newMeta.ratelimits = config.custom_ratelimits[routeMeta.route];
}
return newMeta;
};

View file

@ -13,6 +13,7 @@ export interface ConfigType {
base_url: string;
bind: string;
bind_port: string;
banned_ips: string[];
};
validation: {
@ -67,6 +68,19 @@ export interface ConfigType {
log_requests_verbose: boolean;
log_filters: boolean;
};
ratelimits: {
duration_coeff: number;
max_coeff: number;
};
custom_ratelimits: Record<
string,
{
duration: number;
max: number;
}
>;
[key: string]: unknown;
}
@ -75,6 +89,7 @@ export const configDefaults: ConfigType = {
bind: "http://0.0.0.0",
bind_port: "8000",
base_url: "http://fediproject.localhost:8000",
banned_ips: [],
},
database: {
host: "localhost",
@ -178,6 +193,11 @@ export const configDefaults: ConfigType = {
log_requests_verbose: false,
log_filters: true,
},
ratelimits: {
duration_coeff: 1,
max_coeff: 1,
},
custom_ratelimits: {},
};
export const getConfig = () => {