Skip to content

Commit ce14cd7

Browse files
committed
Adjust FastifyInstance types
1 parent 195e4fd commit ce14cd7

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

lib/errors/errorHandler.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,16 @@ export type ErrorHandlerParams = {
128128
resolveLogObject?: (error: unknown) => FreeformRecord | undefined
129129
}
130130

131-
export function createErrorHandler(params: ErrorHandlerParams) {
131+
export function createErrorHandler(params: ErrorHandlerParams): (
132+
// biome-ignore lint/suspicious/noExplicitAny: We should support any fastify instance generics
133+
this: FastifyInstance<any, any, any, any, any>,
134+
error: FreeformRecord,
135+
request: FastifyRequest,
136+
reply: FastifyReply,
137+
) => void {
132138
return function errorHandler(
133-
this: FastifyInstance,
139+
// biome-ignore lint/suspicious/noExplicitAny: We should support any fastify instance generics
140+
this: FastifyInstance<any, any, any, any, any>,
134141
error: FreeformRecord,
135142
request: FastifyRequest,
136143
reply: FastifyReply,

lib/plugins/healthcheck/healthcheckCommons.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
import type { Either } from '@lokalise/node-core'
1+
import type { CommonLogger, Either } from '@lokalise/node-core'
22
import type { FastifyInstance } from 'fastify'
3+
import type { FastifyTypeProviderDefault } from 'fastify/types/type-provider'
4+
import type {
5+
RawReplyDefaultExpression,
6+
RawRequestDefaultExpression,
7+
RawServerDefault,
8+
} from 'fastify/types/utils'
39

4-
export type HealthChecker = (app: FastifyInstance) => Promise<Either<Error, true>>
10+
export type HealthChecker = (
11+
app: FastifyInstance<
12+
RawServerDefault,
13+
RawRequestDefaultExpression,
14+
RawReplyDefaultExpression,
15+
CommonLogger,
16+
FastifyTypeProviderDefault
17+
>,
18+
) => Promise<Either<Error, true>>
519

620
/**
721
* Return a function which executes healthcheck and throws an error if it fails
822
*/
9-
export const wrapHealthCheck = (app: FastifyInstance, healthCheck: HealthChecker) => {
23+
export const wrapHealthCheck = (
24+
app: FastifyInstance<
25+
RawServerDefault,
26+
RawRequestDefaultExpression,
27+
RawReplyDefaultExpression,
28+
CommonLogger,
29+
FastifyTypeProviderDefault
30+
>,
31+
healthCheck: HealthChecker,
32+
) => {
1033
return async () => {
1134
const response = await healthCheck(app)
1235
if (response.error) {

lib/plugins/healthcheck/healthcheckMetricsPlugin.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { FastifyInstance } from 'fastify'
22
import fp from 'fastify-plugin'
33

4+
import type { CommonLogger } from '@lokalise/node-core'
5+
import type { FastifyTypeProviderDefault } from 'fastify/types/type-provider'
6+
import type {
7+
RawReplyDefaultExpression,
8+
RawRequestDefaultExpression,
9+
RawServerDefault,
10+
} from 'fastify/types/utils'
411
import type { HealthChecker } from './healthcheckCommons'
512

613
const VALID_PROMETHEUS_NAME_REGEX = /[a-zA-Z_:][a-zA-Z0-9_:]*/
@@ -16,7 +23,15 @@ export type HealthcheckResult = {
1623

1724
export type PrometheusHealthCheck = {
1825
name: string
19-
checker: (app: FastifyInstance) => Promise<HealthcheckResult>
26+
checker: (
27+
app: FastifyInstance<
28+
RawServerDefault,
29+
RawRequestDefaultExpression,
30+
RawReplyDefaultExpression,
31+
CommonLogger,
32+
FastifyTypeProviderDefault
33+
>,
34+
) => Promise<HealthcheckResult>
2035
}
2136

2237
/**
@@ -28,7 +43,15 @@ export const wrapHealthCheckForPrometheus = (
2843
): PrometheusHealthCheck => {
2944
return {
3045
name: healthcheckName,
31-
checker: async (app: FastifyInstance): Promise<HealthcheckResult> => {
46+
checker: async (
47+
app: FastifyInstance<
48+
RawServerDefault,
49+
RawRequestDefaultExpression,
50+
RawReplyDefaultExpression,
51+
CommonLogger,
52+
FastifyTypeProviderDefault
53+
>,
54+
): Promise<HealthcheckResult> => {
3255
const startTime = Date.now()
3356
const response = await healthCheck(app)
3457
const checkTimeInMsecs = Date.now() - startTime
@@ -42,7 +65,13 @@ export const wrapHealthCheckForPrometheus = (
4265
}
4366

4467
function plugin(
45-
app: FastifyInstance,
68+
app: FastifyInstance<
69+
RawServerDefault,
70+
RawRequestDefaultExpression,
71+
RawReplyDefaultExpression,
72+
CommonLogger,
73+
FastifyTypeProviderDefault
74+
>,
4675
opts: HealthcheckMetricsPluginOptions,
4776
done: (err?: Error) => void,
4877
) {

lib/plugins/healthcheck/publicHealthcheckPlugin.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { FastifyInstance } from 'fastify'
22
import fp from 'fastify-plugin'
33

4+
import type { CommonLogger } from '@lokalise/node-core'
5+
import type { FastifyTypeProviderDefault } from 'fastify/types/type-provider'
6+
import type {
7+
RawReplyDefaultExpression,
8+
RawRequestDefaultExpression,
9+
RawServerDefault,
10+
} from 'fastify/types/utils'
411
import type { HealthChecker } from './healthcheckCommons'
512

613
export interface PublicHealthcheckPluginOptions {
@@ -22,7 +29,17 @@ export type HealthCheck = {
2229
checker: HealthChecker
2330
}
2431

25-
function plugin(app: FastifyInstance, opts: PublicHealthcheckPluginOptions, done: () => void) {
32+
function plugin(
33+
app: FastifyInstance<
34+
RawServerDefault,
35+
RawRequestDefaultExpression,
36+
RawReplyDefaultExpression,
37+
CommonLogger,
38+
FastifyTypeProviderDefault
39+
>,
40+
opts: PublicHealthcheckPluginOptions,
41+
done: () => void,
42+
) {
2643
const responsePayload = opts.responsePayload ?? {}
2744
app.route({
2845
url: opts.url ?? '/health',

0 commit comments

Comments
 (0)