@moostjs/event-http 0.5.33 → 0.6.1
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/README.md +67 -3
- package/dist/index.cjs +261 -83
- package/dist/index.d.ts +123 -22
- package/dist/index.mjs +248 -86
- package/package.json +40 -34
- package/scripts/setup-skills.js +76 -0
- package/skills/moostjs-event-http/SKILL.md +32 -0
- package/skills/moostjs-event-http/auth.md +275 -0
- package/skills/moostjs-event-http/core.md +193 -0
- package/skills/moostjs-event-http/request.md +230 -0
- package/skills/moostjs-event-http/response.md +287 -0
- package/skills/moostjs-event-http/routing.md +210 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,109 @@
|
|
|
1
|
-
import { THook } from '@wooksjs/event-core';
|
|
2
|
-
import { TCookieAttributes as TCookieAttributes$1, useSetCookies, WooksHttp, TWooksHttpOptions } from '@wooksjs/event-http';
|
|
3
|
-
export { HttpError, TCacheControl, TCookieAttributesInput, TSetCookieData, useHttpContext } from '@wooksjs/event-http';
|
|
4
1
|
import * as moost from 'moost';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
2
|
+
import { TInterceptorPriority, TOvertakeFn, TInterceptorDef, TClassConstructor, TMoostAdapter, Moost, TConsoleBase, TMoostAdapterOptions } from 'moost';
|
|
3
|
+
import { TCookieAttributes as TCookieAttributes$1, TCookieAttributesInput, WooksHttp, TWooksHttpOptions } from '@wooksjs/event-http';
|
|
4
|
+
export { HttpError, TCacheControl, TCookieAttributesInput, TSetCookieData, httpKind, useHttpContext } from '@wooksjs/event-http';
|
|
7
5
|
import * as http from 'http';
|
|
8
6
|
import { TProstoRouterPathBuilder } from '@prostojs/router';
|
|
9
7
|
import { ListenOptions } from 'net';
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
/** Bearer token transport configuration for auth guards. */
|
|
10
|
+
interface TAuthTransportBearer {
|
|
11
|
+
format?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Basic auth transport configuration for auth guards. */
|
|
15
|
+
interface TAuthTransportBasic {
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
/** API key transport configuration specifying location (header, query, or cookie). */
|
|
19
|
+
interface TAuthTransportApiKey {
|
|
20
|
+
name: string;
|
|
21
|
+
in: 'header' | 'query' | 'cookie';
|
|
22
|
+
description?: string;
|
|
23
|
+
}
|
|
24
|
+
/** Cookie-based auth transport configuration. */
|
|
25
|
+
interface TAuthTransportCookie {
|
|
26
|
+
name: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
}
|
|
29
|
+
/** Declares which auth credential transports a guard accepts. */
|
|
30
|
+
interface TAuthTransportDeclaration {
|
|
31
|
+
bearer?: TAuthTransportBearer;
|
|
32
|
+
basic?: TAuthTransportBasic;
|
|
33
|
+
apiKey?: TAuthTransportApiKey;
|
|
34
|
+
cookie?: TAuthTransportCookie;
|
|
35
|
+
}
|
|
36
|
+
/** Type-narrowed credential values extracted from declared transports. */
|
|
37
|
+
type TAuthTransportValues<T extends TAuthTransportDeclaration> = {
|
|
38
|
+
[K in keyof T & keyof TAuthTransportDeclaration]: K extends 'basic' ? {
|
|
39
|
+
username: string;
|
|
40
|
+
password: string;
|
|
41
|
+
} : string;
|
|
42
|
+
};
|
|
43
|
+
/** Extracts auth credentials from the current request based on transport declarations. Throws 401 if none found. */
|
|
44
|
+
declare function extractTransports<T extends TAuthTransportDeclaration>(declaration: T): TAuthTransportValues<T>;
|
|
45
|
+
/** Auth guard def returned by defineAuthGuard — carries transport declarations. */
|
|
46
|
+
interface TAuthGuardDef extends TInterceptorDef {
|
|
47
|
+
__authTransports: TAuthTransportDeclaration;
|
|
48
|
+
}
|
|
49
|
+
/** Auth guard class extending AuthGuard — carries transport declarations as static property. */
|
|
50
|
+
type TAuthGuardClass = TClassConstructor<AuthGuard> & {
|
|
51
|
+
transports: TAuthTransportDeclaration;
|
|
52
|
+
priority: TInterceptorPriority;
|
|
53
|
+
};
|
|
54
|
+
/** Accepted handler for Authenticate — either a functional or class-based auth guard. */
|
|
55
|
+
type TAuthGuardHandler = TAuthGuardDef | TAuthGuardClass;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a functional auth guard interceptor.
|
|
58
|
+
* Extracts credentials from declared transports and passes them to the handler.
|
|
59
|
+
* Return a value from the handler to short-circuit with that response.
|
|
60
|
+
*/
|
|
61
|
+
declare function defineAuthGuard<T extends TAuthTransportDeclaration>(transports: T, handler: (transports: TAuthTransportValues<T>) => unknown | Promise<unknown>): TAuthGuardDef;
|
|
62
|
+
/**
|
|
63
|
+
* Abstract base class for class-based auth guards.
|
|
64
|
+
* Extend this class, set `static transports`, and implement `handle()`.
|
|
65
|
+
*/
|
|
66
|
+
declare abstract class AuthGuard<T extends TAuthTransportDeclaration = TAuthTransportDeclaration> {
|
|
67
|
+
static transports: TAuthTransportDeclaration;
|
|
68
|
+
static priority: TInterceptorPriority;
|
|
69
|
+
abstract handle(transports: TAuthTransportValues<T>): unknown | Promise<unknown>;
|
|
70
|
+
__intercept(reply: TOvertakeFn): Promise<undefined> | undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Registers an auth guard as an interceptor and stores its transport
|
|
74
|
+
* declarations in metadata for swagger auto-discovery.
|
|
75
|
+
*
|
|
76
|
+
* Accepts either a functional guard from `defineAuthGuard()` or a
|
|
77
|
+
* class-based guard extending `AuthGuard`.
|
|
78
|
+
*/
|
|
79
|
+
declare function Authenticate(handler: TAuthGuardHandler): ClassDecorator & MethodDecorator;
|
|
80
|
+
|
|
81
|
+
/** Base decorator for registering an HTTP route handler with an explicit method. */
|
|
82
|
+
declare function HttpMethod(method: '*' | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'UPGRADE', path?: string): MethodDecorator;
|
|
83
|
+
/** Register a catch-all route handler matching any HTTP method. */
|
|
12
84
|
declare const All: (path?: string) => MethodDecorator;
|
|
85
|
+
/** Register a GET route handler. */
|
|
13
86
|
declare const Get: (path?: string) => MethodDecorator;
|
|
87
|
+
/** Register a POST route handler. */
|
|
14
88
|
declare const Post: (path?: string) => MethodDecorator;
|
|
89
|
+
/** Register a PUT route handler. */
|
|
15
90
|
declare const Put: (path?: string) => MethodDecorator;
|
|
91
|
+
/** Register a DELETE route handler. */
|
|
16
92
|
declare const Delete: (path?: string) => MethodDecorator;
|
|
93
|
+
/** Register a PATCH route handler. */
|
|
17
94
|
declare const Patch: (path?: string) => MethodDecorator;
|
|
95
|
+
/**
|
|
96
|
+
* Register an UPGRADE route handler for WebSocket upgrade requests.
|
|
97
|
+
*
|
|
98
|
+
* Use together with `WooksWs` (injected via DI) to complete the handshake:
|
|
99
|
+
* ```ts
|
|
100
|
+
* @Upgrade('/ws')
|
|
101
|
+
* handleUpgrade(ws: WooksWs) {
|
|
102
|
+
* return ws.upgrade()
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare const Upgrade: (path?: string) => MethodDecorator;
|
|
18
107
|
|
|
19
108
|
/**
|
|
20
109
|
* Hook to the Response Status
|
|
@@ -130,16 +219,27 @@ declare function Body(): MethodDecorator & ClassDecorator & ParameterDecorator &
|
|
|
130
219
|
* @paramType Promise<Buffer>
|
|
131
220
|
*/
|
|
132
221
|
declare function RawBody(): ParameterDecorator & PropertyDecorator;
|
|
133
|
-
|
|
134
|
-
|
|
222
|
+
/** Reactive hook for reading/writing the response status code. */
|
|
223
|
+
interface TStatusHook {
|
|
224
|
+
value: number;
|
|
225
|
+
}
|
|
226
|
+
/** Reactive hook for reading/writing a response header value. */
|
|
227
|
+
interface THeaderHook {
|
|
228
|
+
value: string | string[] | undefined;
|
|
229
|
+
}
|
|
230
|
+
/** Partial cookie attributes (all optional). */
|
|
135
231
|
type TCookieAttributes = Partial<TCookieAttributes$1>;
|
|
136
|
-
|
|
232
|
+
/** Reactive hook for reading/writing a response cookie. */
|
|
233
|
+
interface TCookieHook {
|
|
234
|
+
value: string;
|
|
235
|
+
attrs?: TCookieAttributes;
|
|
236
|
+
}
|
|
137
237
|
|
|
138
238
|
declare const setHeaderInterceptor: (name: string, value: string, opts?: {
|
|
139
239
|
force?: boolean;
|
|
140
240
|
status?: number;
|
|
141
|
-
when?:
|
|
142
|
-
}) =>
|
|
241
|
+
when?: "always" | "error" | "ok";
|
|
242
|
+
}) => TInterceptorDef;
|
|
143
243
|
/**
|
|
144
244
|
* Set Header for Request Handler
|
|
145
245
|
*
|
|
@@ -178,7 +278,7 @@ declare const setHeaderInterceptor: (name: string, value: string, opts?: {
|
|
|
178
278
|
* @param options options { status?: number, force?: boolean }
|
|
179
279
|
*/
|
|
180
280
|
declare function SetHeader(...args: Parameters<typeof setHeaderInterceptor>): ClassDecorator & MethodDecorator;
|
|
181
|
-
declare const setCookieInterceptor: (
|
|
281
|
+
declare const setCookieInterceptor: (name: string, value: string, attrs?: TCookieAttributesInput) => TInterceptorDef;
|
|
182
282
|
/**
|
|
183
283
|
* Set Cookie for Request Handler
|
|
184
284
|
* ```ts
|
|
@@ -203,7 +303,7 @@ declare const setCookieInterceptor: (...args: Parameters<ReturnType<typeof useSe
|
|
|
203
303
|
declare function SetCookie(...args: Parameters<typeof setCookieInterceptor>): ClassDecorator & MethodDecorator;
|
|
204
304
|
declare const setStatusInterceptor: (code: number, opts?: {
|
|
205
305
|
force?: boolean;
|
|
206
|
-
}) =>
|
|
306
|
+
}) => TInterceptorDef;
|
|
207
307
|
/**
|
|
208
308
|
* Set Response Status for Request Handler
|
|
209
309
|
*
|
|
@@ -229,23 +329,23 @@ declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): Cl
|
|
|
229
329
|
* Creates an interceptor that sets the maximum allowed inflated body size in bytes.
|
|
230
330
|
*
|
|
231
331
|
* @param n - Maximum body size in bytes after decompression.
|
|
232
|
-
* @returns Interceptor
|
|
332
|
+
* @returns Interceptor def to enforce the limit.
|
|
233
333
|
*/
|
|
234
|
-
declare const globalBodySizeLimit: (n: number) => moost.
|
|
334
|
+
declare const globalBodySizeLimit: (n: number) => moost.TInterceptorDef;
|
|
235
335
|
/**
|
|
236
336
|
* Creates an interceptor that sets the maximum allowed compressed body size in bytes.
|
|
237
337
|
*
|
|
238
338
|
* @param n - Maximum body size in bytes before decompression.
|
|
239
|
-
* @returns Interceptor
|
|
339
|
+
* @returns Interceptor def to enforce the limit.
|
|
240
340
|
*/
|
|
241
|
-
declare const globalCompressedBodySizeLimit: (n: number) => moost.
|
|
341
|
+
declare const globalCompressedBodySizeLimit: (n: number) => moost.TInterceptorDef;
|
|
242
342
|
/**
|
|
243
343
|
* Creates an interceptor that sets the timeout for reading the request body.
|
|
244
344
|
*
|
|
245
345
|
* @param n - Timeout in milliseconds.
|
|
246
|
-
* @returns Interceptor
|
|
346
|
+
* @returns Interceptor def to enforce the timeout.
|
|
247
347
|
*/
|
|
248
|
-
declare const globalBodyReadTimeoutMs: (n: number) => moost.
|
|
348
|
+
declare const globalBodyReadTimeoutMs: (n: number) => moost.TInterceptorDef;
|
|
249
349
|
/**
|
|
250
350
|
* Decorator to limit the maximum inflated body size for the request. Default: 10 MB
|
|
251
351
|
*
|
|
@@ -265,6 +365,7 @@ declare const CompressedBodySizeLimit: (n: number) => ClassDecorator & MethodDec
|
|
|
265
365
|
*/
|
|
266
366
|
declare const BodyReadTimeoutMs: (n: number) => ClassDecorator & MethodDecorator;
|
|
267
367
|
|
|
368
|
+
/** Handler metadata for HTTP events, carrying the HTTP method and route path. */
|
|
268
369
|
interface THttpHandlerMeta {
|
|
269
370
|
method: string;
|
|
270
371
|
path: string;
|
|
@@ -319,10 +420,10 @@ declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
|
|
|
319
420
|
onNotFound(): Promise<unknown>;
|
|
320
421
|
protected moost?: Moost;
|
|
321
422
|
onInit(moost: Moost): void;
|
|
322
|
-
getProvideRegistry():
|
|
423
|
+
getProvideRegistry(): moost.TProvideRegistry;
|
|
323
424
|
getLogger(): TConsoleBase;
|
|
324
425
|
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void;
|
|
325
426
|
}
|
|
326
427
|
|
|
327
|
-
export { All, Authorization, Body, BodyReadTimeoutMs, BodySizeLimit, CompressedBodySizeLimit, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook, Url, globalBodyReadTimeoutMs, globalBodySizeLimit, globalCompressedBodySizeLimit };
|
|
328
|
-
export type { TCookieAttributes, TCookieHook, THeaderHook, THttpHandlerMeta, TStatusHook };
|
|
428
|
+
export { All, AuthGuard, Authenticate, Authorization, Body, BodyReadTimeoutMs, BodySizeLimit, CompressedBodySizeLimit, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook, Upgrade, Url, defineAuthGuard, extractTransports, globalBodyReadTimeoutMs, globalBodySizeLimit, globalCompressedBodySizeLimit };
|
|
429
|
+
export type { TAuthGuardClass, TAuthGuardDef, TAuthGuardHandler, TAuthTransportApiKey, TAuthTransportBasic, TAuthTransportBearer, TAuthTransportCookie, TAuthTransportDeclaration, TAuthTransportValues, TCookieAttributes, TCookieHook, THeaderHook, THttpHandlerMeta, TStatusHook };
|