Skip to content

Commit c8aa150

Browse files
committed
docs: add basic jsdocs for utils
1 parent ddffb0e commit c8aa150

18 files changed

+385
-19
lines changed

Diff for: docs/.config/docs.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description:
66
in any JavaScript runtime.
77
github: unjs/h3
88
url: https://h3.unjs.io
9-
automd: true
9+
# automd: true # HMR seems unstable
1010
redirects: {}
1111
landing:
1212
heroLinks:

Diff for: docs/2.utils/1.request.md

+47
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,89 @@
66

77
### `assertMethod(event, expected, allowHead?)`
88

9+
Asserts that the incoming request method is of the expected type using `isMethod`.
10+
If the method is not allowed, it will throw a 405 error with the message "HTTP method is not allowed".
11+
912
### `getHeader(event, name)`
1013

14+
Get a request header by name.
15+
1116
### `getHeaders(event)`
1217

18+
Get the request headers object.
19+
Array headers are joined with a comma.
20+
1321
### `getQuery(event)`
1422

23+
Get query the params object from the request URL parsed with [unjs/ufo](https://ufo.unjs.io).
24+
1525
### `getRequestHeader(event, name)`
1626

27+
Get a request header by name.
28+
1729
### `getRequestHeaders(event)`
1830

31+
Get the request headers object.
32+
Array headers are joined with a comma.
33+
1934
### `getRequestHost(event, opts: { xForwardedHost? })`
2035

36+
Get the request hostname.
37+
If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
38+
If no host header is found, it will default to "localhost".
39+
2140
### `getRequestIP(event)`
2241

42+
Try to get the client IP address from the incoming request.
43+
If `xForwardedFor` is `true`, it will use the `x-forwarded-for` header if it exists.
44+
2345
### `getRequestProtocol(event, opts: { xForwardedProto? })`
2446

47+
Get the request protocol.
48+
If `x-forwarded-proto` header is set to "https", it will return "https". You can disable this behavior by setting `xForwardedProto` to `false`.
49+
If protocol cannot be determined, it will default to "http".
50+
2551
### `getRequestURL(event, opts: { xForwardedHost?, xForwardedProto? })`
2652

53+
Generated the full incoming request URL using `getRequestProtocol`, `getRequestHost` and `event.path`.
54+
If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
55+
If `xForwardedProto` is `false`, it will not use the `x-forwarded-proto` header.
56+
2757
### `getRouterParam(event, name, opts: { decode? })`
2858

59+
Get a matched route param by name.
60+
2961
### `getRouterParams(event, opts: { decode? })`
3062

63+
Get matched route params.
64+
If `decode` option is `true`, it will decode the matched route params using `decodeURI`.
65+
3166
### `getValidatedQuery(event, validate)`
3267

68+
Get the query param from the request URL parsed with [unjs/ufo](https://ufo.unjs.io) and validated with validate function.
69+
3370
### `getValidatedRouterParams(event, validate, opts: { decode? })`
3471

72+
Get matched route params and validate with validate function.
73+
3574
### `isMethod(event, expected, allowHead?)`
3675

76+
Checks if the incoming request method is of the expected type.
77+
If `allowHead` is `true`, it will allow `HEAD` requests to pass if the expected method is `GET`.
78+
3779
### `toWebRequest(event)`
3880

81+
Convert the H3Event to a WebRequest object.
82+
**NOTE:** This function is not stable and might have edge cases that are not handled properly.
83+
3984
<!-- /automd -->
4085

4186
<!-- automd:jsdocs src="../../src/utils/fingerprint.ts" -->
4287

4388
### `getRequestFingerprint(event, opts)`
4489

90+
Get a unique fingerprint for the incoming request.
91+
4592
<!-- /automd -->
4693

4794
## Body utils

Diff for: docs/2.utils/2.reponse.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,103 @@
66

77
### `appendHeader(event, name, value)`
88

9+
Append a response header by name.
10+
911
### `appendHeaders(event, headers)`
1012

13+
Append the response headers.
14+
1115
### `appendResponseHeader(event, name, value)`
1216

17+
Append a response header by name.
18+
1319
### `appendResponseHeaders(event, headers)`
1420

21+
Append the response headers.
22+
1523
### `clearResponseHeaders(event, headerNames?)`
1624

1725
Remove all response headers, or only those specified in the headerNames array.
1826

1927
### `defaultContentType(event, type?)`
2028

29+
Set the response status code and message.
30+
2131
### `getResponseHeader(event, name)`
2232

33+
Alias for `getResponseHeaders`.
34+
2335
### `getResponseHeaders(event)`
2436

37+
Get the response headers object.
38+
2539
### `getResponseStatus(event)`
2640

41+
Get the current response status code.
42+
2743
### `getResponseStatusText(event)`
2844

45+
Get the current response status message.
46+
2947
### `isStream(data)`
3048

49+
Checks if the data is a stream. (Node.js Readable Stream, React Pipeable Stream, or Web Stream)
50+
3151
### `isWebResponse(data)`
3252

53+
Checks if the data is a Response object.
54+
3355
### `removeResponseHeader(event, name)`
3456

57+
Remove a response header by name.
58+
3559
### `send(event, data?, type?)`
3660

61+
Directly send a response to the client.
62+
**Note:** This function should be used only when you want to send a response directly without using the `h3` event. Normaly you can directly `return` a value inside event handlers.
63+
3764
### `sendNoContent(event, code?)`
3865

39-
Respond with an empty payload.<br> Note that calling this function will close the connection and no other data can be sent to the client afterwards.
66+
Respond with an empty payload.<br>
67+
Note that calling this function will close the connection and no other data can be sent to the client afterwards.
4068

4169
### `sendRedirect(event, location, code)`
4270

71+
Send a redirect response to the client.
72+
It adds the `location` header to the response and sets the status code to 302 by default.
73+
In the body, it sends a simple HTML page with a meta refresh tag to redirect the client in case the headers are ignored.
74+
4375
### `sendStream(event, stream)`
4476

77+
Send a stream response to the client.
78+
Note: You can directly `return` a stream value inside event handlers alternatively which is recommended.
79+
4580
### `sendWebResponse(event, response)`
4681

82+
Send a Response object to the client.
83+
4784
### `setHeader(event, name, value)`
4885

86+
Set a response header by name.
87+
4988
### `setHeaders(event)`
5089

90+
Set the response headers.
91+
5192
### `setResponseHeader(event, name, value)`
5293

94+
Set a response header by name.
95+
5396
### `setResponseHeaders(event)`
5497

98+
Set the response headers.
99+
55100
### `setResponseStatus(event, code?, text?)`
56101

102+
Set the response status code and message.
103+
57104
### `writeEarlyHints(event, hints, cb)`
58105

106+
Write `HTTP/1.1 103 Early Hints` to the client.
107+
59108
<!-- /automd -->

Diff for: docs/2.utils/98.advanced.md

+35-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88

99
### `clearSession(event, config)`
1010

11+
Clear the session data for the current request.
12+
1113
### `getSession(event, config)`
1214

15+
Get the session for the current request.
16+
1317
### `sealSession(event, config)`
1418

19+
Encrypt and sign the session data for the current request.
20+
1521
### `unsealSession(_event, config, sealed)`
1622

23+
Decrypt and verify the session data for the current request.
24+
1725
### `updateSession(event, config, update?)`
1826

27+
Update the session data for the current request.
28+
1929
### `useSession(event, config)`
2030

31+
Create a session manager for the current request.
32+
2133
<!-- /automd -->
2234

2335
## Cookie utils
@@ -40,10 +52,6 @@ Parse the request to get HTTP Cookie header string and returning an object of al
4052

4153
Set a cookie value by name.
4254

43-
### `splitCookiesString(cookiesString)`
44-
45-
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas that are within a single set-cookie field-value, such as in the Expires portion. This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2 Node.js does this for every header _except_ set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128 Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25 Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
46-
4755
<!-- /automd -->
4856

4957
## Sanitize
@@ -52,8 +60,13 @@ Set-Cookie header field-values are sometimes comma joined in one string. This sp
5260

5361
### `sanitizeStatusCode(statusCode?, defaultStatusCode)`
5462

63+
Make sure the status code is a valid HTTP status code.
64+
5565
### `sanitizeStatusMessage(statusMessage)`
5666

67+
Make sure the status message is safe to use in a response.
68+
Allowed characters: horizontal tabs, spaces or visible ascii characters: https://www.rfc-editor.org/rfc/rfc7230#section-3.1.2
69+
5770
<!-- /automd -->
5871

5972
## Route
@@ -82,12 +95,20 @@ Check request caching headers (`If-Modified-Since`) and add caching headers (Las
8295

8396
### `fetchWithEvent(event, req, init?, options?: { fetch: F })`
8497

98+
Make a fetch request with the event's context and headers.
99+
85100
### `getProxyRequestHeaders(event)`
86101

102+
Get the request headers object without headers known to cause issues when proxying.
103+
87104
### `proxyRequest(event, target, opts)`
88105

106+
Proxy the incoming request to a target URL.
107+
89108
### `sendProxy(event, target, opts)`
90109

110+
Make a proxy request to a target URL and send the response back to the client.
111+
91112
<!-- /automd -->
92113

93114
## CORS
@@ -96,17 +117,24 @@ Check request caching headers (`If-Modified-Since`) and add caching headers (Las
96117

97118
### `appendCorsHeaders(event, options)`
98119

99-
c8 ignore end
100-
c8 ignore start
120+
Append CORS headers to the response.
101121

102122
### `appendCorsPreflightHeaders(event, options)`
103123

104-
c8 ignore start
124+
Append CORS preflight headers to the response.
105125

106126
### `handleCors(event, options)`
107127

128+
Handle CORS for the incoming request.
129+
If the incoming request is a CORS preflight request, it will append the CORS preflight headers and send a 204 response.
130+
If return value is `true`, the request is handled and no further action is needed.
131+
108132
### `isCorsOriginAllowed(origin, options)`
109133

134+
Check if the incoming request is a CORS request.
135+
110136
### `isPreflightRequest(event)`
111137

138+
Check if the incoming request is a CORS preflight request.
139+
112140
<!-- /automd -->

Diff for: docs/bun.lockb

0 Bytes
Binary file not shown.

Diff for: docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"dev": "undocs dev"
77
},
88
"devDependencies": {
9-
"undocs": "^0.2.12"
9+
"undocs": "^0.2.13"
1010
}
1111
}

Diff for: src/app.ts

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export interface App {
6868
use: AppUse;
6969
}
7070

71+
/**
72+
* Create a new H3 app instance.
73+
*/
7174
export function createApp(options: AppOptions = {}): App {
7275
const stack: Stack = [];
7376
const handler = createAppEventHandler(stack, options);

Diff for: src/router.ts

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export interface CreateRouterOptions {
4444
preemptive?: boolean;
4545
}
4646

47+
/**
48+
* Create a new h3 router instance.
49+
*/
4750
export function createRouter(opts: CreateRouterOptions = {}): Router {
4851
const _router = _createRouter<RouteNode>({});
4952
const routes: Record<string, RouteNode> = {};

Diff for: src/utils/cookie.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ export function deleteCookie(
7979
}
8080

8181
/**
82-
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
83-
* that are within a single set-cookie field-value, such as in the Expires portion.
82+
* Set-Cookie header field-values are sometimes comma joined in one string.
83+
*
84+
* This splits them without choking on commas that are within a single set-cookie field-value, such as in the Expires portion.
8485
* This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
8586
* Node.js does this for every header _except_ set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
8687
* Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
8788
* Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
8889
* @source https://github.com/nfriedly/set-cookie-parser/blob/3eab8b7d5d12c8ed87832532861c1a35520cf5b3/lib/set-cookie.js#L144
90+
*
91+
* @internal
8992
*/
9093
export function splitCookiesString(cookiesString: string | string[]): string[] {
9194
if (Array.isArray(cookiesString)) {

Diff for: src/utils/cors/handler.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import {
88
} from "./utils";
99
import type { H3CorsOptions } from "./types";
1010

11+
/**
12+
* Handle CORS for the incoming request.
13+
*
14+
* If the incoming request is a CORS preflight request, it will append the CORS preflight headers and send a 204 response.
15+
*
16+
* If return value is `true`, the request is handled and no further action is needed.
17+
*/
1118
export function handleCors(event: H3Event, options: H3CorsOptions): boolean {
1219
const _options = resolveCorsOptions(options);
1320
if (isPreflightRequest(event)) {

0 commit comments

Comments
 (0)