diff --git a/utils/http.ts b/utils/http.ts index 22f77af6e..619ce4d89 100644 --- a/utils/http.ts +++ b/utils/http.ts @@ -1,14 +1,25 @@ // Copyright 2023 the Deno authors. All rights reserved. MIT license. -import type { RedirectStatus, Status } from "std/http/http_status.ts"; +import { RedirectStatus, Status } from "std/http/http_status.ts"; /** + * Returns a response that redirects the client to the given location (URL). + * * @param location A relative (to the request URL) or absolute URL. * @param status HTTP status + * * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location} + * + * @example + * ```ts + * import { redirect } from "@/utils/http.ts"; + * + * redirect("/new-page"); // Redirects client to `/new-page` with HTTP status 303 + * redirect("/new-page", 301); // Redirects client to `/new-page` with HTTP status 301 + * ``` */ export function redirect( location: string, - status: Status.Created | RedirectStatus = 303, + status: Status.Created | RedirectStatus = Status.SeeOther, ) { return new Response(null, { headers: { @@ -18,6 +29,17 @@ export function redirect( }); } +/** + * Returns the `cursor` URL parameter value of the given URL. + * + * @example + * ```ts + * import { getCursor } from "@/utils/http.ts"; + * + * getCursor(new URL("http://example.com?cursor=12345")); // Returns "12345" + * getCursor(new URL("http://example.com")); // Returns "" + * ``` + */ export function getCursor(url: URL) { return url.searchParams.get("cursor") ?? ""; }