@moostjs/event-http 0.3.10 → 0.3.12

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { WooksHttp, createHttpApp, useRequest, HttpError, useHttpContext, useHeaders, EHttpStatusCode, WooksURLSearchParams, useStatus, useSetHeader, useSetCookie, useAuthorization, useCookies, useSearchParams, useResponse, useSetCookies } from '@wooksjs/event-http';
1
+ import { WooksHttp, createHttpApp, HttpError, useRequest, useHttpContext, useHeaders, EHttpStatusCode, WooksURLSearchParams, useStatus, useSetHeader, useSetCookie, useAuthorization, useCookies, useSearchParams, useResponse, useSetCookies } from '@wooksjs/event-http';
2
2
  export { HttpError, useHttpContext } from '@wooksjs/event-http';
3
3
  import { defineMoostEventHandler, getMoostMate, Resolve, Intercept, TInterceptorPriority, defineInterceptorFn } from 'moost';
4
4
  import { createProvideRegistry } from '@prostojs/infact';
@@ -6,707 +6,458 @@ import { Server } from 'http';
6
6
  import { Server as Server$1 } from 'https';
7
7
  import { attachHook } from '@wooksjs/event-core';
8
8
 
9
- /******************************************************************************
10
- Copyright (c) Microsoft Corporation.
11
-
12
- Permission to use, copy, modify, and/or distribute this software for any
13
- purpose with or without fee is hereby granted.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21
- PERFORMANCE OF THIS SOFTWARE.
22
- ***************************************************************************** */
23
- /* global Reflect, Promise */
24
-
25
-
26
- function __awaiter$1(thisArg, _arguments, P, generator) {
27
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
- return new (P || (P = Promise))(function (resolve, reject) {
29
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
- step((generator = generator.apply(thisArg, _arguments || [])).next());
33
- });
9
+ const LOGGER_TITLE = 'moost-http';
10
+ const CONTEXT_TYPE = 'HTTP';
11
+ class MoostHttp {
12
+ constructor(httpApp) {
13
+ this.pathBuilders = {};
14
+ if (httpApp && httpApp instanceof WooksHttp) {
15
+ this.httpApp = httpApp;
16
+ }
17
+ else if (httpApp) {
18
+ this.httpApp = createHttpApp({
19
+ ...httpApp,
20
+ onNotFound: this.onNotFound.bind(this),
21
+ });
22
+ }
23
+ else {
24
+ this.httpApp = createHttpApp({
25
+ onNotFound: this.onNotFound.bind(this),
26
+ });
27
+ }
28
+ }
29
+ getHttpApp() {
30
+ return this.httpApp;
31
+ }
32
+ getServerCb() {
33
+ return this.httpApp.getServerCb();
34
+ }
35
+ listen(...args) {
36
+ return this.httpApp.listen(...args);
37
+ }
38
+ async onNotFound() {
39
+ const response = await defineMoostEventHandler({
40
+ loggerTitle: LOGGER_TITLE,
41
+ getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
42
+ getControllerInstance: () => this.moost,
43
+ callControllerMethod: () => undefined,
44
+ })();
45
+ if (!response) {
46
+ throw new HttpError(404, 'Resource Not Found');
47
+ }
48
+ return response;
49
+ }
50
+ onInit(moost) {
51
+ this.moost = moost;
52
+ }
53
+ getProvideRegistry() {
54
+ return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [
55
+ Server,
56
+ () => this.getHttpApp().getServer(),
57
+ ], [
58
+ Server$1,
59
+ () => this.getHttpApp().getServer(),
60
+ ]);
61
+ }
62
+ getLogger() {
63
+ return this.getHttpApp().getLogger('moost-http');
64
+ }
65
+ bindHandler(opts) {
66
+ let fn;
67
+ for (const handler of opts.handlers) {
68
+ if (handler.type !== 'HTTP')
69
+ continue;
70
+ const httpPath = handler.path;
71
+ const path = typeof httpPath === 'string'
72
+ ? httpPath
73
+ : typeof opts.method === 'string'
74
+ ? opts.method
75
+ : '';
76
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`;
77
+ if (!fn) {
78
+ fn = defineMoostEventHandler({
79
+ contextType: CONTEXT_TYPE,
80
+ loggerTitle: LOGGER_TITLE,
81
+ getIterceptorHandler: opts.getIterceptorHandler,
82
+ getControllerInstance: opts.getInstance,
83
+ controllerMethod: opts.method,
84
+ resolveArgs: opts.resolveArgs,
85
+ manualUnscope: true,
86
+ hooks: {
87
+ init: ({ unscope }) => {
88
+ const { rawRequest } = useRequest();
89
+ rawRequest.on('end', unscope);
90
+ },
91
+ },
92
+ });
93
+ }
94
+ const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
95
+ const { getPath: pathBuilder } = routerBinding;
96
+ const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) ||
97
+ {};
98
+ const id = (methodMeta.id || opts.method);
99
+ if (id) {
100
+ const methods = (this.pathBuilders[id] =
101
+ this.pathBuilders[id] || {});
102
+ if (handler.method === '*') {
103
+ methods.GET = pathBuilder;
104
+ methods.PUT = pathBuilder;
105
+ methods.PATCH = pathBuilder;
106
+ methods.POST = pathBuilder;
107
+ methods.DELETE = pathBuilder;
108
+ }
109
+ else {
110
+ methods[handler.method] = pathBuilder;
111
+ }
112
+ }
113
+ opts.logHandler(`${''}(${handler.method})${''}${targetPath}`);
114
+ const args = routerBinding.getArgs();
115
+ const params = {};
116
+ args.forEach(a => params[a] = `{${a}}`);
117
+ opts.register(handler, routerBinding.getPath(params), args);
118
+ }
119
+ }
34
120
  }
35
121
 
36
- const LOGGER_TITLE = 'moost-http';
37
- const CONTEXT_TYPE = 'HTTP';
38
- /**
39
- * ## Moost HTTP Adapter
40
- *
41
- * Moost Adapter for HTTP events
42
- *
43
- * ```ts
44
- * │ // HTTP server example
45
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
46
- * │ import { Moost, Param } from 'moost'
47
- * │
48
- * │ class MyServer extends Moost {
49
- * │ @Get('test/:name')
50
- * │ test(@Param('name') name: string) {
51
- * │ return { message: `Hello ${name}!` }
52
- * │ }
53
- * │ }
54
- * │
55
- * │ const app = new MyServer()
56
- * │ const http = new MoostHttp()
57
- * │ app.adapter(http).listen(3000, () => {
58
- * │ app.getLogger('MyApp').log('Up on port 3000')
59
- * │ })
60
- * │ app.init()
61
- * ```
62
- */
63
- class MoostHttp {
64
- constructor(httpApp) {
65
- this.pathBuilders = {};
66
- if (httpApp && httpApp instanceof WooksHttp) {
67
- this.httpApp = httpApp;
68
- }
69
- else if (httpApp) {
70
- this.httpApp = createHttpApp(Object.assign(Object.assign({}, httpApp), { onNotFound: this.onNotFound.bind(this) }));
71
- }
72
- else {
73
- this.httpApp = createHttpApp({
74
- onNotFound: this.onNotFound.bind(this),
75
- });
76
- }
77
- }
78
- getHttpApp() {
79
- return this.httpApp;
80
- }
81
- getServerCb() {
82
- return this.httpApp.getServerCb();
83
- }
84
- listen(...args) {
85
- return this.httpApp.listen(...args);
86
- }
87
- onNotFound() {
88
- return __awaiter$1(this, void 0, void 0, function* () {
89
- const response = yield defineMoostEventHandler({
90
- loggerTitle: LOGGER_TITLE,
91
- getIterceptorHandler: () => { var _a; return (_a = this.moost) === null || _a === void 0 ? void 0 : _a.getGlobalInterceptorHandler(); },
92
- getControllerInstance: () => this.moost,
93
- callControllerMethod: () => undefined,
94
- })();
95
- if (!response) {
96
- throw new HttpError(404, 'Resource Not Found');
97
- }
98
- return response;
99
- });
100
- }
101
- onInit(moost) {
102
- this.moost = moost;
103
- }
104
- getProvideRegistry() {
105
- return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [
106
- Server,
107
- () => this.getHttpApp().getServer(),
108
- ], [
109
- Server$1,
110
- () => this.getHttpApp().getServer(),
111
- ]);
112
- }
113
- getLogger() {
114
- return this.getHttpApp().getLogger('moost-http');
115
- }
116
- bindHandler(opts) {
117
- let fn;
118
- for (const handler of opts.handlers) {
119
- if (handler.type !== 'HTTP')
120
- continue;
121
- const httpPath = handler.path;
122
- const path = typeof httpPath === 'string'
123
- ? httpPath
124
- : typeof opts.method === 'string'
125
- ? opts.method
126
- : '';
127
- const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
128
- if (!fn) {
129
- fn = defineMoostEventHandler({
130
- contextType: CONTEXT_TYPE,
131
- loggerTitle: LOGGER_TITLE,
132
- getIterceptorHandler: opts.getIterceptorHandler,
133
- getControllerInstance: opts.getInstance,
134
- controllerMethod: opts.method,
135
- resolveArgs: opts.resolveArgs,
136
- manualUnscope: true,
137
- hooks: {
138
- init: ({ unscope }) => {
139
- const { rawRequest } = useRequest();
140
- rawRequest.on('end', unscope); // will unscope on request end
141
- },
142
- },
143
- });
144
- }
145
- const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
146
- const { getPath: pathBuilder } = routerBinding;
147
- const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) ||
148
- {};
149
- const id = (methodMeta.id || opts.method);
150
- if (id) {
151
- const methods = (this.pathBuilders[id] =
152
- this.pathBuilders[id] || {});
153
- if (handler.method === '*') {
154
- methods.GET = pathBuilder;
155
- methods.PUT = pathBuilder;
156
- methods.PATCH = pathBuilder;
157
- methods.POST = pathBuilder;
158
- methods.DELETE = pathBuilder;
159
- }
160
- else {
161
- methods[handler.method] = pathBuilder;
162
- }
163
- }
164
- opts.logHandler(`${''}(${handler.method})${''}${targetPath}`);
165
- const args = routerBinding.getArgs();
166
- const params = {};
167
- args.forEach(a => params[a] = `{${a}}`);
168
- opts.register(handler, routerBinding.getPath(params), args);
169
- }
170
- }
122
+ function HttpMethod(method, path) {
123
+ return getMoostMate().decorate('handlers', { method, path, type: 'HTTP' }, true);
171
124
  }
172
-
173
- function HttpMethod(method, path) {
174
- return getMoostMate().decorate('handlers', { method, path, type: 'HTTP' }, true);
175
- }
176
- const All = (path) => HttpMethod('*', path);
177
- const Get = (path) => HttpMethod('GET', path);
178
- const Post = (path) => HttpMethod('POST', path);
179
- const Put = (path) => HttpMethod('PUT', path);
180
- const Delete = (path) => HttpMethod('DELETE', path);
125
+ const All = (path) => HttpMethod('*', path);
126
+ const Get = (path) => HttpMethod('GET', path);
127
+ const Post = (path) => HttpMethod('POST', path);
128
+ const Put = (path) => HttpMethod('PUT', path);
129
+ const Delete = (path) => HttpMethod('DELETE', path);
181
130
  const Patch = (path) => HttpMethod('PATCH', path);
182
131
 
183
- /******************************************************************************
184
- Copyright (c) Microsoft Corporation.
185
-
186
- Permission to use, copy, modify, and/or distribute this software for any
187
- purpose with or without fee is hereby granted.
188
-
189
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
190
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
191
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
192
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
193
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
194
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
195
- PERFORMANCE OF THIS SOFTWARE.
196
- ***************************************************************************** */
197
-
198
- function __awaiter(thisArg, _arguments, P, generator) {
199
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
200
- return new (P || (P = Promise))(function (resolve, reject) {
201
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
202
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
203
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
204
- step((generator = generator.apply(thisArg, _arguments || [])).next());
205
- });
132
+ const compressors = {
133
+ identity: {
134
+ compress: (v) => v,
135
+ uncompress: (v) => v,
136
+ },
137
+ };
138
+ async function uncompressBody(encodings, body) {
139
+ let newBody = body;
140
+ for (const e of encodings.reverse()) {
141
+ if (!compressors[e]) {
142
+ throw new Error(`Usupported compression type "${e}".`);
143
+ }
144
+ newBody = await compressors[e].uncompress(body);
145
+ }
146
+ return newBody;
206
147
  }
207
148
 
208
- const compressors = {
209
- identity: {
210
- compress: (v) => v,
211
- uncompress: (v) => v,
212
- },
213
- };
214
- function uncompressBody(encodings, body) {
215
- return __awaiter(this, void 0, void 0, function* () {
216
- let newBody = body;
217
- for (const e of encodings.reverse()) {
218
- if (!compressors[e]) {
219
- throw new Error(`Usupported compression type "${e}".`);
220
- }
221
- newBody = yield compressors[e].uncompress(body);
222
- }
223
- return newBody;
224
- });
149
+ function useBody() {
150
+ const { store } = useHttpContext();
151
+ const { init } = store('request');
152
+ const { rawBody } = useRequest();
153
+ const { 'content-type': contentType, 'content-encoding': contentEncoding } = useHeaders();
154
+ function contentIs(type) {
155
+ return (contentType || '').indexOf(type) >= 0;
156
+ }
157
+ const isJson = () => init('isJson', () => contentIs('application/json'));
158
+ const isHtml = () => init('isHtml', () => contentIs('text/html'));
159
+ const isXml = () => init('isXml', () => contentIs('text/xml'));
160
+ const isText = () => init('isText', () => contentIs('text/plain'));
161
+ const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
162
+ const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
163
+ const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
164
+ const isCompressed = () => init('isCompressed', () => {
165
+ const parts = contentEncodings();
166
+ for (const p of parts) {
167
+ if (['deflate', 'gzip', 'br'].includes(p))
168
+ return true;
169
+ }
170
+ return false;
171
+ });
172
+ const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
173
+ .split(',')
174
+ .map((p) => p.trim())
175
+ .filter((p) => !!p));
176
+ const parseBody = () => init('parsed', async () => {
177
+ const body = await uncompressBody(contentEncodings(), (await rawBody()).toString());
178
+ if (isJson())
179
+ return jsonParser(body);
180
+ else if (isFormData())
181
+ return formDataParser(body);
182
+ else if (isUrlencoded())
183
+ return urlEncodedParser(body);
184
+ else if (isBinary())
185
+ return textParser(body);
186
+ else
187
+ return textParser(body);
188
+ });
189
+ function jsonParser(v) {
190
+ try {
191
+ return JSON.parse(v);
192
+ }
193
+ catch (e) {
194
+ throw new HttpError(400, e.message);
195
+ }
196
+ }
197
+ function textParser(v) {
198
+ return v;
199
+ }
200
+ function formDataParser(v) {
201
+ const boundary = '--' +
202
+ (/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
203
+ if (!boundary)
204
+ throw new HttpError(EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
205
+ const parts = v.trim().split(boundary);
206
+ const result = {};
207
+ let key = '';
208
+ let partContentType = 'text/plain';
209
+ for (const part of parts) {
210
+ parsePart();
211
+ key = '';
212
+ partContentType = 'text/plain';
213
+ let valueMode = false;
214
+ const lines = part
215
+ .trim()
216
+ .split(/\n/g)
217
+ .map((s) => s.trim());
218
+ for (const line of lines) {
219
+ if (valueMode) {
220
+ if (!result[key]) {
221
+ result[key] = line;
222
+ }
223
+ else {
224
+ result[key] += '\n' + line;
225
+ }
226
+ }
227
+ else {
228
+ if (!line || line === '--') {
229
+ valueMode = !!key;
230
+ if (valueMode) {
231
+ key = key.replace(/^["']/, '').replace(/["']$/, '');
232
+ }
233
+ continue;
234
+ }
235
+ if (line
236
+ .toLowerCase()
237
+ .startsWith('content-disposition: form-data;')) {
238
+ key = (/name=([^;]+)/.exec(line) || [])[1];
239
+ if (!key)
240
+ throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
241
+ continue;
242
+ }
243
+ if (line.toLowerCase().startsWith('content-type:')) {
244
+ partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
245
+ if (!partContentType)
246
+ throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
247
+ continue;
248
+ }
249
+ }
250
+ }
251
+ }
252
+ parsePart();
253
+ function parsePart() {
254
+ if (key) {
255
+ if (partContentType.indexOf('application/json') >= 0) {
256
+ result[key] = JSON.parse(result[key]);
257
+ }
258
+ }
259
+ }
260
+ return result;
261
+ }
262
+ function urlEncodedParser(v) {
263
+ return new WooksURLSearchParams(v.trim()).toJson();
264
+ }
265
+ return {
266
+ isJson,
267
+ isHtml,
268
+ isXml,
269
+ isText,
270
+ isBinary,
271
+ isFormData,
272
+ isUrlencoded,
273
+ isCompressed,
274
+ contentEncodings,
275
+ parseBody,
276
+ rawBody,
277
+ };
225
278
  }
226
279
 
227
- function useBody() {
228
- const { store } = useHttpContext();
229
- const { init } = store('request');
230
- const { rawBody } = useRequest();
231
- const { 'content-type': contentType, 'content-encoding': contentEncoding } = useHeaders();
232
- function contentIs(type) {
233
- return (contentType || '').indexOf(type) >= 0;
234
- }
235
- const isJson = () => init('isJson', () => contentIs('application/json'));
236
- const isHtml = () => init('isHtml', () => contentIs('text/html'));
237
- const isXml = () => init('isXml', () => contentIs('text/xml'));
238
- const isText = () => init('isText', () => contentIs('text/plain'));
239
- const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
240
- const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
241
- const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
242
- const isCompressed = () => init('isCompressed', () => {
243
- const parts = contentEncodings();
244
- for (const p of parts) {
245
- if (['deflate', 'gzip', 'br'].includes(p))
246
- return true;
247
- }
248
- return false;
249
- });
250
- const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
251
- .split(',')
252
- .map((p) => p.trim())
253
- .filter((p) => !!p));
254
- const parseBody = () => init('parsed', () => __awaiter(this, void 0, void 0, function* () {
255
- const body = yield uncompressBody(contentEncodings(), (yield rawBody()).toString());
256
- if (isJson())
257
- return jsonParser(body);
258
- else if (isFormData())
259
- return formDataParser(body);
260
- else if (isUrlencoded())
261
- return urlEncodedParser(body);
262
- else if (isBinary())
263
- return textParser(body);
264
- else
265
- return textParser(body);
266
- }));
267
- function jsonParser(v) {
268
- try {
269
- return JSON.parse(v);
270
- }
271
- catch (e) {
272
- throw new HttpError(400, e.message);
273
- }
274
- }
275
- function textParser(v) {
276
- return v;
277
- }
278
- function formDataParser(v) {
279
- const boundary = '--' +
280
- (/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
281
- if (!boundary)
282
- throw new HttpError(EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
283
- const parts = v.trim().split(boundary);
284
- const result = {};
285
- let key = '';
286
- let partContentType = 'text/plain';
287
- for (const part of parts) {
288
- parsePart();
289
- key = '';
290
- partContentType = 'text/plain';
291
- let valueMode = false;
292
- const lines = part
293
- .trim()
294
- .split(/\n/g)
295
- .map((s) => s.trim());
296
- for (const line of lines) {
297
- if (valueMode) {
298
- if (!result[key]) {
299
- result[key] = line;
300
- }
301
- else {
302
- result[key] += '\n' + line;
303
- }
304
- }
305
- else {
306
- if (!line || line === '--') {
307
- valueMode = !!key;
308
- if (valueMode) {
309
- key = key.replace(/^["']/, '').replace(/["']$/, '');
310
- }
311
- continue;
312
- }
313
- if (line
314
- .toLowerCase()
315
- .startsWith('content-disposition: form-data;')) {
316
- key = (/name=([^;]+)/.exec(line) || [])[1];
317
- if (!key)
318
- throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
319
- continue;
320
- }
321
- if (line.toLowerCase().startsWith('content-type:')) {
322
- partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
323
- if (!partContentType)
324
- throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
325
- continue;
326
- }
327
- }
328
- }
329
- }
330
- parsePart();
331
- function parsePart() {
332
- if (key) {
333
- if (partContentType.indexOf('application/json') >= 0) {
334
- result[key] = JSON.parse(result[key]);
335
- }
336
- }
337
- }
338
- return result;
339
- }
340
- function urlEncodedParser(v) {
341
- return new WooksURLSearchParams(v.trim()).toJson();
342
- }
343
- return {
344
- isJson,
345
- isHtml,
346
- isXml,
347
- isText,
348
- isBinary,
349
- isFormData,
350
- isUrlencoded,
351
- isCompressed,
352
- contentEncodings,
353
- parseBody,
354
- rawBody,
355
- };
280
+ const StatusHook = () => Resolve((metas, level) => {
281
+ const hook = useStatus();
282
+ if (level === 'PARAM') {
283
+ return hook;
284
+ }
285
+ if (level === 'PROP' && metas.instance && metas.key) {
286
+ const initialValue = metas.instance[metas.key];
287
+ attachHook(metas.instance, {
288
+ get: () => hook.value,
289
+ set: (v) => (hook.value = v),
290
+ }, metas.key);
291
+ return typeof initialValue === 'number' ? initialValue : 200;
292
+ }
293
+ }, 'statusCode');
294
+ const HeaderHook = (name) => Resolve((metas, level) => {
295
+ const hook = useSetHeader(name);
296
+ if (level === 'PARAM') {
297
+ return hook;
298
+ }
299
+ if (level === 'PROP' && metas.instance && metas.key) {
300
+ const initialValue = metas.instance[metas.key];
301
+ attachHook(metas.instance, {
302
+ get: () => hook.value,
303
+ set: (v) => (hook.value = v),
304
+ }, metas.key);
305
+ return typeof initialValue === 'string' ? initialValue : '';
306
+ }
307
+ }, name);
308
+ const CookieHook = (name) => Resolve((metas, level) => {
309
+ const hook = useSetCookie(name);
310
+ if (level === 'PARAM') {
311
+ return hook;
312
+ }
313
+ if (level === 'PROP' && metas.instance && metas.key) {
314
+ const initialValue = metas.instance[metas.key];
315
+ attachHook(metas.instance, {
316
+ get: () => hook.value,
317
+ set: (v) => (hook.value = v),
318
+ }, metas.key);
319
+ return typeof initialValue === 'string' ? initialValue : '';
320
+ }
321
+ }, name);
322
+ const CookieAttrsHook = (name) => Resolve((metas, level) => {
323
+ const hook = useSetCookie(name);
324
+ if (level === 'PARAM') {
325
+ return attachHook({}, {
326
+ get: () => hook.attrs,
327
+ set: (v) => (hook.attrs = v),
328
+ });
329
+ }
330
+ if (level === 'PROP' && metas.instance && metas.key) {
331
+ const initialValue = metas.instance[metas.key];
332
+ attachHook(metas.instance, {
333
+ get: () => hook.attrs,
334
+ set: (v) => (hook.attrs = v),
335
+ }, metas.key);
336
+ return typeof initialValue === 'object' ? initialValue : {};
337
+ }
338
+ }, name);
339
+ function Authorization(name) {
340
+ return Resolve(() => {
341
+ const auth = useAuthorization();
342
+ switch (name) {
343
+ case 'username':
344
+ return auth.isBasic()
345
+ ? auth.basicCredentials()?.username
346
+ : undefined;
347
+ case 'password':
348
+ return auth.isBasic()
349
+ ? auth.basicCredentials()?.password
350
+ : undefined;
351
+ case 'bearer':
352
+ return auth.isBearer() ? auth.authorization : undefined;
353
+ case 'raw':
354
+ return auth.authRawCredentials();
355
+ case 'type':
356
+ return auth.authType();
357
+ }
358
+ }, 'authorization');
356
359
  }
357
-
358
- /**
359
- * Hook to the Response Status
360
- * @decorator
361
- * @paramType TStatusHook
362
- */
363
- const StatusHook = () => Resolve((metas, level) => {
364
- const hook = useStatus();
365
- if (level === 'PARAM') {
366
- return hook;
367
- }
368
- if (level === 'PROP' && metas.instance && metas.key) {
369
- const initialValue = metas.instance[metas.key];
370
- attachHook(metas.instance, {
371
- get: () => hook.value,
372
- set: (v) => (hook.value = v),
373
- }, metas.key);
374
- return typeof initialValue === 'number' ? initialValue : 200;
375
- }
376
- }, 'statusCode');
377
- /**
378
- * Hook to the Response Header
379
- * @decorator
380
- * @param name - header name
381
- * @paramType THeaderHook
382
- */
383
- const HeaderHook = (name) => Resolve((metas, level) => {
384
- const hook = useSetHeader(name);
385
- if (level === 'PARAM') {
386
- return hook;
387
- }
388
- if (level === 'PROP' && metas.instance && metas.key) {
389
- const initialValue = metas.instance[metas.key];
390
- attachHook(metas.instance, {
391
- get: () => hook.value,
392
- set: (v) => (hook.value = v),
393
- }, metas.key);
394
- return typeof initialValue === 'string' ? initialValue : '';
395
- }
396
- }, name);
397
- /**
398
- * Hook to the Response Cookie
399
- * @decorator
400
- * @param name - cookie name
401
- * @paramType TCookieHook
402
- */
403
- const CookieHook = (name) => Resolve((metas, level) => {
404
- const hook = useSetCookie(name);
405
- if (level === 'PARAM') {
406
- return hook;
407
- }
408
- if (level === 'PROP' && metas.instance && metas.key) {
409
- const initialValue = metas.instance[metas.key];
410
- attachHook(metas.instance, {
411
- get: () => hook.value,
412
- set: (v) => (hook.value = v),
413
- }, metas.key);
414
- return typeof initialValue === 'string' ? initialValue : '';
415
- }
416
- }, name);
417
- /**
418
- * Hook to the Response Cookie Attributes
419
- * @decorator
420
- * @param name - cookie name
421
- * @paramType TCookieAttributes
422
- */
423
- const CookieAttrsHook = (name) => Resolve((metas, level) => {
424
- const hook = useSetCookie(name);
425
- if (level === 'PARAM') {
426
- return attachHook({}, {
427
- get: () => hook.attrs,
428
- set: (v) => (hook.attrs = v),
429
- });
430
- }
431
- if (level === 'PROP' && metas.instance && metas.key) {
432
- const initialValue = metas.instance[metas.key];
433
- attachHook(metas.instance, {
434
- get: () => hook.attrs,
435
- set: (v) => (hook.attrs = v),
436
- }, metas.key);
437
- return typeof initialValue === 'object' ? initialValue : {};
438
- }
439
- }, name);
440
- /**
441
- * Parse Authorisation Header
442
- * @decorator
443
- * @param name - define what to take from the Auth header
444
- * @paramType string
445
- */
446
- function Authorization(name) {
447
- return Resolve(() => {
448
- var _a, _b;
449
- const auth = useAuthorization();
450
- switch (name) {
451
- case 'username':
452
- return auth.isBasic()
453
- ? (_a = auth.basicCredentials()) === null || _a === void 0 ? void 0 : _a.username
454
- : undefined;
455
- case 'password':
456
- return auth.isBasic()
457
- ? (_b = auth.basicCredentials()) === null || _b === void 0 ? void 0 : _b.password
458
- : undefined;
459
- case 'bearer':
460
- return auth.isBearer() ? auth.authorization : undefined;
461
- case 'raw':
462
- return auth.authRawCredentials();
463
- case 'type':
464
- return auth.authType();
465
- }
466
- }, 'authorization');
467
- }
468
- /**
469
- * Get Request Header Value
470
- * @decorator
471
- * @param name - header name
472
- * @paramType string
473
- */
474
- function Header(name) {
475
- return Resolve(() => {
476
- const headers = useHeaders();
477
- return headers[name];
478
- }, 'header: ' + name);
479
- }
480
- /**
481
- * Get Request Cookie Value
482
- * @decorator
483
- * @param name - cookie name
484
- * @paramType string
485
- */
486
- function Cookie(name) {
487
- return Resolve(() => useCookies().getCookie(name), 'cookie: ' + name);
488
- }
489
- /**
490
- * Get Query Item value or the whole parsed Query as an object
491
- * @decorator
492
- * @param name - query item name (optional)
493
- * @paramType string | object
494
- */
495
- function Query(name) {
496
- const isItem = !!name;
497
- const _name = isItem ? name : 'Query';
498
- return getMoostMate().apply(getMoostMate().decorate('paramSource', isItem ? 'QUERY_ITEM' : 'QUERY'), getMoostMate().decorate('paramName', _name), Resolve(() => {
499
- const { jsonSearchParams, urlSearchParams } = useSearchParams();
500
- if (isItem) {
501
- const p = urlSearchParams();
502
- const value = p.get(name);
503
- return value === null ? undefined : value;
504
- }
505
- const json = jsonSearchParams();
506
- return Object.keys(json).length ? json : undefined;
507
- }, _name));
508
- }
509
- /**
510
- * Get Requested URL
511
- * @decorator
512
- * @paramType string
513
- */
514
- function Url() {
515
- return Resolve(() => useRequest().url || '', 'url');
516
- }
517
- /**
518
- * Get Requested HTTP Method
519
- * @decorator
520
- * @paramType string
521
- */
522
- function Method() {
523
- return Resolve(() => useRequest().method, 'http_method');
524
- }
525
- /**
526
- * Get Raw Request Instance
527
- * @decorator
528
- * @paramType IncomingMessage
529
- */
530
- function Req() {
531
- return Resolve(() => useRequest().rawRequest, 'request');
532
- }
533
- /**
534
- * Get Raw Response Instance
535
- * @decorator
536
- * @param opts (optional) { passthrough: boolean }
537
- * @paramType ServerResponse
538
- */
539
- function Res(opts) {
540
- return Resolve(() => useResponse().rawResponse(opts), 'response');
541
- }
542
- /**
543
- * Get Request Unique Identificator (UUID)
544
- * @decorator
545
- * @paramType string
546
- */
547
- function ReqId() {
548
- return Resolve(() => useRequest().reqId(), 'reqId');
549
- }
550
- /**
551
- * Get Request IP Address
552
- * @decorator
553
- * @paramType string
554
- */
555
- function Ip(opts) {
556
- return Resolve(() => useRequest().getIp(opts), 'ip');
557
- }
558
- /**
559
- * Get Request IP Address list
560
- * @decorator
561
- * @paramType string[]
562
- */
563
- function IpList() {
564
- return Resolve(() => useRequest().getIpList(), 'ipList');
565
- }
566
- /**
567
- * Get Parsed Request Body
568
- * @decorator
569
- * @paramType object | string | unknown
570
- */
571
- function Body() {
572
- return getMoostMate().apply(getMoostMate().decorate('paramSource', 'BODY'), Resolve(() => useBody().parseBody(), 'body'));
573
- }
574
- /**
575
- * Get Raw Request Body Buffer
576
- * @decorator
577
- * @paramType Promise<Buffer>
578
- */
579
- function RawBody() {
580
- return Resolve(() => useBody().rawBody(), 'body');
360
+ function Header(name) {
361
+ return Resolve(() => {
362
+ const headers = useHeaders();
363
+ return headers[name];
364
+ }, 'header: ' + name);
365
+ }
366
+ function Cookie(name) {
367
+ return Resolve(() => useCookies().getCookie(name), 'cookie: ' + name);
368
+ }
369
+ function Query(name) {
370
+ const isItem = !!name;
371
+ const _name = isItem ? name : 'Query';
372
+ return getMoostMate().apply(getMoostMate().decorate('paramSource', isItem ? 'QUERY_ITEM' : 'QUERY'), getMoostMate().decorate('paramName', _name), Resolve(() => {
373
+ const { jsonSearchParams, urlSearchParams } = useSearchParams();
374
+ if (isItem) {
375
+ const p = urlSearchParams();
376
+ const value = p.get(name);
377
+ return value === null ? undefined : value;
378
+ }
379
+ const json = jsonSearchParams();
380
+ return Object.keys(json).length ? json : undefined;
381
+ }, _name));
382
+ }
383
+ function Url() {
384
+ return Resolve(() => useRequest().url || '', 'url');
385
+ }
386
+ function Method() {
387
+ return Resolve(() => useRequest().method, 'http_method');
388
+ }
389
+ function Req() {
390
+ return Resolve(() => useRequest().rawRequest, 'request');
391
+ }
392
+ function Res(opts) {
393
+ return Resolve(() => useResponse().rawResponse(opts), 'response');
394
+ }
395
+ function ReqId() {
396
+ return Resolve(() => useRequest().reqId(), 'reqId');
397
+ }
398
+ function Ip(opts) {
399
+ return Resolve(() => useRequest().getIp(opts), 'ip');
400
+ }
401
+ function IpList() {
402
+ return Resolve(() => useRequest().getIpList(), 'ipList');
403
+ }
404
+ function Body() {
405
+ return getMoostMate().apply(getMoostMate().decorate('paramSource', 'BODY'), Resolve(() => useBody().parseBody(), 'body'));
406
+ }
407
+ function RawBody() {
408
+ return Resolve(() => useBody().rawBody(), 'body');
581
409
  }
582
410
 
583
- const setHeaderInterceptor = (name, value, opts) => {
584
- const fn = (_before, after, onError) => {
585
- const h = useSetHeader(name);
586
- const status = useStatus();
587
- const cb = () => {
588
- if ((!h.value || (opts === null || opts === void 0 ? void 0 : opts.force)) &&
589
- (!(opts === null || opts === void 0 ? void 0 : opts.status) || opts.status === status.value)) {
590
- h.value = value;
591
- }
592
- };
593
- if ((opts === null || opts === void 0 ? void 0 : opts.when) !== 'error') {
594
- after(cb);
595
- }
596
- if ((opts === null || opts === void 0 ? void 0 : opts.when) === 'always' || (opts === null || opts === void 0 ? void 0 : opts.when) === 'error') {
597
- onError(cb);
598
- }
599
- };
600
- fn.priority = TInterceptorPriority.AFTER_ALL;
601
- return fn;
602
- };
603
- /**
604
- * Set Header for Request Handler
605
- *
606
- * ```ts
607
- * import { Get, SetHeader } from '@moostjs/event-http';
608
- * import { Controller } from 'moost';
609
- *
610
- * @Controller()
611
- * export class ExampleController {
612
- * @Get('test')
613
- * // setting header for request handler
614
- * @SetHeader('x-server', 'my-server')
615
- * testHandler() {
616
- * return '...'
617
- * }
618
- * }
619
- * ```
620
- *
621
- * ```ts
622
- * import { Get, SetHeader } from '@moostjs/event-http';
623
- * import { Controller } from 'moost';
624
- *
625
- * @Controller()
626
- * export class ExampleController {
627
- * @Get('test')
628
- * // setting header only if status = 400
629
- * @SetHeader('content-type', 'text/plain', { status: 400 })
630
- * testHandler() {
631
- * return '...'
632
- * }
633
- * }
634
- * ```
635
- *
636
- * @param name name of header
637
- * @param value value for header
638
- * @param options options { status?: number, force?: boolean }
639
- */
640
- function SetHeader(...args) {
641
- return Intercept(setHeaderInterceptor(...args));
642
- }
643
- const setCookieInterceptor = (name, value, attrs) => {
644
- const fn = (before, after) => {
645
- const { setCookie, getCookie } = useSetCookies();
646
- after(() => {
647
- if (!getCookie(name)) {
648
- setCookie(name, value, attrs);
649
- }
650
- });
651
- };
652
- fn.priority = TInterceptorPriority.AFTER_ALL;
653
- return fn;
654
- };
655
- /**
656
- * Set Cookie for Request Handler
657
- * ```ts
658
- * import { Get, SetCookie } from '@moostjs/event-http';
659
- * import { Controller } from 'moost';
660
- *
661
- * @Controller()
662
- * export class ExampleController {
663
- * @Get('test')
664
- * // setting 'my-cookie' = 'value' with maxAge of 10 minutes
665
- * @SetCookie('my-cookie', 'value', { maxAge: '10m' })
666
- * testHandler() {
667
- * return '...'
668
- * }
669
- * }
670
- * ```
671
- *
672
- * @param name name of cookie
673
- * @param value value for cookie
674
- * @param attrs cookie attributes
675
- */
676
- function SetCookie(...args) {
677
- return Intercept(setCookieInterceptor(...args));
678
- }
679
- const setStatusInterceptor = (code, opts) => {
680
- return defineInterceptorFn((before, after) => {
681
- const status = useStatus();
682
- after(() => {
683
- if (!status.isDefined || (opts === null || opts === void 0 ? void 0 : opts.force)) {
684
- status.value = code;
685
- }
686
- });
687
- });
688
- };
689
- /**
690
- * Set Response Status for Request Handler
691
- *
692
- * ```ts
693
- * import { Get, SetStatus } from '@moostjs/event-http';
694
- * import { Controller } from 'moost';
695
- *
696
- * @Controller()
697
- * export class ExampleController {
698
- * @Get('test')
699
- * @SetStatus(201)
700
- * testHandler() {
701
- * return '...'
702
- * }
703
- * }
704
- * ```
705
- * @param code number
706
- * @param opts optional { force?: boolean }
707
- */
708
- function SetStatus(...args) {
709
- return Intercept(setStatusInterceptor(...args));
411
+ const setHeaderInterceptor = (name, value, opts) => {
412
+ const fn = (_before, after, onError) => {
413
+ const h = useSetHeader(name);
414
+ const status = useStatus();
415
+ const cb = () => {
416
+ if ((!h.value || opts?.force) &&
417
+ (!opts?.status || opts.status === status.value)) {
418
+ h.value = value;
419
+ }
420
+ };
421
+ if (opts?.when !== 'error') {
422
+ after(cb);
423
+ }
424
+ if (opts?.when === 'always' || opts?.when === 'error') {
425
+ onError(cb);
426
+ }
427
+ };
428
+ fn.priority = TInterceptorPriority.AFTER_ALL;
429
+ return fn;
430
+ };
431
+ function SetHeader(...args) {
432
+ return Intercept(setHeaderInterceptor(...args));
433
+ }
434
+ const setCookieInterceptor = (name, value, attrs) => {
435
+ const fn = (before, after) => {
436
+ const { setCookie, getCookie } = useSetCookies();
437
+ after(() => {
438
+ if (!getCookie(name)) {
439
+ setCookie(name, value, attrs);
440
+ }
441
+ });
442
+ };
443
+ fn.priority = TInterceptorPriority.AFTER_ALL;
444
+ return fn;
445
+ };
446
+ function SetCookie(...args) {
447
+ return Intercept(setCookieInterceptor(...args));
448
+ }
449
+ const setStatusInterceptor = (code, opts) => {
450
+ return defineInterceptorFn((before, after) => {
451
+ const status = useStatus();
452
+ after(() => {
453
+ if (!status.isDefined || opts?.force) {
454
+ status.value = code;
455
+ }
456
+ });
457
+ });
458
+ };
459
+ function SetStatus(...args) {
460
+ return Intercept(setStatusInterceptor(...args));
710
461
  }
711
462
 
712
463
  export { All, Authorization, Body, 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 };