Skip to content

Commit af334d1

Browse files
Stricter TypeScript (#40)
1 parent 97c6674 commit af334d1

File tree

20 files changed

+145
-182
lines changed

20 files changed

+145
-182
lines changed

_examples/node-ts/server-fastify/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ app.get('/json', async (req, reply) => {
110110
})
111111

112112
// Fallback 404 handler for all unmatched routes
113-
app.setNotFoundHandler((req, reply) => {
113+
app.setNotFoundHandler((_req, reply) => {
114114
reply.code(404).send({ msg: 'not found' })
115115
})
116116

_examples/node-ts/server-fastify/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "node-ts-server",
33
"version": "1.0.0",
44
"description": "",
5+
"type": "module",
56
"main": "index.js",
67
"scripts": {
78
"start": "tsx index.ts"
@@ -11,9 +12,10 @@
1112
"license": "ISC",
1213
"packageManager": "[email protected]",
1314
"devDependencies": {
15+
"@fastify/cors": "^11.1.0",
16+
"@tsconfig/strictest": "^2.0.6",
1417
"@types/node": "^24.8.1",
1518
"fastify": "^5.6.1",
16-
"@fastify/cors": "^11.1.0",
1719
"tsx": "^4.20.6",
1820
"typescript": "^5.9.3"
1921
}

_examples/node-ts/server-fastify/pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/node-ts/server-fastify/server.gen.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const serveExampleRpc = async <Context>(service: ExampleServer<Context>,
8282
if (!urlPath.startsWith('/rpc/')) return null
8383
const parts = urlPath.split('/').filter(Boolean)
8484
if (parts.length !== 3 || parts[0] !== 'rpc' || parts[1] !== 'Example') return null
85-
const method = parts[2]
85+
const method = parts[2]!
8686
try {
8787
const result = await dispatchExampleRequest(service, ctx, method, body)
8888
return {
@@ -235,19 +235,16 @@ const validateType = (value: any, type: string) => {
235235
type WebrpcErrorParams = { name?: string, code?: number, message?: string, status?: number, cause?: string }
236236

237237
export class WebrpcError extends Error {
238-
name: string
239238
code: number
240-
message: string
241239
status: number
242-
cause?: string
243240

244241
constructor(error: WebrpcErrorParams = {}) {
245242
super(error.message)
246243
this.name = error.name || 'WebrpcEndpointError'
247244
this.code = typeof error.code === 'number' ? error.code : 0
248245
this.message = error.message || `endpoint error`
249246
this.status = typeof error.status === 'number' ? error.status : 400
250-
this.cause = error.cause
247+
if (error.cause !== undefined) this.cause = error.cause
251248
Object.setPrototypeOf(this, WebrpcError.prototype)
252249
}
253250

@@ -264,7 +261,7 @@ export class WebrpcEndpointError extends WebrpcError {
264261
this.code = typeof error.code === 'number' ? error.code : 0
265262
this.message = error.message || `endpoint error`
266263
this.status = typeof error.status === 'number' ? error.status : 400
267-
this.cause = error.cause
264+
if (error.cause !== undefined) this.cause = error.cause
268265
Object.setPrototypeOf(this, WebrpcEndpointError.prototype)
269266
}
270267
}
@@ -276,7 +273,7 @@ export class WebrpcRequestFailedError extends WebrpcError {
276273
this.code = typeof error.code === 'number' ? error.code : -1
277274
this.message = error.message || `request failed`
278275
this.status = typeof error.status === 'number' ? error.status : 400
279-
this.cause = error.cause
276+
if (error.cause !== undefined) this.cause = error.cause
280277
Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype)
281278
}
282279
}
@@ -288,7 +285,7 @@ export class WebrpcBadRouteError extends WebrpcError {
288285
this.code = typeof error.code === 'number' ? error.code : -2
289286
this.message = error.message || `bad route`
290287
this.status = typeof error.status === 'number' ? error.status : 404
291-
this.cause = error.cause
288+
if (error.cause !== undefined) this.cause = error.cause
292289
Object.setPrototypeOf(this, WebrpcBadRouteError.prototype)
293290
}
294291
}
@@ -300,7 +297,7 @@ export class WebrpcBadMethodError extends WebrpcError {
300297
this.code = typeof error.code === 'number' ? error.code : -3
301298
this.message = error.message || `bad method`
302299
this.status = typeof error.status === 'number' ? error.status : 405
303-
this.cause = error.cause
300+
if (error.cause !== undefined) this.cause = error.cause
304301
Object.setPrototypeOf(this, WebrpcBadMethodError.prototype)
305302
}
306303
}
@@ -312,7 +309,7 @@ export class WebrpcBadRequestError extends WebrpcError {
312309
this.code = typeof error.code === 'number' ? error.code : -4
313310
this.message = error.message || `bad request`
314311
this.status = typeof error.status === 'number' ? error.status : 400
315-
this.cause = error.cause
312+
if (error.cause !== undefined) this.cause = error.cause
316313
Object.setPrototypeOf(this, WebrpcBadRequestError.prototype)
317314
}
318315
}
@@ -324,7 +321,7 @@ export class WebrpcBadResponseError extends WebrpcError {
324321
this.code = typeof error.code === 'number' ? error.code : -5
325322
this.message = error.message || `bad response`
326323
this.status = typeof error.status === 'number' ? error.status : 500
327-
this.cause = error.cause
324+
if (error.cause !== undefined) this.cause = error.cause
328325
Object.setPrototypeOf(this, WebrpcBadResponseError.prototype)
329326
}
330327
}
@@ -336,7 +333,7 @@ export class WebrpcServerPanicError extends WebrpcError {
336333
this.code = typeof error.code === 'number' ? error.code : -6
337334
this.message = error.message || `server panic`
338335
this.status = typeof error.status === 'number' ? error.status : 500
339-
this.cause = error.cause
336+
if (error.cause !== undefined) this.cause = error.cause
340337
Object.setPrototypeOf(this, WebrpcServerPanicError.prototype)
341338
}
342339
}
@@ -348,7 +345,7 @@ export class WebrpcInternalErrorError extends WebrpcError {
348345
this.code = typeof error.code === 'number' ? error.code : -7
349346
this.message = error.message || `internal error`
350347
this.status = typeof error.status === 'number' ? error.status : 500
351-
this.cause = error.cause
348+
if (error.cause !== undefined) this.cause = error.cause
352349
Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype)
353350
}
354351
}
@@ -360,7 +357,7 @@ export class WebrpcClientAbortedError extends WebrpcError {
360357
this.code = typeof error.code === 'number' ? error.code : -8
361358
this.message = error.message || `request aborted by client`
362359
this.status = typeof error.status === 'number' ? error.status : 400
363-
this.cause = error.cause
360+
if (error.cause !== undefined) this.cause = error.cause
364361
Object.setPrototypeOf(this, WebrpcClientAbortedError.prototype)
365362
}
366363
}
@@ -372,7 +369,7 @@ export class WebrpcStreamLostError extends WebrpcError {
372369
this.code = typeof error.code === 'number' ? error.code : -9
373370
this.message = error.message || `stream lost`
374371
this.status = typeof error.status === 'number' ? error.status : 400
375-
this.cause = error.cause
372+
if (error.cause !== undefined) this.cause = error.cause
376373
Object.setPrototypeOf(this, WebrpcStreamLostError.prototype)
377374
}
378375
}
@@ -384,7 +381,7 @@ export class WebrpcStreamFinishedError extends WebrpcError {
384381
this.code = typeof error.code === 'number' ? error.code : -10
385382
this.message = error.message || `stream finished`
386383
this.status = typeof error.status === 'number' ? error.status : 200
387-
this.cause = error.cause
384+
if (error.cause !== undefined) this.cause = error.cause
388385
Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype)
389386
}
390387
}
@@ -401,7 +398,7 @@ export class UnauthorizedError extends WebrpcError {
401398
this.code = typeof error.code === 'number' ? error.code : 1000
402399
this.message = error.message || `Unauthorized access`
403400
this.status = typeof error.status === 'number' ? error.status : 401
404-
this.cause = error.cause
401+
if (error.cause !== undefined) this.cause = error.cause
405402
Object.setPrototypeOf(this, UnauthorizedError.prototype)
406403
}
407404
}
@@ -413,7 +410,7 @@ export class PermissionDeniedError extends WebrpcError {
413410
this.code = typeof error.code === 'number' ? error.code : 1001
414411
this.message = error.message || `Permission denied`
415412
this.status = typeof error.status === 'number' ? error.status : 403
416-
this.cause = error.cause
413+
if (error.cause !== undefined) this.cause = error.cause
417414
Object.setPrototypeOf(this, PermissionDeniedError.prototype)
418415
}
419416
}
@@ -425,7 +422,7 @@ export class SessionExpiredError extends WebrpcError {
425422
this.code = typeof error.code === 'number' ? error.code : 1002
426423
this.message = error.message || `Session expired`
427424
this.status = typeof error.status === 'number' ? error.status : 403
428-
this.cause = error.cause
425+
if (error.cause !== undefined) this.cause = error.cause
429426
Object.setPrototypeOf(this, SessionExpiredError.prototype)
430427
}
431428
}
@@ -437,7 +434,7 @@ export class GeoblockedError extends WebrpcError {
437434
this.code = typeof error.code === 'number' ? error.code : 1003
438435
this.message = error.message || `Geoblocked region`
439436
this.status = typeof error.status === 'number' ? error.status : 451
440-
this.cause = error.cause
437+
if (error.cause !== undefined) this.cause = error.cause
441438
Object.setPrototypeOf(this, GeoblockedError.prototype)
442439
}
443440
}
@@ -449,7 +446,7 @@ export class RateLimitedError extends WebrpcError {
449446
this.code = typeof error.code === 'number' ? error.code : 1004
450447
this.message = error.message || `Rate-limited. Please slow down.`
451448
this.status = typeof error.status === 'number' ? error.status : 429
452-
this.cause = error.cause
449+
if (error.cause !== undefined) this.cause = error.cause
453450
Object.setPrototypeOf(this, RateLimitedError.prototype)
454451
}
455452
}
@@ -461,7 +458,7 @@ export class CorsDisallowedError extends WebrpcError {
461458
this.code = typeof error.code === 'number' ? error.code : 1005
462459
this.message = error.message || `CORS disallowed. JWT can't be used from a web app.`
463460
this.status = typeof error.status === 'number' ? error.status : 403
464-
this.cause = error.cause
461+
if (error.cause !== undefined) this.cause = error.cause
465462
Object.setPrototypeOf(this, CorsDisallowedError.prototype)
466463
}
467464
}

_examples/node-ts/server-fastify/tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
{
2+
"extends": "@tsconfig/strictest/tsconfig.json",
23
"compilerOptions": {
34
"target": "ES2022",
45
"module": "NodeNext",
56
"moduleResolution": "NodeNext",
6-
"strict": true,
7-
"esModuleInterop": true,
8-
"skipLibCheck": true,
97
"resolveJsonModule": true,
108
"types": ["node"]
119
},

_examples/node-ts/server-hono/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "node-ts-server",
33
"version": "1.0.0",
44
"description": "",
5+
"type": "module",
56
"main": "index.js",
67
"scripts": {
78
"start": "tsx index.ts"
@@ -12,11 +13,10 @@
1213
"packageManager": "[email protected]",
1314
"devDependencies": {
1415
"@hono/node-server": "^1.13.3",
16+
"@tsconfig/strictest": "^2.0.6",
1517
"@types/node": "^24.8.1",
1618
"hono": "^4.4.7",
1719
"tsx": "^4.20.6",
1820
"typescript": "^5.9.3"
19-
},
20-
"dependencies": {
2121
}
2222
}

_examples/node-ts/server-hono/pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)