2024-03-12 19:27:50 +01:00
|
|
|
import type { RouteHandler } from "./server/api/routes.type";
|
|
|
|
|
import type { APIRouteMeta } from "./types/api";
|
2023-12-08 09:25:31 +01:00
|
|
|
|
|
|
|
|
// Why are these routes specified manually instead of using Bun's FileSystemRouter?
|
|
|
|
|
// This is to allow for compilation of the routes, so that we can minify them and
|
|
|
|
|
// node_modules in production
|
|
|
|
|
export const rawRoutes = {
|
2024-04-07 07:30:49 +02:00
|
|
|
"/api/v1/accounts": "./server/api/api/v1/accounts",
|
|
|
|
|
"/api/v1/accounts/familiar_followers":
|
|
|
|
|
"+api/v1/accounts/familiar_followers/index",
|
|
|
|
|
"/api/v1/accounts/relationships":
|
|
|
|
|
"./server/api/api/v1/accounts/relationships/index",
|
|
|
|
|
"/api/v1/accounts/search": "./server/api/api/v1/accounts/search/index",
|
|
|
|
|
"/api/v1/accounts/update_credentials":
|
|
|
|
|
"./server/api/api/v1/accounts/update_credentials/index",
|
|
|
|
|
"/api/v1/accounts/verify_credentials":
|
|
|
|
|
"./server/api/api/v1/accounts/verify_credentials/index",
|
|
|
|
|
"/api/v1/apps": "./server/api/api/v1/apps/index",
|
|
|
|
|
"/api/v1/apps/verify_credentials":
|
|
|
|
|
"./server/api/api/v1/apps/verify_credentials/index",
|
|
|
|
|
"/api/v1/blocks": "./server/api/api/v1/blocks/index",
|
|
|
|
|
"/api/v1/custom_emojis": "./server/api/api/v1/custom_emojis/index",
|
|
|
|
|
"/api/v1/favourites": "./server/api/api/v1/favourites/index",
|
|
|
|
|
"/api/v1/follow_requests": "./server/api/api/v1/follow_requests/index",
|
|
|
|
|
"/api/v1/instance": "./server/api/api/v1/instance/index",
|
|
|
|
|
"/api/v1/media": "./server/api/api/v1/media/index",
|
|
|
|
|
"/api/v1/mutes": "./server/api/api/v1/mutes/index",
|
|
|
|
|
"/api/v1/notifications": "./server/api/api/v1/notifications/index",
|
|
|
|
|
"/api/v1/profile/avatar": "./server/api/api/v1/profile/avatar",
|
|
|
|
|
"/api/v1/profile/header": "./server/api/api/v1/profile/header",
|
|
|
|
|
"/api/v1/statuses": "./server/api/api/v1/statuses/index",
|
|
|
|
|
"/api/v1/timelines/home": "./server/api/api/v1/timelines/home",
|
|
|
|
|
"/api/v1/timelines/public": "./server/api/api/v1/timelines/public",
|
|
|
|
|
"/api/v2/media": "./server/api/api/v2/media/index",
|
|
|
|
|
"/api/v2/search": "./server/api/api/v2/search/index",
|
2024-04-09 06:33:59 +02:00
|
|
|
"/api/auth/login": "./server/api/api/auth/login/index",
|
|
|
|
|
"/api/auth/redirect": "./server/api/api/auth/redirect/index",
|
2024-04-07 07:30:49 +02:00
|
|
|
"/nodeinfo/2.0": "./server/api/nodeinfo/2.0/index",
|
|
|
|
|
"/oauth/authorize-external": "./server/api/oauth/authorize-external/index",
|
|
|
|
|
"/oauth/providers": "./server/api/oauth/providers/index",
|
|
|
|
|
"/oauth/token": "./server/api/oauth/token/index",
|
|
|
|
|
"/api/v1/accounts/[id]": "./server/api/api/v1/accounts/[id]/index",
|
|
|
|
|
"/api/v1/accounts/[id]/block": "./server/api/api/v1/accounts/[id]/block",
|
|
|
|
|
"/api/v1/accounts/[id]/follow": "./server/api/api/v1/accounts/[id]/follow",
|
|
|
|
|
"/api/v1/accounts/[id]/followers":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/followers",
|
|
|
|
|
"/api/v1/accounts/[id]/following":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/following",
|
|
|
|
|
"/api/v1/accounts/[id]/mute": "./server/api/api/v1/accounts/[id]/mute",
|
|
|
|
|
"/api/v1/accounts/[id]/note": "./server/api/api/v1/accounts/[id]/note",
|
|
|
|
|
"/api/v1/accounts/[id]/pin": "./server/api/api/v1/accounts/[id]/pin",
|
|
|
|
|
"/api/v1/accounts/[id]/remove_from_followers":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/remove_from_followers",
|
|
|
|
|
"/api/v1/accounts/[id]/statuses":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/statuses",
|
|
|
|
|
"/api/v1/accounts/[id]/unblock":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/unblock",
|
|
|
|
|
"/api/v1/accounts/[id]/unfollow":
|
|
|
|
|
"./server/api/api/v1/accounts/[id]/unfollow",
|
|
|
|
|
"/api/v1/accounts/[id]/unmute": "./server/api/api/v1/accounts/[id]/unmute",
|
|
|
|
|
"/api/v1/accounts/[id]/unpin": "./server/api/api/v1/accounts/[id]/unpin",
|
|
|
|
|
"/api/v1/follow_requests/[account_id]/authorize":
|
|
|
|
|
"./server/api/api/v1/follow_requests/[account_id]/authorize",
|
|
|
|
|
"/api/v1/follow_requests/[account_id]/reject":
|
|
|
|
|
"./server/api/api/v1/follow_requests/[account_id]/reject",
|
|
|
|
|
"/api/v1/media/[id]": "./server/api/api/v1/media/[id]/index",
|
|
|
|
|
"/api/v1/statuses/[id]": "./server/api/api/v1/statuses/[id]/index",
|
|
|
|
|
"/api/v1/statuses/[id]/context":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/context",
|
|
|
|
|
"/api/v1/statuses/[id]/favourite":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/favourite",
|
|
|
|
|
"/api/v1/statuses/[id]/favourited_by":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/favourited_by",
|
|
|
|
|
"/api/v1/statuses/[id]/pin": "./server/api/api/v1/statuses/[id]/pin",
|
|
|
|
|
"/api/v1/statuses/[id]/reblog": "./server/api/api/v1/statuses/[id]/reblog",
|
|
|
|
|
"/api/v1/statuses/[id]/reblogged_by":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/reblogged_by",
|
|
|
|
|
"/api/v1/statuses/[id]/source": "./server/api/api/v1/statuses/[id]/source",
|
|
|
|
|
"/api/v1/statuses/[id]/unfavourite":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/unfavourite",
|
|
|
|
|
"/api/v1/statuses/[id]/unpin": "./server/api/api/v1/statuses/[id]/unpin",
|
|
|
|
|
"/api/v1/statuses/[id]/unreblog":
|
|
|
|
|
"./server/api/api/v1/statuses/[id]/unreblog",
|
|
|
|
|
"/media/[id]": "./server/api/media/[id]/index",
|
|
|
|
|
"/oauth/callback/[issuer]": "./server/api/oauth/callback/[issuer]/index",
|
|
|
|
|
"/object/[uuid]": "./server/api/object/[uuid]/index",
|
|
|
|
|
"/users/[uuid]": "./server/api/users/[uuid]/index",
|
|
|
|
|
"/users/[uuid]/inbox": "./server/api/users/[uuid]/inbox/index",
|
|
|
|
|
"/users/[uuid]/outbox": "./server/api/users/[uuid]/outbox/index",
|
|
|
|
|
"/[...404]": "./server/api/[...404]",
|
2024-03-13 09:10:32 +01:00
|
|
|
} as Record<string, string>;
|
2023-12-08 09:25:31 +01:00
|
|
|
|
|
|
|
|
// Returns the route filesystem path when given a URL
|
|
|
|
|
export const routeMatcher = new Bun.FileSystemRouter({
|
2024-04-07 07:30:49 +02:00
|
|
|
style: "nextjs",
|
|
|
|
|
dir: `${process.cwd()}/server/api`,
|
2023-12-08 09:25:31 +01:00
|
|
|
});
|
|
|
|
|
|
2024-03-13 09:10:32 +01:00
|
|
|
export const matchRoute = async <T = Record<string, never>>(url: string) => {
|
2024-04-07 07:30:49 +02:00
|
|
|
const route = routeMatcher.match(url);
|
|
|
|
|
if (!route) return { file: null, matchedRoute: null };
|
2023-12-08 09:25:31 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
|
|
|
|
file: (await import(rawRoutes[route.name])) as {
|
|
|
|
|
default: RouteHandler<T>;
|
|
|
|
|
meta: APIRouteMeta;
|
|
|
|
|
},
|
|
|
|
|
matchedRoute: route,
|
|
|
|
|
};
|
2023-12-08 09:25:31 +01:00
|
|
|
};
|