Skip to content

Commit

Permalink
fix: redirection in router handler
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Nov 12, 2024
1 parent 482986d commit 91d9acc
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 13 deletions.
6 changes: 6 additions & 0 deletions lib/middleware/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ describe('template', () => {
expect(parsed.items[0].enclosure?.length).toBe('3661');
expect(parsed.items[0].itunes.duration).toBe('10:10:10');
});

it(`redirect`, async () => {
const response = await app.request('/test/redirect');
expect(response.status).toBe(301);
expect(response.headers.get('location')).toBe('/test/1');
});
});
4 changes: 3 additions & 1 deletion lib/middleware/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
return ctx.json(result);
}

if (ctx.get('no-content')) {
if (ctx.get('redirect')) {
return ctx.redirect(ctx.get('redirect'), 301);
} else if (ctx.get('no-content')) {
return ctx.body(null);
} else {
// retain .ums for backward compatibility
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/141jav/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function handler(ctx) {
const $ = load(response.data);

if (getSubPath(ctx) === '/') {
ctx.redirect(`/141jav${$('.overview').first().attr('href')}`);
ctx.set('redirect', `/141jav${$('.overview').first().attr('href')}`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/141ppv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function handler(ctx) {
const $ = load(response.data);

if (getSubPath(ctx) === '/') {
ctx.redirect(`/141ppv${$('.overview').first().attr('href')}`);
ctx.set('redirect', `/141ppv${$('.overview').first().attr('href')}`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/aqara/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ function handler(ctx) {

const { region = 'en', type = 'news' } = ctx.req.param();
const redirectTo = `/aqara/${region}/category/${types[type]}`;
ctx.redirect(redirectTo);
ctx.set('redirect', redirectTo);
}
2 changes: 1 addition & 1 deletion lib/routes/cs/zzkx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ function handler(ctx) {
// https://www.cs.com.cn/sylm/jsbd/

const redirectTo = '/cs/sylm/jsbd';
ctx.redirect(redirectTo);
ctx.set('redirect', redirectTo);
}
2 changes: 1 addition & 1 deletion lib/routes/dongqiudi/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export const route: Route = {
};

function handler(ctx) {
ctx.redirect('/dongqiudi/special/48');
ctx.set('redirect', '/dongqiudi/special/48');
}
2 changes: 1 addition & 1 deletion lib/routes/hostmonit/cloudflareyesv6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const route: Route = {
};

function handler(ctx) {
ctx.redirect('/hostmonit/cloudflareyes/v6');
ctx.set('redirect', '/hostmonit/cloudflareyes/v6');
}
2 changes: 1 addition & 1 deletion lib/routes/jiemian/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ function handler(ctx) {
const id = ctx.req.param('id');

const redirectTo = `/jiemian${id ? `/lists/${id}` : ''}`;
ctx.redirect(redirectTo);
ctx.set('redirect', redirectTo);
}
2 changes: 1 addition & 1 deletion lib/routes/liulinblog/itnews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export const route: Route = {
function handler(ctx) {
const { channel } = ctx.req.param();
const redirectTo = `/liulinblog/${channel}`;
ctx.redirect(redirectTo);
ctx.set('redirect', redirectTo);
}
2 changes: 1 addition & 1 deletion lib/routes/nbd/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export const route: Route = {
};

function handler(ctx) {
ctx.redirect('/nbd/332');
ctx.set('redirect', '/nbd/332');
}
2 changes: 1 addition & 1 deletion lib/routes/scmp/coronavirus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export const route: Route = {
};

function handler(ctx) {
ctx.redirect('/scmp/topics/coronavirus-pandemic-all-stories');
ctx.set('redirect', '/scmp/topics/coronavirus-pandemic-all-stories');
}
4 changes: 4 additions & 0 deletions lib/routes/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ async function handler(ctx) {
if (ctx.req.param('id') === 'invalid-parameter-error') {
throw new InvalidParameterError('Test invalid parameter error');
}
if (ctx.req.param('id') === 'redirect') {
ctx.set('redirect', '/test/1');
return;
}
let item: DataItem[] = [];
let image: string | null = null;
switch (ctx.req.param('id')) {
Expand Down
5 changes: 3 additions & 2 deletions lib/routes/weibo/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ async function handler(ctx) {
ctx.set({
'Cache-Control': 'no-cache',
});
ctx.redirect(`https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${routeParams}` : ''}`);
ctx.set('redirect', `https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${routeParams}` : ''}`);
return;
}
const resultItem = await Promise.all(
response.statuses.map(async (item) => {
Expand Down Expand Up @@ -183,6 +184,6 @@ async function handler(ctx) {
ctx.set({
'Cache-Control': 'no-cache',
});
ctx.redirect(`https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${feature}/${routeParams.replaceAll('&', '%26')}` : ''}`);
ctx.set('redirect', `https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${feature}/${routeParams.replaceAll('&', '%26')}` : ''}`);
}
}

0 comments on commit 91d9acc

Please sign in to comment.