Skip to content

Commit 4e6a6e0

Browse files
committed
Reuse type
1 parent ce14cd7 commit 4e6a6e0

5 files changed

+30
-81
lines changed

lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ export type { ErrorHandlerParams, FreeformRecord } from './errors/errorHandler'
8787
export { generateJwtToken, decodeJwtToken } from './jwt-utils/tokenUtils'
8888

8989
export { createStaticTokenAuthPreHandler } from './route-utils/authPreHandlers'
90+
export type { AnyFastifyInstance, CommonFastifyInstance } from './plugins/pluginsCommon'

lib/plugins/healthcheck/healthcheckCommons.ts

+4-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
1-
import type { CommonLogger, Either } from '@lokalise/node-core'
2-
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'
1+
import type { Either } from '@lokalise/node-core'
2+
import type { AnyFastifyInstance } from '../pluginsCommon'
93

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

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

lib/plugins/healthcheck/healthcheckMetricsPlugin.ts

+4-34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import type { FastifyInstance } from 'fastify'
21
import fp from 'fastify-plugin'
3-
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'
2+
import type { AnyFastifyInstance, CommonFastifyInstance } from '../pluginsCommon'
113
import type { HealthChecker } from './healthcheckCommons'
124

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

2416
export type PrometheusHealthCheck = {
2517
name: string
26-
checker: (
27-
app: FastifyInstance<
28-
RawServerDefault,
29-
RawRequestDefaultExpression,
30-
RawReplyDefaultExpression,
31-
CommonLogger,
32-
FastifyTypeProviderDefault
33-
>,
34-
) => Promise<HealthcheckResult>
18+
checker: (app: CommonFastifyInstance) => Promise<HealthcheckResult>
3519
}
3620

3721
/**
@@ -43,15 +27,7 @@ export const wrapHealthCheckForPrometheus = (
4327
): PrometheusHealthCheck => {
4428
return {
4529
name: healthcheckName,
46-
checker: async (
47-
app: FastifyInstance<
48-
RawServerDefault,
49-
RawRequestDefaultExpression,
50-
RawReplyDefaultExpression,
51-
CommonLogger,
52-
FastifyTypeProviderDefault
53-
>,
54-
): Promise<HealthcheckResult> => {
30+
checker: async (app: CommonFastifyInstance): Promise<HealthcheckResult> => {
5531
const startTime = Date.now()
5632
const response = await healthCheck(app)
5733
const checkTimeInMsecs = Date.now() - startTime
@@ -65,13 +41,7 @@ export const wrapHealthCheckForPrometheus = (
6541
}
6642

6743
function plugin(
68-
app: FastifyInstance<
69-
RawServerDefault,
70-
RawRequestDefaultExpression,
71-
RawReplyDefaultExpression,
72-
CommonLogger,
73-
FastifyTypeProviderDefault
74-
>,
44+
app: AnyFastifyInstance,
7545
opts: HealthcheckMetricsPluginOptions,
7646
done: (err?: Error) => void,
7747
) {

lib/plugins/healthcheck/publicHealthcheckPlugin.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import type { FastifyInstance } from 'fastify'
21
import fp from 'fastify-plugin'
3-
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'
2+
import type { AnyFastifyInstance } from '../pluginsCommon'
113
import type { HealthChecker } from './healthcheckCommons'
124

135
export interface PublicHealthcheckPluginOptions {
@@ -29,17 +21,7 @@ export type HealthCheck = {
2921
checker: HealthChecker
3022
}
3123

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

lib/plugins/pluginsCommon.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { CommonLogger } from '@lokalise/node-core'
2+
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'
9+
10+
export type CommonFastifyInstance = FastifyInstance<
11+
RawServerDefault,
12+
RawRequestDefaultExpression,
13+
RawReplyDefaultExpression,
14+
CommonLogger,
15+
FastifyTypeProviderDefault
16+
>
17+
18+
// biome-ignore lint/suspicious/noExplicitAny: This is intentional
19+
export type AnyFastifyInstance = FastifyInstance<any, any, any, any, any>

0 commit comments

Comments
 (0)