@h3ravel/shared 0.24.0 → 0.25.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/dist/index.cjs CHANGED
@@ -44,6 +44,7 @@ preferred_pm = __toESM(preferred_pm);
44
44
  * Encapsulates the application instance, request, and response objects.
45
45
  */
46
46
  var HttpContext = class HttpContext {
47
+ static contexts = /* @__PURE__ */ new WeakMap();
47
48
  constructor(app, request, response) {
48
49
  this.app = app;
49
50
  this.request = request;
@@ -54,11 +55,23 @@ var HttpContext = class HttpContext {
54
55
  * @param ctx - Object containing app, request, and response
55
56
  * @returns A new HttpContext instance
56
57
  */
57
- static init(ctx) {
58
- /**
59
- * Return a new instance
60
- */
61
- return new HttpContext(ctx.app, ctx.request, ctx.response);
58
+ static init(ctx, event) {
59
+ if (event && HttpContext.contexts.has(event)) return HttpContext.contexts.get(event);
60
+ const instance = new HttpContext(ctx.app, ctx.request, ctx.response);
61
+ if (event) HttpContext.contexts.set(event, instance);
62
+ return instance;
63
+ }
64
+ /**
65
+ * Retrieve an existing HttpContext instance for an event, if any.
66
+ */
67
+ static get(event) {
68
+ return HttpContext.contexts.get(event);
69
+ }
70
+ /**
71
+ * Delete the cached context for a given event (optional cleanup).
72
+ */
73
+ static forget(event) {
74
+ HttpContext.contexts.delete(event);
62
75
  }
63
76
  };
64
77
 
package/dist/index.d.cts CHANGED
@@ -1,7 +1,6 @@
1
1
  /// <reference path="./app.globals.d.ts" />
2
- import { H3, H3Event, Middleware, MiddlewareOptions, serve } from "h3";
2
+ import { H3, H3Event, HTTPResponse, Middleware, MiddlewareOptions, serve } from "h3";
3
3
  import { Edge } from "edge.js";
4
- import { ResponseHeaderMap, TypedHeaders } from "fetchdts";
5
4
  import { DotNestedKeys as DotNestedKeys$1, DotNestedValue as DotNestedValue$1 } from "@h3ravel/shared";
6
5
  import { Separator } from "@inquirer/prompts";
7
6
  import { ChoiceOrSeparatorArray, ChoiceOrSeparatorArray as ChoiceOrSeparatorArray$1 } from "inquirer-autocomplete-standalone";
@@ -171,11 +170,6 @@ interface IRequest {
171
170
  * @returns An object containing query parameters.
172
171
  */
173
172
  query: Record<string, any>;
174
- /**
175
- * Gets the request headers.
176
- * @returns An object containing request headers.
177
- */
178
- headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
179
173
  /**
180
174
  * Gets all input data (query parameters, route parameters, and body).
181
175
  * @returns A promise resolving to an object containing all input data.
@@ -224,7 +218,7 @@ interface IResponse {
224
218
  * @param content - The HTML content to send.
225
219
  * @returns The HTML content.
226
220
  */
227
- html(content: string): string;
221
+ html(content: string): HTTPResponse;
228
222
  /**
229
223
  * Sends a JSON response.
230
224
  * @param data - The data to send as JSON.
@@ -243,7 +237,7 @@ interface IResponse {
243
237
  * @param status - The HTTP status code for the redirect (default: 302).
244
238
  * @returns The redirect URL.
245
239
  */
246
- redirect(url: string, status?: number): string;
240
+ redirect(url: string, status?: number): HTTPResponse;
247
241
  /**
248
242
  * Gets the underlying event object or a specific property of it.
249
243
  * @param key - Optional key to access a nested property of the event.
@@ -349,6 +343,7 @@ declare class HttpContext {
349
343
  app: IApplication;
350
344
  request: IRequest;
351
345
  response: IResponse;
346
+ private static contexts;
352
347
  constructor(app: IApplication, request: IRequest, response: IResponse);
353
348
  /**
354
349
  * Factory method to create a new HttpContext instance from a context object.
@@ -359,7 +354,15 @@ declare class HttpContext {
359
354
  app: IApplication;
360
355
  request: IRequest;
361
356
  response: IResponse;
362
- }): HttpContext;
357
+ }, event?: unknown): HttpContext;
358
+ /**
359
+ * Retrieve an existing HttpContext instance for an event, if any.
360
+ */
361
+ static get(event: unknown): HttpContext | undefined;
362
+ /**
363
+ * Delete the cached context for a given event (optional cleanup).
364
+ */
365
+ static forget(event: unknown): void;
363
366
  }
364
367
  /**
365
368
  * Type for EventHandler, representing a function that handles an H3 event.
@@ -414,7 +417,7 @@ type Bindings = {
414
417
  [key: `app.${string}`]: any;
415
418
  env(): NodeJS.ProcessEnv;
416
419
  env<T extends string>(key: T, def?: any): any;
417
- view(viewPath: string, params?: Record<string, any>): Promise<string>;
420
+ view(viewPath: string, params?: Record<string, any>): Promise<HTTPResponse>;
418
421
  edge: Edge;
419
422
  asset(key: string, def?: string): string;
420
423
  router: IRouter;
package/dist/index.d.ts CHANGED
@@ -2,9 +2,8 @@
2
2
  import { ChalkInstance } from "chalk";
3
3
  import { ChoiceOrSeparatorArray, ChoiceOrSeparatorArray as ChoiceOrSeparatorArray$1 } from "inquirer-autocomplete-standalone";
4
4
  import { Separator } from "@inquirer/prompts";
5
- import { H3, H3Event, Middleware, MiddlewareOptions, serve } from "h3";
5
+ import { H3, H3Event, HTTPResponse, Middleware, MiddlewareOptions, serve } from "h3";
6
6
  import { Edge } from "edge.js";
7
- import { ResponseHeaderMap, TypedHeaders } from "fetchdts";
8
7
  import { DotNestedKeys as DotNestedKeys$1, DotNestedValue as DotNestedValue$1 } from "@h3ravel/shared";
9
8
 
10
9
  //#region src/Contracts/ObjContract.d.ts
@@ -171,11 +170,6 @@ interface IRequest {
171
170
  * @returns An object containing query parameters.
172
171
  */
173
172
  query: Record<string, any>;
174
- /**
175
- * Gets the request headers.
176
- * @returns An object containing request headers.
177
- */
178
- headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
179
173
  /**
180
174
  * Gets all input data (query parameters, route parameters, and body).
181
175
  * @returns A promise resolving to an object containing all input data.
@@ -224,7 +218,7 @@ interface IResponse {
224
218
  * @param content - The HTML content to send.
225
219
  * @returns The HTML content.
226
220
  */
227
- html(content: string): string;
221
+ html(content: string): HTTPResponse;
228
222
  /**
229
223
  * Sends a JSON response.
230
224
  * @param data - The data to send as JSON.
@@ -243,7 +237,7 @@ interface IResponse {
243
237
  * @param status - The HTTP status code for the redirect (default: 302).
244
238
  * @returns The redirect URL.
245
239
  */
246
- redirect(url: string, status?: number): string;
240
+ redirect(url: string, status?: number): HTTPResponse;
247
241
  /**
248
242
  * Gets the underlying event object or a specific property of it.
249
243
  * @param key - Optional key to access a nested property of the event.
@@ -349,6 +343,7 @@ declare class HttpContext {
349
343
  app: IApplication;
350
344
  request: IRequest;
351
345
  response: IResponse;
346
+ private static contexts;
352
347
  constructor(app: IApplication, request: IRequest, response: IResponse);
353
348
  /**
354
349
  * Factory method to create a new HttpContext instance from a context object.
@@ -359,7 +354,15 @@ declare class HttpContext {
359
354
  app: IApplication;
360
355
  request: IRequest;
361
356
  response: IResponse;
362
- }): HttpContext;
357
+ }, event?: unknown): HttpContext;
358
+ /**
359
+ * Retrieve an existing HttpContext instance for an event, if any.
360
+ */
361
+ static get(event: unknown): HttpContext | undefined;
362
+ /**
363
+ * Delete the cached context for a given event (optional cleanup).
364
+ */
365
+ static forget(event: unknown): void;
363
366
  }
364
367
  /**
365
368
  * Type for EventHandler, representing a function that handles an H3 event.
@@ -414,7 +417,7 @@ type Bindings = {
414
417
  [key: `app.${string}`]: any;
415
418
  env(): NodeJS.ProcessEnv;
416
419
  env<T extends string>(key: T, def?: any): any;
417
- view(viewPath: string, params?: Record<string, any>): Promise<string>;
420
+ view(viewPath: string, params?: Record<string, any>): Promise<HTTPResponse>;
418
421
  edge: Edge;
419
422
  asset(key: string, def?: string): string;
420
423
  router: IRouter;
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ import preferredPM from "preferred-pm";
13
13
  * Encapsulates the application instance, request, and response objects.
14
14
  */
15
15
  var HttpContext = class HttpContext {
16
+ static contexts = /* @__PURE__ */ new WeakMap();
16
17
  constructor(app, request, response) {
17
18
  this.app = app;
18
19
  this.request = request;
@@ -23,11 +24,23 @@ var HttpContext = class HttpContext {
23
24
  * @param ctx - Object containing app, request, and response
24
25
  * @returns A new HttpContext instance
25
26
  */
26
- static init(ctx) {
27
- /**
28
- * Return a new instance
29
- */
30
- return new HttpContext(ctx.app, ctx.request, ctx.response);
27
+ static init(ctx, event) {
28
+ if (event && HttpContext.contexts.has(event)) return HttpContext.contexts.get(event);
29
+ const instance = new HttpContext(ctx.app, ctx.request, ctx.response);
30
+ if (event) HttpContext.contexts.set(event, instance);
31
+ return instance;
32
+ }
33
+ /**
34
+ * Retrieve an existing HttpContext instance for an event, if any.
35
+ */
36
+ static get(event) {
37
+ return HttpContext.contexts.get(event);
38
+ }
39
+ /**
40
+ * Delete the cached context for a given event (optional cleanup).
41
+ */
42
+ static forget(event) {
43
+ HttpContext.contexts.delete(event);
31
44
  }
32
45
  };
33
46
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/shared",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "description": "Shared Utilities.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -48,7 +48,7 @@
48
48
  "chalk": "^5.6.0",
49
49
  "edge.js": "^6.3.0",
50
50
  "escalade": "^3.2.0",
51
- "h3": "^2.0.0-beta.4",
51
+ "h3": "2.0.1-rc.5",
52
52
  "inquirer-autocomplete-standalone": "^0.8.1",
53
53
  "preferred-pm": "^4.1.1"
54
54
  },