mirror of
https://github.com/wahyd4/heygo.git
synced 2026-08-09 05:15:52 +10:00
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import type { Env } from './env';
|
|
import { withPrivateNoStoreHeaders } from './lib/responses';
|
|
import { handleLinksApi } from './routes/api.links';
|
|
import { handlePromotionsApi } from './routes/api.promotions';
|
|
import { handlePrivateShortlink, isHeygoPrivateHost } from './routes/private-redirect';
|
|
import { handlePublicShortlink, isHeygoPublicHost, isReservedPublicPath } from './routes/redirect';
|
|
|
|
export type { Env } from './env';
|
|
|
|
const jsonHeaders = {
|
|
'content-type': 'application/json; charset=utf-8',
|
|
};
|
|
|
|
function json(body: unknown, init: ResponseInit = {}) {
|
|
return Response.json(body, {
|
|
...init,
|
|
headers: {
|
|
...jsonHeaders,
|
|
...init.headers,
|
|
},
|
|
});
|
|
}
|
|
|
|
function withPrivateHostNoStoreHeaders(url: URL, response: Response): Response {
|
|
return isHeygoPrivateHost(url) ? withPrivateNoStoreHeaders(response) : response;
|
|
}
|
|
|
|
export default {
|
|
async fetch(request, env, ctx): Promise<Response> {
|
|
const url = new URL(request.url);
|
|
|
|
if (url.pathname === '/api/health') {
|
|
return withPrivateHostNoStoreHeaders(url, json({ ok: true, service: 'heygo-worker' }));
|
|
}
|
|
|
|
if (url.pathname.startsWith('/api/')) {
|
|
const apiResponse = await handleLinksApi(request, env);
|
|
if (apiResponse) {
|
|
return withPrivateHostNoStoreHeaders(url, apiResponse);
|
|
}
|
|
const promotionsResponse = await handlePromotionsApi(request, env);
|
|
if (promotionsResponse) {
|
|
return withPrivateHostNoStoreHeaders(url, promotionsResponse);
|
|
}
|
|
return withPrivateHostNoStoreHeaders(url, json({ error: 'Not found' }, { status: 404 }));
|
|
}
|
|
|
|
if (isHeygoPublicHost(url) && !isReservedPublicPath(url.pathname)) {
|
|
return handlePublicShortlink(request, env, ctx);
|
|
}
|
|
|
|
if (isHeygoPrivateHost(url) && !isReservedPublicPath(url.pathname)) {
|
|
return handlePrivateShortlink(request, env, ctx);
|
|
}
|
|
|
|
return withPrivateHostNoStoreHeaders(url, json({ error: 'Not found' }, { status: 404 }));
|
|
},
|
|
} satisfies ExportedHandler<Env>;
|