@wooksjs/event-http 0.7.0 → 0.7.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/dist/index.cjs +22 -22
- package/dist/index.d.ts +17 -17
- package/dist/index.mjs +22 -22
- package/package.json +5 -5
- package/skills/wooksjs-event-http/SKILL.md +2 -2
- package/skills/wooksjs-event-http/core.md +7 -7
- package/skills/wooksjs-event-http/request.md +33 -33
- package/skills/wooksjs-event-http/testing.md +2 -2
package/dist/index.cjs
CHANGED
|
@@ -80,12 +80,12 @@ const parseCookieValue = (0, __wooksjs_event_core.cachedBy)((name, ctx) => {
|
|
|
80
80
|
* Provides access to parsed request cookies.
|
|
81
81
|
* @example
|
|
82
82
|
* ```ts
|
|
83
|
-
* const { getCookie,
|
|
83
|
+
* const { getCookie, raw } = useCookies()
|
|
84
84
|
* const sessionId = getCookie('session_id')
|
|
85
85
|
* ```
|
|
86
86
|
*/
|
|
87
87
|
const useCookies = (0, __wooksjs_event_core.defineWook)((ctx) => ({
|
|
88
|
-
|
|
88
|
+
raw: ctx.get(httpKind.keys.req).headers.cookie,
|
|
89
89
|
getCookie: (name) => parseCookieValue(name, ctx)
|
|
90
90
|
}));
|
|
91
91
|
|
|
@@ -107,7 +107,7 @@ const useAccept = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
|
107
107
|
const accept = ctx.get(httpKind.keys.req).headers.accept;
|
|
108
108
|
return {
|
|
109
109
|
accept,
|
|
110
|
-
|
|
110
|
+
has: (type) => acceptsMime(type, ctx)
|
|
111
111
|
};
|
|
112
112
|
});
|
|
113
113
|
|
|
@@ -152,17 +152,17 @@ const authIsSlot = (0, __wooksjs_event_core.cachedBy)((type, ctx) => {
|
|
|
152
152
|
* Provides parsed access to the Authorization header (type, credentials, Basic decoding).
|
|
153
153
|
* @example
|
|
154
154
|
* ```ts
|
|
155
|
-
* const {
|
|
156
|
-
* if (
|
|
155
|
+
* const { is, credentials, basicCredentials } = useAuthorization()
|
|
156
|
+
* if (is('bearer')) { const token = credentials() }
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
159
|
const useAuthorization = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
160
160
|
const authorization = ctx.get(httpKind.keys.req).headers.authorization;
|
|
161
161
|
return {
|
|
162
162
|
authorization,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
163
|
+
type: () => ctx.get(authTypeSlot),
|
|
164
|
+
credentials: () => ctx.get(authCredentialsSlot),
|
|
165
|
+
is: (type) => authIsSlot(type, ctx),
|
|
166
166
|
basicCredentials: () => ctx.get(basicCredentialsSlot)
|
|
167
167
|
};
|
|
168
168
|
});
|
|
@@ -520,7 +520,7 @@ const ipListSlot = (0, __wooksjs_event_core.cached)((ctx) => {
|
|
|
520
520
|
* Provides access to the incoming HTTP request (method, url, headers, body, IP).
|
|
521
521
|
* @example
|
|
522
522
|
* ```ts
|
|
523
|
-
* const { method, url, rawBody, getIp } = useRequest()
|
|
523
|
+
* const { method, url, raw, rawBody, getIp } = useRequest()
|
|
524
524
|
* const body = await rawBody()
|
|
525
525
|
* ```
|
|
526
526
|
*/
|
|
@@ -551,7 +551,7 @@ const useRequest = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
|
551
551
|
return ctx.get(remoteIpSlot);
|
|
552
552
|
}
|
|
553
553
|
return {
|
|
554
|
-
|
|
554
|
+
raw: req,
|
|
555
555
|
url: req.url,
|
|
556
556
|
method: req.method,
|
|
557
557
|
headers: req.headers,
|
|
@@ -647,14 +647,14 @@ const urlSearchParamsSlot = (0, __wooksjs_event_core.cached)((ctx) => new WooksU
|
|
|
647
647
|
* Provides access to URL search (query) parameters from the request.
|
|
648
648
|
* @example
|
|
649
649
|
* ```ts
|
|
650
|
-
* const {
|
|
651
|
-
* const page =
|
|
650
|
+
* const { params, toJson } = useUrlParams()
|
|
651
|
+
* const page = params().get('page')
|
|
652
652
|
* ```
|
|
653
653
|
*/
|
|
654
|
-
const
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
654
|
+
const useUrlParams = (0, __wooksjs_event_core.defineWook)((ctx) => ({
|
|
655
|
+
raw: () => ctx.get(rawSearchParamsSlot),
|
|
656
|
+
params: () => ctx.get(urlSearchParamsSlot),
|
|
657
|
+
toJson: () => ctx.get(urlSearchParamsSlot).toJson()
|
|
658
658
|
}));
|
|
659
659
|
|
|
660
660
|
//#endregion
|
|
@@ -1394,7 +1394,7 @@ function error_tl_default(ctx) {
|
|
|
1394
1394
|
//#endregion
|
|
1395
1395
|
//#region packages/event-http/src/response/wooks-http-response.ts
|
|
1396
1396
|
let framework = {
|
|
1397
|
-
version: "0.
|
|
1397
|
+
version: "0.7.0",
|
|
1398
1398
|
poweredBy: "wooksjs",
|
|
1399
1399
|
link: "https://wooks.moost.org/",
|
|
1400
1400
|
image: "https://wooks.moost.org/wooks-full-logo.png"
|
|
@@ -1418,14 +1418,14 @@ var WooksHttpResponse = class extends HttpResponse {
|
|
|
1418
1418
|
}
|
|
1419
1419
|
renderError(data, ctx) {
|
|
1420
1420
|
this._status = data.statusCode || 500;
|
|
1421
|
-
const {
|
|
1422
|
-
if (
|
|
1421
|
+
const { has } = useAccept(ctx);
|
|
1422
|
+
if (has("json")) {
|
|
1423
1423
|
this._headers["content-type"] = "application/json";
|
|
1424
1424
|
this._body = JSON.stringify(data);
|
|
1425
|
-
} else if (
|
|
1425
|
+
} else if (has("html")) {
|
|
1426
1426
|
this._headers["content-type"] = "text/html";
|
|
1427
1427
|
this._body = renderErrorHtml(data);
|
|
1428
|
-
} else if (
|
|
1428
|
+
} else if (has("text")) {
|
|
1429
1429
|
this._headers["content-type"] = "text/plain";
|
|
1430
1430
|
this._body = renderErrorText(data);
|
|
1431
1431
|
} else {
|
|
@@ -1871,4 +1871,4 @@ Object.defineProperty(exports, 'useRouteParams', {
|
|
|
1871
1871
|
return __wooksjs_event_core.useRouteParams;
|
|
1872
1872
|
}
|
|
1873
1873
|
});
|
|
1874
|
-
exports.
|
|
1874
|
+
exports.useUrlParams = useUrlParams;
|
package/dist/index.d.ts
CHANGED
|
@@ -15,12 +15,12 @@ import { Duplex } from 'stream';
|
|
|
15
15
|
* Provides access to parsed request cookies.
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
|
-
* const { getCookie,
|
|
18
|
+
* const { getCookie, raw } = useCookies()
|
|
19
19
|
* const sessionId = getCookie('session_id')
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
declare const useCookies: (ctx?: EventContext) => {
|
|
23
|
-
|
|
23
|
+
raw: string | undefined;
|
|
24
24
|
getCookie: (name: string) => string | null;
|
|
25
25
|
};
|
|
26
26
|
|
|
@@ -29,7 +29,7 @@ type KnownAcceptType = 'json' | 'html' | 'xml' | 'text';
|
|
|
29
29
|
/** Provides helpers to check the request's Accept header for supported MIME types. */
|
|
30
30
|
declare const useAccept: (ctx?: EventContext) => {
|
|
31
31
|
accept: string | undefined;
|
|
32
|
-
|
|
32
|
+
has: (type: KnownAcceptType | (string & {})) => boolean;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
/** Short names for common Authorization schemes. */
|
|
@@ -38,15 +38,15 @@ type KnownAuthType = 'basic' | 'bearer';
|
|
|
38
38
|
* Provides parsed access to the Authorization header (type, credentials, Basic decoding).
|
|
39
39
|
* @example
|
|
40
40
|
* ```ts
|
|
41
|
-
* const {
|
|
42
|
-
* if (
|
|
41
|
+
* const { is, credentials, basicCredentials } = useAuthorization()
|
|
42
|
+
* if (is('bearer')) { const token = credentials() }
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
declare const useAuthorization: (ctx?: EventContext) => {
|
|
46
46
|
authorization: string | undefined;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
type: () => string | null;
|
|
48
|
+
credentials: () => string | null;
|
|
49
|
+
is: (type: KnownAuthType | (string & {})) => boolean;
|
|
50
50
|
basicCredentials: () => {
|
|
51
51
|
username: string;
|
|
52
52
|
password: string;
|
|
@@ -75,12 +75,12 @@ declare const rawBodySlot: _wooksjs_event_core.Cached<Promise<Buffer$1<ArrayBuff
|
|
|
75
75
|
* Provides access to the incoming HTTP request (method, url, headers, body, IP).
|
|
76
76
|
* @example
|
|
77
77
|
* ```ts
|
|
78
|
-
* const { method, url, rawBody, getIp } = useRequest()
|
|
78
|
+
* const { method, url, raw, rawBody, getIp } = useRequest()
|
|
79
79
|
* const body = await rawBody()
|
|
80
80
|
* ```
|
|
81
81
|
*/
|
|
82
82
|
declare const useRequest: (ctx?: EventContext) => {
|
|
83
|
-
|
|
83
|
+
raw: http.IncomingMessage;
|
|
84
84
|
url: string | undefined;
|
|
85
85
|
method: string | undefined;
|
|
86
86
|
headers: http.IncomingHttpHeaders;
|
|
@@ -459,14 +459,14 @@ declare class WooksURLSearchParams extends URLSearchParams {
|
|
|
459
459
|
* Provides access to URL search (query) parameters from the request.
|
|
460
460
|
* @example
|
|
461
461
|
* ```ts
|
|
462
|
-
* const {
|
|
463
|
-
* const page =
|
|
462
|
+
* const { params, toJson } = useUrlParams()
|
|
463
|
+
* const page = params().get('page')
|
|
464
464
|
* ```
|
|
465
465
|
*/
|
|
466
|
-
declare const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
466
|
+
declare const useUrlParams: (ctx?: EventContext) => {
|
|
467
|
+
raw: () => string;
|
|
468
|
+
params: () => WooksURLSearchParams;
|
|
469
|
+
toJson: () => unknown;
|
|
470
470
|
};
|
|
471
471
|
|
|
472
472
|
/** Creates an async event context for an incoming HTTP request/response pair. */
|
|
@@ -671,5 +671,5 @@ interface TTestHttpContext {
|
|
|
671
671
|
*/
|
|
672
672
|
declare function prepareTestHttpContext(options: TTestHttpContext): <T>(cb: (...a: any[]) => T) => T;
|
|
673
673
|
|
|
674
|
-
export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse,
|
|
674
|
+
export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams };
|
|
675
675
|
export type { KnownAcceptType, KnownAuthType, SecurityHeadersOptions, TCacheControl, TCookieAttributes, TCookieAttributesInput, THttpEventData, TRequestLimits, TSetCookieData, TTestHttpContext, TWooksErrorBody, TWooksErrorBodyExt, TWooksHttpOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -57,12 +57,12 @@ const parseCookieValue = cachedBy((name, ctx) => {
|
|
|
57
57
|
* Provides access to parsed request cookies.
|
|
58
58
|
* @example
|
|
59
59
|
* ```ts
|
|
60
|
-
* const { getCookie,
|
|
60
|
+
* const { getCookie, raw } = useCookies()
|
|
61
61
|
* const sessionId = getCookie('session_id')
|
|
62
62
|
* ```
|
|
63
63
|
*/
|
|
64
64
|
const useCookies = defineWook((ctx) => ({
|
|
65
|
-
|
|
65
|
+
raw: ctx.get(httpKind.keys.req).headers.cookie,
|
|
66
66
|
getCookie: (name) => parseCookieValue(name, ctx)
|
|
67
67
|
}));
|
|
68
68
|
|
|
@@ -84,7 +84,7 @@ const useAccept = defineWook((ctx) => {
|
|
|
84
84
|
const accept = ctx.get(httpKind.keys.req).headers.accept;
|
|
85
85
|
return {
|
|
86
86
|
accept,
|
|
87
|
-
|
|
87
|
+
has: (type) => acceptsMime(type, ctx)
|
|
88
88
|
};
|
|
89
89
|
});
|
|
90
90
|
|
|
@@ -129,17 +129,17 @@ const authIsSlot = cachedBy((type, ctx) => {
|
|
|
129
129
|
* Provides parsed access to the Authorization header (type, credentials, Basic decoding).
|
|
130
130
|
* @example
|
|
131
131
|
* ```ts
|
|
132
|
-
* const {
|
|
133
|
-
* if (
|
|
132
|
+
* const { is, credentials, basicCredentials } = useAuthorization()
|
|
133
|
+
* if (is('bearer')) { const token = credentials() }
|
|
134
134
|
* ```
|
|
135
135
|
*/
|
|
136
136
|
const useAuthorization = defineWook((ctx) => {
|
|
137
137
|
const authorization = ctx.get(httpKind.keys.req).headers.authorization;
|
|
138
138
|
return {
|
|
139
139
|
authorization,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
type: () => ctx.get(authTypeSlot),
|
|
141
|
+
credentials: () => ctx.get(authCredentialsSlot),
|
|
142
|
+
is: (type) => authIsSlot(type, ctx),
|
|
143
143
|
basicCredentials: () => ctx.get(basicCredentialsSlot)
|
|
144
144
|
};
|
|
145
145
|
});
|
|
@@ -497,7 +497,7 @@ const ipListSlot = cached((ctx) => {
|
|
|
497
497
|
* Provides access to the incoming HTTP request (method, url, headers, body, IP).
|
|
498
498
|
* @example
|
|
499
499
|
* ```ts
|
|
500
|
-
* const { method, url, rawBody, getIp } = useRequest()
|
|
500
|
+
* const { method, url, raw, rawBody, getIp } = useRequest()
|
|
501
501
|
* const body = await rawBody()
|
|
502
502
|
* ```
|
|
503
503
|
*/
|
|
@@ -528,7 +528,7 @@ const useRequest = defineWook((ctx) => {
|
|
|
528
528
|
return ctx.get(remoteIpSlot);
|
|
529
529
|
}
|
|
530
530
|
return {
|
|
531
|
-
|
|
531
|
+
raw: req,
|
|
532
532
|
url: req.url,
|
|
533
533
|
method: req.method,
|
|
534
534
|
headers: req.headers,
|
|
@@ -624,14 +624,14 @@ const urlSearchParamsSlot = cached((ctx) => new WooksURLSearchParams(ctx.get(raw
|
|
|
624
624
|
* Provides access to URL search (query) parameters from the request.
|
|
625
625
|
* @example
|
|
626
626
|
* ```ts
|
|
627
|
-
* const {
|
|
628
|
-
* const page =
|
|
627
|
+
* const { params, toJson } = useUrlParams()
|
|
628
|
+
* const page = params().get('page')
|
|
629
629
|
* ```
|
|
630
630
|
*/
|
|
631
|
-
const
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
631
|
+
const useUrlParams = defineWook((ctx) => ({
|
|
632
|
+
raw: () => ctx.get(rawSearchParamsSlot),
|
|
633
|
+
params: () => ctx.get(urlSearchParamsSlot),
|
|
634
|
+
toJson: () => ctx.get(urlSearchParamsSlot).toJson()
|
|
635
635
|
}));
|
|
636
636
|
|
|
637
637
|
//#endregion
|
|
@@ -1371,7 +1371,7 @@ function error_tl_default(ctx) {
|
|
|
1371
1371
|
//#endregion
|
|
1372
1372
|
//#region packages/event-http/src/response/wooks-http-response.ts
|
|
1373
1373
|
let framework = {
|
|
1374
|
-
version: "0.
|
|
1374
|
+
version: "0.7.0",
|
|
1375
1375
|
poweredBy: "wooksjs",
|
|
1376
1376
|
link: "https://wooks.moost.org/",
|
|
1377
1377
|
image: "https://wooks.moost.org/wooks-full-logo.png"
|
|
@@ -1395,14 +1395,14 @@ var WooksHttpResponse = class extends HttpResponse {
|
|
|
1395
1395
|
}
|
|
1396
1396
|
renderError(data, ctx) {
|
|
1397
1397
|
this._status = data.statusCode || 500;
|
|
1398
|
-
const {
|
|
1399
|
-
if (
|
|
1398
|
+
const { has } = useAccept(ctx);
|
|
1399
|
+
if (has("json")) {
|
|
1400
1400
|
this._headers["content-type"] = "application/json";
|
|
1401
1401
|
this._body = JSON.stringify(data);
|
|
1402
|
-
} else if (
|
|
1402
|
+
} else if (has("html")) {
|
|
1403
1403
|
this._headers["content-type"] = "text/html";
|
|
1404
1404
|
this._body = renderErrorHtml(data);
|
|
1405
|
-
} else if (
|
|
1405
|
+
} else if (has("text")) {
|
|
1406
1406
|
this._headers["content-type"] = "text/plain";
|
|
1407
1407
|
this._body = renderErrorText(data);
|
|
1408
1408
|
} else {
|
|
@@ -1814,4 +1814,4 @@ function prepareTestHttpContext(options) {
|
|
|
1814
1814
|
}
|
|
1815
1815
|
|
|
1816
1816
|
//#endregion
|
|
1817
|
-
export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useLogger, useRequest, useResponse, useRouteParams,
|
|
1817
|
+
export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useLogger, useRequest, useResponse, useRouteParams, useUrlParams };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -47,14 +47,14 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"typescript": "^5.9.3",
|
|
49
49
|
"vitest": "^3.2.4",
|
|
50
|
-
"@wooksjs/event-core": "^0.7.
|
|
51
|
-
"wooks": "^0.7.
|
|
50
|
+
"@wooksjs/event-core": "^0.7.1",
|
|
51
|
+
"wooks": "^0.7.1"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@prostojs/logger": "^0.4.3",
|
|
55
55
|
"@prostojs/router": "^0.3.2",
|
|
56
|
-
"@wooksjs/event-core": "^0.7.
|
|
57
|
-
"wooks": "^0.7.
|
|
56
|
+
"@wooksjs/event-core": "^0.7.1",
|
|
57
|
+
"wooks": "^0.7.1"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"build": "rolldown -c ../../rolldown.config.mjs",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: wooksjs-event-http
|
|
3
|
-
description: Use this skill when working with @wooksjs/event-http — to create HTTP servers with createHttpApp(), register route handlers with app.get()/post()/put()/patch()/delete(), read request data with useRequest()/useHeaders()/useCookies()/
|
|
3
|
+
description: Use this skill when working with @wooksjs/event-http — to create HTTP servers with createHttpApp(), register route handlers with app.get()/post()/put()/patch()/delete(), read request data with useRequest()/useHeaders()/useCookies()/useUrlParams()/useAuthorization()/useAccept(), control responses with useResponse() and HttpResponse (status, headers, cookies, cache control, streaming), throw HTTP errors with HttpError, test handlers with prepareTestHttpContext(), or integrate with existing Node.js servers via getServerCb().
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# @wooksjs/event-http
|
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
useResponse,
|
|
30
30
|
useHeaders,
|
|
31
31
|
useCookies,
|
|
32
|
-
|
|
32
|
+
useUrlParams,
|
|
33
33
|
useAuthorization,
|
|
34
34
|
useAccept,
|
|
35
35
|
useRouteParams,
|
|
@@ -32,13 +32,13 @@ app.listen(3000)
|
|
|
32
32
|
|
|
33
33
|
**Options (`TWooksHttpOptions`):**
|
|
34
34
|
|
|
35
|
-
| Option
|
|
36
|
-
|
|
|
37
|
-
| `logger`
|
|
38
|
-
| `onNotFound`
|
|
39
|
-
| `router`
|
|
40
|
-
| `requestLimits`
|
|
41
|
-
| `responseClass`
|
|
35
|
+
| Option | Type | Description |
|
|
36
|
+
| ---------------- | ------------------------------------ | ------------------------------------------------------------------------- |
|
|
37
|
+
| `logger` | `TConsoleBase` | Custom logger instance |
|
|
38
|
+
| `onNotFound` | `TWooksHandler` | Handler called when no route matches (default: 404 HttpError) |
|
|
39
|
+
| `router` | router options | Custom router configuration |
|
|
40
|
+
| `requestLimits` | `Omit<TRequestLimits, 'perRequest'>` | Default body size/timeout limits for all requests |
|
|
41
|
+
| `responseClass` | `typeof WooksHttpResponse` | Custom response subclass (default: `WooksHttpResponse`) |
|
|
42
42
|
| `defaultHeaders` | `Record<string, string \| string[]>` | Default headers applied to every response (e.g. from `securityHeaders()`) |
|
|
43
43
|
|
|
44
44
|
### Route registration
|
|
@@ -22,17 +22,17 @@ const { method, url, headers, rawBody, getIp, reqId } = useRequest()
|
|
|
22
22
|
|
|
23
23
|
**Returned properties:**
|
|
24
24
|
|
|
25
|
-
| Property | Type
|
|
26
|
-
| ---------------- |
|
|
27
|
-
| `
|
|
28
|
-
| `url` | `string`
|
|
29
|
-
| `method` | `string`
|
|
30
|
-
| `headers` | `IncomingHttpHeaders`
|
|
31
|
-
| `rawBody` | `() => Promise<Buffer>`
|
|
32
|
-
| `reqId` | `() => string`
|
|
33
|
-
| `getIp(opts?)` | `(opts?: { trustProxy: boolean }) => string`
|
|
25
|
+
| Property | Type | Description |
|
|
26
|
+
| ---------------- | ------------------------------------------------- | -------------------------------------------------- |
|
|
27
|
+
| `raw` | `IncomingMessage` | Node.js raw request object |
|
|
28
|
+
| `url` | `string` | Request URL |
|
|
29
|
+
| `method` | `string` | HTTP method |
|
|
30
|
+
| `headers` | `IncomingHttpHeaders` | Request headers |
|
|
31
|
+
| `rawBody` | `() => Promise<Buffer>` | Lazy — reads and decompresses request body on call |
|
|
32
|
+
| `reqId` | `() => string` | Lazy UUID per request |
|
|
33
|
+
| `getIp(opts?)` | `(opts?: { trustProxy: boolean }) => string` | Client IP (with optional proxy trust) |
|
|
34
34
|
| `getIpList()` | `() => { remoteIp: string; forwarded: string[] }` | All IPs (remote + X-Forwarded-For) |
|
|
35
|
-
| `isCompressed()` | `() => boolean`
|
|
35
|
+
| `isCompressed()` | `() => boolean` | Whether the request body is compressed |
|
|
36
36
|
|
|
37
37
|
**Request limits (per-request override):**
|
|
38
38
|
|
|
@@ -61,30 +61,30 @@ Parses incoming request cookies lazily (per cookie name, via `cachedBy`).
|
|
|
61
61
|
```ts
|
|
62
62
|
import { useCookies } from '@wooksjs/event-http'
|
|
63
63
|
|
|
64
|
-
const { getCookie,
|
|
64
|
+
const { getCookie, raw } = useCookies()
|
|
65
65
|
const session = getCookie('session_id') // parsed + cached
|
|
66
66
|
const theme = getCookie('theme') // parsed + cached (different key)
|
|
67
|
-
|
|
67
|
+
// raw = raw Cookie header string
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
### `
|
|
70
|
+
### `useUrlParams(ctx?)`
|
|
71
71
|
|
|
72
72
|
Provides access to URL query parameters.
|
|
73
73
|
|
|
74
74
|
```ts
|
|
75
|
-
import {
|
|
75
|
+
import { useUrlParams } from '@wooksjs/event-http'
|
|
76
76
|
|
|
77
|
-
const {
|
|
77
|
+
const { params, toJson, raw } = useUrlParams()
|
|
78
78
|
|
|
79
79
|
// URLSearchParams API
|
|
80
|
-
const page =
|
|
81
|
-
const tags =
|
|
80
|
+
const page = params().get('page')
|
|
81
|
+
const tags = params().getAll('tag')
|
|
82
82
|
|
|
83
83
|
// As a plain object
|
|
84
|
-
const query =
|
|
84
|
+
const query = toJson() // { page: '1', tag: ['a', 'b'] }
|
|
85
85
|
|
|
86
86
|
// Raw query string
|
|
87
|
-
const
|
|
87
|
+
const rawQuery = raw() // '?page=1&tag=a&tag=b'
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
### `useAuthorization(ctx?)`
|
|
@@ -94,13 +94,13 @@ Parses the Authorization header (supports Basic and Bearer).
|
|
|
94
94
|
```ts
|
|
95
95
|
import { useAuthorization } from '@wooksjs/event-http'
|
|
96
96
|
|
|
97
|
-
const { authorization,
|
|
97
|
+
const { authorization, type, credentials, is, basicCredentials } = useAuthorization()
|
|
98
98
|
|
|
99
|
-
if (
|
|
100
|
-
const token =
|
|
99
|
+
if (is('bearer')) {
|
|
100
|
+
const token = credentials() // the raw token string
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
if (
|
|
103
|
+
if (is('basic')) {
|
|
104
104
|
const { username, password } = basicCredentials()!
|
|
105
105
|
}
|
|
106
106
|
```
|
|
@@ -110,9 +110,9 @@ if (authIs('basic')) {
|
|
|
110
110
|
| Property | Type | Description |
|
|
111
111
|
| ---------------------- | -------------------------------- | -------------------------------------------------------------- |
|
|
112
112
|
| `authorization` | `string \| undefined` | Raw Authorization header value |
|
|
113
|
-
| `
|
|
114
|
-
| `
|
|
115
|
-
| `
|
|
113
|
+
| `type()` | `string \| null` | Auth scheme: `'Basic'`, `'Bearer'`, etc. |
|
|
114
|
+
| `credentials()` | `string \| null` | Everything after the scheme |
|
|
115
|
+
| `is(type)` | `boolean` | Check auth scheme: `'basic'`, `'bearer'`, or any custom scheme |
|
|
116
116
|
| `basicCredentials()` | `{ username, password } \| null` | Decoded Basic credentials |
|
|
117
117
|
|
|
118
118
|
### `useAccept(ctx?)`
|
|
@@ -122,12 +122,12 @@ Checks the request's `Accept` header for MIME type support. Uses short names (`'
|
|
|
122
122
|
```ts
|
|
123
123
|
import { useAccept } from '@wooksjs/event-http'
|
|
124
124
|
|
|
125
|
-
const { accept,
|
|
125
|
+
const { accept, has } = useAccept()
|
|
126
126
|
|
|
127
|
-
if (
|
|
127
|
+
if (has('json')) {
|
|
128
128
|
/* ... */
|
|
129
129
|
}
|
|
130
|
-
if (
|
|
130
|
+
if (has('image/png')) {
|
|
131
131
|
/* ... */
|
|
132
132
|
}
|
|
133
133
|
```
|
|
@@ -162,10 +162,10 @@ log.info('handling request')
|
|
|
162
162
|
|
|
163
163
|
```ts
|
|
164
164
|
app.post('/admin/action', async () => {
|
|
165
|
-
const {
|
|
166
|
-
if (!
|
|
165
|
+
const { is, credentials } = useAuthorization()
|
|
166
|
+
if (!is('bearer')) throw new HttpError(401)
|
|
167
167
|
|
|
168
|
-
const token =
|
|
168
|
+
const token = credentials()!
|
|
169
169
|
const user = await verifyToken(token)
|
|
170
170
|
if (!user.isAdmin) throw new HttpError(403)
|
|
171
171
|
|
|
@@ -184,7 +184,7 @@ app.get('/hot-path', () => {
|
|
|
184
184
|
const ctx = current()
|
|
185
185
|
const { method } = useRequest(ctx)
|
|
186
186
|
const { getCookie } = useCookies(ctx)
|
|
187
|
-
const {
|
|
187
|
+
const { params } = useUrlParams(ctx)
|
|
188
188
|
// 1 ALS lookup instead of 3
|
|
189
189
|
})
|
|
190
190
|
```
|
|
@@ -40,10 +40,10 @@ run(() => {
|
|
|
40
40
|
// All composables work here
|
|
41
41
|
const { method } = useRequest()
|
|
42
42
|
const { params } = useRouteParams()
|
|
43
|
-
const {
|
|
43
|
+
const { is } = useAuthorization()
|
|
44
44
|
expect(method).toBe('GET')
|
|
45
45
|
expect(params.id).toBe('42')
|
|
46
|
-
expect(
|
|
46
|
+
expect(is('bearer')).toBe(true)
|
|
47
47
|
})
|
|
48
48
|
```
|
|
49
49
|
|