@aomex/web 0.0.28 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -59
- package/LICENSE +1 -1
- package/dist/index.d.ts +225 -272
- package/dist/index.js +524 -623
- package/dist/index.js.map +1 -1
- package/package.json +28 -34
- package/dist/bin.d.ts +0 -1
- package/dist/bin.js +0 -10
- package/dist/bin.js.map +0 -1
- package/src/bin.ts +0 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,122 +1,149 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { I18nFormat, Next, Middleware, OpenAPI, MiddlewareChain, MixinMiddlewareToken, Validator, TransformedValidator, magistrate, MixinMiddlewareChain, I18n, ValidatorToken } from '@aomex/core';
|
|
2
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, RequestListener, Server } from 'node:http';
|
|
3
|
+
import { Stream, EventEmitter } from 'node:stream';
|
|
4
|
+
import { Accepts } from 'accepts';
|
|
5
5
|
import { HttpError } from 'http-errors';
|
|
6
6
|
export { HttpError, default as createHttpError } from 'http-errors';
|
|
7
|
-
import { IParseOptions } from 'qs';
|
|
8
|
-
import { CookieParseOptions, CookieSerializeOptions } from 'cookie';
|
|
9
|
-
import { Accepts } from 'accepts';
|
|
10
|
-
import { Stream } from 'node:stream';
|
|
11
7
|
import contentDisposition from 'content-disposition';
|
|
8
|
+
import { CookieSerializeOptions } from 'cookie';
|
|
9
|
+
import { NonReadonly } from '@aomex/internal-tools';
|
|
12
10
|
import { File } from 'formidable';
|
|
13
|
-
import { RequestListener as RequestListener$1, Server as Server$1 } from 'http';
|
|
14
11
|
export { default as statuses } from 'statuses';
|
|
15
12
|
|
|
16
13
|
declare module '@aomex/core' {
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
namespace I18n {
|
|
15
|
+
interface Definition {
|
|
16
|
+
web: {
|
|
17
|
+
validator: {
|
|
18
|
+
file: {
|
|
19
|
+
must_be_file: I18nFormat<{
|
|
20
|
+
label: string;
|
|
21
|
+
}>;
|
|
22
|
+
too_large: I18nFormat<{
|
|
23
|
+
label: string;
|
|
24
|
+
}>;
|
|
25
|
+
unsupported_mimetype: I18nFormat<{
|
|
26
|
+
label: string;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
19
32
|
}
|
|
20
33
|
}
|
|
21
|
-
type WebMiddlewareToken<P extends object = object> = WebMiddleware<P> | WebChain<P> | PureMiddlewareToken<P>;
|
|
22
|
-
declare class WebChain<Props extends object = object> extends Chain<Props> {
|
|
23
|
-
protected _web_chain_: 'web-chain';
|
|
24
|
-
mount: {
|
|
25
|
-
<P extends object>(middleware: WebChain | PureChain | WebMiddlewareToken<P> | null): WebChain<Props & P>;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
type UpperHeaderKeys = UpperArrayHeaderKeys | UpperStringHeaderKeys;
|
|
36
|
+
type UpperArrayHeaderKeys = 'Set-Cookie';
|
|
37
|
+
type UpperStringHeaderKeys = UpperExternalStringHeaderKeys | UpperOfficialStringHeaderKeys;
|
|
38
|
+
type LowerExternalStringHeaderKeys = 'accept-encoding' | 'x-forwarded-for' | 'x-forwarded-proto' | 'x-forwarded-host' | 'x-real-ip' | 'access-control-request-private-network' | 'access-control-allow-private-network' | ':authority';
|
|
39
|
+
type UpperExternalStringHeaderKeys = 'Accept-Encoding' | 'X-Forwarded-For' | 'X-Forwarded-Proto' | 'X-Forwarded-Host' | 'X-Real-IP' | 'Access-Control-Request-Private-Network' | 'Access-Control-Allow-Private-Network';
|
|
40
|
+
type UpperOfficialStringHeaderKeys = 'Accept' | 'Accept-Language' | 'Accept-Patch' | 'Accept-Ranges' | 'Access-Control-Allow-Credentials' | 'Access-Control-Allow-Headers' | 'Access-Control-Allow-Methods' | 'Access-Control-Allow-Origin' | 'Access-Control-Expose-Headers' | 'Access-Control-Max-Age' | 'Access-Control-Request-Headers' | 'Access-Control-Request-Method' | 'Age' | 'Allow' | 'Alt-Svc' | 'Authorization' | 'Cache-Control' | 'Connection' | 'Content-Disposition' | 'Content-Encoding' | 'Content-Language' | 'Content-Length' | 'Content-Location' | 'Content-Range' | 'Content-Type' | 'Cookie' | 'Date' | 'Etag' | 'Expect' | 'Expires' | 'Forwarded' | 'From' | 'Host' | 'If-Match' | 'If-Modified-Since' | 'If-None-Match' | 'If-Unmodified-Since' | 'Last-Modified' | 'Location' | 'Origin' | 'Pragma' | 'Proxy-Authenticate' | 'Proxy-Authorization' | 'Public-Key-Pins' | 'Range' | 'Referer' | 'Retry-After' | 'Sec-Websocket-Accept' | 'Sec-Websocket-Extensions' | 'Sec-Websocket-Key' | 'Sec-Websocket-Protocol' | 'Sec-Websocket-Version' | 'Strict-Transport-Security' | 'Tk' | 'Trailer' | 'Transfer-Encoding' | 'Upgrade' | 'User-Agent' | 'Vary' | 'Via' | 'Warning' | 'WWW-Authenticate';
|
|
41
|
+
|
|
42
|
+
declare class WebRequest {
|
|
43
|
+
app: WebApp;
|
|
44
|
+
req: IncomingMessage;
|
|
45
|
+
res: ServerResponse;
|
|
46
|
+
ctx: WebContext;
|
|
47
|
+
url: string;
|
|
48
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'CONNECT' | (string & {});
|
|
49
|
+
params: Record<string, unknown>;
|
|
50
|
+
private _accept?;
|
|
51
|
+
private _cookies?;
|
|
52
|
+
private _body?;
|
|
53
|
+
private _parsedUrl;
|
|
54
|
+
private _query?;
|
|
55
|
+
get accept(): Accepts;
|
|
56
|
+
get contentType(): string;
|
|
30
57
|
/**
|
|
31
|
-
*
|
|
58
|
+
* 返回从`headers['cookie']`解析后的cookie列表
|
|
32
59
|
*/
|
|
33
|
-
|
|
60
|
+
get cookies(): {
|
|
61
|
+
readonly [key: string]: string | undefined;
|
|
62
|
+
};
|
|
63
|
+
get body(): Promise<unknown>;
|
|
64
|
+
get fresh(): boolean;
|
|
65
|
+
get host(): string;
|
|
34
66
|
/**
|
|
35
|
-
*
|
|
67
|
+
* 包括了协议,域名,端口和路径的完整链接
|
|
36
68
|
*/
|
|
37
|
-
|
|
69
|
+
get href(): string;
|
|
70
|
+
get ip(): string;
|
|
71
|
+
get origin(): string;
|
|
72
|
+
get pathname(): string;
|
|
73
|
+
get protocol(): string;
|
|
38
74
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @see WebRequest.query
|
|
75
|
+
* 查询字符串对象
|
|
41
76
|
*/
|
|
42
|
-
query
|
|
43
|
-
cookie?: {
|
|
44
|
-
/**
|
|
45
|
-
* `ctx.request.cookie`的配置
|
|
46
|
-
* @see WebRequest.cookie
|
|
47
|
-
*/
|
|
48
|
-
get?: CookieParseOptions;
|
|
49
|
-
/**
|
|
50
|
-
* `ctx.response.cookie`的配置
|
|
51
|
-
* @see WebResponse.cookie
|
|
52
|
-
*/
|
|
53
|
-
set?: CookieSerializeOptions;
|
|
54
|
-
};
|
|
77
|
+
get query(): Record<string, unknown>;
|
|
55
78
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @see response()
|
|
79
|
+
* 查询字符串
|
|
58
80
|
*/
|
|
59
|
-
|
|
81
|
+
get querystring(): string;
|
|
82
|
+
/**
|
|
83
|
+
* 搜索字符串,比查询字符串多了一个开头问号(?)
|
|
84
|
+
*/
|
|
85
|
+
get search(): string;
|
|
86
|
+
get secure(): boolean;
|
|
87
|
+
protected get URL(): URL;
|
|
88
|
+
matchContentType(type: string, ...types: string[]): string | null;
|
|
60
89
|
}
|
|
61
|
-
declare
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
listen: Server['listen'];
|
|
68
|
-
callback(): RequestListener<any, any>;
|
|
69
|
-
log(err: HttpError): void;
|
|
70
|
-
on(eventName: 'error', listener: (err: HttpError, ctx: WebContext) => void): this;
|
|
71
|
-
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
72
|
-
mount(middleware: WebMiddlewareToken | null): void;
|
|
90
|
+
declare module 'node:http' {
|
|
91
|
+
type ExternalHeaders = {
|
|
92
|
+
[K in LowerExternalStringHeaderKeys]?: string;
|
|
93
|
+
};
|
|
94
|
+
interface IncomingHttpHeaders extends ExternalHeaders {
|
|
95
|
+
}
|
|
73
96
|
}
|
|
74
97
|
|
|
75
|
-
type UpperHeaderKeys = UpperArrayHeaderKeys | UpperStringHeaderKeys;
|
|
76
|
-
type UpperArrayHeaderKeys = 'Set-Cookie';
|
|
77
|
-
type UpperStringHeaderKeys = UpperExternalStringHeaderKeys | UpperOfficialStringHeaderKeys;
|
|
78
|
-
type LowerExternalStringHeaderKeys = 'accept-encoding' | 'x-forwarded-for' | 'x-forwarded-proto' | 'x-forwarded-host' | 'x-real-ip' | 'access-control-request-private-network' | 'access-control-allow-private-network' | ':authority';
|
|
79
|
-
type UpperExternalStringHeaderKeys = 'Accept-Encoding' | 'X-Forwarded-For' | 'X-Forwarded-Proto' | 'X-Forwarded-Host' | 'X-Real-IP' | 'Access-Control-Request-Private-Network' | 'Access-Control-Allow-Private-Network';
|
|
80
|
-
type UpperOfficialStringHeaderKeys = 'Accept' | 'Accept-Language' | 'Accept-Patch' | 'Accept-Ranges' | 'Access-Control-Allow-Credentials' | 'Access-Control-Allow-Headers' | 'Access-Control-Allow-Methods' | 'Access-Control-Allow-Origin' | 'Access-Control-Expose-Headers' | 'Access-Control-Max-Age' | 'Access-Control-Request-Headers' | 'Access-Control-Request-Method' | 'Age' | 'Allow' | 'Alt-Svc' | 'Authorization' | 'Cache-Control' | 'Connection' | 'Content-Disposition' | 'Content-Encoding' | 'Content-Language' | 'Content-Length' | 'Content-Location' | 'Content-Range' | 'Content-Type' | 'Cookie' | 'Date' | 'Etag' | 'Expect' | 'Expires' | 'Forwarded' | 'From' | 'Host' | 'If-Match' | 'If-Modified-Since' | 'If-None-Match' | 'If-Unmodified-Since' | 'Last-Modified' | 'Location' | 'Origin' | 'Pragma' | 'Proxy-Authenticate' | 'Proxy-Authorization' | 'Public-Key-Pins' | 'Range' | 'Referer' | 'Retry-After' | 'Sec-Websocket-Accept' | 'Sec-Websocket-Extensions' | 'Sec-Websocket-Key' | 'Sec-Websocket-Protocol' | 'Sec-Websocket-Version' | 'Strict-Transport-Security' | 'Tk' | 'Trailer' | 'Transfer-Encoding' | 'Upgrade' | 'User-Agent' | 'Vary' | 'Via' | 'Warning' | 'WWW-Authenticate';
|
|
81
|
-
|
|
82
98
|
type Body = string | object | Stream | Buffer | null;
|
|
83
|
-
|
|
84
|
-
set(name: string, value: string, options?: CookieSerializeOptions): void;
|
|
85
|
-
remove(name: string, options?: Omit<CookieSerializeOptions, 'maxAge' | 'expires'>): void;
|
|
86
|
-
}
|
|
87
|
-
declare module WebResponse {
|
|
88
|
-
type FakeType = typeof ServerResponse & (new (req: IncomingMessage) => ServerResponse<IncomingMessage>);
|
|
89
|
-
}
|
|
90
|
-
declare class WebResponse<Request extends WebRequest = WebRequest> extends ServerResponse<Request> {
|
|
99
|
+
declare class WebResponse {
|
|
91
100
|
app: WebApp;
|
|
101
|
+
res: ServerResponse;
|
|
92
102
|
ctx: WebContext;
|
|
103
|
+
req: WebRequest;
|
|
104
|
+
statusCode: number;
|
|
105
|
+
statusMessage: string;
|
|
93
106
|
private _body;
|
|
107
|
+
/**
|
|
108
|
+
* 是否明确设置过内容
|
|
109
|
+
*/
|
|
94
110
|
private _explicitBody;
|
|
111
|
+
/**
|
|
112
|
+
* 是否明确设置过状态码
|
|
113
|
+
*/
|
|
95
114
|
private _explicitStatus;
|
|
96
115
|
private _statusCode;
|
|
97
|
-
private
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
116
|
+
private _updatingBodyType;
|
|
117
|
+
constructor(req: IncomingMessage);
|
|
118
|
+
get contentLength(): number;
|
|
119
|
+
set contentLength(length: number);
|
|
120
|
+
get contentType(): string;
|
|
121
|
+
set contentType(typeOrFilenameOrExt: string);
|
|
122
|
+
get body(): Body;
|
|
123
|
+
set body(val: Body);
|
|
124
|
+
download(filePath: string, options?: contentDisposition.Options): void;
|
|
125
|
+
getHeader: {
|
|
107
126
|
(name: UpperStringHeaderKeys): string | undefined;
|
|
108
127
|
(name: UpperArrayHeaderKeys): string[] | undefined;
|
|
109
128
|
(name: string): string | string[] | undefined;
|
|
110
129
|
};
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
130
|
+
getHeaderNames: () => (UpperStringHeaderKeys | UpperArrayHeaderKeys | (string & {}))[];
|
|
131
|
+
getHeaders: () => OutgoingHttpHeaders;
|
|
132
|
+
flush(): void;
|
|
133
|
+
hasHeader: (name: UpperStringHeaderKeys | UpperArrayHeaderKeys | (string & {})) => boolean;
|
|
134
|
+
isJSON(body: Body): body is object;
|
|
135
|
+
matchContentType(type: string, ...types: string[]): string | null;
|
|
136
|
+
onError(error?: Error | HttpError | null): void;
|
|
137
|
+
redirect(url: string): void;
|
|
138
|
+
redirect(statusCode: 300 | 301 | 302 | 303 | 305 | 307 | 308, url: string): void;
|
|
139
|
+
removeCookie(name: string, options?: Pick<CookieSerializeOptions, 'path'>): void;
|
|
140
|
+
removeHeader: (name: UpperStringHeaderKeys | UpperArrayHeaderKeys | (string & {})) => void;
|
|
141
|
+
removeHeaders(...headers: ((string & {}) | UpperStringHeaderKeys | UpperArrayHeaderKeys)[]): void;
|
|
142
|
+
setCookie(name: string, value: string, options?: CookieSerializeOptions): void;
|
|
143
|
+
setHeader: {
|
|
144
|
+
<T extends WebResponse>(name: UpperStringHeaderKeys, value: number | string): T;
|
|
145
|
+
<T extends WebResponse>(name: UpperArrayHeaderKeys, value: readonly string[]): T;
|
|
146
|
+
<T extends WebResponse>(name: string, value: number | string | readonly string[]): T;
|
|
120
147
|
};
|
|
121
148
|
setHeaders(headers: {
|
|
122
149
|
[K: string]: string | number | readonly string[];
|
|
@@ -125,98 +152,9 @@ declare class WebResponse<Request extends WebRequest = WebRequest> extends Serve
|
|
|
125
152
|
} & {
|
|
126
153
|
[K in UpperArrayHeaderKeys]?: readonly string[];
|
|
127
154
|
}): void;
|
|
128
|
-
|
|
129
|
-
protected removeHeaders(...headers: Array<string>): void;
|
|
130
|
-
redirect(url: string): void;
|
|
131
|
-
redirect(statusCode: 300 | 301 | 302 | 303 | 305 | 307 | 308, url: string): void;
|
|
132
|
-
download(filePath: string, options?: contentDisposition.Options): void;
|
|
133
|
-
get contentLength(): number;
|
|
134
|
-
set contentLength(length: number);
|
|
135
|
-
get contentType(): string;
|
|
136
|
-
set contentType(typeOrFilenameOrExt: string);
|
|
137
|
-
get body(): Body;
|
|
138
|
-
set body(val: Body);
|
|
139
|
-
isJSON(body: Body): body is object;
|
|
140
|
-
flush(): any;
|
|
141
|
-
onError(error?: Error | HttpError | null): void;
|
|
142
|
-
findContentType(type: string, ...types: string[]): string | null;
|
|
143
|
-
vary(field: UpperHeaderKeys | UpperHeaderKeys[]): void;
|
|
144
|
-
vary(field: string | string[]): void;
|
|
145
|
-
varyAppend(header: UpperHeaderKeys, field: UpperHeaderKeys | UpperHeaderKeys[]): string;
|
|
146
|
-
varyAppend(header: UpperHeaderKeys, field: string | string[]): string;
|
|
147
|
-
varyAppend(header: string, field: UpperHeaderKeys | UpperHeaderKeys[]): string;
|
|
148
|
-
varyAppend(header: string, field: string | string[]): string;
|
|
149
|
-
get cookie(): CookieCache;
|
|
155
|
+
vary(field: UpperHeaderKeys | UpperHeaderKeys[] | (string & {}) | (string & {})[]): string;
|
|
150
156
|
protected setStatus(code: number): void;
|
|
151
|
-
protected
|
|
152
|
-
protected determineNullBody(): void;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
declare const getMimeType: (filenameOrExt: string) => string | false;
|
|
156
|
-
|
|
157
|
-
declare enum METHOD {
|
|
158
|
-
GET = "GET",
|
|
159
|
-
POST = "POST",
|
|
160
|
-
PUT = "PUT",
|
|
161
|
-
PATCH = "PATCH",
|
|
162
|
-
DELETE = "DELETE",
|
|
163
|
-
OPTIONS = "OPTIONS",
|
|
164
|
-
HEAD = "HEAD"
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
declare const createHttpServer: (listener: RequestListener$1<typeof WebRequest,
|
|
168
|
-
/** @ts-ignore */
|
|
169
|
-
typeof WebResponse<WebRequest>>) => Server$1<any, any>;
|
|
170
|
-
|
|
171
|
-
declare class WebRequest extends IncomingMessage {
|
|
172
|
-
app: WebApp;
|
|
173
|
-
res: WebResponse;
|
|
174
|
-
ctx: WebContext;
|
|
175
|
-
method: METHOD;
|
|
176
|
-
url: string;
|
|
177
|
-
params: Record<string, unknown>;
|
|
178
|
-
protected _query?: any;
|
|
179
|
-
protected _body?: any;
|
|
180
|
-
protected _accept?: Accepts;
|
|
181
|
-
protected _cookie?: {
|
|
182
|
-
readonly [key: string]: string | undefined;
|
|
183
|
-
};
|
|
184
|
-
protected _parsedUrl: URL | null;
|
|
185
|
-
get pathname(): string;
|
|
186
|
-
/**
|
|
187
|
-
* 搜索字符串,比查询字符串多了一个开头问号(?)
|
|
188
|
-
*/
|
|
189
|
-
get search(): string;
|
|
190
|
-
get querystring(): string;
|
|
191
|
-
get query(): Record<string, unknown>;
|
|
192
|
-
get body(): Promise<unknown>;
|
|
193
|
-
get contentType(): string;
|
|
194
|
-
findContentType(type: string, ...types: string[]): string | null;
|
|
195
|
-
get accept(): Accepts;
|
|
196
|
-
get ip(): string;
|
|
197
|
-
/**
|
|
198
|
-
* 包括了协议,域名,端口和路径的完整链接
|
|
199
|
-
*/
|
|
200
|
-
get href(): string;
|
|
201
|
-
get origin(): string;
|
|
202
|
-
get host(): string;
|
|
203
|
-
get protocol(): string;
|
|
204
|
-
get secure(): boolean;
|
|
205
|
-
get fresh(): boolean;
|
|
206
|
-
/**
|
|
207
|
-
* 把请求头部的`Cookie`字段解析成对象格式
|
|
208
|
-
*/
|
|
209
|
-
get cookie(): {
|
|
210
|
-
readonly [key: string]: string | undefined;
|
|
211
|
-
};
|
|
212
|
-
protected get URL(): URL;
|
|
213
|
-
}
|
|
214
|
-
declare module 'node:http' {
|
|
215
|
-
type ExternalHeaders = {
|
|
216
|
-
[K in LowerExternalStringHeaderKeys]?: string;
|
|
217
|
-
};
|
|
218
|
-
interface IncomingHttpHeaders extends ExternalHeaders {
|
|
219
|
-
}
|
|
157
|
+
protected updateBodyType(): void;
|
|
220
158
|
}
|
|
221
159
|
|
|
222
160
|
declare class WebContext {
|
|
@@ -224,6 +162,16 @@ declare class WebContext {
|
|
|
224
162
|
readonly request: WebRequest;
|
|
225
163
|
readonly response: WebResponse;
|
|
226
164
|
constructor(app: WebApp, request: WebRequest, response: WebResponse);
|
|
165
|
+
/**
|
|
166
|
+
* 发送响应内容和状态,包含字符串,对象,数据流,缓冲区等。
|
|
167
|
+
* ```typescript
|
|
168
|
+
* ctx.send(200, 'hello aomex');
|
|
169
|
+
* ctx.send({ count: 1 });
|
|
170
|
+
* ```
|
|
171
|
+
* 更多快捷方式:
|
|
172
|
+
* - 链接重定向:`ctx.response.redirect(...)`
|
|
173
|
+
* - 下载文件:`ctx.response.download(...)`
|
|
174
|
+
*/
|
|
227
175
|
send(statusCode: number, body?: Body): this;
|
|
228
176
|
send(body: Body): this;
|
|
229
177
|
/**
|
|
@@ -242,41 +190,63 @@ declare class WebContext {
|
|
|
242
190
|
* @see createHttpError.HttpError
|
|
243
191
|
*
|
|
244
192
|
*/
|
|
245
|
-
throw(statusCode: number, message?: string | Error, properties?:
|
|
246
|
-
throw(message: string | Error, statusCode?: number, properties?:
|
|
247
|
-
throw(
|
|
193
|
+
throw(statusCode: number, message?: string | Error, properties?: HttpErrorProperties): never;
|
|
194
|
+
throw(message: string | Error, statusCode?: number, properties?: HttpErrorProperties): never;
|
|
195
|
+
throw(statusCode?: number, properties?: HttpErrorProperties): never;
|
|
248
196
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
197
|
+
interface HttpErrorProperties {
|
|
198
|
+
expose?: boolean;
|
|
199
|
+
statusCode?: number;
|
|
200
|
+
code?: string;
|
|
201
|
+
headers?: Record<string, any>;
|
|
202
|
+
cause?: any;
|
|
203
|
+
[key: string]: any;
|
|
255
204
|
}
|
|
256
|
-
/**
|
|
257
|
-
* 跳过中间件或者链条
|
|
258
|
-
* - 通过条件判断 - options=`object|function`
|
|
259
|
-
* - 不跳过 - options=`false`
|
|
260
|
-
* - 总是跳过 - options=`true`
|
|
261
|
-
*/
|
|
262
|
-
declare function skip<Props extends object, T extends WebMiddlewareSkipOptions | NonNullable<WebMiddlewareSkipOptions['custom']> | boolean>(token: WebMiddlewareToken<Props>, options: T): WebMiddleware<T extends false ? Props : T extends true ? object : Partial<Props>>;
|
|
263
205
|
|
|
264
206
|
declare module '@aomex/core' {
|
|
265
207
|
interface MiddlewarePlatform {
|
|
266
|
-
readonly web: <Props extends object = object>(fn:
|
|
208
|
+
readonly web: <Props extends object = object>(fn: WebMiddlewareArguments<Props>['fn'] | WebMiddlewareArguments<Props>) => WebMiddleware<Props>;
|
|
267
209
|
}
|
|
268
210
|
}
|
|
269
|
-
interface
|
|
270
|
-
document: OpenAPI.Document;
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
211
|
+
interface OpenApiInjector {
|
|
212
|
+
onDocument?: (document: OpenAPI.Document) => void;
|
|
213
|
+
postDocument?: OpenApiInjector['onDocument'];
|
|
214
|
+
onPath?: (pathItem: OpenAPI.PathItemObject, opts: {
|
|
215
|
+
document: OpenAPI.Document;
|
|
216
|
+
pathName: string;
|
|
217
|
+
}) => void;
|
|
218
|
+
postPath?: OpenApiInjector['onPath'];
|
|
219
|
+
onMethod?: (methodItem: OpenAPI.OperationObject, opts: {
|
|
220
|
+
document: OpenAPI.Document;
|
|
221
|
+
pathName: string;
|
|
222
|
+
pathItem: OpenAPI.PathItemObject;
|
|
223
|
+
methodName: `${Lowercase<WebRequest['method']>}`;
|
|
224
|
+
}) => void;
|
|
225
|
+
postMethod?: OpenApiInjector['onMethod'];
|
|
226
|
+
}
|
|
227
|
+
interface WebMiddlewareArguments<T extends object> {
|
|
228
|
+
fn: (ctx: NonReadonly<T> & WebContext, next: Next) => any;
|
|
229
|
+
openapi?: OpenApiInjector;
|
|
274
230
|
}
|
|
275
231
|
declare class WebMiddleware<Props extends object = object> extends Middleware<Props> {
|
|
232
|
+
_contextType: WebContext;
|
|
276
233
|
protected _web_middleware_: 'web-middleware';
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
234
|
+
private readonly openapiInjector;
|
|
235
|
+
constructor(args: WebMiddlewareArguments<Props>['fn'] | WebMiddlewareArguments<Props>);
|
|
236
|
+
protected openapi(): OpenApiInjector;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
declare module '@aomex/core' {
|
|
240
|
+
interface MiddlewareChainPlatform {
|
|
241
|
+
readonly web: WebMiddlewareChain;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
type WebMiddlewareToken<P extends object = object> = WebMiddleware<P> | WebMiddlewareChain<P> | MixinMiddlewareToken<P>;
|
|
245
|
+
declare class WebMiddlewareChain<Props extends object = object> extends MiddlewareChain<Props> {
|
|
246
|
+
protected _web_chain_: 'web-chain';
|
|
247
|
+
mount: {
|
|
248
|
+
<P extends object>(middleware: WebMiddlewareToken<P> | null): WebMiddlewareChain<Props & P>;
|
|
249
|
+
};
|
|
280
250
|
}
|
|
281
251
|
|
|
282
252
|
declare module '@aomex/core' {
|
|
@@ -318,22 +288,42 @@ declare class FileValidator<T = FileValidator.FormidableFile> extends Validator<
|
|
|
318
288
|
mimeTypes(mineOrExt: string, ...others: string[]): this;
|
|
319
289
|
mimeTypes(mineOrExt: string[]): this;
|
|
320
290
|
protected isEmpty(value: any): boolean;
|
|
321
|
-
protected validateValue(file: FileValidator.FormidableFile,
|
|
291
|
+
protected validateValue(file: FileValidator.FormidableFile, _key: string, label: string): magistrate.Result<FileValidator.FormidableFile>;
|
|
322
292
|
protected copy: () => FileValidator<T>;
|
|
323
293
|
protected toDocument(): OpenAPI.SchemaObject;
|
|
324
294
|
}
|
|
325
295
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
296
|
+
interface WebAppOption {
|
|
297
|
+
/**
|
|
298
|
+
* 调试模式。默认值:`process.env.NODE_ENV !== 'production'`
|
|
299
|
+
*/
|
|
300
|
+
debug?: boolean;
|
|
301
|
+
/**
|
|
302
|
+
* 全局中间件组,挂载后该组会被打上标记。
|
|
303
|
+
* ```typescript
|
|
304
|
+
* const appChain = mdchain.web.mount(md1).mount(md2);
|
|
305
|
+
* const chain1 = appChain.mount(md3);
|
|
306
|
+
* const chain2 = chain1.mount(md4);
|
|
307
|
+
*
|
|
308
|
+
* const app = new WebApp({ box: appChain });
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
mount?: WebMiddlewareChain | MixinMiddlewareChain;
|
|
312
|
+
locale?: I18n.LocaleName;
|
|
313
|
+
}
|
|
314
|
+
declare class WebApp extends EventEmitter<{
|
|
315
|
+
error: [err: HttpError, ctx: WebContext];
|
|
332
316
|
}> {
|
|
333
|
-
protected readonly
|
|
334
|
-
|
|
335
|
-
|
|
317
|
+
protected readonly options: WebAppOption;
|
|
318
|
+
protected readonly point?: string;
|
|
319
|
+
protected readonly middlewareList: Middleware[];
|
|
320
|
+
constructor(options?: WebAppOption);
|
|
321
|
+
get debug(): boolean;
|
|
322
|
+
callback(): RequestListener<any, any>;
|
|
323
|
+
listen: Server['listen'];
|
|
324
|
+
log(err: HttpError, ctx: WebContext): void;
|
|
336
325
|
}
|
|
326
|
+
|
|
337
327
|
/**
|
|
338
328
|
* 验证请求实体
|
|
339
329
|
*
|
|
@@ -352,19 +342,10 @@ declare class WebBodyMiddleware<Props extends {
|
|
|
352
342
|
*/
|
|
353
343
|
declare const body: <T extends {
|
|
354
344
|
[key: string]: P;
|
|
355
|
-
}, P extends Validator<unknown>>(fields: T) =>
|
|
345
|
+
}, P extends Validator<unknown>>(fields: T) => WebMiddleware<{
|
|
346
|
+
readonly body: Validator.Infer<T>;
|
|
347
|
+
}>;
|
|
356
348
|
|
|
357
|
-
declare class WebQueryMiddleware<Props extends {
|
|
358
|
-
[key: string]: Validator;
|
|
359
|
-
}> extends WebMiddleware<{
|
|
360
|
-
readonly query: {
|
|
361
|
-
[K in keyof Props]: Validator.Infer<Props[K]>;
|
|
362
|
-
};
|
|
363
|
-
}> {
|
|
364
|
-
protected readonly props: Props;
|
|
365
|
-
constructor(props: Props);
|
|
366
|
-
toDocument(options: WebMiddlewareToDocument): void;
|
|
367
|
-
}
|
|
368
349
|
/**
|
|
369
350
|
* 验证请求查询字符串
|
|
370
351
|
*
|
|
@@ -382,19 +363,10 @@ declare class WebQueryMiddleware<Props extends {
|
|
|
382
363
|
*/
|
|
383
364
|
declare const query: <T extends {
|
|
384
365
|
[key: string]: P;
|
|
385
|
-
}, P extends Validator<unknown>>(fields: T) =>
|
|
366
|
+
}, P extends Validator<unknown>>(fields: T) => WebMiddleware<{
|
|
367
|
+
readonly query: Validator.Infer<T>;
|
|
368
|
+
}>;
|
|
386
369
|
|
|
387
|
-
declare class WebParamMiddleware<Props extends {
|
|
388
|
-
[key: string]: Validator;
|
|
389
|
-
}> extends WebMiddleware<{
|
|
390
|
-
readonly params: {
|
|
391
|
-
[K in keyof Props]: Validator.Infer<Props[K]>;
|
|
392
|
-
};
|
|
393
|
-
}> {
|
|
394
|
-
protected readonly props: Props;
|
|
395
|
-
constructor(props: Props);
|
|
396
|
-
toDocument(options: WebMiddlewareToDocument): void;
|
|
397
|
-
}
|
|
398
370
|
/**
|
|
399
371
|
* 验证请求路径参数
|
|
400
372
|
*
|
|
@@ -411,61 +383,42 @@ declare class WebParamMiddleware<Props extends {
|
|
|
411
383
|
*/
|
|
412
384
|
declare const params: <T extends {
|
|
413
385
|
[key: string]: P;
|
|
414
|
-
}, P extends Validator<unknown>>(fields: T) =>
|
|
386
|
+
}, P extends Validator<unknown>>(fields: T) => WebMiddleware<{
|
|
387
|
+
readonly params: Validator.Infer<T>;
|
|
388
|
+
}>;
|
|
415
389
|
|
|
416
390
|
interface WebResponseOptions extends Pick<OpenAPI.MediaTypeObject, 'example'> {
|
|
391
|
+
statusCode: number;
|
|
417
392
|
/**
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* - 201
|
|
421
|
-
* - 2xx
|
|
422
|
-
* - 3xx
|
|
423
|
-
* - 20x
|
|
424
|
-
* - 40x
|
|
425
|
-
* - default (handle unknown response)
|
|
426
|
-
*/
|
|
427
|
-
statusCode: number | string;
|
|
428
|
-
/**
|
|
393
|
+
* 响应格式,支持简写和完整写法。如果不填,则根据响应值判断
|
|
394
|
+
*
|
|
429
395
|
* - json
|
|
430
396
|
* - application/json
|
|
431
397
|
* - text
|
|
432
398
|
* - binary
|
|
433
399
|
* - html
|
|
434
400
|
* - stream
|
|
435
|
-
* - \*\/\*
|
|
401
|
+
* - \*\/\*
|
|
436
402
|
*/
|
|
437
403
|
contentType?: string;
|
|
438
404
|
/**
|
|
439
|
-
*
|
|
405
|
+
* 最终的响应值类型
|
|
440
406
|
*/
|
|
441
|
-
schema?:
|
|
407
|
+
schema?: ValidatorToken;
|
|
442
408
|
headers?: {
|
|
443
409
|
[key: string]: Validator;
|
|
444
410
|
};
|
|
445
411
|
description?: string;
|
|
446
|
-
/**
|
|
447
|
-
* 验证响应数据。
|
|
448
|
-
*
|
|
449
|
-
* 如果未设置,则查看`app.options.validateResponse`。仍未设置,则查看`app.options.debug`
|
|
450
|
-
*
|
|
451
|
-
* ```typescript
|
|
452
|
-
* const app = new WebApp({
|
|
453
|
-
* validateResponse: false
|
|
454
|
-
* })
|
|
455
|
-
* ```
|
|
456
|
-
*/
|
|
457
|
-
validate?: boolean;
|
|
458
412
|
}
|
|
459
|
-
/**
|
|
460
|
-
* 为openapi生成响应数据文档,并提供运行时验证(严格模式)
|
|
461
|
-
*/
|
|
462
|
-
declare const response: (options: WebResponseOptions) => WebResponseMiddleware;
|
|
463
413
|
declare class WebResponseMiddleware extends WebMiddleware<object> {
|
|
464
414
|
protected readonly options: WebResponseOptions;
|
|
465
415
|
constructor(options: WebResponseOptions);
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
protected fixContentType(contentType: string): string;
|
|
416
|
+
protected openapi(): OpenApiInjector;
|
|
417
|
+
protected getContentType(): string;
|
|
469
418
|
}
|
|
419
|
+
/**
|
|
420
|
+
* 为openapi生成响应数据文档
|
|
421
|
+
*/
|
|
422
|
+
declare const response: (options: WebResponseOptions) => WebResponseMiddleware;
|
|
470
423
|
|
|
471
|
-
export { Body, FileValidator,
|
|
424
|
+
export { type Body, FileValidator, type OpenApiInjector, WebApp, type WebAppOption, WebContext, WebMiddleware, WebMiddlewareChain, type WebMiddlewareToken, WebRequest, WebResponse, WebResponseMiddleware, type WebResponseOptions, body, params, query, response };
|