react-url-checker
v1.0.3
Published
A package to check URL status and content type.
Readme
Here is an enhanced version of your README.md that includes a more structured workflow, purpose, use case, example, and general flow. This will help others understand how to use your UrlChecker package.
UrlChecker
UrlChecker is an npm package designed to check whether a URL exists and determine its content type (e.g., video, image, HTML, etc.). The package simulates a browser environment using jsdom to parse and interact with URLs. It supports asynchronous URL checks and provides an easy-to-use interface for various content types.
Purpose
The main purpose of UrlChecker is to:
- Verify if a given URL exists and is reachable.
- Determine the content type of the URL (video, image, HTML, etc.).
- Display relevant media (such as videos or images) directly in a simulated browser environment.
This is particularly useful for applications needing to validate links or media URLs, and for dynamically displaying content based on the URL's type.
Use Cases
- URL Validation: Ensure that URLs on your website are not broken.
- Content Validation: Check the content type of a URL before displaying it.
- Link Crawlers: Crawl URLs and categorize them by type (e.g., media, HTML).
- Dynamic Content Display: Based on the content type, render appropriate elements (video player, iframe, image, etc.).
Installation
To install UrlChecker, run the following npm command:
npm install react-url-checkerWorkflow
- Input: A list of URLs you want to check.
- Process: For each URL:
- Send a
HEADrequest to check if the URL exists. - Retrieve the content type from the response headers.
- Send a
- Output:
- Status: Whether the URL exists or not.
- Content Type: What type of content the URL points to (video, image, HTML).
- Dynamic content rendering: Display relevant media or an iframe based on the content type.
Example
Here’s an example of how you can use UrlChecker to check multiple URLs and handle them based on their content type:
Example Usage
const { handleUrl } = require('react-url-checker'); // Import the function
// URL to check
const url = 'https://example.com/video.mp4';
// Check the URL and handle its content
handleUrl(url);How It Works:
handleUrl(url):- Checks if the URL exists.
- Based on the content type (video, image, HTML, etc.), it displays the appropriate content (video player, image, iframe, etc.).
Content Types:
- Video: If the content type is a video, it will display a video player.
- Image: If the content type is an image, it will display the image.
- HTML: If the content type is HTML, it will display the content inside an iframe.
Functions
1. checkUrl(url)
- Purpose: Checks if the URL exists and returns the status.
- Parameters:
url(string): The URL to check.
- Returns: An object with
status(true/false) and a message.
Example:
const result = await checkUrl('https://example.com');
console.log(result);2. handleUrl(url)
- Purpose: Handles the URL, checks its status, and displays the content based on the type.
- Parameters:
url(string): The URL to handle.
- Returns: None (side effect of displaying the content).
Example:
handleUrl('https://example.com/video.mp4');3. displayVideo(url, document)
- Purpose: Displays a video element if the URL points to a video.
- Parameters:
url(string): The URL of the video.document(object): The document object fromjsdom.
- Returns: None.
4. displayIframe(url, document)
- Purpose: Displays an iframe element if the URL points to an HTML page.
- Parameters:
url(string): The URL of the HTML page.document(object): The document object fromjsdom.
- Returns: None.
5. displayImage(url, document)
- Purpose: Displays an image element if the URL points to an image.
- Parameters:
url(string): The URL of the image.document(object): The document object fromjsdom.
- Returns: None.
Example Flow
Provide the URL(s):
- You pass a URL or a list of URLs to the
handleUrlfunction.
- You pass a URL or a list of URLs to the
Check the URL:
checkUrl(url)is called to check if the URL is reachable using aHEADrequest.
Determine the Content Type:
- Based on the response headers, the function checks the
Content-Typeand decides how to render the URL:- Video: Displays a video player.
- Image: Displays the image.
- HTML: Embeds the page in an iframe.
- Based on the response headers, the function checks the
Output:
- The relevant media is displayed dynamically, or a message is logged if the URL cannot be accessed.
License
This package is licensed under the MIT License. See LICENSE for more information.
