codewithdeepak.fetch
v1.0.7
Published
`codewithdeepak.fetch` is a simple HTTP request library built with TypeScript and Axios. It provides an easy-to-use `fetch` function for making HTTP requests with support for different HTTP methods and custom headers.
Downloads
36
Readme
codewithdeepak.fetch
codewithdeepak.fetch
is a simple HTTP request library built with TypeScript and Axios. It provides an easy-to-use fetch
function for making HTTP requests with support for different HTTP methods and custom headers.
Installation
You can install codewithdeepak.fetch
using npm:
npm install codewithdeepak.fetch
Usage
JavaScript
To use the package in a JavaScript environment:
GET Request
const { fetch } = require('codewithdeepak.fetch');
(async () => {
try {
const data = await fetch({
url: 'https://api.github.com/repos/nodejs/node',
method: 'GET'
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
POST Request with JSON Data
const { fetch } = require('codewithdeepak.fetch');
(async () => {
try {
const data = await fetch({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
title: 'foo',
body: 'bar',
userId: 1
}
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
POST Request with Form Data
const { fetch } = require('codewithdeepak.fetch');
const FormData = require('form-data');
(async () => {
try {
const form = new FormData();
form.append('username', 'example');
form.append('password', '123456');
const data = await fetch({
url: 'https://example.com/api/login',
method: 'POST',
headers: {
...form.getHeaders()
},
data: form
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
TypeScript
To use the package in a TypeScript environment:
GET Request
import { fetch } from 'codewithdeepak.fetch';
(async () => {
try {
const data = await fetch({
url: 'https://api.github.com/repos/nodejs/node',
method: 'GET'
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
POST Request with JSON Data
import { fetch } from 'codewithdeepak.fetch';
(async () => {
try {
const data = await fetch({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
title: 'foo',
body: 'bar',
userId: 1
}
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
POST Request with Form Data
import { fetch } from 'codewithdeepak.fetch';
import FormData from 'form-data';
(async () => {
try {
const form = new FormData();
form.append('username', 'example');
form.append('password', '123456');
const data = await fetch({
url: 'https://example.com/api/login',
method: 'POST',
headers: {
...form.getHeaders()
},
data: form
});
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
API
fetch(options: FetchOptions): Promise<any>
Makes an HTTP request based on the provided options.
Parameters
url
(string): The URL to which the request is made.method
(string, optional): The HTTP method to use. Defaults to'GET'
. Can be'POST'
,'PUT'
, or'DELETE'
.headers
(Record<string, string>, optional): Custom headers to include in the request.data
(any, optional): The request payload forPOST
orPUT
requests.
Returns
A Promise that resolves with the response data. In case of an error, it returns an object with the following properties:
error
(string): A description of the error.status
(number, optional): The HTTP status code.data
(any, optional): The error response data.
Development
To build the package from source:
npm run build
To run tests (assuming you have a test file named test-fetch.ts
):
npm test
License
This package is licensed under the ISC License.
Contributing
If you'd like to contribute to this project, please fork the repository and submit a pull request with your changes. For detailed contribution guidelines, please refer to the project's repository.
For more information, please refer to the Axios documentation and TypeScript documentation.
### Key Additions
1. **POST Request with JSON Data**: Shows how to send JSON payloads with `POST` requests.
2. **POST Request with Form Data**: Demonstrates sending form data, which is useful for scenarios where you need to send data in `application/x-www-form-urlencoded` or `multipart/form-data` formats.
3. **Headers Management**: Shows how to include custom headers in requests.
This extended README provides more comprehensive examples for using `POST` requests with different types of payloads, which should help users understand how to interact with different types of APIs effectively.