validate-image-url

1.0.1 • Public • Published

Validate Image URL

NPM Version Build Status Test Coverage Dependencies devDependencies
js-standard-style

Checks asynchronously whether an image URL is valid or not.

By default, if an image URL is invalid, the browser would show the following image:
before
In that case, you might want show a different image such as:
after

This package tells you whether the image URL is valid or not.

Installation:

$ npm install --save validate-image-url

Usage:

import validator from 'validate-image-url'
 
const promise = validator({url: '<image-url>', timeout: 10000})
  .then(({image, url}) => {
    console.log('Image URL is valid.', 'URL:', url, 'image:', image)
  })
  .catch((err) => {
    console.error('Invalid image URL or image could not be fetched within 10000 milliseconds.', 'error:', err)
  })
  
  // NOTE: the promise can be canceled by calling 
  // promise.cancel()

Options:

  • url - (required) image URL.
  • timeout - (optional) timeout in milliseconds before canceling request and rejecting the promise. Default: 5000 milliseconds.

License

MIT

Author

Oron Nadiv (oron@nadiv.us)

Package Sidebar

Install

npm i validate-image-url

Weekly Downloads

12

Version

1.0.1

License

MIT

Last publish

Collaborators

  • oronnadiv