@d1g1tal/transportr 1.2.0 → 1.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d1g1tal/transportr",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "JavaScript wrapper for the Fetch API",
5
5
  "type": "module",
6
6
  "exports": {
@@ -43,17 +43,17 @@
43
43
  "homepage": "https://github.com/D1g1talEntr0py/transportr#readme",
44
44
  "devDependencies": {
45
45
  "@skypack/package-check": "^0.2.2",
46
- "@xmldom/xmldom": "^0.8.7",
47
- "esbuild": "^0.17.19",
48
- "eslint": "^8.41.0",
46
+ "@xmldom/xmldom": "^0.8.10",
47
+ "esbuild": "^0.19.0",
48
+ "eslint": "^8.46.0",
49
49
  "eslint-plugin-compat": "^4.1.4",
50
- "eslint-plugin-jsdoc": "^44.2.5",
51
- "jest": "^29.5.0",
50
+ "eslint-plugin-jsdoc": "^46.4.6",
51
+ "jest": "^29.6.2",
52
52
  "rimraf": "^5.0.1"
53
53
  },
54
54
  "dependencies": {
55
55
  "@d1g1tal/chrysalis": "^1.2.3",
56
- "@d1g1tal/collections": "^0.0.4",
56
+ "@d1g1tal/collections": "^0.2.3",
57
57
  "@d1g1tal/media-type": "^4.1.0",
58
58
  "@d1g1tal/subscribr": "^3.0.1"
59
59
  },
package/src/transportr.js CHANGED
@@ -378,7 +378,7 @@ export default class Transportr {
378
378
  referrer: 'about:client',
379
379
  referrerPolicy: Transportr.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
380
380
  signal: undefined,
381
- timeout: 10000,
381
+ timeout: 30000,
382
382
  global: true,
383
383
  window: null
384
384
  });
@@ -429,8 +429,8 @@ export default class Transportr {
429
429
  * the specified options, where the method is GET.
430
430
  *
431
431
  * @async
432
- * @param {string} path - The path to the resource you want to get.
433
- * @param {RequestOptions} [options] - The options for the request.
432
+ * @param {string} [path] The path to the resource you want to get.
433
+ * @param {RequestOptions} [options] The options for the request.
434
434
  * @returns {Promise<ResponseBody>} A promise that resolves to the response of the request.
435
435
  */
436
436
  async get(path, options) {
@@ -441,9 +441,9 @@ export default class Transportr {
441
441
  * This function makes a POST request to the given path with the given body and options.
442
442
  *
443
443
  * @async
444
- * @param {string} path - The path to the endpoint you want to call.
445
- * @param {RequestBody} body - The body of the request.
446
- * @param {RequestOptions} [options] - The options for the request.
444
+ * @param {string} [path] The path to the endpoint you want to call.
445
+ * @param {RequestBody} body The body of the request.
446
+ * @param {RequestOptions} [options] The options for the request.
447
447
  * @returns {Promise<ResponseBody>} A promise that resolves to the response body.
448
448
  */
449
449
  async post(path, body, options) {
@@ -455,8 +455,8 @@ export default class Transportr {
455
455
  * the specified options, where the method is PUT.
456
456
  *
457
457
  * @async
458
- * @param {string} path - The path to the endpoint you want to call.
459
- * @param {RequestOptions} [options] - The options for the request.
458
+ * @param {string} [path] The path to the endpoint you want to call.
459
+ * @param {RequestOptions} [options] The options for the request.
460
460
  * @returns {Promise<ResponseBody>} The return value of the #request method.
461
461
  */
462
462
  async put(path, options) {
@@ -467,8 +467,8 @@ export default class Transportr {
467
467
  * It takes a path and options, and returns a request with the method set to PATCH.
468
468
  *
469
469
  * @async
470
- * @param {string} path - The path to the endpoint you want to hit.
471
- * @param {RequestOptions} [options] - The options for the request.
470
+ * @param {string} [path] The path to the endpoint you want to hit.
471
+ * @param {RequestOptions} [options] The options for the request.
472
472
  * @returns {Promise<ResponseBody>} A promise that resolves to the response of the request.
473
473
  */
474
474
  async patch(path, options) {
@@ -479,8 +479,8 @@ export default class Transportr {
479
479
  * It takes a path and options, and returns a request with the method set to DELETE.
480
480
  *
481
481
  * @async
482
- * @param {string} path - The path to the resource you want to access.
483
- * @param {RequestOptions} [options] - The options for the request.
482
+ * @param {string} [path] The path to the resource you want to access.
483
+ * @param {RequestOptions} [options] The options for the request.
484
484
  * @returns {Promise<ResponseBody>} The result of the request.
485
485
  */
486
486
  async delete(path, options) {
@@ -491,8 +491,8 @@ export default class Transportr {
491
491
  * Returns the response headers of a request to the given path.
492
492
  *
493
493
  * @async
494
- * @param {string} path - The path to the resource you want to access.
495
- * @param {RequestOptions} [options] - The options for the request.
494
+ * @param {string} [path] The path to the resource you want to access.
495
+ * @param {RequestOptions} [options] The options for the request.
496
496
  * @returns {Promise<ResponseBody>} A promise that resolves to the response object.
497
497
  */
498
498
  async head(path, options) {
@@ -503,8 +503,8 @@ export default class Transportr {
503
503
  * It takes a path and options, and returns a request with the method set to OPTIONS.
504
504
  *
505
505
  * @async
506
- * @param {string} path - The path to the resource.
507
- * @param {RequestOptions} [options] - The options for the request.
506
+ * @param {string} [path] The path to the resource.
507
+ * @param {RequestOptions} [options] The options for the request.
508
508
  * @returns {Promise<ResponseBody>} The return value of the #request method.
509
509
  */
510
510
  async options(path, options) {
@@ -515,8 +515,8 @@ export default class Transportr {
515
515
  * It takes a path and options, and makes a request to the server.
516
516
  *
517
517
  * @async
518
- * @param {string} path - The path to the endpoint you want to hit.
519
- * @param {RequestOptions} [options] - The options for the request.
518
+ * @param {string} [path] The path to the endpoint you want to hit.
519
+ * @param {RequestOptions} [options] The options for the request.
520
520
  * @returns {Promise<ResponseBody>} The return value of the function is the return value of the function that is passed to the `then` method of the promise returned by the `fetch` method.
521
521
  */
522
522
  async request(path, options) {
@@ -527,8 +527,8 @@ export default class Transportr {
527
527
  * It gets a JSON resource from the server.
528
528
  *
529
529
  * @async
530
- * @param {string} path - The path to the resource.
531
- * @param {RequestOptions} [options] - The options object to pass to the request.
530
+ * @param {string} [path] The path to the resource.
531
+ * @param {RequestOptions} [options] The options object to pass to the request.
532
532
  * @returns {Promise<JsonObject>} A promise that resolves to the response body as a JSON object.
533
533
  */
534
534
  async getJson(path, options) {
@@ -539,8 +539,8 @@ export default class Transportr {
539
539
  * It gets the XML representation of the resource at the given path.
540
540
  *
541
541
  * @async
542
- * @param {string} path - The path to the resource you want to get.
543
- * @param {RequestOptions} [options] - The options for the request.
542
+ * @param {string} [path] The path to the resource you want to get.
543
+ * @param {RequestOptions} [options] The options for the request.
544
544
  * @returns {Promise<Document>} The result of the function call to #get.
545
545
  */
546
546
  async getXml(path, options) {
@@ -552,8 +552,8 @@ export default class Transportr {
552
552
  *
553
553
  * @todo Add way to return portion of the retrieved HTML using a selector. Like jQuery.
554
554
  * @async
555
- * @param {string} path - The path to the resource.
556
- * @param {RequestOptions} [options] - The options for the request.
555
+ * @param {string} [path] The path to the resource.
556
+ * @param {RequestOptions} [options] The options for the request.
557
557
  * @returns {Promise<Document>} The return value of the function is the return value of the function passed to the `then`
558
558
  * method of the promise returned by the `#get` method.
559
559
  */
@@ -564,10 +564,10 @@ export default class Transportr {
564
564
  /**
565
565
  * It returns a promise that resolves to the HTML fragment at the given path.
566
566
  *
567
- * @todo - Add way to return portion of the retrieved HTML using a selector. Like jQuery.
567
+ * @todo Add way to return portion of the retrieved HTML using a selector. Like jQuery.
568
568
  * @async
569
- * @param {string} path - The path to the resource.
570
- * @param {RequestOptions} [options] - The options for the request.
569
+ * @param {string} [path] The path to the resource.
570
+ * @param {RequestOptions} [options] The options for the request.
571
571
  * @returns {Promise<DocumentFragment>} A promise that resolves to an HTML fragment.
572
572
  */
573
573
  async getHtmlFragment(path, options) {
@@ -579,8 +579,8 @@ export default class Transportr {
579
579
  * CORS is enabled by default.
580
580
  *
581
581
  * @async
582
- * @param {string} path - The path to the script.
583
- * @param {RequestOptions} [options] - The options for the request.
582
+ * @param {string} [path] The path to the script.
583
+ * @param {RequestOptions} [options] The options for the request.
584
584
  * @returns {Promise<void>} A promise that has been resolved.
585
585
  */
586
586
  async getScript(path, options) {
@@ -591,8 +591,8 @@ export default class Transportr {
591
591
  * Gets a stylesheet from the server, and adds it as a {@link Blob} {@link URL}.
592
592
  *
593
593
  * @async
594
- * @param {string} path - The path to the stylesheet.
595
- * @param {RequestOptions} [options] - The options for the request.
594
+ * @param {string} [path] The path to the stylesheet.
595
+ * @param {RequestOptions} [options] The options for the request.
596
596
  * @returns {Promise<void>} A promise that has been resolved.
597
597
  */
598
598
  async getStylesheet(path, options) {
@@ -603,8 +603,8 @@ export default class Transportr {
603
603
  * It returns a blob from the specified path.
604
604
  *
605
605
  * @async
606
- * @param {string} path - The path to the resource.
607
- * @param {RequestOptions} [options] - The options for the request.
606
+ * @param {string} [path] The path to the resource.
607
+ * @param {RequestOptions} [options] The options for the request.
608
608
  * @returns {Promise<Blob>} A promise that resolves to a blob.
609
609
  */
610
610
  async getBlob(path, options) {
@@ -615,8 +615,8 @@ export default class Transportr {
615
615
  * It returns a promise that resolves to an object URL.
616
616
  *
617
617
  * @async
618
- * @param {string} path - The path to the resource.
619
- * @param {RequestOptions} [options] - The options for the request.
618
+ * @param {string|RequestOptions} [path] The path to the resource.
619
+ * @param {RequestOptions} [options] The options for the request.
620
620
  * @returns {Promise<string>} A promise that resolves to an object URL.
621
621
  */
622
622
  async getImage(path, options) {
@@ -627,8 +627,8 @@ export default class Transportr {
627
627
  * It gets a buffer from the specified path
628
628
  *
629
629
  * @async
630
- * @param {string} path - The path to the resource.
631
- * @param {RequestOptions} [options] - The options for the request.
630
+ * @param {string} [path] The path to the resource.
631
+ * @param {RequestOptions} [options] The options for the request.
632
632
  * @returns {Promise<ArrayBuffer>} A promise that resolves to a buffer.
633
633
  */
634
634
  async getBuffer(path, options) {
@@ -639,8 +639,8 @@ export default class Transportr {
639
639
  * It returns a readable stream of the response body from the specified path.
640
640
  *
641
641
  * @async
642
- * @param {string} path - The path to the resource.
643
- * @param {RequestOptions} [options] - The options for the request.
642
+ * @param {string} [path] The path to the resource.
643
+ * @param {RequestOptions} [options] The options for the request.
644
644
  * @returns {Promise<ReadableStream<Uint8Array>>} A readable stream.
645
645
  */
646
646
  async getStream(path, options) {
@@ -653,13 +653,15 @@ export default class Transportr {
653
653
  *
654
654
  * @private
655
655
  * @async
656
- * @param {string} path - The path to the endpoint you want to call.
657
- * @param {RequestOptions} [userOptions] - The options passed to the public function to use for the request.
658
- * @param {RequestOptions} [options] - The options for the request.
659
- * @param {ResponseHandler<ResponseBody>} [responseHandler] - A function that will be called with the response object.
656
+ * @param {string} [path] The path to the endpoint you want to call.
657
+ * @param {RequestOptions} [userOptions] The options passed to the public function to use for the request.
658
+ * @param {RequestOptions} [options] The options for the request.
659
+ * @param {ResponseHandler<ResponseBody>} [responseHandler] A function that will be called with the response object.
660
660
  * @returns {Promise<ResponseBody>} The result of the #request method.
661
661
  */
662
662
  async #get(path, userOptions, options, responseHandler) {
663
+ delete userOptions?.method;
664
+
663
665
  return this.#request(path, userOptions, options, responseHandler);
664
666
  }
665
667
 
@@ -669,14 +671,19 @@ export default class Transportr {
669
671
  *
670
672
  * @private
671
673
  * @async
672
- * @param {string} path - The path to the resource you want to access.
673
- * @param {RequestOptions} [userOptions={}] - The options passed to the public function to use for the request.
674
- * @param {RequestOptions} [options={}] - The options to use for the request.
675
- * @param {ResponseHandler<ResponseBody>} [responseHandler] - A function that will be called with the response body as a parameter. This
674
+ * @param {string} [path] The path to the resource you want to access.
675
+ * @param {RequestOptions} [userOptions={}] The options passed to the public function to use for the request.
676
+ * @param {RequestOptions} [options={}] The options to use for the request.
677
+ * @param {ResponseHandler<ResponseBody>} [responseHandler] A function that will be called with the response body as a parameter. This
676
678
  * is useful if you want to do something with the response body before returning it.
677
679
  * @returns {Promise<ResponseBody>} The response from the API call.
678
680
  */
679
681
  async #request(path, userOptions = {}, options = {}, responseHandler) {
682
+ if (_type(path) == Object) {
683
+ userOptions = path;
684
+ path = undefined;
685
+ }
686
+
680
687
  const requestOptions = _objectMerge(this.#options, Transportr.#convertRequestOptions(userOptions), options);
681
688
  const url = Transportr.#createUrl(this.#baseUrl, path, requestOptions.searchParams);
682
689
  const signalController = new SignalController(requestOptions.signal);
@@ -796,9 +803,9 @@ export default class Transportr {
796
803
  * @private
797
804
  * @static
798
805
  * @async
799
- * @param {Response} response - The response object returned by the fetch API.
800
- * @param {URL} url - The path to the resource you want to access. Used for error handling.
801
- * @param {ResponseHandler<ResponseBody>} [handler] - The handler to use for processing the response.
806
+ * @param {Response} response The response object returned by the fetch API.
807
+ * @param {URL} url The path to the resource you want to access. Used for error handling.
808
+ * @param {ResponseHandler<ResponseBody>} [handler] The handler to use for processing the response.
802
809
  * @returns {Promise<ResponseBody>} The response is being returned.
803
810
  */
804
811
  async #processResponse(response, url, handler) {
@@ -830,21 +837,25 @@ export default class Transportr {
830
837
  *
831
838
  * @private
832
839
  * @static
833
- * @param {URL} url - The URL to use as a base.
834
- * @param {string} path - The path to the resource. This can be a relative path or a full URL.
835
- * @param {Object<string, string>} [searchParams={}] - An object containing the query parameters to be added to the URL.
840
+ * @param {URL} url The URL to use as a base.
841
+ * @param {string} [path] The path to the resource. This can be a relative path or a full URL.
842
+ * @param {Object<string, string>} [searchParams={}] An object containing the query parameters to be added to the URL.
836
843
  * @returns {URL} A new URL object with the pathname and origin of the url parameter, and the path parameter
837
844
  * appended to the end of the pathname.
838
845
  */
839
846
  static #createUrl(url, path, searchParams = {}) {
847
+ let _url;
840
848
  if (path) {
841
849
  // Create the object URL with a relative or absolute path
842
- url = path.startsWith('/') ? new URL(`${url.pathname.replace(endsWithSlashRegEx, '')}${path}`, url.origin) : new URL(path);
850
+ _url = path.startsWith('/') ? new URL(`${url.pathname.replace(endsWithSlashRegEx, '')}${path}`, url.origin) : new URL(path);
851
+ } else {
852
+ // Create a new URL object from the existing URL
853
+ _url = new URL(url);
843
854
  }
844
855
 
845
- Object.entries(searchParams).forEach(([key, value]) => url.searchParams.append(key, value));
856
+ Object.entries(searchParams).forEach(([key, value]) => _url.searchParams.append(key, value));
846
857
 
847
- return url;
858
+ return _url;
848
859
  }
849
860
 
850
861
  /**
@@ -853,8 +864,8 @@ export default class Transportr {
853
864
  *
854
865
  * @private
855
866
  * @static
856
- * @param {RequestMethod} method - The HTTP request method.
857
- * @param {HttpMediaType} contentType - The headers of the request.
867
+ * @param {RequestMethod} method The HTTP request method.
868
+ * @param {HttpMediaType} contentType The headers of the request.
858
869
  * @returns {boolean} `true` if the request body needs to be serialized, `false` otherwise.
859
870
  */
860
871
  static #needsSerialization(method, contentType) {
@@ -863,7 +874,7 @@ export default class Transportr {
863
874
 
864
875
  /**
865
876
  *
866
- * @param {RequestOptions} options - The options passed to the public function to use for the request.
877
+ * @param {RequestOptions} options The options passed to the public function to use for the request.
867
878
  * @returns {RequestOptions} The options to use for the request.
868
879
  */
869
880
  static #convertRequestOptions(options) {