mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-08-24 02:41:42 +02:00
103 lines
2.2 KiB
Markdown
103 lines
2.2 KiB
Markdown
# JavaScript Framework
|
|
|
|
This contains some simple JavaScript functions that are used throughout Alliance Auth,
|
|
so they can be used by community app developers as well.
|
|
|
|
The JS file is already loaded in the base template, so it is globally available.
|
|
|
|
## Functions
|
|
|
|
The following functions are available in the JavaScript framework:
|
|
|
|
### isArray()
|
|
|
|
Checks if the given value is an array.
|
|
|
|
Usage:
|
|
|
|
```javascript
|
|
/* global isArray */
|
|
|
|
if (isArray(someVariable)) {
|
|
console.log('This is an array');
|
|
} else {
|
|
console.log('This is not an array');
|
|
}
|
|
```
|
|
|
|
### isObject()
|
|
|
|
Checks if the given value is an object.
|
|
|
|
Usage:
|
|
|
|
```javascript
|
|
/* global isObject */
|
|
|
|
if (isObject(someVariable)) {
|
|
console.log('This is a plain object');
|
|
} else {
|
|
console.log('This is not a plain object');
|
|
}
|
|
```
|
|
|
|
### fetchGet()
|
|
|
|
Performs a GET request to the given URL and returns a Promise that resolves with the response data.
|
|
|
|
Usage:
|
|
|
|
```javascript
|
|
/* global fetchGet */
|
|
|
|
fetchGet({
|
|
url: url,
|
|
responseIsJson: false
|
|
}).then((data) => {
|
|
// Process the fetched data
|
|
}).catch((error) => {
|
|
console.error(`Error: ${error.message}`);
|
|
|
|
// Handle the error appropriately
|
|
});
|
|
```
|
|
|
|
#### fetchGet() Parameters
|
|
|
|
- `url`: The URL to fetch data from.
|
|
- `payload`: Optional data to send with the request. Can be an object or a string.
|
|
- `responseIsJson`: Optional boolean indicating if the response should be parsed as JSON (default is `true`).
|
|
|
|
### fetchPost()
|
|
|
|
Performs a POST request to the given URL with the provided data and returns a Promise that resolves with the response data.
|
|
|
|
Usage:
|
|
|
|
```javascript
|
|
/* global fetchPost */
|
|
|
|
fetchPost({
|
|
url: url,
|
|
csrfToken: csrfToken,
|
|
payload: {
|
|
key: 'value',
|
|
anotherKey: 'anotherValue'
|
|
},
|
|
responseIsJson: true
|
|
}).then((data) => {
|
|
// Process the fetched data
|
|
}).catch((error) => {
|
|
console.error(`Error: ${error.message}`);
|
|
|
|
// Handle the error appropriately
|
|
});
|
|
```
|
|
|
|
#### fetchPost() Parameters
|
|
|
|
- `url`: The URL to send the POST request to.
|
|
- `csrfToken`: The CSRF token to include in the request headers.
|
|
- `payload`: The data as JS object to send with the request.
|
|
- `responseIsJson`: Optional boolean indicating if the response should be parsed as JSON (default is `true`).
|