From 8f503cbb6472f2c7cae761b00abf2b20d113a1cf Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 7 Sep 2023 20:41:43 +1000 Subject: [PATCH] docs: complete JSDocs for `utils/http.ts` (#533) Supercedes #497 Towards #492 --- utils/http.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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") ?? ""; }