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