@moostjs/event-http 0.5.28 → 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 +272 -72
- package/dist/index.d.ts +236 -9
- package/dist/index.mjs +240 -39
- 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;
|
|
@@ -26,7 +25,6 @@ const moost = __toESM(require("moost"));
|
|
|
26
25
|
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
27
26
|
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
28
27
|
const __wooksjs_http_body = __toESM(require("@wooksjs/http-body"));
|
|
29
|
-
const packages_moost_dist = __toESM(require("packages/moost/dist"));
|
|
30
28
|
const __prostojs_infact = __toESM(require("@prostojs/infact"));
|
|
31
29
|
const http = __toESM(require("http"));
|
|
32
30
|
const https = __toESM(require("https"));
|
|
@@ -48,7 +46,11 @@ const Patch = (path) => HttpMethod("PATCH", path);
|
|
|
48
46
|
|
|
49
47
|
//#endregion
|
|
50
48
|
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
51
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Hook to the Response Status
|
|
51
|
+
* @decorator
|
|
52
|
+
* @paramType TStatusHook
|
|
53
|
+
*/ const StatusHook = () => (0, moost.Resolve)((metas, level) => {
|
|
52
54
|
const hook = (0, __wooksjs_event_http.useStatus)();
|
|
53
55
|
if (level === "PARAM") return hook;
|
|
54
56
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -60,7 +62,12 @@ const StatusHook = () => (0, moost.Resolve)((metas, level) => {
|
|
|
60
62
|
return typeof initialValue === "number" ? initialValue : 200;
|
|
61
63
|
}
|
|
62
64
|
}, "statusCode");
|
|
63
|
-
|
|
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) => {
|
|
64
71
|
const hook = (0, __wooksjs_event_http.useSetHeader)(name);
|
|
65
72
|
if (level === "PARAM") return hook;
|
|
66
73
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -72,7 +79,12 @@ const HeaderHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
72
79
|
return typeof initialValue === "string" ? initialValue : "";
|
|
73
80
|
}
|
|
74
81
|
}, name);
|
|
75
|
-
|
|
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) => {
|
|
76
88
|
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
77
89
|
if (level === "PARAM") return hook;
|
|
78
90
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -84,7 +96,12 @@ const CookieHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
84
96
|
return typeof initialValue === "string" ? initialValue : "";
|
|
85
97
|
}
|
|
86
98
|
}, name);
|
|
87
|
-
|
|
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) => {
|
|
88
105
|
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
89
106
|
if (level === "PARAM") return (0, __wooksjs_event_core.attachHook)({}, {
|
|
90
107
|
get: () => hook.attrs,
|
|
@@ -99,28 +116,49 @@ const CookieAttrsHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
|
99
116
|
return typeof initialValue === "object" ? initialValue : {};
|
|
100
117
|
}
|
|
101
118
|
}, name);
|
|
102
|
-
|
|
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) {
|
|
103
125
|
return (0, moost.Resolve)(() => {
|
|
104
126
|
const auth = (0, __wooksjs_event_http.useAuthorization)();
|
|
105
127
|
switch (name) {
|
|
106
|
-
case "username": return auth.isBasic() ? auth.basicCredentials()?.username :
|
|
107
|
-
case "password": return auth.isBasic() ? auth.basicCredentials()?.password :
|
|
108
|
-
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;
|
|
109
131
|
case "raw": return auth.authRawCredentials();
|
|
110
132
|
case "type": return auth.authType();
|
|
133
|
+
default: return void 0;
|
|
111
134
|
}
|
|
112
135
|
}, "authorization");
|
|
113
136
|
}
|
|
114
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Get Request Header Value
|
|
139
|
+
* @decorator
|
|
140
|
+
* @param name - header name
|
|
141
|
+
* @paramType string
|
|
142
|
+
*/ function Header(name) {
|
|
115
143
|
return (0, moost.Resolve)(() => {
|
|
116
144
|
const headers = (0, __wooksjs_event_http.useHeaders)();
|
|
117
145
|
return headers[name];
|
|
118
146
|
}, `header: ${name}`);
|
|
119
147
|
}
|
|
120
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Get Request Cookie Value
|
|
150
|
+
* @decorator
|
|
151
|
+
* @param name - cookie name
|
|
152
|
+
* @paramType string
|
|
153
|
+
*/ function Cookie(name) {
|
|
121
154
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useCookies)().getCookie(name), `cookie: ${name}`);
|
|
122
155
|
}
|
|
123
|
-
|
|
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) {
|
|
124
162
|
const isItem = !!name;
|
|
125
163
|
const _name = isItem ? name : "Query";
|
|
126
164
|
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), (0, moost.getMoostMate)().decorate("paramName", _name), (0, moost.Resolve)(() => {
|
|
@@ -128,37 +166,74 @@ function Query(name) {
|
|
|
128
166
|
if (isItem) {
|
|
129
167
|
const p = urlSearchParams();
|
|
130
168
|
const value = p.get(name);
|
|
131
|
-
return value === null ?
|
|
169
|
+
return value === null ? void 0 : value;
|
|
132
170
|
}
|
|
133
171
|
const json = jsonSearchParams();
|
|
134
|
-
return Object.keys(json).length > 0 ? json :
|
|
172
|
+
return Object.keys(json).length > 0 ? json : void 0;
|
|
135
173
|
}, _name));
|
|
136
174
|
}
|
|
137
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Get Requested URL
|
|
177
|
+
* @decorator
|
|
178
|
+
* @paramType string
|
|
179
|
+
*/ function Url() {
|
|
138
180
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().url || "", "url");
|
|
139
181
|
}
|
|
140
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Get Requested HTTP Method
|
|
184
|
+
* @decorator
|
|
185
|
+
* @paramType string
|
|
186
|
+
*/ function Method() {
|
|
141
187
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().method, "http_method");
|
|
142
188
|
}
|
|
143
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Get Raw Request Instance
|
|
191
|
+
* @decorator
|
|
192
|
+
* @paramType IncomingMessage
|
|
193
|
+
*/ function Req() {
|
|
144
194
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().rawRequest, "request");
|
|
145
195
|
}
|
|
146
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Get Raw Response Instance
|
|
198
|
+
* @decorator
|
|
199
|
+
* @param opts (optional) { passthrough: boolean }
|
|
200
|
+
* @paramType ServerResponse
|
|
201
|
+
*/ function Res(opts) {
|
|
147
202
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useResponse)().rawResponse(opts), "response");
|
|
148
203
|
}
|
|
149
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Get Request Unique Identificator (UUID)
|
|
206
|
+
* @decorator
|
|
207
|
+
* @paramType string
|
|
208
|
+
*/ function ReqId() {
|
|
150
209
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().reqId(), "reqId");
|
|
151
210
|
}
|
|
152
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Get Request IP Address
|
|
213
|
+
* @decorator
|
|
214
|
+
* @paramType string
|
|
215
|
+
*/ function Ip(opts) {
|
|
153
216
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIp(opts), "ip");
|
|
154
217
|
}
|
|
155
|
-
|
|
218
|
+
/**
|
|
219
|
+
* Get Request IP Address list
|
|
220
|
+
* @decorator
|
|
221
|
+
* @paramType string[]
|
|
222
|
+
*/ function IpList() {
|
|
156
223
|
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIpList(), "ipList");
|
|
157
224
|
}
|
|
158
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Get Parsed Request Body
|
|
227
|
+
* @decorator
|
|
228
|
+
* @paramType object | string | unknown
|
|
229
|
+
*/ function Body() {
|
|
159
230
|
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", "BODY"), (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().parseBody(), "body"));
|
|
160
231
|
}
|
|
161
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Get Raw Request Body Buffer
|
|
234
|
+
* @decorator
|
|
235
|
+
* @paramType Promise<Buffer>
|
|
236
|
+
*/ function RawBody() {
|
|
162
237
|
return (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().rawBody(), "body");
|
|
163
238
|
}
|
|
164
239
|
|
|
@@ -177,7 +252,43 @@ const setHeaderInterceptor = (name, value, opts) => {
|
|
|
177
252
|
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
178
253
|
return fn;
|
|
179
254
|
};
|
|
180
|
-
|
|
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) {
|
|
181
292
|
return (0, moost.Intercept)(setHeaderInterceptor(...args));
|
|
182
293
|
}
|
|
183
294
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
@@ -190,7 +301,27 @@ const setCookieInterceptor = (name, value, attrs) => {
|
|
|
190
301
|
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
191
302
|
return fn;
|
|
192
303
|
};
|
|
193
|
-
|
|
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) {
|
|
194
325
|
return (0, moost.Intercept)(setCookieInterceptor(...args));
|
|
195
326
|
}
|
|
196
327
|
const setStatusInterceptor = (code, opts) => (0, moost.defineInterceptorFn)((before, after) => {
|
|
@@ -199,24 +330,69 @@ const setStatusInterceptor = (code, opts) => (0, moost.defineInterceptorFn)((bef
|
|
|
199
330
|
if (!status.isDefined || opts?.force) status.value = code;
|
|
200
331
|
});
|
|
201
332
|
});
|
|
202
|
-
|
|
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) {
|
|
203
352
|
return (0, moost.Intercept)(setStatusInterceptor(...args));
|
|
204
353
|
}
|
|
205
354
|
|
|
206
355
|
//#endregion
|
|
207
356
|
//#region packages/event-http/src/decorators/limits.decorator.ts
|
|
208
|
-
|
|
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)(() => {
|
|
209
363
|
(0, __wooksjs_event_http.useRequest)().setMaxInflated(n);
|
|
210
|
-
},
|
|
211
|
-
|
|
364
|
+
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
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)(() => {
|
|
212
371
|
(0, __wooksjs_event_http.useRequest)().setMaxCompressed(n);
|
|
213
|
-
},
|
|
214
|
-
|
|
372
|
+
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
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)(() => {
|
|
215
379
|
(0, __wooksjs_event_http.useRequest)().setReadTimeoutMs(n);
|
|
216
|
-
},
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
380
|
+
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
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));
|
|
220
396
|
|
|
221
397
|
//#endregion
|
|
222
398
|
//#region packages/event-http/src/event-http.ts
|
|
@@ -232,7 +408,31 @@ function _define_property(obj, key, value) {
|
|
|
232
408
|
}
|
|
233
409
|
const LOGGER_TITLE = "moost-http";
|
|
234
410
|
const CONTEXT_TYPE = "HTTP";
|
|
235
|
-
|
|
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 {
|
|
236
436
|
getHttpApp() {
|
|
237
437
|
return this.httpApp;
|
|
238
438
|
}
|
|
@@ -297,7 +497,7 @@ var MoostHttp = class {
|
|
|
297
497
|
methods.DELETE = pathBuilder;
|
|
298
498
|
} else methods[handler.method] = pathBuilder;
|
|
299
499
|
}
|
|
300
|
-
opts.logHandler(
|
|
500
|
+
opts.logHandler(`[36m(${handler.method})[32m${targetPath}`);
|
|
301
501
|
const args = routerBinding.getArgs();
|
|
302
502
|
const params = {};
|
|
303
503
|
args.forEach((a) => params[a] = `{${a}}`);
|
|
@@ -319,46 +519,46 @@ var MoostHttp = class {
|
|
|
319
519
|
};
|
|
320
520
|
|
|
321
521
|
//#endregion
|
|
322
|
-
exports.All = All
|
|
323
|
-
exports.Authorization = Authorization
|
|
324
|
-
exports.Body = Body
|
|
325
|
-
exports.BodyReadTimeoutMs = BodyReadTimeoutMs
|
|
326
|
-
exports.BodySizeLimit = BodySizeLimit
|
|
327
|
-
exports.CompressedBodySizeLimit = CompressedBodySizeLimit
|
|
328
|
-
exports.Cookie = Cookie
|
|
329
|
-
exports.CookieAttrsHook = CookieAttrsHook
|
|
330
|
-
exports.CookieHook = CookieHook
|
|
331
|
-
exports.Delete = Delete
|
|
332
|
-
exports.Get = Get
|
|
333
|
-
exports.Header = Header
|
|
334
|
-
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;
|
|
335
535
|
Object.defineProperty(exports, 'HttpError', {
|
|
336
536
|
enumerable: true,
|
|
337
537
|
get: function () {
|
|
338
538
|
return __wooksjs_event_http.HttpError;
|
|
339
539
|
}
|
|
340
540
|
});
|
|
341
|
-
exports.HttpMethod = HttpMethod
|
|
342
|
-
exports.Ip = Ip
|
|
343
|
-
exports.IpList = IpList
|
|
344
|
-
exports.Method = Method
|
|
345
|
-
exports.MoostHttp = MoostHttp
|
|
346
|
-
exports.Patch = Patch
|
|
347
|
-
exports.Post = Post
|
|
348
|
-
exports.Put = Put
|
|
349
|
-
exports.Query = Query
|
|
350
|
-
exports.RawBody = RawBody
|
|
351
|
-
exports.Req = Req
|
|
352
|
-
exports.ReqId = ReqId
|
|
353
|
-
exports.Res = Res
|
|
354
|
-
exports.SetCookie = SetCookie
|
|
355
|
-
exports.SetHeader = SetHeader
|
|
356
|
-
exports.SetStatus = SetStatus
|
|
357
|
-
exports.StatusHook = StatusHook
|
|
358
|
-
exports.Url = Url
|
|
359
|
-
exports.globalBodyReadTimeoutMs = globalBodyReadTimeoutMs
|
|
360
|
-
exports.globalBodySizeLimit = globalBodySizeLimit
|
|
361
|
-
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;
|
|
362
562
|
Object.defineProperty(exports, 'useHttpContext', {
|
|
363
563
|
enumerable: true,
|
|
364
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';
|
|
4
|
+
import * as moost from 'moost';
|
|
5
5
|
import { TInterceptorFn, TMoostAdapter, Moost, TConsoleBase, TMoostAdapterOptions } from 'moost';
|
|
6
|
-
import * as packages_moost_dist from 'packages/moost/dist';
|
|
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
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
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,8 +1,7 @@
|
|
|
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
|
-
import { Intercept as Intercept$1, TInterceptorPriority as TInterceptorPriority$1, defineInterceptorFn as defineInterceptorFn$1 } from "packages/moost/dist";
|
|
6
5
|
import { createProvideRegistry } from "@prostojs/infact";
|
|
7
6
|
import { Server } from "http";
|
|
8
7
|
import { Server as Server$1 } from "https";
|
|
@@ -24,7 +23,11 @@ const Patch = (path) => HttpMethod("PATCH", path);
|
|
|
24
23
|
|
|
25
24
|
//#endregion
|
|
26
25
|
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Hook to the Response Status
|
|
28
|
+
* @decorator
|
|
29
|
+
* @paramType TStatusHook
|
|
30
|
+
*/ const StatusHook = () => Resolve((metas, level) => {
|
|
28
31
|
const hook = useStatus();
|
|
29
32
|
if (level === "PARAM") return hook;
|
|
30
33
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -36,7 +39,12 @@ const StatusHook = () => Resolve((metas, level) => {
|
|
|
36
39
|
return typeof initialValue === "number" ? initialValue : 200;
|
|
37
40
|
}
|
|
38
41
|
}, "statusCode");
|
|
39
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Hook to the Response Header
|
|
44
|
+
* @decorator
|
|
45
|
+
* @param name - header name
|
|
46
|
+
* @paramType THeaderHook
|
|
47
|
+
*/ const HeaderHook = (name) => Resolve((metas, level) => {
|
|
40
48
|
const hook = useSetHeader(name);
|
|
41
49
|
if (level === "PARAM") return hook;
|
|
42
50
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -48,7 +56,12 @@ const HeaderHook = (name) => Resolve((metas, level) => {
|
|
|
48
56
|
return typeof initialValue === "string" ? initialValue : "";
|
|
49
57
|
}
|
|
50
58
|
}, name);
|
|
51
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Hook to the Response Cookie
|
|
61
|
+
* @decorator
|
|
62
|
+
* @param name - cookie name
|
|
63
|
+
* @paramType TCookieHook
|
|
64
|
+
*/ const CookieHook = (name) => Resolve((metas, level) => {
|
|
52
65
|
const hook = useSetCookie(name);
|
|
53
66
|
if (level === "PARAM") return hook;
|
|
54
67
|
if (level === "PROP" && metas.instance && metas.key) {
|
|
@@ -60,7 +73,12 @@ const CookieHook = (name) => Resolve((metas, level) => {
|
|
|
60
73
|
return typeof initialValue === "string" ? initialValue : "";
|
|
61
74
|
}
|
|
62
75
|
}, name);
|
|
63
|
-
|
|
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) => {
|
|
64
82
|
const hook = useSetCookie(name);
|
|
65
83
|
if (level === "PARAM") return attachHook({}, {
|
|
66
84
|
get: () => hook.attrs,
|
|
@@ -75,28 +93,49 @@ const CookieAttrsHook = (name) => Resolve((metas, level) => {
|
|
|
75
93
|
return typeof initialValue === "object" ? initialValue : {};
|
|
76
94
|
}
|
|
77
95
|
}, name);
|
|
78
|
-
|
|
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) {
|
|
79
102
|
return Resolve(() => {
|
|
80
103
|
const auth = useAuthorization();
|
|
81
104
|
switch (name) {
|
|
82
|
-
case "username": return auth.isBasic() ? auth.basicCredentials()?.username :
|
|
83
|
-
case "password": return auth.isBasic() ? auth.basicCredentials()?.password :
|
|
84
|
-
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;
|
|
85
108
|
case "raw": return auth.authRawCredentials();
|
|
86
109
|
case "type": return auth.authType();
|
|
110
|
+
default: return void 0;
|
|
87
111
|
}
|
|
88
112
|
}, "authorization");
|
|
89
113
|
}
|
|
90
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Get Request Header Value
|
|
116
|
+
* @decorator
|
|
117
|
+
* @param name - header name
|
|
118
|
+
* @paramType string
|
|
119
|
+
*/ function Header(name) {
|
|
91
120
|
return Resolve(() => {
|
|
92
121
|
const headers = useHeaders();
|
|
93
122
|
return headers[name];
|
|
94
123
|
}, `header: ${name}`);
|
|
95
124
|
}
|
|
96
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Get Request Cookie Value
|
|
127
|
+
* @decorator
|
|
128
|
+
* @param name - cookie name
|
|
129
|
+
* @paramType string
|
|
130
|
+
*/ function Cookie(name) {
|
|
97
131
|
return Resolve(() => useCookies().getCookie(name), `cookie: ${name}`);
|
|
98
132
|
}
|
|
99
|
-
|
|
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) {
|
|
100
139
|
const isItem = !!name;
|
|
101
140
|
const _name = isItem ? name : "Query";
|
|
102
141
|
return getMoostMate().apply(getMoostMate().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), getMoostMate().decorate("paramName", _name), Resolve(() => {
|
|
@@ -104,37 +143,74 @@ function Query(name) {
|
|
|
104
143
|
if (isItem) {
|
|
105
144
|
const p = urlSearchParams();
|
|
106
145
|
const value = p.get(name);
|
|
107
|
-
return value === null ?
|
|
146
|
+
return value === null ? void 0 : value;
|
|
108
147
|
}
|
|
109
148
|
const json = jsonSearchParams();
|
|
110
|
-
return Object.keys(json).length > 0 ? json :
|
|
149
|
+
return Object.keys(json).length > 0 ? json : void 0;
|
|
111
150
|
}, _name));
|
|
112
151
|
}
|
|
113
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Get Requested URL
|
|
154
|
+
* @decorator
|
|
155
|
+
* @paramType string
|
|
156
|
+
*/ function Url() {
|
|
114
157
|
return Resolve(() => useRequest().url || "", "url");
|
|
115
158
|
}
|
|
116
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Get Requested HTTP Method
|
|
161
|
+
* @decorator
|
|
162
|
+
* @paramType string
|
|
163
|
+
*/ function Method() {
|
|
117
164
|
return Resolve(() => useRequest().method, "http_method");
|
|
118
165
|
}
|
|
119
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Get Raw Request Instance
|
|
168
|
+
* @decorator
|
|
169
|
+
* @paramType IncomingMessage
|
|
170
|
+
*/ function Req() {
|
|
120
171
|
return Resolve(() => useRequest().rawRequest, "request");
|
|
121
172
|
}
|
|
122
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Get Raw Response Instance
|
|
175
|
+
* @decorator
|
|
176
|
+
* @param opts (optional) { passthrough: boolean }
|
|
177
|
+
* @paramType ServerResponse
|
|
178
|
+
*/ function Res(opts) {
|
|
123
179
|
return Resolve(() => useResponse().rawResponse(opts), "response");
|
|
124
180
|
}
|
|
125
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Get Request Unique Identificator (UUID)
|
|
183
|
+
* @decorator
|
|
184
|
+
* @paramType string
|
|
185
|
+
*/ function ReqId() {
|
|
126
186
|
return Resolve(() => useRequest().reqId(), "reqId");
|
|
127
187
|
}
|
|
128
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Get Request IP Address
|
|
190
|
+
* @decorator
|
|
191
|
+
* @paramType string
|
|
192
|
+
*/ function Ip(opts) {
|
|
129
193
|
return Resolve(() => useRequest().getIp(opts), "ip");
|
|
130
194
|
}
|
|
131
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Get Request IP Address list
|
|
197
|
+
* @decorator
|
|
198
|
+
* @paramType string[]
|
|
199
|
+
*/ function IpList() {
|
|
132
200
|
return Resolve(() => useRequest().getIpList(), "ipList");
|
|
133
201
|
}
|
|
134
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Get Parsed Request Body
|
|
204
|
+
* @decorator
|
|
205
|
+
* @paramType object | string | unknown
|
|
206
|
+
*/ function Body() {
|
|
135
207
|
return getMoostMate().apply(getMoostMate().decorate("paramSource", "BODY"), Resolve(() => useBody().parseBody(), "body"));
|
|
136
208
|
}
|
|
137
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Get Raw Request Body Buffer
|
|
211
|
+
* @decorator
|
|
212
|
+
* @paramType Promise<Buffer>
|
|
213
|
+
*/ function RawBody() {
|
|
138
214
|
return Resolve(() => useBody().rawBody(), "body");
|
|
139
215
|
}
|
|
140
216
|
|
|
@@ -153,7 +229,43 @@ const setHeaderInterceptor = (name, value, opts) => {
|
|
|
153
229
|
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
154
230
|
return fn;
|
|
155
231
|
};
|
|
156
|
-
|
|
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) {
|
|
157
269
|
return Intercept(setHeaderInterceptor(...args));
|
|
158
270
|
}
|
|
159
271
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
@@ -166,7 +278,27 @@ const setCookieInterceptor = (name, value, attrs) => {
|
|
|
166
278
|
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
167
279
|
return fn;
|
|
168
280
|
};
|
|
169
|
-
|
|
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) {
|
|
170
302
|
return Intercept(setCookieInterceptor(...args));
|
|
171
303
|
}
|
|
172
304
|
const setStatusInterceptor = (code, opts) => defineInterceptorFn((before, after) => {
|
|
@@ -175,24 +307,69 @@ const setStatusInterceptor = (code, opts) => defineInterceptorFn((before, after)
|
|
|
175
307
|
if (!status.isDefined || opts?.force) status.value = code;
|
|
176
308
|
});
|
|
177
309
|
});
|
|
178
|
-
|
|
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) {
|
|
179
329
|
return Intercept(setStatusInterceptor(...args));
|
|
180
330
|
}
|
|
181
331
|
|
|
182
332
|
//#endregion
|
|
183
333
|
//#region packages/event-http/src/decorators/limits.decorator.ts
|
|
184
|
-
|
|
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(() => {
|
|
185
340
|
useRequest().setMaxInflated(n);
|
|
186
|
-
}, TInterceptorPriority
|
|
187
|
-
|
|
341
|
+
}, TInterceptorPriority.BEFORE_ALL);
|
|
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(() => {
|
|
188
348
|
useRequest().setMaxCompressed(n);
|
|
189
|
-
}, TInterceptorPriority
|
|
190
|
-
|
|
349
|
+
}, TInterceptorPriority.BEFORE_ALL);
|
|
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(() => {
|
|
191
356
|
useRequest().setReadTimeoutMs(n);
|
|
192
|
-
}, TInterceptorPriority
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
357
|
+
}, TInterceptorPriority.BEFORE_ALL);
|
|
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));
|
|
196
373
|
|
|
197
374
|
//#endregion
|
|
198
375
|
//#region packages/event-http/src/event-http.ts
|
|
@@ -208,7 +385,31 @@ function _define_property(obj, key, value) {
|
|
|
208
385
|
}
|
|
209
386
|
const LOGGER_TITLE = "moost-http";
|
|
210
387
|
const CONTEXT_TYPE = "HTTP";
|
|
211
|
-
|
|
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 {
|
|
212
413
|
getHttpApp() {
|
|
213
414
|
return this.httpApp;
|
|
214
415
|
}
|
|
@@ -223,7 +424,7 @@ var MoostHttp = class {
|
|
|
223
424
|
loggerTitle: LOGGER_TITLE,
|
|
224
425
|
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
225
426
|
getControllerInstance: () => this.moost,
|
|
226
|
-
callControllerMethod: () => new HttpError(404, "Resource Not Found"),
|
|
427
|
+
callControllerMethod: () => new HttpError$1(404, "Resource Not Found"),
|
|
227
428
|
targetPath: "",
|
|
228
429
|
handlerType: "__SYSTEM__"
|
|
229
430
|
})();
|
|
@@ -273,7 +474,7 @@ var MoostHttp = class {
|
|
|
273
474
|
methods.DELETE = pathBuilder;
|
|
274
475
|
} else methods[handler.method] = pathBuilder;
|
|
275
476
|
}
|
|
276
|
-
opts.logHandler(
|
|
477
|
+
opts.logHandler(`[36m(${handler.method})[32m${targetPath}`);
|
|
277
478
|
const args = routerBinding.getArgs();
|
|
278
479
|
const params = {};
|
|
279
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",
|