
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@distributedlab/fetcher
Advanced tools
Fetch API wrapper with the extended functionality and simple interface
Fetch API wrapper with the extended functionality and simple interface.
yarn add @distributedlab/fetcher
ECMAScript modules:
import { Fetcher } from '@distributedlab/fetcher'
CommonJS:
const { Fetcher } = require('@distributedlab/fetcher')
Via CDN:
In HTML:
<script src='https://unpkg.com/@distributedlab/fetcher'></script>
In code:
const api = new DL_fetcher.Fetcher({
baseUrl: 'https://api.example.com',
})
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
headers: {
'Content-Type': 'application/json',
},
mode: 'cors',
timeout: 10000,
credentials: 'include',
referrerPolicy: 'no-referrer',
})
// Override request headers for one request via request options argument:
const { data } = await api.get<{ name: string }>('/data', {}, {
headers: {
'Content-Type': 'text/html',
},
})
GET
request:
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
const getData = async () => {
const { data } = await api.get<{ name: string }>('/data')
return data
}
GET
request with the query params:
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
const getDataWithQuery = async () => {
const { data } = await api.get<{ name: string }>('/data', {
query: {
filter: "John",
exists: true,
"page[number]": 1,
include: ["comments", "posts"],
}
})
return data
}
POST
request (PUT
, PATCH
request has pretty much the same interface, just use put
or patch
method instead of post
):
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
const postData = async () => {
const { data } = await api.post<{ name: string }>('/data', {
body: {
name: "John",
}
})
return data
}
Posting the FormData
:
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
const postFormData = async () => {
const formData = new FormData()
formData.append('name', 'John')
formData.append('age', '25')
const { data } = await api.post<{ name: string, age: string }>('/data', { body: formData })
return data
}
Abort the request:
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
const abortRequest = async () => {
const requestId = api.createRequestId()
setTimeout(() => {
api.abort(requestId)
}, 1000)
const { data } = await api.get<{ name: string }>('/data', {
id: requestId,
})
}
Using the interceptors:
import { Fetcher } from '@distributedlab/fetcher'
const api = new Fetcher({
baseUrl: 'https://api.example.com',
})
api.addInterceptor({
request: async request => {
// Do something before request is sent
return { ...request, url: `${request.url}?foo=bar` }
},
response: async response => {
// Do something with response
if (response.ok) {
return response
}
return api.get('/auth/refresh')
},
error: async response => {
// Do something if response errored
if (response.status === 401) {
return api.get('/auth/refresh')
}
return response
},
})
The Fetcher
standalone feature offers the convenience of making requests to an external API without
the need to create an instance or configure options such as baseUrl
. It proves particularly useful
in scenarios where you require a one-time request and prefers to avoid the overhead of setting up a
Fetcher
instance:
import { fetcher } from '@distributedlab/fetcher'
const getData = async () => {
const { data } = await fetcher.get<{ name: string }>('https://api.example.com/data')
console.log(data) // { name: 'John' }
}
For the change log, see CHANGELOG.md.
[1.0.0-rc.17] - 2024-09-26
@distributedlab/w3p
- removeProvider
method to ProviderDetector
FAQs
Fetch API wrapper with the extended functionality and simple interface
The npm package @distributedlab/fetcher receives a total of 35 weekly downloads. As such, @distributedlab/fetcher popularity was classified as not popular.
We found that @distributedlab/fetcher demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 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.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.