https-node.js
Advanced tools
Comparing version 1.0.5 to 1.1.3
@@ -1,1 +0,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}`) } } } } }, r = {}; !function e(s) { var a = r[s]; if (void 0 !== a) return a.exports; var o = r[s] = { exports: {} }; return t[s](o, o.exports, e), o.exports }(82) })(); | ||
(()=>{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)})(); |
109
LEARN.md
@@ -13,2 +13,3 @@ # Learn More About JS-HTTP | ||
- [Custom Headers](#custom-headers) | ||
- [Making Other HTTP Requests](#making-other-http-requests) | ||
- [Contributing](#contributing) | ||
@@ -110,2 +111,106 @@ | ||
## Making Other HTTP Requests | ||
JS-HTTP supports various other HTTP methods in addition to GET and POST. Here's how you can make requests using these methods: | ||
### PUT Request | ||
To make a PUT request, use the `put` method: | ||
```javascript | ||
JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', data) | ||
.then(response => { | ||
console.log('PUT Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('PUT Error:', error); | ||
}); | ||
``` | ||
### DELETE Request | ||
To make a DELETE request, use the `delete` method: | ||
```javascript | ||
JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101') | ||
.then(response => { | ||
console.log('DELETE Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('DELETE Error:', error); | ||
}); | ||
``` | ||
### OPTIONS Request | ||
To make an OPTIONS request, use the `options` method: | ||
```javascript | ||
JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', data) | ||
.then(response => { | ||
console.log('OPTIONS Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('OPTIONS Error:', error); | ||
}); | ||
``` | ||
### HEAD Request | ||
To make a HEAD request, use the `head` method: | ||
```javascript | ||
JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', data) | ||
.then(response => { | ||
console.log('HEAD Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('HEAD Error:', error); | ||
}); | ||
``` | ||
### CONNECT Request | ||
To make a CONNECT request, use the `connect` method: | ||
```javascript | ||
JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', data) | ||
.then(response => { | ||
console.log('CONNECT Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('CONNECT Error:', error); | ||
}); | ||
``` | ||
### TRACE Request | ||
To make a TRACE request, use the `trace` method: | ||
```javascript | ||
JSHTTP.trace('https://jsonplaceholder.typicode.com/some-resource', data) | ||
.then(response => { | ||
console.log('TRACE Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('TRACE Error:', error); | ||
}); | ||
``` | ||
### PATCH Request | ||
To make a PATCH request, use the `patch` method: | ||
```javascript | ||
JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', data) | ||
.then(response => { | ||
console | ||
.log('PATCH Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('PATCH Error:', error); | ||
}); | ||
``` | ||
## Contributing | ||
@@ -115,2 +220,4 @@ | ||
We welcome your contributions, bug reports, feature requests, and feedback! | ||
We welcome your contributions, bug reports, feature requests, and feedback! | ||
## Happy Coding 🚀 |
@@ -18,3 +18,3 @@ { | ||
"main": "dist/js-http.min.js", | ||
"version": "1.0.5", | ||
"version": "1.1.3", | ||
"scripts": { | ||
@@ -21,0 +21,0 @@ "test": "mocha tests/test-js-http.js", |
@@ -0,4 +1,7 @@ | ||
Certainly! Here's your updated `README.md` file with similar calls for the other HTTP methods (PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, PATCH) added to the Usage section: | ||
```markdown | ||
# JS-HTTP | ||
A simple JavaScript HTTP request library for making GET and POST requests using the Fetch API. | ||
A simple JavaScript HTTP request library for making GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, and PATCH requests using the Fetch API. | ||
@@ -21,3 +24,3 @@ - [Installation](#installation) | ||
```bash | ||
npm i https-node.js | ||
npm install https-node.js | ||
``` | ||
@@ -63,3 +66,3 @@ | ||
JS-HTTP is a lightweight JavaScript library that simplifies making HTTP requests in your web applications. It provides a straightforward API for making GET and POST requests using the Fetch API. | ||
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. | ||
@@ -96,2 +99,73 @@ ## Examples | ||
}); | ||
// Make a PUT request | ||
const putData = { userId: 1, id: 1, title: 'updated title', body: 'updated body' }; | ||
JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', putData) | ||
.then(response => { | ||
console.log('PUT Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('PUT Error:', error); | ||
}); | ||
// Make a DELETE request | ||
JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101') | ||
.then(response => { | ||
console.log('DELETE Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('DELETE Error:', error); | ||
}); | ||
// Make an OPTIONS request | ||
const optionsData = { someOption: 'value' }; | ||
JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', optionsData) | ||
.then(response => { | ||
console.log('OPTIONS Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('OPTIONS Error:', error); | ||
}); | ||
// Make a HEAD request | ||
const headData = { someHeader: 'value' }; | ||
JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', headData) | ||
.then(response => { | ||
console.log('HEAD Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('HEAD Error:', error); | ||
}); | ||
// Make a CONNECT request | ||
const connectData = { someData: 'value' }; | ||
JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', connectData) | ||
.then(response => { | ||
console.log('CONNECT Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('CONNECT Error:', error); | ||
}); | ||
// Make a TRACE request | ||
const traceData = { someData: 'value' }; | ||
JSHTTP.trace('https://jsonplaceholder.typicode.com/some | ||
-resource', traceData) | ||
.then(response => { | ||
console.log('TRACE Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('TRACE Error:', error); | ||
}); | ||
// Make a PATCH request | ||
const patchData = { someData: 'value' }; | ||
JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', patchData) | ||
.then(response => { | ||
console.log('PATCH Response:', response); | ||
}) | ||
.catch(error => { | ||
console.error('PATCH Error:', error); | ||
}); | ||
``` | ||
@@ -109,2 +183,4 @@ | ||
Feel free to reach out for questions, feedback, or collaboration opportunities. | ||
Feel free to reach out for questions, feedback, or collaboration opportunities. | ||
## Happy Coding 🚀 |
@@ -33,4 +33,126 @@ // src/js-http.js | ||
} | ||
static async put(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`PUT request failed: ${error.message}`); | ||
} | ||
} | ||
static async delete(url) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'DELETE', | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`DELETE request failed: ${error.message}`); | ||
} | ||
} | ||
static async options(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'OPTIONS', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`OPTIONS request failed: ${error.message}`); | ||
} | ||
} | ||
static async head(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'HEAD', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`HEAD request failed: ${error.message}`); | ||
} | ||
} | ||
static async connect(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'CONNECT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`CONNECT request failed: ${error.message}`); | ||
} | ||
} | ||
static async trace(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'TRACE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`TRACE request failed: ${error.message}`); | ||
} | ||
} | ||
static async patch(url, data) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`PATCH request failed: ${error.message}`); | ||
} | ||
} | ||
} | ||
module.exports = JSHTTP; | ||
module.exports = JSHTTP; |
@@ -25,18 +25,49 @@ // tests/test-js-http.js | ||
it('should handle GET request errors', async () => { | ||
try { | ||
await JSHTTP.get('https://jsonplaceholder.typicode.com/nonexistent'); | ||
} catch (error) { | ||
expect(error.message).to.contain('GET request failed'); | ||
} | ||
// Test for PUT request | ||
it('should make a successful PUT request', async () => { | ||
const data = { userId: 1, id: 1, title: 'updated title', body: 'updated body' }; | ||
const response = await JSHTTP.put('https://jsonplaceholder.typicode.com/posts/1', data); | ||
expect(response).to.be.an('object'); | ||
expect(response.id).to.equal(1); | ||
expect(response.title).to.equal('updated title'); | ||
expect(response.body).to.equal('updated body'); | ||
}); | ||
it('should handle POST request errors', async () => { | ||
try { | ||
const data = { invalid: 'data' }; | ||
await JSHTTP.post('https://jsonplaceholder.typicode.com/posts', data); | ||
} catch (error) { | ||
expect(error.message).to.contain('POST request failed'); | ||
} | ||
// Test for DELETE request | ||
it('should make a successful DELETE request', async () => { | ||
const response = await JSHTTP.delete('https://jsonplaceholder.typicode.com/posts/101'); | ||
expect(response).to.be.an('object'); | ||
expect(response).to.deep.equal({}); | ||
}); | ||
}); | ||
// Test for OPTIONS request | ||
it('should make a successful OPTIONS request', async () => { | ||
const data = { someOption: 'value' }; // Replace with appropriate data if needed | ||
const response = await JSHTTP.options('https://jsonplaceholder.typicode.com/some-resource', data); | ||
expect(response).to.be.an('object'); | ||
}); | ||
// Test for HEAD request | ||
it('should make a successful HEAD request', async () => { | ||
const data = { someHeader: 'value' }; // Replace with appropriate data if needed | ||
const response = await JSHTTP.head('https://jsonplaceholder.typicode.com/some-resource', data); | ||
}); | ||
// Test for CONNECT request | ||
it('should make a successful CONNECT request', async () => { | ||
const data = { someData: 'value' }; // Replace with appropriate data if needed | ||
const response = await JSHTTP.connect('https://jsonplaceholder.typicode.com/some-resource', data); | ||
}); | ||
// Test for TRACE request | ||
it('should make a successful TRACE request', async () => { | ||
const data = { someData: 'value' }; // Replace with appropriate data if needed | ||
const response = await JSHTTP.trace('https://jsonplaceholder.typicode.com/some-resource', data); | ||
}); | ||
// Test for PATCH request | ||
it('should make a successful PATCH request', async () => { | ||
const data = { someData: 'value' }; // Replace with appropriate data if needed | ||
const response = await JSHTTP.patch('https://jsonplaceholder.typicode.com/some-resource', data); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36932
13
214
182
18