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