fetchuri
v0.0.1
Published
Universal URI content fetcher (http/https/file/data) for Node.js and browsers
Maintainers
Readme
fetchuri
Universal content fetcher for any URI type (http/https/file/data) that works in both Node.js and browsers.
Installation
npm install fetchuri
# Or install globally for CLI usage
npm install -g fetchuriCLI Usage
# Fetch any URI from the command line
fetchuri https://api.github.com/users/github
# Set Accept header for content negotiation
fetchuri https://api.example.com/data --accept application/json
# Add custom headers
fetchuri https://api.example.com --header "Authorization: Bearer token123"
fetchuri https://api.example.com -H "User-Agent: fetchuri/0.0.1" -H "Accept: text/html"
# Pretty-print JSON
fetchuri https://api.example.com/data.json --json
# Save to file
fetchuri https://example.com --output index.html
# Fetch local file
fetchuri file:///etc/hosts
# Decode data URI
fetchuri "data:text/plain,Hello%20World"
# Show help
fetchuri --helpLibrary Usage
import fetchuri from 'fetchuri'
// Fetch from HTTP/HTTPS
const html = await fetchuri('https://example.com')
const json = await fetchuri('https://api.example.com/data.json')
// With Accept header for content negotiation
const jsonData = await fetchuri('https://api.example.com/data', {
headers: { 'Accept': 'application/json' }
})
// With custom headers
const response = await fetchuri('https://api.example.com/data', {
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer token123',
'User-Agent': 'MyApp/1.0'
}
})
// Fetch from local file
const config = await fetchuri('file:///home/user/config.json')
// Fetch from data URI
const inline = await fetchuri('data:text/plain,Hello%20World')
const base64 = await fetchuri('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ==')
// No protocol defaults to HTTPS
const page = await fetchuri('example.com') // → https://example.comAPI
fetchuri(uri: string, options?: object): Promise<string>
Fetches content from any URI and returns it as a string.
Parameters:
uri- The URI to fetch (required)options- Optional configuration objectheaders- Custom headers for HTTP/HTTPS requests
Supported protocols:
http://- HTTP URLs (Node.js & Browser)https://- HTTPS URLs (Node.js & Browser)file://- Local file paths (Node.js only)data:- Data URIs (RFC 2397) (Node.js & Browser)
Error Handling
try {
const content = await fetchuri('invalid://uri')
} catch (error) {
console.error('Fetch failed:', error.message)
}Browser vs Node.js
This module works in both environments with these differences:
- Browser: Supports
http://,https://, anddata:protocols - Node.js: Supports all protocols including
file:// - Uses native
fetch()in browsers,node-fetchin Node.js - Automatically detects the environment
Use Cases
- Fetching JSON from APIs (universal)
- Loading local configuration files (Node.js)
- Reading HTML pages (universal)
- Processing data URIs from HTML (universal)
- Universal content loader for CLI tools and web apps
License
MIT
