@tatumcreative/get
v1.0.0
Published
A simple XMLHttpRequest wrapper
Downloads
5
Readme
@tatumcreative/get
A collection of zero-config GET
functions for fetching things on the internet.
Make sure node and npm is installed and then run
npm install @tatumcreative/get
. Then require the function you need like
const { getText } = require("@tatumcreative/get")
. The internals use only
ES5 code, but all examples and tests are ES6.
getText(url) => Promise(text)
Load the text, returns a Promise that resolves to the text. The error is the failing XMLHttpRequest object.
getText("/foo.txt").then(
(text) => console.log('Here is the text', text),
(error) => console.error('There was an error', error),
)
getJSON(url) => Promise(json)
Load some JSON, returns a Promise that resolves to the parsed JSON object. The error is either the failing XMLHttpRequest object, or a SyntaxError if the JSON failed to parse.
getJSON("/foo.json").then(
(json) => console.log('Here is the JSON object', json),
(error) => console.error('There was an error', error),
)
getImage(url) => Promise(imageElement)
Load an HTMLImageElement. It only resolves after the image is fully downloaded.
getImage("/foo.png").then(
(imageElement) => document.body.append(imageElement),
(error) => console.error('There was an error', error),
)
getImageData(url) => Promise({data, width, height})
Load an image and get an ImageData object {data, width, height}
.
The data property is a Uint8ClampedArray that is an array
with values 0-255.
getImageData("/foo.png").then(
({data, width, height}) => console.log('The first pixel is', data.slice(0,4)),
(error) => console.error('There was an error', error),
)
Example with parameters
These functions don't care about fancy parameters, but it's trivial to do with the query-string package.
const { getText } = require('@tatumcreative/get')
const { stringify } = require("query-string")
getText("/foo?" + stringify({
bar: 'a',
baz: 'b',
}))