@wooksjs/event-http 0.7.4 → 0.7.5

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
@@ -2,8 +2,8 @@ import * as _wooksjs_event_core from '@wooksjs/event-core';
2
2
  import { EventContext, Logger, EventContextOptions, EventKindSeeds } from '@wooksjs/event-core';
3
3
  export { EventContext, EventContextOptions, useLogger, useRouteParams } from '@wooksjs/event-core';
4
4
  import * as http from 'http';
5
- import { IncomingHttpHeaders, IncomingMessage, ServerResponse, Server } from 'http';
6
- import { Buffer as Buffer$1 } from 'buffer';
5
+ import http__default, { IncomingHttpHeaders, IncomingMessage, ServerResponse, Server } from 'http';
6
+ import { Buffer } from 'buffer';
7
7
  import { URLSearchParams } from 'url';
8
8
  import * as wooks from 'wooks';
9
9
  import { TWooksHandler, TWooksOptions, WooksAdapterBase, Wooks, WooksUpgradeHandler } from 'wooks';
@@ -19,18 +19,18 @@ import { Duplex } from 'stream';
19
19
  * const sessionId = getCookie('session_id')
20
20
  * ```
21
21
  */
22
- declare const useCookies: (ctx?: EventContext) => {
22
+ declare const useCookies: _wooksjs_event_core.WookComposable<{
23
23
  raw: string | undefined;
24
24
  getCookie: (name: string) => string | null;
25
- };
25
+ }>;
26
26
 
27
27
  /** Short names for common Accept MIME types. */
28
28
  type KnownAcceptType = 'json' | 'html' | 'xml' | 'text';
29
29
  /** Provides helpers to check the request's Accept header for supported MIME types. */
30
- declare const useAccept: (ctx?: EventContext) => {
30
+ declare const useAccept: _wooksjs_event_core.WookComposable<{
31
31
  accept: string | undefined;
32
32
  has: (type: KnownAcceptType | (string & {})) => boolean;
33
- };
33
+ }>;
34
34
 
35
35
  /** Short names for common Authorization schemes. */
36
36
  type KnownAuthType = 'basic' | 'bearer';
@@ -42,7 +42,7 @@ type KnownAuthType = 'basic' | 'bearer';
42
42
  * if (is('bearer')) { const token = credentials() }
43
43
  * ```
44
44
  */
45
- declare const useAuthorization: (ctx?: EventContext) => {
45
+ declare const useAuthorization: _wooksjs_event_core.WookComposable<{
46
46
  authorization: string | undefined;
47
47
  type: () => string | null;
48
48
  credentials: () => string | null;
@@ -51,7 +51,7 @@ declare const useAuthorization: (ctx?: EventContext) => {
51
51
  username: string;
52
52
  password: string;
53
53
  } | null;
54
- };
54
+ }>;
55
55
 
56
56
  /**
57
57
  * Returns the incoming request headers.
@@ -70,7 +70,7 @@ declare const DEFAULT_LIMITS: {
70
70
  readonly readTimeoutMs: 10000;
71
71
  };
72
72
  /** @internal Exported for test pre-seeding via `ctx.set(rawBodySlot, ...)`. */
73
- declare const rawBodySlot: _wooksjs_event_core.Cached<Promise<Buffer$1<ArrayBufferLike>>>;
73
+ declare const rawBodySlot: _wooksjs_event_core.Cached<Promise<Buffer<ArrayBufferLike>>>;
74
74
  /**
75
75
  * Provides access to the incoming HTTP request (method, url, headers, body, IP).
76
76
  * @example
@@ -79,12 +79,12 @@ declare const rawBodySlot: _wooksjs_event_core.Cached<Promise<Buffer$1<ArrayBuff
79
79
  * const body = await rawBody()
80
80
  * ```
81
81
  */
82
- declare const useRequest: (ctx?: EventContext) => {
82
+ declare const useRequest: _wooksjs_event_core.WookComposable<{
83
83
  raw: http.IncomingMessage;
84
84
  url: string | undefined;
85
85
  method: string | undefined;
86
86
  headers: http.IncomingHttpHeaders;
87
- rawBody: () => Promise<Buffer$1<ArrayBufferLike>>;
87
+ rawBody: () => Promise<Buffer<ArrayBufferLike>>;
88
88
  reqId: () => string;
89
89
  getIp: (options?: {
90
90
  trustProxy: boolean;
@@ -102,7 +102,7 @@ declare const useRequest: (ctx?: EventContext) => {
102
102
  setMaxInflated: (limit: number) => void;
103
103
  getMaxRatio: () => number;
104
104
  setMaxRatio: (limit: number) => void;
105
- };
105
+ }>;
106
106
 
107
107
  /** Maps numeric HTTP status codes to their human-readable descriptions. */
108
108
  declare const httpStatusCodes: {
@@ -344,13 +344,14 @@ declare class HttpResponse {
344
344
  protected readonly _res: ServerResponse;
345
345
  protected readonly _req: IncomingMessage;
346
346
  protected readonly _logger: Logger;
347
+ protected readonly _captureMode: boolean;
347
348
  /**
348
349
  * @param _res - The underlying Node.js `ServerResponse`.
349
350
  * @param _req - The underlying Node.js `IncomingMessage`.
350
351
  * @param _logger - Logger instance for error reporting.
351
352
  * @param defaultHeaders - Optional headers to pre-populate on this response (e.g. from `securityHeaders()`).
352
353
  */
353
- constructor(_res: ServerResponse, _req: IncomingMessage, _logger: Logger, defaultHeaders?: Record<string, string | string[]>);
354
+ constructor(_res: ServerResponse, _req: IncomingMessage, _logger: Logger, defaultHeaders?: Record<string, string | string[]>, _captureMode?: boolean);
354
355
  protected _status: EHttpStatusCode;
355
356
  protected _body: unknown;
356
357
  protected _headers: Record<string, string | string[]>;
@@ -409,6 +410,14 @@ declare class HttpResponse {
409
410
  getRawRes(passthrough?: boolean): ServerResponse;
410
411
  /** Whether the response has already been sent (or the underlying stream is no longer writable). */
411
412
  get responded(): boolean;
413
+ /**
414
+ * Builds a Web Standard `Response` from the accumulated response state
415
+ * (status, headers, cookies, body) without writing to the underlying `ServerResponse`.
416
+ *
417
+ * Used by `WooksHttp.fetch()` for programmatic invocation.
418
+ */
419
+ toWebResponse(): Response;
420
+ private _buildWebHeaders;
412
421
  protected renderBody(): string | Uint8Array;
413
422
  protected renderError(data: TWooksErrorBodyExt, _ctx: EventContext): void;
414
423
  /** Renders and sends an HTTP error response. Called automatically by the framework when a handler throws an `HttpError`. */
@@ -428,6 +437,8 @@ declare class HttpResponse {
428
437
  private sendFetchResponse;
429
438
  private sendRegular;
430
439
  }
440
+ /** Converts a Record of headers to a Web Standard `Headers` object. */
441
+ declare function recordToWebHeaders(record: Record<string, string | string[]>): Headers;
431
442
 
432
443
  /**
433
444
  * Returns the HttpResponse instance for the current request.
@@ -463,11 +474,11 @@ declare class WooksURLSearchParams extends URLSearchParams {
463
474
  * const page = params().get('page')
464
475
  * ```
465
476
  */
466
- declare const useUrlParams: (ctx?: EventContext) => {
477
+ declare const useUrlParams: _wooksjs_event_core.WookComposable<{
467
478
  raw: () => string;
468
479
  params: () => WooksURLSearchParams;
469
480
  toJson: () => unknown;
470
- };
481
+ }>;
471
482
 
472
483
  /** Event kind definition for HTTP requests. Provides typed context slots for `req`, `response`, and `requestLimits`. */
473
484
  declare const httpKind: _wooksjs_event_core.EventKind<{
@@ -509,6 +520,12 @@ interface TWooksHttpOptions {
509
520
  responseClass?: typeof WooksHttpResponse;
510
521
  /** Default headers applied to every response. Use `securityHeaders()` for recommended security headers. */
511
522
  defaultHeaders?: Record<string, string | string[]>;
523
+ /**
524
+ * Headers forwarded from the calling HTTP context during programmatic `fetch()`.
525
+ * Set to `false` to disable forwarding entirely.
526
+ * @default ['authorization', 'cookie', 'accept-language', 'x-forwarded-for', 'x-request-id']
527
+ */
528
+ forwardHeaders?: string[] | false;
512
529
  }
513
530
  /** HTTP adapter for Wooks that provides route registration, server lifecycle, and request handling. */
514
531
  declare class WooksHttp extends WooksAdapterBase {
@@ -564,7 +581,7 @@ declare class WooksHttp extends WooksAdapterBase {
564
581
  * See attachServer method docs
565
582
  * @returns Server
566
583
  */
567
- getServer(): Server<typeof IncomingMessage, typeof ServerResponse> | undefined;
584
+ getServer(): http__default.Server<typeof http__default.IncomingMessage, typeof http__default.ServerResponse> | undefined;
568
585
  /**
569
586
  * Attaches http(s) server instance
570
587
  * to Wooks.
@@ -595,6 +612,29 @@ declare class WooksHttp extends WooksAdapterBase {
595
612
  protected processUpgradeHandlers(handlers: TWooksHandler[], ctx: EventContext, socket: Duplex): void | Promise<unknown>;
596
613
  protected processHandlers(handlers: TWooksHandler[], ctx: EventContext, response: HttpResponse): void | Promise<unknown>;
597
614
  private processAsyncResult;
615
+ /**
616
+ * Programmatic route invocation using the Web Standard fetch API.
617
+ * Goes through the full dispatch pipeline: context creation, route matching,
618
+ * handler execution, response finalization.
619
+ *
620
+ * When called from within an existing HTTP context (e.g. during SSR),
621
+ * identity headers (authorization, cookie) are automatically forwarded
622
+ * from the calling request unless already present on the given Request.
623
+ *
624
+ * @param request - A Web Standard Request object.
625
+ * @returns A Web Standard Response.
626
+ */
627
+ fetch(request: Request): Promise<Response>;
628
+ /**
629
+ * Convenience wrapper for programmatic route invocation.
630
+ * Accepts a URL string (relative paths auto-prefixed with `http://localhost`),
631
+ * URL object, or Request, plus optional `RequestInit`.
632
+ *
633
+ * @param input - URL string, URL object, or Request.
634
+ * @param init - Optional RequestInit (method, headers, body, etc.).
635
+ * @returns A Web Standard Response.
636
+ */
637
+ request(input: string | URL | Request, init?: RequestInit): Promise<Response>;
598
638
  }
599
639
  /**
600
640
  * Creates a new WooksHttp application instance.
@@ -650,7 +690,7 @@ interface TTestHttpContext {
650
690
  /** Custom request body limits. */
651
691
  requestLimits?: TRequestLimits;
652
692
  /** Pre-seed the raw body for body-parsing tests. */
653
- rawBody?: string | Buffer$1;
693
+ rawBody?: string | Buffer;
654
694
  /** Default headers to pre-populate on the response (e.g. from `securityHeaders()`). */
655
695
  defaultHeaders?: Record<string, string | string[]>;
656
696
  }
@@ -671,5 +711,5 @@ interface TTestHttpContext {
671
711
  */
672
712
  declare function prepareTestHttpContext(options: TTestHttpContext): <T>(cb: (...a: any[]) => T) => T;
673
713
 
674
- export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams };
714
+ export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, recordToWebHeaders, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams };
675
715
  export type { KnownAcceptType, KnownAuthType, SecurityHeadersOptions, TCacheControl, TCookieAttributes, TCookieAttributesInput, THttpEventData, TRequestLimits, TSetCookieData, TTestHttpContext, TWooksErrorBody, TWooksErrorBodyExt, TWooksHttpOptions };