faker-api
Version:
A fully customizible rest api faking package that allows you to mock , clone and fake Rest API with fake yet realistic data
118 lines • 4.89 kB
TypeScript
import { Express, Request, Response } from "express";
import { AbstractModel } from "../models";
import { ParamsType } from "./path";
import { Transformer } from "../transformers";
import { Router as FARouter } from "../router";
import { ViewSet } from "../router/viewset";
declare type FunctionRequestHandler = (request: Request, response: Response, params?: ParamsType) => void;
declare type RequestHandlerType = FunctionRequestHandler | AbstractModel | Transformer | {
[key: string]: any;
} | any[];
declare type MethodType = "GET" | "POST" | "DELETE" | "PUT" | "OPTIONS" | "PATCH" | "HEAD" | string;
/**
* This is the class that is been used to instantiate the api faker server instance
*
*/
declare class FakerServer {
/**
* @type string specific the route the server would be running on
*/
private serverPath;
private expressApp;
private expressRouter;
private appPassed;
private methodRequestHanders;
private routerMaps;
private __internalRouter;
private __internlRouterRoot;
constructor(serverPath?: string | undefined, expressInstance?: Express);
/**
* Returns an instance of a FakerServer with the given express application instance
*
* @param expressInstance An express application instance
* @param path [string="/api"] The path the Faker Server will be running on, on the express application
* @returns An instance of FakerServer
*
*/
static from(expressInstance: Express, path?: string): FakerServer;
/**
* Start the faker server.
* @remark This method should be called if the faker server instance was created / instantiated without passing an express application instance.
* @param port - The port the faker server internally created express application instance will run on
*/
run(port?: number): void;
/**
* Adds a new request method handle to the faker server
* @param path - The url the handler should handle
* @param handler - The handler it self
* @param method - The request method type the handle should accept
*/
private __addMethodRequestHandler;
/**
* Adds a new GET request handler to the faker server instance
* @param path - The url the handler should handle
* @param handler - The handler it self
*
*/
get(path: string, handler: RequestHandlerType): this;
/**
* Adds a new POST request handler to the faker server instance
* @param path - The url the handler should handle
* @param handler - The handler it self
*
*/
post(path: string, handler: RequestHandlerType): this;
/**
* Adds a new DELETE request handler to the faker server instance
* @param path - The url the handler should handle
* @param handler - The handler it self
*
*/
delete(path: string, handler: RequestHandlerType): this;
/**
* Adds a new PUT request handler to the faker server instance
* @param path - The url the handler should handle
* @param handler - The handler it self
*
*/
put(path: string, handler: RequestHandlerType): this;
/**
* Adds a new PATCH request handler to the faker server instance
* @param path - The url the handler should handle
* @param handler - The handler it self
*
*/
patch(path: string, handler: RequestHandlerType): this;
/**
* Register a new Router to the faker server instance
* @param path - The path the router should handle request to (avoid using paths that ends with a /)
* @param router - The router instance
*
* @remark This method can throw error if the `path` ends with /
*/
route(path: string, routerOrViewSet: FARouter | (new () => ViewSet)): void;
/**
* Handles request that matches a specific static path that has a method handler registered to it
*
* @param path - The url the handler should handle
* @param handler - The handler it self
* @param method - The request method type the handle should accept
*/
private _handleMethodHandler;
/**
* Called internally to check if there exist a router that can handle a request, after confirming that there is no method handler found to handle the request
* @param request - The request object
* @param response - The response object
* path - The request path without the server prefix path
* @returns - true if the request handled by a router or false if there was no router to handle the request
*/
private _handleRouterRequest;
/**
* This method is called internally everytime a new request is directed to the faker server
* @param request - The new request object
* @param response - The request response object
*/
private onNewRequest;
}
export { FakerServer, ParamsType, MethodType };
//# sourceMappingURL=index.d.ts.map