Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Download Atomic / View the demo
Compiled and production-ready code can be found in the dist
directory. The src
directory contains development code.
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/atomic@4/dist/atomic.polyfills.min.js"></script>
<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/atomic@4.0/dist/atomic.polyfills.min.js"></script>
<!-- Get a specific version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/atomic@4.0.0/dist/atomic.polyfills.min.js"></script>
NPM
You can also use NPM (or your favorite package manager).
npm install atomicjs
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
});
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.
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.
Make sure these are installed first.
cd
into your project directory.npm install
to install required files.gulp
manually compiles files.gulp watch
automatically compiles files when changes are made and applies changes using LiveReload.gulp test
runs unit tests.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)
});
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();
.then()
and .catch()
.atomic()
like you would with the Fetch API. You no longer need to pass in an object with the url
parameter.atomic()
. The atomic.ajax()
method no longer exists..success()
, .error()
, and .always()
callbacks have been removed. Use .then()
and .catch()
instead.You can still download Atomic 3 and earlier on the releases page.
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.
The code is available under the MIT License.
FAQs
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
The npm package atomicjs receives a total of 238 weekly downloads. As such, atomicjs popularity was classified as not popular.
We found that atomicjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.