Request
Request Headers
getRequestHeaders
Get the request headers
import { eventHandler, getRequestHeaders } from "vinxi/http";
export default eventHandler(async (event) => {
const headers = getRequestHeaders(event);
});
Signature
export function getRequestHeaders(event: HTTPEvent): RequestHeaders;
getRequestHeader
Get a request header by name
import { eventHandler, getRequestHeader } from "vinxi/http";
export default eventHandler(async (event) => {
const header = getRequestHeader(event, "content-type");
});
Signature
export function getRequestHeader(
event: HTTPEvent,
name: HTTPHeaderName,
): RequestHeaders[string];
Request Body
readBody
Reads request body and tries to safely parse as JSON using destr.
import { eventHandler, readBody } from "vinxi/http";
export default eventHandler(async (event) => {
const body = await readBody(event);
});
Signature
function readBody<
T,
Event extends H3Event<EventHandlerRequest> = H3Event<EventHandlerRequest>,
_T = InferEventInput<"body", Event, T>,
>(event: Event, options?: { strict?: boolean }): Promise<_T>;
readFormData
Constructs a FormData
object from an event, after converting it to a a web request.
import { eventHandler, readFormData } from "vinxi/http";
export default eventHandler(async (event) => {
const formData = await readFormData(event);
const email = formData.get("email");
const password = formData.get("password");
});
Signature
export function readFormData(event: HTTPEvent): Promise<FormData>;
readMultipartFormData
Tries to read and parse the body of an HTTPEvent as a multipart form.
import { eventHandler, readMultipartFormData } from "vinxi/http";
export default eventHandler(async (event) => {
const data = await readMultipartFormData(event);
});
Signature
type MultiPartData = {
data: Buffer;
name?: string;
filename?: string;
type?: string;
};
export function readMultipartFormData(
event: HTTPEvent,
): Promise<MultiPartData[] | undefined>;
readValidatedBody
Tries to read the request body via readBody
, then uses the provided validation function and either throws a validation error or returns the result.
Using zod
import { eventHandler, readValidatedBody } from "vinxi/http";
import { z } from "zod";
const objectSchema = z.object({
email: z.string(),
password: z.string(),
});
//
export default eventHandler(async (event) => {
const body = await readValidatedBody(event, objectSchema.safeParse);
});
Using custom validation function
import { eventHandler, readValidatedBody } from "vinxi/http";
//
export default eventHandler(async (event) => {
// With a custom validation function
const body = await readValidatedBody(event, (body) => {
return typeof body === "object" && body !== null;
});
});
Signature
export function readValidatedBody<
T,
Event extends HTTPEvent = HTTPEvent,
_T = InferEventInput<"body", Event, T>,
>(event: Event, validate: ValidateFunction<_T>): Promise<_T>;
readRawBody
Reads body of the request and returns encoded raw string (default), or Buffer if encoding is falsy.
import { eventHandler, readRawBody } from "vinxi/http";
export default eventHandler(async (event) => {
const body = await readRawBody(event, "utf-8");
});
Signature
function readRawBody<E extends Encoding = "utf8">(
event: HTTPEvent,
encoding?: E,
): E extends false ? Promise<Buffer | undefined> : Promise<string | undefined>;
getRequestWebStream
Captures a ReadableStream from a request.
import { eventHandler, getRequestWebStream } from "vinxi/http";
export default eventHandler(async (event) => {
const stream = getRequestWebStream(event);
});
Signature
// @lib: es2015
// @filename: index.d.ts
import { HTTPEvent } from "vinxi/http";
import { ReadableStream } from "vinxi/types/web";
// ---cut---
export function getRequestWebStream(
event: HTTPEvent,
): ReadableStream | undefined;
Request Query
getQuery
Get the query
import { eventHandler, getQuery } from "vinxi/http";
export default eventHandler(async (event) => {
const query = getQuery(event);
});
Signature
export function getQuery<
T,
Event extends HTTPEvent = HTTPEvent,
_T = Exclude<InferEventInput<"query", Event, T>, undefined>,
>(event: Event): _T;
getValidatedQuery
Get the validated query
import { eventHandler, getValidatedQuery } from "vinxi/http";
export default eventHandler(async (event) => {
const query = await getValidatedQuery(event, (query) => {
return typeof query === "object" && query !== null;
});
});
Signature
export function getValidatedQuery<
T,
Event extends HTTPEvent = HTTPEvent,
_T = InferEventInput<"query", Event, T>,
>(event: Event, validate: ValidateFunction<_T>): Promise<_T>;
Request Info
getRequestHost
Get the request host
import { eventHandler, getRequestHost } from "vinxi/http";
export default eventHandler(async (event) => {
const host = getRequestHost(event);
});
Signature
export function getRequestHost(
event: HTTPEvent,
opts?: {
xForwardedHost?: boolean;
},
): string;
getRequestProtocol
Get the request protocol, whether its http
or https
.
import { eventHandler, getRequestProtocol } from "vinxi/http";
export default eventHandler(async (event) => {
const protocol = getRequestProtocol(event);
});
Signature
export function getRequestProtocol(
event: HTTPEvent,
opts?: {
xForwardedProto?: boolean;
},
): "https" | "http";
getRequestURL
Get the request URL
// @noErrors
import { eventHandler, getRequestURL } from "vinxi/http";
export default eventHandler(async (event) => {
const url = getRequestURL(event);
url.protocol; // "http"
url.hostname; // "localhost"
url.port; // "3000"
url.host; // "localhost:3000"
url.username; // ""
url.password; // ""
url.origin; // "http://localhost:3000"
url.pathname; // "/products"
url.search; // "?category=shoes"
url.searchParams; // URLSearchParams { category: "shoes" };
url.hash; // "#section";
url.href; // "http://localhost:3000/products?category=shoes#section";
});
Signature
export function getRequestURL(
event: HTTPEvent,
opts?: {
xForwardedHost?: boolean;
xForwardedProto?: boolean;
},
): URL;
getRequestIP
Get the request IP, if visible.
import { eventHandler, getRequestIP } from "vinxi/http";
export default eventHandler(async (event) => {
const ip = getRequestIP(event);
});
Signature
export function getRequestIP(
event: HTTPEvent,
opts?: {
xForwardedFor?: boolean;
},
): string | undefined;
isPreflightRequest
Check if the request is a CORS preflight request
import { eventHandler, isPreflightRequest } from "vinxi/http";
export default eventHandler(async (event) => {
const isPreflight = isPreflightRequest(event);
});
Signature
export function isPreflightRequest(event: HTTPEvent): boolean;
getWebRequest
Get a Web Fetch API compliant Request
instance from the HTTPEvent
import { eventHandler, getWebRequest } from "vinxi/http";
export default eventHandler(async (event) => {
const request = getWebRequest(event);
request.url;
request.method;
request.headers;
request.json();
request.formData();
request.text();
request.arrayBuffer();
request.blob();
});
Signature
export function getWebRequest(event: HTTPEvent): Request;