@d1g1tal/transportr 1.4.0 → 1.4.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,77 +1,69 @@
1
- {
2
- "name": "@d1g1tal/transportr",
3
- "version": "1.4.0",
4
- "description": "JavaScript wrapper for the Fetch API",
5
- "type": "module",
6
- "exports": {
7
- ".": {
8
- "import": "./src/transportr.js",
9
- "default": "./dist/transportr.min.js"
10
- }
11
- },
12
- "browser": {
13
- "transportr.js": "./dist/transportr.js",
14
- "transportr.min.js": "./dist/transportr.min.js",
15
- "transportr.min.js.map": "./dist/transportr.min.js.map"
16
- },
17
- "publishConfig": {
18
- "access": "public"
19
- },
20
- "files": [
21
- "/src",
22
- "/dist",
23
- "index.js"
24
- ],
25
- "scripts": {
26
- "build": "rimraf dist && esbuild src/transportr.js --bundle --platform=browser --global-name=Transportr --footer:js='window.Transportr = Transportr.default;' --outfile=dist/transportr.js && esbuild src/transportr.js --bundle --sourcemap --minify --platform=browser --global-name=Transportr --footer:js='window.Transportr = Transportr.default;' --outfile=dist/transportr.min.js",
27
- "lint": "eslint --ext .js --ignore-path .gitignore .",
28
- "test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js"
29
- },
30
- "repository": {
31
- "type": "git",
32
- "url": "git+https://github.com/D1g1talEntr0py/transportr.git"
33
- },
34
- "keywords": [
35
- "Fetch",
36
- "JavaScript"
37
- ],
38
- "author": "Jason DiMeo",
39
- "license": "MIT",
40
- "bugs": {
41
- "url": "https://github.com/D1g1talEntr0py/transportr/issues"
42
- },
43
- "homepage": "https://github.com/D1g1talEntr0py/transportr#readme",
44
- "devDependencies": {
45
- "@skypack/package-check": "^0.2.2",
46
- "@xmldom/xmldom": "^0.8.10",
47
- "esbuild": "^0.19.3",
48
- "eslint": "^8.50.0",
49
- "eslint-plugin-compat": "^4.2.0",
50
- "eslint-plugin-jsdoc": "^46.8.2",
51
- "jest": "^29.7.0",
52
- "rimraf": "^5.0.4"
53
- },
54
- "dependencies": {
55
- "@d1g1tal/chrysalis": "^1.2.3",
56
- "@d1g1tal/collections": "^0.2.3",
57
- "@d1g1tal/media-type": "^4.1.0",
58
- "@d1g1tal/subscribr": "^3.0.1"
59
- },
60
- "browserslist": [
61
- "defaults",
62
- "not ios_saf < 15",
63
- "not op_mini all"
64
- ],
65
- "jest": {
66
- "verbose": true,
67
- "clearMocks": true,
68
- "coverageDirectory": "./tests/coverage/",
69
- "coveragePathIgnorePatterns": [
70
- "/node_modules/"
71
- ],
72
- "collectCoverage": true,
73
- "collectCoverageFrom": [
74
- "src/**/*.js"
75
- ]
76
- }
77
- }
1
+ {
2
+ "name": "@d1g1tal/transportr",
3
+ "version": "1.4.2",
4
+ "description": "JavaScript wrapper for the Fetch API",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./src/transportr.js",
8
+ "./dist/*": "./dist/*"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "files": [
14
+ "/dist",
15
+ "/src"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/D1g1talEntr0py/transportr.git"
20
+ },
21
+ "keywords": [
22
+ "Fetch",
23
+ "JavaScript"
24
+ ],
25
+ "author": "Jason DiMeo",
26
+ "license": "ISC",
27
+ "bugs": {
28
+ "url": "https://github.com/D1g1talEntr0py/transportr/issues"
29
+ },
30
+ "homepage": "https://github.com/D1g1talEntr0py/transportr#readme",
31
+ "packageManager": "pnpm@8.10.2+sha256.487609e857f1c11780cc98dd0bfe4c8a8b11c7f23bc3a4493ac7d263d6fb6c8c",
32
+ "dependencies": {
33
+ "@d1g1tal/chrysalis": "^2.2.0",
34
+ "@d1g1tal/media-type": "^5.0.0",
35
+ "@d1g1tal/subscribr": "^3.0.2"
36
+ },
37
+ "devDependencies": {
38
+ "@xmldom/xmldom": "^0.8.10",
39
+ "esbuild": "^0.19.5",
40
+ "esbuild-library": "^1.0.2",
41
+ "eslint": "^8.53.0",
42
+ "eslint-plugin-compat": "^4.2.0",
43
+ "eslint-plugin-jsdoc": "^46.9.0",
44
+ "jest": "^29.7.0"
45
+ },
46
+ "browserslist": [
47
+ "defaults",
48
+ "not ios_saf < 15",
49
+ "not op_mini all"
50
+ ],
51
+ "jest": {
52
+ "verbose": true,
53
+ "clearMocks": true,
54
+ "collectCoverage": true,
55
+ "coverageDirectory": "./tests/coverage",
56
+ "collectCoverageFrom": [
57
+ "src/**/*.js"
58
+ ],
59
+ "coveragePathIgnorePatterns": [
60
+ "/node_modules/"
61
+ ]
62
+ },
63
+ "scripts": {
64
+ "build": "node ./esbuild.js",
65
+ "lint": "eslint --ext .js --ignore-path .gitignore .",
66
+ "test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js",
67
+ "check": "npx package-check"
68
+ }
69
+ }
@@ -1,21 +1,23 @@
1
1
  import { SignalEvents, abortEvent } from './constants.js';
2
2
 
3
+ /** @typedef {globalThis.AbortSignal} NativeAbortSignal */
4
+
3
5
  /**
4
6
  * @typedef {function(Event):void} EventListener
5
7
  * @param {Event} event The event object
6
8
  */
7
9
 
8
- export default class AbortSignal extends EventTarget {
10
+ export default class AbortSignal {
9
11
  /** @type {AbortController} */
10
12
  #abortController;
11
13
  /** @type {number} */
12
14
  #timeoutId;
13
15
 
14
16
  /**
15
- * @param {AbortSignal} signal The signal to listen to
17
+ * @param {NativeAbortSignal} signal The signal to chain.
18
+ * This signal will be able to abort the request, but will not be notified if the request is aborted by the timeout.
16
19
  */
17
20
  constructor(signal) {
18
- super();
19
21
  this.#abortController = new AbortController();
20
22
  signal?.addEventListener(SignalEvents.ABORT, (event) => this.#abort(event));
21
23
  }
package/src/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import HttpRequestMethod from './http-request-methods.js';
2
2
  import ResponseStatus from './response-status.js';
3
- import { MediaType } from '@d1g1tal/media-type';
3
+ import MediaType from '@d1g1tal/media-type';
4
4
  import HttpMediaType from './http-media-type.js';
5
5
 
6
6
  /** @typedef {'configured'|'success'|'error'|'aborted'|'timeout'|'complete'} RequestEvent */
package/src/transportr.js CHANGED
@@ -1,4 +1,3 @@
1
- import SetMultiMap from '@d1g1tal/collections/set-multi-map.js';
2
1
  import Subscribr from '@d1g1tal/subscribr';
3
2
  import AbortSignal from './abort-signal.js';
4
3
  import HttpError from './http-error.js';
@@ -8,7 +7,7 @@ import HttpRequestMethod from './http-request-methods.js';
8
7
  import HttpResponseHeader from './http-response-headers.js';
9
8
  import ParameterMap from './parameter-map.js';
10
9
  import ResponseStatus from './response-status.js';
11
- import { MediaType } from '@d1g1tal/media-type';
10
+ import MediaType from '@d1g1tal/media-type';
12
11
  import { _objectMerge, _type } from '@d1g1tal/chrysalis';
13
12
  import { RequestEvents, abortEvent, abortSignalProxyHandler, endsWithSlashRegEx, eventResponseStatuses, internalServerError, mediaTypes, requestBodyMethods } from './constants.js';
14
13
 
@@ -132,8 +131,8 @@ export default class Transportr {
132
131
  static #globalSubscribr = new Subscribr();
133
132
  /** @type {Set<AbortSignal>} */
134
133
  static #activeRequests = new Set();
135
- /** @type {SetMultiMap<ResponseHandler<ResponseBody>, string>} */
136
- static #contentTypeHandlers = new SetMultiMap([
134
+ /** @type {Map<ResponseHandler<ResponseBody>, string>} */
135
+ static #contentTypeHandlers = new Map([
137
136
  [_handleImage, mediaTypes.get(HttpMediaType.PNG).type],
138
137
  [_handleText, mediaTypes.get(HttpMediaType.TEXT).type],
139
138
  [_handleJson, mediaTypes.get(HttpMediaType.JSON).subtype],
@@ -257,7 +256,7 @@ export default class Transportr {
257
256
  body: null,
258
257
  cache: Transportr.CachingPolicy.NO_STORE,
259
258
  credentials: Transportr.CredentialsPolicy.SAME_ORIGIN,
260
- headers: { [HttpRequestHeader.CONTENT_TYPE]: mediaTypes.get(HttpMediaType.JSON).toString(), [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.JSON).toString() },
259
+ headers: { [HttpRequestHeader.CONTENT_TYPE]: `${mediaTypes.get(HttpMediaType.JSON)}`, [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.JSON)}` },
261
260
  searchParams: {},
262
261
  integrity: undefined,
263
262
  keepalive: undefined,
@@ -366,8 +365,10 @@ export default class Transportr {
366
365
  * @param {RequestOptions} [options] The options for the request.
367
366
  * @returns {Promise<ResponseBody>} A promise that resolves to the response body.
368
367
  */
369
- async post(path, body, options) {
370
- return this.#request(path, { ...options, body }, { method: HttpRequestMethod.POST });
368
+ async post(path, body = {}, options = {}) {
369
+ if (_type(path) != String) { [ path, body, options ] = [ undefined, path, body ] }
370
+
371
+ return this.#request(path, Object.assign(options, { body }), { method: HttpRequestMethod.POST });
371
372
  }
372
373
 
373
374
  /**
@@ -454,7 +455,7 @@ export default class Transportr {
454
455
  * @returns {Promise<JsonObject>} A promise that resolves to the response body as a JSON object.
455
456
  */
456
457
  async getJson(path, options) {
457
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.JSON).toString() } }, _handleJson);
458
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.JSON)}` } }, _handleJson);
458
459
  }
459
460
 
460
461
  /**
@@ -466,7 +467,7 @@ export default class Transportr {
466
467
  * @returns {Promise<Document>} The result of the function call to #get.
467
468
  */
468
469
  async getXml(path, options) {
469
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.XML).toString() } }, _handleXml);
470
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.XML)}` } }, _handleXml);
470
471
  }
471
472
 
472
473
  /**
@@ -480,7 +481,7 @@ export default class Transportr {
480
481
  * method of the promise returned by the `#get` method.
481
482
  */
482
483
  async getHtml(path, options) {
483
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.HTML).toString() } }, _handleHtml);
484
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.HTML)}` } }, _handleHtml);
484
485
  }
485
486
 
486
487
  /**
@@ -493,7 +494,7 @@ export default class Transportr {
493
494
  * @returns {Promise<DocumentFragment>} A promise that resolves to an HTML fragment.
494
495
  */
495
496
  async getHtmlFragment(path, options) {
496
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.HTML).toString() } }, _handleHtmlFragment);
497
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.HTML)}` } }, _handleHtmlFragment);
497
498
  }
498
499
 
499
500
  /**
@@ -506,7 +507,7 @@ export default class Transportr {
506
507
  * @returns {Promise<void>} A promise that has been resolved.
507
508
  */
508
509
  async getScript(path, options) {
509
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.JAVA_SCRIPT).toString() } }, _handleScript);
510
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.JAVA_SCRIPT)}` } }, _handleScript);
510
511
  }
511
512
 
512
513
  /**
@@ -518,7 +519,7 @@ export default class Transportr {
518
519
  * @returns {Promise<void>} A promise that has been resolved.
519
520
  */
520
521
  async getStylesheet(path, options) {
521
- return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: mediaTypes.get(HttpMediaType.CSS).toString() } }, _handleCss);
522
+ return this.#get(path, options, { headers: { [HttpRequestHeader.ACCEPT]: `${mediaTypes.get(HttpMediaType.CSS)}` } }, _handleCss);
522
523
  }
523
524
 
524
525
  /**
@@ -632,7 +633,7 @@ export default class Transportr {
632
633
 
633
634
  Transportr.#activeRequests.delete(options.signal);
634
635
 
635
- if (Transportr.#activeRequests.size === 0) {
636
+ if (Transportr.#activeRequests.size == 0) {
636
637
  this.#publish({ name: RequestEvents.ALL_COMPLETE, global: options.global });
637
638
  }
638
639
  }
@@ -753,7 +754,7 @@ export default class Transportr {
753
754
  static #createUrl(url, path, searchParams) {
754
755
  const requestUrl = path ? new URL(`${url.pathname.replace(endsWithSlashRegEx, '')}${path}`, url.origin) : new URL(url);
755
756
 
756
- searchParams?.forEach((value, key) => requestUrl.searchParams.append(key, value));
757
+ searchParams?.forEach((value, name) => requestUrl.searchParams.append(name, value));
757
758
 
758
759
  return requestUrl;
759
760
  }
@@ -814,19 +815,15 @@ export default class Transportr {
814
815
  * @returns {ResponseHandler<ResponseBody>} The response handler.
815
816
  */
816
817
  #getResponseHandler(contentType) {
817
- let handler;
818
818
  const mediaType = MediaType.parse(contentType);
819
819
 
820
820
  if (mediaType) {
821
- for (const [responseHandler, contentTypes] of Transportr.#contentTypeHandlers) {
822
- if (contentTypes.has(mediaType.type) || contentTypes.has(mediaType.subtype)) {
823
- handler = responseHandler;
824
- break;
825
- }
821
+ for (const [responseHandler, contentType] of Transportr.#contentTypeHandlers) {
822
+ if (mediaType.matches(contentType)) { return responseHandler }
826
823
  }
827
824
  }
828
825
 
829
- return handler;
826
+ return undefined;
830
827
  }
831
828
 
832
829
  /**
package/index.js DELETED
@@ -1 +0,0 @@
1
- export { default as Transportr } from './src/transportr.js';