import { GeneralCustomRequestHandler, EncodedHttpRequest, EncodedHttpResponse, PathModifier, GeneralRestApiHandler, GeneralServerParams, GeneralRestApiClient } from "@phenyl/interfaces"; /** * */ export default class ServerLogic { /** * Instance containing API logic invoked via run(). * PhenylRestApi instance is expected. */ restApiHandler: GeneralRestApiHandler; /** * (path: string) => string * Real server path to regular path. * The argument is path string, start with "/api/". * e.g. (path) => path.slice(5) */ modifyPath: PathModifier; /** * Custom Request Handler. * Receive non-API HTTP request and return HTTP response. * (non-API Request: request whose path doesn't start with "/api/") * Response can be any type, like HTML/CSS/JavaScript/Image. * * Intended Use: Web page. Don't use this function as API. * Example: Rich API explorer like swagger. * * The second argument "restApiClient" is a client to access directly to PhenylRestApi (bypass HTTP). */ customRequestHandler: GeneralCustomRequestHandler; /** * A client for the rest api of the given restApiHandler. */ restApiClient: GeneralRestApiClient; constructor(params: GeneralServerParams); /** * Handle request to get response. * If modified path starts with "/api/", it will invoke GeneralRestApiHandler#handleRequestData(). * Otherwise, it will invoke registered customRequestHandler. */ handleRequest(encodedHttpRequest: EncodedHttpRequest): Promise; /** * @private * Invoke GeneralRestApiHandler#handleRequestData(). */ handleApiRequest(encodedHttpRequest: EncodedHttpRequest): Promise; /** * @private * Run custom request registered. * Note that encodedHttpRequest.path is originalPath ( not equal to the modified path by this.modifyPath()). */ handleCustomRequest(encodedHttpRequest: EncodedHttpRequest): Promise; }