@wooksjs/event-http 0.4.26 → 0.4.27

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.d.ts CHANGED
@@ -1,92 +1,12 @@
1
- import * as http from 'http';
2
- import http__default, { IncomingHttpHeaders, IncomingMessage, ServerResponse, Server } from 'http';
3
1
  import * as _wooksjs_event_core from '@wooksjs/event-core';
4
2
  import { TEventOptions, TEmpty } from '@wooksjs/event-core';
3
+ import * as http from 'http';
4
+ import { IncomingMessage, ServerResponse, IncomingHttpHeaders, Server } from 'http';
5
5
  import { URLSearchParams } from 'url';
6
6
  import { TConsoleBase } from '@prostojs/logger';
7
7
  import * as _prostojs_router from '@prostojs/router';
8
8
  import { TWooksHandler, TWooksOptions, WooksAdapterBase, Wooks } from 'wooks';
9
9
 
10
- declare function useHeaders(): IncomingHttpHeaders;
11
- declare function useSetHeaders(): {
12
- setHeader: (name: string, value: string | number) => void;
13
- getHeader: <K2 extends string | number>(key2: K2) => {
14
- [name: string]: string | string[];
15
- }[K2];
16
- removeHeader: <K2_1 extends string | number>(key2: K2_1) => void;
17
- setContentType: (value: string) => void;
18
- headers: () => {
19
- [name: string]: string | string[];
20
- };
21
- enableCors: (origin?: string) => void;
22
- };
23
- declare function useSetHeader(name: string): {
24
- value: string | string[];
25
- isDefined: boolean;
26
- };
27
- type THeaderHook = ReturnType<typeof useSetHeader>;
28
-
29
- declare function useAccept(): {
30
- accept: string | undefined;
31
- accepts: (mime: string) => unknown;
32
- acceptsJson: () => unknown;
33
- acceptsXml: () => unknown;
34
- acceptsText: () => unknown;
35
- acceptsHtml: () => unknown;
36
- };
37
-
38
- declare function useAuthorization(): {
39
- authorization: string | undefined;
40
- authType: () => string | null;
41
- authRawCredentials: () => string | null;
42
- isBasic: () => boolean;
43
- isBearer: () => boolean;
44
- basicCredentials: () => {
45
- username: string;
46
- password: string;
47
- } | null;
48
- };
49
-
50
- type TTimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'Y';
51
- type TTimeSingleString = `${number}${TTimeUnit}`;
52
- type TTimeMultiString = `${TTimeSingleString}${TTimeSingleString | ''}${TTimeSingleString | ''}${TTimeSingleString | ''}`;
53
-
54
- type TCacheControl = {
55
- mustRevalidate?: boolean;
56
- noCache?: boolean | string;
57
- noStore?: boolean;
58
- noTransform?: boolean;
59
- public?: boolean;
60
- private?: boolean | string;
61
- proxyRevalidate?: boolean;
62
- maxAge?: number | TTimeMultiString;
63
- sMaxage?: number | TTimeMultiString;
64
- };
65
- declare function renderCacheControl(data: TCacheControl): string;
66
-
67
- declare function useSetCacheControl(): {
68
- setExpires: (value: Date | string | number) => void;
69
- setAge: (value: number | TTimeMultiString) => void;
70
- setPragmaNoCache: (value?: boolean) => void;
71
- setCacheControl: (data: TCacheControl) => void;
72
- };
73
-
74
- declare function useRequest(): {
75
- rawRequest: http.IncomingMessage;
76
- url: string | undefined;
77
- method: string | undefined;
78
- headers: http.IncomingHttpHeaders;
79
- rawBody: () => Promise<Buffer>;
80
- reqId: () => string;
81
- getIp: (options?: {
82
- trustProxy: boolean;
83
- }) => string;
84
- getIpList: () => {
85
- remoteIp: string;
86
- forwarded: string[];
87
- };
88
- };
89
-
90
10
  declare const httpStatusCodes: {
91
11
  100: string;
92
12
  101: string;
@@ -221,19 +141,9 @@ type THttpBadRequestCodes = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408
221
141
  type THttpServerErrorCodes = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
222
142
  type THttpErrorCodes = THttpBadRequestCodes | THttpServerErrorCodes;
223
143
 
224
- type TUseResponseOptions = {
225
- passthrough: boolean;
226
- };
227
- declare function useResponse(): {
228
- rawResponse: (options?: TUseResponseOptions) => http.ServerResponse<http.IncomingMessage>;
229
- hasResponded: () => boolean;
230
- status: ((code?: EHttpStatusCode) => EHttpStatusCode) & _wooksjs_event_core.THook<EHttpStatusCode, "value">;
231
- };
232
- declare function useStatus(): {
233
- value: EHttpStatusCode;
234
- isDefined: boolean;
235
- };
236
- type TStatusHook = ReturnType<typeof useStatus>;
144
+ type TTimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'Y';
145
+ type TTimeSingleString = `${number}${TTimeUnit}`;
146
+ type TTimeMultiString = `${TTimeSingleString}${TTimeSingleString | ''}${TTimeSingleString | ''}${TTimeSingleString | ''}`;
237
147
 
238
148
  declare class WooksURLSearchParams extends URLSearchParams {
239
149
  toJson<T = unknown>(): T;
@@ -248,19 +158,11 @@ interface THttpEvent {
248
158
  }
249
159
  interface THttpContextStore {
250
160
  searchParams?: TSearchParamsCache;
251
- cookies?: {
252
- [name: string]: string | null;
253
- };
254
- setCookies?: {
255
- [name: string]: TSetCookieData;
256
- };
257
- accept?: {
258
- [name: string]: boolean;
259
- };
161
+ cookies?: Record<string, string | null>;
162
+ setCookies?: Record<string, TSetCookieData>;
163
+ accept?: Record<string, boolean>;
260
164
  authorization?: TAuthCache;
261
- setHeader?: {
262
- [name: string]: string | string[];
263
- };
165
+ setHeader?: Record<string, string | string[]>;
264
166
  request?: TRequestCache;
265
167
  response?: {
266
168
  responded: boolean;
@@ -269,10 +171,10 @@ interface THttpContextStore {
269
171
  code: EHttpStatusCode;
270
172
  };
271
173
  }
272
- type TSetCookieData = {
174
+ interface TSetCookieData {
273
175
  value: string;
274
176
  attrs: TCookieAttributesInput;
275
- };
177
+ }
276
178
  type TCookieAttributesInput = Partial<TCookieAttributes>;
277
179
  interface TCookieAttributes {
278
180
  expires: Date | string | number;
@@ -283,15 +185,15 @@ interface TCookieAttributes {
283
185
  httpOnly: boolean;
284
186
  sameSite: boolean | 'Lax' | 'None' | 'Strict';
285
187
  }
286
- type TAuthCache = {
188
+ interface TAuthCache {
287
189
  type: string | null;
288
190
  credentials: string | null;
289
191
  basicCredentials: {
290
192
  username: string;
291
193
  password: string;
292
194
  } | null;
293
- };
294
- type TRequestCache = {
195
+ }
196
+ interface TRequestCache {
295
197
  rawBody: Promise<Buffer>;
296
198
  parsed: unknown;
297
199
  forwardedIp?: string;
@@ -300,11 +202,11 @@ type TRequestCache = {
300
202
  remoteIp: string;
301
203
  forwarded: string[];
302
204
  };
303
- };
304
- type TSearchParamsCache = {
205
+ }
206
+ interface TSearchParamsCache {
305
207
  raw?: string;
306
208
  urlSearchParams?: WooksURLSearchParams;
307
- };
209
+ }
308
210
 
309
211
  declare function useCookies(): {
310
212
  rawCookies: string | undefined;
@@ -312,10 +214,8 @@ declare function useCookies(): {
312
214
  };
313
215
  declare function useSetCookies(): {
314
216
  setCookie: (name: string, value: string, attrs?: Partial<TCookieAttributes>) => void;
315
- getCookie: <K2 extends string | number>(key2: K2) => {
316
- [name: string]: TSetCookieData;
317
- }[K2];
318
- removeCookie: <K2_1 extends string | number>(key2: K2_1) => void;
217
+ getCookie: <K2 extends string>(key2: K2) => TSetCookieData;
218
+ removeCookie: <K2_1 extends string>(key2: K2_1) => void;
319
219
  clearCookies: () => void;
320
220
  cookies: () => string[];
321
221
  };
@@ -325,6 +225,92 @@ declare function useSetCookie(name: string): {
325
225
  } & _wooksjs_event_core.THook<string, "value"> & _wooksjs_event_core.THook<TCookieAttributes, "attrs">;
326
226
  type TCookieHook = ReturnType<typeof useSetCookie>;
327
227
 
228
+ declare function useAccept(): {
229
+ accept: string | undefined;
230
+ accepts: (mime: string) => unknown;
231
+ acceptsJson: () => unknown;
232
+ acceptsXml: () => unknown;
233
+ acceptsText: () => unknown;
234
+ acceptsHtml: () => unknown;
235
+ };
236
+
237
+ declare function useAuthorization(): {
238
+ authorization: string | undefined;
239
+ authType: () => string | null;
240
+ authRawCredentials: () => string | null;
241
+ isBasic: () => boolean;
242
+ isBearer: () => boolean;
243
+ basicCredentials: () => {
244
+ username: string;
245
+ password: string;
246
+ } | null;
247
+ };
248
+
249
+ interface TCacheControl {
250
+ mustRevalidate?: boolean;
251
+ noCache?: boolean | string;
252
+ noStore?: boolean;
253
+ noTransform?: boolean;
254
+ public?: boolean;
255
+ private?: boolean | string;
256
+ proxyRevalidate?: boolean;
257
+ maxAge?: number | TTimeMultiString;
258
+ sMaxage?: number | TTimeMultiString;
259
+ }
260
+ declare function renderCacheControl(data: TCacheControl): string;
261
+
262
+ declare function useSetCacheControl(): {
263
+ setExpires: (value: Date | string | number) => void;
264
+ setAge: (value: number | TTimeMultiString) => void;
265
+ setPragmaNoCache: (value?: boolean) => void;
266
+ setCacheControl: (data: TCacheControl) => void;
267
+ };
268
+
269
+ declare function useHeaders(): IncomingHttpHeaders;
270
+ declare function useSetHeaders(): {
271
+ setHeader: (name: string, value: string | number) => void;
272
+ getHeader: <K2 extends string>(key2: K2) => string | string[];
273
+ removeHeader: <K2_1 extends string>(key2: K2_1) => void;
274
+ setContentType: (value: string) => void;
275
+ headers: () => Record<string, string | string[]>;
276
+ enableCors: (origin?: string) => void;
277
+ };
278
+ declare function useSetHeader(name: string): {
279
+ value: string | string[];
280
+ isDefined: boolean;
281
+ };
282
+ type THeaderHook = ReturnType<typeof useSetHeader>;
283
+
284
+ declare function useRequest(): {
285
+ rawRequest: http.IncomingMessage;
286
+ url: string | undefined;
287
+ method: string | undefined;
288
+ headers: http.IncomingHttpHeaders;
289
+ rawBody: () => Promise<Buffer>;
290
+ reqId: () => string;
291
+ getIp: (options?: {
292
+ trustProxy: boolean;
293
+ }) => string;
294
+ getIpList: () => {
295
+ remoteIp: string;
296
+ forwarded: string[];
297
+ };
298
+ };
299
+
300
+ interface TUseResponseOptions {
301
+ passthrough: boolean;
302
+ }
303
+ declare function useResponse(): {
304
+ rawResponse: (options?: TUseResponseOptions) => http.ServerResponse<http.IncomingMessage>;
305
+ hasResponded: () => boolean;
306
+ status: ((code?: EHttpStatusCode) => EHttpStatusCode) & _wooksjs_event_core.THook<EHttpStatusCode, "value">;
307
+ };
308
+ declare function useStatus(): {
309
+ value: EHttpStatusCode;
310
+ isDefined: boolean;
311
+ };
312
+ type TStatusHook = ReturnType<typeof useStatus>;
313
+
328
314
  declare function useSearchParams(): {
329
315
  rawSearchParams: () => string;
330
316
  urlSearchParams: () => WooksURLSearchParams;
@@ -366,16 +352,10 @@ declare class BaseHttpResponse<BodyType = unknown> {
366
352
  respond(): Promise<unknown>;
367
353
  }
368
354
 
369
- declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBodyExt> {
370
- renderHtml(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
371
- renderText(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
372
- renderJson(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
373
- render(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
374
- }
375
-
376
355
  declare class HttpError<T extends TWooksErrorBody = TWooksErrorBody> extends Error {
377
356
  protected code: THttpErrorCodes;
378
357
  protected _body: string | T;
358
+ name: string;
379
359
  constructor(code?: THttpErrorCodes, _body?: string | T);
380
360
  get body(): TWooksErrorBodyExt;
381
361
  protected renderer?: HttpErrorRenderer;
@@ -391,17 +371,19 @@ interface TWooksErrorBodyExt extends TWooksErrorBody {
391
371
  error: string;
392
372
  }
393
373
 
394
- declare function createWooksResponder(renderer?: TWooksResponseRenderer<any>, errorRenderer?: TWooksResponseRenderer<any>): {
395
- createResponse: <T = unknown>(data: T) => BaseHttpResponse<TWooksErrorBodyExt | T> | null;
396
- respond: (data: unknown) => Promise<unknown> | undefined;
397
- };
374
+ declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBodyExt> {
375
+ renderHtml(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
376
+ renderText(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
377
+ renderJson(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
378
+ render(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
379
+ }
398
380
 
399
381
  declare function createHttpContext(data: THttpEventData, options: TEventOptions): {
400
382
  getCtx: () => THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>;
401
383
  restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
402
384
  clearCtx: () => null;
403
385
  store: <K extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData>>(key: K) => {
404
- value: (THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K];
386
+ value: (THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K] | undefined;
405
387
  hook: <K2 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2) => {
406
388
  value: Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K][K2];
407
389
  isDefined: boolean;
@@ -426,7 +408,7 @@ declare function useHttpContext<T extends TEmpty>(): {
426
408
  restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
427
409
  clearCtx: () => null;
428
410
  store: <K extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData> | keyof T>(key: K) => {
429
- value: (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K];
411
+ value: (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K] | undefined;
430
412
  hook: <K2 extends keyof Required<THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2) => {
431
413
  value: Required<THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K][K2];
432
414
  isDefined: boolean;
@@ -443,10 +425,15 @@ declare function useHttpContext<T extends TEmpty>(): {
443
425
  setStore: <K_2 extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData> | keyof T>(key: K_2, v: (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K_2]) => void;
444
426
  };
445
427
 
428
+ declare function createWooksResponder(renderer?: TWooksResponseRenderer<any>, errorRenderer?: TWooksResponseRenderer<any>): {
429
+ createResponse: <T = unknown>(data: T) => BaseHttpResponse<TWooksErrorBodyExt | T> | null;
430
+ respond: (data: unknown) => Promise<unknown>;
431
+ };
432
+
446
433
  interface TWooksHttpOptions {
447
434
  logger?: TConsoleBase;
448
435
  eventOptions?: TEventOptions;
449
- onNotFound?: TWooksHandler<unknown>;
436
+ onNotFound?: TWooksHandler;
450
437
  router?: TWooksOptions['router'];
451
438
  }
452
439
  declare class WooksHttp extends WooksAdapterBase {
@@ -479,7 +466,7 @@ declare class WooksHttp extends WooksAdapterBase {
479
466
  * See attachServer method docs
480
467
  * @returns Server
481
468
  */
482
- getServer(): http__default.Server<typeof http__default.IncomingMessage, typeof http__default.ServerResponse> | undefined;
469
+ getServer(): Server<typeof IncomingMessage, typeof ServerResponse> | undefined;
483
470
  /**
484
471
  * Attaches http(s) server instance
485
472
  * to Wooks.
@@ -490,7 +477,7 @@ declare class WooksHttp extends WooksAdapterBase {
490
477
  attachServer(server?: Server): void;
491
478
  protected responder: {
492
479
  createResponse: <T = unknown>(data: T) => BaseHttpResponse<TWooksErrorBodyExt | T> | null;
493
- respond: (data: unknown) => Promise<unknown> | undefined;
480
+ respond: (data: unknown) => Promise<unknown>;
494
481
  };
495
482
  protected respond(data: unknown): void;
496
483
  /**
@@ -506,7 +493,7 @@ declare class WooksHttp extends WooksAdapterBase {
506
493
  * ```
507
494
  */
508
495
  getServerCb(): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
509
- protected processHandlers(handlers: TWooksHandler<unknown>[]): Promise<void>;
496
+ protected processHandlers(handlers: TWooksHandler[]): Promise<void>;
510
497
  }
511
498
  /**
512
499
  * Factory for WooksHttp App