@h3ravel/http 11.18.4 → 11.19.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.d.ts CHANGED
@@ -3,7 +3,8 @@ import { Command } from "@h3ravel/musket";
3
3
  import { CacheOptions, ClassConstructor, DotNestedKeys, DotNestedValue, GenericObject, IApplication, IHeaderBag, IHttpContext, IHttpResponse, IMiddleware, IParamBag, IRequest, IResponsable, IResponse, IRoute, ISessionManager, IUrl, MessagesForRules, RequestMethod, RequestObject, ResponseObject, RulesForData } from "@h3ravel/contracts";
4
4
  import { ResponseCodes } from "@h3ravel/foundation";
5
5
  import { DateTime } from "@h3ravel/support";
6
- import { EventHandlerRequest, H3Event, HTTPResponse } from "h3";
6
+ import { H3Event, HTTPResponse } from "h3";
7
+ import { Collectible, CollectionLike, NonCollectible, PaginatorLike, ResoraConfig as ResourceConfig, Resource as Resource$1, ResourceCollection, ResourceData } from "resora";
7
8
 
8
9
  //#region src/Commands/FireCommand.d.ts
9
10
  declare class FireCommand extends Command {
@@ -904,6 +905,11 @@ declare class LogRequests extends Middleware {
904
905
  handle(request: IRequest, next: (request: IRequest) => Promise<unknown>): Promise<unknown>;
905
906
  }
906
907
  //#endregion
908
+ //#region src/Middleware/ResoraMiddleware.d.ts
909
+ declare class ResoraMiddleware extends Middleware {
910
+ handle(request: IRequest, next: (request: IRequest) => Promise<unknown>): Promise<unknown>;
911
+ }
912
+ //#endregion
907
913
  //#region src/Middleware/TrustHosts.d.ts
908
914
  declare class TrustHosts extends Middleware {
909
915
  /**
@@ -2066,100 +2072,11 @@ declare class Request<D extends Record<string, any> = Record<string, any>, R ext
2066
2072
  getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
2067
2073
  }
2068
2074
  //#endregion
2069
- //#region src/Resources/JsonResource.d.ts
2070
- interface Resource {
2071
- [key: string]: any;
2072
- pagination?: {
2073
- from?: number | undefined;
2074
- to?: number | undefined;
2075
- perPage?: number | undefined;
2076
- total?: number | undefined;
2077
- } | undefined;
2078
- }
2079
- type BodyResource = Resource & {
2080
- data: Omit<Resource, 'pagination'>;
2081
- meta?: {
2082
- pagination?: Resource['pagination'];
2083
- } | undefined;
2084
- };
2085
- /**
2086
- * Class to render API resource
2087
- */
2088
- declare class JsonResource<R extends Resource = any> {
2089
- #private;
2090
- protected event: H3Event;
2091
- /**
2092
- * The request instance
2093
- */
2094
- request: H3Event<EventHandlerRequest>['req'];
2095
- /**
2096
- * The response instance
2097
- */
2098
- response: H3Event['res'];
2099
- /**
2100
- * The data to send to the client
2101
- */
2102
- resource: R;
2103
- /**
2104
- * The final response data object
2105
- */
2106
- body: BodyResource;
2107
- /**
2108
- * Flag to track if response should be sent automatically
2109
- */
2110
- private shouldSend;
2111
- /**
2112
- * Flag to track if response has been sent
2113
- */
2114
- private responseSent;
2115
- /**
2116
- * Declare that this includes R's properties
2117
- */
2118
- [key: string]: any;
2119
- /**
2120
- * @param req The request instance
2121
- * @param res The response instance
2122
- * @param rsc The data to send to the client
2123
- */
2124
- constructor(event: H3Event, rsc: R);
2125
- /**
2126
- * Return the data in the expected format
2127
- *
2128
- * @returns
2129
- */
2130
- data(): Resource;
2131
- /**
2132
- * Build the response object
2133
- * @returns this
2134
- */
2135
- json(): this;
2136
- /**
2137
- * Add context data to the response object
2138
- * @param data Context data
2139
- * @returns this
2140
- */
2141
- additional<X extends {
2142
- [key: string]: any;
2143
- }>(data: X): this;
2144
- /**
2145
- * Send the output to the client
2146
- * @returns this
2147
- */
2148
- send(): this;
2149
- /**
2150
- * Set the status code for this response
2151
- * @param code Status code
2152
- * @returns this
2153
- */
2154
- status(code: number): this;
2155
- /**
2156
- * Check if send should be triggered automatically
2157
- */
2158
- private checkSend;
2159
- }
2075
+ //#region src/Resources/Collection.d.ts
2076
+ declare class Collection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends ResourceCollection<R, T> {}
2160
2077
  //#endregion
2161
- //#region src/Resources/ApiResource.d.ts
2162
- declare function ApiResource(instance: JsonResource): JsonResource<any>;
2078
+ //#region src/Resources/Resource.d.ts
2079
+ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends Resource$1<R> {}
2163
2080
  //#endregion
2164
2081
  //#region src/Utilities/HeaderUtility.d.ts
2165
2082
  /**
@@ -2266,4 +2183,4 @@ declare class IpUtils {
2266
2183
  private static setCacheResult;
2267
2184
  }
2268
2185
  //#endregion
2269
- export { ApiResource, BadRequestException, ConflictingHeadersException, Cookie, FileBag, FireCommand, FlashDataMiddleware, FormRequest, HeaderBag, HeaderUtility, HttpContext, HttpRequest, HttpResponse, HttpResponseException, HttpServiceProvider, InputBag, IpUtils, JsonResource, JsonResponse, LogRequests, Middleware, ParamBag, Request, Resource, Responsable, Response, ResponseHeaderBag, ServerBag, SuspiciousOperationException, TrustHosts, UnexpectedValueException, UploadedFile };
2186
+ export { BadRequestException, Collection, ConflictingHeadersException, Cookie, FileBag, FireCommand, FlashDataMiddleware, FormRequest, HeaderBag, HeaderUtility, HttpContext, HttpRequest, HttpResponse, HttpResponseException, HttpServiceProvider, InputBag, IpUtils, JsonResponse, LogRequests, Middleware, ParamBag, Request, ResoraMiddleware, Resource, type ResourceConfig, Responsable, Response, ResponseHeaderBag, ServerBag, SuspiciousOperationException, TrustHosts, UnexpectedValueException, UploadedFile };
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ import preferredPM from "preferred-pm";
5
5
  import { writeFile } from "fs/promises";
6
6
  import { IApplication, IHeaderBag, IHttpContext, IHttpResponse, IMiddleware, IRequest, IResponsable, IResponse } from "@h3ravel/contracts";
7
7
  import { HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES, Injectable, ResponseCodes, statusTexts } from "@h3ravel/foundation";
8
- import { Response as Response$1 } from "@h3ravel/support/facades";
9
8
  import { Arr, DateTime, InvalidArgumentException, Obj, RuntimeException, Str, data_get, data_set, safeDot } from "@h3ravel/support";
10
9
  import { H3Event, getQuery, getRequestIP, getRequestProtocol, getRouterParams, parseCookies } from "h3";
10
+ import { Resource as Resource$1, ResourceCollection } from "resora";
11
11
  //#region src/Commands/FireCommand.ts
12
12
  var FireCommand = class extends Command {
13
13
  /**
@@ -247,7 +247,7 @@ __decorate([
247
247
  var LogRequests = class extends Middleware {
248
248
  async handle(request, next) {
249
249
  const _next = await next(request);
250
- const code = Number(Response$1.getStatusCode());
250
+ const code = request.getEvent().res.status ?? 200;
251
251
  const method = request.method().toLowerCase();
252
252
  let color = "bgRed";
253
253
  if (code < 200) color = "bgWhite";
@@ -273,6 +273,36 @@ __decorate([
273
273
  __decorateMetadata("design:returntype", Promise)
274
274
  ], LogRequests.prototype, "handle", null);
275
275
  //#endregion
276
+ //#region src/Middleware/ResoraMiddleware.ts
277
+ var ResoraMiddleware = class extends Middleware {
278
+ async handle(request, next) {
279
+ const { applyRuntimeConfig, runWithCtx } = await import("resora");
280
+ const { req, res } = request.context.event;
281
+ applyRuntimeConfig(Object.assign({}, config("resources"), {
282
+ resourcesDir: "src/app/http/resources",
283
+ localStubsDir: "node_modules/@h3ravel/http/stubs",
284
+ stubs: {
285
+ resource: "resource.stub",
286
+ collection: "resource.collection.stub",
287
+ controller: "controller.stub",
288
+ api: "controller.api.stub",
289
+ model: "controller.model.stub",
290
+ apiResource: "controller.api.resource.stub"
291
+ }
292
+ }));
293
+ return runWithCtx({
294
+ res,
295
+ req
296
+ }, () => next(request));
297
+ }
298
+ };
299
+ __decorate([
300
+ Injectable(),
301
+ __decorateMetadata("design:type", Function),
302
+ __decorateMetadata("design:paramtypes", [typeof IRequest === "undefined" ? Object : IRequest, Function]),
303
+ __decorateMetadata("design:returntype", Promise)
304
+ ], ResoraMiddleware.prototype, "handle", null);
305
+ //#endregion
276
306
  //#region src/HttpContext.ts
277
307
  /**
278
308
  * Represents the HTTP context for a single request lifecycle.
@@ -289,6 +319,7 @@ var HttpContext = class HttpContext extends IHttpContext {
289
319
  this.app = app;
290
320
  this.request = request;
291
321
  this.response = response;
322
+ this.app.bindMiddleware("ResoraMiddleware", ResoraMiddleware);
292
323
  this.app.bindMiddleware("LogRequests", LogRequests);
293
324
  this.app.bindMiddleware("FlashDataMiddleware", FlashDataMiddleware);
294
325
  }
@@ -1098,6 +1129,7 @@ var HttpResponse = class HttpResponse extends IHttpResponse {
1098
1129
  super();
1099
1130
  this.event = event;
1100
1131
  this.headers = new ResponseHeaderBag(this.event);
1132
+ this.sentHeaders = Object.fromEntries(Array.from(this.event.res.headers.entries(), ([name, value]) => [name, [value]]));
1101
1133
  this.setContent();
1102
1134
  this.setProtocolVersion("1.0");
1103
1135
  }
@@ -4012,151 +4044,10 @@ var Request = class Request extends HttpRequest {
4012
4044
  }
4013
4045
  };
4014
4046
  //#endregion
4015
- //#region src/Resources/JsonResource.ts
4016
- /**
4017
- * Class to render API resource
4018
- */
4019
- var JsonResource = class {
4020
- event;
4021
- /**
4022
- * The request instance
4023
- */
4024
- request;
4025
- /**
4026
- * The response instance
4027
- */
4028
- response;
4029
- /**
4030
- * The data to send to the client
4031
- */
4032
- resource;
4033
- /**
4034
- * The final response data object
4035
- */
4036
- body = { data: {} };
4037
- /**
4038
- * Flag to track if response should be sent automatically
4039
- */
4040
- shouldSend = false;
4041
- /**
4042
- * Flag to track if response has been sent
4043
- */
4044
- responseSent = false;
4045
- /**
4046
- * @param req The request instance
4047
- * @param res The response instance
4048
- * @param rsc The data to send to the client
4049
- */
4050
- constructor(event, rsc) {
4051
- this.event = event;
4052
- this.request = event.req;
4053
- this.response = event.res;
4054
- this.resource = rsc;
4055
- for (const key of Object.keys(rsc)) if (!(key in this)) Object.defineProperty(this, key, {
4056
- enumerable: true,
4057
- configurable: true,
4058
- get: () => this.resource[key],
4059
- set: (value) => {
4060
- this.resource[key] = value;
4061
- }
4062
- });
4063
- }
4064
- /**
4065
- * Return the data in the expected format
4066
- *
4067
- * @returns
4068
- */
4069
- data() {
4070
- return this.resource;
4071
- }
4072
- /**
4073
- * Build the response object
4074
- * @returns this
4075
- */
4076
- json() {
4077
- this.shouldSend = true;
4078
- this.response.status = 200;
4079
- const resource = this.data();
4080
- let data = Array.isArray(resource) ? [...resource] : { ...resource };
4081
- if (typeof data.data !== "undefined") data = data.data;
4082
- if (!Array.isArray(resource)) delete data.pagination;
4083
- this.body = { data };
4084
- if (!Array.isArray(resource) && resource.pagination) {
4085
- const meta = this.body.meta ?? {};
4086
- meta.pagination = resource.pagination;
4087
- this.body.meta = meta;
4088
- }
4089
- if (this.resource.pagination && !this.body.meta?.pagination) {
4090
- const meta = this.body.meta ?? {};
4091
- meta.pagination = this.resource.pagination;
4092
- this.body.meta = meta;
4093
- }
4094
- return this;
4095
- }
4096
- /**
4097
- * Add context data to the response object
4098
- * @param data Context data
4099
- * @returns this
4100
- */
4101
- additional(data) {
4102
- this.shouldSend = true;
4103
- delete data.data;
4104
- delete data.pagination;
4105
- this.body = {
4106
- ...this.body,
4107
- ...data
4108
- };
4109
- return this;
4110
- }
4111
- /**
4112
- * Send the output to the client
4113
- * @returns this
4114
- */
4115
- send() {
4116
- this.shouldSend = false;
4117
- if (!this.responseSent) this.#send();
4118
- return this;
4119
- }
4120
- /**
4121
- * Set the status code for this response
4122
- * @param code Status code
4123
- * @returns this
4124
- */
4125
- status(code) {
4126
- this.response.status = code;
4127
- return this;
4128
- }
4129
- /**
4130
- * Private method to send the response
4131
- */
4132
- #send() {
4133
- if (!this.responseSent) this.responseSent = true;
4134
- }
4135
- /**
4136
- * Check if send should be triggered automatically
4137
- */
4138
- checkSend() {
4139
- if (this.shouldSend && !this.responseSent) this.#send();
4140
- }
4141
- };
4047
+ //#region src/Resources/Collection.ts
4048
+ var Collection = class extends ResourceCollection {};
4142
4049
  //#endregion
4143
- //#region src/Resources/ApiResource.ts
4144
- function ApiResource(instance) {
4145
- return new Proxy(instance, { get(target, prop, receiver) {
4146
- const value = Reflect.get(target, prop, receiver);
4147
- if (typeof value === "function") {
4148
- if (prop === "json" || prop === "additional") return (...args) => {
4149
- const result = value.apply(target, args);
4150
- setImmediate(() => target["checkSend"]());
4151
- return result;
4152
- };
4153
- else if (prop === "send") return (...args) => {
4154
- target["shouldSend"] = false;
4155
- return value.apply(target, args);
4156
- };
4157
- }
4158
- return value;
4159
- } });
4160
- }
4050
+ //#region src/Resources/Resource.ts
4051
+ var Resource = class extends Resource$1 {};
4161
4052
  //#endregion
4162
- export { ApiResource, BadRequestException, ConflictingHeadersException, Cookie, FileBag, FireCommand, FlashDataMiddleware, FormRequest, HeaderBag, HeaderUtility, HttpContext, HttpRequest, HttpResponse, HttpResponseException, HttpServiceProvider, InputBag, IpUtils, JsonResource, JsonResponse, LogRequests, Middleware, ParamBag, Request, Responsable, Response, ResponseHeaderBag, ServerBag, SuspiciousOperationException, TrustHosts, UnexpectedValueException, UploadedFile };
4053
+ export { BadRequestException, Collection, ConflictingHeadersException, Cookie, FileBag, FireCommand, FlashDataMiddleware, FormRequest, HeaderBag, HeaderUtility, HttpContext, HttpRequest, HttpResponse, HttpResponseException, HttpServiceProvider, InputBag, IpUtils, JsonResponse, LogRequests, Middleware, ParamBag, Request, ResoraMiddleware, Resource, Responsable, Response, ResponseHeaderBag, ServerBag, SuspiciousOperationException, TrustHosts, UnexpectedValueException, UploadedFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/http",
3
- "version": "11.18.4",
3
+ "version": "11.19.0",
4
4
  "description": "HTTP kernel, middleware pipeline, request/response classes for H3ravel.",
5
5
  "h3ravel": {
6
6
  "providers": [
@@ -39,17 +39,18 @@
39
39
  "laravel"
40
40
  ],
41
41
  "dependencies": {
42
- "@h3ravel/contracts": "^2.1.4",
43
- "@h3ravel/support": "^2.1.4",
42
+ "@h3ravel/contracts": "^2.2.0",
43
+ "@h3ravel/support": "^2.2.0",
44
44
  "@h3ravel/musket": "^1.29.0-alpha.15",
45
- "@h3ravel/shared": "^2.1.4",
46
- "@h3ravel/session": "^2.1.4",
45
+ "@h3ravel/shared": "^2.2.0",
46
+ "@h3ravel/session": "^2.2.0",
47
47
  "h3": "2.0.1-rc.5",
48
+ "resora": "^1.3.18",
48
49
  "srvx": "^0.8.2"
49
50
  },
50
51
  "peerDependencies": {
51
- "@h3ravel/validation": "^2.1.4",
52
- "@h3ravel/foundation": "^2.1.4"
52
+ "@h3ravel/validation": "^2.2.0",
53
+ "@h3ravel/foundation": "^2.2.0"
53
54
  },
54
55
  "devDependencies": {
55
56
  "typescript": "^6.0.0"