Skip to content

Commit

Permalink
feat(common): narrow getInstance return type for http platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Apr 5, 2023
1 parent 353b974 commit 3b6b66b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/common/interfaces/http/http-server.interface.ts
Expand Up @@ -18,7 +18,11 @@ export type RequestHandler<TRequest = any, TResponse = any> = (
next?: Function,
) => any;

export interface HttpServer<TRequest = any, TResponse = any> {
export interface HttpServer<
TRequest = any,
TResponse = any,
ServerInstance = any,
> {
use(
handler:
| RequestHandler<TRequest, TResponse>
Expand Down Expand Up @@ -69,7 +73,7 @@ export interface HttpServer<TRequest = any, TResponse = any> {
getRequestHostname?(request: TRequest): string;
getRequestMethod?(request: TRequest): string;
getRequestUrl?(request: TRequest): string;
getInstance(): any;
getInstance(): ServerInstance;
registerParserMiddleware(...args: any[]): any;
enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>): any;
getHttpServer(): any;
Expand Down
@@ -1,7 +1,8 @@
import { INestApplication } from '@nestjs/common';
import type { Server as HttpServer } from 'http';
import type { Server as HttpsServer } from 'https';
import { INestApplication, HttpServer } from '@nestjs/common';
import type { Server as CoreHttpServer } from 'http';
import type { Server as CoreHttpsServer } from 'https';
import type { Server } from 'net';
import type { Express } from 'express';
import { NestExpressBodyParserOptions } from './nest-express-body-parser-options.interface';
import { NestExpressBodyParserType } from './nest-express-body-parser.interface';
import { ServeStaticOptions } from './serve-static-options.interface';
Expand All @@ -14,8 +15,15 @@ import { ServeStaticOptions } from './serve-static-options.interface';
* @publicApi
*/
export interface NestExpressApplication<
TServer extends HttpServer | HttpsServer = HttpServer,
TServer extends CoreHttpServer | CoreHttpsServer = CoreHttpServer,
> extends INestApplication<TServer> {
/**
* Returns the underlying HTTP adapter bounded to the Express.js app.
*
* @returns {HttpServer}
*/
getHttpAdapter(): HttpServer<Express.Request, Express.Response, Express>;

/**
* Starts the application.
*
Expand Down
@@ -1,11 +1,13 @@
import { INestApplication } from '@nestjs/common';
import { INestApplication, HttpServer } from '@nestjs/common';
import {
FastifyBodyParser,
FastifyInstance,
FastifyPluginAsync,
FastifyPluginCallback,
FastifyPluginOptions,
FastifyRegisterOptions,
FastifyRequest,
FastifyReply,
RawServerBase,
RawServerDefault,
} from 'fastify';
Expand All @@ -23,6 +25,13 @@ import { NestFastifyBodyParserOptions } from './nest-fastify-body-parser-options
export interface NestFastifyApplication<
TServer extends RawServerBase = RawServerDefault,
> extends INestApplication<TServer> {
/**
* Returns the underlying HTTP adapter bounded to a Fastify app.
*
* @returns {HttpServer}
*/
getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>;

/**
* A wrapper function around native `fastify.register()` method.
* Example `app.register(require('@fastify/formbody'))
Expand Down

0 comments on commit 3b6b66b

Please sign in to comment.