npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bitfluent/wirejs

v1.0.7

Published

A simple library for processing AJAX requests using the Fetch API and other ES8 features.

Downloads

4

Readme

Wire.js

A simple, class-based library for managing AJAX requests. Uses the Fetch API and other ES8 features. Currently in VERY early development. I wouldn't recommend using in production at this point; many holes need to be filled and features added.

Why Wire?

Many HTTP request libraries exist, so why Wire? Since Wire is class-based, we can create instances with preferences for each REST API service our application uses. This helps with staying organized and makes future API interactions a breeze.

Even better, since Wire is a js class, you can extend it in nearly unlimited creative ways to better suit your individual use case. For example, if the API service you're consuming requires that you first log in to receive an authentication token, you have the ability to extend Wire to do just that, and apply said token to future HTTP requests. "Smart" HTTP requests are the best requests. :)

Installing & Importing

Browser

In your HTML document, include wire.js in your <script> tag. If using in an existing js file, simply import it:

import Wire from 'wire.js';

Node

Run npm install @bitfluent/wirejs and import to your application. Only supports module types.

import Wire from '@bitfluent/wirejs';

Wire Instances and Configuration

Creating an instance

Create a new instance of Wire for repeated usage in your document/application. Pass the options object as an argument to configure. Leaving an option out will assume the default.

const wireOptions = {};
const api = new Wire(wireOptions);

The Options Object

The options object is passed as an argument into new instances of Wire. Each property is used as follows:

apiRoot: The root URI for requests made with this instance. Default is ''.
headers: An object containing any headers to be sent with this instance. It's recommended that you place any authorization headers here for the instance. Default value is { 'Content-Type': 'application/json' }.
auth: Can be a string or a function that returns a string. Whatever is returned by the function will be placed into the Authorization header for each request made by the Wire instance. Using a function to return a value is handy when the authorization token changes or is updated often, as the function will always return the most recent value before every API call.
includeCredentials: A boolean value that determines if cookies should be sent with cross-origin requests. Default value is false.
returnJSON: A boolean value that determines if JSON.parse should be called upon receiving a response. If invalid JSON is retrieved from the server, text will be returned instead. Default value is false.

Example:

const api = new Wire({
	apiRoot: 'https://localhost:3000/api/v1',
	headers: {
		'Content-Type': 'application/json; charset=UTF-8',
		// Add any additional headers go here.
	},
	auth: () => {
		return `Bearer ${myToken}`;
	},
	includeCredentials: true,
	returnJSON: true,
});

Now that we've declared our new Wire instance as api, we will use that to make any requests going forward.

GET Example:

The get() method accepts two arguments. api.get(path, handleErrors).

async function getUser() {
	let user = await api.get('/users/johndoe/', (err) => {
		console.error(err);
	});

	console.log(user); // Do whatever you want with the server's response.
}

POST Example:

The post() method accepts three arguments. api.post(path, body, handleErrors).

let postData = { firstname: 'John', lastname: 'Doe' };

async function createUser() {
	let submission = await api.post('/user/', postData, (err) => {
		console.error(err);
	});

	console.log(submission); // Do whatever you want with the server's response.
}

PUT Example:

The put() method accepts three arguments. api.put(path, body, handleErrors).

let newData = { firstname: 'Jane', lastname: 'Doe' };

async function updateUser() {
	let submission = await api.put('/user/49875432327/', newData, (err) => {
		console.error(err);
	});

	console.log(submission); // Do whatever you want with the server's response.
}

DELETE Example:

The delete() method accepts two arguments. api.delete(path, handleErrors).

async function removeUser() {
	let submission = await api.delete('/user/49875432327/', (err) => {
		console.error(err);
	});

	console.log(submission); // Do whatever you want with the server's response.
}