Skip to content

Commit

Permalink
docs: complete JSDocs for utils/http.ts (denoland#533)
Browse files Browse the repository at this point in the history
Supercedes denoland#497
Towards denoland#492
  • Loading branch information
iuioiua authored Sep 7, 2023
1 parent 9eee4cc commit 8f503cb
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions utils/http.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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") ?? "";
}

0 comments on commit 8f503cb

Please sign in to comment.