mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-12-18 06:45:04 +01:00
[ADD] JS functions to the AA framework
This commit is contained in:
@@ -13,5 +13,6 @@ The Alliance Auth framework is split into several submodules, each of which is d
|
||||
|
||||
framework/api
|
||||
framework/css
|
||||
framework/js
|
||||
framework/templates
|
||||
:::
|
||||
|
||||
102
docs/development/custom/framework/js.md
Normal file
102
docs/development/custom/framework/js.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# 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`).
|
||||
Reference in New Issue
Block a user