-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
29 lines (28 loc) · 805 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { RedirectStatus, STATUS_CODE } from "$std/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: typeof STATUS_CODE.Created | RedirectStatus = STATUS_CODE.SeeOther,
) {
return new Response(null, {
headers: {
location,
},
status,
});
}