-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
216 lines (177 loc) · 6.74 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as KoaStaticServer from 'koa-static-server';
import * as Koa from 'koa';
import { Context, Configuration, Workflow } from '@axiosleo/cli-tool';
import { IncomingHttpHeaders } from 'http';
import { Rules, ErrorMessages, Validator } from 'validatorjs';
import { EventEmitter } from 'events';
import { File } from '@koa/multer';
import * as session from 'koa-session';
type StatusCode = string | '000;Unknown Error' |
'200;Success' | '404;Not Found' |
'500;Internal Server Error' |
'400;Bad Data' | '401;Unauthorized' |
'403;Not Authorized' | '400;Invalid Signature' |
'501;Failed' | '409;Data Already Exists';
type HttpMethod = 'ANY' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT' |
'any' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace' | 'connect' | string;
export function response(data: unknown, code?: StatusCode, httpStatus?: number, headers?: Record<string, string>): void;
export function result(data: unknown, httpStatus?: number, headers?: Record<string, string>): void;
export function success(data?: unknown, headers?: Record<string, string>): void;
export function failed(data?: unknown, code?: StatusCode, httpStatus?: number, headers?: Record<string, string>): void;
export function error(httpStatus: number, msg: string, headers?: Record<string, string>): void;
export function log(...data: any): void;
export interface HttpResponseConfig {
status?: number;
headers?: IncomingHttpHeaders;
data?: unknown;
format?: 'json' | 'text';
}
export declare class HttpResponse extends Error {
public readonly status: number;
public readonly headers: IncomingHttpHeaders;
public readonly data: unknown;
constructor(config?: HttpResponseConfig);
}
export declare class HttpError extends Error {
public readonly status: number;
public readonly headers: IncomingHttpHeaders;
public readonly message: string;
constructor(httpStatus: number, message: string, headers?: IncomingHttpHeaders);
}
interface ControllerInterface {
response(data: unknown, code?: StatusCode, status?: number, headers?: Record<string, string>): void;
result(data: unknown, status?: number, headers?: Record<string, string>): void;
success(data?: unknown, headers?: Record<string, string>): void;
failed(data?: unknown, code?: StatusCode, status?: number, headers?: Record<string, string>): void;
error(status: number, msg: string, headers?: Record<string, string>): void;
log(...data: any): void;
}
export declare class Controller implements ControllerInterface {
response(data: unknown, code?: StatusCode, status?: number, headers?: Record<string, string>): void;
result(data: unknown, status?: number, headers?: Record<string, string>): void;
success(data?: unknown, headers?: Record<string, string>): void;
failed(data?: unknown, code?: StatusCode, status?: number, headers?: Record<string, string>): void;
error(status: number, msg: string, headers?: Record<string, string>): void;
log(...data: any): void;
}
interface ValidatorConfig {
rules: Rules,
messages?: ErrorMessages
}
interface RouterValidator {
params?: ValidatorConfig,
query?: ValidatorConfig,
body?: ValidatorConfig
}
interface RouterInfo {
pathinfo: string;
validators: RouterValidator;
middlewares: ContextHandler<KoaContext>[];
handlers: ContextHandler<KoaContext>[];
afters: ContextHandler<KoaContext>[];
methods: string[];
params: {
[key: string]: string;
};
}
interface AppContext extends Context {
app: Application,
app_id: string,
config: AppConfiguration,
}
interface KoaContext extends AppContext {
koa: Koa.ParameterizedContext,
method: HttpMethod,
url: string,
// eslint-disable-next-line no-use-before-define
router?: RouterInfo | null,
access_key_id?: string,
app_key?: string,
params?: any,
body?: any,
file?: File | null,
files?: File[],
query?: any,
headers?: IncomingHttpHeaders,
response?: HttpResponse | HttpError,
}
type ContextHandler<T extends KoaContext> = (context: T) => Promise<void>
interface RouterOptions<T extends KoaContext> {
method?: HttpMethod,
handlers?: ContextHandler<T>[],
middlewares?: ContextHandler<T>[],
afters?: ContextHandler<T>[],
intro?: string,
routers?: Router[],
validators?: RouterValidator;
}
export class Router<T extends KoaContext = KoaContext> {
prefix: string;
method: HttpMethod;
routers: Router[];
handlers: ContextHandler<T>[];
middlewares: ContextHandler<T>[];
validators: RouterValidator;
afters?: ContextHandler<T>[];
constructor(prefix?: string, options?: RouterOptions<T>);
add<T extends KoaContext>(...router: Router<T>[]): this;
add<T extends KoaContext>(prefix: string, ...router: Router<T>[]): this;
new(prefix: string, options?: RouterOptions<T>): this;
push(method: HttpMethod, prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
get(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
post(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
put(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
patch(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
delete(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
any(prefix: string, handle: ContextHandler<T>, validator?: RouterValidator): this;
}
interface AppConfiguration {
[key: string]: any;
debug?: boolean,
count?: number,
port?: number,
app_id?: string,
paths?: Record<string, string>,
routers?: Router[],
operator?: Record<string, ContextHandler<KoaContext>>,
server?: {
env?: string | undefined,
keys?: string[] | undefined,
proxy?: boolean | undefined,
subdomainOffset?: number | undefined,
proxyIpHeader?: string | undefined,
maxIpsCount?: number | undefined,
},
session_key?: string,
session?: Partial<session.opts>,
static?: KoaStaticServer.Options
}
interface KoaApplicationConfig extends AppConfiguration {
listen_host: 'localhost',
}
type TriggerFunc = (...args: any[]) => void
export declare abstract class Application extends EventEmitter {
routes: any;
app_id: string;
config: Configuration;
workflow: Workflow<KoaContext>;
constructor(config: AppConfiguration);
abstract start(): Promise<void>;
}
export declare class KoaApplication extends Application {
koa: Koa;
constructor(config: AppConfiguration);
start(): Promise<void>;
}
export declare class Model {
constructor(obj?: { [key: string]: any }, rules?: Rules, msg?: ErrorMessages);
static create<T extends Model>(
obj?: { [key: string]: any },
rules?: Rules,
msg?: ErrorMessages
): T;
toJson(): string;
properties(): Array<string>;
count(): number;
validate(rules: Rules, msg?: ErrorMessages): Validator<this>;
}