-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add Typescript generics * adf --------- Co-authored-by: Dennis Chen <denchen@yahooinc.com>
- Loading branch information
Showing
2 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,113 @@ | ||
import { fastify, FastifyInstance, FastifyServerOptions } from 'fastify' | ||
import { fastify, FastifyInstance } from "fastify"; | ||
|
||
export type RestartHook = (instance: FastifyInstance, restartOpts?: unknown) => Promise<unknown> | ||
import type { | ||
FastifyBaseLogger, | ||
FastifyHttp2Options, | ||
FastifyHttp2SecureOptions, | ||
FastifyHttpOptions, | ||
FastifyHttpsOptions, | ||
FastifyServerOptions, | ||
FastifyTypeProvider, | ||
FastifyTypeProviderDefault, | ||
RawReplyDefaultExpression, | ||
RawRequestDefaultExpression, | ||
RawServerBase, | ||
RawServerDefault, | ||
} from "fastify"; | ||
import * as http from "http"; | ||
import * as http2 from "http2"; | ||
import * as https from "https"; | ||
|
||
declare module 'fastify' { | ||
export type RestartHook = ( | ||
instance: FastifyInstance, | ||
restartOpts?: unknown | ||
) => Promise<unknown>; | ||
|
||
declare module "fastify" { | ||
interface FastifyInstance { | ||
restart: (restartOpts?: unknown) => Promise<void>, | ||
addPreRestartHook: (fn: RestartHook) => void, | ||
addOnRestartHook: (fn: RestartHook) => void, | ||
restarted: boolean | ||
closingRestartable: boolean | ||
restart: (restartOpts?: unknown) => Promise<void>; | ||
addPreRestartHook: (fn: RestartHook) => void; | ||
addOnRestartHook: (fn: RestartHook) => void; | ||
restarted: boolean; | ||
closingRestartable: boolean; | ||
} | ||
} | ||
|
||
type Fastify = typeof fastify; | ||
|
||
export type ApplicationFactory = (fastify: Fastify, opts: FastifyServerOptions, restartOpts?: unknown) => Promise<FastifyInstance> | ||
export type ApplicationFactory< | ||
Server extends | ||
| RawServerBase | ||
| http.Server | ||
| https.Server | ||
| http2.Http2Server | ||
| http2.Http2SecureServer = RawServerDefault, | ||
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>, | ||
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
> = ( | ||
fastify: Fastify, | ||
opts: Server extends infer S | ||
? S extends http2.Http2SecureServer | ||
? FastifyHttp2SecureOptions<S, Logger> | ||
: S extends http2.Http2Server | ||
? FastifyHttp2Options<S, Logger> | ||
: S extends https.Server | ||
? FastifyHttpsOptions<S, Logger> | ||
: S extends http.Server | ||
? FastifyHttpOptions<S, Logger> | ||
: FastifyServerOptions<Server> | ||
: FastifyServerOptions<Server>, | ||
restartOpts?: unknown | ||
) => Promise<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>; | ||
|
||
// These overloads follow the same overloads for the fastify factory | ||
|
||
export declare function restartable< | ||
Server extends http2.Http2SecureServer, | ||
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>, | ||
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
factory: ApplicationFactory<Server, Request, Reply, Logger, TypeProvider>, | ||
opts?: FastifyHttp2SecureOptions<Server, Logger>, | ||
fastify?: Fastify | ||
): Promise<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>; | ||
|
||
export declare function restartable< | ||
Server extends http2.Http2Server, | ||
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>, | ||
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
factory: ApplicationFactory<Server, Request, Reply, Logger, TypeProvider>, | ||
opts?: FastifyHttp2Options<Server, Logger>, | ||
fastify?: Fastify | ||
): Promise<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>; | ||
|
||
export declare function restartable< | ||
Server extends https.Server, | ||
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>, | ||
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
factory: ApplicationFactory<Server, Request, Reply, Logger, TypeProvider>, | ||
opts?: FastifyHttpsOptions<Server, Logger>, | ||
fastify?: Fastify | ||
): Promise<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>; | ||
|
||
export declare function restartable(factory: ApplicationFactory, opts?: FastifyServerOptions, fastify?: Fastify): Promise<FastifyInstance> | ||
export declare function restartable< | ||
Server extends http.Server, | ||
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>, | ||
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
factory: ApplicationFactory<Server, Request, Reply, Logger, TypeProvider>, | ||
opts?: FastifyHttpOptions<Server, Logger>, | ||
fastify?: Fastify | ||
): Promise<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters