Skip to content

Commit 61a1123

Browse files
committed
lint fix
1 parent 2291a05 commit 61a1123

7 files changed

Lines changed: 77 additions & 46 deletions

File tree

src/app/[locale]/feeds/[feedDataType]/[feedId]/lib/feed-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type FeedData = FeedDataResult;
2727
*
2828
* Each user gets their own cached version that persists across page navigations
2929
* (e.g., /feeds/gtfs/mdb-123 → /feeds/gtfs/mdb-123/map)
30-
*
30+
*
3131
* Revalidation is short due to the per-user-per-feed cache, but can be adjusted based on needs.
3232
*/
3333
export const fetchCompleteFeedData = cache(

src/app/api/revalidate/route.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ const defaultRevalidateOptions: RevalidateBody = {
2525
gbfsFeedIds: [],
2626
};
2727

28-
function json(status: number, body: Record<string, unknown>) {
29-
return NextResponse.json(body, { status });
30-
}
31-
32-
export async function POST(req: Request) {
28+
export async function POST(req: Request): Promise<NextResponse> {
3329
const expectedSecret = process.env.REVALIDATE_SECRET;
34-
if (!expectedSecret) {
35-
return json(500, {
36-
ok: false,
37-
error: 'Server misconfigured: REVALIDATE_SECRET missing',
38-
});
30+
if (expectedSecret == null) {
31+
return NextResponse.json(
32+
{ ok: false, error: 'Server misconfigured: REVALIDATE_SECRET missing' },
33+
{ status: 500 },
34+
);
3935
}
4036

4137
const providedSecret = req.headers.get('x-revalidate-secret');
42-
if (!providedSecret || providedSecret !== expectedSecret) {
43-
return json(401, { ok: false, error: 'Unauthorized' });
38+
if (providedSecret == null || providedSecret !== expectedSecret) {
39+
return NextResponse.json(
40+
{ ok: false, error: 'Unauthorized' },
41+
{ status: 401 },
42+
);
4443
}
4544

4645
let payload: RevalidateBody = { ...defaultRevalidateOptions }; // default to full revalidation if body is missing/invalid
@@ -114,15 +113,18 @@ export async function POST(req: Request) {
114113
});
115114
}
116115

117-
return json(200, {
116+
return NextResponse.json({
118117
ok: true,
119118
message: 'Revalidation triggered successfully',
120119
});
121120
} catch (error) {
122121
console.error('Revalidation failed:', error);
123-
return json(500, {
124-
ok: false,
125-
error: 'Failed to revalidate',
126-
});
122+
return NextResponse.json(
123+
{
124+
ok: false,
125+
error: 'Failed to revalidate',
126+
},
127+
{ status: 500 },
128+
);
127129
}
128130
}

src/app/screens/Feed/FeedView.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ export default async function FeedView({
123123
maxWidth='xl'
124124
>
125125
{/* TODO: remove this timestamp after confirming ISR is working in production and providing real value to users (e.g. helps with debugging feed updates) */}
126-
<div>
127-
Generated at: {new Date().toISOString()}
128-
</div>
126+
<div>Generated at: {new Date().toISOString()}</div>
129127
<CssBaseline />
130128
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
131129
<Box

src/app/utils/proxy-helpers.spec.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
import type { NextRequest, NextResponse as NextResponseType } from 'next/server';
21
import {
32
isFeedDetailPage,
4-
isAuthenticatedNotGuest,
53
rewriteFeedRequest,
64
hasLocaleInPathname,
75
rewriteWithDefaultLocale,
86
DEFAULT_LOCALE,
97
AUTHED_PROXY_HEADER,
108
STATIC_PROXY_HEADER,
119
} from './proxy-helpers';
12-
import * as sessionJwt from './session-jwt';
1310

1411
// Mock the session-jwt module
1512
jest.mock('./session-jwt');
1613

1714
// Helper to create mock NextRequest
18-
function createMockNextRequest(overrides: Partial<any> = {}): any {
15+
/* eslint-disable @typescript-eslint/consistent-type-assertions */
16+
function createMockNextRequest(
17+
overrides: Record<string, unknown> = {},
18+
): Record<string, unknown> {
1919
return {
2020
nextUrl: {
21-
clone: jest.fn(function (this: any) {
21+
clone: jest.fn(function (this: Record<string, unknown>) {
2222
const url = new URL('http://localhost');
2323
url.pathname = '/original';
2424
return url;
2525
}),
26-
} as any,
26+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
27+
} as Record<string, unknown>,
2728
headers: new Headers(),
2829
cookies: {
2930
get: jest.fn(),
30-
} as any,
31+
} as Record<string, unknown>,
3132
...overrides,
3233
};
3334
}
35+
/* eslint-enable @typescript-eslint/consistent-type-assertions */
3436

3537
// Mock routing module
3638
jest.mock('../../i18n/routing', () => ({
@@ -41,14 +43,17 @@ jest.mock('../../i18n/routing', () => ({
4143
}));
4244

4345
// Spy on NextResponse.rewrite
46+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
4447
let rewriteSpy: jest.SpyInstance;
48+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
4549
let NextResponse: any;
4650

4751
describe('proxy-helpers', () => {
4852
beforeAll(async () => {
4953
// Dynamically import NextResponse to avoid module loading issues
5054
const nextServer = await import('next/server');
51-
NextResponse = nextServer.NextResponse;
55+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
56+
NextResponse = nextServer.NextResponse as any;
5257
rewriteSpy = jest.spyOn(NextResponse, 'rewrite');
5358
});
5459

@@ -135,6 +140,7 @@ describe('proxy-helpers', () => {
135140
// ============================================================================
136141

137142
describe('rewriteFeedRequest', () => {
143+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
138144
let mockRequest: any;
139145

140146
beforeEach(() => {
@@ -278,7 +284,9 @@ describe('proxy-helpers', () => {
278284
it('should handle paths with multiple segments', () => {
279285
mockUrl = new URL('http://localhost/feeds/gbfs/test-456/map/details');
280286
rewriteWithDefaultLocale(mockUrl);
281-
expect(mockUrl.pathname).toBe(`/${DEFAULT_LOCALE}/feeds/gbfs/test-456/map/details`);
287+
expect(mockUrl.pathname).toBe(
288+
`/${DEFAULT_LOCALE}/feeds/gbfs/test-456/map/details`,
289+
);
282290
});
283291

284292
it('should return NextResponse', () => {

src/app/utils/proxy-helpers.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function isFeedDetailPage(pathname: string): FeedDetailPageInfo {
5757
export function isAuthenticatedNotGuest(request: NextRequest): boolean {
5858
const sessionCookie = request.cookies.get('md_session');
5959
const userData = verifySessionToken(sessionCookie?.value ?? '');
60-
const isAuthenticated = userData != null ? !userData.isGuest : false;
60+
const isAuthenticated = !(userData?.isGuest === true);
6161

6262
return isAuthenticated;
6363
}
@@ -91,10 +91,17 @@ export interface RewriteFeedRequestParams {
9191
*/
9292
export function rewriteFeedRequest(
9393
request: NextRequest,
94-
{ locale, feedDataType, feedId, subPath, routeType }: RewriteFeedRequestParams,
94+
{
95+
locale,
96+
feedDataType,
97+
feedId,
98+
subPath,
99+
routeType,
100+
}: RewriteFeedRequestParams,
95101
): NextResponse {
96102
const url = request.nextUrl.clone();
97-
const headerName = routeType === 'authed' ? AUTHED_PROXY_HEADER : STATIC_PROXY_HEADER;
103+
const headerName =
104+
routeType === 'authed' ? AUTHED_PROXY_HEADER : STATIC_PROXY_HEADER;
98105
url.pathname = `/${locale}/feeds/${feedDataType}/${feedId}/${routeType}${subPath}`;
99106

100107
const headers = createRequestWithHeader(request, headerName);
@@ -113,7 +120,8 @@ export function rewriteFeedRequest(
113120
*/
114121
export function hasLocaleInPathname(pathname: string): boolean {
115122
return routing.locales.some(
116-
(locale: string) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
123+
(locale: string) =>
124+
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
117125
);
118126
}
119127

src/proxy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ export default function proxy(request: NextRequest): NextResponse<unknown> {
5959
const feedDetailPageInfo = isFeedDetailPage(pathname);
6060
if (
6161
feedDetailPageInfo.match &&
62-
feedDetailPageInfo.feedDataType &&
63-
feedDetailPageInfo.feedId
62+
feedDetailPageInfo.feedDataType != null &&
63+
feedDetailPageInfo.feedDataType !== '' &&
64+
feedDetailPageInfo.feedId != null &&
65+
feedDetailPageInfo.feedId !== ''
6466
) {
6567
const isAuthenticated = isAuthenticatedNotGuest(request);
6668
const locale = feedDetailPageInfo.locale ?? DEFAULT_LOCALE;

src/setupTests.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,33 @@ jest.mock('next-intl/server', () => ({
5757

5858
// Mock next/server for middleware tests
5959
jest.mock('next/server', () => {
60-
const createMockResponse = (data?: any, init?: any) => ({
61-
body: data,
62-
status: init?.status || 200,
63-
json: jest.fn().mockResolvedValue(data),
64-
ok: (init?.status || 200) < 400,
65-
headers: new Headers(init?.headers),
66-
});
60+
const createMockResponse = (
61+
data?: unknown,
62+
init?: Record<string, unknown>,
63+
): Record<string, unknown> => {
64+
const status = typeof init?.status === 'number' ? init.status : 200;
65+
const statusValue = typeof init?.status === 'number' ? init.status : 200;
66+
return {
67+
body: data,
68+
status,
69+
json: jest.fn().mockResolvedValue(data),
70+
ok: statusValue < 400,
71+
headers: new Headers(
72+
typeof init?.headers === 'object'
73+
? (init.headers as HeadersInit)
74+
: undefined,
75+
),
76+
};
77+
};
6778

6879
return {
6980
NextResponse: {
7081
next: jest.fn(() => createMockResponse()),
71-
rewrite: jest.fn((url, config) => createMockResponse()),
72-
redirect: jest.fn((url) => createMockResponse()),
73-
json: jest.fn((data, init) => createMockResponse(data, init)),
82+
rewrite: jest.fn((url: unknown, config: unknown) => createMockResponse()),
83+
redirect: jest.fn((url: unknown) => createMockResponse()),
84+
json: jest.fn((data: unknown, init: unknown) =>
85+
createMockResponse(data, init as Record<string, unknown>),
86+
),
7487
},
7588
NextRequest: jest.fn(),
7689
};

0 commit comments

Comments
 (0)