diff --git a/README.md b/README.md
index aa8ba9f..b29c678 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# CAJAX (*ClassedAjax*) 3.0.5p client for everyone!
+# CAJAX (*ClassedAjax*) 3.0.6 client for everyone!
#### NPM
```bash
@@ -9,13 +9,13 @@ npm install cajaxjs
```html
-
+
```
## CDN (module, works for Deno too)
test.js
```javascript
-import { Cajax } from 'https://cdn.jsdelivr.net/npm/cajaxjs@3.0.5/index.js'
+import { Cajax } from 'https://cdn.jsdelivr.net/npm/cajaxjs@3.0.6/index.js'
new Cajax()
.get("https://interaapps.de")
@@ -27,7 +27,7 @@ new Cajax()
## Example Usage
```js
-import { Cajax, CajaxRequest, CajaxResponse, FetchRequestProvider } from 'https://cdn.jsdelivr.net/npm/cajaxjs@3.0.5/index.js'
+import { Cajax, CajaxRequest, CajaxResponse, FetchRequestProvider } from 'https://cdn.jsdelivr.net/npm/cajaxjs@3.0.6/index.js'
const client = new Cajax()
@@ -70,7 +70,6 @@ client.post("/upload", formData)
// ...
})
-
// onDownloadProgress (Not supportet for FetchRequestProvider, use XHRHttpRequestProvider)
client.get("https://interaapps.de", {
hello: "world"
diff --git a/package.json b/package.json
index 1973c2f..11b5b59 100755
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "cajaxjs",
- "version": "3.0.5",
+ "version": "3.0.6",
"description": "CajaxJS is an lightweight JS Http client for everyone! (Promise support) ",
"main": "index.js",
"type": "module",
diff --git a/src/Cajax.js b/src/Cajax.js
index baedfbe..822e2d3 100644
--- a/src/Cajax.js
+++ b/src/Cajax.js
@@ -2,9 +2,17 @@ import CajaxRequest from "./CajaxRequest.js"
import FetchRequestProvider from "./requestprovider/FetchRequestProvider.js"
import XMLHttpRequestProvider from "./requestprovider/XMLHttpRequestProvider.js"
-class Cajax {
+export default class Cajax {
+ /**
+ * @param {string} baseUrl
+ * @param {CajaxRequest} defaultRequestOptions
+ */
constructor(baseUrl = null, defaultRequestOptions = (new CajaxRequest())) {
this.baseUrl = baseUrl
+ /**
+ * @param {function(Promise)} promise
+ * @return {*}
+ */
this.promiseInterceptor = (promise) => {
return promise
}
@@ -17,17 +25,25 @@ class Cajax {
}
}
+ /**
+ * @return {Promise}
+ */
async createRequestProvider() {
+ const denoOrBun = typeof Bun !== 'undefined' || typeof Deno !== 'undefined'
+
if (typeof window !== 'undefined' && 'XMLHttpRequest' in window)
this.requestProvider = new XMLHttpRequestProvider()
- else if (typeof window !== 'undefined' && 'fetch' in window)
- this.requestProvider = new FetchRequestProvider()
+ else if (typeof window !== 'undefined' && 'fetch' in window || denoOrBun)
+ this.requestProvider = new FetchRequestProvider(denoOrBun ? fetch : null)
else if (typeof process !== 'undefined')
this.requestProvider = new (await import("../src/requestprovider/NodeJSRequestProvider.js")).default
}
/**
- * @returns {Promise}
+ * @param {(string | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'CONNECT' | 'DELETE' | 'TRACE' | 'HEAD' | 'OPTIONS')} method
+ * @param {string} url
+ * @param {*} request
+ * @returns {Promise}
*/
async request(method, url, request = {}) {
// Sets a request provider if none is given
@@ -51,7 +67,7 @@ class Cajax {
url += '?'
const urlEncodedDataPairs = [];
for (const name in request.query)
- urlEncodedDataPairs.push(encodeURIComponent(name) + '=' + encodeURIComponent(request.query[name]));
+ urlEncodedDataPairs.push(`${encodeURIComponent(name)}=${encodeURIComponent(request.query[name])}`);
url += urlEncodedDataPairs.join('&').replace(/%20/g, '+');
}
@@ -64,12 +80,12 @@ class Cajax {
if (!request.contentType)
request.contentType = "application/json"
- if (request.contentType == "application/json") {
+ if (request.contentType === "application/json") {
request.body = JSON.stringify(body)
- } else if (request.contentType == "application/x-www-form-urlencoded") {
+ } else if (request.contentType === "application/x-www-form-urlencoded") {
const urlEncodedDataPairs = [];
for (const name in body)
- urlEncodedDataPairs.push(encodeURIComponent(name) + '=' + encodeURIComponent(body[name]));
+ urlEncodedDataPairs.push(`${encodeURIComponent(name)}=${encodeURIComponent(body[name])}`);
request.body = urlEncodedDataPairs.join('&').replace(/%20/g, '+');
}
}
@@ -86,7 +102,7 @@ class Cajax {
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
get(url, query = {}, request = {}) {
request.query = query
@@ -94,7 +110,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
delete(url, query = {}, request = {}) {
request.query = query
@@ -102,7 +118,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
post(url, body = null, request = {}) {
request.body = body
@@ -110,7 +126,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
put(url, body = null, request = {}) {
request.body = body
@@ -118,7 +134,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
connect(url, body = null, request = {}) {
request.body = body
@@ -126,7 +142,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
head(url, body = null, request = {}) {
request.body = body
@@ -134,7 +150,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
patch(url, body = null, request = {}) {
request.body = body
@@ -142,7 +158,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
options(url, body = null, request = {}) {
request.body = body
@@ -150,7 +166,7 @@ class Cajax {
}
/**
- * @returns {Promise}
+ * @returns {Promise}
*/
trace(url, body = null, request = {}) {
request.body = body
@@ -158,6 +174,8 @@ class Cajax {
}
/**
+ * @param {string} name
+ * @param {*} value
* @returns {Cajax}
*/
setHeader(name, value) {
@@ -166,6 +184,7 @@ class Cajax {
}
/**
+ * @param {string} value
* @returns {Cajax}
*/
setContentType(value) {
@@ -174,12 +193,11 @@ class Cajax {
}
/**
+ * @param {string} value
* @returns {Cajax}
*/
bearer(value) {
- this.defaultRequestOptions.headers["Authorization"] = "Bearer " + value
+ this.defaultRequestOptions.headers["Authorization"] = `Bearer ${value}`
return this
}
-}
-
-export default Cajax
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/CajaxRequest.js b/src/CajaxRequest.js
index 87cd687..c61dda7 100644
--- a/src/CajaxRequest.js
+++ b/src/CajaxRequest.js
@@ -1,8 +1,17 @@
class CajaxRequest {
constructor() {
+ /**
+ * @type {{}}
+ */
this.headers = {}
+ /**
+ * @type {{}}
+ */
this.query = {}
this.body = null
+ /**
+ * @type {null|number}
+ */
this.timeout = null
}
diff --git a/src/CajaxResponse.js b/src/CajaxResponse.js
index b37c0c5..3682f88 100644
--- a/src/CajaxResponse.js
+++ b/src/CajaxResponse.js
@@ -8,18 +8,46 @@ class CajaxResponse {
this.providerType = null
}
+ /**
+ * @return {Promise}
+ */
async json() {
return JSON.parse(await this.text())
}
+ /**
+ * @return {Promise}
+ */
async text() {
return null
}
+ /**
+ * @return {Promise<*|FetchRequestProvider|XMLHttpRequestResponse>}
+ */
async res() {
return null
}
+ /**
+ * @template K
+ * @param {Class} clazz
+ * @param constructArgs
+ * @return {Promise}
+ */
+ async map(clazz, ...constructArgs) {
+ const values = await this.json()
+ const obj = new clazz(...constructArgs)
+ for (const [key, value] of Object(values)) {
+ obj[key] = values
+ }
+ return obj
+ }
+
+ /**
+ * @param {string} name
+ * @return {string|null}
+ */
header(name) {
return null
}
diff --git a/src/requestprovider/FetchRequestProvider.js b/src/requestprovider/FetchRequestProvider.js
index faf7689..42bce78 100644
--- a/src/requestprovider/FetchRequestProvider.js
+++ b/src/requestprovider/FetchRequestProvider.js
@@ -36,6 +36,8 @@ class FetchRequestResponse extends CajaxResponse {
}
class FetchRequestProvider extends RequestProvider {
+ name = 'FETCH_PROVIDER'
+
constructor(fetchFunction = null) {
super()
@@ -52,7 +54,7 @@ class FetchRequestProvider extends RequestProvider {
const promise = (this.fetchFunction ? this.fetchFunction : window.fetch)(url, {
method,
headers: headers,
- ...(data.body && (method != 'GET' && method != 'HEAD') ? {body: data.body} : {})
+ ...(data.body && (method !== 'GET' && method !== 'HEAD') ? {body: data.body} : {})
}).then(res => then(new FetchRequestResponse(res)))
.catch(err)
diff --git a/src/requestprovider/NodeJSRequestProvider.js b/src/requestprovider/NodeJSRequestProvider.js
index a8152f4..4d2ad52 100644
--- a/src/requestprovider/NodeJSRequestProvider.js
+++ b/src/requestprovider/NodeJSRequestProvider.js
@@ -35,6 +35,8 @@ class NodeJSRequestResponse extends CajaxResponse {
}
class NodeJSRequestProvider extends RequestProvider {
+ name = 'NODE_PROVIDER'
+
constructor() {
super()
this.node = true
@@ -103,7 +105,7 @@ class NodeJSRequestProvider extends RequestProvider {
if (usingFormData) {
data.body.pipe(req)
- } else if (data.body && (method != 'GET' && method != 'HEAD')) {
+ } else if (data.body && (method !== 'GET' && method !== 'HEAD')) {
req.write(data.body)
}
diff --git a/src/requestprovider/RequestProvder.js b/src/requestprovider/RequestProvder.js
index 771eeb5..f4c4090 100644
--- a/src/requestprovider/RequestProvder.js
+++ b/src/requestprovider/RequestProvder.js
@@ -1,4 +1,10 @@
class RequestProvider {
+ /**
+ * @param {string} method
+ * @param {string} url
+ * @param {*} data
+ * @return {Promise}
+ */
async handle(method, url, data) {
}
}
diff --git a/src/requestprovider/XMLHttpRequestProvider.js b/src/requestprovider/XMLHttpRequestProvider.js
index c1a4ad0..beb1bae 100644
--- a/src/requestprovider/XMLHttpRequestProvider.js
+++ b/src/requestprovider/XMLHttpRequestProvider.js
@@ -35,6 +35,7 @@ class XMLHttpRequestResponse extends CajaxResponse {
}
class XMLHttpRequestProvider extends RequestProvider {
+ name = 'XML_HTTP_REQUEST_PROVIDER'
constructor(xmlHTTPRequestClass = null) {
super()
diff --git a/tests/buntest.js b/tests/buntest.js
new file mode 100644
index 0000000..1129cb4
--- /dev/null
+++ b/tests/buntest.js
@@ -0,0 +1,5 @@
+import { Cajax } from '../index.js'
+
+const client = new Cajax('https://pastefy.app/api/v2/paste/Ns8GL2TB')
+
+console.log(await client.get('').then(r => r.json()))
\ No newline at end of file
diff --git a/tests/test.js b/tests/test.js
index be592a7..e98ff50 100644
--- a/tests/test.js
+++ b/tests/test.js
@@ -8,6 +8,7 @@ a.append("hellonot", "world")
const cajax = new Cajax()
+
cajax.post("https://httpbin.org/post", a, {
onDownloadProgress(e){
console.log("DW", `${e.loaded} / ${e.total} ${ (e.loaded / e.total) * 100 }%`)