Skip to content

Commit cde5a90

Browse files
committed
fix: resolve all 117 type errors, make type check blocking in CI
- Add D1Database import and typed .first() returns in auth.ts - Fix db-mock: add missing D1Meta/D1Database properties (withSession, size_after, etc.) - Fix fixtures: remove invalid Platform.context, add createMockRequestEvent helper - Fix helpers: correct signToken import and TokenPayload fields - Fix test files: use correct route IDs, cast handler imports to bypass strict RequestEvent types - Fix cli-auth: rename state → pageState to avoid $state rune name conflict - Fix og endpoint: cast Uint8Array through unknown for BodyInit - Remove continue-on-error from CI type check step
1 parent 4404570 commit cde5a90

File tree

14 files changed

+179
-147
lines changed

14 files changed

+179
-147
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525

2626
- name: Type check
2727
run: npm run check
28-
continue-on-error: true # TODO: fix 117 pre-existing type errors, then remove this
2928

3029
- name: Run tests with coverage
3130
run: npm run test:coverage

src/lib/server/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Cookies } from '@sveltejs/kit';
2+
import type { D1Database } from '@cloudflare/workers-types';
23

34
interface TokenPayload {
45
userId: string;
@@ -55,7 +56,7 @@ export async function getCurrentUser(request: Request, cookies: Cookies, db: D1D
5556
.bind(tokenRow.id).run();
5657

5758
const user = await db.prepare('SELECT id, username, email, avatar_url FROM users WHERE id = ?')
58-
.bind(tokenRow.user_id).first();
59+
.bind(tokenRow.user_id).first<{ id: string; username: string; email: string; avatar_url: string | null }>();
5960
if (user) return user;
6061
}
6162
}
@@ -66,7 +67,7 @@ export async function getCurrentUser(request: Request, cookies: Cookies, db: D1D
6667
const payload = await verifyToken(token, secret);
6768
if (!payload) return null;
6869

69-
const user = await db.prepare('SELECT id, username, email, avatar_url FROM users WHERE id = ?').bind(payload.userId).first();
70+
const user = await db.prepare('SELECT id, username, email, avatar_url FROM users WHERE id = ?').bind(payload.userId).first<{ id: string; username: string; email: string; avatar_url: string | null }>();
7071
return user;
7172
}
7273

src/lib/test/db-mock.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ export function createMockDB(data: MockData = {}): D1Database & { data: Record<s
4242
},
4343
exec(query: string): Promise<D1Result> {
4444
throw new Error('exec() not implemented in mock');
45+
},
46+
withSession() {
47+
throw new Error('withSession() not implemented in mock');
4548
}
46-
} as D1Database & { data: Record<string, any[]> };
49+
} as unknown as D1Database & { data: Record<string, any[]> };
4750
}
4851

4952
function createMockStatement(sql: string, tables: Record<string, any[]>): D1PreparedStatement {
@@ -71,8 +74,12 @@ function createMockStatement(sql: string, tables: Record<string, any[]>): D1Prep
7174
success: true,
7275
meta: {
7376
duration: 0.1,
77+
size_after: 0,
7478
rows_read: results.length,
75-
rows_written: 0
79+
rows_written: 0,
80+
changed_db: false,
81+
changes: 0,
82+
last_row_id: 0
7683
}
7784
};
7885
},
@@ -86,10 +93,12 @@ function createMockStatement(sql: string, tables: Record<string, any[]>): D1Prep
8693
success: true,
8794
meta: {
8895
duration: 0.1,
89-
changes,
90-
last_row_id: changes ? 1 : 0,
96+
size_after: 0,
9197
rows_read: 0,
92-
rows_written: changes ? 1 : 0
98+
rows_written: changes ? 1 : 0,
99+
changed_db: changes > 0,
100+
changes,
101+
last_row_id: changes ? 1 : 0
93102
}
94103
};
95104
},

src/lib/test/fixtures.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,25 @@ export function createMockPlatform(db?: any): App.Platform {
174174
GOOGLE_CLIENT_ID: 'test-google-client-id',
175175
GOOGLE_CLIENT_SECRET: 'test-google-client-secret',
176176
APP_URL: 'http://localhost:5173'
177-
},
178-
context: {
179-
waitUntil: () => {},
180-
passThroughOnException: () => {}
181-
},
182-
caches: undefined as any,
183-
cf: undefined as any
177+
}
178+
} as App.Platform;
179+
}
180+
181+
/**
182+
* Helper to create a mock SvelteKit RequestEvent for testing route handlers.
183+
* Returns `any` to avoid strict route-id type constraints in tests.
184+
*/
185+
export function createMockRequestEvent(db: any, overrides: Record<string, any> = {}): any {
186+
return {
187+
platform: createMockPlatform(db),
188+
url: new URL('http://localhost:5173'),
189+
route: { id: '' },
190+
locals: {},
191+
isDataRequest: false,
192+
isSubRequest: false,
193+
cookies: createMockCookies(),
194+
getClientAddress: () => '127.0.0.1',
195+
fetch: globalThis.fetch,
196+
...overrides
184197
};
185198
}

src/lib/test/helpers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
* Test helper functions
33
*/
44

5-
import { sign } from '$lib/server/auth';
5+
import { signToken } from '$lib/server/auth';
66
import type { D1Database } from '@cloudflare/workers-types';
77

88
/**
99
* Creates a valid JWT token for testing
1010
*/
1111
export async function createTestJWT(userId: string, username: string): Promise<string> {
12-
return await sign(
12+
return await signToken(
1313
{
14-
sub: userId,
14+
userId,
1515
username,
16-
iat: Math.floor(Date.now() / 1000)
16+
exp: Date.now() + 3600 * 1000
1717
},
1818
'test-jwt-secret-key-32-chars-long'
1919
);

src/routes/[username]/[slug]/config/server.test.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
*/
55

66
import { describe, it, expect } from 'vitest';
7-
import { GET } from './+server';
7+
import { GET as _GET } from './+server';
8+
const GET = _GET as (event: any) => Promise<Response>;
89
import { createMockDB } from '$lib/test/db-mock';
910
import {
1011
mockUser,
@@ -35,7 +36,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
3536
platform,
3637
params: { username: 'testuser', slug: 'public-config' },
3738
url: new URL(baseUrl),
38-
route: { id: '' },
39+
route: { id: '/[username]/[slug]/config' },
3940
locals: {},
4041
isDataRequest: false,
4142
isSubRequest: false,
@@ -71,7 +72,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
7172
platform,
7273
params: { username: 'testuser', slug: 'private-config' },
7374
url: new URL(baseUrl),
74-
route: { id: '' },
75+
route: { id: '/[username]/[slug]/config' },
7576
locals: {},
7677
isDataRequest: false,
7778
isSubRequest: false,
@@ -103,7 +104,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
103104
platform,
104105
params: { username: 'testuser', slug: 'private-config' },
105106
url: new URL(baseUrl),
106-
route: { id: '' },
107+
route: { id: '/[username]/[slug]/config' },
107108
locals: {},
108109
isDataRequest: false,
109110
isSubRequest: false,
@@ -133,7 +134,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
133134
platform,
134135
params: { username: 'testuser', slug: 'private-config' },
135136
url: new URL(baseUrl),
136-
route: { id: '' },
137+
route: { id: '/[username]/[slug]/config' },
137138
locals: {},
138139
isDataRequest: false,
139140
isSubRequest: false,
@@ -163,7 +164,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
163164
platform,
164165
params: { username: 'testuser', slug: 'private-config' },
165166
url: new URL(baseUrl),
166-
route: { id: '' },
167+
route: { id: '/[username]/[slug]/config' },
167168
locals: {},
168169
isDataRequest: false,
169170
isSubRequest: false,
@@ -210,7 +211,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
210211
platform,
211212
params: { username: 'testuser', slug: 'public-config' },
212213
url: new URL(baseUrl),
213-
route: { id: '' },
214+
route: { id: '/[username]/[slug]/config' },
214215
locals: {},
215216
isDataRequest: false,
216217
isSubRequest: false,
@@ -250,7 +251,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
250251
platform,
251252
params: { username: 'testuser', slug: 'public-config' },
252253
url: new URL(baseUrl),
253-
route: { id: '' },
254+
route: { id: '/[username]/[slug]/config' },
254255
locals: {},
255256
isDataRequest: false,
256257
isSubRequest: false,
@@ -289,7 +290,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
289290
platform,
290291
params: { username: 'testuser', slug: 'public-config' },
291292
url: new URL(baseUrl),
292-
route: { id: '' },
293+
route: { id: '/[username]/[slug]/config' },
293294
locals: {},
294295
isDataRequest: false,
295296
isSubRequest: false,
@@ -328,7 +329,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
328329
platform,
329330
params: { username: 'testuser', slug: 'public-config' },
330331
url: new URL(baseUrl),
331-
route: { id: '' },
332+
route: { id: '/[username]/[slug]/config' },
332333
locals: {},
333334
isDataRequest: false,
334335
isSubRequest: false,
@@ -366,7 +367,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
366367
platform,
367368
params: { username: 'testuser', slug: 'public-config' },
368369
url: new URL(baseUrl),
369-
route: { id: '' },
370+
route: { id: '/[username]/[slug]/config' },
370371
locals: {},
371372
isDataRequest: false,
372373
isSubRequest: false,
@@ -404,7 +405,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
404405
platform,
405406
params: { username: 'testuser', slug: 'public-config' },
406407
url: new URL(baseUrl),
407-
route: { id: '' },
408+
route: { id: '/[username]/[slug]/config' },
408409
locals: {},
409410
isDataRequest: false,
410411
isSubRequest: false,
@@ -446,7 +447,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
446447
platform,
447448
params: { username: 'testuser', slug: 'public-config' },
448449
url: new URL(baseUrl),
449-
route: { id: '' },
450+
route: { id: '/[username]/[slug]/config' },
450451
locals: {},
451452
isDataRequest: false,
452453
isSubRequest: false,
@@ -482,7 +483,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
482483
platform,
483484
params: { username: 'testuser', slug: 'public-config' },
484485
url: new URL(baseUrl),
485-
route: { id: '' },
486+
route: { id: '/[username]/[slug]/config' },
486487
locals: {},
487488
isDataRequest: false,
488489
isSubRequest: false,
@@ -514,7 +515,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
514515
platform,
515516
params: { username: 'testuser', slug: 'public-config' },
516517
url: new URL(baseUrl),
517-
route: { id: '' },
518+
route: { id: '/[username]/[slug]/config' },
518519
locals: {},
519520
isDataRequest: false,
520521
isSubRequest: false,
@@ -551,7 +552,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
551552
platform,
552553
params: { username: 'testuser', slug: 'public-config' },
553554
url: new URL(baseUrl),
554-
route: { id: '' },
555+
route: { id: '/[username]/[slug]/config' },
555556
locals: {},
556557
isDataRequest: false,
557558
isSubRequest: false,
@@ -586,7 +587,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
586587
platform,
587588
params: { username: 'testuser', slug: 'public-config' },
588589
url: new URL(baseUrl),
589-
route: { id: '' },
590+
route: { id: '/[username]/[slug]/config' },
590591
locals: {},
591592
isDataRequest: false,
592593
isSubRequest: false,
@@ -618,7 +619,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
618619
platform,
619620
params: { username: 'testuser', slug: 'public-config' },
620621
url: new URL(baseUrl),
621-
route: { id: '' },
622+
route: { id: '/[username]/[slug]/config' },
622623
locals: {},
623624
isDataRequest: false,
624625
isSubRequest: false,
@@ -650,7 +651,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
650651
platform,
651652
params: { username: 'testuser', slug: 'public-config' },
652653
url: new URL(baseUrl),
653-
route: { id: '' },
654+
route: { id: '/[username]/[slug]/config' },
654655
locals: {},
655656
isDataRequest: false,
656657
isSubRequest: false,
@@ -682,7 +683,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
682683
platform,
683684
params: { username: 'testuser', slug: 'public-config' },
684685
url: new URL(baseUrl),
685-
route: { id: '' },
686+
route: { id: '/[username]/[slug]/config' },
686687
locals: {},
687688
isDataRequest: false,
688689
isSubRequest: false,
@@ -714,7 +715,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
714715
platform,
715716
params: { username: 'testuser', slug: 'public-config' },
716717
url: new URL(baseUrl),
717-
route: { id: '' },
718+
route: { id: '/[username]/[slug]/config' },
718719
locals: {},
719720
isDataRequest: false,
720721
isSubRequest: false,
@@ -734,14 +735,14 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
734735
describe('Error handling', () => {
735736
it('should return 500 when platform env missing', async () => {
736737
const request = createMockRequest({ url: baseUrl });
737-
const platform = { env: undefined };
738+
const platform = { env: undefined } as any;
738739

739740
const response = await GET({
740741
request,
741742
platform,
742743
params: { username: 'testuser', slug: 'public-config' },
743744
url: new URL(baseUrl),
744-
route: { id: '' },
745+
route: { id: '/[username]/[slug]/config' },
745746
locals: {},
746747
isDataRequest: false,
747748
isSubRequest: false,
@@ -765,7 +766,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
765766
platform,
766767
params: { username: 'nonexistent', slug: 'config' },
767768
url: new URL(baseUrl),
768-
route: { id: '' },
769+
route: { id: '/[username]/[slug]/config' },
769770
locals: {},
770771
isDataRequest: false,
771772
isSubRequest: false,
@@ -789,7 +790,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
789790
platform,
790791
params: { username: 'testuser', slug: 'nonexistent' },
791792
url: new URL(baseUrl),
792-
route: { id: '' },
793+
route: { id: '/[username]/[slug]/config' },
793794
locals: {},
794795
isDataRequest: false,
795796
isSubRequest: false,
@@ -827,7 +828,7 @@ describe('[username]/[slug]/config GET - Visibility Auth', () => {
827828
platform,
828829
params: { username: 'testuser', slug: 'private-config' },
829830
url: new URL(baseUrl),
830-
route: { id: '' },
831+
route: { id: '/[username]/[slug]/config' },
831832
locals: {},
832833
isDataRequest: false,
833834
isSubRequest: false,

0 commit comments

Comments
 (0)