Skip to content

Commit

Permalink
docs: add jsdoc examples for response utils (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes authored Apr 17, 2024
1 parent d9c2c01 commit 94bc020
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
168 changes: 168 additions & 0 deletions docs/2.utils/2.reponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,68 @@ icon: material-symbols-light:output

Append a response header by name.

**Example:**

```ts
export default defineEventHandler((event) => {
appendResponseHeader(event, "content-type", "text/html");
});
```

### `appendHeaders(event, headers)`

Append the response headers.

**Example:**

```ts
export default defineEventHandler((event) => {
appendResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
```

### `appendResponseHeader(event, name, value)`

Append a response header by name.

**Example:**

```ts
export default defineEventHandler((event) => {
appendResponseHeader(event, "content-type", "text/html");
});
```

### `appendResponseHeaders(event, headers)`

Append the response headers.

**Example:**

```ts
export default defineEventHandler((event) => {
appendResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
```

### `clearResponseHeaders(event, headerNames?)`

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

**Example:**

```ts
export default defineEventHandler((event) => {
clearResponseHeaders(event, ["content-type", "cache-control"]); // Remove content-type and cache-control headers
});
```

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

Set the response status code and message.
Expand All @@ -36,18 +82,52 @@ Set the response status code and message.

Alias for `getResponseHeaders`.

**Example:**

```ts
export default defineEventHandler((event) => {
const contentType = getResponseHeader(event, "content-type"); // Get the response content-type header
});
```

### `getResponseHeaders(event)`

Get the response headers object.

**Example:**

```ts
export default defineEventHandler((event) => {
const headers = getResponseHeaders(event);
});
```

### `getResponseStatus(event)`

Get the current response status code.

**Example:**

```ts
export default defineEventHandler((event) => {
const status = getResponseStatus(event);
return `Status: ${status}`;
});
```

### `getResponseStatusText(event)`

Get the current response status message.

**Example:**

```ts
export default defineEventHandler((event) => {
const statusText = getResponseStatusText(event);
return `Status: ${statusText}`;
});
```

### `isStream(data)`

Checks if the data is a stream. (Node.js Readable Stream, React Pipeable Stream, or Web Stream)
Expand All @@ -60,6 +140,14 @@ Checks if the data is a Response object.

Remove a response header by name.

**Example:**

```ts
export default defineEventHandler((event) => {
removeResponseHeader(event, "content-type"); // Remove content-type header
});
```

### `send(event, data?, type?)`

Directly send a response to the client.
Expand Down Expand Up @@ -103,6 +191,23 @@ 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.

**Example:**

```ts
export default defineEventHandler((event) => {
return sendNoContent(event);
});
```

**Example:**

```ts
export default defineEventHandler((event) => {
sendNoContent(event); // Close the connection
console.log("This will not be executed");
});
```

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

Send a redirect response to the client.
Expand All @@ -111,6 +216,22 @@ It adds the `location` header to the response and sets the status code to 302 by

In the body, it sends a simple HTML page with a meta refresh tag to redirect the client in case the headers are ignored.

**Example:**

```ts
export default defineEventHandler((event) => {
return sendRedirect(event, "https://example.com");
});
```

**Example:**

```ts
export default defineEventHandler((event) => {
return sendRedirect(event, "https://example.com", 301); // Permanent redirect
});
```

### `sendStream(event, stream)`

Send a stream response to the client.
Expand All @@ -125,22 +246,69 @@ Send a Response object to the client.

Set a response header by name.

**Example:**

```ts
export default defineEventHandler((event) => {
setResponseHeader(event, "content-type", "text/html");
});
```

### `setHeaders(event)`

Set the response headers.

**Example:**

```ts
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
```

### `setResponseHeader(event, name, value)`

Set a response header by name.

**Example:**

```ts
export default defineEventHandler((event) => {
setResponseHeader(event, "content-type", "text/html");
});
```

### `setResponseHeaders(event)`

Set the response headers.

**Example:**

```ts
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
```

### `setResponseStatus(event, code?, text?)`

Set the response status code and message.

**Example:**

```ts
export default defineEventHandler((event) => {
setResponseStatus(event, 404, "Not Found");
return "Not Found";
});
```

### `writeEarlyHints(event, hints, cb)`

Write `HTTP/1.1 103 Early Hints` to the client.
Expand Down
Loading

0 comments on commit 94bc020

Please sign in to comment.