mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 13:36:08 +00:00

* feat(discord): soundboard api * chore: apply formatting * fix(gateway): events * feat(events): soundboard * chore: apply formatting * fix: xd * feat(api): soundboard routes * chore: apply formatting --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com>
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { CDN_URL } from '../common';
|
|
import type { APIRoutes, ApiHandler, CDNRoute } from './index';
|
|
import type { HttpMethods, ImageExtension, ImageSize, SoundExtension, StickerExtension } from './shared';
|
|
|
|
export enum ProxyRequestMethod {
|
|
Delete = 'delete',
|
|
Get = 'get',
|
|
Patch = 'patch',
|
|
Post = 'post',
|
|
Put = 'put',
|
|
}
|
|
|
|
const ArrRequestsMethods = Object.freeze(Object.values(ProxyRequestMethod)) as string[];
|
|
|
|
export class Router {
|
|
noop = () => {
|
|
return;
|
|
};
|
|
|
|
constructor(private rest: ApiHandler) {}
|
|
|
|
createProxy(route = [] as string[]): APIRoutes {
|
|
return new Proxy(this.noop, {
|
|
get: (_, key: string) => {
|
|
if (ArrRequestsMethods.includes(key)) {
|
|
return (...options: any[]) =>
|
|
this.rest.request(key.toUpperCase() as HttpMethods, `/${route.join('/')}`, ...options);
|
|
}
|
|
return this.createProxy([...route, key]);
|
|
},
|
|
apply: (...[, _, args]) => {
|
|
return this.createProxy([...route, ...args]);
|
|
},
|
|
}) as unknown as APIRoutes;
|
|
}
|
|
}
|
|
|
|
export const CDNRouter = {
|
|
createProxy(route = [] as string[]): CDNRoute {
|
|
const noop = () => {
|
|
return;
|
|
};
|
|
return new Proxy(noop, {
|
|
get: (_, key: string) => {
|
|
if (key === 'get') {
|
|
return (value: string | CDNUrlOptions | undefined, options?: CDNUrlOptions) => {
|
|
const lastRoute = `${CDN_URL}/${route.join('/')}`;
|
|
let routeResult = lastRoute;
|
|
if (typeof value === 'string' || typeof value === 'number') {
|
|
routeResult = `${lastRoute}${value ? `/${value}` : ''}`;
|
|
return parseCDNURL(routeResult, options);
|
|
}
|
|
return parseCDNURL(routeResult, value);
|
|
};
|
|
}
|
|
return this.createProxy([...route, key]);
|
|
},
|
|
apply: (...[, _, args]) => {
|
|
return this.createProxy([...route, ...args]);
|
|
},
|
|
}) as unknown as CDNRoute;
|
|
},
|
|
};
|
|
|
|
export interface BaseCDNUrlOptions {
|
|
extension?: ImageExtension | StickerExtension | SoundExtension;
|
|
size?: ImageSize;
|
|
}
|
|
|
|
export interface CDNUrlOptions extends BaseCDNUrlOptions {
|
|
forceStatic?: boolean;
|
|
}
|
|
|
|
export function parseCDNURL(route: string, options: CDNUrlOptions = {}) {
|
|
if (options.forceStatic && route.includes('a_')) options.extension = 'png';
|
|
if (!options.extension && route.includes('a_')) options.extension = 'gif';
|
|
|
|
options.extension ||= route.includes('soundboard') ? 'ogg' : 'png';
|
|
|
|
const url = new URL(`${route}.${options.extension}`);
|
|
|
|
if (options.size) url.searchParams.set('size', `${options.size}`);
|
|
|
|
return url.toString();
|
|
}
|