@moostjs/event-http 0.5.20 → 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/dist/index.mjs
CHANGED
|
@@ -1,302 +1,282 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { attachHook } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { Server } from
|
|
8
|
-
import { Server as Server$1 } from 'https';
|
|
1
|
+
import { Intercept, Resolve, TInterceptorPriority, defineInterceptorFn, defineMoostEventHandler, getMoostMate } from "moost";
|
|
2
|
+
import { attachHook } from "@wooksjs/event-core";
|
|
3
|
+
import { HttpError, WooksHttp, createHttpApp, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCookie, useSetCookies, useSetHeader, useStatus } from "@wooksjs/event-http";
|
|
4
|
+
import { useBody } from "@wooksjs/http-body";
|
|
5
|
+
import { createProvideRegistry } from "@prostojs/infact";
|
|
6
|
+
import { Server } from "http";
|
|
7
|
+
import { Server as Server$1 } from "https";
|
|
9
8
|
|
|
9
|
+
//#region packages/event-http/src/decorators/http-method.decorator.ts
|
|
10
10
|
function HttpMethod(method, path) {
|
|
11
|
-
|
|
11
|
+
return getMoostMate().decorate("handlers", {
|
|
12
|
+
method,
|
|
13
|
+
path,
|
|
14
|
+
type: "HTTP"
|
|
15
|
+
}, true);
|
|
12
16
|
}
|
|
13
|
-
const All = (path) => HttpMethod(
|
|
14
|
-
const Get = (path) => HttpMethod(
|
|
15
|
-
const Post = (path) => HttpMethod(
|
|
16
|
-
const Put = (path) => HttpMethod(
|
|
17
|
-
const Delete = (path) => HttpMethod(
|
|
18
|
-
const Patch = (path) => HttpMethod(
|
|
17
|
+
const All = (path) => HttpMethod("*", path);
|
|
18
|
+
const Get = (path) => HttpMethod("GET", path);
|
|
19
|
+
const Post = (path) => HttpMethod("POST", path);
|
|
20
|
+
const Put = (path) => HttpMethod("PUT", path);
|
|
21
|
+
const Delete = (path) => HttpMethod("DELETE", path);
|
|
22
|
+
const Patch = (path) => HttpMethod("PATCH", path);
|
|
19
23
|
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region packages/event-http/src/decorators/resolve.decorator.ts
|
|
20
26
|
const StatusHook = () => Resolve((metas, level) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
}, 'statusCode');
|
|
27
|
+
const hook = useStatus();
|
|
28
|
+
if (level === "PARAM") return hook;
|
|
29
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
30
|
+
const initialValue = metas.instance[metas.key];
|
|
31
|
+
attachHook(metas.instance, {
|
|
32
|
+
get: () => hook.value,
|
|
33
|
+
set: (v) => hook.value = v
|
|
34
|
+
}, metas.key);
|
|
35
|
+
return typeof initialValue === "number" ? initialValue : 200;
|
|
36
|
+
}
|
|
37
|
+
}, "statusCode");
|
|
34
38
|
const HeaderHook = (name) => Resolve((metas, level) => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return typeof initialValue === 'string' ? initialValue : '';
|
|
46
|
-
}
|
|
39
|
+
const hook = useSetHeader(name);
|
|
40
|
+
if (level === "PARAM") return hook;
|
|
41
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
42
|
+
const initialValue = metas.instance[metas.key];
|
|
43
|
+
attachHook(metas.instance, {
|
|
44
|
+
get: () => hook.value,
|
|
45
|
+
set: (v) => hook.value = v
|
|
46
|
+
}, metas.key);
|
|
47
|
+
return typeof initialValue === "string" ? initialValue : "";
|
|
48
|
+
}
|
|
47
49
|
}, name);
|
|
48
50
|
const CookieHook = (name) => Resolve((metas, level) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return typeof initialValue === 'string' ? initialValue : '';
|
|
60
|
-
}
|
|
51
|
+
const hook = useSetCookie(name);
|
|
52
|
+
if (level === "PARAM") return hook;
|
|
53
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
54
|
+
const initialValue = metas.instance[metas.key];
|
|
55
|
+
attachHook(metas.instance, {
|
|
56
|
+
get: () => hook.value,
|
|
57
|
+
set: (v) => hook.value = v
|
|
58
|
+
}, metas.key);
|
|
59
|
+
return typeof initialValue === "string" ? initialValue : "";
|
|
60
|
+
}
|
|
61
61
|
}, name);
|
|
62
62
|
const CookieAttrsHook = (name) => Resolve((metas, level) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return typeof initialValue === 'object' ? initialValue : {};
|
|
77
|
-
}
|
|
63
|
+
const hook = useSetCookie(name);
|
|
64
|
+
if (level === "PARAM") return attachHook({}, {
|
|
65
|
+
get: () => hook.attrs,
|
|
66
|
+
set: (v) => hook.attrs = v
|
|
67
|
+
});
|
|
68
|
+
if (level === "PROP" && metas.instance && metas.key) {
|
|
69
|
+
const initialValue = metas.instance[metas.key];
|
|
70
|
+
attachHook(metas.instance, {
|
|
71
|
+
get: () => hook.attrs,
|
|
72
|
+
set: (v) => hook.attrs = v
|
|
73
|
+
}, metas.key);
|
|
74
|
+
return typeof initialValue === "object" ? initialValue : {};
|
|
75
|
+
}
|
|
78
76
|
}, name);
|
|
79
77
|
function Authorization(name) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return auth.isBearer() ? auth.authorization : undefined;
|
|
91
|
-
}
|
|
92
|
-
case 'raw': {
|
|
93
|
-
return auth.authRawCredentials();
|
|
94
|
-
}
|
|
95
|
-
case 'type': {
|
|
96
|
-
return auth.authType();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}, 'authorization');
|
|
78
|
+
return Resolve(() => {
|
|
79
|
+
const auth = useAuthorization();
|
|
80
|
+
switch (name) {
|
|
81
|
+
case "username": return auth.isBasic() ? auth.basicCredentials()?.username : undefined;
|
|
82
|
+
case "password": return auth.isBasic() ? auth.basicCredentials()?.password : undefined;
|
|
83
|
+
case "bearer": return auth.isBearer() ? auth.authorization : undefined;
|
|
84
|
+
case "raw": return auth.authRawCredentials();
|
|
85
|
+
case "type": return auth.authType();
|
|
86
|
+
}
|
|
87
|
+
}, "authorization");
|
|
100
88
|
}
|
|
101
89
|
function Header(name) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
90
|
+
return Resolve(() => {
|
|
91
|
+
const headers = useHeaders();
|
|
92
|
+
return headers[name];
|
|
93
|
+
}, `header: ${name}`);
|
|
106
94
|
}
|
|
107
95
|
function Cookie(name) {
|
|
108
|
-
|
|
96
|
+
return Resolve(() => useCookies().getCookie(name), `cookie: ${name}`);
|
|
109
97
|
}
|
|
110
98
|
function Query(name) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
99
|
+
const isItem = !!name;
|
|
100
|
+
const _name = isItem ? name : "Query";
|
|
101
|
+
return getMoostMate().apply(getMoostMate().decorate("paramSource", isItem ? "QUERY_ITEM" : "QUERY"), getMoostMate().decorate("paramName", _name), Resolve(() => {
|
|
102
|
+
const { jsonSearchParams, urlSearchParams } = useSearchParams();
|
|
103
|
+
if (isItem) {
|
|
104
|
+
const p = urlSearchParams();
|
|
105
|
+
const value = p.get(name);
|
|
106
|
+
return value === null ? undefined : value;
|
|
107
|
+
}
|
|
108
|
+
const json = jsonSearchParams();
|
|
109
|
+
return Object.keys(json).length > 0 ? json : undefined;
|
|
110
|
+
}, _name));
|
|
123
111
|
}
|
|
124
112
|
function Url() {
|
|
125
|
-
|
|
113
|
+
return Resolve(() => useRequest().url || "", "url");
|
|
126
114
|
}
|
|
127
115
|
function Method() {
|
|
128
|
-
|
|
116
|
+
return Resolve(() => useRequest().method, "http_method");
|
|
129
117
|
}
|
|
130
118
|
function Req() {
|
|
131
|
-
|
|
119
|
+
return Resolve(() => useRequest().rawRequest, "request");
|
|
132
120
|
}
|
|
133
121
|
function Res(opts) {
|
|
134
|
-
|
|
122
|
+
return Resolve(() => useResponse().rawResponse(opts), "response");
|
|
135
123
|
}
|
|
136
124
|
function ReqId() {
|
|
137
|
-
|
|
125
|
+
return Resolve(() => useRequest().reqId(), "reqId");
|
|
138
126
|
}
|
|
139
127
|
function Ip(opts) {
|
|
140
|
-
|
|
128
|
+
return Resolve(() => useRequest().getIp(opts), "ip");
|
|
141
129
|
}
|
|
142
130
|
function IpList() {
|
|
143
|
-
|
|
131
|
+
return Resolve(() => useRequest().getIpList(), "ipList");
|
|
144
132
|
}
|
|
145
133
|
function Body() {
|
|
146
|
-
|
|
134
|
+
return getMoostMate().apply(getMoostMate().decorate("paramSource", "BODY"), Resolve(() => useBody().parseBody(), "body"));
|
|
147
135
|
}
|
|
148
136
|
function RawBody() {
|
|
149
|
-
|
|
137
|
+
return Resolve(() => useBody().rawBody(), "body");
|
|
150
138
|
}
|
|
151
139
|
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region packages/event-http/src/decorators/set.decorator.ts
|
|
152
142
|
const setHeaderInterceptor = (name, value, opts) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (opts?.when === 'always' || opts?.when === 'error') {
|
|
165
|
-
onError(cb);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
169
|
-
return fn;
|
|
143
|
+
const fn = (_before, after, onError) => {
|
|
144
|
+
const h = useSetHeader(name);
|
|
145
|
+
const status = useStatus();
|
|
146
|
+
const cb = () => {
|
|
147
|
+
if ((!h.value || opts?.force) && (!opts?.status || opts.status === status.value)) h.value = value;
|
|
148
|
+
};
|
|
149
|
+
if (opts?.when !== "error") after(cb);
|
|
150
|
+
if (opts?.when === "always" || opts?.when === "error") onError(cb);
|
|
151
|
+
};
|
|
152
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
153
|
+
return fn;
|
|
170
154
|
};
|
|
171
155
|
function SetHeader(...args) {
|
|
172
|
-
|
|
156
|
+
return Intercept(setHeaderInterceptor(...args));
|
|
173
157
|
}
|
|
174
158
|
const setCookieInterceptor = (name, value, attrs) => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
184
|
-
return fn;
|
|
159
|
+
const fn = (before, after) => {
|
|
160
|
+
const { setCookie, getCookie } = useSetCookies();
|
|
161
|
+
after(() => {
|
|
162
|
+
if (!getCookie(name)) setCookie(name, value, attrs);
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
166
|
+
return fn;
|
|
185
167
|
};
|
|
186
168
|
function SetCookie(...args) {
|
|
187
|
-
|
|
169
|
+
return Intercept(setCookieInterceptor(...args));
|
|
188
170
|
}
|
|
189
171
|
const setStatusInterceptor = (code, opts) => defineInterceptorFn((before, after) => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
});
|
|
172
|
+
const status = useStatus();
|
|
173
|
+
after(() => {
|
|
174
|
+
if (!status.isDefined || opts?.force) status.value = code;
|
|
175
|
+
});
|
|
196
176
|
});
|
|
197
177
|
function SetStatus(...args) {
|
|
198
|
-
|
|
178
|
+
return Intercept(setStatusInterceptor(...args));
|
|
199
179
|
}
|
|
200
180
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
...httpApp,
|
|
213
|
-
onNotFound: this.onNotFound.bind(this),
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
this.httpApp = createHttpApp({
|
|
218
|
-
onNotFound: this.onNotFound.bind(this),
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
getHttpApp() {
|
|
223
|
-
return this.httpApp;
|
|
224
|
-
}
|
|
225
|
-
getServerCb() {
|
|
226
|
-
return this.httpApp.getServerCb();
|
|
227
|
-
}
|
|
228
|
-
listen(port, hostname, backlog, listeningListener) {
|
|
229
|
-
return this.httpApp.listen(port, hostname, backlog, listeningListener);
|
|
230
|
-
}
|
|
231
|
-
async onNotFound() {
|
|
232
|
-
return defineMoostEventHandler({
|
|
233
|
-
loggerTitle: LOGGER_TITLE,
|
|
234
|
-
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
235
|
-
getControllerInstance: () => this.moost,
|
|
236
|
-
callControllerMethod: () => new HttpError(404, 'Resource Not Found'),
|
|
237
|
-
targetPath: '',
|
|
238
|
-
handlerType: '__SYSTEM__',
|
|
239
|
-
})();
|
|
240
|
-
}
|
|
241
|
-
onInit(moost) {
|
|
242
|
-
this.moost = moost;
|
|
243
|
-
}
|
|
244
|
-
getProvideRegistry() {
|
|
245
|
-
return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [Server, () => this.getHttpApp().getServer()], [Server$1, () => this.getHttpApp().getServer()]);
|
|
246
|
-
}
|
|
247
|
-
getLogger() {
|
|
248
|
-
return this.getHttpApp().getLogger('[moost-http]');
|
|
249
|
-
}
|
|
250
|
-
bindHandler(opts) {
|
|
251
|
-
let fn;
|
|
252
|
-
for (const handler of opts.handlers) {
|
|
253
|
-
if (handler.type !== 'HTTP') {
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
const httpPath = handler.path;
|
|
257
|
-
const path = typeof httpPath === 'string' ? httpPath : typeof opts.method === 'string' ? opts.method : '';
|
|
258
|
-
const targetPath = `${`${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/')}${path.endsWith('//') ? '/' : ''}`;
|
|
259
|
-
fn = defineMoostEventHandler({
|
|
260
|
-
contextType: CONTEXT_TYPE,
|
|
261
|
-
loggerTitle: LOGGER_TITLE,
|
|
262
|
-
getIterceptorHandler: opts.getIterceptorHandler,
|
|
263
|
-
getControllerInstance: opts.getInstance,
|
|
264
|
-
controllerMethod: opts.method,
|
|
265
|
-
resolveArgs: opts.resolveArgs,
|
|
266
|
-
manualUnscope: true,
|
|
267
|
-
hooks: {
|
|
268
|
-
init: ({ unscope }) => {
|
|
269
|
-
const { rawRequest } = useRequest();
|
|
270
|
-
rawRequest.on('end', unscope);
|
|
271
|
-
},
|
|
272
|
-
},
|
|
273
|
-
targetPath,
|
|
274
|
-
handlerType: handler.type,
|
|
275
|
-
});
|
|
276
|
-
const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
|
|
277
|
-
const { getPath: pathBuilder } = routerBinding;
|
|
278
|
-
const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) || {};
|
|
279
|
-
const id = (methodMeta.id || opts.method);
|
|
280
|
-
if (id) {
|
|
281
|
-
const methods = (this.pathBuilders[id] = this.pathBuilders[id] || {});
|
|
282
|
-
if (handler.method === '*') {
|
|
283
|
-
methods.GET = pathBuilder;
|
|
284
|
-
methods.PUT = pathBuilder;
|
|
285
|
-
methods.PATCH = pathBuilder;
|
|
286
|
-
methods.POST = pathBuilder;
|
|
287
|
-
methods.DELETE = pathBuilder;
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
methods[handler.method] = pathBuilder;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
opts.logHandler(`${'[36m'}(${handler.method})${'[32m'}${targetPath}`);
|
|
294
|
-
const args = routerBinding.getArgs();
|
|
295
|
-
const params = {};
|
|
296
|
-
args.forEach(a => (params[a] = `{${a}}`));
|
|
297
|
-
opts.register(handler, routerBinding.getPath(params), args);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region packages/event-http/src/event-http.ts
|
|
183
|
+
function _define_property(obj, key, value) {
|
|
184
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
185
|
+
value,
|
|
186
|
+
enumerable: true,
|
|
187
|
+
configurable: true,
|
|
188
|
+
writable: true
|
|
189
|
+
});
|
|
190
|
+
else obj[key] = value;
|
|
191
|
+
return obj;
|
|
300
192
|
}
|
|
193
|
+
const LOGGER_TITLE = "moost-http";
|
|
194
|
+
const CONTEXT_TYPE = "HTTP";
|
|
195
|
+
var MoostHttp = class {
|
|
196
|
+
getHttpApp() {
|
|
197
|
+
return this.httpApp;
|
|
198
|
+
}
|
|
199
|
+
getServerCb() {
|
|
200
|
+
return this.httpApp.getServerCb();
|
|
201
|
+
}
|
|
202
|
+
listen(port, hostname, backlog, listeningListener) {
|
|
203
|
+
return this.httpApp.listen(port, hostname, backlog, listeningListener);
|
|
204
|
+
}
|
|
205
|
+
async onNotFound() {
|
|
206
|
+
return defineMoostEventHandler({
|
|
207
|
+
loggerTitle: LOGGER_TITLE,
|
|
208
|
+
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
209
|
+
getControllerInstance: () => this.moost,
|
|
210
|
+
callControllerMethod: () => new HttpError(404, "Resource Not Found"),
|
|
211
|
+
targetPath: "",
|
|
212
|
+
handlerType: "__SYSTEM__"
|
|
213
|
+
})();
|
|
214
|
+
}
|
|
215
|
+
onInit(moost) {
|
|
216
|
+
this.moost = moost;
|
|
217
|
+
}
|
|
218
|
+
getProvideRegistry() {
|
|
219
|
+
return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ["WooksHttp", () => this.getHttpApp()], [Server, () => this.getHttpApp().getServer()], [Server$1, () => this.getHttpApp().getServer()]);
|
|
220
|
+
}
|
|
221
|
+
getLogger() {
|
|
222
|
+
return this.getHttpApp().getLogger("[moost-http]");
|
|
223
|
+
}
|
|
224
|
+
bindHandler(opts) {
|
|
225
|
+
let fn;
|
|
226
|
+
for (const handler of opts.handlers) {
|
|
227
|
+
if (handler.type !== "HTTP") continue;
|
|
228
|
+
const httpPath = handler.path;
|
|
229
|
+
const path = typeof httpPath === "string" ? httpPath : typeof opts.method === "string" ? opts.method : "";
|
|
230
|
+
const targetPath = `${`${opts.prefix || ""}/${path}`.replace(/\/\/+/g, "/")}${path.endsWith("//") ? "/" : ""}`;
|
|
231
|
+
fn = defineMoostEventHandler({
|
|
232
|
+
contextType: CONTEXT_TYPE,
|
|
233
|
+
loggerTitle: LOGGER_TITLE,
|
|
234
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
235
|
+
getControllerInstance: opts.getInstance,
|
|
236
|
+
controllerMethod: opts.method,
|
|
237
|
+
resolveArgs: opts.resolveArgs,
|
|
238
|
+
manualUnscope: true,
|
|
239
|
+
hooks: { init: ({ unscope }) => {
|
|
240
|
+
const { rawRequest } = useRequest();
|
|
241
|
+
rawRequest.on("end", unscope);
|
|
242
|
+
} },
|
|
243
|
+
targetPath,
|
|
244
|
+
handlerType: handler.type
|
|
245
|
+
});
|
|
246
|
+
const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
|
|
247
|
+
const { getPath: pathBuilder } = routerBinding;
|
|
248
|
+
const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) || {};
|
|
249
|
+
const id = methodMeta.id || opts.method;
|
|
250
|
+
if (id) {
|
|
251
|
+
const methods = this.pathBuilders[id] = this.pathBuilders[id] || {};
|
|
252
|
+
if (handler.method === "*") {
|
|
253
|
+
methods.GET = pathBuilder;
|
|
254
|
+
methods.PUT = pathBuilder;
|
|
255
|
+
methods.PATCH = pathBuilder;
|
|
256
|
+
methods.POST = pathBuilder;
|
|
257
|
+
methods.DELETE = pathBuilder;
|
|
258
|
+
} else methods[handler.method] = pathBuilder;
|
|
259
|
+
}
|
|
260
|
+
opts.logHandler(`${"\x1B[36m"}(${handler.method})${"\x1B[32m"}${targetPath}`);
|
|
261
|
+
const args = routerBinding.getArgs();
|
|
262
|
+
const params = {};
|
|
263
|
+
args.forEach((a) => params[a] = `{${a}}`);
|
|
264
|
+
opts.register(handler, routerBinding.getPath(params), args);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
constructor(httpApp) {
|
|
268
|
+
_define_property(this, "name", "http");
|
|
269
|
+
_define_property(this, "httpApp", void 0);
|
|
270
|
+
_define_property(this, "pathBuilders", {});
|
|
271
|
+
_define_property(this, "moost", void 0);
|
|
272
|
+
if (httpApp && httpApp instanceof WooksHttp) this.httpApp = httpApp;
|
|
273
|
+
else if (httpApp) this.httpApp = createHttpApp({
|
|
274
|
+
...httpApp,
|
|
275
|
+
onNotFound: this.onNotFound.bind(this)
|
|
276
|
+
});
|
|
277
|
+
else this.httpApp = createHttpApp({ onNotFound: this.onNotFound.bind(this) });
|
|
278
|
+
}
|
|
279
|
+
};
|
|
301
280
|
|
|
302
|
-
|
|
281
|
+
//#endregion
|
|
282
|
+
export { All, Authorization, Body, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpError, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook, Url, useHttpContext };
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-http",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.22",
|
|
4
4
|
"description": "@moostjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"sideEffects": false,
|
|
9
|
+
"type": "module",
|
|
9
10
|
"exports": {
|
|
10
11
|
"./package.json": "./package.json",
|
|
11
12
|
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
12
14
|
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
"types": "./dist/index.d.ts"
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
15
16
|
}
|
|
16
17
|
},
|
|
17
18
|
"files": [
|
|
@@ -36,13 +37,19 @@
|
|
|
36
37
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
37
38
|
},
|
|
38
39
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
|
|
39
|
-
"peerDependencies": {},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"moost": "0.5.20",
|
|
42
|
-
"@wooksjs/event-core": "^0.5.20",
|
|
43
|
-
"@wooksjs/event-http": "^0.5.20",
|
|
44
|
-
"@wooksjs/http-body": "^0.5.20",
|
|
45
41
|
"@prostojs/infact": "^0.3.3",
|
|
46
|
-
"@prostojs/router": "^0.2.1"
|
|
42
|
+
"@prostojs/router": "^0.2.1",
|
|
43
|
+
"@wooksjs/event-core": "^0.5.25",
|
|
44
|
+
"@wooksjs/event-http": "^0.5.25",
|
|
45
|
+
"@wooksjs/http-body": "^0.5.25",
|
|
46
|
+
"moost": "^0.5.22"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"vitest": "^3.0.5"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"pub": "pnpm publish --access public",
|
|
53
|
+
"test": "vitest"
|
|
47
54
|
}
|
|
48
|
-
}
|
|
55
|
+
}
|