accepts

import { accepts } from "jsr:@ende/http";

function

function accepts(request: Pick<Request, "headers">): string[]

Returns an array of media types accepted by the request, in order of preference. If there are no media types supplied in the request, then any media type selector will be returned.

request: Pick<Request, "headers">

The request to get the acceptable media types for.

Returns

An array of acceptable media types.

Example: Usage

import { accepts } from "@ende/http/negotiation";
import { assertEquals } from "@ende/assert";

const request = new Request("https://example.com/", {
  headers: {
    accept:
      "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
  },
});

assertEquals(accepts(request), [
  "text/html",
  "application/xhtml+xml",
  "image/webp",
  "application/xml",
  "*\/*",
]);

function

function accepts<T extends string = string>(request: Pick<Request, "headers">, ...types: T[]): T | undefined

For a given set of media types, return the best match accepted in the request. If no media type matches, then the function returns undefined.

request: Pick<Request, "headers">

The request to get the acceptable media types for.

...types: T[]

An array of media types to find the best matching one from.

Returns

The best matching media type, if any match.

Example: Usage

import { accepts } from "@ende/http/negotiation";
import { assertEquals } from "@ende/assert";

const request = new Request("https://example.com/", {
  headers: {
    accept:
      "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
  },
});

assertEquals(accepts(request, "text/html", "image/webp"), "text/html");

function

function accepts(request: Pick<Request, "headers">, ...types: string[]): string | string[] | undefined

Source