Function accepts

  • 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.

    Parameters

    • request: Pick<Request, "headers">

      The request to get the acceptable media types for.

    Returns string[]

    An array of acceptable media types.

    import { accepts } from "@std/http/negotiation";
    import { assertEquals } from "@std/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",
    "*/*",
    ]);
  • 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.

    Parameters

    • request: Pick<Request, "headers">

      The request to get the acceptable media types for.

    • Rest...types: string[]

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

    Returns string | undefined

    The best matching media type, if any match.

    import { accepts } from "@std/http/negotiation";
    import { assertEquals } from "@std/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");