import Knex = require('knex') import * as expressCore from 'express-serve-static-core' import DPError = require('./types/misc/dp_error') import DPJobError = require('./types/misc/dp_error') import { ModuleLoadizable, LoadedProxy, NonFunctionType } from './types/loader' import { Controller } from './types/controller'; declare global { namespace Express { interface Application { dp?: DpNode.DpInstance } } namespace NodeJS { interface Global { isTest: boolean DP_TEST_PORT?: number } } var isTest: boolean var DP_TEST_PORT: number } type Lazy = T | (() => T) type Defer = T | (T extends Promise ? never : Promise) type Some = T | T[] declare namespace DpNode { export type Body = string export interface RenderOptions { doNotMinify?: boolean } export class DPError { constructor(msg: Error | string, req: expressCore.Request) name: string code?: string stack: string message: string originalError?: Error req: expressCore.Request } export class DPJobError { constructor(err: Error | string) name: string code?: string stack: string message: string originalError?: Error } export type DPWebError = DPError | Error export interface ControllerDelegateBase, H extends ModuleLoadizable> extends Controller { model: LoadedProxy helper: LoadedProxy } type KnexBindings = Knex.RawBuilder extends ((sql: string, bindings: infer R) => any) ? R : never type TranFunc> = (...args: T) => [string, KnexBindings] export interface ModelDelegate { knex: Knex row>(query: string, params: KnexBindings, dsn: string): Promise rows(query: string, params: KnexBindings, dsn: string): Promise tran(blocks: [TranFunc<[]>], dsn: string): Promise<[T]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>], dsn: string): Promise<[T,U]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>], dsn: string): Promise<[T,U,V]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>, TranFunc<[T,U,V]>], dsn: string): Promise<[T,U,V,W]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>, TranFunc<[T,U,V]>, TranFunc<[T,U,V,W]>], dsn: string): Promise<[T,U,V,W,X]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>, TranFunc<[T,U,V]>, TranFunc<[T,U,V,W]>, TranFunc<[T,U,V,W,X,Y]>], dsn: string): Promise<[T,U,V,W,X,Y]> tran(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>, TranFunc<[T,U,V]>, TranFunc<[T,U,V,W]>, TranFunc<[T,U,V,W,X,Y]>, TranFunc<[T,U,V,W,X,Y,Z]>], dsn: string): Promise<[T,U,V,W,X,Y,Z]> execute>(query: string, params: KnexBindings): Promise paginate(knex: Knex.QueryInterface, page: number, rpp: number, resultMap?: (value: T, index: number, array: T[]) => U): U[] grouping(prefixes: string | string[]): (row: T) => T grouping(prefixes: string | string[], row: T): T } export interface ModelDelegateBase, H extends ModuleLoadizable> extends ModelDelegate { model: LoadedProxy helper: LoadedProxy } export interface HelperDelegateBase> { helper: LoadedProxy } export type MFunc = expressCore.RequestHandler | expressCore.ErrorRequestHandler export type HRet = void | boolean | Body type Defer = T | (T extends Promise ? never : Promise) export interface Router { route( method: string, path: string, delegate: string, middlewares: MFunc[], middlewaresEnd: MFunc[], controllerPrefix: HandlerMethod, controllerSuffix: HandlerMethod, controllerErrorHandler: HandlerMethod ): void } export interface Options { mode: 'web' | 'job' debug: boolean apppath: string controller: string view: string minifyRemoveLineBreakWhitespace: boolean requestSizeLimit: string /* XXX */ errorLogging: boolean error?(controller: Controller, error: DPWebError, statusCode: number): Promise viewHelpers: { [name: string]: (...args: any[]) => string /* handlebars */ } redirectNakedToWWW?: boolean compression?: boolean enhanceSecurity?: boolean cookieEnabled?: boolean preMiddlewares?: expressCore.RequestHandlerParams | expressCore.RequestHandlerParams[] session?: { secret: string volatility: boolean ttl: boolean } cookie?: { secret: string volatility: boolean ttl: boolean } databaseDsn?: string[] static?: string | string[] port?: number } export interface ModelHelper { model: M helper: H } export interface DpInstance { app: expressCore.Application } export interface HandlerPlainMethod { (this: C, data: D): Defer } export interface HandlerArrowMethod { (this: unknown, controller: C, data: D): Defer prototype?: void | boolean | number | string | symbol | bigint } export type HandlerMethod = HandlerPlainMethod | HandlerArrowMethod export interface ControllerLoadizable { [key: string]: HandlerMethod | NonFunctionType } type Config = any } declare function DpNode(options: T): ( 'job' extends T['mode'] ? ReturnType : never ) | ('web' extends T['mode'] ? DpNode.DpInstance : never); export = DpNode;