@wooksjs/event-http 0.6.2 → 0.6.4

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
@@ -155,6 +155,7 @@ declare class WooksURLSearchParams extends URLSearchParams {
155
155
  interface THttpEventData {
156
156
  req: IncomingMessage;
157
157
  res: ServerResponse;
158
+ requestLimits?: TRequestLimits;
158
159
  }
159
160
  interface THttpEvent {
160
161
  type: 'HTTP';
@@ -196,6 +197,19 @@ interface TAuthCache {
196
197
  password: string;
197
198
  } | null;
198
199
  }
200
+ /** App-level request body limits (all optional, defaults apply when omitted). */
201
+ interface TRequestLimits {
202
+ /** Max compressed body size in bytes (default: 1 MB). */
203
+ maxCompressed?: number;
204
+ /** Max inflated (decompressed) body size in bytes (default: 10 MB). */
205
+ maxInflated?: number;
206
+ /** Max compression ratio, e.g. 100 means 100× expansion (default: 100). */
207
+ maxRatio?: number;
208
+ /** Body read timeout in milliseconds (default: 10 000). */
209
+ readTimeoutMs?: number;
210
+ /** Internal flag: true when this object is a per-request clone (copy-on-write). */
211
+ perRequest?: boolean;
212
+ }
199
213
  interface TRequestCache {
200
214
  rawBody: Promise<Buffer>;
201
215
  parsed: unknown;
@@ -207,20 +221,25 @@ interface TRequestCache {
207
221
  };
208
222
  contentEncodings?: string[];
209
223
  isCompressed?: boolean;
210
- maxCompressed?: number;
211
- maxInflated?: number;
212
- maxRatio?: number;
213
- readTimeoutMs?: number;
214
224
  }
215
225
  interface TSearchParamsCache {
216
226
  raw?: string;
217
227
  urlSearchParams?: WooksURLSearchParams;
218
228
  }
219
229
 
230
+ /**
231
+ * Provides access to parsed request cookies.
232
+ * @example
233
+ * ```ts
234
+ * const { getCookie, rawCookies } = useCookies()
235
+ * const sessionId = getCookie('session_id')
236
+ * ```
237
+ */
220
238
  declare function useCookies(): {
221
239
  rawCookies: string | undefined;
222
240
  getCookie: (name: string) => string | null;
223
241
  };
242
+ /** Provides methods to set, get, remove, and clear outgoing response cookies. */
224
243
  declare function useSetCookies(): {
225
244
  setCookie: (name: string, value: string, attrs?: Partial<TCookieAttributes>) => void;
226
245
  getCookie: <K2 extends string>(key2: K2) => TSetCookieData | undefined;
@@ -228,12 +247,15 @@ declare function useSetCookies(): {
228
247
  clearCookies: () => void;
229
248
  cookies: () => string[];
230
249
  };
250
+ /** Returns a hookable accessor for a single outgoing cookie by name. */
231
251
  declare function useSetCookie(name: string): {
232
252
  name: string;
233
253
  type: string;
234
254
  } & _wooksjs_event_core.THook<string, "value"> & _wooksjs_event_core.THook<TCookieAttributes, "attrs">;
255
+ /** Hook type returned by {@link useSetCookie}. */
235
256
  type TCookieHook = ReturnType<typeof useSetCookie>;
236
257
 
258
+ /** Provides helpers to check the request's Accept header for supported MIME types. */
237
259
  declare function useAccept(): {
238
260
  accept: string | undefined;
239
261
  accepts: (mime: string) => unknown;
@@ -243,6 +265,14 @@ declare function useAccept(): {
243
265
  acceptsHtml: () => unknown;
244
266
  };
245
267
 
268
+ /**
269
+ * Provides parsed access to the Authorization header (type, credentials, Basic decoding).
270
+ * @example
271
+ * ```ts
272
+ * const { isBearer, authRawCredentials, basicCredentials } = useAuthorization()
273
+ * if (isBearer()) { const token = authRawCredentials() }
274
+ * ```
275
+ */
246
276
  declare function useAuthorization(): {
247
277
  authorization: string | undefined;
248
278
  authType: () => string | null;
@@ -268,6 +298,7 @@ interface TCacheControl {
268
298
  }
269
299
  declare function renderCacheControl(data: TCacheControl): string;
270
300
 
301
+ /** Provides helpers to set cache-related response headers (Cache-Control, Expires, Age, Pragma). */
271
302
  declare function useSetCacheControl(): {
272
303
  setExpires: (value: Date | string | number) => void;
273
304
  setAge: (value: number | TTimeMultiString) => void;
@@ -275,7 +306,22 @@ declare function useSetCacheControl(): {
275
306
  setCacheControl: (data: TCacheControl) => void;
276
307
  };
277
308
 
309
+ /**
310
+ * Returns the incoming request headers.
311
+ * @example
312
+ * ```ts
313
+ * const { host, authorization } = useHeaders()
314
+ * ```
315
+ */
278
316
  declare function useHeaders(): IncomingHttpHeaders;
317
+ /**
318
+ * Provides methods to set, get, and remove outgoing response headers.
319
+ * @example
320
+ * ```ts
321
+ * const { setHeader, setContentType, enableCors } = useSetHeaders()
322
+ * setHeader('x-request-id', '123')
323
+ * ```
324
+ */
279
325
  declare function useSetHeaders(): {
280
326
  setHeader: (name: string, value: string | number) => void;
281
327
  getHeader: <K2 extends string>(key2: K2) => string | string[] | undefined;
@@ -284,18 +330,29 @@ declare function useSetHeaders(): {
284
330
  headers: () => Record<string, string | string[]>;
285
331
  enableCors: (origin?: string) => void;
286
332
  };
333
+ /** Returns a hookable accessor for a single outgoing response header by name. */
287
334
  declare function useSetHeader(name: string): {
288
335
  value: string | string[];
289
336
  isDefined: boolean;
290
337
  };
338
+ /** Hook type returned by {@link useSetHeader}. */
291
339
  type THeaderHook = ReturnType<typeof useSetHeader>;
292
340
 
341
+ /** Default safety limits for request body reading (size, ratio, timeout). */
293
342
  declare const DEFAULT_LIMITS: {
294
343
  readonly maxCompressed: number;
295
344
  readonly maxInflated: number;
296
345
  readonly maxRatio: 100;
297
346
  readonly readTimeoutMs: 10000;
298
347
  };
348
+ /**
349
+ * Provides access to the incoming HTTP request (method, url, headers, body, IP).
350
+ * @example
351
+ * ```ts
352
+ * const { method, url, rawBody, getIp } = useRequest()
353
+ * const body = await rawBody()
354
+ * ```
355
+ */
299
356
  declare function useRequest(): {
300
357
  rawRequest: http.IncomingMessage;
301
358
  url: string | undefined;
@@ -312,27 +369,47 @@ declare function useRequest(): {
312
369
  };
313
370
  isCompressed: () => boolean;
314
371
  getMaxCompressed: () => number;
315
- setMaxCompressed: (limit: number) => unknown;
372
+ setMaxCompressed: (limit: number) => void;
316
373
  getReadTimeoutMs: () => number;
317
- setReadTimeoutMs: (limit: number) => unknown;
374
+ setReadTimeoutMs: (limit: number) => void;
318
375
  getMaxInflated: () => number;
319
- setMaxInflated: (limit: number) => unknown;
376
+ setMaxInflated: (limit: number) => void;
377
+ getMaxRatio: () => number;
378
+ setMaxRatio: (limit: number) => void;
320
379
  };
321
380
 
322
381
  interface TUseResponseOptions {
323
382
  passthrough: boolean;
324
383
  }
384
+ /**
385
+ * Provides access to the raw HTTP response and status code management.
386
+ * @example
387
+ * ```ts
388
+ * const { status, rawResponse, hasResponded } = useResponse()
389
+ * status(200)
390
+ * ```
391
+ */
325
392
  declare function useResponse(): {
326
393
  rawResponse: (options?: TUseResponseOptions) => http.ServerResponse<http.IncomingMessage>;
327
394
  hasResponded: () => boolean;
328
395
  status: ((code?: EHttpStatusCode) => EHttpStatusCode) & _wooksjs_event_core.THook<EHttpStatusCode, "value">;
329
396
  };
397
+ /** Returns a hookable accessor for the response status code. */
330
398
  declare function useStatus(): {
331
399
  value: EHttpStatusCode;
332
400
  isDefined: boolean;
333
401
  };
402
+ /** Hook type returned by {@link useStatus}. */
334
403
  type TStatusHook = ReturnType<typeof useStatus>;
335
404
 
405
+ /**
406
+ * Provides access to URL search (query) parameters from the request.
407
+ * @example
408
+ * ```ts
409
+ * const { urlSearchParams, jsonSearchParams } = useSearchParams()
410
+ * const page = urlSearchParams().get('page')
411
+ * ```
412
+ */
336
413
  declare function useSearchParams(): {
337
414
  rawSearchParams: () => string;
338
415
  urlSearchParams: () => WooksURLSearchParams;
@@ -374,6 +451,7 @@ declare class BaseHttpResponse<BodyType = unknown> {
374
451
  respond(): Promise<unknown>;
375
452
  }
376
453
 
454
+ /** Represents an HTTP error with a status code and optional structured body. */
377
455
  declare class HttpError<T extends TWooksErrorBody = TWooksErrorBody> extends Error {
378
456
  protected code: THttpErrorCodes;
379
457
  protected _body: string | T;
@@ -384,15 +462,18 @@ declare class HttpError<T extends TWooksErrorBody = TWooksErrorBody> extends Err
384
462
  attachRenderer(renderer: HttpErrorRenderer): void;
385
463
  getRenderer(): HttpErrorRenderer | undefined;
386
464
  }
465
+ /** Base shape for an HTTP error response body. */
387
466
  interface TWooksErrorBody {
388
467
  message: string;
389
468
  statusCode: EHttpStatusCode;
390
469
  error?: string;
391
470
  }
471
+ /** Extended error body that always includes the error description string. */
392
472
  interface TWooksErrorBodyExt extends TWooksErrorBody {
393
473
  error: string;
394
474
  }
395
475
 
476
+ /** Renders HTTP error responses in HTML, JSON, or plain text based on the Accept header. */
396
477
  declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBodyExt> {
397
478
  protected opts?: {
398
479
  version: string;
@@ -424,6 +505,7 @@ declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBody
424
505
  render(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
425
506
  }
426
507
 
508
+ /** Creates an async event context for an incoming HTTP request/response pair. */
427
509
  declare function createHttpContext(data: THttpEventData, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
428
510
  /**
429
511
  * Wrapper on useEventContext with HTTP event types
@@ -436,23 +518,35 @@ declare function createWooksResponder(renderer?: TWooksResponseRenderer<any>, er
436
518
  respond: (data: unknown) => Promise<unknown> | undefined;
437
519
  };
438
520
 
521
+ /** Configuration options for the WooksHttp adapter. */
439
522
  interface TWooksHttpOptions {
440
523
  logger?: TConsoleBase;
441
524
  eventOptions?: TEventOptions;
442
525
  onNotFound?: TWooksHandler;
443
526
  router?: TWooksOptions['router'];
527
+ /** Default request body limits applied to every request (overridable per-request via `useRequest()`). */
528
+ requestLimits?: Omit<TRequestLimits, 'perRequest'>;
444
529
  }
530
+ /** HTTP adapter for Wooks that provides route registration, server lifecycle, and request handling. */
445
531
  declare class WooksHttp extends WooksAdapterBase {
446
532
  protected opts?: TWooksHttpOptions | undefined;
447
533
  protected logger: TConsoleBase;
448
534
  constructor(opts?: TWooksHttpOptions | undefined, wooks?: Wooks | WooksAdapterBase);
535
+ /** Registers a handler for all HTTP methods on the given path. */
449
536
  all<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
537
+ /** Registers a GET route handler. */
450
538
  get<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
539
+ /** Registers a POST route handler. */
451
540
  post<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
541
+ /** Registers a PUT route handler. */
452
542
  put<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
543
+ /** Registers a PATCH route handler. */
453
544
  patch<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
545
+ /** Registers a DELETE route handler. */
454
546
  delete<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
547
+ /** Registers a HEAD route handler. */
455
548
  head<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
549
+ /** Registers an OPTIONS route handler. */
456
550
  options<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
457
551
  protected server?: Server;
458
552
  /**
@@ -510,11 +604,15 @@ declare class WooksHttp extends WooksAdapterBase {
510
604
  protected processHandlers(handlers: TWooksHandler[]): Promise<unknown>;
511
605
  }
512
606
  /**
513
- * Factory for WooksHttp App
514
- * @param opts TWooksHttpOptions
515
- * @param wooks Wooks | WooksAdapterBase
516
- * @returns WooksHttp
607
+ * Creates a new WooksHttp application instance.
608
+ * @example
609
+ * ```ts
610
+ * const app = createHttpApp()
611
+ * app.get('/hello', () => 'Hello World!')
612
+ * app.listen(3000)
613
+ * ```
517
614
  */
518
615
  declare function createHttpApp(opts?: TWooksHttpOptions, wooks?: Wooks | WooksAdapterBase): WooksHttp;
519
616
 
520
- export { BaseHttpResponse, BaseHttpResponseRenderer, DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpErrorRenderer, type TAuthCache, type TCacheControl, type TCookieAttributes, type TCookieAttributesInput, type TCookieHook, type THeaderHook, type THttpContextStore, type THttpEvent, type THttpEventData, type TRequestCache, type TSearchParamsCache, type TSetCookieData, type TStatusHook, type TWooksErrorBody, type TWooksErrorBodyExt, type TWooksHttpOptions, type TWooksResponseRenderer, WooksHttp, WooksURLSearchParams, createHttpApp, createHttpContext, createWooksResponder, httpStatusCodes, renderCacheControl, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
617
+ export { BaseHttpResponse, BaseHttpResponseRenderer, DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpErrorRenderer, WooksHttp, WooksURLSearchParams, createHttpApp, createHttpContext, createWooksResponder, httpStatusCodes, renderCacheControl, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
618
+ export type { TAuthCache, TCacheControl, TCookieAttributes, TCookieAttributesInput, TCookieHook, THeaderHook, THttpContextStore, THttpEvent, THttpEventData, TRequestCache, TRequestLimits, TSearchParamsCache, TSetCookieData, TStatusHook, TWooksErrorBody, TWooksErrorBodyExt, TWooksHttpOptions, TWooksResponseRenderer };