xhr

xhr ci

A XMLHttpRequest polyfill for Deno CLI and Deno Deploy. The main intent is to make code written for a browser that uses XMLHttpRequest to fetch JSON, bytes, or text asynchronously work with Deno.

![WARNING] At this stage, this polyfill is very experimental and is not well tested. USE AT YOUR OWN RISK. Bug reports are welcome.

This polyfill has several known/intentional limitations from a browser standard XMLHttpRequest:

  • It does not handle XML, though the name implies it, nor does it handle HTML being treated as a response type "document". This uses the browser's built in parser to parse the XML and HTML into a DOM object.
  • Sync is not supported (passing false to the async argument). Most browsers have deprecated it in the main thread. Since this polyfill works by calling Deno's fetch() it is nearly impossible to generate a sync version, plus it is a really bad idea to block a thread while waiting for a server response. Don't do it, don't use software that requires it.

Usage

Import the module. The module will analyze the global scope, and if XMLHttpRequest and its associated APIs are not defined, it will add them:

import "jsr:/@kitsonk/xhr";

Now, XMLHttpRequest should be available in the global scope.

Types

The built in types for Deno do not include the XMLHttpRequest and associated types, so if you are type checking your code and get errors about them not being defined, there are a couple of solutions. If your code is generally written for the browser, you might want to consider Targeting Deno and the Browser section of the Deno Manual.

If all you want to do is "polyfill" the types, they are available here under ./globals.d.ts. You can either import them like:

import type {} from "jsr:/@kitsonk/xhr/globals";

Or using the triple slash reference like:

/// <reference types="jsr:/@kitsonk/xhr/globals" />

Copyright 2021-2024 Kitson P. Kelly. All rights reserved.