Skip to content

Commit

Permalink
Added timeout, onUploadProgress and many more.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Jun 20, 2021
1 parent efc4c07 commit 89f2f18
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 23 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CAJAX (*ClassedAjax*) 3.0.2
# CAJAX (*ClassedAjax*) 3.0.3
CajaxJS is an lightweight JS Http client for everyone!

#### NPM
Expand All @@ -10,13 +10,13 @@ npm install cajaxjs
```html
<script src="https://cdn.jsdelivr.net/npm/cajaxjs@x/dist/cajax.js"></script>
<!-- OR -->
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/cajax.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].3/dist/cajax.js"></script>
```

## CDN (module, works for Deno too)
test.js
```javascript
import { Cajax } from 'https://cdn.jsdelivr.net/npm/[email protected].2/index.js'
import { Cajax } from 'https://cdn.jsdelivr.net/npm/[email protected].3/index.js'

new Cajax()
.get("https://interaapps.de")
Expand All @@ -28,7 +28,7 @@ new Cajax()
## Example Usage
```js

import { Cajax, CajaxRequest, CajaxResponse, FetchRequestProvider } from 'https://cdn.jsdelivr.net/npm/[email protected].2/index.js'
import { Cajax, CajaxRequest, CajaxResponse, FetchRequestProvider } from 'https://cdn.jsdelivr.net/npm/[email protected].3/index.js'

const client = new Cajax()

Expand Down Expand Up @@ -62,6 +62,24 @@ client.get("https://interaapps.de", {
client.post("https://interaapps.de", {
hello: "world"
})

// File Upload
const formData = new FormData()
formData.append("file", document.getElementById("input").files[0])
client.post("/upload", formData)
.then(res=>{
// ...
})


// onProgress (Not supportet for FetchRequestProvider, use XHRHttpRequestProvider)
client.get("https://interaapps.de", {
hello: "world"
}, {
onProgress(event){
console.log(event.loaded+' of '+event.total)
}
})
```

## options
Expand Down
2 changes: 1 addition & 1 deletion dist/cajax.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cajax",
"description": "Cajax/Prajax is an lightweight JS Http client for everyone!",
"version": "3.0.2",
"version": "3.0.3",
"bump": "patch",
"entry": "index.js",
"unstable": false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cajaxjs",
"version": "3.0.2",
"version": "3.0.3",
"description": "CajaxJS is an lightweight JS Http client for everyone! (Promise support) ",
"main": "index.js",
"scripts": {
Expand Down
21 changes: 13 additions & 8 deletions src/Cajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import FetchRequestProvider from "./requestprovider/FetchRequestProvider.js"
import XMLHttpRequestProvider from "./requestprovider/XMLHttpRequestProvider.js"

class Cajax {
constructor(baseUrl = null, defaultRequestOptions={}){
constructor(baseUrl = null, defaultRequestOptions=(new CajaxRequest())){
this.baseUrl = baseUrl
this.promiseInterceptor = (promise)=>{
return promise
}

this.defaultRequestOptions = defaultRequestOptions

if ('fetch' in window)
this.requestProvider = new FetchRequestProvider()
else
if (defaultRequestOptions.promiseInterceptor) {
this.promiseInterceptor = defaultRequestOptions.promiseInterceptor
defaultRequestOptions.promiseInterceptor = null
}


if ('XMLHttpRequest' in window)
this.requestProvider = new XMLHttpRequestProvider()
else
this.requestProvider = new FetchRequestProvider()
}

request(method, url, request = {}){
Expand All @@ -40,9 +46,8 @@ class Cajax {
}

if (body instanceof FormData) {
request.body = data
if (!request.contentType)
request.contentType = "application/x-www-form-urlencoded"
// Fetch and XHR-HTTP-REQUEST usualy check ít and sets the correct Content-Type for it.
request.body = body
} else if (typeof body == 'string') {
request.body = data
} else if (typeof body == 'object') {
Expand Down Expand Up @@ -128,7 +133,7 @@ class Cajax {
}

bearer(value){
this.defaultRequestOptions.headers["Authentication"] = "Bearer "+value
this.defaultRequestOptions.headers["Authorization"] = "Bearer "+value
return this
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/CajaxRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class CajaxRequest {
this.headers = {}
this.query = {}
this.body = null
this.timeout = null
}

setHeader(name, value){
Expand Down Expand Up @@ -37,6 +38,11 @@ class CajaxRequest {
this.body = body
return this
}

setTimeout(milliSecounds){
this.timeout = milliSecounds
return this
}
}

export default CajaxRequest
30 changes: 27 additions & 3 deletions src/requestprovider/FetchRequestProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,38 @@ class FetchRequestResponse extends CajaxResponse {
}

class FetchRequestProvider extends RequestProvider {
constructor(fetchFunction = null){
super()

this.fetchFunction = fetchFunction
}

handle(method, url, data) {
return new Promise((then, err)=>{
fetch(url, {
let headers = data.headers
if (data.contentType) {
headers = {'content-type':(data.contentType), ...data.headers};
}

const promise = (this.fetchFunction ? this.fetchFunction : window.fetch)(url, {
method,
headers: {'content-type':(data.contentType), ...data.headers},
headers: headers,
...(data.body && (method != 'GET' && method != 'HEAD') ? {body: data.body} : {})
}).then(res=>then(new FetchRequestResponse(res)))
.catch(err)
.catch(err)

if (data.timeout) {
Promise.race([
promise,
// Timeout Promise
new Promise((_,err)=>{
setTimeout(()=>{
err(new Error("timeout"))
}, data.timeout)
})
])
}

})
}
}
Expand Down
29 changes: 25 additions & 4 deletions src/requestprovider/XMLHttpRequestProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,45 @@ class XMLHttpRequestResponse extends CajaxResponse {
}

class XMLHttpRequestProvider extends RequestProvider {
constructor(xmlHTTPRequestClass = null){
super()


if (!xmlHTTPRequestClass)
this.xmlHTTPRequestClass = XMLHttpRequest
else
this.xmlHTTPRequestClass = xmlHTTPRequestClass
}

handle(method, url, data) {
return new Promise((then, err)=>{
const xhr = new XMLHttpRequest();
const xhr = new this.xmlHTTPRequestClass();

xhr.open(method, url);
xhr.setRequestHeader('content-type', data.contentType);
if (data.contentType)
xhr.setRequestHeader('content-type', data.contentType);

for (const name in data.headers)
xhr.setRequestHeader(name, data.headers[name]);

xhr.send(data.body)

xhr.onload = () => {
then(new XMLHttpRequestResponse(xhr))
}

if (data.onDownloadProgress)
xhr.onprogress = data.onDownloadProgress

if (xhr.upload && data.onUploadProgress)
xhr.upload.onprogress = data.onUploadProgress

xhr.onerror = err
xhr.onblocked = err
xhr.ontimeout = err

if (data.timeout)
xhr.timeout = data.timeout

xhr.send(data.body)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion uppm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cajax",
"version": "3.0.2",
"version": "3.0.3",
"description": "",
"author": "",
"keywords": [
Expand Down

0 comments on commit 89f2f18

Please sign in to comment.