├── .gitignore ├── webpack.config.js ├── STRUCTURE.md ├── package.json ├── LICENSE ├── dist └── js-http.min.js ├── CONTRIBUTING.md ├── DEBUG.log ├── CODE_OF_CONDUCT.md ├── tests └── test-js-http.js ├── docs └── index.html ├── src └── js-http.js ├── README.md └── LEARN.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | // webpack.config.js 2 | 3 | const path = require('path'); 4 | 5 | module.exports = { 6 | entry: './src/js-http.js', 7 | output: { 8 | filename: 'js-http.min.js', 9 | path: path.resolve(__dirname, 'dist'), 10 | }, 11 | mode: 'production', 12 | }; -------------------------------------------------------------------------------- /STRUCTURE.md: -------------------------------------------------------------------------------- 1 | ```bash 2 | js-http/ 3 | |-- dist/ 4 | | |-- js-http.min.js 5 | |-- src/ 6 | | |-- js-http.js 7 | |-- examples/ 8 | | |-- index.html 9 | |-- tests/ 10 | | |-- test-js-http.js 11 | |-- docs/ 12 | | |-- index.html 13 | |-- README.md 14 | |-- package.json 15 | |-- webpack.config.js 16 | |-- .gitignore 17 | ``` -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "https-node.js", 3 | "description": "A simple HTTP request library for JavaScript.", 4 | "url": "https://github.com/pb2204/JS-HTTP", 5 | "keywords": [ 6 | "http request", 7 | "ajax request", 8 | "http server", 9 | "ajax server", 10 | "get method", 11 | "post method" 12 | ], 13 | "author": "Pabitra Banerjee", 14 | "contributors": [], 15 | "license": "MIT", 16 | "lib": ".", 17 | "main": "dist/js-http.min.js", 18 | "version": "1.1.9", 19 | "scripts": { 20 | "test": "mocha tests/test-js-http.js", 21 | "build": "webpack --mode production" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/pb2204/JS-HTTP" 26 | }, 27 | "devDependencies": { 28 | "chai": "^4.3.9", 29 | "chai-http": "^4.4.0", 30 | "mocha": "^10.2.0", 31 | "webpack": "^5.88.2", 32 | "webpack-cli": "^5.1.4" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Pabitra Banerjee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /dist/js-http.min.js: -------------------------------------------------------------------------------- 1 | (()=>{var t={82:t=>{t.exports=class{static async get(t){try{const r=await fetch(t);if(!r.ok)throw new Error(`HTTP error! Status: ${r.status}`);return await r.json()}catch(t){throw new Error(`GET request failed: ${t.message}`)}}static async post(t,r){try{const e=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`POST request failed: ${t.message}`)}}static async put(t,r){try{const e=await fetch(t,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`PUT request failed: ${t.message}`)}}static async delete(t){try{const r=await fetch(t,{method:"DELETE"});if(!r.ok)throw new Error(`HTTP error! Status: ${r.status}`);return await r.json()}catch(t){throw new Error(`DELETE request failed: ${t.message}`)}}static async options(t,r){try{const e=await fetch(t,{method:"OPTIONS",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`OPTIONS request failed: ${t.message}`)}}static async head(t,r){try{const e=await fetch(t,{method:"HEAD",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`HEAD request failed: ${t.message}`)}}static async connect(t,r){try{const e=await fetch(t,{method:"CONNECT",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`CONNECT request failed: ${t.message}`)}}static async trace(t,r){try{const e=await fetch(t,{method:"TRACE",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`TRACE request failed: ${t.message}`)}}static async patch(t,r){try{const e=await fetch(t,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!e.ok)throw new Error(`HTTP error! Status: ${e.status}`);return await e.json()}catch(t){throw new Error(`PATCH request failed: ${t.message}`)}}}}},r={};!function e(a){var s=r[a];if(void 0!==s)return s.exports;var o=r[a]={exports:{}};return t[a](o,o.exports,e),o.exports}(82)})(); -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to JS-HTTP 2 | 3 | Welcome to the JS-HTTP project! We appreciate your interest in contributing. Before you get started, please take a moment to review the following guidelines. 4 | 5 | ## Table of Contents 6 | 7 | - [Getting Started](#getting-started) 8 | - [Contributor Workflow](#contributor-workflow) 9 | - [Reporting Issues](#reporting-issues) 10 | - [Feature Requests](#feature-requests) 11 | - [Code of Conduct](#code-of-conduct) 12 | 13 | ## Getting Started 14 | 15 | To start contributing to JS-HTTP, follow these steps: 16 | 17 | 1. Fork the JS-HTTP repository to your GitHub account. 18 | 2. Clone your forked repository to your local machine: 19 | 20 | ```bash 21 | git clone https://github.com/pb2204/js-http.git 22 | ``` 23 | 24 | 3. Create a new branch for your work: 25 | 26 | ```bash 27 | git checkout -b feature/your-feature 28 | ``` 29 | 30 | 4. Make your changes and commit them: 31 | 32 | ```bash 33 | git add . 34 | git commit -m "Add your meaningful commit message here" 35 | ``` 36 | 37 | 5. Push your changes to your forked repository: 38 | 39 | ```bash 40 | git push origin feature/your-feature 41 | ``` 42 | 43 | 6. Create a pull request (PR) to the `main` branch of the original JS-HTTP repository. 44 | 45 | 7. Wait for the maintainers to review your PR. Please be patient and responsive to feedback. 46 | 47 | 8. Once your PR is approved, it will be merged, and your contribution will be part of the project. 48 | 49 | ## Contributor Workflow 50 | 51 | - Always work on a feature branch, not the `main` branch. 52 | - Keep your forked repository up-to-date with the latest changes from the `main` branch of the original repository. 53 | - Write clear and concise commit messages. 54 | - Be respectful and constructive in all communication. 55 | 56 | ## Reporting Issues 57 | 58 | If you encounter any bugs, problems, or unexpected behavior, please help us by [reporting the issue](https://github.com/yourusername/js-http/issues) with the following details: 59 | 60 | - A clear and descriptive title. 61 | - A detailed description of the issue, including any error messages. 62 | - Steps to reproduce the issue. 63 | - Your environment (e.g., Node.js version, browser, operating system). 64 | 65 | ## Feature Requests 66 | 67 | We welcome ideas and suggestions for new features or improvements to JS-HTTP. Please open an issue and use the "Feature Request" template to provide details about your proposal. 68 | 69 | ## Code of Conduct 70 | 71 | By participating in this project, you agree to abide by the [Code of Conduct](CODE_OF_CONDUCT.md). We expect all contributors to maintain a respectful and inclusive environment for everyone. 72 | 73 | Thank you for considering contributing to JS-HTTP! Your contributions are valuable and help make this project better for everyone. -------------------------------------------------------------------------------- /DEBUG.log: -------------------------------------------------------------------------------- 1 | 0 verbose cli C:\Program Files\nodejs\node.exe C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js 2 | 1 info using npm@9.6.0 3 | 2 info using node@v18.14.0 4 | 3 timing npm:load:whichnode Completed in 2ms 5 | 4 timing config:load:defaults Completed in 3ms 6 | 5 timing config:load:file:C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\npmrc Completed in 4ms 7 | 6 timing config:load:builtin Completed in 4ms 8 | 7 timing config:load:cli Completed in 3ms 9 | 8 timing config:load:env Completed in 1ms 10 | 9 timing config:load:file:C:\Users\rocks\OneDrive\Desktop\JS-HTTP\.npmrc Completed in 1ms 11 | 10 timing config:load:project Completed in 8ms 12 | 11 timing config:load:file:C:\Users\rocks\.npmrc Completed in 2ms 13 | 12 timing config:load:user Completed in 2ms 14 | 13 timing config:load:file:C:\Users\rocks\AppData\Roaming\npm\etc\npmrc Completed in 1ms 15 | 14 timing config:load:global Completed in 1ms 16 | 15 timing config:load:setEnvs Completed in 2ms 17 | 16 timing config:load Completed in 24ms 18 | 17 timing npm:load:configload Completed in 24ms 19 | 18 timing npm:load:mkdirpcache Completed in 1ms 20 | 19 timing npm:load:mkdirplogs Completed in 1ms 21 | 20 verbose title npm version patch 22 | 21 verbose argv "version" "patch" 23 | 22 timing npm:load:setTitle Completed in 2ms 24 | 23 timing config:load:flatten Completed in 10ms 25 | 24 timing npm:load:display Completed in 12ms 26 | 25 verbose logfile logs-max:10 dir:C:\Users\rocks\AppData\Local\npm-cache\_logs\2023-09-28T04_46_40_927Z- 27 | 26 verbose logfile C:\Users\rocks\AppData\Local\npm-cache\_logs\2023-09-28T04_46_40_927Z-debug-0.log 28 | 27 timing npm:load:logFile Completed in 9ms 29 | 28 timing npm:load:timers Completed in 0ms 30 | 29 timing npm:load:configScope Completed in 0ms 31 | 30 timing npm:load Completed in 53ms 32 | 31 silly logfile start cleaning logs, removing 2 files 33 | 32 silly logfile done cleaning log files 34 | 33 timing command:version Completed in 84ms 35 | 34 verbose stack Error: Git working directory not clean. 36 | 34 verbose stack at module.exports (C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\node_modules\libnpmversion\lib\enforce-clean.js:26:13) 37 | 34 verbose stack at async module.exports (C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\node_modules\libnpmversion\lib\version.js:60:46) 38 | 34 verbose stack at async Version.change (C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\lib\commands\version.js:77:21) 39 | 34 verbose stack at async module.exports (C:\Users\rocks\AppData\Roaming\npm\node_modules\npm\lib\cli.js:134:5) 40 | 35 verbose cwd C:\Users\rocks\OneDrive\Desktop\JS-HTTP 41 | 36 verbose Windows_NT 10.0.22000 42 | 37 verbose node v18.14.0 43 | 38 verbose npm v9.6.0 44 | 39 error Git working directory not clean. 45 | 40 verbose exit 1 46 | 41 timing npm Completed in 190ms 47 | 42 verbose code 1 48 | 43 error A complete log of this run can be found in: 49 | 43 error C:\Users\rocks\AppData\Local\npm-cache\_logs\2023-09-28T04_46_40_927Z-debug-0.log -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # JS-HTTP Code of Conduct 2 | 3 | ## Introduction 4 | 5 | JS-HTTP is an open-source project, and we welcome contributors from all backgrounds and experience levels. To ensure that our community is a safe and welcoming space for everyone, we have established this code of conduct. It outlines our expectations for all members of our community, including contributors, maintainers, and users. 6 | 7 | ## Our Pledge 8 | 9 | In the interest of fostering an open and welcoming environment, we pledge to: 10 | 11 | 1. Be respectful and considerate: We value and respect the contributions and ideas of all participants and will treat everyone with kindness and empathy. 12 | 13 | 2. Be inclusive: We will not tolerate discrimination, harassment, or exclusion based on race, color, religion, gender, sexual orientation, gender identity and expression, national origin, age, disability, or any other protected category. 14 | 15 | 3. Be collaborative: We encourage collaboration, constructive feedback, and open communication among community members. 16 | 17 | 4. Be patient and understanding: We understand that people have different skill levels and perspectives. We will be patient and supportive in our interactions with others. 18 | 19 | ## Expected Behavior 20 | 21 | We expect all members of the JS-HTTP community to: 22 | 23 | - Be respectful and considerate in all interactions, whether written or verbal. 24 | 25 | - Use inclusive language and avoid offensive, derogatory, or discriminatory comments and actions. 26 | 27 | - Be open to feedback and willing to learn from others. 28 | 29 | - Refrain from personal attacks, trolling, or harassment in any form. 30 | 31 | ## Unacceptable Behavior 32 | 33 | Unacceptable behavior includes, but is not limited to: 34 | 35 | - Offensive, derogatory, or discriminatory comments or actions. 36 | 37 | - Harassment, intimidation, or threats directed at anyone in the community. 38 | 39 | - Personal attacks or ad hominem criticism. 40 | 41 | - Trolling or intentionally disruptive behavior. 42 | 43 | - Publishing others' private information without consent. 44 | 45 | - Any other conduct that could reasonably be considered inappropriate in a professional setting. 46 | 47 | ## Reporting Violations 48 | 49 | If you witness or experience behavior that violates this code of conduct, please report it to the project maintainers immediately by sending an email to [maintainers@example.com]. All reports will be kept confidential. 50 | 51 | ## Enforcement 52 | 53 | Community members who engage in unacceptable behavior may be asked to stop or face temporary or permanent expulsion from the community at the discretion of the project maintainers. 54 | 55 | ## Acknowledgment 56 | 57 | This code of conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/version/2/0/code_of_conduct/), version 2.0. 58 | 59 | ## Questions or Concerns 60 | 61 | If you have questions or concerns regarding this code of conduct, please contact the project maintainer [Pabitra Banerjee](mailto:rockstarpabitra2204@gmail.com). 62 | 63 | We strive to create a positive and inclusive community, and we appreciate your cooperation in helping us achieve this goal. 64 | 65 | Thank you for being a part of the JS-HTTP community. -------------------------------------------------------------------------------- /tests/test-js-http.js: -------------------------------------------------------------------------------- 1 | // tests/test-js-http.js 2 | 3 | const chai = require('chai'); 4 | const chaiHttp = require('chai-http'); 5 | const JSHTTP = require('../src/js-http'); 6 | 7 | const { expect } = chai; 8 | 9 | chai.use(chaiHttp); 10 | 11 | describe('JSHTTP Library Tests', () => { 12 | it('should make a successful GET request', async () => { 13 | const response = await JSHTTP.get('https://jsonplaceholder.typicode.com/posts/1'); 14 | expect(response).to.be.an('object'); 15 | expect(response.id).to.equal(1); 16 | }); 17 | 18 | it('should make a successful POST request', async () => { 19 | const data = { userId: 1, id: 101, title: 'foo', body: 'bar' }; 20 | const response = await JSHTTP.post('https://jsonplaceholder.typicode.com/posts', data); 21 | expect(response).to.be.an('object'); 22 | expect(response.id).to.equal(101); 23 | }); 24 | 25 | // Test for PUT request 26 | it('should make a successful PUT request', async () => { 27 | const data = { userId: 1, id: 1, title: 'updated title', body: 'updated body' }; 28 | const response = await JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', data); 29 | expect(response).to.be.an('object'); 30 | expect(response.id).to.equal(1); 31 | expect(response.title).to.equal('updated title'); 32 | expect(response.body).to.equal('updated body'); 33 | }); 34 | 35 | // Test for DELETE request 36 | it('should make a successful DELETE request', async () => { 37 | const response = await JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101'); 38 | expect(response).to.be.an('object'); 39 | expect(response).to.deep.equal({}); 40 | }); 41 | 42 | // Test for OPTIONS request 43 | it('should make a successful OPTIONS request', async () => { 44 | const data = { someOption: 'value' }; // Replace with appropriate data if needed 45 | const response = await JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', data); 46 | expect(response).to.be.an('object'); 47 | }); 48 | 49 | // Test for HEAD request 50 | it('should make a successful HEAD request', async () => { 51 | const data = { someHeader: 'value' }; // Replace with appropriate data if needed 52 | const response = await JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', data); 53 | }); 54 | 55 | // Test for CONNECT request 56 | it('should make a successful CONNECT request', async () => { 57 | const data = { someData: 'value' }; // Replace with appropriate data if needed 58 | const response = await JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', data); 59 | }); 60 | 61 | // Test for TRACE request 62 | it('should make a successful TRACE request', async () => { 63 | const data = { someData: 'value' }; // Replace with appropriate data if needed 64 | const response = await JSHTTP.trace('https://jsonplaceholder.typicode.com/some-resource', data); 65 | }); 66 | 67 | // Test for PATCH request 68 | it('should make a successful PATCH request', async () => { 69 | const data = { someData: 'value' }; // Replace with appropriate data if needed 70 | const response = await JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', data); 71 | }); 72 | }); -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | JS-HTTP Library Test 9 | 10 | 11 | 12 |

JS-HTTP Library Test

13 | 14 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /src/js-http.js: -------------------------------------------------------------------------------- 1 | // src/js-http.js 2 | 3 | class JSHTTP { 4 | static async get(url) { 5 | try { 6 | const response = await fetch(url); 7 | if (!response.ok) { 8 | throw new Error(`HTTP error! Status: ${response.status}`); 9 | } 10 | return await response.json(); 11 | } catch (error) { 12 | throw new Error(`GET request failed: ${error.message}`); 13 | } 14 | } 15 | 16 | static async post(url, data) { 17 | try { 18 | const response = await fetch(url, { 19 | method: 'POST', 20 | headers: { 21 | 'Content-Type': 'application/json', 22 | }, 23 | body: JSON.stringify(data), 24 | }); 25 | if (!response.ok) { 26 | throw new Error(`HTTP error! Status: ${response.status}`); 27 | } 28 | return await response.json(); 29 | } catch (error) { 30 | throw new Error(`POST request failed: ${error.message}`); 31 | } 32 | } 33 | 34 | static async put(url, data) { 35 | try { 36 | const response = await fetch(url, { 37 | method: 'PUT', 38 | headers: { 39 | 'Content-Type': 'application/json', 40 | }, 41 | body: JSON.stringify(data), 42 | }); 43 | if (!response.ok) { 44 | throw new Error(`HTTP error! Status: ${response.status}`); 45 | } 46 | return await response.json(); 47 | } catch (error) { 48 | throw new Error(`PUT request failed: ${error.message}`); 49 | } 50 | } 51 | 52 | static async delete(url) { 53 | try { 54 | const response = await fetch(url, { 55 | method: 'DELETE', 56 | }); 57 | if (!response.ok) { 58 | throw new Error(`HTTP error! Status: ${response.status}`); 59 | } 60 | return await response.json(); 61 | } catch (error) { 62 | throw new Error(`DELETE request failed: ${error.message}`); 63 | } 64 | } 65 | 66 | static async options(url, data) { 67 | try { 68 | const response = await fetch(url, { 69 | method: 'OPTIONS', 70 | headers: { 71 | 'Content-Type': 'application/json', 72 | }, 73 | body: JSON.stringify(data), 74 | }); 75 | if (!response.ok) { 76 | throw new Error(`HTTP error! Status: ${response.status}`); 77 | } 78 | return await response.json(); 79 | } catch (error) { 80 | throw new Error(`OPTIONS request failed: ${error.message}`); 81 | } 82 | } 83 | 84 | static async head(url, data) { 85 | try { 86 | const response = await fetch(url, { 87 | method: 'HEAD', 88 | headers: { 89 | 'Content-Type': 'application/json', 90 | }, 91 | body: JSON.stringify(data), 92 | }); 93 | if (!response.ok) { 94 | throw new Error(`HTTP error! Status: ${response.status}`); 95 | } 96 | return await response.json(); 97 | } catch (error) { 98 | throw new Error(`HEAD request failed: ${error.message}`); 99 | } 100 | } 101 | 102 | static async connect(url, data) { 103 | try { 104 | const response = await fetch(url, { 105 | method: 'CONNECT', 106 | headers: { 107 | 'Content-Type': 'application/json', 108 | }, 109 | body: JSON.stringify(data), 110 | }); 111 | if (!response.ok) { 112 | throw new Error(`HTTP error! Status: ${response.status}`); 113 | } 114 | return await response.json(); 115 | } catch (error) { 116 | throw new Error(`CONNECT request failed: ${error.message}`); 117 | } 118 | } 119 | 120 | static async trace(url, data) { 121 | try { 122 | const response = await fetch(url, { 123 | method: 'TRACE', 124 | headers: { 125 | 'Content-Type': 'application/json', 126 | }, 127 | body: JSON.stringify(data), 128 | }); 129 | if (!response.ok) { 130 | throw new Error(`HTTP error! Status: ${response.status}`); 131 | } 132 | return await response.json(); 133 | } catch (error) { 134 | throw new Error(`TRACE request failed: ${error.message}`); 135 | } 136 | } 137 | 138 | static async patch(url, data) { 139 | try { 140 | const response = await fetch(url, { 141 | method: 'PATCH', 142 | headers: { 143 | 'Content-Type': 'application/json', 144 | }, 145 | body: JSON.stringify(data), 146 | }); 147 | if (!response.ok) { 148 | throw new Error(`HTTP error! Status: ${response.status}`); 149 | } 150 | return await response.json(); 151 | } catch (error) { 152 | throw new Error(`PATCH request failed: ${error.message}`); 153 | } 154 | } 155 | } 156 | 157 | module.exports = JSHTTP; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JS-HTTP 2 | 3 | A simple JavaScript HTTP request library for making GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, and PATCH requests using the Fetch API. 4 | 5 | - [Installation](#installation) 6 | - [Project Structure](#project-structure) 7 | - [Overview](#overview) 8 | - [Examples](#examples) 9 | - [Usage](#usage) 10 | - [License](#license) 11 | - [Code of Conduct](CODE_OF_CONDUCT.md) 12 | - [Contributing](CONTRIBUTING.md) 13 | - [Learn More](LEARN.md) 14 | - [Developer Details](#developer-details) 15 | 16 | ## Installation 17 | 18 | You can install JS-HTTP using npm: 19 | 20 | ```bash 21 | npm install https-node.js 22 | ``` 23 | 24 | ## Project Structure 25 | 26 | The project structure is organized as follows: 27 | 28 | ```bash 29 | js-http/ 30 | |-- dist/ 31 | |-- src/ 32 | | |-- js-http.js 33 | |-- examples/ 34 | | |-- index.html 35 | |-- tests/ 36 | | |-- test-js-http.js 37 | |-- CODE_OF_CONDUCT.md 38 | |-- CONTRIBUTING.md 39 | |-- LEARN.md 40 | |-- README.md 41 | |-- LICENSE 42 | |-- package.json 43 | |-- webpack.config.js 44 | |-- .gitignore 45 | ``` 46 | 47 | - **`dist/`**: Contains the distribution version of the library. 48 | - **`src/`**: Contains the source code of the library. 49 | - **`examples/`**: Includes HTML examples demonstrating library usage. 50 | - **`tests/`**: Contains test files for the library. 51 | - **`CODE_OF_CONDUCT.md`**: Guidelines for community behavior. 52 | - **`CONTRIBUTING.md`**: Information on how to contribute to the project. 53 | - **`LEARN.md`**: Additional resources and learning materials. 54 | - **`README.md`**: This README file. 55 | - **`LICENSE`**: The license file for the project. 56 | - **`package.json`**: Configuration file for npm. 57 | - **`webpack.config.js`**: Configuration for bundling the library. 58 | - **`.gitignore`**: Specifies files and directories to be ignored by Git. 59 | 60 | ## Overview 61 | 62 | JS-HTTP is a lightweight JavaScript library that simplifies making various HTTP requests in your web applications. It provides a straightforward API for making GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, and PATCH requests using the Fetch API. 63 | 64 | ## Examples 65 | 66 | You can find usage examples in the `examples/` directory. To run the examples, open the HTML files in your browser. 67 | 68 | ## Usage 69 | 70 | Here's how you can use JS-HTTP in your JavaScript code: 71 | 72 | ```javascript 73 | // Import the JS-HTTP library 74 | const JSHTTP = require('https-node.js); 75 | ``` 76 | 77 | Make a `GET` request 78 | ```javascript 79 | JSHTTP.get('https://jsonplaceholder.typicode.com/posts/1') 80 | .then(response => { 81 | console.log('GET Response:', response); 82 | }) 83 | .catch(error => { 84 | console.error('GET Error:', error); 85 | }); 86 | ``` 87 | 88 | Make a `POST` request 89 | ```javascript 90 | const data = { userId: 1, id: 101, title: 'foo', body: 'bar' }; 91 | JSHTTP.post('https://jsonplaceholder.typicode.com/posts', data) 92 | .then(response => { 93 | console.log('POST Response:', response); 94 | }) 95 | .catch(error => { 96 | console.error('POST Error:', error); 97 | }); 98 | ``` 99 | 100 | Make a `PUT` request 101 | ```javascript 102 | const putData = { userId: 1, id: 1, title: 'updated title', body: 'updated body' }; 103 | JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', putData) 104 | .then(response => { 105 | console.log('PUT Response:', response); 106 | }) 107 | .catch(error => { 108 | console.error('PUT Error:', error); 109 | }); 110 | ``` 111 | 112 | Make a `DELETE` request 113 | ```javascript 114 | JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101') 115 | .then(response => { 116 | console.log('DELETE Response:', response); 117 | }) 118 | .catch(error => { 119 | console.error('DELETE Error:', error); 120 | }); 121 | ``` 122 | 123 | Make an `OPTIONS` request 124 | ```javascript 125 | const optionsData = { someOption: 'value' }; 126 | JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', optionsData) 127 | .then(response => { 128 | console.log('OPTIONS Response:', response); 129 | }) 130 | .catch(error => { 131 | console.error('OPTIONS Error:', error); 132 | }); 133 | ``` 134 | 135 | Make a `HEAD` request 136 | ```javascript 137 | const headData = { someHeader: 'value' }; 138 | JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', headData) 139 | .then(response => { 140 | console.log('HEAD Response:', response); 141 | }) 142 | .catch(error => { 143 | console.error('HEAD Error:', error); 144 | }); 145 | ``` 146 | 147 | Make a `CONNECT` request 148 | ```javascript 149 | const connectData = { someData: 'value' }; 150 | JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', connectData) 151 | .then(response => { 152 | console.log('CONNECT Response:', response); 153 | }) 154 | .catch(error => { 155 | console.error('CONNECT Error:', error); 156 | }); 157 | ``` 158 | 159 | Make a `TRACE` request 160 | ```javascript 161 | const traceData = { someData: 'value' }; 162 | JSHTTP.trace('https://jsonplaceholder.typicode.com/some 163 | 164 | -resource', traceData) 165 | .then(response => { 166 | console.log('TRACE Response:', response); 167 | }) 168 | .catch(error => { 169 | console.error('TRACE Error:', error); 170 | }); 171 | ``` 172 | 173 | Make a `PATCH` request 174 | ```javascript 175 | const patchData = { someData: 'value' }; 176 | JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', patchData) 177 | .then(response => { 178 | console.log('PATCH Response:', response); 179 | }) 180 | .catch(error => { 181 | console.error('PATCH Error:', error); 182 | }); 183 | ``` 184 | 185 | ## License 186 | 187 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 188 | 189 | ## Developer Details 190 | 191 | - **Author**: [Pabitra Banerjee](https://pabitrabanerjee.me) 192 | - **Email**: [rockstarpabitra2204@gmail.com](mailto:rockstarpabitra2204@gmail.com) 193 | - **GitHub**: [PB2204](https://github.com/pb2204) 194 | 195 | Feel free to reach out for questions, feedback, or collaboration opportunities. 196 | 197 | ## Happy Coding 🚀 -------------------------------------------------------------------------------- /LEARN.md: -------------------------------------------------------------------------------- 1 | # Learn More About JS-HTTP 2 | 3 | Welcome to the "Learn More" guide for JS-HTTP, a simple JavaScript HTTP request library. In this guide, you will find additional information, advanced usage, and tips for getting the most out of JS-HTTP. 4 | 5 | ## Table of Contents 6 | 7 | - [Installation](#installation) 8 | - [Basic Usage](#basic-usage) 9 | - [Advanced Usage](#advanced-usage) 10 | - [Handling Responses](#handling-responses) 11 | - [Error Handling](#error-handling) 12 | - [Custom Headers](#custom-headers) 13 | - [Making Other HTTP Requests](#making-other-http-requests) 14 | - [Contributing](#contributing) 15 | 16 | ## Installation 17 | 18 | Before diving into JS-HTTP, make sure you have it installed in your project. You can install it via npm: 19 | 20 | ```bash 21 | npm install https-node.js 22 | ``` 23 | 24 | ## Basic Usage 25 | 26 | ### Making a GET Request 27 | 28 | To make a GET request, simply import the `JSHTTP` module and use the `get` method: 29 | 30 | ```javascript 31 | const JSHTTP = require('https-node.js'); 32 | 33 | JSHTTP.get('https://jsonplaceholder.typicode.com/posts/1') 34 | .then(response => { 35 | console.log('GET Response:', response); 36 | }) 37 | .catch(error => { 38 | console.error('GET Error:', error); 39 | }); 40 | ``` 41 | 42 | ### Making a POST Request 43 | 44 | To make a POST request, use the `post` method: 45 | 46 | ```javascript 47 | const JSHTTP = require('https-node.js'); 48 | 49 | const data = { userId: 1, id: 101, title: 'foo', body: 'bar' }; 50 | 51 | JSHTTP.post('https://jsonplaceholder.typicode.com/posts', data) 52 | .then(response => { 53 | console.log('POST Response:', response); 54 | }) 55 | .catch(error => { 56 | console.error('POST Error:', error); 57 | }); 58 | ``` 59 | 60 | ## Advanced Usage 61 | 62 | ### Handling Responses 63 | 64 | JS-HTTP returns the response data in JSON format by default. You can access the response data using `.then()` as shown in the basic examples. You can also access other properties of the response, such as status and headers. 65 | 66 | ```javascript 67 | JSHTTP.get('https://jsonplaceholder.typicode.com/posts/1') 68 | .then(response => { 69 | console.log('Status Code:', response.status); 70 | console.log('Response Headers:', response.headers); 71 | }) 72 | .catch(error => { 73 | console.error('GET Error:', error); 74 | }); 75 | ``` 76 | 77 | ### Error Handling 78 | 79 | JS-HTTP provides error handling for both GET and POST requests. If a request fails, you can catch the error and handle it gracefully. 80 | 81 | ```javascript 82 | JSHTTP.get('https://jsonplaceholder.typicode.com/nonexistent') 83 | .then(response => { 84 | console.log('GET Response:', response); 85 | }) 86 | .catch(error => { 87 | console.error('GET Error:', error.message); 88 | }); 89 | ``` 90 | 91 | ## Custom Headers 92 | 93 | You can customize headers for your requests by passing an optional `headers` object to the `get` or `post` methods. This allows you to set custom content types, authentication headers, and more. 94 | 95 | ```javascript 96 | const customHeaders = { 97 | 'Authorization': 'Bearer your-access-token', 98 | 'Content-Type': 'application/json', 99 | }; 100 | 101 | JSHTTP.get('https://api.example.com/data', { headers: customHeaders }) 102 | .then(response => { 103 | console.log('Custom Headers GET Response:', response); 104 | }) 105 | .catch(error => { 106 | console.error('Custom Headers GET Error:', error.message); 107 | }); 108 | ``` 109 | 110 | ## Making Other HTTP Requests 111 | 112 | JS-HTTP supports various other HTTP methods in addition to GET and POST. Here's how you can make requests using these methods: 113 | 114 | ### PUT Request 115 | 116 | To make a PUT request, use the `put` method: 117 | 118 | ```javascript 119 | JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', data) 120 | .then(response => { 121 | console.log('PUT Response:', response); 122 | }) 123 | .catch(error => { 124 | console.error('PUT Error:', error); 125 | }); 126 | ``` 127 | 128 | ### DELETE Request 129 | 130 | To make a DELETE request, use the `delete` method: 131 | 132 | ```javascript 133 | JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101') 134 | .then(response => { 135 | console.log('DELETE Response:', response); 136 | }) 137 | .catch(error => { 138 | console.error('DELETE Error:', error); 139 | }); 140 | ``` 141 | 142 | ### OPTIONS Request 143 | 144 | To make an OPTIONS request, use the `options` method: 145 | 146 | ```javascript 147 | JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', data) 148 | .then(response => { 149 | console.log('OPTIONS Response:', response); 150 | }) 151 | .catch(error => { 152 | console.error('OPTIONS Error:', error); 153 | }); 154 | ``` 155 | 156 | ### HEAD Request 157 | 158 | To make a HEAD request, use the `head` method: 159 | 160 | ```javascript 161 | JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', data) 162 | .then(response => { 163 | console.log('HEAD Response:', response); 164 | }) 165 | .catch(error => { 166 | console.error('HEAD Error:', error); 167 | }); 168 | ``` 169 | 170 | ### CONNECT Request 171 | 172 | To make a CONNECT request, use the `connect` method: 173 | 174 | ```javascript 175 | JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', data) 176 | .then(response => { 177 | console.log('CONNECT Response:', response); 178 | }) 179 | .catch(error => { 180 | console.error('CONNECT Error:', error); 181 | }); 182 | ``` 183 | 184 | ### TRACE Request 185 | 186 | To make a TRACE request, use the `trace` method: 187 | 188 | ```javascript 189 | JSHTTP.trace('https://jsonplaceholder.typicode.com/some-resource', data) 190 | .then(response => { 191 | console.log('TRACE Response:', response); 192 | }) 193 | .catch(error => { 194 | console.error('TRACE Error:', error); 195 | }); 196 | ``` 197 | 198 | ### PATCH Request 199 | 200 | To make a PATCH request, use the `patch` method: 201 | 202 | ```javascript 203 | JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', data) 204 | .then(response => { 205 | console 206 | 207 | .log('PATCH Response:', response); 208 | }) 209 | .catch(error => { 210 | console.error('PATCH Error:', error); 211 | }); 212 | ``` 213 | 214 | ## Contributing 215 | 216 | If you would like to contribute to the development of JS-HTTP, please read the [Contributing Guidelines](CONTRIBUTING.md) for information on how to get started. 217 | 218 | We welcome your contributions, bug reports, feature requests, and feedback! 219 | 220 | ## Happy Coding 🚀 --------------------------------------------------------------------------------