http-fetch-request
v1.0.7
Published
The `Fetch Request` class provides utility methods for sending API requests using GET and POST methods. This class supports authentication with a token and allows additional headers to be configured for API requests.
Downloads
4
Readme
Fetch request
The Fetch Request
class provides utility methods for sending API requests using GET and POST methods. This class supports authentication with a token and allows additional headers to be configured for API requests.
Install
npm install http-fetch-request
or
yarn add http-fetch-request
Example
Basic
instead of
fetch(url, {
headers:{
'Content-Type': 'application/json',
'Accept': "application/json",
}
})
.then(res=>res.json())
.then(response=>{
console.log(response);
});
to
import { getRequest } from "http-fetch-request";
getRequest(url).then(response=>{
console.log(response);
});
//or
const response = await getRequest(url); // nested async function
console.log(response);
instead of
fetch(url, {
headers:{
'Content-Type': 'application/json',
'Accept': "application/json",
}
body: JSON.stringify(body)
})
.then(res=>res.json())
.then(response=>{
console.log(response);
});
to
import { postRequest } from "http-fetch-request";
postRequest(url, body).then(response=>{
console.log(response);
});
//or
const response = await postRequest(url, body); // nested async function
console.log(response);
Repository method
| Function | Description |
|-------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| getRequest(url: string): Promise<any>
| Sends a GET request to the specified URL. |
| postRequest(url: string, body: any): Promise<any>
| Sends a POST request to the specified URL with the data in the body. |
| getRequestBearer(url: string, token: string): Promise<any>
| Sends an authenticated GET request to the specified URL with the provided token. |
| postRequestBearer(url: string, token: string, body: any): Promise<any>
| Sends an authenticated POST request to the specified URL with the data in the body and the provided token. |
| putRequest(url: string, body: any, token?: string): Promise<any>
| Sends a PUT request to the specified URL with the data in the body. Optionally accepts an authentication token. |
| deleteRequest(url: string, token?: string): Promise<any>
| Sends a DELETE request to the specified URL. Optionally accepts an authentication token. |
| putRequestBearer(url: string, token: string, body: any): Promise<any>
| Sends an authenticated PUT request to the specified URL with the data in the body and the provided token. |
| deleteRequestBearer(url: string, token: string): Promise<any>
| Sends an authenticated DELETE request to the specified URL with the provided token. |
headers:{ 'Content-Type': 'application/json', 'Accept': "application/json", }
- getRequest(url: string): Promise
- postRequest(url: string, body?:any): Promise
- putRequest(url: string, body?:any): Promise
- deleteRequest(url: string, body?:any): Promise
headers:{
'Content-Type': 'application/json',
'Accept': "application/json",
'Authorization': Bearer ${token}
}
- getRequestBearer(url: string, token:string): Promise
- postRequestBearer(url: string, token:string, body?:any): Promise
- putRequestBearer(url: string, token:string, body?:any): Promise
- deleteRequestBearer(url: string, token:string, body?:any): Promise