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