Portals v2 Released
I recently rewrote Helpful Human’s “Portals” library because I wanted a simpler, smaller tool for creating XHR requests that didn’t sacrifice flexibility. I also wanted to add “type” support as many of our internal projects have been recently been moved to Typescript.
What’s Different?
Before, you would create an instance of a Portal
class either directly or through the factory function. Then you would configure your middleware, globals, etc… that would be applied to each request made by that class.
import { createPortal } from "portals";
const http = createPortal({
globals: {
hostname: "https://example.com",
headers: {
Authorization: "Bearer S#Gwer6in456DFGhje#$5dfgsr5)Lgeryugh",
},
},
});
http.send({
method: "GET",
url: "/some-endpoint",
headers: {
Accept: "application/json",
},
}).then(function (res) {
// do something with response
});
Instead of using having a Portal
class, I decided that I wanted a “portal” to be nothing more than a function that ran an XHR request through some middleware and returned the result. Middleware is also not assumed and is made available via pure functions that can be imported separately.
Note: You don’t supply any middleware to the factory function that creates your “portal”. Not adding middleware will simply return a standard XHR call with
Promise
support.
import {createPortal, supportsJson, withPrefix, withBearer} from "portals";
const http = createPortal(
supportsJson(),
withPrefix("https://api.example.com"),
withBearer(req => "Bearer S#Gwer6in456DFGhje#$5dfgsr5)Lgeryugh"),
);
http({
url: "/some-endpoint"
}).then(function (res) {
// do something with response
});
Try It Out
Try it out for yourself and let me know what you think.