All Projects → cferdinandi → Atomic

cferdinandi / Atomic

Licence: mit
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Atomic

Rext
🎈A lightweight (< 5kb gzipped) and Promise-supported HTTP request library, for all browsers.
Stars: ✭ 14 (-97.34%)
Mutual labels:  promise, ajax, xhr
upload-file-to-backblaze-b2-from-browser-example
Demonstrates calling the b2_upload_file Backblaze B2 Cloud Storage API from a web browser using AJAX.
Stars: ✭ 28 (-94.68%)
Mutual labels:  xhr, ajax
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-10.08%)
Mutual labels:  ajax, vanilla-js
angular-progress-http
[DEPRECATED] Use @angular/common/http instead
Stars: ✭ 43 (-91.83%)
Mutual labels:  xhr, ajax
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (+33.08%)
Mutual labels:  promise, ajax
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+334.41%)
Mutual labels:  promise, ajax
buzy
Async queue manager for node and browser
Stars: ✭ 23 (-95.63%)
Mutual labels:  promise, ajax
Ironwing
universal, framework agnostic, transport layer
Stars: ✭ 17 (-96.77%)
Mutual labels:  ajax, xhr
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (-91.44%)
Mutual labels:  xhr, ajax
tall
Promise-based, No-dependency URL unshortner (expander) module for Node.js
Stars: ✭ 56 (-89.35%)
Mutual labels:  promise, no-dependencies
Tabby
Lightweight, accessible vanilla JS toggle tabs.
Stars: ✭ 449 (-14.64%)
Mutual labels:  no-dependencies, vanilla-js
Axios Tutorial
axios实例应用及源码剖析 - xhr篇 (走心教程)
Stars: ✭ 219 (-58.37%)
Mutual labels:  ajax, xhr
Mappersmith
is a lightweight rest client for node.js and the browser
Stars: ✭ 282 (-46.39%)
Mutual labels:  promise, ajax
form-saver
A simple script that lets users save and reuse form data.
Stars: ✭ 67 (-87.26%)
Mutual labels:  vanilla-js, no-dependencies
Xhr.js
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
Stars: ✭ 12 (-97.72%)
Mutual labels:  ajax, xhr
http
Tiny, embeddable HTTP client with simple API for the browser
Stars: ✭ 21 (-96.01%)
Mutual labels:  xhr, ajax
Swiped Events
Adds `swiped` events to the DOM in 0.7k of pure JavaScript
Stars: ✭ 249 (-52.66%)
Mutual labels:  no-dependencies, vanilla-js
Push State
Turn static web sites into dynamic web apps.
Stars: ✭ 16 (-96.96%)
Mutual labels:  ajax, vanilla-js
tabbis.js
Pure vanilla javascript tabs with nesting
Stars: ✭ 44 (-91.63%)
Mutual labels:  vanilla-js, no-dependencies
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (-49.05%)
Mutual labels:  ajax, xhr

Atomic Build Status

A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.

Download Atomic / View the demo


Want to learn how to write your own vanilla JS plugins? Check out my Vanilla JS Pocket Guides or join the Vanilla JS Academy and level-up as a web developer. 🚀


Getting Started

Compiled and production-ready code can be found in the dist directory. The src directory contains development code.

1. Include Atomic on your site.

There are two versions of Atomic: the standalone version, and one that comes preloaded with a polyfill for ES6 Promises, which are only supported in newer browsers.

If you're including your own polyfill or don't want to enable this feature for older browsers, use the standalone version. Otherwise, use the version with the polyfill.

Direct Download

You can download the files directly from GitHub.

<script src="path/to/atomic.polyfills.min.js"></script>

CDN

You can also use the jsDelivr CDN. I recommend linking to a specific version number or version range to prevent major updates from breaking your site. Smooth Scroll uses semantic versioning.

<!-- Always get the latest version -->
<!-- Not recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/atomic/dist/atomic.polyfills.min.js"></script>

<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/atomic.polyfills.min.js"></script>

<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/atomic.polyfills.min.js"></script>

<!-- Get a specific version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/atomic.polyfills.min.js"></script>

NPM

You can also use NPM (or your favorite package manager).

npm install atomicjs

2. Make your Ajax request.

Pass in the requested URL, and optionally, your desired options. Request method defaults to GET.

Use .then() with a callback to handle successful responses, and catch() to handle errors.

// A basic GET request
atomic('https://some-url.com')
	.then(function (response) {
		console.log(response.data); // xhr.responseText
		console.log(response.xhr);  // full response
	})
	.catch(function (error) {
		console.log(error.status); // xhr.status
		console.log(error.statusText); // xhr.statusText
	});

// A POST request
atomic('https://some-url.com', {
	method: 'POST'
})
	.then(function (response) {
		console.log(response.data); // xhr.responseText
		console.log(response.xhr);  // full response
	})
	.catch(function (error) {
		console.log(error.status); // xhr.status
		console.log(error.statusText); // xhr.statusText
	});

ES6 Modules

Atomic does not have a default export, but does support CommonJS and can be used with native ES6 module imports.

import('/path/to/atomic.polyfills.min.js')
	.then(function () {
		atomic('https://some-url.com')
			.then(function (response) {
				console.log(response.data); // xhr.responseText
				console.log(response.xhr);  // full response
			})
			.catch(function (error) {
				console.log(error.status); // xhr.status
				console.log(error.statusText); // xhr.statusText
			});
	});

It uses a UMD pattern, and should also work in most major module bundlers and package managers.

Working with the Source Files

If you would prefer, you can work with the development code in the src directory using the included Gulp build system. This compiles, lints, and minifies code.

Dependencies

Make sure these are installed first.

Quick Start

  1. In bash/terminal/command line, cd into your project directory.
  2. Run npm install to install required files.
  3. When it's done installing, run one of the task runners to get going:
    • gulp manually compiles files.
    • gulp watch automatically compiles files when changes are made and applies changes using LiveReload.
    • gulp test runs unit tests.

Options and Settings

Atomic includes smart defaults and works right out of the box. You can pass options into Atomic through the ajax() function:

atomic('https://some-url.com', {
	method: 'GET', // {String} the request type
	username: null, // {String} an optional username for authentication purposes
	password: null, // {String} an optional password for authentication purposes
	data: {}, // {Object|Array|String} data to be sent to the server
	headers: { // {Object} Adds headers to your request: request.setRequestHeader(key, value)
		'Content-type': 'application/x-www-form-urlencoded'
	},
	responseType: 'text', // {String} the response type (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType)
	timeout: null, // {Integer} the number of milliseconds a request can take before automatically being terminated
	withCredentials: false // {Boolean} If true, send credentials with request (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
});

Canceling a Request

While Promises can't be canceled, Atomic does have an internal API for aborting your XHR request using the cancel() method.

In order to work, you must set your atomic() method to a variable without .then() methods. They can be called on the variable after setting.

// Setup your request
var xhr = atomic('https://some-url.com');

// Handle responses
xhr.then(function (response) {
		console.log(response.data); // xhr.responseText
		console.log(response.xhr);  // full response
	})
	.catch(function (error) {
		console.log(error.status); // xhr.status
		console.log(error.statusText); // xhr.statusText
	});

// Cancel your request
xhr.cancel();

Migrating from Atomic 3

New Features

  • Atomic is now Promise-based, and supports chaining with .then() and .catch().
  • For simple requests, you can now just pass in a URL to atomic() like you would with the Fetch API. You no longer need to pass in an object with the url parameter.

Breaking Changes

  • You must pass in a URL as the first argument. The URL as an options parameter is no longer support.
  • You now pass arguments directly into atomic(). The atomic.ajax() method no longer exists.
  • The .success(), .error(), and .always() callbacks have been removed. Use .then() and .catch() instead.
  • JSONP support has been removed.

You can still download Atomic 3 and earlier on the releases page.

Browser Compatibility

Atomic works in all modern browsers, and IE8 and above.

The standalone version provides native support for all modern browsers. Use the .polyfills version (or include your own) to support IE.

License

The code is available under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].