From 93878022626aa8c5ce1c9de3c8fbe228de9b5198 Mon Sep 17 00:00:00 2001 From: Jacob Pargin Date: Tue, 12 Mar 2024 23:39:18 +0000 Subject: [PATCH] Support non-promise function options These functions don't really _need_ to be async but currently the types require that they are. For example, currently this is an invalid option: ```typescript onSignal: () => logger.info("signal received") ``` and instead you'd have to write: ```typescript onSignal: async () => logger.info("signal received") ``` which might trigger a linter error such as typescript-eslint's `Async method 'onSignal' has no 'await' expression.` or ```typescript onSignal: () => { logger.info("signal received") return Promise.resolve() } ``` which creates a promise for no reason other than to satisfy the types! With this PR the types make it clear that you can actually just return nothing or, in the case of the health checks, return a value without using async/await or creating a promise. --- typings/index.d.ts | 10 +++++----- typings/index.test.ts | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 72521e8..66424a5 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3,7 +3,7 @@ declare module "@godaddy/terminus" { isShuttingDown: boolean; } - export type HealthCheck = ({ state }: { state: TerminusState }) => Promise; + export type HealthCheck = ({ state }: { state: TerminusState }) => Promise | any; export class HealthCheckError extends Error { constructor(message: string, causes: any); @@ -28,10 +28,10 @@ declare module "@godaddy/terminus" { statusError?: number, statusErrorResponse?: Record, useExit0?: boolean, - onSignal?: () => Promise; - onSendFailureDuringShutdown?: () => Promise; - onShutdown?: () => Promise; - beforeShutdown?: () => Promise; + onSignal?: () => Promise | void; + onSendFailureDuringShutdown?: () => Promise | void; + onShutdown?: () => Promise | void; + beforeShutdown?: () => Promise | void; logger?: (msg: string, err: Error) => void; headers?:{ [key: string]: string }; diff --git a/typings/index.test.ts b/typings/index.test.ts index a151433..230e16e 100644 --- a/typings/index.test.ts +++ b/typings/index.test.ts @@ -8,9 +8,13 @@ async function onSignal() { ]); } -async function onShutdown() { +async function onShutdownAsync() { + console.log('cleanup finished, server is shutting down'); + return true; +} + +const onShutdown: TerminusOptions["onShutdown"] = () => { console.log('cleanup finished, server is shutting down'); - return Promise.resolve(); } const server = http.createServer((request, response) => { @@ -23,14 +27,25 @@ const healthcheck: HealthCheck = () => { throw error; } +const healthcheckAsync: HealthCheck = async () => { + const result = await Promise.resolve([{ status: 'up' }]); + return result; +} + +const healthcheckSync: HealthCheck = () => { + return [{ status: 'up' }]; +} + const options: TerminusOptions = { healthChecks: { "/healthcheck": healthcheck, + "/healthcheck-sync": healthcheckSync, + "/healthcheck-async": healthcheckAsync, verbatim: true }, timeout: 1000, onSignal, - onShutdown, + onShutdown: onShutdownAsync, logger: console.log };