@h3ravel/shared 0.26.0 → 0.27.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
@@ -428,6 +428,16 @@ var Resolver = class {
428
428
  static hashObjectOrFunction(provider) {
429
429
  return crypto.default.createHash("sha1").update(provider.toString()).digest("hex");
430
430
  }
431
+ /**
432
+ * Checks if a function is asyncronous
433
+ *
434
+ * @param func
435
+ * @returns
436
+ */
437
+ static isAsyncFunction(func) {
438
+ if (typeof func !== "function") return false;
439
+ return Object.prototype.toString.call(func) === "[object AsyncFunction]";
440
+ }
431
441
  };
432
442
 
433
443
  //#endregion
package/dist/index.d.cts CHANGED
@@ -151,57 +151,336 @@ interface IApplication extends IContainer {
151
151
  getVersion(key: string): string | undefined;
152
152
  }
153
153
  //#endregion
154
+ //#region src/Contracts/IHttpResponse.d.ts
155
+ /**
156
+ * Interface for the Response contract, defining methods for handling HTTP responses.
157
+ */
158
+ interface IHttpResponse {
159
+ /**
160
+ * Set HTTP status code.
161
+ */
162
+ setStatusCode(code: number, text?: string): this;
163
+ /**
164
+ * Retrieves the status code for the current web response.
165
+ */
166
+ getStatusCode(): number;
167
+ /**
168
+ * Sets the response charset.
169
+ */
170
+ setCharset(charset: string): this;
171
+ /**
172
+ * Retrieves the response charset.
173
+ */
174
+ getCharset(): string | undefined;
175
+ /**
176
+ * Returns true if the response may safely be kept in a shared (surrogate) cache.
177
+ *
178
+ * Responses marked "private" with an explicit Cache-Control directive are
179
+ * considered uncacheable.
180
+ *
181
+ * Responses with neither a freshness lifetime (Expires, max-age) nor cache
182
+ * validator (Last-Modified, ETag) are considered uncacheable because there is
183
+ * no way to tell when or how to remove them from the cache.
184
+ *
185
+ * Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation,
186
+ * for example "status codes that are defined as cacheable by default [...]
187
+ * can be reused by a cache with heuristic expiration unless otherwise indicated"
188
+ * (https://tools.ietf.org/html/rfc7231#section-6.1)
189
+ *
190
+ * @final
191
+ */
192
+ isCacheable(): boolean;
193
+ /**
194
+ * Returns true if the response is "fresh".
195
+ *
196
+ * Fresh responses may be served from cache without any interaction with the
197
+ * origin. A response is considered fresh when it includes a Cache-Control/max-age
198
+ * indicator or Expires header and the calculated age is less than the freshness lifetime.
199
+ */
200
+ isFresh(): boolean;
201
+ /**
202
+ * Returns true if the response includes headers that can be used to validate
203
+ * the response with the origin server using a conditional GET request.
204
+ */
205
+ isValidateable(): boolean;
206
+ /**
207
+ * Sets the response content.
208
+ */
209
+ setContent(content?: any): this;
210
+ /**
211
+ * Gets the current response content.
212
+ */
213
+ getContent(): any;
214
+ /**
215
+ * Set a header.
216
+ */
217
+ setHeader(name: string, value: string): this;
218
+ /**
219
+ * Sets the HTTP protocol version (1.0 or 1.1).
220
+ */
221
+ setProtocolVersion(version: string): this;
222
+ /**
223
+ * Gets the HTTP protocol version.
224
+ */
225
+ getProtocolVersion(): string;
226
+ /**
227
+ * Marks the response as "private".
228
+ *
229
+ * It makes the response ineligible for serving other clients.
230
+ */
231
+ setPrivate(): this;
232
+ /**
233
+ * Marks the response as "public".
234
+ *
235
+ * It makes the response eligible for serving other clients.
236
+ */
237
+ setPublic(): this;
238
+ /**
239
+ * Returns the Date header as a DateTime instance.
240
+ * @throws {RuntimeException} When the header is not parseable
241
+ */
242
+ getDate(): any;
243
+ /**
244
+ * Returns the age of the response in seconds.
245
+ *
246
+ * @final
247
+ */
248
+ getAge(): number;
249
+ /**
250
+ * Marks the response stale by setting the Age header to be equal to the maximum age of the response.
251
+ */
252
+ expire(): this;
253
+ /**
254
+ * Returns the value of the Expires header as a DateTime instance.
255
+ *
256
+ * @final
257
+ */
258
+ getExpires(): any;
259
+ /**
260
+ * Returns the number of seconds after the time specified in the response's Date
261
+ * header when the response should no longer be considered fresh.
262
+ *
263
+ * First, it checks for a s-maxage directive, then a max-age directive, and then it falls
264
+ * back on an expires header. It returns null when no maximum age can be established.
265
+ */
266
+ getMaxAge(): number | undefined;
267
+ /**
268
+ * Sets the number of seconds after which the response should no longer be considered fresh.
269
+ *
270
+ * This method sets the Cache-Control max-age directive.
271
+ */
272
+ setMaxAge(value: number): this;
273
+ /**
274
+ * Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
275
+ *
276
+ * This method sets the Cache-Control stale-if-error directive.
277
+ */
278
+ setStaleIfError(value: number): this;
279
+ /**
280
+ * Sets the number of seconds after which the response should no longer return stale content by shared caches.
281
+ *
282
+ * This method sets the Cache-Control stale-while-revalidate directive.
283
+ */
284
+ setStaleWhileRevalidate(value: number): this;
285
+ /**
286
+ * Returns the response's time-to-live in seconds.
287
+ *
288
+ * It returns null when no freshness information is present in the response.
289
+ *
290
+ * When the response's TTL is 0, the response may not be served from cache without first
291
+ * revalidating with the origin.
292
+ *
293
+ * @final
294
+ */
295
+ getTtl(): number | undefined;
296
+ /**
297
+ * Sets the response's time-to-live for shared caches in seconds.
298
+ *
299
+ * This method adjusts the Cache-Control/s-maxage directive.
300
+ */
301
+ setTtl(seconds: number): this;
302
+ /**
303
+ * Sets the response's time-to-live for private/client caches in seconds.
304
+ *
305
+ * This method adjusts the Cache-Control/max-age directive.
306
+ */
307
+ setClientTtl(seconds: number): this;
308
+ /**
309
+ * Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
310
+ *
311
+ * This method sets the Cache-Control s-maxage directive.
312
+ */
313
+ setSharedMaxAge(value: number): this;
314
+ /**
315
+ * Returns the Last-Modified HTTP header as a DateTime instance.
316
+ *
317
+ * @throws \RuntimeException When the HTTP header is not parseable
318
+ *
319
+ * @final
320
+ */
321
+ getLastModified(): any;
322
+ /**
323
+ * Sets the Last-Modified HTTP header with a DateTime instance.
324
+ *
325
+ * Passing null as value will remove the header.
326
+ *
327
+ * @return $this
328
+ *
329
+ * @final
330
+ */
331
+ setLastModified(date?: any): this;
332
+ /**
333
+ * Returns the literal value of the ETag HTTP header.
334
+ */
335
+ getEtag(): string | null;
336
+ /**
337
+ * Sets the ETag value.
338
+ *
339
+ * @param etag The ETag unique identifier or null to remove the header
340
+ * @param weak Whether you want a weak ETag or not
341
+ */
342
+ setEtag(etag?: string, weak?: boolean): this;
343
+ /**
344
+ * Sets the response's cache headers (validation and/or expiration).
345
+ *
346
+ * Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
347
+ *
348
+ * @throws {InvalidArgumentException}
349
+ */
350
+ setCache(options: any): this;
351
+ /**
352
+ * Modifies the response so that it conforms to the rules defined for a 304 status code.
353
+ *
354
+ * This sets the status, removes the body, and discards any headers
355
+ * that MUST NOT be included in 304 responses.
356
+ * @see https://tools.ietf.org/html/rfc2616#section-10.3.5
357
+ */
358
+ setNotModified(): this;
359
+ /**
360
+ * Add an array of headers to the response.
361
+ *
362
+ */
363
+ withHeaders(headers: any): this;
364
+ /**
365
+ * Set the exception to attach to the response.
366
+ */
367
+ withException(e: Error): this;
368
+ /**
369
+ * Throws the response in a HttpResponseException instance.
370
+ *
371
+ * @throws {HttpResponseException}
372
+ */
373
+ throwResponse(): void;
374
+ /**
375
+ * Is response invalid?
376
+ *
377
+ * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
378
+ */
379
+ isInvalid(): boolean;
380
+ /**
381
+ * Is response informative?
382
+ */
383
+ isInformational(): boolean;
384
+ /**
385
+ * Is response successful?
386
+ */
387
+ isSuccessful(): boolean;
388
+ /**
389
+ * Is the response a redirect?
390
+ */
391
+ isRedirection(): boolean;
392
+ /**
393
+ * Is there a client error?
394
+ */
395
+ isClientError(): boolean;
396
+ /**
397
+ * Was there a server side error?
398
+ */
399
+ isServerError(): boolean;
400
+ /**
401
+ * Is the response OK?
402
+ */
403
+ isOk(): boolean;
404
+ /**
405
+ * Is the response forbidden?
406
+ */
407
+ isForbidden(): boolean;
408
+ /**
409
+ * Is the response a not found error?
410
+ */
411
+ isNotFound(): boolean;
412
+ /**
413
+ * Is the response a redirect of some form?
414
+ */
415
+ isRedirect(location?: string | null): boolean;
416
+ /**
417
+ * Is the response empty?
418
+ */
419
+ isEmpty(): boolean;
420
+ /**
421
+ * Apply headers before sending response.
422
+ */
423
+ sendHeaders(statusCode?: number): this;
424
+ /**
425
+ * Prepares the Response before it is sent to the client.
426
+ *
427
+ * This method tweaks the Response to ensure that it is
428
+ * compliant with RFC 2616. Most of the changes are based on
429
+ * the Request that is "associated" with this Response.
430
+ **/
431
+ prepare(request: IRequest): this;
432
+ }
433
+ //#endregion
154
434
  //#region src/Contracts/IResponse.d.ts
155
435
  /**
156
436
  * Interface for the Response contract, defining methods for handling HTTP responses.
157
437
  */
158
- interface IResponse {
438
+ interface IResponse extends IHttpResponse {
159
439
  /**
160
440
  * The current app instance
161
441
  */
162
442
  app: IApplication;
163
443
  /**
164
- * Sets the HTTP status code for the response.
165
- * @param code - The HTTP status code.
166
- * @returns The instance for method chaining.
444
+ * Sends content for the current web response.
167
445
  */
168
- setStatusCode(code: number): this;
446
+ sendContent(type?: 'html' | 'json' | 'text' | 'xml', parse?: boolean): unknown;
169
447
  /**
170
- * Sets a response header.
171
- * @param name - The header name.
172
- * @param value - The header value.
173
- * @returns The instance for method chaining.
448
+ * Sends content for the current web response.
174
449
  */
175
- setHeader(name: string, value: string): this;
450
+ send(type?: 'html' | 'json' | 'text' | 'xml'): unknown;
451
+ /**
452
+ *
453
+ * @param content The content to serve
454
+ * @param send if set to true, the content will be returned, instead of the Response instance
455
+ * @returns
456
+ */
457
+ html(content?: string): this;
458
+ html(content: string, parse: boolean): HTTPResponse;
176
459
  /**
177
- * Sends an HTML response.
178
- * @param content - The HTML content to send.
179
- * @returns The HTML content.
460
+ * Send a JSON response.
180
461
  */
181
- html(content: string): HTTPResponse;
462
+ json<T = unknown>(data?: T): this;
463
+ json<T = unknown>(data: T, parse: boolean): T;
182
464
  /**
183
- * Sends a JSON response.
184
- * @param data - The data to send as JSON.
185
- * @returns The input data.
465
+ * Send plain text.
186
466
  */
187
- json<T = unknown>(data: T): T;
467
+ text(content?: string): this;
468
+ text(content: string, parse: boolean): HTTPResponse;
188
469
  /**
189
- * Sends a plain text response.
190
- * @param data - The text content to send.
191
- * @returns The text content.
470
+ * Send plain xml.
192
471
  */
193
- text(data: string): string;
472
+ xml(data?: string): this;
473
+ xml(data: string, parse: boolean): HTTPResponse;
194
474
  /**
195
- * Redirects to another URL.
196
- * @param url - The URL to redirect to.
197
- * @param status - The HTTP status code for the redirect (default: 302).
198
- * @returns The redirect URL.
475
+ * Redirect to another URL.
199
476
  */
200
- redirect(url: string, status?: number): HTTPResponse;
477
+ redirect(location: string, status?: number, statusText?: string | undefined): HTTPResponse;
201
478
  /**
202
- * Gets the underlying event object or a specific property of it.
203
- * @param key - Optional key to access a nested property of the event.
204
- * @returns The entire event object or the value of the specified property.
479
+ * Dump the response.
480
+ */
481
+ dump(): this;
482
+ /**
483
+ * Get the base event
205
484
  */
206
485
  getEvent(): H3Event;
207
486
  getEvent<K extends DotNestedKeys$1<H3Event>>(key: K): DotNestedValue$1<H3Event, K>;
@@ -211,6 +490,7 @@ interface IResponse {
211
490
  type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';
212
491
  type RequestMethod = 'HEAD' | 'GET' | 'PUT' | 'DELETE' | 'TRACE' | 'OPTIONS' | 'PURGE' | 'POST' | 'CONNECT' | 'PATCH';
213
492
  type RequestObject = Record<string, any>;
493
+ type ResponseObject = Record<string, any>;
214
494
  type ExtractControllerMethods<T$1> = { [K in keyof T$1]: T$1[K] extends ((...args: any[]) => any) ? K : never }[keyof T$1];
215
495
  /**
216
496
  * Interface for the Router contract, defining methods for HTTP routing.
@@ -1109,6 +1389,13 @@ declare class Resolver {
1109
1389
  * @returns
1110
1390
  */
1111
1391
  static hashObjectOrFunction(provider: object | ((..._: any[]) => any)): string;
1392
+ /**
1393
+ * Checks if a function is asyncronous
1394
+ *
1395
+ * @param func
1396
+ * @returns
1397
+ */
1398
+ static isAsyncFunction(func: any): boolean;
1112
1399
  }
1113
1400
  //#endregion
1114
1401
  //#region src/Utils/scripts.d.ts
@@ -1158,4 +1445,4 @@ declare class TaskManager {
1158
1445
  static advancedTaskRunner<R = any>(info: [[string, string], [string, string]] | [[string, string]], task: (() => Promise<R>) | (() => R)): Promise<R | undefined>;
1159
1446
  }
1160
1447
  //#endregion
1161
- export { Bindings, Choice, type ChoiceOrSeparatorArray, Choices, DotFlatten, DotNestedKeys, DotNestedValue, EnvParser, EventHandler, ExtractControllerMethods, FileSystem, GenericWithNullableStringValues, HttpContext, IApplication, IContainer, IController, IMiddleware, IParamBag, IPathName, IRequest, IResponse, IRouter, ISeparator, IServiceProvider, IUploadedFile, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, PathLoader, Prompts, RequestMethod, RequestObject, Resolver, RouteDefinition, RouteEventHandler, RouteMethod, RouterEnd, TaskManager, UseKey, baseTsconfig, mainTsconfig, packageJsonScript };
1448
+ export { Bindings, Choice, type ChoiceOrSeparatorArray, Choices, DotFlatten, DotNestedKeys, DotNestedValue, EnvParser, EventHandler, ExtractControllerMethods, FileSystem, GenericWithNullableStringValues, HttpContext, IApplication, IContainer, IController, IHttpResponse, IMiddleware, IParamBag, IPathName, IRequest, IResponse, IRouter, ISeparator, IServiceProvider, IUploadedFile, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, PathLoader, Prompts, RequestMethod, RequestObject, Resolver, ResponseObject, RouteDefinition, RouteEventHandler, RouteMethod, RouterEnd, TaskManager, UseKey, baseTsconfig, mainTsconfig, packageJsonScript };
package/dist/index.d.ts CHANGED
@@ -151,57 +151,336 @@ interface IApplication extends IContainer {
151
151
  getVersion(key: string): string | undefined;
152
152
  }
153
153
  //#endregion
154
+ //#region src/Contracts/IHttpResponse.d.ts
155
+ /**
156
+ * Interface for the Response contract, defining methods for handling HTTP responses.
157
+ */
158
+ interface IHttpResponse {
159
+ /**
160
+ * Set HTTP status code.
161
+ */
162
+ setStatusCode(code: number, text?: string): this;
163
+ /**
164
+ * Retrieves the status code for the current web response.
165
+ */
166
+ getStatusCode(): number;
167
+ /**
168
+ * Sets the response charset.
169
+ */
170
+ setCharset(charset: string): this;
171
+ /**
172
+ * Retrieves the response charset.
173
+ */
174
+ getCharset(): string | undefined;
175
+ /**
176
+ * Returns true if the response may safely be kept in a shared (surrogate) cache.
177
+ *
178
+ * Responses marked "private" with an explicit Cache-Control directive are
179
+ * considered uncacheable.
180
+ *
181
+ * Responses with neither a freshness lifetime (Expires, max-age) nor cache
182
+ * validator (Last-Modified, ETag) are considered uncacheable because there is
183
+ * no way to tell when or how to remove them from the cache.
184
+ *
185
+ * Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation,
186
+ * for example "status codes that are defined as cacheable by default [...]
187
+ * can be reused by a cache with heuristic expiration unless otherwise indicated"
188
+ * (https://tools.ietf.org/html/rfc7231#section-6.1)
189
+ *
190
+ * @final
191
+ */
192
+ isCacheable(): boolean;
193
+ /**
194
+ * Returns true if the response is "fresh".
195
+ *
196
+ * Fresh responses may be served from cache without any interaction with the
197
+ * origin. A response is considered fresh when it includes a Cache-Control/max-age
198
+ * indicator or Expires header and the calculated age is less than the freshness lifetime.
199
+ */
200
+ isFresh(): boolean;
201
+ /**
202
+ * Returns true if the response includes headers that can be used to validate
203
+ * the response with the origin server using a conditional GET request.
204
+ */
205
+ isValidateable(): boolean;
206
+ /**
207
+ * Sets the response content.
208
+ */
209
+ setContent(content?: any): this;
210
+ /**
211
+ * Gets the current response content.
212
+ */
213
+ getContent(): any;
214
+ /**
215
+ * Set a header.
216
+ */
217
+ setHeader(name: string, value: string): this;
218
+ /**
219
+ * Sets the HTTP protocol version (1.0 or 1.1).
220
+ */
221
+ setProtocolVersion(version: string): this;
222
+ /**
223
+ * Gets the HTTP protocol version.
224
+ */
225
+ getProtocolVersion(): string;
226
+ /**
227
+ * Marks the response as "private".
228
+ *
229
+ * It makes the response ineligible for serving other clients.
230
+ */
231
+ setPrivate(): this;
232
+ /**
233
+ * Marks the response as "public".
234
+ *
235
+ * It makes the response eligible for serving other clients.
236
+ */
237
+ setPublic(): this;
238
+ /**
239
+ * Returns the Date header as a DateTime instance.
240
+ * @throws {RuntimeException} When the header is not parseable
241
+ */
242
+ getDate(): any;
243
+ /**
244
+ * Returns the age of the response in seconds.
245
+ *
246
+ * @final
247
+ */
248
+ getAge(): number;
249
+ /**
250
+ * Marks the response stale by setting the Age header to be equal to the maximum age of the response.
251
+ */
252
+ expire(): this;
253
+ /**
254
+ * Returns the value of the Expires header as a DateTime instance.
255
+ *
256
+ * @final
257
+ */
258
+ getExpires(): any;
259
+ /**
260
+ * Returns the number of seconds after the time specified in the response's Date
261
+ * header when the response should no longer be considered fresh.
262
+ *
263
+ * First, it checks for a s-maxage directive, then a max-age directive, and then it falls
264
+ * back on an expires header. It returns null when no maximum age can be established.
265
+ */
266
+ getMaxAge(): number | undefined;
267
+ /**
268
+ * Sets the number of seconds after which the response should no longer be considered fresh.
269
+ *
270
+ * This method sets the Cache-Control max-age directive.
271
+ */
272
+ setMaxAge(value: number): this;
273
+ /**
274
+ * Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
275
+ *
276
+ * This method sets the Cache-Control stale-if-error directive.
277
+ */
278
+ setStaleIfError(value: number): this;
279
+ /**
280
+ * Sets the number of seconds after which the response should no longer return stale content by shared caches.
281
+ *
282
+ * This method sets the Cache-Control stale-while-revalidate directive.
283
+ */
284
+ setStaleWhileRevalidate(value: number): this;
285
+ /**
286
+ * Returns the response's time-to-live in seconds.
287
+ *
288
+ * It returns null when no freshness information is present in the response.
289
+ *
290
+ * When the response's TTL is 0, the response may not be served from cache without first
291
+ * revalidating with the origin.
292
+ *
293
+ * @final
294
+ */
295
+ getTtl(): number | undefined;
296
+ /**
297
+ * Sets the response's time-to-live for shared caches in seconds.
298
+ *
299
+ * This method adjusts the Cache-Control/s-maxage directive.
300
+ */
301
+ setTtl(seconds: number): this;
302
+ /**
303
+ * Sets the response's time-to-live for private/client caches in seconds.
304
+ *
305
+ * This method adjusts the Cache-Control/max-age directive.
306
+ */
307
+ setClientTtl(seconds: number): this;
308
+ /**
309
+ * Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
310
+ *
311
+ * This method sets the Cache-Control s-maxage directive.
312
+ */
313
+ setSharedMaxAge(value: number): this;
314
+ /**
315
+ * Returns the Last-Modified HTTP header as a DateTime instance.
316
+ *
317
+ * @throws \RuntimeException When the HTTP header is not parseable
318
+ *
319
+ * @final
320
+ */
321
+ getLastModified(): any;
322
+ /**
323
+ * Sets the Last-Modified HTTP header with a DateTime instance.
324
+ *
325
+ * Passing null as value will remove the header.
326
+ *
327
+ * @return $this
328
+ *
329
+ * @final
330
+ */
331
+ setLastModified(date?: any): this;
332
+ /**
333
+ * Returns the literal value of the ETag HTTP header.
334
+ */
335
+ getEtag(): string | null;
336
+ /**
337
+ * Sets the ETag value.
338
+ *
339
+ * @param etag The ETag unique identifier or null to remove the header
340
+ * @param weak Whether you want a weak ETag or not
341
+ */
342
+ setEtag(etag?: string, weak?: boolean): this;
343
+ /**
344
+ * Sets the response's cache headers (validation and/or expiration).
345
+ *
346
+ * Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
347
+ *
348
+ * @throws {InvalidArgumentException}
349
+ */
350
+ setCache(options: any): this;
351
+ /**
352
+ * Modifies the response so that it conforms to the rules defined for a 304 status code.
353
+ *
354
+ * This sets the status, removes the body, and discards any headers
355
+ * that MUST NOT be included in 304 responses.
356
+ * @see https://tools.ietf.org/html/rfc2616#section-10.3.5
357
+ */
358
+ setNotModified(): this;
359
+ /**
360
+ * Add an array of headers to the response.
361
+ *
362
+ */
363
+ withHeaders(headers: any): this;
364
+ /**
365
+ * Set the exception to attach to the response.
366
+ */
367
+ withException(e: Error): this;
368
+ /**
369
+ * Throws the response in a HttpResponseException instance.
370
+ *
371
+ * @throws {HttpResponseException}
372
+ */
373
+ throwResponse(): void;
374
+ /**
375
+ * Is response invalid?
376
+ *
377
+ * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
378
+ */
379
+ isInvalid(): boolean;
380
+ /**
381
+ * Is response informative?
382
+ */
383
+ isInformational(): boolean;
384
+ /**
385
+ * Is response successful?
386
+ */
387
+ isSuccessful(): boolean;
388
+ /**
389
+ * Is the response a redirect?
390
+ */
391
+ isRedirection(): boolean;
392
+ /**
393
+ * Is there a client error?
394
+ */
395
+ isClientError(): boolean;
396
+ /**
397
+ * Was there a server side error?
398
+ */
399
+ isServerError(): boolean;
400
+ /**
401
+ * Is the response OK?
402
+ */
403
+ isOk(): boolean;
404
+ /**
405
+ * Is the response forbidden?
406
+ */
407
+ isForbidden(): boolean;
408
+ /**
409
+ * Is the response a not found error?
410
+ */
411
+ isNotFound(): boolean;
412
+ /**
413
+ * Is the response a redirect of some form?
414
+ */
415
+ isRedirect(location?: string | null): boolean;
416
+ /**
417
+ * Is the response empty?
418
+ */
419
+ isEmpty(): boolean;
420
+ /**
421
+ * Apply headers before sending response.
422
+ */
423
+ sendHeaders(statusCode?: number): this;
424
+ /**
425
+ * Prepares the Response before it is sent to the client.
426
+ *
427
+ * This method tweaks the Response to ensure that it is
428
+ * compliant with RFC 2616. Most of the changes are based on
429
+ * the Request that is "associated" with this Response.
430
+ **/
431
+ prepare(request: IRequest): this;
432
+ }
433
+ //#endregion
154
434
  //#region src/Contracts/IResponse.d.ts
155
435
  /**
156
436
  * Interface for the Response contract, defining methods for handling HTTP responses.
157
437
  */
158
- interface IResponse {
438
+ interface IResponse extends IHttpResponse {
159
439
  /**
160
440
  * The current app instance
161
441
  */
162
442
  app: IApplication;
163
443
  /**
164
- * Sets the HTTP status code for the response.
165
- * @param code - The HTTP status code.
166
- * @returns The instance for method chaining.
444
+ * Sends content for the current web response.
167
445
  */
168
- setStatusCode(code: number): this;
446
+ sendContent(type?: 'html' | 'json' | 'text' | 'xml', parse?: boolean): unknown;
169
447
  /**
170
- * Sets a response header.
171
- * @param name - The header name.
172
- * @param value - The header value.
173
- * @returns The instance for method chaining.
448
+ * Sends content for the current web response.
174
449
  */
175
- setHeader(name: string, value: string): this;
450
+ send(type?: 'html' | 'json' | 'text' | 'xml'): unknown;
451
+ /**
452
+ *
453
+ * @param content The content to serve
454
+ * @param send if set to true, the content will be returned, instead of the Response instance
455
+ * @returns
456
+ */
457
+ html(content?: string): this;
458
+ html(content: string, parse: boolean): HTTPResponse;
176
459
  /**
177
- * Sends an HTML response.
178
- * @param content - The HTML content to send.
179
- * @returns The HTML content.
460
+ * Send a JSON response.
180
461
  */
181
- html(content: string): HTTPResponse;
462
+ json<T = unknown>(data?: T): this;
463
+ json<T = unknown>(data: T, parse: boolean): T;
182
464
  /**
183
- * Sends a JSON response.
184
- * @param data - The data to send as JSON.
185
- * @returns The input data.
465
+ * Send plain text.
186
466
  */
187
- json<T = unknown>(data: T): T;
467
+ text(content?: string): this;
468
+ text(content: string, parse: boolean): HTTPResponse;
188
469
  /**
189
- * Sends a plain text response.
190
- * @param data - The text content to send.
191
- * @returns The text content.
470
+ * Send plain xml.
192
471
  */
193
- text(data: string): string;
472
+ xml(data?: string): this;
473
+ xml(data: string, parse: boolean): HTTPResponse;
194
474
  /**
195
- * Redirects to another URL.
196
- * @param url - The URL to redirect to.
197
- * @param status - The HTTP status code for the redirect (default: 302).
198
- * @returns The redirect URL.
475
+ * Redirect to another URL.
199
476
  */
200
- redirect(url: string, status?: number): HTTPResponse;
477
+ redirect(location: string, status?: number, statusText?: string | undefined): HTTPResponse;
201
478
  /**
202
- * Gets the underlying event object or a specific property of it.
203
- * @param key - Optional key to access a nested property of the event.
204
- * @returns The entire event object or the value of the specified property.
479
+ * Dump the response.
480
+ */
481
+ dump(): this;
482
+ /**
483
+ * Get the base event
205
484
  */
206
485
  getEvent(): H3Event;
207
486
  getEvent<K extends DotNestedKeys$1<H3Event>>(key: K): DotNestedValue$1<H3Event, K>;
@@ -211,6 +490,7 @@ interface IResponse {
211
490
  type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';
212
491
  type RequestMethod = 'HEAD' | 'GET' | 'PUT' | 'DELETE' | 'TRACE' | 'OPTIONS' | 'PURGE' | 'POST' | 'CONNECT' | 'PATCH';
213
492
  type RequestObject = Record<string, any>;
493
+ type ResponseObject = Record<string, any>;
214
494
  type ExtractControllerMethods<T$1> = { [K in keyof T$1]: T$1[K] extends ((...args: any[]) => any) ? K : never }[keyof T$1];
215
495
  /**
216
496
  * Interface for the Router contract, defining methods for HTTP routing.
@@ -1109,6 +1389,13 @@ declare class Resolver {
1109
1389
  * @returns
1110
1390
  */
1111
1391
  static hashObjectOrFunction(provider: object | ((..._: any[]) => any)): string;
1392
+ /**
1393
+ * Checks if a function is asyncronous
1394
+ *
1395
+ * @param func
1396
+ * @returns
1397
+ */
1398
+ static isAsyncFunction(func: any): boolean;
1112
1399
  }
1113
1400
  //#endregion
1114
1401
  //#region src/Utils/scripts.d.ts
@@ -1158,4 +1445,4 @@ declare class TaskManager {
1158
1445
  static advancedTaskRunner<R = any>(info: [[string, string], [string, string]] | [[string, string]], task: (() => Promise<R>) | (() => R)): Promise<R | undefined>;
1159
1446
  }
1160
1447
  //#endregion
1161
- export { Bindings, Choice, type ChoiceOrSeparatorArray, Choices, DotFlatten, DotNestedKeys, DotNestedValue, EnvParser, EventHandler, ExtractControllerMethods, FileSystem, GenericWithNullableStringValues, HttpContext, IApplication, IContainer, IController, IMiddleware, IParamBag, IPathName, IRequest, IResponse, IRouter, ISeparator, IServiceProvider, IUploadedFile, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, PathLoader, Prompts, RequestMethod, RequestObject, Resolver, RouteDefinition, RouteEventHandler, RouteMethod, RouterEnd, TaskManager, UseKey, baseTsconfig, mainTsconfig, packageJsonScript };
1448
+ export { Bindings, Choice, type ChoiceOrSeparatorArray, Choices, DotFlatten, DotNestedKeys, DotNestedValue, EnvParser, EventHandler, ExtractControllerMethods, FileSystem, GenericWithNullableStringValues, HttpContext, IApplication, IContainer, IController, IHttpResponse, IMiddleware, IParamBag, IPathName, IRequest, IResponse, IRouter, ISeparator, IServiceProvider, IUploadedFile, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, PathLoader, Prompts, RequestMethod, RequestObject, Resolver, ResponseObject, RouteDefinition, RouteEventHandler, RouteMethod, RouterEnd, TaskManager, UseKey, baseTsconfig, mainTsconfig, packageJsonScript };
package/dist/index.js CHANGED
@@ -397,6 +397,16 @@ var Resolver = class {
397
397
  static hashObjectOrFunction(provider) {
398
398
  return crypto.createHash("sha1").update(provider.toString()).digest("hex");
399
399
  }
400
+ /**
401
+ * Checks if a function is asyncronous
402
+ *
403
+ * @param func
404
+ * @returns
405
+ */
406
+ static isAsyncFunction(func) {
407
+ if (typeof func !== "function") return false;
408
+ return Object.prototype.toString.call(func) === "[object AsyncFunction]";
409
+ }
400
410
  };
401
411
 
402
412
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/shared",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "Shared Utilities.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",