Http client for browser and node js
Report Bug
.
Request Feature
- Make http requests from browser and node js
- Intercept request
- Formatted Response and Error configuration
- Support Nextjs cache and revalidate
Typescript and built in browser fetch
Using npm:
$ npm install next-nfetch@latest
Once the package is installed, you can import the library using import
or require
approach:
import httpClient from "next-nfetch";
Or
const httpClient = require('next-nfetch');
You can directly call a function as default exported is a function. There are lots of methods attach to the base function such as get, post, put, etc.
Function Can ba called as:
const response = httpClient("url", options) // or
const response = httpClient({
...options, // Available options are listed below
url
})
options | types | default | required |
---|---|---|---|
url | string | yes | |
method | string | get | no |
headers | Headers | no | |
params | Object | { } | no |
next | Object | { } | no |
data | any | undefined | no |
timeout | number | 0 | no |
cache | RequestCache | no-cache | no |
timeoutMessage | string | Request Timeout. Failed to fetch | no |
get, delete, options and head
post. put and patch
const response = await httpClient.get("url", options);
const response = await httpClient.delete("url", options);
const response = await httpClient.post("url", options);
const response = await httpClient.patch("url", options);
and so on
{
data: any; // obtained data from api endpoint
headers: Record<string, string>; // response headers
request: HTTPRequestDetails; // request configuration
response: Response; // actual response send by fetch request
status: number; // http status code
statusText: string;
}
{
message: string; // error message
request?: HTTPRequestDetails; // request configuration
response?: HTTPResponseDetails; // error response details
name: "HttpError" | "TimeoutError";
stack?: any; // stack trace where error occurs
}
You can create a new instance of next-nfetch with a custom config.
const instance = httpClient.create({
baseURL: "https://some-domain.com/api/",
timeout: 1000,
});
try {
const res = await instance.get("/users/1", options);
console.log(res);
} catch (error) {
console.log(error);
}
You can create a request interceptor of next-nfetch with a custom config.
instance.useRequestInterceptor({
onFulfilled(config) {
// way to intercept request methods
config.headers.set("Authorization", "Bearer token");
return config;
},
onRejected(error) {
return Promise.reject(error);
},
});
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- If you have suggestions for adding or removing projects, feel free to open an issue to discuss it, or directly create a pull request after you edit the README.md file with necessary changes.
- Please make sure you check your spelling and grammar.
- Create individual PR for each suggestion.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
- Prabin Gautam - Comp Sci Student - Prabin Gautam