@weapnl/js-junction 0.0.11 → 0.1.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.1.1
6
+ - Added functionality to add the same callback on a request multiple times.
7
+ - ⚠️ With the old functionality setting a callback on a request would overwrite the globally defined callback. This is no longer the case, as both the global and request callbacks will be executed.
8
+ - Improved how axios responses are handled.
9
+ - ⚠️ Deprecated `failed` getter on the `Response` class in favor of a new `isFailed` getter.
10
+ - Added `onCancelled` method on `Api` and `Response` classes.
11
+ - Updated the `docker-compose.yml` file to use the `node:22` image.
12
+
13
+ ## v0.1.0
14
+ - Added the Temporary Media Upload functionality.
15
+
5
16
  ## v0.0.11
6
17
  - Fixed a bug where cancelled requests would cause issues when cancelling subsequent requests.
7
18
 
package/README.md CHANGED
@@ -12,6 +12,7 @@ This package has support for Typescript (TS).
12
12
  - [Creating Models](#creating-models)
13
13
  - [Performing Requests](#performing-requests)
14
14
  - [Applying Filters and Scopes](#applying-filters-and-scopes)
15
+ - [Uploading Files with Spatie Medialibrary](#uploading-files-with-spatie-medialibrary)
15
16
 
16
17
  ## Installation
17
18
  ```bash
@@ -310,11 +311,27 @@ let request = await api.request('users')
310
311
  // Any other status code not caught by other callbacks.
311
312
  })
312
313
  .onFinished((response) => {
313
- // After the request was finished (cancelled requests excluded).
314
+ // After the request was finished (a request is finished if it returned a response).
315
+ })
316
+ .onCancelled((response) => {
317
+ // When a request is cancelled.
314
318
  })
315
319
  .get();
316
320
  ```
317
321
 
322
+ It's possible to clear the currently set callbacks on a request.
323
+
324
+ ```javascript
325
+ request
326
+ .clearOnSuccessCallbacks()
327
+ .clearOnUnauthorizedCallbacks()
328
+ .clearOnForbiddenCallbacks()
329
+ .clearOnValidationErrorCallbacks()
330
+ .clearOnErrorCallbacks()
331
+ .clearOnFinishedCallbacks()
332
+ .clearOnCancelledCallbacks();
333
+ ```
334
+
318
335
  **Cancel requests**
319
336
 
320
337
  If you want to cancel a pending request, you can do the following:
@@ -371,3 +388,53 @@ api.removeHeader('HEADER NAME HERE'); // Removes the header.
371
388
  **Sample response**
372
389
 
373
390
  After executing a request, the property `response` contains a `Response` object, which has properties `statusCode`, `data` and `validation`.
391
+
392
+ ### Uploading Files with [Spatie Medialibrary](https://spatie.be/docs/laravel-medialibrary/v11/introduction)
393
+
394
+ #### Step 1: Uploading Files to a Model
395
+ To upload files to a model, use the `upload` function available on the model instance. This function requires two arguments:
396
+
397
+ 1. **Uploaded Files**: An array of files, typically obtained from an input field of `type="file"`.
398
+ 2. **Collection Name**: The name of the media [collection](https://spatie.be/docs/laravel-medialibrary/v11/working-with-media-collections/simple-media-collections) to which the files should be attached. This corresponds to the collection defined in your Laravel model.
399
+
400
+ **Example Usage:**
401
+ ```js
402
+ // Retrieve the employee model instance (e.g., Employee with ID 3)
403
+ const employee = Employee.show(3);
404
+
405
+ // Upload the files to the 'IdentityFiles' collection
406
+ employee.upload(uploadedFiles, 'IdentityFiles');
407
+ ```
408
+
409
+ In this example, `uploadedFiles` is an array of files from an input field, and `'IdentityFiles'` is the name of the media collection on the `Employee` model where these files should be stored.
410
+
411
+ #### Step 2: Handling the Uploaded Files
412
+ Once the `upload` function is called, the files are sent to the API. The API temporarily stores these files in the media library and returns the media IDs associated with each file. These media IDs are automatically set on the model instance.
413
+
414
+ #### Step 3: Saving the Model with Attached Media
415
+ After uploading the files, you can call the `.save()` method on the model instance. This step finalizes the process by permanently attaching the uploaded files to the specified media collection on the model. The media IDs stored on the model are now linked to the correct collection in the database.
416
+
417
+ **Example of Saving the Model:**
418
+ ```js
419
+ // Save the employee model with the uploaded files attached
420
+ employee.save();
421
+ ```
422
+
423
+ When the `save()` method is invoked, the model is updated or created (depending on whether it was previously persisted), and the uploaded media files are attached to the correct collection as defined in the earlier steps.
424
+
425
+ #### Advanced Usage: Uploading Files in Nested Structures
426
+ If your model contains nested relationships, such as an `Employee` model with a `Contact` relationship, you can still use the `upload` function to attach files to the appropriate collection within the nested structure.
427
+
428
+ **Example with Nested Structure:**
429
+ ```js
430
+ // Retrieve the employee model instance
431
+ const employee = Employee.show(3);
432
+
433
+ // Upload a profile picture to the 'ProfilePicture' collection within the 'Contact' relationship
434
+ employee.contact.upload(uploadedFiles, 'ProfilePicture');
435
+
436
+ // Save the employee model, including the nested contact with the attached profile picture
437
+ employee.save();
438
+ ```
439
+
440
+ In this scenario, the uploaded files are linked to the `ProfilePicture` collection within the `Contact` relationship of the `Employee` model. When the `save()` method is called, the files are properly attached within the nested structure.