-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
153 lines (147 loc) · 5.63 KB
/
index.d.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 | (() => T)
type Defer<T> = T | (T extends Promise<any> ? never : Promise<T>)
type Some<T> = 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<M extends ModuleLoadizable<any>, H extends ModuleLoadizable<any>> extends Controller {
model: LoadedProxy<M>
helper: LoadedProxy<H>
}
type KnexBindings = Knex.RawBuilder extends ((sql: string, bindings: infer R) => any) ? R : never
type TranFunc<T extends Array<any>> = (...args: T) => [string, KnexBindings]
export interface ModelDelegate {
knex: Knex
row<T extends Array<any>>(query: string, params: KnexBindings, dsn: string): Promise<T | {}>
rows<T>(query: string, params: KnexBindings, dsn: string): Promise<T | null>
tran<T>(blocks: [TranFunc<[]>], dsn: string): Promise<[T]>
tran<T,U>(blocks: [TranFunc<[]>, TranFunc<[T]>], dsn: string): Promise<[T,U]>
tran<T,U,V>(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>], dsn: string): Promise<[T,U,V]>
tran<T,U,V,W>(blocks: [TranFunc<[]>, TranFunc<[T]>, TranFunc<[T,U]>, TranFunc<[T,U,V]>], dsn: string): Promise<[T,U,V,W]>
tran<T,U,V,W,X>(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<T,U,V,W,X,Y>(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<T,U,V,W,X,Y,Z>(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<T extends Array<any>>(query: string, params: KnexBindings): Promise<T | {}>
paginate<T, U>(knex: Knex.QueryInterface, page: number, rpp: number, resultMap?: (value: T, index: number, array: T[]) => U): U[]
grouping(prefixes: string | string[]): <T extends object>(row: T) => T
grouping<T extends object>(prefixes: string | string[], row: T): T
}
export interface ModelDelegateBase<M extends ModuleLoadizable<any>, H extends ModuleLoadizable<any>> extends ModelDelegate {
model: LoadedProxy<M>
helper: LoadedProxy<H>
}
export interface HelperDelegateBase<H extends ModuleLoadizable<any>> {
helper: LoadedProxy<H>
}
export type MFunc = expressCore.RequestHandler | expressCore.ErrorRequestHandler
export type HRet = void | boolean | Body
type Defer<T> = T | (T extends Promise<any> ? never : Promise<T>)
export interface Router<C, P = HRet> {
route(
method: string,
path: string,
delegate: string,
middlewares: MFunc[],
middlewaresEnd: MFunc[],
controllerPrefix: HandlerMethod<C, undefined, P>,
controllerSuffix: HandlerMethod<C, HRet>,
controllerErrorHandler: HandlerMethod<C, DPWebError>
): 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<false | undefined>
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<M, H> {
model: M
helper: H
}
export interface DpInstance {
app: expressCore.Application
}
export interface HandlerPlainMethod<C, D = undefined, R = HRet> {
(this: C, data: D): Defer<R>
}
export interface HandlerArrowMethod<C, D = undefined, R = HRet> {
(this: unknown, controller: C, data: D): Defer<R>
prototype?: void | boolean | number | string | symbol | bigint
}
export type HandlerMethod<C, D = undefined, R = HRet> = HandlerPlainMethod<C, D, R> | HandlerArrowMethod<C, D, R>
export interface ControllerLoadizable<C, P> {
[key: string]: HandlerMethod<C, P> | NonFunctionType
}
type Config = any
}
declare function DpNode<T extends DpNode.Options>(options: T): (
'job' extends T['mode'] ? ReturnType<import('./lib/job')> : never
) | ('web' extends T['mode'] ? DpNode.DpInstance : never);
export = DpNode;