-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { z } from "zod"; | ||
import { getHeaders } from "./get-headers.js"; | ||
|
||
const FetchDestSchema = z.enum([ | ||
"audio", | ||
"audioworklet", | ||
"document", | ||
"embed", | ||
"empty", | ||
"font", | ||
"frame", | ||
"iframe", | ||
"image", | ||
"manifest", | ||
"object", | ||
"paintworklet", | ||
"report", | ||
"script", | ||
"serviceworker", | ||
"sharedworker", | ||
"style", | ||
"track", | ||
"video", | ||
"worker", | ||
"xslt", | ||
]); | ||
|
||
export type FetchDest = z.output<typeof FetchDestSchema>; | ||
|
||
/** | ||
* Returns the value of the `Sec-Fetch-Dest` header. | ||
* | ||
* The `Sec-Fetch-Dest` header indicates the destination of the request and | ||
* can be used to know if the request came from a `fetch` call or not, among | ||
* other things. | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Dest | ||
* @example | ||
* // Detect if the request came from a fetch call and return | ||
* // json, otherwise return a redirect | ||
* export async function action({ request }: ActionFunctionArgs) { | ||
* let dest = fetchDest(request); | ||
* if (dest === "empty") return json(data) | ||
* return redirect(destination) | ||
* } | ||
*/ | ||
export function fetchDest(request: Request): FetchDest | null; | ||
export function fetchDest(headers: Headers): FetchDest | null; | ||
export function fetchDest(input: Request | Headers): FetchDest | null { | ||
let header = getHeaders(input).get("Sec-Fetch-Dest"); | ||
let result = FetchDestSchema.safeParse(header); | ||
if (result.success) return result.data; | ||
return null; | ||
} | ||
|
||
const FetchModeSchema = z.enum([ | ||
"cors", | ||
"navigate", | ||
"no-cors", | ||
"same-origin", | ||
"websocket", | ||
]); | ||
|
||
export type FetchMode = z.output<typeof FetchModeSchema>; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode | ||
*/ | ||
export function fetchMode(request: Request): FetchMode | null; | ||
export function fetchMode(headers: Headers): FetchMode | null; | ||
export function fetchMode(input: Request | Headers): FetchMode | null { | ||
let headers = getHeaders(input).get("Set-Fetch-Mode"); | ||
let result = FetchModeSchema.safeParse(headers); | ||
if (result.success) return result.data; | ||
return null; | ||
} | ||
|
||
const FetchSiteSchema = z.enum([ | ||
"cross-site", | ||
"same-origin", | ||
"same-site", | ||
"none", | ||
]); | ||
|
||
export type FetchSite = z.output<typeof FetchSiteSchema>; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Site | ||
*/ | ||
export function fetchSite(request: Request): FetchSite | null; | ||
export function fetchSite(headers: Headers): FetchSite | null; | ||
export function fetchSite(input: Request | Headers): FetchSite | null { | ||
let headers = getHeaders(input).get("Set-Fetch-Site"); | ||
let result = FetchSiteSchema.safeParse(headers); | ||
if (result.success) return result.data; | ||
return null; | ||
} | ||
|
||
const FetchUserSchema = z.literal("?1"); | ||
|
||
export type FetchUser = z.output<typeof FetchUserSchema>; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-User | ||
*/ | ||
export function isUserInitiated(request: Request): boolean; | ||
export function isUserInitiated(headers: Headers): boolean; | ||
export function isUserInitiated(input: Request | Headers): boolean { | ||
let headers = getHeaders(input).get("Set-Fetch-User"); | ||
let result = FetchUserSchema.safeParse(headers); | ||
if (result.success) return true; | ||
return false; | ||
} |