data-fetch-ts
is a TypeScript-based library for making HTTP requests with support for different methods (GET
, POST
, PUT
, DELETE
, PATCH
). It simplifies fetching data from APIs by providing a unified interface for various HTTP methods and optional token-based authentication.
To install data-fetch-ts
, run the following command:
npm install data-fetch-ts
bun install data-fetch-ts
This package does not have any external dependencies.
- Initial release with support for GET, POST, PUT, DELETE, and PATCH methods.
- TypeScript Support: Strong typing ensures better code quality and fewer runtime errors.
- Unified Interface: Simplifies HTTP requests by providing a single function to handle various methods.
- Token-based Authentication: Easily include authorization tokens in your requests.
- Flexible Options: Support for optional body and headers, making it adaptable to different API requirements.
- Caching Control: Allows specifying caching strategies for improved performance.
fetchData
endpoint
:string
- The URL endpoint to send the request to.method
:Methods
- The HTTP method to use (GET
,POST
,PUT
,DELETE
,PATCH
). Default isGET
token
:string
- (optional) - The token for authorization (if required).body
:string
- (optional) - The body of the request. Required forPOST
,PUT
, andPATCH
methods.cash
:cache
- (optional) - The caching mode to use.
Promise<any>
- A promise that resolves with the response data in JSON format.
Here is an example of using the fetchData function to fetch and log the length of the data:
import fetchData from 'data-fetch-ts';
const data = async () => {
const endpoint = 'https://jsonplaceholder.typicode.com/todos';
const res = await fetchData({ endpoint });
console.log(res.length);
};
data();
import fetchData from 'data-fetch-ts';
const data = async () => {
const endpoint = 'https://jsonplaceholder.typicode.com/todos';
const body = { 'name' : 'soumik', 'email' : `01754759169`, };
const res = await fetchData({ endpoint, method : 'POST', body });
console.log(res.length);
}
when you use post request the body required
import fetchData from 'data-fetch-ts';
const data = async () => {
const endpoint = 'https://jsonplaceholder.typicode.com/todos';
const body = { 'name' : 'soumik', 'email' : `01754759169`, 'age' : '19' };
const token = 'dsjlfjalkfjdalskdfjaskldfjklutwiueiweojflaskdjfasdfjsdfk_jsldfjskldfj'
const res = await fetchData({ endpoint, method : 'PUT', body, token });
console.log(res.length);
}
If a token is required, you don't need to do a lot of work; just pass the token. Authorization and Bearer are already set, so you just need to pass the token.
# extract token from headers
const extractToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
if (authHeader) {
const token = authHeader.split(' ')[1]; // Assuming 'Bearer <token>'
req.token = token; // Attach the token to the request object
} else {
req.token = null;
}
next();
};
Contributions are welcome! Please open an issue or submit a pull request.
Soumik Sarkar - ratulsarkar216@gmail.com
If you like this package, please give it a star.