Skip to content

Commit

Permalink
Added JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 5, 2024
1 parent af8394d commit 7d0e5e3
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 29 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CAJAX (*ClassedAjax*) 3.0.5p client for everyone!
# CAJAX (*ClassedAjax*) 3.0.6 client for everyone!

#### NPM
```bash
Expand All @@ -9,13 +9,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/cajaxjs@3.0.5/dist/cajax.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cajaxjs@3.0.6/dist/cajax.js"></script>
```

## 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")
Expand All @@ -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()

Expand Down Expand Up @@ -70,7 +70,6 @@ client.post("/upload", formData)
// ...
})


// onDownloadProgress (Not supportet for FetchRequestProvider, use XHRHttpRequestProvider)
client.get("https://interaapps.de", {
hello: "world"
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.5",
"version": "3.0.6",
"description": "CajaxJS is an lightweight JS Http client for everyone! (Promise support) ",
"main": "index.js",
"type": "module",
Expand Down
60 changes: 39 additions & 21 deletions src/Cajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<CajaxResponse>)} promise
* @return {*}
*/
this.promiseInterceptor = (promise) => {
return promise
}
Expand All @@ -17,17 +25,25 @@ class Cajax {
}
}

/**
* @return {Promise<void>}
*/
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<CajaxResponse>}
* @param {(string | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'CONNECT' | 'DELETE' | 'TRACE' | 'HEAD' | 'OPTIONS')} method
* @param {string} url
* @param {*} request
* @returns {Promise<CajaxResponse|*>}
*/
async request(method, url, request = {}) {
// Sets a request provider if none is given
Expand All @@ -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, '+');
}

Expand All @@ -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, '+');
}
}
Expand All @@ -86,78 +102,80 @@ class Cajax {


/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
get(url, query = {}, request = {}) {
request.query = query
return this.request("GET", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
delete(url, query = {}, request = {}) {
request.query = query
return this.request("DELETE", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
post(url, body = null, request = {}) {
request.body = body
return this.request("POST", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
put(url, body = null, request = {}) {
request.body = body
return this.request("PUT", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
connect(url, body = null, request = {}) {
request.body = body
return this.request("CONNECT", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
head(url, body = null, request = {}) {
request.body = body
return this.request("HEAD", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
patch(url, body = null, request = {}) {
request.body = body
return this.request("PATCH", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
options(url, body = null, request = {}) {
request.body = body
return this.request("OPTIONS", url, request)
}

/**
* @returns {Promise<CajaxResponse>}
* @returns {Promise<CajaxResponse|*>}
*/
trace(url, body = null, request = {}) {
request.body = body
return this.request("TRACE", url, request)
}

/**
* @param {string} name
* @param {*} value
* @returns {Cajax}
*/
setHeader(name, value) {
Expand All @@ -166,6 +184,7 @@ class Cajax {
}

/**
* @param {string} value
* @returns {Cajax}
*/
setContentType(value) {
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions src/CajaxRequest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
class CajaxRequest {
constructor() {
/**
* @type {{}}
*/
this.headers = {}
/**
* @type {{}}
*/
this.query = {}
this.body = null
/**
* @type {null|number}
*/
this.timeout = null
}

Expand Down
28 changes: 28 additions & 0 deletions src/CajaxResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,46 @@ class CajaxResponse {
this.providerType = null
}

/**
* @return {Promise<any>}
*/
async json() {
return JSON.parse(await this.text())
}

/**
* @return {Promise<null|string>}
*/
async text() {
return null
}

/**
* @return {Promise<*|FetchRequestProvider|XMLHttpRequestResponse>}
*/
async res() {
return null
}

/**
* @template K
* @param {Class<K>} clazz
* @param constructArgs
* @return {Promise<K>}
*/
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
}
Expand Down
4 changes: 3 additions & 1 deletion src/requestprovider/FetchRequestProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class FetchRequestResponse extends CajaxResponse {
}

class FetchRequestProvider extends RequestProvider {
name = 'FETCH_PROVIDER'

constructor(fetchFunction = null) {
super()

Expand All @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion src/requestprovider/NodeJSRequestProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class NodeJSRequestResponse extends CajaxResponse {
}

class NodeJSRequestProvider extends RequestProvider {
name = 'NODE_PROVIDER'

constructor() {
super()
this.node = true
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions src/requestprovider/RequestProvder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class RequestProvider {
/**
* @param {string} method
* @param {string} url
* @param {*} data
* @return {Promise<CajaxResponse>}
*/
async handle(method, url, data) {
}
}
Expand Down
1 change: 1 addition & 0 deletions src/requestprovider/XMLHttpRequestProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class XMLHttpRequestResponse extends CajaxResponse {
}

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

Expand Down
5 changes: 5 additions & 0 deletions tests/buntest.js
Original file line number Diff line number Diff line change
@@ -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()))
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }%`)
Expand Down

0 comments on commit 7d0e5e3

Please sign in to comment.