@moostjs/event-http 0.5.21 → 0.5.22
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/LICENSE +21 -0
- package/dist/index.cjs +293 -286
- package/dist/index.d.ts +3 -198
- package/dist/index.mjs +229 -249
- package/package.json +17 -10
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 moostjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -1,338 +1,345 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const moost = __toESM(require("moost"));
|
|
26
|
+
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
27
|
+
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
28
|
+
const __wooksjs_http_body = __toESM(require("@wooksjs/http-body"));
|
|
29
|
+
const __prostojs_infact = __toESM(require("@prostojs/infact"));
|
|
30
|
+
const http = __toESM(require("http"));
|
|
31
|
+
const https = __toESM(require("https"));
|
|
10
32
|
|
|
33
|
+
//#region packages/event-http/src/decorators/http-method.decorator.ts
|
|
11
34
|
function HttpMethod(method, path) {
|
|
12
|
-
|
|
35
|
+
return (0, moost.getMoostMate)().decorate("handlers", {
|
|
36
|
+
method,
|
|
37
|
+
path,
|
|
38
|
+
type: "HTTP"
|
|
39
|
+
}, true);
|
|
13
40
|
}
|
|
14
|
-
const All = (path) => HttpMethod(
|
|
15
|
-
const Get = (path) => HttpMethod(
|
|
16
|
-
const Post = (path) => HttpMethod(
|
|
17
|
-
const Put = (path) => HttpMethod(
|
|
18
|
-
const Delete = (path) => HttpMethod(
|
|
19
|
-
const Patch = (path) => HttpMethod(
|
|
41
|
+
const All = (path) => HttpMethod("*", path);
|
|
42
|
+
const Get = (path) => HttpMethod("GET", path);
|
|
43
|
+
const Post = (path) => HttpMethod("POST", path);
|
|
44
|
+
const Put = (path) => HttpMethod("PUT", path);
|
|
45
|
+
const Delete = (path) => HttpMethod("DELETE", path);
|
|
46
|
+
const Patch = (path) => HttpMethod("PATCH", path);
|
|
20
47
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
const HeaderHook = (name) => moost.Resolve((metas, level) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return typeof initialValue === 'string' ? initialValue : '';
|
|
47
|
-
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
50
|
+
const StatusHook = () => (0, moost.Resolve)((metas, level) => {
|
|
51
|
+
const hook = (0, __wooksjs_event_http.useStatus)();
|
|
52
|
+
if (level === "PARAM") return hook;
|
|
53
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
54
|
+
const initialValue = metas.instance[metas.key];
|
|
55
|
+
(0, __wooksjs_event_core.attachHook)(metas.instance, {
|
|
56
|
+
get: () => hook.value,
|
|
57
|
+
set: (v) => hook.value = v
|
|
58
|
+
}, metas.key);
|
|
59
|
+
return typeof initialValue === "number" ? initialValue : 200;
|
|
60
|
+
}
|
|
61
|
+
}, "statusCode");
|
|
62
|
+
const HeaderHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
63
|
+
const hook = (0, __wooksjs_event_http.useSetHeader)(name);
|
|
64
|
+
if (level === "PARAM") return hook;
|
|
65
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
66
|
+
const initialValue = metas.instance[metas.key];
|
|
67
|
+
(0, __wooksjs_event_core.attachHook)(metas.instance, {
|
|
68
|
+
get: () => hook.value,
|
|
69
|
+
set: (v) => hook.value = v
|
|
70
|
+
}, metas.key);
|
|
71
|
+
return typeof initialValue === "string" ? initialValue : "";
|
|
72
|
+
}
|
|
48
73
|
}, name);
|
|
49
|
-
const CookieHook = (name) => moost.Resolve((metas, level) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return typeof initialValue === 'string' ? initialValue : '';
|
|
61
|
-
}
|
|
74
|
+
const CookieHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
75
|
+
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
76
|
+
if (level === "PARAM") return hook;
|
|
77
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
78
|
+
const initialValue = metas.instance[metas.key];
|
|
79
|
+
(0, __wooksjs_event_core.attachHook)(metas.instance, {
|
|
80
|
+
get: () => hook.value,
|
|
81
|
+
set: (v) => hook.value = v
|
|
82
|
+
}, metas.key);
|
|
83
|
+
return typeof initialValue === "string" ? initialValue : "";
|
|
84
|
+
}
|
|
62
85
|
}, name);
|
|
63
|
-
const CookieAttrsHook = (name) => moost.Resolve((metas, level) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return typeof initialValue === 'object' ? initialValue : {};
|
|
78
|
-
}
|
|
86
|
+
const CookieAttrsHook = (name) => (0, moost.Resolve)((metas, level) => {
|
|
87
|
+
const hook = (0, __wooksjs_event_http.useSetCookie)(name);
|
|
88
|
+
if (level === "PARAM") return (0, __wooksjs_event_core.attachHook)({}, {
|
|
89
|
+
get: () => hook.attrs,
|
|
90
|
+
set: (v) => hook.attrs = v
|
|
91
|
+
});
|
|
92
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
93
|
+
const initialValue = metas.instance[metas.key];
|
|
94
|
+
(0, __wooksjs_event_core.attachHook)(metas.instance, {
|
|
95
|
+
get: () => hook.attrs,
|
|
96
|
+
set: (v) => hook.attrs = v
|
|
97
|
+
}, metas.key);
|
|
98
|
+
return typeof initialValue === "object" ? initialValue : {};
|
|
99
|
+
}
|
|
79
100
|
}, name);
|
|
80
101
|
function Authorization(name) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return auth.isBearer() ? auth.authorization : undefined;
|
|
92
|
-
}
|
|
93
|
-
case 'raw': {
|
|
94
|
-
return auth.authRawCredentials();
|
|
95
|
-
}
|
|
96
|
-
case 'type': {
|
|
97
|
-
return auth.authType();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}, 'authorization');
|
|
102
|
+
return (0, moost.Resolve)(() => {
|
|
103
|
+
const auth = (0, __wooksjs_event_http.useAuthorization)();
|
|
104
|
+
switch (name) {
|
|
105
|
+
case "username": return auth.isBasic() ? auth.basicCredentials()?.username : undefined;
|
|
106
|
+
case "password": return auth.isBasic() ? auth.basicCredentials()?.password : undefined;
|
|
107
|
+
case "bearer": return auth.isBearer() ? auth.authorization : undefined;
|
|
108
|
+
case "raw": return auth.authRawCredentials();
|
|
109
|
+
case "type": return auth.authType();
|
|
110
|
+
}
|
|
111
|
+
}, "authorization");
|
|
101
112
|
}
|
|
102
113
|
function Header(name) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
114
|
+
return (0, moost.Resolve)(() => {
|
|
115
|
+
const headers = (0, __wooksjs_event_http.useHeaders)();
|
|
116
|
+
return headers[name];
|
|
117
|
+
}, `header: ${name}`);
|
|
107
118
|
}
|
|
108
119
|
function Cookie(name) {
|
|
109
|
-
|
|
120
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useCookies)().getCookie(name), `cookie: ${name}`);
|
|
110
121
|
}
|
|
111
122
|
function Query(name) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
const isItem = !!name;
|
|
124
|
+
const _name = isItem ? name : "Query";
|
|
125
|
+
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), (0, moost.getMoostMate)().decorate("paramName", _name), (0, moost.Resolve)(() => {
|
|
126
|
+
const { jsonSearchParams, urlSearchParams } = (0, __wooksjs_event_http.useSearchParams)();
|
|
127
|
+
if (isItem) {
|
|
128
|
+
const p = urlSearchParams();
|
|
129
|
+
const value = p.get(name);
|
|
130
|
+
return value === null ? undefined : value;
|
|
131
|
+
}
|
|
132
|
+
const json = jsonSearchParams();
|
|
133
|
+
return Object.keys(json).length > 0 ? json : undefined;
|
|
134
|
+
}, _name));
|
|
124
135
|
}
|
|
125
136
|
function Url() {
|
|
126
|
-
|
|
137
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().url || "", "url");
|
|
127
138
|
}
|
|
128
139
|
function Method() {
|
|
129
|
-
|
|
140
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().method, "http_method");
|
|
130
141
|
}
|
|
131
142
|
function Req() {
|
|
132
|
-
|
|
143
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().rawRequest, "request");
|
|
133
144
|
}
|
|
134
145
|
function Res(opts) {
|
|
135
|
-
|
|
146
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useResponse)().rawResponse(opts), "response");
|
|
136
147
|
}
|
|
137
148
|
function ReqId() {
|
|
138
|
-
|
|
149
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().reqId(), "reqId");
|
|
139
150
|
}
|
|
140
151
|
function Ip(opts) {
|
|
141
|
-
|
|
152
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIp(opts), "ip");
|
|
142
153
|
}
|
|
143
154
|
function IpList() {
|
|
144
|
-
|
|
155
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_http.useRequest)().getIpList(), "ipList");
|
|
145
156
|
}
|
|
146
157
|
function Body() {
|
|
147
|
-
|
|
158
|
+
return (0, moost.getMoostMate)().apply((0, moost.getMoostMate)().decorate("paramSource", "BODY"), (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().parseBody(), "body"));
|
|
148
159
|
}
|
|
149
160
|
function RawBody() {
|
|
150
|
-
|
|
161
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_http_body.useBody)().rawBody(), "body");
|
|
151
162
|
}
|
|
152
163
|
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region packages/event-http/src/decorators/set.decorator.ts
|
|
153
166
|
const setHeaderInterceptor = (name, value, opts) => {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (opts?.when === 'always' || opts?.when === 'error') {
|
|
166
|
-
onError(cb);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
170
|
-
return fn;
|
|
167
|
+
const fn = (_before, after, onError) => {
|
|
168
|
+
const h = (0, __wooksjs_event_http.useSetHeader)(name);
|
|
169
|
+
const status = (0, __wooksjs_event_http.useStatus)();
|
|
170
|
+
const cb = () => {
|
|
171
|
+
if ((!h.value || opts?.force) && (!opts?.status || opts.status === status.value)) h.value = value;
|
|
172
|
+
};
|
|
173
|
+
if (opts?.when !== "error") after(cb);
|
|
174
|
+
if (opts?.when === "always" || opts?.when === "error") onError(cb);
|
|
175
|
+
};
|
|
176
|
+
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
177
|
+
return fn;
|
|
171
178
|
};
|
|
172
179
|
function SetHeader(...args) {
|
|
173
|
-
|
|
180
|
+
return (0, moost.Intercept)(setHeaderInterceptor(...args));
|
|
174
181
|
}
|
|
175
182
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
185
|
-
return fn;
|
|
183
|
+
const fn = (before, after) => {
|
|
184
|
+
const { setCookie, getCookie } = (0, __wooksjs_event_http.useSetCookies)();
|
|
185
|
+
after(() => {
|
|
186
|
+
if (!getCookie(name)) setCookie(name, value, attrs);
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
fn.priority = moost.TInterceptorPriority.AFTER_ALL;
|
|
190
|
+
return fn;
|
|
186
191
|
};
|
|
187
192
|
function SetCookie(...args) {
|
|
188
|
-
|
|
193
|
+
return (0, moost.Intercept)(setCookieInterceptor(...args));
|
|
189
194
|
}
|
|
190
|
-
const setStatusInterceptor = (code, opts) => moost.defineInterceptorFn((before, after) => {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
});
|
|
195
|
+
const setStatusInterceptor = (code, opts) => (0, moost.defineInterceptorFn)((before, after) => {
|
|
196
|
+
const status = (0, __wooksjs_event_http.useStatus)();
|
|
197
|
+
after(() => {
|
|
198
|
+
if (!status.isDefined || opts?.force) status.value = code;
|
|
199
|
+
});
|
|
197
200
|
});
|
|
198
201
|
function SetStatus(...args) {
|
|
199
|
-
|
|
202
|
+
return (0, moost.Intercept)(setStatusInterceptor(...args));
|
|
200
203
|
}
|
|
201
204
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
...httpApp,
|
|
214
|
-
onNotFound: this.onNotFound.bind(this),
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
this.httpApp = eventHttp.createHttpApp({
|
|
219
|
-
onNotFound: this.onNotFound.bind(this),
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
getHttpApp() {
|
|
224
|
-
return this.httpApp;
|
|
225
|
-
}
|
|
226
|
-
getServerCb() {
|
|
227
|
-
return this.httpApp.getServerCb();
|
|
228
|
-
}
|
|
229
|
-
listen(port, hostname, backlog, listeningListener) {
|
|
230
|
-
return this.httpApp.listen(port, hostname, backlog, listeningListener);
|
|
231
|
-
}
|
|
232
|
-
async onNotFound() {
|
|
233
|
-
return moost.defineMoostEventHandler({
|
|
234
|
-
loggerTitle: LOGGER_TITLE,
|
|
235
|
-
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
236
|
-
getControllerInstance: () => this.moost,
|
|
237
|
-
callControllerMethod: () => new eventHttp.HttpError(404, 'Resource Not Found'),
|
|
238
|
-
targetPath: '',
|
|
239
|
-
handlerType: '__SYSTEM__',
|
|
240
|
-
})();
|
|
241
|
-
}
|
|
242
|
-
onInit(moost) {
|
|
243
|
-
this.moost = moost;
|
|
244
|
-
}
|
|
245
|
-
getProvideRegistry() {
|
|
246
|
-
return infact.createProvideRegistry([eventHttp.WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [http.Server, () => this.getHttpApp().getServer()], [https.Server, () => this.getHttpApp().getServer()]);
|
|
247
|
-
}
|
|
248
|
-
getLogger() {
|
|
249
|
-
return this.getHttpApp().getLogger('[moost-http]');
|
|
250
|
-
}
|
|
251
|
-
bindHandler(opts) {
|
|
252
|
-
let fn;
|
|
253
|
-
for (const handler of opts.handlers) {
|
|
254
|
-
if (handler.type !== 'HTTP') {
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
const httpPath = handler.path;
|
|
258
|
-
const path = typeof httpPath === 'string' ? httpPath : typeof opts.method === 'string' ? opts.method : '';
|
|
259
|
-
const targetPath = `${`${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/')}${path.endsWith('//') ? '/' : ''}`;
|
|
260
|
-
fn = moost.defineMoostEventHandler({
|
|
261
|
-
contextType: CONTEXT_TYPE,
|
|
262
|
-
loggerTitle: LOGGER_TITLE,
|
|
263
|
-
getIterceptorHandler: opts.getIterceptorHandler,
|
|
264
|
-
getControllerInstance: opts.getInstance,
|
|
265
|
-
controllerMethod: opts.method,
|
|
266
|
-
resolveArgs: opts.resolveArgs,
|
|
267
|
-
manualUnscope: true,
|
|
268
|
-
hooks: {
|
|
269
|
-
init: ({ unscope }) => {
|
|
270
|
-
const { rawRequest } = eventHttp.useRequest();
|
|
271
|
-
rawRequest.on('end', unscope);
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
targetPath,
|
|
275
|
-
handlerType: handler.type,
|
|
276
|
-
});
|
|
277
|
-
const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
|
|
278
|
-
const { getPath: pathBuilder } = routerBinding;
|
|
279
|
-
const methodMeta = moost.getMoostMate().read(opts.fakeInstance, opts.method) || {};
|
|
280
|
-
const id = (methodMeta.id || opts.method);
|
|
281
|
-
if (id) {
|
|
282
|
-
const methods = (this.pathBuilders[id] = this.pathBuilders[id] || {});
|
|
283
|
-
if (handler.method === '*') {
|
|
284
|
-
methods.GET = pathBuilder;
|
|
285
|
-
methods.PUT = pathBuilder;
|
|
286
|
-
methods.PATCH = pathBuilder;
|
|
287
|
-
methods.POST = pathBuilder;
|
|
288
|
-
methods.DELETE = pathBuilder;
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
methods[handler.method] = pathBuilder;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
opts.logHandler(`${'[36m'}(${handler.method})${'[32m'}${targetPath}`);
|
|
295
|
-
const args = routerBinding.getArgs();
|
|
296
|
-
const params = {};
|
|
297
|
-
args.forEach(a => (params[a] = `{${a}}`));
|
|
298
|
-
opts.register(handler, routerBinding.getPath(params), args);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region packages/event-http/src/event-http.ts
|
|
207
|
+
function _define_property(obj, key, value) {
|
|
208
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
209
|
+
value,
|
|
210
|
+
enumerable: true,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true
|
|
213
|
+
});
|
|
214
|
+
else obj[key] = value;
|
|
215
|
+
return obj;
|
|
301
216
|
}
|
|
217
|
+
const LOGGER_TITLE = "moost-http";
|
|
218
|
+
const CONTEXT_TYPE = "HTTP";
|
|
219
|
+
var MoostHttp = class {
|
|
220
|
+
getHttpApp() {
|
|
221
|
+
return this.httpApp;
|
|
222
|
+
}
|
|
223
|
+
getServerCb() {
|
|
224
|
+
return this.httpApp.getServerCb();
|
|
225
|
+
}
|
|
226
|
+
listen(port, hostname, backlog, listeningListener) {
|
|
227
|
+
return this.httpApp.listen(port, hostname, backlog, listeningListener);
|
|
228
|
+
}
|
|
229
|
+
async onNotFound() {
|
|
230
|
+
return (0, moost.defineMoostEventHandler)({
|
|
231
|
+
loggerTitle: LOGGER_TITLE,
|
|
232
|
+
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
233
|
+
getControllerInstance: () => this.moost,
|
|
234
|
+
callControllerMethod: () => new __wooksjs_event_http.HttpError(404, "Resource Not Found"),
|
|
235
|
+
targetPath: "",
|
|
236
|
+
handlerType: "__SYSTEM__"
|
|
237
|
+
})();
|
|
238
|
+
}
|
|
239
|
+
onInit(moost$1) {
|
|
240
|
+
this.moost = moost$1;
|
|
241
|
+
}
|
|
242
|
+
getProvideRegistry() {
|
|
243
|
+
return (0, __prostojs_infact.createProvideRegistry)([__wooksjs_event_http.WooksHttp, () => this.getHttpApp()], ["WooksHttp", () => this.getHttpApp()], [http.Server, () => this.getHttpApp().getServer()], [https.Server, () => this.getHttpApp().getServer()]);
|
|
244
|
+
}
|
|
245
|
+
getLogger() {
|
|
246
|
+
return this.getHttpApp().getLogger("[moost-http]");
|
|
247
|
+
}
|
|
248
|
+
bindHandler(opts) {
|
|
249
|
+
let fn;
|
|
250
|
+
for (const handler of opts.handlers) {
|
|
251
|
+
if (handler.type !== "HTTP") continue;
|
|
252
|
+
const httpPath = handler.path;
|
|
253
|
+
const path = typeof httpPath === "string" ? httpPath : typeof opts.method === "string" ? opts.method : "";
|
|
254
|
+
const targetPath = `${`${opts.prefix || ""}/${path}`.replace(/\/\/+/g, "/")}${path.endsWith("//") ? "/" : ""}`;
|
|
255
|
+
fn = (0, moost.defineMoostEventHandler)({
|
|
256
|
+
contextType: CONTEXT_TYPE,
|
|
257
|
+
loggerTitle: LOGGER_TITLE,
|
|
258
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
259
|
+
getControllerInstance: opts.getInstance,
|
|
260
|
+
controllerMethod: opts.method,
|
|
261
|
+
resolveArgs: opts.resolveArgs,
|
|
262
|
+
manualUnscope: true,
|
|
263
|
+
hooks: { init: ({ unscope }) => {
|
|
264
|
+
const { rawRequest } = (0, __wooksjs_event_http.useRequest)();
|
|
265
|
+
rawRequest.on("end", unscope);
|
|
266
|
+
} },
|
|
267
|
+
targetPath,
|
|
268
|
+
handlerType: handler.type
|
|
269
|
+
});
|
|
270
|
+
const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
|
|
271
|
+
const { getPath: pathBuilder } = routerBinding;
|
|
272
|
+
const methodMeta = (0, moost.getMoostMate)().read(opts.fakeInstance, opts.method) || {};
|
|
273
|
+
const id = methodMeta.id || opts.method;
|
|
274
|
+
if (id) {
|
|
275
|
+
const methods = this.pathBuilders[id] = this.pathBuilders[id] || {};
|
|
276
|
+
if (handler.method === "*") {
|
|
277
|
+
methods.GET = pathBuilder;
|
|
278
|
+
methods.PUT = pathBuilder;
|
|
279
|
+
methods.PATCH = pathBuilder;
|
|
280
|
+
methods.POST = pathBuilder;
|
|
281
|
+
methods.DELETE = pathBuilder;
|
|
282
|
+
} else methods[handler.method] = pathBuilder;
|
|
283
|
+
}
|
|
284
|
+
opts.logHandler(`${"\x1B[36m"}(${handler.method})${"\x1B[32m"}${targetPath}`);
|
|
285
|
+
const args = routerBinding.getArgs();
|
|
286
|
+
const params = {};
|
|
287
|
+
args.forEach((a) => params[a] = `{${a}}`);
|
|
288
|
+
opts.register(handler, routerBinding.getPath(params), args);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
constructor(httpApp) {
|
|
292
|
+
_define_property(this, "name", "http");
|
|
293
|
+
_define_property(this, "httpApp", void 0);
|
|
294
|
+
_define_property(this, "pathBuilders", {});
|
|
295
|
+
_define_property(this, "moost", void 0);
|
|
296
|
+
if (httpApp && httpApp instanceof __wooksjs_event_http.WooksHttp) this.httpApp = httpApp;
|
|
297
|
+
else if (httpApp) this.httpApp = (0, __wooksjs_event_http.createHttpApp)({
|
|
298
|
+
...httpApp,
|
|
299
|
+
onNotFound: this.onNotFound.bind(this)
|
|
300
|
+
});
|
|
301
|
+
else this.httpApp = (0, __wooksjs_event_http.createHttpApp)({ onNotFound: this.onNotFound.bind(this) });
|
|
302
|
+
}
|
|
303
|
+
};
|
|
302
304
|
|
|
303
|
-
|
|
305
|
+
//#endregion
|
|
306
|
+
exports.All = All
|
|
307
|
+
exports.Authorization = Authorization
|
|
308
|
+
exports.Body = Body
|
|
309
|
+
exports.Cookie = Cookie
|
|
310
|
+
exports.CookieAttrsHook = CookieAttrsHook
|
|
311
|
+
exports.CookieHook = CookieHook
|
|
312
|
+
exports.Delete = Delete
|
|
313
|
+
exports.Get = Get
|
|
314
|
+
exports.Header = Header
|
|
315
|
+
exports.HeaderHook = HeaderHook
|
|
316
|
+
Object.defineProperty(exports, 'HttpError', {
|
|
304
317
|
enumerable: true,
|
|
305
|
-
get: function () {
|
|
318
|
+
get: function () {
|
|
319
|
+
return __wooksjs_event_http.HttpError;
|
|
320
|
+
}
|
|
306
321
|
});
|
|
307
|
-
|
|
322
|
+
exports.HttpMethod = HttpMethod
|
|
323
|
+
exports.Ip = Ip
|
|
324
|
+
exports.IpList = IpList
|
|
325
|
+
exports.Method = Method
|
|
326
|
+
exports.MoostHttp = MoostHttp
|
|
327
|
+
exports.Patch = Patch
|
|
328
|
+
exports.Post = Post
|
|
329
|
+
exports.Put = Put
|
|
330
|
+
exports.Query = Query
|
|
331
|
+
exports.RawBody = RawBody
|
|
332
|
+
exports.Req = Req
|
|
333
|
+
exports.ReqId = ReqId
|
|
334
|
+
exports.Res = Res
|
|
335
|
+
exports.SetCookie = SetCookie
|
|
336
|
+
exports.SetHeader = SetHeader
|
|
337
|
+
exports.SetStatus = SetStatus
|
|
338
|
+
exports.StatusHook = StatusHook
|
|
339
|
+
exports.Url = Url
|
|
340
|
+
Object.defineProperty(exports, 'useHttpContext', {
|
|
308
341
|
enumerable: true,
|
|
309
|
-
get: function () {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
exports.Body = Body;
|
|
314
|
-
exports.Cookie = Cookie;
|
|
315
|
-
exports.CookieAttrsHook = CookieAttrsHook;
|
|
316
|
-
exports.CookieHook = CookieHook;
|
|
317
|
-
exports.Delete = Delete;
|
|
318
|
-
exports.Get = Get;
|
|
319
|
-
exports.Header = Header;
|
|
320
|
-
exports.HeaderHook = HeaderHook;
|
|
321
|
-
exports.HttpMethod = HttpMethod;
|
|
322
|
-
exports.Ip = Ip;
|
|
323
|
-
exports.IpList = IpList;
|
|
324
|
-
exports.Method = Method;
|
|
325
|
-
exports.MoostHttp = MoostHttp;
|
|
326
|
-
exports.Patch = Patch;
|
|
327
|
-
exports.Post = Post;
|
|
328
|
-
exports.Put = Put;
|
|
329
|
-
exports.Query = Query;
|
|
330
|
-
exports.RawBody = RawBody;
|
|
331
|
-
exports.Req = Req;
|
|
332
|
-
exports.ReqId = ReqId;
|
|
333
|
-
exports.Res = Res;
|
|
334
|
-
exports.SetCookie = SetCookie;
|
|
335
|
-
exports.SetHeader = SetHeader;
|
|
336
|
-
exports.SetStatus = SetStatus;
|
|
337
|
-
exports.StatusHook = StatusHook;
|
|
338
|
-
exports.Url = Url;
|
|
342
|
+
get: function () {
|
|
343
|
+
return __wooksjs_event_http.useHttpContext;
|
|
344
|
+
}
|
|
345
|
+
});
|