Fixed itty-router Request typing

This commit is contained in:
dangered wolf 2022-07-19 13:26:44 -04:00
parent 1a84aff39f
commit e02b9fd3f3
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
2 changed files with 15 additions and 9 deletions

View file

@ -5,10 +5,10 @@ import { Strings } from './strings';
const router = Router(); const router = Router();
const statusRequest = async (request: any, event: FetchEvent, flags: InputFlags = {}) => { const statusRequest = async (request: Request, event: FetchEvent, flags: InputFlags = {}) => {
const { handle, id, mediaNumber } = request.params; const { handle, id, mediaNumber } = request.params;
const url = new URL(request.url); const url = new URL(request.url);
const userAgent = request.headers.get('User-Agent'); const userAgent = request.headers.get('User-Agent') || '';
let isBotUA = userAgent.match(/bot|facebook/gi) !== null; let isBotUA = userAgent.match(/bot|facebook/gi) !== null;
@ -27,8 +27,8 @@ const statusRequest = async (request: any, event: FetchEvent, flags: InputFlags
let statusResponse = await handleStatus( let statusResponse = await handleStatus(
event, event,
id.match(/\d{2,20}/)?.[0], id?.match(/\d{2,20}/)?.[0] || '0',
parseInt(mediaNumber || 1), parseInt(mediaNumber || '1'),
userAgent, userAgent,
flags flags
); );
@ -61,15 +61,15 @@ const statusRequest = async (request: any, event: FetchEvent, flags: InputFlags
} }
}; };
const statusDirectMediaRequest = async (request: any, event: FetchEvent) => { const statusDirectMediaRequest = async (request: Request, event: FetchEvent) => {
return await statusRequest(request, event, { direct: true }); return await statusRequest(request, event, { direct: true });
}; };
const profileRequest = async (request: any, _event: FetchEvent) => { const profileRequest = async (request: Request, _event: FetchEvent) => {
const { handle } = request.params; const { handle } = request.params;
const url = new URL(request.url); const url = new URL(request.url);
if (handle.match(/[a-z0-9_]{1,15}/gi)[0] !== handle) { if (handle.match(/[a-z0-9_]{1,15}/gi)?.[0] !== handle) {
return Response.redirect(Constants.REDIRECT_URL, 302); return Response.redirect(Constants.REDIRECT_URL, 302);
} else { } else {
return Response.redirect(`${Constants.TWITTER_ROOT}${url.pathname}`, 302); return Response.redirect(`${Constants.TWITTER_ROOT}${url.pathname}`, 302);
@ -99,7 +99,7 @@ router.get('/:handle/statuses/:id', statusRequest);
router.get('/:handle/statuses/:id/photo/:mediaNumber', statusRequest); router.get('/:handle/statuses/:id/photo/:mediaNumber', statusRequest);
router.get('/:handle/statuses/:id/video/:mediaNumber', statusRequest); router.get('/:handle/statuses/:id/video/:mediaNumber', statusRequest);
router.get('/owoembed', async (request: any) => { router.get('/owoembed', async (request: Request) => {
console.log('oembed hit!'); console.log('oembed hit!');
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
@ -128,7 +128,7 @@ router.get('/owoembed', async (request: any) => {
router.get('/:handle', profileRequest); router.get('/:handle', profileRequest);
router.get('/:handle/', profileRequest); router.get('/:handle/', profileRequest);
router.get('*', async request => { router.get('*', async (_request: Request) => {
return Response.redirect(Constants.REDIRECT_URL, 307); return Response.redirect(Constants.REDIRECT_URL, 307);
}); });

6
src/types.d.ts vendored
View file

@ -9,3 +9,9 @@ interface StatusResponse {
text?: string; text?: string;
response?: Response; response?: Response;
} }
interface Request {
params: {
[param: string]: string;
}
}