@h3ravel/http 11.0.0 → 11.1.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
@@ -1,393 +1,352 @@
1
- "use strict";
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
8
  var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
18
17
  };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
20
22
 
21
- // src/index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- ApiResource: () => ApiResource,
25
- HttpContext: () => import_shared.HttpContext,
26
- HttpServiceProvider: () => HttpServiceProvider,
27
- JsonResource: () => JsonResource,
28
- LogRequests: () => LogRequests,
29
- Middleware: () => Middleware,
30
- Request: () => Request,
31
- Response: () => Response
32
- });
33
- module.exports = __toCommonJS(index_exports);
23
+ //#endregion
24
+ let __h3ravel_shared = require("@h3ravel/shared");
25
+ __h3ravel_shared = __toESM(__h3ravel_shared);
26
+ let h3 = require("h3");
27
+ h3 = __toESM(h3);
28
+ let __h3ravel_core = require("@h3ravel/core");
29
+ __h3ravel_core = __toESM(__h3ravel_core);
30
+ let __h3ravel_support = require("@h3ravel/support");
31
+ __h3ravel_support = __toESM(__h3ravel_support);
34
32
 
35
- // src/Contracts/HttpContract.ts
36
- var import_shared = require("@h3ravel/shared");
33
+ //#region src/Middleware.ts
34
+ var Middleware = class {};
37
35
 
38
- // src/Middleware.ts
39
- var Middleware = class {
40
- static {
41
- __name(this, "Middleware");
42
- }
43
- };
44
-
45
- // src/Middleware/LogRequests.ts
36
+ //#endregion
37
+ //#region src/Middleware/LogRequests.ts
46
38
  var LogRequests = class extends Middleware {
47
- static {
48
- __name(this, "LogRequests");
49
- }
50
- async handle({ request }, next) {
51
- const url = request.getEvent("url");
52
- console.log(`[${request.getEvent("method")}] ${url.pathname + url.search}`);
53
- return next();
54
- }
39
+ async handle({ request }, next) {
40
+ const url = request.getEvent("url");
41
+ console.log(`[${request.getEvent("method")}] ${url.pathname + url.search}`);
42
+ return next();
43
+ }
55
44
  };
56
45
 
57
- // src/Providers/HttpServiceProvider.ts
58
- var import_h3 = require("h3");
59
- var import_core = require("@h3ravel/core");
60
- var HttpServiceProvider = class extends import_core.ServiceProvider {
61
- static {
62
- __name(this, "HttpServiceProvider");
63
- }
64
- static priority = 998;
65
- register() {
66
- this.app.singleton("http.app", () => {
67
- return new import_h3.H3();
68
- });
69
- this.app.singleton("http.serve", () => import_h3.serve);
70
- }
46
+ //#endregion
47
+ //#region src/Providers/HttpServiceProvider.ts
48
+ /**
49
+ * Sets up HTTP kernel and request lifecycle.
50
+ *
51
+ * Register Request, Response, and Middleware classes.
52
+ * Configure global middleware stack.
53
+ * Boot HTTP kernel.
54
+ *
55
+ * Auto-Registered
56
+ */
57
+ var HttpServiceProvider = class extends __h3ravel_core.ServiceProvider {
58
+ static priority = 998;
59
+ register() {
60
+ this.app.singleton("http.app", () => {
61
+ return new h3.H3();
62
+ });
63
+ this.app.singleton("http.serve", () => h3.serve);
64
+ }
71
65
  };
72
66
 
73
- // src/Request.ts
74
- var import_h32 = require("h3");
75
- var import_support = require("@h3ravel/support");
67
+ //#endregion
68
+ //#region src/Request.ts
76
69
  var Request = class {
77
- static {
78
- __name(this, "Request");
79
- }
80
- app;
81
- /**
82
- * Gets route parameters.
83
- * @returns An object containing route parameters.
84
- */
85
- params;
86
- /**
87
- * Gets query parameters.
88
- * @returns An object containing query parameters.
89
- */
90
- query;
91
- /**
92
- * Gets the request headers.
93
- * @returns An object containing request headers.
94
- */
95
- headers;
96
- /**
97
- * The current H3 H3Event instance
98
- */
99
- event;
100
- constructor(event, app) {
101
- this.app = app;
102
- this.event = event;
103
- this.query = (0, import_h32.getQuery)(this.event);
104
- this.params = (0, import_h32.getRouterParams)(this.event);
105
- this.headers = this.event.req.headers;
106
- }
107
- /**
108
- * Get all input data (query + body).
109
- */
110
- async all() {
111
- let data = {
112
- ...(0, import_h32.getRouterParams)(this.event),
113
- ...(0, import_h32.getQuery)(this.event)
114
- };
115
- if (this.event.req.method === "POST") {
116
- data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()));
117
- } else if (this.event.req.method === "PUT") {
118
- data = Object.fromEntries(Object.entries(await (0, import_h32.readBody)(this.event)));
119
- }
120
- return data;
121
- }
122
- /**
123
- * Get a single input field from query or body.
124
- */
125
- async input(key, defaultValue) {
126
- const data = await this.all();
127
- return data[key] ?? defaultValue;
128
- }
129
- getEvent(key) {
130
- return (0, import_support.safeDot)(this.event, key);
131
- }
70
+ /**
71
+ * Gets route parameters.
72
+ * @returns An object containing route parameters.
73
+ */
74
+ params;
75
+ /**
76
+ * Gets query parameters.
77
+ * @returns An object containing query parameters.
78
+ */
79
+ query;
80
+ /**
81
+ * Gets the request headers.
82
+ * @returns An object containing request headers.
83
+ */
84
+ headers;
85
+ /**
86
+ * The current H3 H3Event instance
87
+ */
88
+ event;
89
+ constructor(event, app) {
90
+ this.app = app;
91
+ this.event = event;
92
+ this.query = (0, h3.getQuery)(this.event);
93
+ this.params = (0, h3.getRouterParams)(this.event);
94
+ this.headers = this.event.req.headers;
95
+ }
96
+ /**
97
+ * Get all input data (query + body).
98
+ */
99
+ async all() {
100
+ let data = {
101
+ ...(0, h3.getRouterParams)(this.event),
102
+ ...(0, h3.getQuery)(this.event)
103
+ };
104
+ if (this.event.req.method === "POST") data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()));
105
+ else if (this.event.req.method === "PUT") data = Object.fromEntries(Object.entries(await (0, h3.readBody)(this.event)));
106
+ return data;
107
+ }
108
+ /**
109
+ * Get a single input field from query or body.
110
+ */
111
+ async input(key, defaultValue) {
112
+ return (await this.all())[key] ?? defaultValue;
113
+ }
114
+ getEvent(key) {
115
+ return (0, __h3ravel_support.safeDot)(this.event, key);
116
+ }
132
117
  };
133
118
 
134
- // src/Resources/JsonResource.ts
119
+ //#endregion
120
+ //#region src/Resources/JsonResource.ts
121
+ /**
122
+ * Class to render API resource
123
+ */
135
124
  var JsonResource = class {
136
- static {
137
- __name(this, "JsonResource");
138
- }
139
- event;
140
- /**
141
- * The request instance
142
- */
143
- request;
144
- /**
145
- * The response instance
146
- */
147
- response;
148
- /**
149
- * The data to send to the client
150
- */
151
- resource;
152
- /**
153
- * The final response data object
154
- */
155
- body = {
156
- data: {}
157
- };
158
- /**
159
- * Flag to track if response should be sent automatically
160
- */
161
- shouldSend = false;
162
- /**
163
- * Flag to track if response has been sent
164
- */
165
- responseSent = false;
166
- /**
167
- * @param req The request instance
168
- * @param res The response instance
169
- * @param rsc The data to send to the client
170
- */
171
- constructor(event, rsc) {
172
- this.event = event;
173
- this.request = event.req;
174
- this.response = event.res;
175
- this.resource = rsc;
176
- for (const key of Object.keys(rsc)) {
177
- if (!(key in this)) {
178
- Object.defineProperty(this, key, {
179
- enumerable: true,
180
- configurable: true,
181
- get: /* @__PURE__ */ __name(() => this.resource[key], "get"),
182
- set: /* @__PURE__ */ __name((value) => {
183
- this.resource[key] = value;
184
- }, "set")
185
- });
186
- }
187
- }
188
- }
189
- /**
190
- * Return the data in the expected format
191
- *
192
- * @returns
193
- */
194
- data() {
195
- return this.resource;
196
- }
197
- /**
198
- * Build the response object
199
- * @returns this
200
- */
201
- json() {
202
- this.shouldSend = true;
203
- this.response.status = 200;
204
- const resource = this.data();
205
- let data = Array.isArray(resource) ? [
206
- ...resource
207
- ] : {
208
- ...resource
209
- };
210
- if (typeof data.data !== "undefined") {
211
- data = data.data;
212
- }
213
- if (!Array.isArray(resource)) {
214
- delete data.pagination;
215
- }
216
- this.body = {
217
- data
218
- };
219
- if (!Array.isArray(resource) && resource.pagination) {
220
- const meta = this.body.meta ?? {};
221
- meta.pagination = resource.pagination;
222
- this.body.meta = meta;
223
- }
224
- if (this.resource.pagination && !this.body.meta?.pagination) {
225
- const meta = this.body.meta ?? {};
226
- meta.pagination = this.resource.pagination;
227
- this.body.meta = meta;
228
- }
229
- return this;
230
- }
231
- /**
232
- * Add context data to the response object
233
- * @param data Context data
234
- * @returns this
235
- */
236
- additional(data) {
237
- this.shouldSend = true;
238
- delete data.data;
239
- delete data.pagination;
240
- this.body = {
241
- ...this.body,
242
- ...data
243
- };
244
- return this;
245
- }
246
- /**
247
- * Send the output to the client
248
- * @returns this
249
- */
250
- send() {
251
- this.shouldSend = false;
252
- if (!this.responseSent) {
253
- this.#send();
254
- }
255
- return this;
256
- }
257
- /**
258
- * Set the status code for this response
259
- * @param code Status code
260
- * @returns this
261
- */
262
- status(code) {
263
- this.response.status = code;
264
- return this;
265
- }
266
- /**
267
- * Private method to send the response
268
- */
269
- #send() {
270
- if (!this.responseSent) {
271
- this.event.context.this.response.json(this.body);
272
- this.responseSent = true;
273
- }
274
- }
275
- /**
276
- * Check if send should be triggered automatically
277
- */
278
- checkSend() {
279
- if (this.shouldSend && !this.responseSent) {
280
- this.#send();
281
- }
282
- }
125
+ /**
126
+ * The request instance
127
+ */
128
+ request;
129
+ /**
130
+ * The response instance
131
+ */
132
+ response;
133
+ /**
134
+ * The data to send to the client
135
+ */
136
+ resource;
137
+ /**
138
+ * The final response data object
139
+ */
140
+ body = { data: {} };
141
+ /**
142
+ * Flag to track if response should be sent automatically
143
+ */
144
+ shouldSend = false;
145
+ /**
146
+ * Flag to track if response has been sent
147
+ */
148
+ responseSent = false;
149
+ /**
150
+ * @param req The request instance
151
+ * @param res The response instance
152
+ * @param rsc The data to send to the client
153
+ */
154
+ constructor(event, rsc) {
155
+ this.event = event;
156
+ this.request = event.req;
157
+ this.response = event.res;
158
+ this.resource = rsc;
159
+ for (const key of Object.keys(rsc)) if (!(key in this)) Object.defineProperty(this, key, {
160
+ enumerable: true,
161
+ configurable: true,
162
+ get: () => this.resource[key],
163
+ set: (value) => {
164
+ this.resource[key] = value;
165
+ }
166
+ });
167
+ }
168
+ /**
169
+ * Return the data in the expected format
170
+ *
171
+ * @returns
172
+ */
173
+ data() {
174
+ return this.resource;
175
+ }
176
+ /**
177
+ * Build the response object
178
+ * @returns this
179
+ */
180
+ json() {
181
+ this.shouldSend = true;
182
+ this.response.status = 200;
183
+ const resource = this.data();
184
+ let data = Array.isArray(resource) ? [...resource] : { ...resource };
185
+ if (typeof data.data !== "undefined") data = data.data;
186
+ if (!Array.isArray(resource)) delete data.pagination;
187
+ this.body = { data };
188
+ if (!Array.isArray(resource) && resource.pagination) {
189
+ const meta = this.body.meta ?? {};
190
+ meta.pagination = resource.pagination;
191
+ this.body.meta = meta;
192
+ }
193
+ if (this.resource.pagination && !this.body.meta?.pagination) {
194
+ const meta = this.body.meta ?? {};
195
+ meta.pagination = this.resource.pagination;
196
+ this.body.meta = meta;
197
+ }
198
+ return this;
199
+ }
200
+ /**
201
+ * Add context data to the response object
202
+ * @param data Context data
203
+ * @returns this
204
+ */
205
+ additional(data) {
206
+ this.shouldSend = true;
207
+ delete data.data;
208
+ delete data.pagination;
209
+ this.body = {
210
+ ...this.body,
211
+ ...data
212
+ };
213
+ return this;
214
+ }
215
+ /**
216
+ * Send the output to the client
217
+ * @returns this
218
+ */
219
+ send() {
220
+ this.shouldSend = false;
221
+ if (!this.responseSent) this.#send();
222
+ return this;
223
+ }
224
+ /**
225
+ * Set the status code for this response
226
+ * @param code Status code
227
+ * @returns this
228
+ */
229
+ status(code) {
230
+ this.response.status = code;
231
+ return this;
232
+ }
233
+ /**
234
+ * Private method to send the response
235
+ */
236
+ #send() {
237
+ if (!this.responseSent) {
238
+ this.event.context.this.response.json(this.body);
239
+ this.responseSent = true;
240
+ }
241
+ }
242
+ /**
243
+ * Check if send should be triggered automatically
244
+ */
245
+ checkSend() {
246
+ if (this.shouldSend && !this.responseSent) this.#send();
247
+ }
283
248
  };
284
249
 
285
- // src/Resources/ApiResource.ts
250
+ //#endregion
251
+ //#region src/Resources/ApiResource.ts
286
252
  function ApiResource(instance) {
287
- return new Proxy(instance, {
288
- get(target, prop, receiver) {
289
- const value = Reflect.get(target, prop, receiver);
290
- if (typeof value === "function") {
291
- if (prop === "json" || prop === "additional") {
292
- return (...args) => {
293
- const result = value.apply(target, args);
294
- setImmediate(() => target["checkSend"]());
295
- return result;
296
- };
297
- } else if (prop === "send") {
298
- return (...args) => {
299
- target["shouldSend"] = false;
300
- return value.apply(target, args);
301
- };
302
- }
303
- }
304
- return value;
305
- }
306
- });
253
+ return new Proxy(instance, { get(target, prop, receiver) {
254
+ const value = Reflect.get(target, prop, receiver);
255
+ if (typeof value === "function") {
256
+ if (prop === "json" || prop === "additional") return (...args) => {
257
+ const result = value.apply(target, args);
258
+ setImmediate(() => target["checkSend"]());
259
+ return result;
260
+ };
261
+ else if (prop === "send") return (...args) => {
262
+ target["shouldSend"] = false;
263
+ return value.apply(target, args);
264
+ };
265
+ }
266
+ return value;
267
+ } });
307
268
  }
308
- __name(ApiResource, "ApiResource");
309
269
 
310
- // src/Response.ts
311
- var import_support2 = require("@h3ravel/support");
312
- var import_h33 = require("h3");
270
+ //#endregion
271
+ //#region src/Response.ts
313
272
  var Response = class {
314
- static {
315
- __name(this, "Response");
316
- }
317
- app;
318
- /**
319
- * The current H3 H3Event instance
320
- */
321
- event;
322
- statusCode = 200;
323
- headers = {};
324
- constructor(event, app) {
325
- this.app = app;
326
- this.event = event;
327
- }
328
- /**
329
- * Set HTTP status code.
330
- */
331
- setStatusCode(code) {
332
- this.statusCode = code;
333
- this.event.res.status = code;
334
- return this;
335
- }
336
- /**
337
- * Set a header.
338
- */
339
- setHeader(name, value) {
340
- this.headers[name] = value;
341
- return this;
342
- }
343
- html(content) {
344
- this.applyHeaders();
345
- return (0, import_h33.html)(this.event, content);
346
- }
347
- /**
348
- * Send a JSON response.
349
- */
350
- json(data) {
351
- this.setHeader("content-type", "application/json; charset=utf-8");
352
- this.applyHeaders();
353
- return data;
354
- }
355
- /**
356
- * Send plain text.
357
- */
358
- text(data) {
359
- this.setHeader("content-type", "text/plain; charset=utf-8");
360
- this.applyHeaders();
361
- return data;
362
- }
363
- /**
364
- * Redirect to another URL.
365
- */
366
- redirect(url, status = 302) {
367
- this.setStatusCode(status);
368
- return (0, import_h33.redirect)(this.event, url, this.statusCode);
369
- }
370
- /**
371
- * Apply headers before sending response.
372
- */
373
- applyHeaders() {
374
- Object.entries(this.headers).forEach(([key, value]) => {
375
- this.event.res.headers.set(key, value);
376
- });
377
- }
378
- getEvent(key) {
379
- return (0, import_support2.safeDot)(this.event, key);
380
- }
273
+ /**
274
+ * The current H3 H3Event instance
275
+ */
276
+ event;
277
+ statusCode = 200;
278
+ headers = {};
279
+ constructor(event, app) {
280
+ this.app = app;
281
+ this.event = event;
282
+ }
283
+ /**
284
+ * Set HTTP status code.
285
+ */
286
+ setStatusCode(code) {
287
+ this.statusCode = code;
288
+ this.event.res.status = code;
289
+ return this;
290
+ }
291
+ /**
292
+ * Set a header.
293
+ */
294
+ setHeader(name, value) {
295
+ this.headers[name] = value;
296
+ return this;
297
+ }
298
+ html(content) {
299
+ this.applyHeaders();
300
+ return (0, h3.html)(this.event, content);
301
+ }
302
+ /**
303
+ * Send a JSON response.
304
+ */
305
+ json(data) {
306
+ this.setHeader("content-type", "application/json; charset=utf-8");
307
+ this.applyHeaders();
308
+ return data;
309
+ }
310
+ /**
311
+ * Send plain text.
312
+ */
313
+ text(data) {
314
+ this.setHeader("content-type", "text/plain; charset=utf-8");
315
+ this.applyHeaders();
316
+ return data;
317
+ }
318
+ /**
319
+ * Redirect to another URL.
320
+ */
321
+ redirect(url, status = 302) {
322
+ this.setStatusCode(status);
323
+ return (0, h3.redirect)(this.event, url, this.statusCode);
324
+ }
325
+ /**
326
+ * Apply headers before sending response.
327
+ */
328
+ applyHeaders() {
329
+ Object.entries(this.headers).forEach(([key, value]) => {
330
+ this.event.res.headers.set(key, value);
331
+ });
332
+ }
333
+ getEvent(key) {
334
+ return (0, __h3ravel_support.safeDot)(this.event, key);
335
+ }
381
336
  };
382
- // Annotate the CommonJS export names for ESM import in node:
383
- 0 && (module.exports = {
384
- ApiResource,
385
- HttpContext,
386
- HttpServiceProvider,
387
- JsonResource,
388
- LogRequests,
389
- Middleware,
390
- Request,
391
- Response
337
+
338
+ //#endregion
339
+ exports.ApiResource = ApiResource;
340
+ Object.defineProperty(exports, 'HttpContext', {
341
+ enumerable: true,
342
+ get: function () {
343
+ return __h3ravel_shared.HttpContext;
344
+ }
392
345
  });
346
+ exports.HttpServiceProvider = HttpServiceProvider;
347
+ exports.JsonResource = JsonResource;
348
+ exports.LogRequests = LogRequests;
349
+ exports.Middleware = Middleware;
350
+ exports.Request = Request;
351
+ exports.Response = Response;
393
352
  //# sourceMappingURL=index.cjs.map