h3 0.7.17 → 0.7.18
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/dist/index.cjs +161 -537
- package/dist/index.d.ts +56 -249
- package/dist/index.mjs +159 -478
- package/package.json +19 -20
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,4 @@
|
|
|
1
|
-
import http
|
|
2
|
-
import { CookieSerializeOptions } from 'cookie-es';
|
|
3
|
-
import * as ufo from 'ufo';
|
|
4
|
-
|
|
5
|
-
interface H3EventContext extends Record<string, any> {
|
|
6
|
-
}
|
|
7
|
-
interface H3Event {
|
|
8
|
-
'__is_event__': true;
|
|
9
|
-
event: H3Event;
|
|
10
|
-
req: IncomingMessage;
|
|
11
|
-
res: ServerResponse;
|
|
12
|
-
context: H3EventContext;
|
|
13
|
-
}
|
|
14
|
-
declare type CompatibilityEvent = H3Event | IncomingMessage;
|
|
15
|
-
declare type H3Response<T = any> = T | Promise<T>;
|
|
16
|
-
interface EventHandler<T = any> {
|
|
17
|
-
'__is_handler__'?: true;
|
|
18
|
-
(event: CompatibilityEvent): H3Response<T>;
|
|
19
|
-
}
|
|
20
|
-
declare function defineEventHandler<T = any>(handler: EventHandler<T>): EventHandler<T>;
|
|
21
|
-
declare const eventHandler: typeof defineEventHandler;
|
|
22
|
-
declare type LazyEventHandler = () => EventHandler | Promise<EventHandler>;
|
|
23
|
-
declare function defineLazyEventHandler(factory: LazyEventHandler): EventHandler;
|
|
24
|
-
declare const lazyEventHandler: typeof defineLazyEventHandler;
|
|
25
|
-
interface DynamicEventHandler extends EventHandler {
|
|
26
|
-
set: (handler: EventHandler) => void;
|
|
27
|
-
}
|
|
28
|
-
declare function dynamicEventHandler(initial?: EventHandler): DynamicEventHandler;
|
|
29
|
-
declare function isEventHandler(input: any): input is EventHandler;
|
|
30
|
-
declare type CompatibilityEventHandler = EventHandler | Handler | Middleware;
|
|
31
|
-
declare function toEventHandler(handler: CompatibilityEventHandler): EventHandler;
|
|
32
|
-
declare function createEvent(req: http.IncomingMessage, res: http.ServerResponse): CompatibilityEvent;
|
|
33
|
-
declare function isEvent(input: any): input is H3Event;
|
|
1
|
+
import http from 'http';
|
|
34
2
|
|
|
35
3
|
interface CompatibilityRequestProps {
|
|
36
4
|
event: H3Event;
|
|
@@ -48,230 +16,69 @@ interface ServerResponse extends http.ServerResponse {
|
|
|
48
16
|
req: http.ServerResponse['req'] & CompatibilityRequestProps;
|
|
49
17
|
}
|
|
50
18
|
declare type Handler<T = any, ReqT = {}> = (req: IncomingMessage & ReqT, res: ServerResponse) => T;
|
|
51
|
-
declare type PromisifiedHandler = Handler<Promise<any>>;
|
|
52
19
|
declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any;
|
|
53
|
-
|
|
54
|
-
declare type Encoding = false | 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'latin1' | 'binary' | 'hex';
|
|
55
|
-
declare type HTTPMethod = 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE';
|
|
56
|
-
|
|
57
|
-
interface Layer {
|
|
58
|
-
route: string;
|
|
59
|
-
match?: Matcher;
|
|
60
|
-
handler: EventHandler;
|
|
61
|
-
}
|
|
62
|
-
declare type Stack = Layer[];
|
|
63
|
-
interface InputLayer {
|
|
64
|
-
route?: string;
|
|
65
|
-
match?: Matcher;
|
|
66
|
-
handler: Handler | LazyHandler | EventHandler | LazyEventHandler;
|
|
67
|
-
lazy?: boolean;
|
|
68
|
-
/** @deprecated */
|
|
69
|
-
handle?: Handler;
|
|
70
|
-
/** @deprecated */
|
|
71
|
-
promisify?: boolean;
|
|
72
|
-
}
|
|
73
|
-
declare type InputStack = InputLayer[];
|
|
74
|
-
declare type Matcher = (url: string, event?: CompatibilityEvent) => boolean;
|
|
75
|
-
interface AppUse {
|
|
76
|
-
(route: string | string[], handler: CompatibilityEventHandler | CompatibilityEventHandler[], options?: Partial<InputLayer>): App;
|
|
77
|
-
(handler: CompatibilityEventHandler | CompatibilityEventHandler[], options?: Partial<InputLayer>): App;
|
|
78
|
-
(options: InputLayer): App;
|
|
79
|
-
}
|
|
80
|
-
declare type NodeHandler = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
|
|
81
|
-
interface App extends NodeHandler {
|
|
82
|
-
stack: Stack;
|
|
83
|
-
handler: EventHandler;
|
|
84
|
-
nodeHandler: NodeHandler;
|
|
85
|
-
use: AppUse;
|
|
20
|
+
interface H3EventContext extends Record<string, any> {
|
|
86
21
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
22
|
+
declare type HandlerResponse<T = any> = T | Promise<T>;
|
|
23
|
+
interface EventHandler<T = any> {
|
|
24
|
+
'__is_handler__'?: true;
|
|
25
|
+
(event: H3Event): HandlerResponse<T>;
|
|
90
26
|
}
|
|
91
|
-
declare
|
|
92
|
-
declare
|
|
93
|
-
declare function createAppEventHandler(stack: Stack, options: AppOptions): EventHandler<void>;
|
|
27
|
+
declare type LazyEventHandler = () => EventHandler | Promise<EventHandler>;
|
|
28
|
+
declare type CompatibilityEventHandler = EventHandler | Handler | Middleware;
|
|
94
29
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
* This can be used to pass additional information about the error.
|
|
105
|
-
* @property {Boolean} internal Setting this property to <code>true</code> will mark error as an internal error
|
|
106
|
-
*/
|
|
107
|
-
declare class H3Error extends Error {
|
|
108
|
-
static __h3_error__: boolean;
|
|
109
|
-
statusCode: number;
|
|
110
|
-
fatal: boolean;
|
|
111
|
-
unhandled: boolean;
|
|
112
|
-
statusMessage: string;
|
|
113
|
-
data?: any;
|
|
30
|
+
declare class H3Headers implements Headers {
|
|
31
|
+
_headers: Record<string, string>;
|
|
32
|
+
constructor(init?: HeadersInit);
|
|
33
|
+
append(name: string, value: string): void;
|
|
34
|
+
delete(name: string): void;
|
|
35
|
+
get(name: string): string | null;
|
|
36
|
+
has(name: string): boolean;
|
|
37
|
+
set(name: string, value: string): void;
|
|
38
|
+
forEach(callbackfn: (value: string, key: string, parent: Headers) => void): void;
|
|
114
39
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Creates new `Error` that can be used to handle both internal and runtime errors.
|
|
117
|
-
*
|
|
118
|
-
* @param input {Partial<H3Error>}
|
|
119
|
-
* @return {H3Error} An instance of the H3Error
|
|
120
|
-
*/
|
|
121
|
-
declare function createError(input: string | Partial<H3Error>): H3Error;
|
|
122
|
-
/**
|
|
123
|
-
* Receive an error and return the corresponding response.<br>
|
|
124
|
-
* H3 internally uses this function to handle unhandled errors.<br>
|
|
125
|
-
* Note that calling this function will close the connection and no other data will be sent to client afterwards.
|
|
126
|
-
*
|
|
127
|
-
@param event {CompatibilityEvent} H3 event or req passed by h3 handler
|
|
128
|
-
* @param error {H3Error|Error} Raised error
|
|
129
|
-
* @param debug {Boolean} Whether application is in debug mode.<br>
|
|
130
|
-
* In the debug mode the stack trace of errors will be return in response.
|
|
131
|
-
*/
|
|
132
|
-
declare function sendError(event: CompatibilityEvent, error: Error | H3Error, debug?: boolean): void;
|
|
133
|
-
declare function isError(input: any): input is H3Error;
|
|
134
|
-
|
|
135
|
-
declare const defineHandler: <T>(handler: Handler<T, {}>) => Handler<T, {}>;
|
|
136
|
-
/** @deprecated Use defineHandler */
|
|
137
|
-
declare const defineHandle: <T>(handler: Handler<T, {}>) => Handler<T, {}>;
|
|
138
|
-
declare const defineMiddleware: (middleware: Middleware) => Middleware;
|
|
139
|
-
declare function promisifyHandler(handler: Handler | Middleware): PromisifiedHandler;
|
|
140
|
-
/** @deprecated Use defineHandler */
|
|
141
|
-
declare const promisifyHandle: typeof promisifyHandler;
|
|
142
|
-
declare function callHandler(handler: Middleware, req: IncomingMessage, res: ServerResponse): Promise<unknown>;
|
|
143
|
-
declare function defineLazyHandler(handler: LazyHandler, promisify?: boolean): Handler;
|
|
144
|
-
/** @deprecated Use defineLazyHandler */
|
|
145
|
-
declare const lazyHandle: typeof defineLazyHandler;
|
|
146
|
-
declare function useBase(base: string, handler: Handler): Handler;
|
|
147
40
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
* const body = await useBody(req)
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
declare function readBody<T = any>(event: CompatibilityEvent): Promise<T>;
|
|
170
|
-
/** @deprecated Use `h3.readBody` */
|
|
171
|
-
declare const useBody: typeof readBody;
|
|
172
|
-
|
|
173
|
-
interface CacheConditions {
|
|
174
|
-
modifiedTime?: string | Date;
|
|
175
|
-
maxAge?: number;
|
|
176
|
-
etag?: string;
|
|
177
|
-
cacheControls?: string[];
|
|
41
|
+
declare class H3Response implements Response {
|
|
42
|
+
readonly headers: H3Headers;
|
|
43
|
+
readonly status: number;
|
|
44
|
+
readonly statusText: string;
|
|
45
|
+
readonly redirected: boolean;
|
|
46
|
+
readonly ok: boolean;
|
|
47
|
+
readonly url: string;
|
|
48
|
+
_body: string | ArrayBuffer | Uint8Array;
|
|
49
|
+
readonly body: ReadableStream<Uint8Array> | null;
|
|
50
|
+
readonly type: ResponseType;
|
|
51
|
+
readonly bodyUsed = false;
|
|
52
|
+
constructor(body?: BodyInit | HandlerResponse | null, init?: ResponseInit);
|
|
53
|
+
clone(): H3Response;
|
|
54
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
55
|
+
blob(): Promise<Blob>;
|
|
56
|
+
formData(): Promise<FormData>;
|
|
57
|
+
json<T = any>(): Promise<T>;
|
|
58
|
+
text(): Promise<string>;
|
|
178
59
|
}
|
|
179
|
-
/**
|
|
180
|
-
* Check request caching headers (`If-Modified-Since`) and add caching headers (Last-Modified, Cache-Control)
|
|
181
|
-
* Note: `public` cache control will be added by default
|
|
182
|
-
* @returns `true` when cache headers are matching. When `true` is returned, no reponse should be sent anymore
|
|
183
|
-
*/
|
|
184
|
-
declare function handleCacheHeaders(event: CompatibilityEvent, opts: CacheConditions): boolean;
|
|
185
|
-
|
|
186
|
-
declare const MIMES: {
|
|
187
|
-
html: string;
|
|
188
|
-
json: string;
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Parse the request to get HTTP Cookie header string and returning an object of all cookie name-value pairs.
|
|
193
|
-
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
|
|
194
|
-
* @returns Object of cookie name-value pairs
|
|
195
|
-
* ```ts
|
|
196
|
-
* const cookies = parseCookies(event)
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
declare function parseCookies(event: CompatibilityEvent): Record<string, string>;
|
|
200
|
-
/** @deprecated Use `h3.parseCookies` */
|
|
201
|
-
declare const useCookies: typeof parseCookies;
|
|
202
|
-
/**
|
|
203
|
-
* Get a cookie value by name.
|
|
204
|
-
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
|
|
205
|
-
* @param name Name of the cookie to get
|
|
206
|
-
* @returns {*} Value of the cookie (String or undefined)
|
|
207
|
-
* ```ts
|
|
208
|
-
* const authorization = useCookie(request, 'Authorization')
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
declare function getCookie(event: CompatibilityEvent, name: string): string | undefined;
|
|
212
|
-
/** @deprecated Use `h3.getCookie` */
|
|
213
|
-
declare const useCookie: typeof getCookie;
|
|
214
|
-
/**
|
|
215
|
-
* Set a cookie value by name.
|
|
216
|
-
* @param event {CompatibilityEvent} H3 event or res passed by h3 handler
|
|
217
|
-
* @param name Name of the cookie to set
|
|
218
|
-
* @param value Value of the cookie to set
|
|
219
|
-
* @param serializeOptions {CookieSerializeOptions} Options for serializing the cookie
|
|
220
|
-
* ```ts
|
|
221
|
-
* setCookie(res, 'Authorization', '1234567')
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
|
-
declare function setCookie(event: CompatibilityEvent, name: string, value: string, serializeOptions?: CookieSerializeOptions): void;
|
|
225
|
-
/**
|
|
226
|
-
* Set a cookie value by name.
|
|
227
|
-
* @param event {CompatibilityEvent} H3 event or res passed by h3 handler
|
|
228
|
-
* @param name Name of the cookie to delete
|
|
229
|
-
* @param serializeOptions {CookieSerializeOptions} Cookie options
|
|
230
|
-
* ```ts
|
|
231
|
-
* deleteCookie(res, 'SessionId')
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
declare function deleteCookie(event: CompatibilityEvent, name: string, serializeOptions?: CookieSerializeOptions): void;
|
|
235
60
|
|
|
236
|
-
declare
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
declare function
|
|
246
|
-
declare function
|
|
247
|
-
declare const getHeaders: typeof getRequestHeaders;
|
|
248
|
-
declare function getRequestHeader(event: CompatibilityEvent, name: string): CompatibilityEvent['req']['headers'][string];
|
|
249
|
-
declare const getHeader: typeof getRequestHeader;
|
|
250
|
-
|
|
251
|
-
declare function send(event: CompatibilityEvent, data?: any, type?: string): Promise<void>;
|
|
252
|
-
declare function defaultContentType(event: CompatibilityEvent, type?: string): void;
|
|
253
|
-
declare function sendRedirect(event: CompatibilityEvent, location: string, code?: number): Promise<void>;
|
|
254
|
-
declare function getResponseHeaders(event: CompatibilityEvent): ReturnType<CompatibilityEvent['res']['getHeaders']>;
|
|
255
|
-
declare function getResponseHeader(event: CompatibilityEvent, name: string): ReturnType<CompatibilityEvent['res']['getHeader']>;
|
|
256
|
-
declare function setResponseHeaders(event: CompatibilityEvent, headers: Record<string, Parameters<OutgoingMessage['setHeader']>[1]>): void;
|
|
257
|
-
declare const setHeaders: typeof setResponseHeaders;
|
|
258
|
-
declare function setResponseHeader(event: CompatibilityEvent, name: string, value: Parameters<OutgoingMessage['setHeader']>[1]): void;
|
|
259
|
-
declare const setHeader: typeof setResponseHeader;
|
|
260
|
-
declare function appendResponseHeaders(event: CompatibilityEvent, headers: Record<string, string>): void;
|
|
261
|
-
declare const appendHeaders: typeof appendResponseHeaders;
|
|
262
|
-
declare function appendResponseHeader(event: CompatibilityEvent, name: string, value: string): void;
|
|
263
|
-
declare const appendHeader: typeof appendResponseHeader;
|
|
264
|
-
declare function isStream(data: any): any;
|
|
265
|
-
declare function sendStream(event: CompatibilityEvent, data: any): Promise<void>;
|
|
61
|
+
declare class H3Event implements Pick<FetchEvent, 'respondWith'> {
|
|
62
|
+
'__is_event__': boolean;
|
|
63
|
+
req: IncomingMessage;
|
|
64
|
+
res: ServerResponse;
|
|
65
|
+
event: H3Event;
|
|
66
|
+
context: H3EventContext;
|
|
67
|
+
constructor(req: http.IncomingMessage | IncomingMessage, res: http.ServerResponse | ServerResponse);
|
|
68
|
+
respondWith(r: H3Response | PromiseLike<H3Response>): void;
|
|
69
|
+
}
|
|
70
|
+
declare function isEvent(input: any): input is H3Event;
|
|
71
|
+
declare function createEvent(req: http.IncomingMessage, res: http.ServerResponse): H3Event;
|
|
266
72
|
|
|
267
|
-
declare
|
|
268
|
-
declare
|
|
269
|
-
declare
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
handler: EventHandler;
|
|
73
|
+
declare function defineEventHandler<T = any>(handler: EventHandler<T>): EventHandler<T>;
|
|
74
|
+
declare const eventHandler: typeof defineEventHandler;
|
|
75
|
+
declare function isEventHandler(input: any): input is EventHandler;
|
|
76
|
+
declare function toEventHandler(handler: CompatibilityEventHandler): EventHandler;
|
|
77
|
+
interface DynamicEventHandler extends EventHandler {
|
|
78
|
+
set: (handler: EventHandler) => void;
|
|
274
79
|
}
|
|
275
|
-
declare function
|
|
80
|
+
declare function dynamicEventHandler(initial?: EventHandler): DynamicEventHandler;
|
|
81
|
+
declare function defineLazyEventHandler(factory: LazyEventHandler): EventHandler;
|
|
82
|
+
declare const lazyEventHandler: typeof defineLazyEventHandler;
|
|
276
83
|
|
|
277
|
-
export {
|
|
84
|
+
export { DynamicEventHandler, H3Event, H3Headers, H3Response, createEvent, defineEventHandler, defineLazyEventHandler, dynamicEventHandler, eventHandler, isEvent, isEventHandler, lazyEventHandler, toEventHandler };
|