@moostjs/event-http 0.5.29 → 0.5.30
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 +269 -68
- package/dist/index.d.ts +232 -5
- package/dist/index.mjs +237 -35
- package/package.json +9 -7
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -47,7 +46,11 @@ const Patch = (path) => HttpMethod("PATCH", path);
|
|
|
47
46
|
|
|
48
47
|
//#endregion
|
|
49
48
|
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
50
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Hook to the Response Status
|
|
51
|
+
* @decorator
|
|
52
|
+
* @paramType TStatusHook
|
|
53
|
+
*/ const StatusHook = () => (0, moost.Resolve)((metas, level) => {
|
|
51
54
|
const hook = (0, __wooksjs_event_http.useStatus)();
|
|
52
55
|
if (level === "PARAM") return hook;
|
|
53
56
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -59,7 +62,12 @@ const StatusHook = () => (0, moost.Resolve)((metas, level) => {
|
|
|
59
62
|
return typeof initialValue === "number" ? initialValue : 200;
|
|
60
63
|
}
|
|
61
64
|
}, "statusCode");
|
|
62
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Hook to the Response Header
|
|
67
|
+
* @decorator
|
|
68
|
+
* @param name - header name
|
|
69
|
+
* @paramType THeaderHook
|
|
70
|
+
*/ const HeaderHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
63
71
|
const hook = (0, __wooksjs_event_http.useSetHeader)(name);
|
|
64
72
|
if (level === "PARAM") return hook;
|
|
65
73
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -71,7 +79,12 @@ const HeaderHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
71
79
|
return typeof initialValue === "string" ? initialValue : "";
|
|
72
80
|
}
|
|
73
81
|
}, name);
|
|
74
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Hook to the Response Cookie
|
|
84
|
+
* @decorator
|
|
85
|
+
* @param name - cookie name
|
|
86
|
+
* @paramType TCookieHook
|
|
87
|
+
*/ const CookieHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
75
88
|
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
76
89
|
if (level === "PARAM") return hook;
|
|
77
90
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -83,7 +96,12 @@ const CookieHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
83
96
|
return typeof initialValue === "string" ? initialValue : "";
|
|
84
97
|
}
|
|
85
98
|
}, name);
|
|
86
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Hook to the Response Cookie Attributes
|
|
101
|
+
* @decorator
|
|
102
|
+
* @param name - cookie name
|
|
103
|
+
* @paramType TCookieAttributes
|
|
104
|
+
*/ const CookieAttrsHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
87
105
|
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
88
106
|
if (level === "PARAM") return (0, __wooksjs_event_core.attachHook)({}, {
|
|
89
107
|
get: () => hook.attrs,
|
|
@@ -98,28 +116,49 @@ const CookieAttrsHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
98
116
|
return typeof initialValue === "object" ? initialValue : {};
|
|
99
117
|
}
|
|
100
118
|
}, name);
|
|
101
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Parse Authorisation Header
|
|
121
|
+
* @decorator
|
|
122
|
+
* @param name - define what to take from the Auth header
|
|
123
|
+
* @paramType string
|
|
124
|
+
*/ function Authorization(name) {
|
|
102
125
|
return (0, moost.Resolve)(() => {
|
|
103
126
|
const auth = (0, __wooksjs_event_http.useAuthorization)();
|
|
104
127
|
switch (name) {
|
|
105
|
-
case "username": return auth.isBasic() ? auth.basicCredentials()?.username :
|
|
106
|
-
case "password": return auth.isBasic() ? auth.basicCredentials()?.password :
|
|
107
|
-
case "bearer": return auth.isBearer() ? auth.authorization :
|
|
128
|
+
case "username": return auth.isBasic() ? auth.basicCredentials()?.username : void 0;
|
|
129
|
+
case "password": return auth.isBasic() ? auth.basicCredentials()?.password : void 0;
|
|
130
|
+
case "bearer": return auth.isBearer() ? auth.authorization : void 0;
|
|
108
131
|
case "raw": return auth.authRawCredentials();
|
|
109
132
|
case "type": return auth.authType();
|
|
133
|
+
default: return void 0;
|
|
110
134
|
}
|
|
111
135
|
}, "authorization");
|
|
112
136
|
}
|
|
113
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Get Request Header Value
|
|
139
|
+
* @decorator
|
|
140
|
+
* @param name - header name
|
|
141
|
+
* @paramType string
|
|
142
|
+
*/ function Header(name) {
|
|
114
143
|
return (0, moost.Resolve)(() => {
|
|
115
144
|
const headers = (0, __wooksjs_event_http.useHeaders)();
|
|
116
145
|
return headers[name];
|
|
117
146
|
}, `header: ${name}`);
|
|
118
147
|
}
|
|
119
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Get Request Cookie Value
|
|
150
|
+
* @decorator
|
|
151
|
+
* @param name - cookie name
|
|
152
|
+
* @paramType string
|
|
153
|
+
*/ function Cookie(name) {
|
|
120
154
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useCookies)().getCookie(name), `cookie: ${name}`);
|
|
121
155
|
}
|
|
122
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Get Query Item value or the whole parsed Query as an object
|
|
158
|
+
* @decorator
|
|
159
|
+
* @param name - query item name (optional)
|
|
160
|
+
* @paramType string | object
|
|
161
|
+
*/ function Query(name) {
|
|
123
162
|
const isItem = !!name;
|
|
124
163
|
const _name = isItem ? name : "Query";
|
|
125
164
|
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), (0, moost.getMoostMate)().decorate("paramName", _name), (0, moost.Resolve)(() => {
|
|
@@ -127,37 +166,74 @@ function Query(name) {
|
|
|
127
166
|
if (isItem) {
|
|
128
167
|
const p = urlSearchParams();
|
|
129
168
|
const value = p.get(name);
|
|
130
|
-
return value === null ?
|
|
169
|
+
return value === null ? void 0 : value;
|
|
131
170
|
}
|
|
132
171
|
const json = jsonSearchParams();
|
|
133
|
-
return Object.keys(json).length > 0 ? json :
|
|
172
|
+
return Object.keys(json).length > 0 ? json : void 0;
|
|
134
173
|
}, _name));
|
|
135
174
|
}
|
|
136
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Get Requested URL
|
|
177
|
+
* @decorator
|
|
178
|
+
* @paramType string
|
|
179
|
+
*/ function Url() {
|
|
137
180
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().url || "", "url");
|
|
138
181
|
}
|
|
139
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Get Requested HTTP Method
|
|
184
|
+
* @decorator
|
|
185
|
+
* @paramType string
|
|
186
|
+
*/ function Method() {
|
|
140
187
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().method, "http_method");
|
|
141
188
|
}
|
|
142
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Get Raw Request Instance
|
|
191
|
+
* @decorator
|
|
192
|
+
* @paramType IncomingMessage
|
|
193
|
+
*/ function Req() {
|
|
143
194
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().rawRequest, "request");
|
|
144
195
|
}
|
|
145
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Get Raw Response Instance
|
|
198
|
+
* @decorator
|
|
199
|
+
* @param opts (optional) { passthrough: boolean }
|
|
200
|
+
* @paramType ServerResponse
|
|
201
|
+
*/ function Res(opts) {
|
|
146
202
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useResponse)().rawResponse(opts), "response");
|
|
147
203
|
}
|
|
148
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Get Request Unique Identificator (UUID)
|
|
206
|
+
* @decorator
|
|
207
|
+
* @paramType string
|
|
208
|
+
*/ function ReqId() {
|
|
149
209
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().reqId(), "reqId");
|
|
150
210
|
}
|
|
151
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Get Request IP Address
|
|
213
|
+
* @decorator
|
|
214
|
+
* @paramType string
|
|
215
|
+
*/ function Ip(opts) {
|
|
152
216
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIp(opts), "ip");
|
|
153
217
|
}
|
|
154
|
-
|
|
218
|
+
/**
|
|
219
|
+
* Get Request IP Address list
|
|
220
|
+
* @decorator
|
|
221
|
+
* @paramType string[]
|
|
222
|
+
*/ function IpList() {
|
|
155
223
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIpList(), "ipList");
|
|
156
224
|
}
|
|
157
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Get Parsed Request Body
|
|
227
|
+
* @decorator
|
|
228
|
+
* @paramType object | string | unknown
|
|
229
|
+
*/ function Body() {
|
|
158
230
|
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", "BODY"), (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().parseBody(), "body"));
|
|
159
231
|
}
|
|
160
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Get Raw Request Body Buffer
|
|
234
|
+
* @decorator
|
|
235
|
+
* @paramType Promise<Buffer>
|
|
236
|
+
*/ function RawBody() {
|
|
161
237
|
return (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().rawBody(), "body");
|
|
162
238
|
}
|
|
163
239
|
|
|
@@ -176,7 +252,43 @@ const setHeaderInterceptor = (name, value, opts) => {
|
|
|
176
252
|
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
177
253
|
return fn;
|
|
178
254
|
};
|
|
179
|
-
|
|
255
|
+
/**
|
|
256
|
+
* Set Header for Request Handler
|
|
257
|
+
*
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
260
|
+
* import { Controller } from 'moost';
|
|
261
|
+
*
|
|
262
|
+
* @Controller()
|
|
263
|
+
* export class ExampleController {
|
|
264
|
+
* @Get('test')
|
|
265
|
+
* // setting header for request handler
|
|
266
|
+
* @SetHeader('x-server', 'my-server')
|
|
267
|
+
* testHandler() {
|
|
268
|
+
* return '...'
|
|
269
|
+
* }
|
|
270
|
+
* }
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
275
|
+
* import { Controller } from 'moost';
|
|
276
|
+
*
|
|
277
|
+
* @Controller()
|
|
278
|
+
* export class ExampleController {
|
|
279
|
+
* @Get('test')
|
|
280
|
+
* // setting header only if status = 400
|
|
281
|
+
* @SetHeader('content-type', 'text/plain', { status: 400 })
|
|
282
|
+
* testHandler() {
|
|
283
|
+
* return '...'
|
|
284
|
+
* }
|
|
285
|
+
* }
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @param name name of header
|
|
289
|
+
* @param value value for header
|
|
290
|
+
* @param options options { status?: number, force?: boolean }
|
|
291
|
+
*/ function SetHeader(...args) {
|
|
180
292
|
return (0, moost.Intercept)(setHeaderInterceptor(...args));
|
|
181
293
|
}
|
|
182
294
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
@@ -189,7 +301,27 @@ const setCookieInterceptor = (name, value, attrs) => {
|
|
|
189
301
|
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
190
302
|
return fn;
|
|
191
303
|
};
|
|
192
|
-
|
|
304
|
+
/**
|
|
305
|
+
* Set Cookie for Request Handler
|
|
306
|
+
* ```ts
|
|
307
|
+
* import { Get, SetCookie } from '@moostjs/event-http';
|
|
308
|
+
* import { Controller } from 'moost';
|
|
309
|
+
*
|
|
310
|
+
* @Controller()
|
|
311
|
+
* export class ExampleController {
|
|
312
|
+
* @Get('test')
|
|
313
|
+
* // setting 'my-cookie' = 'value' with maxAge of 10 minutes
|
|
314
|
+
* @SetCookie('my-cookie', 'value', { maxAge: '10m' })
|
|
315
|
+
* testHandler() {
|
|
316
|
+
* return '...'
|
|
317
|
+
* }
|
|
318
|
+
* }
|
|
319
|
+
* ```
|
|
320
|
+
*
|
|
321
|
+
* @param name name of cookie
|
|
322
|
+
* @param value value for cookie
|
|
323
|
+
* @param attrs cookie attributes
|
|
324
|
+
*/ function SetCookie(...args) {
|
|
193
325
|
return (0, moost.Intercept)(setCookieInterceptor(...args));
|
|
194
326
|
}
|
|
195
327
|
const setStatusInterceptor = (code, opts) => (0, moost.defineInterceptorFn)((before, after) => {
|
|
@@ -198,24 +330,69 @@ const setStatusInterceptor = (code, opts) => (0, moost.defineInterceptorFn)((bef
|
|
|
198
330
|
if (!status.isDefined || opts?.force) status.value = code;
|
|
199
331
|
});
|
|
200
332
|
});
|
|
201
|
-
|
|
333
|
+
/**
|
|
334
|
+
* Set Response Status for Request Handler
|
|
335
|
+
*
|
|
336
|
+
* ```ts
|
|
337
|
+
* import { Get, SetStatus } from '@moostjs/event-http';
|
|
338
|
+
* import { Controller } from 'moost';
|
|
339
|
+
*
|
|
340
|
+
* @Controller()
|
|
341
|
+
* export class ExampleController {
|
|
342
|
+
* @Get('test')
|
|
343
|
+
* @SetStatus(201)
|
|
344
|
+
* testHandler() {
|
|
345
|
+
* return '...'
|
|
346
|
+
* }
|
|
347
|
+
* }
|
|
348
|
+
* ```
|
|
349
|
+
* @param code number
|
|
350
|
+
* @param opts optional { force?: boolean }
|
|
351
|
+
*/ function SetStatus(...args) {
|
|
202
352
|
return (0, moost.Intercept)(setStatusInterceptor(...args));
|
|
203
353
|
}
|
|
204
354
|
|
|
205
355
|
//#endregion
|
|
206
356
|
//#region packages/event-http/src/decorators/limits.decorator.ts
|
|
207
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Creates an interceptor that sets the maximum allowed inflated body size in bytes.
|
|
359
|
+
*
|
|
360
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
361
|
+
* @returns Interceptor function to enforce the limit.
|
|
362
|
+
*/ const globalBodySizeLimit = (n) => (0, moost.defineInterceptorFn)(() => {
|
|
208
363
|
(0, __wooksjs_event_http.useRequest)().setMaxInflated(n);
|
|
209
364
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
210
|
-
|
|
365
|
+
/**
|
|
366
|
+
* Creates an interceptor that sets the maximum allowed compressed body size in bytes.
|
|
367
|
+
*
|
|
368
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
369
|
+
* @returns Interceptor function to enforce the limit.
|
|
370
|
+
*/ const globalCompressedBodySizeLimit = (n) => (0, moost.defineInterceptorFn)(() => {
|
|
211
371
|
(0, __wooksjs_event_http.useRequest)().setMaxCompressed(n);
|
|
212
372
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
213
|
-
|
|
373
|
+
/**
|
|
374
|
+
* Creates an interceptor that sets the timeout for reading the request body.
|
|
375
|
+
*
|
|
376
|
+
* @param n - Timeout in milliseconds.
|
|
377
|
+
* @returns Interceptor function to enforce the timeout.
|
|
378
|
+
*/ const globalBodyReadTimeoutMs = (n) => (0, moost.defineInterceptorFn)(() => {
|
|
214
379
|
(0, __wooksjs_event_http.useRequest)().setReadTimeoutMs(n);
|
|
215
380
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
381
|
+
/**
|
|
382
|
+
* Decorator to limit the maximum inflated body size for the request. Default: 10 MB
|
|
383
|
+
*
|
|
384
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
385
|
+
*/ const BodySizeLimit = (n) => (0, moost.Intercept)(globalBodySizeLimit(n));
|
|
386
|
+
/**
|
|
387
|
+
* Decorator to limit the maximum compressed body size for the request. Default: 1 MB
|
|
388
|
+
*
|
|
389
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
390
|
+
*/ const CompressedBodySizeLimit = (n) => (0, moost.Intercept)(globalCompressedBodySizeLimit(n));
|
|
391
|
+
/**
|
|
392
|
+
* Decorator to set a timeout (in milliseconds) for reading the request body. Default: 10 s
|
|
393
|
+
*
|
|
394
|
+
* @param n - Timeout duration in milliseconds.
|
|
395
|
+
*/ const BodyReadTimeoutMs = (n) => (0, moost.Intercept)(globalBodyReadTimeoutMs(n));
|
|
219
396
|
|
|
220
397
|
//#endregion
|
|
221
398
|
//#region packages/event-http/src/event-http.ts
|
|
@@ -231,7 +408,31 @@ function _define_property(obj, key, value) {
|
|
|
231
408
|
}
|
|
232
409
|
const LOGGER_TITLE = "moost-http";
|
|
233
410
|
const CONTEXT_TYPE = "HTTP";
|
|
234
|
-
|
|
411
|
+
/**
|
|
412
|
+
* ## Moost HTTP Adapter
|
|
413
|
+
*
|
|
414
|
+
* Moost Adapter for HTTP events
|
|
415
|
+
*
|
|
416
|
+
* ```ts
|
|
417
|
+
* │ // HTTP server example
|
|
418
|
+
* │ import { MoostHttp, Get } from '@moostjs/event-http'
|
|
419
|
+
* │ import { Moost, Param } from 'moost'
|
|
420
|
+
* │
|
|
421
|
+
* │ class MyServer extends Moost {
|
|
422
|
+
* │ @Get('test/:name')
|
|
423
|
+
* │ test(@Param('name') name: string) {
|
|
424
|
+
* │ return { message: `Hello ${name}!` }
|
|
425
|
+
* │ }
|
|
426
|
+
* │ }
|
|
427
|
+
* │
|
|
428
|
+
* │ const app = new MyServer()
|
|
429
|
+
* │ const http = new MoostHttp()
|
|
430
|
+
* │ app.adapter(http).listen(3000, () => {
|
|
431
|
+
* │ app.getLogger('MyApp').log('Up on port 3000')
|
|
432
|
+
* │ })
|
|
433
|
+
* │ app.init()
|
|
434
|
+
* ```
|
|
435
|
+
*/ var MoostHttp = class {
|
|
235
436
|
getHttpApp() {
|
|
236
437
|
return this.httpApp;
|
|
237
438
|
}
|
|
@@ -296,7 +497,7 @@ var MoostHttp = class {
|
|
|
296
497
|
methods.DELETE = pathBuilder;
|
|
297
498
|
} else methods[handler.method] = pathBuilder;
|
|
298
499
|
}
|
|
299
|
-
opts.logHandler(
|
|
500
|
+
opts.logHandler(`[36m(${handler.method})[32m${targetPath}`);
|
|
300
501
|
const args = routerBinding.getArgs();
|
|
301
502
|
const params = {};
|
|
302
503
|
args.forEach((a) => params[a] = `{${a}}`);
|
|
@@ -318,46 +519,46 @@ var MoostHttp = class {
|
|
|
318
519
|
};
|
|
319
520
|
|
|
320
521
|
//#endregion
|
|
321
|
-
exports.All = All
|
|
322
|
-
exports.Authorization = Authorization
|
|
323
|
-
exports.Body = Body
|
|
324
|
-
exports.BodyReadTimeoutMs = BodyReadTimeoutMs
|
|
325
|
-
exports.BodySizeLimit = BodySizeLimit
|
|
326
|
-
exports.CompressedBodySizeLimit = CompressedBodySizeLimit
|
|
327
|
-
exports.Cookie = Cookie
|
|
328
|
-
exports.CookieAttrsHook = CookieAttrsHook
|
|
329
|
-
exports.CookieHook = CookieHook
|
|
330
|
-
exports.Delete = Delete
|
|
331
|
-
exports.Get = Get
|
|
332
|
-
exports.Header = Header
|
|
333
|
-
exports.HeaderHook = HeaderHook
|
|
522
|
+
exports.All = All;
|
|
523
|
+
exports.Authorization = Authorization;
|
|
524
|
+
exports.Body = Body;
|
|
525
|
+
exports.BodyReadTimeoutMs = BodyReadTimeoutMs;
|
|
526
|
+
exports.BodySizeLimit = BodySizeLimit;
|
|
527
|
+
exports.CompressedBodySizeLimit = CompressedBodySizeLimit;
|
|
528
|
+
exports.Cookie = Cookie;
|
|
529
|
+
exports.CookieAttrsHook = CookieAttrsHook;
|
|
530
|
+
exports.CookieHook = CookieHook;
|
|
531
|
+
exports.Delete = Delete;
|
|
532
|
+
exports.Get = Get;
|
|
533
|
+
exports.Header = Header;
|
|
534
|
+
exports.HeaderHook = HeaderHook;
|
|
334
535
|
Object.defineProperty(exports, 'HttpError', {
|
|
335
536
|
enumerable: true,
|
|
336
537
|
get: function () {
|
|
337
538
|
return __wooksjs_event_http.HttpError;
|
|
338
539
|
}
|
|
339
540
|
});
|
|
340
|
-
exports.HttpMethod = HttpMethod
|
|
341
|
-
exports.Ip = Ip
|
|
342
|
-
exports.IpList = IpList
|
|
343
|
-
exports.Method = Method
|
|
344
|
-
exports.MoostHttp = MoostHttp
|
|
345
|
-
exports.Patch = Patch
|
|
346
|
-
exports.Post = Post
|
|
347
|
-
exports.Put = Put
|
|
348
|
-
exports.Query = Query
|
|
349
|
-
exports.RawBody = RawBody
|
|
350
|
-
exports.Req = Req
|
|
351
|
-
exports.ReqId = ReqId
|
|
352
|
-
exports.Res = Res
|
|
353
|
-
exports.SetCookie = SetCookie
|
|
354
|
-
exports.SetHeader = SetHeader
|
|
355
|
-
exports.SetStatus = SetStatus
|
|
356
|
-
exports.StatusHook = StatusHook
|
|
357
|
-
exports.Url = Url
|
|
358
|
-
exports.globalBodyReadTimeoutMs = globalBodyReadTimeoutMs
|
|
359
|
-
exports.globalBodySizeLimit = globalBodySizeLimit
|
|
360
|
-
exports.globalCompressedBodySizeLimit = globalCompressedBodySizeLimit
|
|
541
|
+
exports.HttpMethod = HttpMethod;
|
|
542
|
+
exports.Ip = Ip;
|
|
543
|
+
exports.IpList = IpList;
|
|
544
|
+
exports.Method = Method;
|
|
545
|
+
exports.MoostHttp = MoostHttp;
|
|
546
|
+
exports.Patch = Patch;
|
|
547
|
+
exports.Post = Post;
|
|
548
|
+
exports.Put = Put;
|
|
549
|
+
exports.Query = Query;
|
|
550
|
+
exports.RawBody = RawBody;
|
|
551
|
+
exports.Req = Req;
|
|
552
|
+
exports.ReqId = ReqId;
|
|
553
|
+
exports.Res = Res;
|
|
554
|
+
exports.SetCookie = SetCookie;
|
|
555
|
+
exports.SetHeader = SetHeader;
|
|
556
|
+
exports.SetStatus = SetStatus;
|
|
557
|
+
exports.StatusHook = StatusHook;
|
|
558
|
+
exports.Url = Url;
|
|
559
|
+
exports.globalBodyReadTimeoutMs = globalBodyReadTimeoutMs;
|
|
560
|
+
exports.globalBodySizeLimit = globalBodySizeLimit;
|
|
561
|
+
exports.globalCompressedBodySizeLimit = globalCompressedBodySizeLimit;
|
|
361
562
|
Object.defineProperty(exports, 'useHttpContext', {
|
|
362
563
|
enumerable: true,
|
|
363
564
|
get: function () {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { THook } from '@wooksjs/event-core';
|
|
3
2
|
import { TCookieAttributes as TCookieAttributes$1, useSetCookies, WooksHttp, TWooksHttpOptions } from '@wooksjs/event-http';
|
|
4
3
|
export { HttpError, TCacheControl, TCookieAttributesInput, TSetCookieData, useHttpContext } from '@wooksjs/event-http';
|
|
5
4
|
import * as moost from 'moost';
|
|
6
5
|
import { TInterceptorFn, TMoostAdapter, Moost, TConsoleBase, TMoostAdapterOptions } from 'moost';
|
|
7
|
-
import * as _prostojs_infact from '@prostojs/infact';
|
|
8
6
|
import * as http from 'http';
|
|
9
7
|
import { TProstoRouterPathBuilder } from '@prostojs/router';
|
|
10
8
|
import { ListenOptions } from 'net';
|
|
@@ -17,26 +15,119 @@ declare const Put: (path?: string) => MethodDecorator;
|
|
|
17
15
|
declare const Delete: (path?: string) => MethodDecorator;
|
|
18
16
|
declare const Patch: (path?: string) => MethodDecorator;
|
|
19
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Hook to the Response Status
|
|
20
|
+
* @decorator
|
|
21
|
+
* @paramType TStatusHook
|
|
22
|
+
*/
|
|
20
23
|
declare const StatusHook: () => ParameterDecorator & PropertyDecorator;
|
|
24
|
+
/**
|
|
25
|
+
* Hook to the Response Header
|
|
26
|
+
* @decorator
|
|
27
|
+
* @param name - header name
|
|
28
|
+
* @paramType THeaderHook
|
|
29
|
+
*/
|
|
21
30
|
declare const HeaderHook: (name: string) => ParameterDecorator & PropertyDecorator;
|
|
31
|
+
/**
|
|
32
|
+
* Hook to the Response Cookie
|
|
33
|
+
* @decorator
|
|
34
|
+
* @param name - cookie name
|
|
35
|
+
* @paramType TCookieHook
|
|
36
|
+
*/
|
|
22
37
|
declare const CookieHook: (name: string) => ParameterDecorator & PropertyDecorator;
|
|
38
|
+
/**
|
|
39
|
+
* Hook to the Response Cookie Attributes
|
|
40
|
+
* @decorator
|
|
41
|
+
* @param name - cookie name
|
|
42
|
+
* @paramType TCookieAttributes
|
|
43
|
+
*/
|
|
23
44
|
declare const CookieAttrsHook: (name: string) => ParameterDecorator & PropertyDecorator;
|
|
45
|
+
/**
|
|
46
|
+
* Parse Authorisation Header
|
|
47
|
+
* @decorator
|
|
48
|
+
* @param name - define what to take from the Auth header
|
|
49
|
+
* @paramType string
|
|
50
|
+
*/
|
|
24
51
|
declare function Authorization(name: 'username' | 'password' | 'bearer' | 'raw' | 'type'): ParameterDecorator & PropertyDecorator;
|
|
52
|
+
/**
|
|
53
|
+
* Get Request Header Value
|
|
54
|
+
* @decorator
|
|
55
|
+
* @param name - header name
|
|
56
|
+
* @paramType string
|
|
57
|
+
*/
|
|
25
58
|
declare function Header(name: string): ParameterDecorator & PropertyDecorator;
|
|
59
|
+
/**
|
|
60
|
+
* Get Request Cookie Value
|
|
61
|
+
* @decorator
|
|
62
|
+
* @param name - cookie name
|
|
63
|
+
* @paramType string
|
|
64
|
+
*/
|
|
26
65
|
declare function Cookie(name: string): ParameterDecorator & PropertyDecorator;
|
|
66
|
+
/**
|
|
67
|
+
* Get Query Item value or the whole parsed Query as an object
|
|
68
|
+
* @decorator
|
|
69
|
+
* @param name - query item name (optional)
|
|
70
|
+
* @paramType string | object
|
|
71
|
+
*/
|
|
27
72
|
declare function Query(name?: string): ParameterDecorator;
|
|
73
|
+
/**
|
|
74
|
+
* Get Requested URL
|
|
75
|
+
* @decorator
|
|
76
|
+
* @paramType string
|
|
77
|
+
*/
|
|
28
78
|
declare function Url(): ParameterDecorator & PropertyDecorator;
|
|
79
|
+
/**
|
|
80
|
+
* Get Requested HTTP Method
|
|
81
|
+
* @decorator
|
|
82
|
+
* @paramType string
|
|
83
|
+
*/
|
|
29
84
|
declare function Method(): ParameterDecorator & PropertyDecorator;
|
|
85
|
+
/**
|
|
86
|
+
* Get Raw Request Instance
|
|
87
|
+
* @decorator
|
|
88
|
+
* @paramType IncomingMessage
|
|
89
|
+
*/
|
|
30
90
|
declare function Req(): ParameterDecorator & PropertyDecorator;
|
|
91
|
+
/**
|
|
92
|
+
* Get Raw Response Instance
|
|
93
|
+
* @decorator
|
|
94
|
+
* @param opts (optional) { passthrough: boolean }
|
|
95
|
+
* @paramType ServerResponse
|
|
96
|
+
*/
|
|
31
97
|
declare function Res(opts?: {
|
|
32
98
|
passthrough: boolean;
|
|
33
99
|
}): ParameterDecorator & PropertyDecorator;
|
|
100
|
+
/**
|
|
101
|
+
* Get Request Unique Identificator (UUID)
|
|
102
|
+
* @decorator
|
|
103
|
+
* @paramType string
|
|
104
|
+
*/
|
|
34
105
|
declare function ReqId(): ParameterDecorator & PropertyDecorator;
|
|
106
|
+
/**
|
|
107
|
+
* Get Request IP Address
|
|
108
|
+
* @decorator
|
|
109
|
+
* @paramType string
|
|
110
|
+
*/
|
|
35
111
|
declare function Ip(opts?: {
|
|
36
112
|
trustProxy: boolean;
|
|
37
113
|
}): ParameterDecorator & PropertyDecorator;
|
|
114
|
+
/**
|
|
115
|
+
* Get Request IP Address list
|
|
116
|
+
* @decorator
|
|
117
|
+
* @paramType string[]
|
|
118
|
+
*/
|
|
38
119
|
declare function IpList(): ParameterDecorator & PropertyDecorator;
|
|
120
|
+
/**
|
|
121
|
+
* Get Parsed Request Body
|
|
122
|
+
* @decorator
|
|
123
|
+
* @paramType object | string | unknown
|
|
124
|
+
*/
|
|
39
125
|
declare function Body(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
126
|
+
/**
|
|
127
|
+
* Get Raw Request Body Buffer
|
|
128
|
+
* @decorator
|
|
129
|
+
* @paramType Promise<Buffer>
|
|
130
|
+
*/
|
|
40
131
|
declare function RawBody(): ParameterDecorator & PropertyDecorator;
|
|
41
132
|
type TStatusHook = THook<number>;
|
|
42
133
|
type THeaderHook = THook;
|
|
@@ -48,31 +139,166 @@ declare const setHeaderInterceptor: (name: string, value: string, opts?: {
|
|
|
48
139
|
status?: number;
|
|
49
140
|
when?: 'always' | 'error' | 'ok';
|
|
50
141
|
}) => TInterceptorFn;
|
|
142
|
+
/**
|
|
143
|
+
* Set Header for Request Handler
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
147
|
+
* import { Controller } from 'moost';
|
|
148
|
+
*
|
|
149
|
+
* @Controller()
|
|
150
|
+
* export class ExampleController {
|
|
151
|
+
* @Get('test')
|
|
152
|
+
* // setting header for request handler
|
|
153
|
+
* @SetHeader('x-server', 'my-server')
|
|
154
|
+
* testHandler() {
|
|
155
|
+
* return '...'
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
162
|
+
* import { Controller } from 'moost';
|
|
163
|
+
*
|
|
164
|
+
* @Controller()
|
|
165
|
+
* export class ExampleController {
|
|
166
|
+
* @Get('test')
|
|
167
|
+
* // setting header only if status = 400
|
|
168
|
+
* @SetHeader('content-type', 'text/plain', { status: 400 })
|
|
169
|
+
* testHandler() {
|
|
170
|
+
* return '...'
|
|
171
|
+
* }
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param name name of header
|
|
176
|
+
* @param value value for header
|
|
177
|
+
* @param options options { status?: number, force?: boolean }
|
|
178
|
+
*/
|
|
51
179
|
declare function SetHeader(...args: Parameters<typeof setHeaderInterceptor>): ClassDecorator & MethodDecorator;
|
|
52
180
|
declare const setCookieInterceptor: (...args: Parameters<ReturnType<typeof useSetCookies>['setCookie']>) => TInterceptorFn;
|
|
181
|
+
/**
|
|
182
|
+
* Set Cookie for Request Handler
|
|
183
|
+
* ```ts
|
|
184
|
+
* import { Get, SetCookie } from '@moostjs/event-http';
|
|
185
|
+
* import { Controller } from 'moost';
|
|
186
|
+
*
|
|
187
|
+
* @Controller()
|
|
188
|
+
* export class ExampleController {
|
|
189
|
+
* @Get('test')
|
|
190
|
+
* // setting 'my-cookie' = 'value' with maxAge of 10 minutes
|
|
191
|
+
* @SetCookie('my-cookie', 'value', { maxAge: '10m' })
|
|
192
|
+
* testHandler() {
|
|
193
|
+
* return '...'
|
|
194
|
+
* }
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @param name name of cookie
|
|
199
|
+
* @param value value for cookie
|
|
200
|
+
* @param attrs cookie attributes
|
|
201
|
+
*/
|
|
53
202
|
declare function SetCookie(...args: Parameters<typeof setCookieInterceptor>): ClassDecorator & MethodDecorator;
|
|
54
203
|
declare const setStatusInterceptor: (code: number, opts?: {
|
|
55
204
|
force?: boolean;
|
|
56
205
|
}) => TInterceptorFn;
|
|
206
|
+
/**
|
|
207
|
+
* Set Response Status for Request Handler
|
|
208
|
+
*
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { Get, SetStatus } from '@moostjs/event-http';
|
|
211
|
+
* import { Controller } from 'moost';
|
|
212
|
+
*
|
|
213
|
+
* @Controller()
|
|
214
|
+
* export class ExampleController {
|
|
215
|
+
* @Get('test')
|
|
216
|
+
* @SetStatus(201)
|
|
217
|
+
* testHandler() {
|
|
218
|
+
* return '...'
|
|
219
|
+
* }
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
* @param code number
|
|
223
|
+
* @param opts optional { force?: boolean }
|
|
224
|
+
*/
|
|
57
225
|
declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): ClassDecorator & MethodDecorator;
|
|
58
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Creates an interceptor that sets the maximum allowed inflated body size in bytes.
|
|
229
|
+
*
|
|
230
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
231
|
+
* @returns Interceptor function to enforce the limit.
|
|
232
|
+
*/
|
|
59
233
|
declare const globalBodySizeLimit: (n: number) => moost.TInterceptorFn;
|
|
234
|
+
/**
|
|
235
|
+
* Creates an interceptor that sets the maximum allowed compressed body size in bytes.
|
|
236
|
+
*
|
|
237
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
238
|
+
* @returns Interceptor function to enforce the limit.
|
|
239
|
+
*/
|
|
60
240
|
declare const globalCompressedBodySizeLimit: (n: number) => moost.TInterceptorFn;
|
|
241
|
+
/**
|
|
242
|
+
* Creates an interceptor that sets the timeout for reading the request body.
|
|
243
|
+
*
|
|
244
|
+
* @param n - Timeout in milliseconds.
|
|
245
|
+
* @returns Interceptor function to enforce the timeout.
|
|
246
|
+
*/
|
|
61
247
|
declare const globalBodyReadTimeoutMs: (n: number) => moost.TInterceptorFn;
|
|
248
|
+
/**
|
|
249
|
+
* Decorator to limit the maximum inflated body size for the request. Default: 10 MB
|
|
250
|
+
*
|
|
251
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
252
|
+
*/
|
|
62
253
|
declare const BodySizeLimit: (n: number) => ClassDecorator & MethodDecorator;
|
|
254
|
+
/**
|
|
255
|
+
* Decorator to limit the maximum compressed body size for the request. Default: 1 MB
|
|
256
|
+
*
|
|
257
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
258
|
+
*/
|
|
63
259
|
declare const CompressedBodySizeLimit: (n: number) => ClassDecorator & MethodDecorator;
|
|
260
|
+
/**
|
|
261
|
+
* Decorator to set a timeout (in milliseconds) for reading the request body. Default: 10 s
|
|
262
|
+
*
|
|
263
|
+
* @param n - Timeout duration in milliseconds.
|
|
264
|
+
*/
|
|
64
265
|
declare const BodyReadTimeoutMs: (n: number) => ClassDecorator & MethodDecorator;
|
|
65
266
|
|
|
66
267
|
interface THttpHandlerMeta {
|
|
67
268
|
method: string;
|
|
68
269
|
path: string;
|
|
69
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* ## Moost HTTP Adapter
|
|
273
|
+
*
|
|
274
|
+
* Moost Adapter for HTTP events
|
|
275
|
+
*
|
|
276
|
+
* ```ts
|
|
277
|
+
* │ // HTTP server example
|
|
278
|
+
* │ import { MoostHttp, Get } from '@moostjs/event-http'
|
|
279
|
+
* │ import { Moost, Param } from 'moost'
|
|
280
|
+
* │
|
|
281
|
+
* │ class MyServer extends Moost {
|
|
282
|
+
* │ @Get('test/:name')
|
|
283
|
+
* │ test(@Param('name') name: string) {
|
|
284
|
+
* │ return { message: `Hello ${name}!` }
|
|
285
|
+
* │ }
|
|
286
|
+
* │ }
|
|
287
|
+
* │
|
|
288
|
+
* │ const app = new MyServer()
|
|
289
|
+
* │ const http = new MoostHttp()
|
|
290
|
+
* │ app.adapter(http).listen(3000, () => {
|
|
291
|
+
* │ app.getLogger('MyApp').log('Up on port 3000')
|
|
292
|
+
* │ })
|
|
293
|
+
* │ app.init()
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
70
296
|
declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
|
|
71
297
|
readonly name = "http";
|
|
72
298
|
protected httpApp: WooksHttp;
|
|
73
299
|
constructor(httpApp?: WooksHttp | TWooksHttpOptions);
|
|
74
300
|
getHttpApp(): WooksHttp;
|
|
75
|
-
getServerCb(): (req: http.IncomingMessage, res: http.ServerResponse
|
|
301
|
+
getServerCb(): (req: http.IncomingMessage, res: http.ServerResponse) => void;
|
|
76
302
|
listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Promise<void>;
|
|
77
303
|
listen(port?: number, hostname?: string, listeningListener?: () => void): Promise<void>;
|
|
78
304
|
listen(port?: number, backlog?: number, listeningListener?: () => void): Promise<void>;
|
|
@@ -92,9 +318,10 @@ declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
|
|
|
92
318
|
onNotFound(): Promise<unknown>;
|
|
93
319
|
protected moost?: Moost;
|
|
94
320
|
onInit(moost: Moost): void;
|
|
95
|
-
getProvideRegistry():
|
|
321
|
+
getProvideRegistry(): moost.TProvideRegistry;
|
|
96
322
|
getLogger(): TConsoleBase;
|
|
97
323
|
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void;
|
|
98
324
|
}
|
|
99
325
|
|
|
100
|
-
export { All, Authorization, Body, BodyReadTimeoutMs, BodySizeLimit, CompressedBodySizeLimit, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook,
|
|
326
|
+
export { All, Authorization, Body, BodyReadTimeoutMs, BodySizeLimit, CompressedBodySizeLimit, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook, Url, globalBodyReadTimeoutMs, globalBodySizeLimit, globalCompressedBodySizeLimit };
|
|
327
|
+
export type { TCookieAttributes, TCookieHook, THeaderHook, THttpHandlerMeta, TStatusHook };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Intercept, Resolve, TInterceptorPriority, defineInterceptorFn, defineMoostEventHandler, getMoostMate } from "moost";
|
|
2
2
|
import { attachHook } from "@wooksjs/event-core";
|
|
3
|
-
import { HttpError, WooksHttp, createHttpApp, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCookie, useSetCookies, useSetHeader, useStatus } from "@wooksjs/event-http";
|
|
3
|
+
import { HttpError, HttpError as HttpError$1, WooksHttp, createHttpApp, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCookie, useSetCookies, useSetHeader, useStatus } from "@wooksjs/event-http";
|
|
4
4
|
import { useBody } from "@wooksjs/http-body";
|
|
5
5
|
import { createProvideRegistry } from "@prostojs/infact";
|
|
6
6
|
import { Server } from "http";
|
|
@@ -23,7 +23,11 @@ const Patch = (path) => HttpMethod("PATCH", path);
|
|
|
23
23
|
|
|
24
24
|
//#endregion
|
|
25
25
|
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Hook to the Response Status
|
|
28
|
+
* @decorator
|
|
29
|
+
* @paramType TStatusHook
|
|
30
|
+
*/ const StatusHook = () => Resolve((metas, level) => {
|
|
27
31
|
const hook = useStatus();
|
|
28
32
|
if (level === "PARAM") return hook;
|
|
29
33
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -35,7 +39,12 @@ const StatusHook = () => Resolve((metas, level) => {
|
|
|
35
39
|
return typeof initialValue === "number" ? initialValue : 200;
|
|
36
40
|
}
|
|
37
41
|
}, "statusCode");
|
|
38
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Hook to the Response Header
|
|
44
|
+
* @decorator
|
|
45
|
+
* @param name - header name
|
|
46
|
+
* @paramType THeaderHook
|
|
47
|
+
*/ const HeaderHook = (name) => Resolve((metas, level) => {
|
|
39
48
|
const hook = useSetHeader(name);
|
|
40
49
|
if (level === "PARAM") return hook;
|
|
41
50
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -47,7 +56,12 @@ const HeaderHook = (name) => Resolve((metas, level) => {
|
|
|
47
56
|
return typeof initialValue === "string" ? initialValue : "";
|
|
48
57
|
}
|
|
49
58
|
}, name);
|
|
50
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Hook to the Response Cookie
|
|
61
|
+
* @decorator
|
|
62
|
+
* @param name - cookie name
|
|
63
|
+
* @paramType TCookieHook
|
|
64
|
+
*/ const CookieHook = (name) => Resolve((metas, level) => {
|
|
51
65
|
const hook = useSetCookie(name);
|
|
52
66
|
if (level === "PARAM") return hook;
|
|
53
67
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -59,7 +73,12 @@ const CookieHook = (name) => Resolve((metas, level) => {
|
|
|
59
73
|
return typeof initialValue === "string" ? initialValue : "";
|
|
60
74
|
}
|
|
61
75
|
}, name);
|
|
62
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Hook to the Response Cookie Attributes
|
|
78
|
+
* @decorator
|
|
79
|
+
* @param name - cookie name
|
|
80
|
+
* @paramType TCookieAttributes
|
|
81
|
+
*/ const CookieAttrsHook = (name) => Resolve((metas, level) => {
|
|
63
82
|
const hook = useSetCookie(name);
|
|
64
83
|
if (level === "PARAM") return attachHook({}, {
|
|
65
84
|
get: () => hook.attrs,
|
|
@@ -74,28 +93,49 @@ const CookieAttrsHook = (name) => Resolve((metas, level) => {
|
|
|
74
93
|
return typeof initialValue === "object" ? initialValue : {};
|
|
75
94
|
}
|
|
76
95
|
}, name);
|
|
77
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Parse Authorisation Header
|
|
98
|
+
* @decorator
|
|
99
|
+
* @param name - define what to take from the Auth header
|
|
100
|
+
* @paramType string
|
|
101
|
+
*/ function Authorization(name) {
|
|
78
102
|
return Resolve(() => {
|
|
79
103
|
const auth = useAuthorization();
|
|
80
104
|
switch (name) {
|
|
81
|
-
case "username": return auth.isBasic() ? auth.basicCredentials()?.username :
|
|
82
|
-
case "password": return auth.isBasic() ? auth.basicCredentials()?.password :
|
|
83
|
-
case "bearer": return auth.isBearer() ? auth.authorization :
|
|
105
|
+
case "username": return auth.isBasic() ? auth.basicCredentials()?.username : void 0;
|
|
106
|
+
case "password": return auth.isBasic() ? auth.basicCredentials()?.password : void 0;
|
|
107
|
+
case "bearer": return auth.isBearer() ? auth.authorization : void 0;
|
|
84
108
|
case "raw": return auth.authRawCredentials();
|
|
85
109
|
case "type": return auth.authType();
|
|
110
|
+
default: return void 0;
|
|
86
111
|
}
|
|
87
112
|
}, "authorization");
|
|
88
113
|
}
|
|
89
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Get Request Header Value
|
|
116
|
+
* @decorator
|
|
117
|
+
* @param name - header name
|
|
118
|
+
* @paramType string
|
|
119
|
+
*/ function Header(name) {
|
|
90
120
|
return Resolve(() => {
|
|
91
121
|
const headers = useHeaders();
|
|
92
122
|
return headers[name];
|
|
93
123
|
}, `header: ${name}`);
|
|
94
124
|
}
|
|
95
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Get Request Cookie Value
|
|
127
|
+
* @decorator
|
|
128
|
+
* @param name - cookie name
|
|
129
|
+
* @paramType string
|
|
130
|
+
*/ function Cookie(name) {
|
|
96
131
|
return Resolve(() => useCookies().getCookie(name), `cookie: ${name}`);
|
|
97
132
|
}
|
|
98
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Get Query Item value or the whole parsed Query as an object
|
|
135
|
+
* @decorator
|
|
136
|
+
* @param name - query item name (optional)
|
|
137
|
+
* @paramType string | object
|
|
138
|
+
*/ function Query(name) {
|
|
99
139
|
const isItem = !!name;
|
|
100
140
|
const _name = isItem ? name : "Query";
|
|
101
141
|
return getMoostMate().apply(getMoostMate().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), getMoostMate().decorate("paramName", _name), Resolve(() => {
|
|
@@ -103,37 +143,74 @@ function Query(name) {
|
|
|
103
143
|
if (isItem) {
|
|
104
144
|
const p = urlSearchParams();
|
|
105
145
|
const value = p.get(name);
|
|
106
|
-
return value === null ?
|
|
146
|
+
return value === null ? void 0 : value;
|
|
107
147
|
}
|
|
108
148
|
const json = jsonSearchParams();
|
|
109
|
-
return Object.keys(json).length > 0 ? json :
|
|
149
|
+
return Object.keys(json).length > 0 ? json : void 0;
|
|
110
150
|
}, _name));
|
|
111
151
|
}
|
|
112
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Get Requested URL
|
|
154
|
+
* @decorator
|
|
155
|
+
* @paramType string
|
|
156
|
+
*/ function Url() {
|
|
113
157
|
return Resolve(() => useRequest().url || "", "url");
|
|
114
158
|
}
|
|
115
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Get Requested HTTP Method
|
|
161
|
+
* @decorator
|
|
162
|
+
* @paramType string
|
|
163
|
+
*/ function Method() {
|
|
116
164
|
return Resolve(() => useRequest().method, "http_method");
|
|
117
165
|
}
|
|
118
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Get Raw Request Instance
|
|
168
|
+
* @decorator
|
|
169
|
+
* @paramType IncomingMessage
|
|
170
|
+
*/ function Req() {
|
|
119
171
|
return Resolve(() => useRequest().rawRequest, "request");
|
|
120
172
|
}
|
|
121
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Get Raw Response Instance
|
|
175
|
+
* @decorator
|
|
176
|
+
* @param opts (optional) { passthrough: boolean }
|
|
177
|
+
* @paramType ServerResponse
|
|
178
|
+
*/ function Res(opts) {
|
|
122
179
|
return Resolve(() => useResponse().rawResponse(opts), "response");
|
|
123
180
|
}
|
|
124
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Get Request Unique Identificator (UUID)
|
|
183
|
+
* @decorator
|
|
184
|
+
* @paramType string
|
|
185
|
+
*/ function ReqId() {
|
|
125
186
|
return Resolve(() => useRequest().reqId(), "reqId");
|
|
126
187
|
}
|
|
127
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Get Request IP Address
|
|
190
|
+
* @decorator
|
|
191
|
+
* @paramType string
|
|
192
|
+
*/ function Ip(opts) {
|
|
128
193
|
return Resolve(() => useRequest().getIp(opts), "ip");
|
|
129
194
|
}
|
|
130
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Get Request IP Address list
|
|
197
|
+
* @decorator
|
|
198
|
+
* @paramType string[]
|
|
199
|
+
*/ function IpList() {
|
|
131
200
|
return Resolve(() => useRequest().getIpList(), "ipList");
|
|
132
201
|
}
|
|
133
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Get Parsed Request Body
|
|
204
|
+
* @decorator
|
|
205
|
+
* @paramType object | string | unknown
|
|
206
|
+
*/ function Body() {
|
|
134
207
|
return getMoostMate().apply(getMoostMate().decorate("paramSource", "BODY"), Resolve(() => useBody().parseBody(), "body"));
|
|
135
208
|
}
|
|
136
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Get Raw Request Body Buffer
|
|
211
|
+
* @decorator
|
|
212
|
+
* @paramType Promise<Buffer>
|
|
213
|
+
*/ function RawBody() {
|
|
137
214
|
return Resolve(() => useBody().rawBody(), "body");
|
|
138
215
|
}
|
|
139
216
|
|
|
@@ -152,7 +229,43 @@ const setHeaderInterceptor = (name, value, opts) => {
|
|
|
152
229
|
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
153
230
|
return fn;
|
|
154
231
|
};
|
|
155
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Set Header for Request Handler
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
237
|
+
* import { Controller } from 'moost';
|
|
238
|
+
*
|
|
239
|
+
* @Controller()
|
|
240
|
+
* export class ExampleController {
|
|
241
|
+
* @Get('test')
|
|
242
|
+
* // setting header for request handler
|
|
243
|
+
* @SetHeader('x-server', 'my-server')
|
|
244
|
+
* testHandler() {
|
|
245
|
+
* return '...'
|
|
246
|
+
* }
|
|
247
|
+
* }
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { Get, SetHeader } from '@moostjs/event-http';
|
|
252
|
+
* import { Controller } from 'moost';
|
|
253
|
+
*
|
|
254
|
+
* @Controller()
|
|
255
|
+
* export class ExampleController {
|
|
256
|
+
* @Get('test')
|
|
257
|
+
* // setting header only if status = 400
|
|
258
|
+
* @SetHeader('content-type', 'text/plain', { status: 400 })
|
|
259
|
+
* testHandler() {
|
|
260
|
+
* return '...'
|
|
261
|
+
* }
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @param name name of header
|
|
266
|
+
* @param value value for header
|
|
267
|
+
* @param options options { status?: number, force?: boolean }
|
|
268
|
+
*/ function SetHeader(...args) {
|
|
156
269
|
return Intercept(setHeaderInterceptor(...args));
|
|
157
270
|
}
|
|
158
271
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
@@ -165,7 +278,27 @@ const setCookieInterceptor = (name, value, attrs) => {
|
|
|
165
278
|
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
166
279
|
return fn;
|
|
167
280
|
};
|
|
168
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Set Cookie for Request Handler
|
|
283
|
+
* ```ts
|
|
284
|
+
* import { Get, SetCookie } from '@moostjs/event-http';
|
|
285
|
+
* import { Controller } from 'moost';
|
|
286
|
+
*
|
|
287
|
+
* @Controller()
|
|
288
|
+
* export class ExampleController {
|
|
289
|
+
* @Get('test')
|
|
290
|
+
* // setting 'my-cookie' = 'value' with maxAge of 10 minutes
|
|
291
|
+
* @SetCookie('my-cookie', 'value', { maxAge: '10m' })
|
|
292
|
+
* testHandler() {
|
|
293
|
+
* return '...'
|
|
294
|
+
* }
|
|
295
|
+
* }
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @param name name of cookie
|
|
299
|
+
* @param value value for cookie
|
|
300
|
+
* @param attrs cookie attributes
|
|
301
|
+
*/ function SetCookie(...args) {
|
|
169
302
|
return Intercept(setCookieInterceptor(...args));
|
|
170
303
|
}
|
|
171
304
|
const setStatusInterceptor = (code, opts) => defineInterceptorFn((before, after) => {
|
|
@@ -174,24 +307,69 @@ const setStatusInterceptor = (code, opts) => defineInterceptorFn((before, after)
|
|
|
174
307
|
if (!status.isDefined || opts?.force) status.value = code;
|
|
175
308
|
});
|
|
176
309
|
});
|
|
177
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Set Response Status for Request Handler
|
|
312
|
+
*
|
|
313
|
+
* ```ts
|
|
314
|
+
* import { Get, SetStatus } from '@moostjs/event-http';
|
|
315
|
+
* import { Controller } from 'moost';
|
|
316
|
+
*
|
|
317
|
+
* @Controller()
|
|
318
|
+
* export class ExampleController {
|
|
319
|
+
* @Get('test')
|
|
320
|
+
* @SetStatus(201)
|
|
321
|
+
* testHandler() {
|
|
322
|
+
* return '...'
|
|
323
|
+
* }
|
|
324
|
+
* }
|
|
325
|
+
* ```
|
|
326
|
+
* @param code number
|
|
327
|
+
* @param opts optional { force?: boolean }
|
|
328
|
+
*/ function SetStatus(...args) {
|
|
178
329
|
return Intercept(setStatusInterceptor(...args));
|
|
179
330
|
}
|
|
180
331
|
|
|
181
332
|
//#endregion
|
|
182
333
|
//#region packages/event-http/src/decorators/limits.decorator.ts
|
|
183
|
-
|
|
334
|
+
/**
|
|
335
|
+
* Creates an interceptor that sets the maximum allowed inflated body size in bytes.
|
|
336
|
+
*
|
|
337
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
338
|
+
* @returns Interceptor function to enforce the limit.
|
|
339
|
+
*/ const globalBodySizeLimit = (n) => defineInterceptorFn(() => {
|
|
184
340
|
useRequest().setMaxInflated(n);
|
|
185
341
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
186
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Creates an interceptor that sets the maximum allowed compressed body size in bytes.
|
|
344
|
+
*
|
|
345
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
346
|
+
* @returns Interceptor function to enforce the limit.
|
|
347
|
+
*/ const globalCompressedBodySizeLimit = (n) => defineInterceptorFn(() => {
|
|
187
348
|
useRequest().setMaxCompressed(n);
|
|
188
349
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
189
|
-
|
|
350
|
+
/**
|
|
351
|
+
* Creates an interceptor that sets the timeout for reading the request body.
|
|
352
|
+
*
|
|
353
|
+
* @param n - Timeout in milliseconds.
|
|
354
|
+
* @returns Interceptor function to enforce the timeout.
|
|
355
|
+
*/ const globalBodyReadTimeoutMs = (n) => defineInterceptorFn(() => {
|
|
190
356
|
useRequest().setReadTimeoutMs(n);
|
|
191
357
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
358
|
+
/**
|
|
359
|
+
* Decorator to limit the maximum inflated body size for the request. Default: 10 MB
|
|
360
|
+
*
|
|
361
|
+
* @param n - Maximum body size in bytes after decompression.
|
|
362
|
+
*/ const BodySizeLimit = (n) => Intercept(globalBodySizeLimit(n));
|
|
363
|
+
/**
|
|
364
|
+
* Decorator to limit the maximum compressed body size for the request. Default: 1 MB
|
|
365
|
+
*
|
|
366
|
+
* @param n - Maximum body size in bytes before decompression.
|
|
367
|
+
*/ const CompressedBodySizeLimit = (n) => Intercept(globalCompressedBodySizeLimit(n));
|
|
368
|
+
/**
|
|
369
|
+
* Decorator to set a timeout (in milliseconds) for reading the request body. Default: 10 s
|
|
370
|
+
*
|
|
371
|
+
* @param n - Timeout duration in milliseconds.
|
|
372
|
+
*/ const BodyReadTimeoutMs = (n) => Intercept(globalBodyReadTimeoutMs(n));
|
|
195
373
|
|
|
196
374
|
//#endregion
|
|
197
375
|
//#region packages/event-http/src/event-http.ts
|
|
@@ -207,7 +385,31 @@ function _define_property(obj, key, value) {
|
|
|
207
385
|
}
|
|
208
386
|
const LOGGER_TITLE = "moost-http";
|
|
209
387
|
const CONTEXT_TYPE = "HTTP";
|
|
210
|
-
|
|
388
|
+
/**
|
|
389
|
+
* ## Moost HTTP Adapter
|
|
390
|
+
*
|
|
391
|
+
* Moost Adapter for HTTP events
|
|
392
|
+
*
|
|
393
|
+
* ```ts
|
|
394
|
+
* │ // HTTP server example
|
|
395
|
+
* │ import { MoostHttp, Get } from '@moostjs/event-http'
|
|
396
|
+
* │ import { Moost, Param } from 'moost'
|
|
397
|
+
* │
|
|
398
|
+
* │ class MyServer extends Moost {
|
|
399
|
+
* │ @Get('test/:name')
|
|
400
|
+
* │ test(@Param('name') name: string) {
|
|
401
|
+
* │ return { message: `Hello ${name}!` }
|
|
402
|
+
* │ }
|
|
403
|
+
* │ }
|
|
404
|
+
* │
|
|
405
|
+
* │ const app = new MyServer()
|
|
406
|
+
* │ const http = new MoostHttp()
|
|
407
|
+
* │ app.adapter(http).listen(3000, () => {
|
|
408
|
+
* │ app.getLogger('MyApp').log('Up on port 3000')
|
|
409
|
+
* │ })
|
|
410
|
+
* │ app.init()
|
|
411
|
+
* ```
|
|
412
|
+
*/ var MoostHttp = class {
|
|
211
413
|
getHttpApp() {
|
|
212
414
|
return this.httpApp;
|
|
213
415
|
}
|
|
@@ -222,7 +424,7 @@ var MoostHttp = class {
|
|
|
222
424
|
loggerTitle: LOGGER_TITLE,
|
|
223
425
|
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
224
426
|
getControllerInstance: () => this.moost,
|
|
225
|
-
callControllerMethod: () => new HttpError(404, "Resource Not Found"),
|
|
427
|
+
callControllerMethod: () => new HttpError$1(404, "Resource Not Found"),
|
|
226
428
|
targetPath: "",
|
|
227
429
|
handlerType: "__SYSTEM__"
|
|
228
430
|
})();
|
|
@@ -272,7 +474,7 @@ var MoostHttp = class {
|
|
|
272
474
|
methods.DELETE = pathBuilder;
|
|
273
475
|
} else methods[handler.method] = pathBuilder;
|
|
274
476
|
}
|
|
275
|
-
opts.logHandler(
|
|
477
|
+
opts.logHandler(`[36m(${handler.method})[32m${targetPath}`);
|
|
276
478
|
const args = routerBinding.getArgs();
|
|
277
479
|
const params = {};
|
|
278
480
|
args.forEach((a) => params[a] = `{${a}}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-http",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.30",
|
|
4
4
|
"description": "@moostjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -37,16 +37,18 @@
|
|
|
37
37
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
|
|
40
|
-
"
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@wooksjs/event-core": "^0.6.1",
|
|
41
42
|
"@prostojs/infact": "^0.3.3",
|
|
42
43
|
"@prostojs/router": "^0.2.1",
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
44
|
+
"moost": "^0.5.30"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@wooksjs/event-http": "^0.6.1",
|
|
48
|
+
"@wooksjs/http-body": "^0.6.1"
|
|
47
49
|
},
|
|
48
50
|
"devDependencies": {
|
|
49
|
-
"vitest": "
|
|
51
|
+
"vitest": "3.2.4"
|
|
50
52
|
},
|
|
51
53
|
"scripts": {
|
|
52
54
|
"pub": "pnpm publish --access public",
|