ap-laravel-ajax-helper 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+
2
+ ---
3
+
4
+ ### ✅ `LICENSE`
5
+
6
+ ```txt
7
+ MIT License
8
+
9
+ Copyright (c) 2025 AkashAp
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in
19
+ all copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Laravel AJAX Helper
2
+
3
+ A lightweight utility to simplify Laravel AJAX form requests with validation error handling.
4
+
5
+ ## 🚀 Usage
6
+
7
+ ### 📄 HTML Form Example
8
+
9
+ ```html
10
+ <form id="loginForm">
11
+ <input type="email" name="email" />
12
+ <span class="error-email text-red-500"></span>
13
+
14
+ <input type="password" name="password" />
15
+ <span class="error-password text-red-500"></span>
16
+
17
+ <button id="loginBtn">Login</button>
18
+ </form>
19
+ ```
20
+
21
+ ### 🧠 JavaScript Usage
22
+
23
+ ```js
24
+ import laravelAjax from 'laravel-ajax-helper';
25
+
26
+ laravelAjax({
27
+ url: '/login',
28
+ method: 'POST',
29
+ formId: 'loginForm',
30
+ buttonId: 'loginBtn',
31
+ loadingText: 'Logging in...'
32
+ })
33
+ .then(response => {
34
+ console.log('Success:', response);
35
+ })
36
+ .catch(errors => {
37
+ console.log('Validation or Server Errors:', errors);
38
+ });
39
+ ```
40
+
41
+ ## 🧰 Parameters
42
+
43
+ | Parameter | Type | Required | Description |
44
+ | ------------- | ------ | -------- | -------------------------------------------------------------------- |
45
+ | `url` | string | ✅ Yes | The Laravel route URL for the AJAX request. |
46
+ | `method` | string | ❌ No | Request method: 'POST', 'GET', 'PUT', 'DELETE'. Default: 'POST'. |
47
+ | `formId` | string | ✅ Yes | ID of the form element (do not pass form data manually). |
48
+ | `buttonId` | string | ❌ No | ID of the button to show a loading spinner during request. |
49
+ | `loadingText` | string | ❌ No | Text to show with spinner during the request. Default: 'Loading...'. |
50
+
51
+ ## ❗ Error Handling
52
+
53
+ * Laravel validation errors (422) are automatically displayed in elements with class `.error-[field_name]`.
54
+
55
+ * **Example:** For field `email`, use `.error-email`
56
+ * On other errors (like 500), a generic error is returned.
57
+
58
+ ## 📁 File Structure (Simplified)
59
+
60
+ ```
61
+ laravel-ajax-helper/
62
+ ├── dist/
63
+ │ └── index.js # Transpiled output
64
+ ├── src/
65
+ │ └── index.js # Main AJAX function
66
+ ├── README.md
67
+ ├── LICENSE
68
+ ├── package.json
69
+ ```
70
+
71
+ ## 🪪 License
72
+
73
+ This project is licensed under the [MIT License](./LICENSE).
74
+
75
+ ## 🙌 Contributing
76
+
77
+ Pull requests are welcome! Feel free to open issues or suggest improvements.
78
+
79
+ ## 💬 Author
80
+
81
+ Akash Ap – [@akashap](https://github.com/aakashap01)
82
+ Built with ❤️ for Laravel developers.
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = laravelAjax;
7
+ function laravelAjax(_ref) {
8
+ var url = _ref.url,
9
+ _ref$method = _ref.method,
10
+ method = _ref$method === void 0 ? 'POST' : _ref$method,
11
+ _ref$formId = _ref.formId,
12
+ formId = _ref$formId === void 0 ? null : _ref$formId,
13
+ _ref$buttonId = _ref.buttonId,
14
+ buttonId = _ref$buttonId === void 0 ? null : _ref$buttonId,
15
+ _ref$loadingText = _ref.loadingText,
16
+ loadingText = _ref$loadingText === void 0 ? 'Loading...' : _ref$loadingText;
17
+ return new Promise(function (resolve, reject) {
18
+ if (!url) return reject({
19
+ error: 'URL is required.'
20
+ });
21
+ if (!formId) return reject({
22
+ error: 'formId is required.'
23
+ });
24
+ var form = document.getElementById(formId);
25
+ if (!form) return reject({
26
+ error: "Form with ID \"".concat(formId, "\" not found.")
27
+ });
28
+ var formData = new FormData(form);
29
+ var btn = buttonId ? document.getElementById(buttonId) : null;
30
+ var originalButtonText = btn ? btn.innerHTML : null;
31
+
32
+ // Disable and show loading on button
33
+ if (btn) {
34
+ btn.disabled = true;
35
+ btn.innerHTML = "<span class=\"spinner-border spinner-border-sm me-2\"></span>".concat(loadingText);
36
+ }
37
+
38
+ // Clear all validation error messages
39
+ document.querySelectorAll('[class^="error-"]').forEach(function (el) {
40
+ if (el.tagName === 'DIV' || el.tagName === 'SPAN') {
41
+ el.innerText = '';
42
+ }
43
+ });
44
+ $.ajax({
45
+ url: url,
46
+ method: method,
47
+ data: formData,
48
+ contentType: false,
49
+ processData: false,
50
+ cache: false,
51
+ success: function success(response) {
52
+ if (btn) {
53
+ btn.innerHTML = originalButtonText;
54
+ btn.disabled = false;
55
+ }
56
+ resolve(response);
57
+ },
58
+ error: function error(xhr) {
59
+ if (xhr.status === 422) {
60
+ var _xhr$responseJSON;
61
+ var errors = ((_xhr$responseJSON = xhr.responseJSON) === null || _xhr$responseJSON === void 0 ? void 0 : _xhr$responseJSON.errors) || {};
62
+ for (var field in errors) {
63
+ var target = document.querySelector(".error-".concat(field));
64
+ if (target && (target.tagName === 'DIV' || target.tagName === 'SPAN')) {
65
+ target.innerText = errors[field][0];
66
+ }
67
+ }
68
+ reject(errors);
69
+ } else {
70
+ reject({
71
+ error: 'Unexpected error occurred.'
72
+ });
73
+ }
74
+ if (btn) {
75
+ btn.innerHTML = originalButtonText;
76
+ btn.disabled = false;
77
+ }
78
+ }
79
+ });
80
+ });
81
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "ap-laravel-ajax-helper",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight JavaScript utility for Laravel-style AJAX requests.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "scripts": {
8
+ "build": "babel src --out-dir dist"
9
+ },
10
+ "author": "AkashAp",
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "laravel",
14
+ "ajax",
15
+ "form",
16
+ "validation",
17
+ "jquery"
18
+ ],
19
+ "devDependencies": {
20
+ "@babel/cli": "^7.28.0",
21
+ "@babel/core": "^7.28.0",
22
+ "@babel/preset-env": "^7.28.0",
23
+ "rollup": "^3.0.0"
24
+ },
25
+ "peerDependencies": {
26
+ "jquery": ">=3.0.0"
27
+ },
28
+ "files": [
29
+ "dist/",
30
+ "src/",
31
+ "README.md",
32
+ "LICENSE"
33
+ ]
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,68 @@
1
+ export default function laravelAjax({
2
+ url,
3
+ method = 'POST',
4
+ formId = null,
5
+ buttonId = null,
6
+ loadingText = 'Loading...',
7
+ }) {
8
+ return new Promise((resolve, reject) => {
9
+ if (!url) return reject({ error: 'URL is required.' });
10
+ if (!formId) return reject({ error: 'formId is required.' });
11
+
12
+ const form = document.getElementById(formId);
13
+ if (!form) return reject({ error: `Form with ID "${formId}" not found.` });
14
+
15
+ const formData = new FormData(form);
16
+
17
+ const btn = buttonId ? document.getElementById(buttonId) : null;
18
+ const originalButtonText = btn ? btn.innerHTML : null;
19
+
20
+ // Disable and show loading on button
21
+ if (btn) {
22
+ btn.disabled = true;
23
+ btn.innerHTML = `<span class="spinner-border spinner-border-sm me-2"></span>${loadingText}`;
24
+ }
25
+
26
+ // Clear all validation error messages
27
+ document.querySelectorAll('[class^="error-"]').forEach((el) => {
28
+ if (el.tagName === 'DIV' || el.tagName === 'SPAN') {
29
+ el.innerText = '';
30
+ }
31
+ });
32
+
33
+ $.ajax({
34
+ url,
35
+ method,
36
+ data: formData,
37
+ contentType: false,
38
+ processData: false,
39
+ cache: false,
40
+ success: function (response) {
41
+ if (btn) {
42
+ btn.innerHTML = originalButtonText;
43
+ btn.disabled = false;
44
+ }
45
+ resolve(response);
46
+ },
47
+ error: function (xhr) {
48
+ if (xhr.status === 422) {
49
+ const errors = xhr.responseJSON?.errors || {};
50
+ for (const field in errors) {
51
+ const target = document.querySelector(`.error-${field}`);
52
+ if (target && (target.tagName === 'DIV' || target.tagName === 'SPAN')) {
53
+ target.innerText = errors[field][0];
54
+ }
55
+ }
56
+ reject(errors);
57
+ } else {
58
+ reject({ error: 'Unexpected error occurred.' });
59
+ }
60
+
61
+ if (btn) {
62
+ btn.innerHTML = originalButtonText;
63
+ btn.disabled = false;
64
+ }
65
+ },
66
+ });
67
+ });
68
+ }