0.8.0 • Published 2 years ago
@charleswong28/bearer-api-scraping v0.8.0
Bearer API Scraping tool
Simple scraping library with auto-retry of API with structure of Bearer ${token} in header.
Logic flow
- Fetch the access token api and store it
- Fetch the API with access token in header
- If the response status code is not 2xx, retry with
numberOfRetryBeforeRefetchAccessTokentimes. - If the request failed with more than
numberOfRetryBeforeRefetchAccessTokentimes, renew access token and try again withnumberOfAccessTokenRetry. - If access token api failed
numberOfAccessTokenRetrytimes or request failed withnumberOfAccessTokenRetry + accessToken == null ? 0 : 1xnumberOfRetryBeforeRefetchAccessTokentimes, throw an error.
Usage
import Scraper from 'api-scaping';
for (let i = 0; i < 1000; i++) {
const result = await Scraper.get(`some_uri/${i}/item`, {
numberOfRetryBeforeRefetchAccessToken: 5,
refetchAccessTokenUri,
getAccessToken: (response) => response.accessToken,
numberOfAccessTokenRetry: 5,
});
console.log('result', result);
}Method
| Name | parameters |
|---|---|
| get | uri - uri to fetch opts - fetch options |
| post | uri - uri to fetch opts - fetch options |
Structure
- Code located in
index.ts - Tests located in
index.test.ts
Limitation
- Proxy can be implemented by passing requestConfig to Axios.
- Access Token API is only supported as get request. PR is welcomed.