Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide types #28

Open
benkeil opened this issue Jul 28, 2020 · 1 comment
Open

Provide types #28

benkeil opened this issue Jul 28, 2020 · 1 comment

Comments

@benkeil
Copy link

benkeil commented Jul 28, 2020

Please make it usable with typescript.

@rickdgeerling rickdgeerling mentioned this issue Sep 14, 2020
@Jackman3005
Copy link

Jackman3005 commented Nov 4, 2021

I have created this type definition for use in our projects as we are fully typescript. I can't say everything is tested fully as we didn't use all features of the library, but I put a bit of effort in to ensure I used the right types and added in the information provided in the docs. Let me know if you find any problems. I'm not sure what the process would be for including this into the repo, or including it in the @types repo. But happy to help if someone else is interested.

declare module 'mock-http-server' {
  import { IncomingMessage } from 'connect';
  import { Socket } from 'node:net';
  import { SecureContextOptions } from 'node:tls';

  export type Request = IncomingMessage & { body: any };
  export type Connection = Socket;

  export interface ServerConfig {
    host: string;
    port: number;

    /**
     *  key: String – HTTPS key (if missing the server is HTTP)
     */
    key?: SecureContextOptions['key'];

    /**
     *  cert: String – HTTPS certificate (if missing the server is HTTP)
     */
    cert?: SecureContextOptions['cert'];
  }

  export interface RequestProps {
    /**
     * HTTP Method to match, default is GET. Can be * to match any method.
     */
    method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | '*';

    /**
     * HTTP request path to match. Can be * to match any path (to be used in conjunction with filter to allow custom matching)
     */
    path: '*' | string;

    reply: ReplyProps;

    /**
     * The value is a filter function fn(request): if it returns true the handler gets executed.
     */
    filter?: (request: RequestFilter) => boolean;

    /**
     * Delays the response by X milliseconds.
     */
    delay?: number;
  }

  /**
   * ReplyProps
   */
  export interface ReplyProps {
    /**
     * Default is 200
     */
    status?: number | ((req: Request) => number);

    /**
     * HTTP response headers. content-length is managed by the server implementation.
     * Default is { "content-type": "application/json" }
     */
    headers?: Record<string, string>;

    /**
     * HTTP response headers to override to default headers (ie. content-length). If a value is set to undefined, the header gets removed from the response.
     */
    headerOverrides?: Record<string, string>;

    /**
     * HTTP response body. Can be a string, a synchronous function fn(request) that returns the body, or an asynchronous function fn(request, reply) that send the response body invoking reply(body).
     */
    body?:
      | string
      | ((req: Request) => string)
      | ((req: Request, reply: (body: string) => void) => void);

    /**
     * End the response once the body has been sent (default behaviour). If false, it will keep the response connection open indefinitely (useful to test special cases on the client side - ie. read timeout after partial body response sent).
     * Default is true
     */
    end?: boolean;
  }

  export interface RequestFilter {
    method: string;
    path: string;
  }

  class ServerMock {
    constructor(config: ServerConfig);

    start: (callback: (...none: unknown[]) => void) => void;
    stop: (callback: (...none: unknown[]) => void) => void;
    on: (request: RequestProps) => ServerMock;

    /**
     * Returns an array containing all requests received. If `filter` is defined,
     * filters the requests by:
     * - method
     * - path
     *
     * @param  {Object} filter
     * @return {Array}
     */
    requests: (filter?: RequestFilter) => Request[];

    /**
     * Returns an array containing all active connections.
     *
     * @return {Array}
     */
    connections: () => Connection[];

    /**
     * Returns the port number if set or null otherwise
     *
     * @return {Number|null}
     */
    getHttpPort: () => number | null;

    /**
     * Returns the port number if set or null otherwise
     *
     * @return {Number|null}
     */
    getHttpsPort: () => number | null;

    /**
     * Clears request handlers and requests received by the HTTP server.
     */
    reset: () => void;

    /**
     * Clears all request handlers that were previously set using `on()` method.
     */
    resetHandlers: () => void;

    /**
     * Clears all requests received by the HTTP server.
     */
    resetRequests: () => void;
  }

  export default ServerMock;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants