Fix any type usage in the saas/ folder (#5934)

# Description of Changes
Ages ago I made #4835 to try and fix all the `any` type usage in the
system but never got it finished, and there were just too many to review
and ensure it still worked. There's even more now.

My new tactic is to fix folder by folder. This fixes the `any` typing in
the `saas/` folder, and also enables `no-unnecessary-type-assertion`,
which really helps reduce pointless `as` casts that AI generates when
the type is already known. I hope to expand both of these to the rest of
the folders soon, but one folder is better than none.
This commit is contained in:
James Brunton
2026-03-16 11:51:16 +00:00
committed by GitHub
parent 1722733802
commit dbff05814f
22 changed files with 123 additions and 112 deletions
+24 -28
View File
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { Session, AuthError } from '@supabase/supabase-js';
import { supabase } from '@app/auth/supabase';
// Mock supabase
@@ -20,13 +21,13 @@ describe('apiClient', () => {
it('should add JWT token to request headers when session exists', async () => {
const mockToken = 'test-jwt-token-12345';
const mockSession: any = {
const mockSession = {
access_token: mockToken,
refresh_token: 'refresh-token',
expires_in: 3600,
token_type: 'bearer',
user: { id: 'user-123' },
};
} as unknown as Session;
// Mock getSession to return a session with token
vi.mocked(supabase.auth.getSession).mockResolvedValue({
@@ -99,21 +100,21 @@ describe('apiClient', () => {
const oldToken = 'old-token';
const newToken = 'new-refreshed-token';
const oldSession: any = {
const oldSession = {
access_token: oldToken,
refresh_token: 'refresh-token',
expires_in: 3600,
token_type: 'bearer',
user: { id: 'user-123' },
};
} as unknown as Session;
const newSession: any = {
const newSession = {
access_token: newToken,
refresh_token: 'new-refresh-token',
expires_in: 3600,
token_type: 'bearer',
user: { id: 'user-123' },
};
} as unknown as Session;
// Mock initial session for first request
let getSessionCallCount = 0;
@@ -129,8 +130,8 @@ describe('apiClient', () => {
// Mock refresh to return new session
vi.mocked(supabase.auth.refreshSession).mockResolvedValue({
data: { user: null, session: newSession },
error: null as any,
} as any);
error: null,
});
// Import apiClient after mocking
const { default: apiClient } = await import('@app/services/apiClient');
@@ -143,12 +144,10 @@ describe('apiClient', () => {
if (requestCount === 1) {
// Verify first request has old token
expect(config.headers.Authorization).toBe(`Bearer ${oldToken}`);
const error: any = new Error('Unauthorized');
error.response = {
status: 401,
data: { error: 'Unauthorized' },
};
error.config = config;
const error = Object.assign(new Error('Unauthorized'), {
response: { status: 401, data: { error: 'Unauthorized' } },
config,
});
return Promise.reject(error);
}
@@ -187,35 +186,32 @@ describe('apiClient', () => {
// Mock initial session
vi.mocked(supabase.auth.getSession).mockResolvedValue({
data: { session: oldSession },
data: { session: oldSession as unknown as Session },
error: null,
} as any);
});
vi.mocked(supabase.auth.getSession).mockResolvedValue({
data: { session: oldSession },
data: { session: oldSession as unknown as Session },
error: null,
} as any);
});
// Mock refresh to fail
vi.mocked(supabase.auth.refreshSession).mockResolvedValue({
data: { user: null, session: null },
error: { name: 'AuthError', message: 'Refresh failed', status: 400, code: 'auth_error', __isAuthError: true } as any,
} as any);
error: { name: 'AuthError', message: 'Refresh failed', status: 400, code: 'auth_error' } as unknown as AuthError,
});
// Import apiClient after mocking
const { default: apiClient } = await import('@app/services/apiClient');
// Mock window.location for redirect test
delete (window as any).location;
window.location = { href: '' } as any;
Object.defineProperty(window, 'location', { writable: true, value: { href: '' } });
const mockAdapter = vi.fn((config) => {
// Always return 401 to trigger refresh
const error: any = new Error('Unauthorized');
error.response = {
status: 401,
data: { error: 'Unauthorized' },
};
error.config = config;
const error = Object.assign(new Error('Unauthorized'), {
response: { status: 401, data: { error: 'Unauthorized' } },
config,
});
return Promise.reject(error);
});