All files types.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                  1x       3x                                                            
import { HttpMethod } from "@xtaskjs/common";
 
export type HttpAdapterType = "express";
 
export interface HttpServerOptions {
  port?: number;
  host?: string;
}
 
export interface HttpRequestLike {
  method?: string;
  url?: string;
  path?: string;
  body?: any;
}
 
export interface HttpViewResult {
  readonly __xtaskView: true;
  template: string;
  model?: Record<string, any>;
  statusCode?: number;
}
 
export interface HttpResponseLike {
  statusCode?: number;
  headersSent?: boolean;
  setHeader?: (name: string, value: string) => void;
  end?: (chunk?: any) => void;
  json?: (payload: any) => void;
  send?: (payload: any) => void;
  status?: (code: number) => HttpResponseLike;
  render?: (view: string, locals?: Record<string, any>, callback?: (error: Error | null, html?: string) => void) => void;
}
 
export type HttpRequestHandler = (
  method: HttpMethod,
  path: string,
  req: HttpRequestLike,
  res: HttpResponseLike
) => Promise<void>;
 
export interface HttpAdapter {
  readonly type: HttpAdapterType;
  registerRequestHandler(handler: HttpRequestHandler): void;
  renderView?(req: HttpRequestLike, res: HttpResponseLike, payload: HttpViewResult): Promise<void>;
  listen(options: Required<HttpServerOptions>): Promise<void>;
  close(): Promise<void>;
}
 
export const view = (
  template: string,
  model?: Record<string, any>,
  statusCode?: number
): HttpViewResult => ({
  __xtaskView: true,
  template,
  model,
  statusCode,
});
 
export interface ExpressTemplateEngineOptions {
  viewsPath?: string;
  fileExtension?: string;
  viewEngine?: string;
  extension?: string;
  engine?: (...args: any[]) => any;
  render?: (
    template: string,
    model: Record<string, any>,
    context: { req: HttpRequestLike; res: HttpResponseLike }
  ) => string | Promise<string>;
}
 
export interface ExpressStaticFilesOptions {
  enabled?: boolean;
  publicPath?: string;
  urlPrefix?: string;
}
 
export interface ExpressAdapterOptions {
  templateEngine?: ExpressTemplateEngineOptions;
  staticFiles?: ExpressStaticFilesOptions;
}