@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/LICENSE +9 -18
- package/dist/iife/transportr.js +2656 -0
- package/dist/iife/transportr.min.js +3 -0
- package/dist/iife/transportr.min.js.map +7 -0
- package/dist/transportr.js +2635 -2812
- package/dist/transportr.min.js +2 -3
- package/dist/transportr.min.js.map +4 -4
- package/package.json +69 -77
- package/src/abort-signal.js +5 -3
- package/src/constants.js +1 -1
- package/src/transportr.js +19 -22
- package/index.js +0 -1
package/package.json
CHANGED
|
@@ -1,77 +1,69 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@d1g1tal/transportr",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "JavaScript wrapper for the Fetch API",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
".":
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
],
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
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
|
+
}
|
package/src/abort-signal.js
CHANGED
|
@@ -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
|
|
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 {
|
|
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
|
|
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
|
|
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 {
|
|
136
|
-
static #contentTypeHandlers = new
|
|
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)
|
|
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
|
-
|
|
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)
|
|
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)
|
|
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)
|
|
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)
|
|
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)
|
|
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)
|
|
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
|
|
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,
|
|
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,
|
|
822
|
-
if (
|
|
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
|
|
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';
|