@empathyco/x-adapter 8.0.0-alpha.4 → 8.0.0-alpha.7
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/dist/cjs/http-clients/__mocks__/fetch.mock.js +6 -1
- package/dist/cjs/http-clients/__mocks__/fetch.mock.js.map +1 -1
- package/dist/cjs/http-clients/fetch.http-client.js +3 -3
- package/dist/cjs/http-clients/fetch.http-client.js.map +1 -1
- package/dist/cjs/http-clients/index.js +0 -1
- package/dist/cjs/http-clients/index.js.map +1 -1
- package/dist/cjs/http-clients/types.js.map +1 -1
- package/dist/cjs/http-clients/utils.js +1 -1
- package/dist/cjs/http-clients/utils.js.map +1 -1
- package/dist/esm/http-clients/__mocks__/fetch.mock.js +6 -1
- package/dist/esm/http-clients/__mocks__/fetch.mock.js.map +1 -1
- package/dist/esm/http-clients/fetch.http-client.js +3 -3
- package/dist/esm/http-clients/fetch.http-client.js.map +1 -1
- package/dist/esm/http-clients/index.js +0 -1
- package/dist/esm/http-clients/index.js.map +1 -1
- package/dist/esm/http-clients/types.js.map +1 -1
- package/dist/esm/http-clients/utils.js +1 -1
- package/dist/esm/http-clients/utils.js.map +1 -1
- package/dist/types/http-clients/__mocks__/fetch.mock.d.ts +2 -2
- package/dist/types/http-clients/index.d.ts +0 -1
- package/dist/types/http-clients/types.d.ts +4 -0
- package/package.json +4 -4
- package/dist/cjs/http-clients/beacon.http-client.js +0 -61
- package/dist/cjs/http-clients/beacon.http-client.js.map +0 -1
- package/dist/esm/http-clients/beacon.http-client.js +0 -57
- package/dist/esm/http-clients/beacon.http-client.js.map +0 -1
- package/dist/types/http-clients/beacon.http-client.d.ts +0 -14
|
@@ -24,7 +24,12 @@ function fetchMock(response) {
|
|
|
24
24
|
reject(new DOMException('Aborted', 'AbortError'));
|
|
25
25
|
}
|
|
26
26
|
else {
|
|
27
|
-
resolve({
|
|
27
|
+
resolve({
|
|
28
|
+
ok: true,
|
|
29
|
+
status: 200,
|
|
30
|
+
json: () => Promise.resolve(response),
|
|
31
|
+
text: () => Promise.resolve(JSON.stringify(response))
|
|
32
|
+
});
|
|
28
33
|
}
|
|
29
34
|
});
|
|
30
35
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AAErC,QAAA,WAAW,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CACtC,OAAO,CAAC,OAAO,CAAC;IACd,EAAE,EAAE,KAAK;IACT,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IAC/B,MAAM,EAAE,GAAG;IACX,UAAU,EAAE,kBAAkB;CAC/B,CAAC,CACH,CAAC;AACF;;;;;;;GAOG;AACH,SAAgB,SAAS,CACvB,QAAiB;IAEjB,OAAO,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QAC1B,OAAO,IAAI,OAAO,CAAW,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC/C,UAAU,CAAC,GAAG,EAAE;gBACd,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EAAE;oBACnB,MAAM,CAAC,IAAI,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC;iBACnD;qBAAM;oBACL,OAAO,CAAC;wBACN,EAAE,EAAE,IAAI;wBACR,MAAM,EAAE,GAAG;wBACX,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;wBACrC,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;qBAC1C,CAAC,CAAC;iBAChB;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAnBD,8BAmBC","sourcesContent":["export const okFetchMock = jest.fn(fetchMock({}));\n\nexport const koFetchMock = jest.fn(() =>\n Promise.resolve({\n ok: false,\n json: () => Promise.resolve({}),\n status: 500,\n statusText: 'Unexpected error'\n })\n);\n/**\n * The `fetchMock()` method mocks a `fetch` API call.\n *\n * @param response - The expected response resolved by calling `fetch()`.\n * @returns A Promise object.\n *\n * @internal\n */\nexport function fetchMock(\n response: unknown\n): (url: string, params: RequestInit) => Promise<Response> {\n return (_url, { signal }) => {\n return new Promise<Response>((resolve, reject) => {\n setTimeout(() => {\n if (signal?.aborted) {\n reject(new DOMException('Aborted', 'AbortError'));\n } else {\n resolve({\n ok: true,\n status: 200,\n json: () => Promise.resolve(response),\n text: () => Promise.resolve(JSON.stringify(response))\n } as Response);\n }\n });\n });\n };\n}\n"]}
|
|
@@ -13,15 +13,15 @@ const utils_1 = require("./utils");
|
|
|
13
13
|
*
|
|
14
14
|
* @public
|
|
15
15
|
*/
|
|
16
|
-
const fetchHttpClient = (endpoint, { id = endpoint, parameters = {}, properties, sendParamsInBody = false } = {}) => {
|
|
17
|
-
const signal = abortAndGetNewAbortSignal(id);
|
|
16
|
+
const fetchHttpClient = (endpoint, { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false } = {}) => {
|
|
17
|
+
const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
18
18
|
const flatParameters = (0, x_utils_1.flatObject)(parameters);
|
|
19
19
|
const url = sendParamsInBody ? endpoint : (0, utils_1.buildUrl)(endpoint, flatParameters);
|
|
20
20
|
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(parameters) } : {};
|
|
21
21
|
return fetch(url, {
|
|
22
22
|
...properties,
|
|
23
23
|
...bodyParameters,
|
|
24
|
-
signal
|
|
24
|
+
...signal
|
|
25
25
|
}).then(utils_1.toJson);
|
|
26
26
|
};
|
|
27
27
|
exports.fetchHttpClient = fetchHttpClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":";;;AAAA,gDAA4D;AAE5D,mCAA2C;AAE3C;;;;;;;;;GASG;AACI,MAAM,eAAe,GAAe,CACzC,QAAQ,EACR,EAAE,EAAE,GAAG,QAAQ,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":";;;AAAA,gDAA4D;AAE5D,mCAA2C;AAE3C;;;;;;;;;GASG;AACI,MAAM,eAAe,GAAe,CACzC,QAAQ,EACR,EAAE,EAAE,GAAG,QAAQ,EAAE,UAAU,GAAG,IAAI,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,EAAE,EAChG,EAAE;IACF,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,yBAAyB,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3E,MAAM,cAAc,GAAG,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAC;IAC9C,MAAM,GAAG,GAAG,gBAAgB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAA,gBAAQ,EAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;IAC7E,MAAM,cAAc,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAEpF,OAAO,KAAK,CAAC,GAAG,EAAE;QAChB,GAAG,UAAU;QACb,GAAG,cAAc;QACjB,GAAG,MAAM;KACV,CAAC,CAAC,IAAI,CAAC,cAAM,CAAC,CAAC;AAClB,CAAC,CAAC;AAdW,QAAA,eAAe,mBAc1B;AAEF;;GAEG;AACH,MAAM,uBAAuB,GAAgC,EAAE,CAAC;AAEhE;;;;;;;GAOG;AACH,SAAS,yBAAyB,CAAC,EAAU;;IAC3C,MAAA,uBAAuB,CAAC,EAAE,CAAC,0CAAE,KAAK,EAAE,CAAC;IACrC,uBAAuB,CAAC,EAAE,CAAC,GAAG,IAAI,eAAe,EAAE,CAAC;IACpD,OAAO,uBAAuB,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC;AAC5C,CAAC","sourcesContent":["import { Dictionary, flatObject } from '@empathyco/x-utils';\nimport { HttpClient } from './types';\nimport { buildUrl, toJson } from './utils';\n\n/**\n * The `fetchHttpClient()` function is a http client implementation using the `fetch` WebAPI.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n *\n * @returns A `Promise` object.\n *\n * @public\n */\nexport const fetchHttpClient: HttpClient = (\n endpoint,\n { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false } = {}\n) => {\n const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};\n const flatParameters = flatObject(parameters);\n const url = sendParamsInBody ? endpoint : buildUrl(endpoint, flatParameters);\n const bodyParameters = sendParamsInBody ? { body: JSON.stringify(parameters) } : {};\n\n return fetch(url, {\n ...properties,\n ...bodyParameters,\n ...signal\n }).then(toJson);\n};\n\n/**\n * Dictionary with the request id as key and an `AbortController` as value.\n */\nconst requestAbortControllers: Dictionary<AbortController> = {};\n\n/**\n * Function that cancels previous request with the same `id` and returns a new `AbortSignal` for\n * the new request.\n *\n * @param id - The identifier of the request to cancel and create a new `AbortSignal`.\n *\n * @returns The new `AbortSignal`.\n */\nfunction abortAndGetNewAbortSignal(id: string): AbortSignal {\n requestAbortControllers[id]?.abort();\n requestAbortControllers[id] = new AbortController();\n return requestAbortControllers[id].signal;\n}\n"]}
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./fetch.http-client"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./beacon.http-client"), exports);
|
|
6
5
|
tslib_1.__exportStar(require("./types"), exports);
|
|
7
6
|
tslib_1.__exportStar(require("./utils"), exports);
|
|
8
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/http-clients/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/http-clients/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,kDAAwB;AACxB,kDAAwB","sourcesContent":["export * from './fetch.http-client';\nexport * from './types';\nexport * from './utils';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/http-clients/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Dictionary } from '@empathyco/x-utils';\n\n/**\n * Makes a request to a backend API using the given parameters.\n *\n * @param endpoint - The endpoint to use.\n * @param options - Additional options to make the request with.\n * @returns A promise wrapped object containing the response.\n * @public\n */\nexport type HttpClient = <Response = unknown>(\n endpoint: string,\n options?: Omit<RequestOptions, 'endpoint'>\n) => Promise<Readonly<Response>>;\n\n/**\n * A record of options to make the request with.\n *\n * @public\n */\nexport interface RequestOptions {\n /**\n * A unique identifier for this request. Can be used to abort requests with same id.\n */\n id?: string;\n /**\n * A flag to send parameters in the body if true or in the url QueryString if false.\n */\n sendParamsInBody?: boolean;\n /**\n * A list of parameters to send to the API.\n */\n parameters?: Dictionary<unknown>;\n /**\n * The RequestInit object to create the request with.\n */\n properties?: RequestInit;\n /**\n * The base endpoint that the request should use.\n */\n endpoint?: string;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/http-clients/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Dictionary } from '@empathyco/x-utils';\n\n/**\n * Makes a request to a backend API using the given parameters.\n *\n * @param endpoint - The endpoint to use.\n * @param options - Additional options to make the request with.\n * @returns A promise wrapped object containing the response.\n * @public\n */\nexport type HttpClient = <Response = unknown>(\n endpoint: string,\n options?: Omit<RequestOptions, 'endpoint'>\n) => Promise<Readonly<Response>>;\n\n/**\n * A record of options to make the request with.\n *\n * @public\n */\nexport interface RequestOptions {\n /**\n * A unique identifier for this request. Can be used to abort requests with same id.\n */\n id?: string;\n /**\n * True if the request can be cancelled.\n */\n cancelable?: boolean;\n /**\n * A flag to send parameters in the body if true or in the url QueryString if false.\n */\n sendParamsInBody?: boolean;\n /**\n * A list of parameters to send to the API.\n */\n parameters?: Dictionary<unknown>;\n /**\n * The RequestInit object to create the request with.\n */\n properties?: RequestInit;\n /**\n * The base endpoint that the request should use.\n */\n endpoint?: string;\n}\n"]}
|
|
@@ -15,7 +15,7 @@ const request_error_1 = require("./errors/request-error");
|
|
|
15
15
|
*/
|
|
16
16
|
function toJson(response) {
|
|
17
17
|
if (response.ok) {
|
|
18
|
-
return response.
|
|
18
|
+
return response.text().then(text => (text ? JSON.parse(text) : {}));
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
21
|
throw new request_error_1.RequestError('Request failed', response);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/http-clients/utils.ts"],"names":[],"mappings":";;;AAAA,gDAAyD;AACzD,0DAAsD;AAEtD;;;;;;;;;GASG;AACH,SAAgB,MAAM,CAAC,QAAkB;IACvC,IAAI,QAAQ,CAAC,EAAE,EAAE;QACf,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/http-clients/utils.ts"],"names":[],"mappings":";;;AAAA,gDAAyD;AACzD,0DAAsD;AAEtD;;;;;;;;;GASG;AACH,SAAgB,MAAM,CAAC,QAAkB;IACvC,IAAI,QAAQ,CAAC,EAAE,EAAE;QACf,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;KACrE;SAAM;QACL,MAAM,IAAI,4BAAY,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC;KACpD;AACH,CAAC;AAND,wBAMC;AAED;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,QAAgB,EAAE,SAA8B,EAAE;IACzE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,IAAA,iBAAO,EAAC,MAAM,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAC7B,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,CAChE,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC,CACrD,CACF,CAAC;IACF,OAAO,GAAG,CAAC,IAAI,CAAC;AAClB,CAAC;AARD,4BAQC","sourcesContent":["import { Dictionary, forEach } from '@empathyco/x-utils';\nimport { RequestError } from './errors/request-error';\n\n/**\n * Formats a response object to JSON.\n *\n * @remarks If the `response.ok` is falsy, a `RequestError` object is thrown.\n *\n * @param response - The response to convert to JSON format.\n * @returns - The resultant promise of formatting the response to JSON.\n *\n * @public\n */\nexport function toJson(response: Response): Promise<any> {\n if (response.ok) {\n return response.text().then(text => (text ? JSON.parse(text) : {}));\n } else {\n throw new RequestError('Request failed', response);\n }\n}\n\n/**\n * Builds a URL object based on the passed endpoint and the request parameters.\n *\n * @param endpoint - The endpoint.\n * @param params - The request parameters.\n *\n * @returns The `href` property of the newly built `URL` object.\n *\n * @public\n */\nexport function buildUrl(endpoint: string, params: Dictionary<unknown> = {}): URL['href'] {\n const url = new URL(endpoint);\n forEach(params, (key, value) =>\n (Array.isArray(value) ? value : [value]).forEach(arrayItemValue =>\n url.searchParams.append(key, String(arrayItemValue))\n )\n );\n return url.href;\n}\n"]}
|
|
@@ -21,7 +21,12 @@ export function fetchMock(response) {
|
|
|
21
21
|
reject(new DOMException('Aborted', 'AbortError'));
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
resolve({
|
|
24
|
+
resolve({
|
|
25
|
+
ok: true,
|
|
26
|
+
status: 200,
|
|
27
|
+
json: () => Promise.resolve(response),
|
|
28
|
+
text: () => Promise.resolve(JSON.stringify(response))
|
|
29
|
+
});
|
|
25
30
|
}
|
|
26
31
|
});
|
|
27
32
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AAElD,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CACtC,OAAO,CAAC,OAAO,CAAC;IACd,EAAE,EAAE,KAAK;IACT,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IAC/B,MAAM,EAAE,GAAG;IACX,UAAU,EAAE,kBAAkB;CAC/B,CAAC,CACH,CAAC;AACF;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CACvB,QAAiB;IAEjB,OAAO,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QAC1B,OAAO,IAAI,OAAO,CAAW,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC/C,UAAU,CAAC,GAAG,EAAE;gBACd,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EAAE;oBACnB,MAAM,CAAC,IAAI,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC;iBACnD;qBAAM;oBACL,OAAO,CAAC;wBACN,EAAE,EAAE,IAAI;wBACR,MAAM,EAAE,GAAG;wBACX,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;wBACrC,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;qBAC1C,CAAC,CAAC;iBAChB;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["export const okFetchMock = jest.fn(fetchMock({}));\n\nexport const koFetchMock = jest.fn(() =>\n Promise.resolve({\n ok: false,\n json: () => Promise.resolve({}),\n status: 500,\n statusText: 'Unexpected error'\n })\n);\n/**\n * The `fetchMock()` method mocks a `fetch` API call.\n *\n * @param response - The expected response resolved by calling `fetch()`.\n * @returns A Promise object.\n *\n * @internal\n */\nexport function fetchMock(\n response: unknown\n): (url: string, params: RequestInit) => Promise<Response> {\n return (_url, { signal }) => {\n return new Promise<Response>((resolve, reject) => {\n setTimeout(() => {\n if (signal?.aborted) {\n reject(new DOMException('Aborted', 'AbortError'));\n } else {\n resolve({\n ok: true,\n status: 200,\n json: () => Promise.resolve(response),\n text: () => Promise.resolve(JSON.stringify(response))\n } as Response);\n }\n });\n });\n };\n}\n"]}
|
|
@@ -10,15 +10,15 @@ import { buildUrl, toJson } from './utils';
|
|
|
10
10
|
*
|
|
11
11
|
* @public
|
|
12
12
|
*/
|
|
13
|
-
export const fetchHttpClient = (endpoint, { id = endpoint, parameters = {}, properties, sendParamsInBody = false } = {}) => {
|
|
14
|
-
const signal = abortAndGetNewAbortSignal(id);
|
|
13
|
+
export const fetchHttpClient = (endpoint, { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false } = {}) => {
|
|
14
|
+
const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
15
15
|
const flatParameters = flatObject(parameters);
|
|
16
16
|
const url = sendParamsInBody ? endpoint : buildUrl(endpoint, flatParameters);
|
|
17
17
|
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(parameters) } : {};
|
|
18
18
|
return fetch(url, {
|
|
19
19
|
...properties,
|
|
20
20
|
...bodyParameters,
|
|
21
|
-
signal
|
|
21
|
+
...signal
|
|
22
22
|
}).then(toJson);
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAe,CACzC,QAAQ,EACR,EAAE,EAAE,GAAG,QAAQ,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAe,CACzC,QAAQ,EACR,EAAE,EAAE,GAAG,QAAQ,EAAE,UAAU,GAAG,IAAI,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,EAAE,EAChG,EAAE;IACF,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,yBAAyB,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3E,MAAM,cAAc,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;IAC9C,MAAM,GAAG,GAAG,gBAAgB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;IAC7E,MAAM,cAAc,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAEpF,OAAO,KAAK,CAAC,GAAG,EAAE;QAChB,GAAG,UAAU;QACb,GAAG,cAAc;QACjB,GAAG,MAAM;KACV,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAClB,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,uBAAuB,GAAgC,EAAE,CAAC;AAEhE;;;;;;;GAOG;AACH,SAAS,yBAAyB,CAAC,EAAU;;IAC3C,MAAA,uBAAuB,CAAC,EAAE,CAAC,0CAAE,KAAK,EAAE,CAAC;IACrC,uBAAuB,CAAC,EAAE,CAAC,GAAG,IAAI,eAAe,EAAE,CAAC;IACpD,OAAO,uBAAuB,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC;AAC5C,CAAC","sourcesContent":["import { Dictionary, flatObject } from '@empathyco/x-utils';\nimport { HttpClient } from './types';\nimport { buildUrl, toJson } from './utils';\n\n/**\n * The `fetchHttpClient()` function is a http client implementation using the `fetch` WebAPI.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n *\n * @returns A `Promise` object.\n *\n * @public\n */\nexport const fetchHttpClient: HttpClient = (\n endpoint,\n { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false } = {}\n) => {\n const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};\n const flatParameters = flatObject(parameters);\n const url = sendParamsInBody ? endpoint : buildUrl(endpoint, flatParameters);\n const bodyParameters = sendParamsInBody ? { body: JSON.stringify(parameters) } : {};\n\n return fetch(url, {\n ...properties,\n ...bodyParameters,\n ...signal\n }).then(toJson);\n};\n\n/**\n * Dictionary with the request id as key and an `AbortController` as value.\n */\nconst requestAbortControllers: Dictionary<AbortController> = {};\n\n/**\n * Function that cancels previous request with the same `id` and returns a new `AbortSignal` for\n * the new request.\n *\n * @param id - The identifier of the request to cancel and create a new `AbortSignal`.\n *\n * @returns The new `AbortSignal`.\n */\nfunction abortAndGetNewAbortSignal(id: string): AbortSignal {\n requestAbortControllers[id]?.abort();\n requestAbortControllers[id] = new AbortController();\n return requestAbortControllers[id].signal;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/http-clients/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/http-clients/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC","sourcesContent":["export * from './fetch.http-client';\nexport * from './types';\nexport * from './utils';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/http-clients/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Dictionary } from '@empathyco/x-utils';\n\n/**\n * Makes a request to a backend API using the given parameters.\n *\n * @param endpoint - The endpoint to use.\n * @param options - Additional options to make the request with.\n * @returns A promise wrapped object containing the response.\n * @public\n */\nexport type HttpClient = <Response = unknown>(\n endpoint: string,\n options?: Omit<RequestOptions, 'endpoint'>\n) => Promise<Readonly<Response>>;\n\n/**\n * A record of options to make the request with.\n *\n * @public\n */\nexport interface RequestOptions {\n /**\n * A unique identifier for this request. Can be used to abort requests with same id.\n */\n id?: string;\n /**\n * A flag to send parameters in the body if true or in the url QueryString if false.\n */\n sendParamsInBody?: boolean;\n /**\n * A list of parameters to send to the API.\n */\n parameters?: Dictionary<unknown>;\n /**\n * The RequestInit object to create the request with.\n */\n properties?: RequestInit;\n /**\n * The base endpoint that the request should use.\n */\n endpoint?: string;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/http-clients/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Dictionary } from '@empathyco/x-utils';\n\n/**\n * Makes a request to a backend API using the given parameters.\n *\n * @param endpoint - The endpoint to use.\n * @param options - Additional options to make the request with.\n * @returns A promise wrapped object containing the response.\n * @public\n */\nexport type HttpClient = <Response = unknown>(\n endpoint: string,\n options?: Omit<RequestOptions, 'endpoint'>\n) => Promise<Readonly<Response>>;\n\n/**\n * A record of options to make the request with.\n *\n * @public\n */\nexport interface RequestOptions {\n /**\n * A unique identifier for this request. Can be used to abort requests with same id.\n */\n id?: string;\n /**\n * True if the request can be cancelled.\n */\n cancelable?: boolean;\n /**\n * A flag to send parameters in the body if true or in the url QueryString if false.\n */\n sendParamsInBody?: boolean;\n /**\n * A list of parameters to send to the API.\n */\n parameters?: Dictionary<unknown>;\n /**\n * The RequestInit object to create the request with.\n */\n properties?: RequestInit;\n /**\n * The base endpoint that the request should use.\n */\n endpoint?: string;\n}\n"]}
|
|
@@ -12,7 +12,7 @@ import { RequestError } from './errors/request-error';
|
|
|
12
12
|
*/
|
|
13
13
|
export function toJson(response) {
|
|
14
14
|
if (response.ok) {
|
|
15
|
-
return response.
|
|
15
|
+
return response.text().then(text => (text ? JSON.parse(text) : {}));
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
18
18
|
throw new RequestError('Request failed', response);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/http-clients/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;;;;;GASG;AACH,MAAM,UAAU,MAAM,CAAC,QAAkB;IACvC,IAAI,QAAQ,CAAC,EAAE,EAAE;QACf,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/http-clients/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;;;;;GASG;AACH,MAAM,UAAU,MAAM,CAAC,QAAkB;IACvC,IAAI,QAAQ,CAAC,EAAE,EAAE;QACf,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;KACrE;SAAM;QACL,MAAM,IAAI,YAAY,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC;KACpD;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAC,QAAgB,EAAE,SAA8B,EAAE;IACzE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,OAAO,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAC7B,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,CAChE,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC,CACrD,CACF,CAAC;IACF,OAAO,GAAG,CAAC,IAAI,CAAC;AAClB,CAAC","sourcesContent":["import { Dictionary, forEach } from '@empathyco/x-utils';\nimport { RequestError } from './errors/request-error';\n\n/**\n * Formats a response object to JSON.\n *\n * @remarks If the `response.ok` is falsy, a `RequestError` object is thrown.\n *\n * @param response - The response to convert to JSON format.\n * @returns - The resultant promise of formatting the response to JSON.\n *\n * @public\n */\nexport function toJson(response: Response): Promise<any> {\n if (response.ok) {\n return response.text().then(text => (text ? JSON.parse(text) : {}));\n } else {\n throw new RequestError('Request failed', response);\n }\n}\n\n/**\n * Builds a URL object based on the passed endpoint and the request parameters.\n *\n * @param endpoint - The endpoint.\n * @param params - The request parameters.\n *\n * @returns The `href` property of the newly built `URL` object.\n *\n * @public\n */\nexport function buildUrl(endpoint: string, params: Dictionary<unknown> = {}): URL['href'] {\n const url = new URL(endpoint);\n forEach(params, (key, value) =>\n (Array.isArray(value) ? value : [value]).forEach(arrayItemValue =>\n url.searchParams.append(key, String(arrayItemValue))\n )\n );\n return url.href;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="jest" />
|
|
2
|
-
export declare const okFetchMock: jest.Mock<
|
|
2
|
+
export declare const okFetchMock: jest.Mock<Promise<Response>, [url: string, params: RequestInit]>;
|
|
3
3
|
export declare const koFetchMock: jest.Mock<Promise<{
|
|
4
4
|
ok: boolean;
|
|
5
5
|
json: () => Promise<{}>;
|
|
@@ -14,4 +14,4 @@ export declare const koFetchMock: jest.Mock<Promise<{
|
|
|
14
14
|
*
|
|
15
15
|
* @internal
|
|
16
16
|
*/
|
|
17
|
-
export declare function fetchMock(response:
|
|
17
|
+
export declare function fetchMock(response: unknown): (url: string, params: RequestInit) => Promise<Response>;
|
|
@@ -18,6 +18,10 @@ export interface RequestOptions {
|
|
|
18
18
|
* A unique identifier for this request. Can be used to abort requests with same id.
|
|
19
19
|
*/
|
|
20
20
|
id?: string;
|
|
21
|
+
/**
|
|
22
|
+
* True if the request can be cancelled.
|
|
23
|
+
*/
|
|
24
|
+
cancelable?: boolean;
|
|
21
25
|
/**
|
|
22
26
|
* A flag to send parameters in the body if true or in the url QueryString if false.
|
|
23
27
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empathyco/x-adapter",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.7",
|
|
4
4
|
"description": "A utils library to create a client for any API",
|
|
5
5
|
"author": "Empathy Systems Corporation S.L.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"prepublishOnly": "npm run build"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@empathyco/x-deep-merge": "^1.3.0-alpha.
|
|
35
|
+
"@empathyco/x-deep-merge": "^1.3.0-alpha.20",
|
|
36
36
|
"@empathyco/x-get-safe-property-chain": "^1.3.0-alpha.3",
|
|
37
37
|
"tslib": "~2.3.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@empathyco/x-utils": "^1.0.0-alpha.
|
|
40
|
+
"@empathyco/x-utils": "^1.0.0-alpha.6",
|
|
41
41
|
"@types/jest": "~27.0.3",
|
|
42
42
|
"concurrently": "~7.0.0",
|
|
43
43
|
"jest": "~27.3.1",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "5dac07f17af4bd325f1feddc9d26a7c48e0cc73a"
|
|
52
52
|
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.beaconHttpClient = void 0;
|
|
4
|
-
const x_utils_1 = require("@empathyco/x-utils");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
/**
|
|
7
|
-
* Flag determining if the user has an ad-blocker installed.
|
|
8
|
-
*/
|
|
9
|
-
let hasAdBlocker = undefined;
|
|
10
|
-
/**
|
|
11
|
-
* The `beaconHttpClient()` function is a http client implementation using the WebAPI
|
|
12
|
-
* `navigator.sendBeacon`.
|
|
13
|
-
*
|
|
14
|
-
* @remarks As a `navigation.sendBeacon` request might be cancelled by an ad-blocker, the function
|
|
15
|
-
* firstly checks if an ad-blocker is installed. In case it is, the `fetch` API is used as fallback.
|
|
16
|
-
*
|
|
17
|
-
* @param endpoint - The endpoint to make the request to.
|
|
18
|
-
* @param options - The request options.
|
|
19
|
-
*
|
|
20
|
-
* @public
|
|
21
|
-
*/
|
|
22
|
-
const beaconHttpClient = async (endpoint, { parameters = {}, properties } = {}) => {
|
|
23
|
-
const url = (0, utils_1.buildUrl)(endpoint, (0, x_utils_1.flatObject)(parameters));
|
|
24
|
-
if (hasAdBlocker === undefined) {
|
|
25
|
-
try {
|
|
26
|
-
const response = await detectAdBlocker();
|
|
27
|
-
// The Promise returned from fetch() won't reject on HTTP error status even if the response is
|
|
28
|
-
// an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it
|
|
29
|
-
// will only reject on network failure or if anything prevented the request from completing.
|
|
30
|
-
// eslint-disable-next-line require-atomic-updates
|
|
31
|
-
hasAdBlocker = !response.ok;
|
|
32
|
-
}
|
|
33
|
-
catch (_error) {
|
|
34
|
-
// eslint-disable-next-line require-atomic-updates
|
|
35
|
-
hasAdBlocker = true;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (hasAdBlocker) {
|
|
39
|
-
return fetch(url, properties);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
navigator.sendBeacon(url);
|
|
43
|
-
return Promise.resolve({});
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
exports.beaconHttpClient = beaconHttpClient;
|
|
47
|
-
/**
|
|
48
|
-
* The `detectAdBlocker()` function checks if the user has an ad-blocker installed.
|
|
49
|
-
*
|
|
50
|
-
* @returns A Promise object. If the `response` is `ok`, no ad-blocker is installed. Otherwise, an
|
|
51
|
-
* ad-blocker is active.
|
|
52
|
-
*
|
|
53
|
-
* @internal
|
|
54
|
-
*/
|
|
55
|
-
function detectAdBlocker() {
|
|
56
|
-
return fetch('https://google.com/pagead/js/adsbygoogle.js', {
|
|
57
|
-
method: 'HEAD',
|
|
58
|
-
mode: 'no-cors'
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
//# sourceMappingURL=beacon.http-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"beacon.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/beacon.http-client.ts"],"names":[],"mappings":";;;AAAA,gDAAgD;AAEhD,mCAAmC;AAEnC;;GAEG;AACH,IAAI,YAAY,GAAwB,SAAS,CAAC;AAElD;;;;;;;;;;;GAWG;AACI,MAAM,gBAAgB,GAAe,KAAK,EAC/C,QAAQ,EACR,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,EACpC,EAAE;IACF,MAAM,GAAG,GAAG,IAAA,gBAAQ,EAAC,QAAQ,EAAE,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAC,CAAC;IAEvD,IAAI,YAAY,KAAK,SAAS,EAAE;QAC9B,IAAI;YACF,MAAM,QAAQ,GAAG,MAAM,eAAe,EAAE,CAAC;YACzC,8FAA8F;YAC9F,8FAA8F;YAC9F,4FAA4F;YAC5F,kDAAkD;YAClD,YAAY,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;SAC7B;QAAC,OAAO,MAAM,EAAE;YACf,kDAAkD;YAClD,YAAY,GAAG,IAAI,CAAC;SACrB;KACF;IAED,IAAI,YAAY,EAAE;QAChB,OAAO,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;KAC/B;SAAM;QACL,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAE1B,OAAO,OAAO,CAAC,OAAO,CAAM,EAAE,CAAC,CAAC;KACjC;AACH,CAAC,CAAC;AA3BW,QAAA,gBAAgB,oBA2B3B;AAEF;;;;;;;GAOG;AACH,SAAS,eAAe;IACtB,OAAO,KAAK,CAAC,6CAA6C,EAAE;QAC1D,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,SAAS;KAChB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { flatObject } from '@empathyco/x-utils';\nimport { HttpClient } from './types';\nimport { buildUrl } from './utils';\n\n/**\n * Flag determining if the user has an ad-blocker installed.\n */\nlet hasAdBlocker: boolean | undefined = undefined;\n\n/**\n * The `beaconHttpClient()` function is a http client implementation using the WebAPI\n * `navigator.sendBeacon`.\n *\n * @remarks As a `navigation.sendBeacon` request might be cancelled by an ad-blocker, the function\n * firstly checks if an ad-blocker is installed. In case it is, the `fetch` API is used as fallback.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n *\n * @public\n */\nexport const beaconHttpClient: HttpClient = async (\n endpoint,\n { parameters = {}, properties } = {}\n) => {\n const url = buildUrl(endpoint, flatObject(parameters));\n\n if (hasAdBlocker === undefined) {\n try {\n const response = await detectAdBlocker();\n // The Promise returned from fetch() won't reject on HTTP error status even if the response is\n // an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it\n // will only reject on network failure or if anything prevented the request from completing.\n // eslint-disable-next-line require-atomic-updates\n hasAdBlocker = !response.ok;\n } catch (_error) {\n // eslint-disable-next-line require-atomic-updates\n hasAdBlocker = true;\n }\n }\n\n if (hasAdBlocker) {\n return fetch(url, properties);\n } else {\n navigator.sendBeacon(url);\n\n return Promise.resolve<any>({});\n }\n};\n\n/**\n * The `detectAdBlocker()` function checks if the user has an ad-blocker installed.\n *\n * @returns A Promise object. If the `response` is `ok`, no ad-blocker is installed. Otherwise, an\n * ad-blocker is active.\n *\n * @internal\n */\nfunction detectAdBlocker(): Promise<Response> {\n return fetch('https://google.com/pagead/js/adsbygoogle.js', {\n method: 'HEAD',\n mode: 'no-cors'\n });\n}\n"]}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { flatObject } from '@empathyco/x-utils';
|
|
2
|
-
import { buildUrl } from './utils';
|
|
3
|
-
/**
|
|
4
|
-
* Flag determining if the user has an ad-blocker installed.
|
|
5
|
-
*/
|
|
6
|
-
let hasAdBlocker = undefined;
|
|
7
|
-
/**
|
|
8
|
-
* The `beaconHttpClient()` function is a http client implementation using the WebAPI
|
|
9
|
-
* `navigator.sendBeacon`.
|
|
10
|
-
*
|
|
11
|
-
* @remarks As a `navigation.sendBeacon` request might be cancelled by an ad-blocker, the function
|
|
12
|
-
* firstly checks if an ad-blocker is installed. In case it is, the `fetch` API is used as fallback.
|
|
13
|
-
*
|
|
14
|
-
* @param endpoint - The endpoint to make the request to.
|
|
15
|
-
* @param options - The request options.
|
|
16
|
-
*
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
export const beaconHttpClient = async (endpoint, { parameters = {}, properties } = {}) => {
|
|
20
|
-
const url = buildUrl(endpoint, flatObject(parameters));
|
|
21
|
-
if (hasAdBlocker === undefined) {
|
|
22
|
-
try {
|
|
23
|
-
const response = await detectAdBlocker();
|
|
24
|
-
// The Promise returned from fetch() won't reject on HTTP error status even if the response is
|
|
25
|
-
// an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it
|
|
26
|
-
// will only reject on network failure or if anything prevented the request from completing.
|
|
27
|
-
// eslint-disable-next-line require-atomic-updates
|
|
28
|
-
hasAdBlocker = !response.ok;
|
|
29
|
-
}
|
|
30
|
-
catch (_error) {
|
|
31
|
-
// eslint-disable-next-line require-atomic-updates
|
|
32
|
-
hasAdBlocker = true;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (hasAdBlocker) {
|
|
36
|
-
return fetch(url, properties);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
navigator.sendBeacon(url);
|
|
40
|
-
return Promise.resolve({});
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* The `detectAdBlocker()` function checks if the user has an ad-blocker installed.
|
|
45
|
-
*
|
|
46
|
-
* @returns A Promise object. If the `response` is `ok`, no ad-blocker is installed. Otherwise, an
|
|
47
|
-
* ad-blocker is active.
|
|
48
|
-
*
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
function detectAdBlocker() {
|
|
52
|
-
return fetch('https://google.com/pagead/js/adsbygoogle.js', {
|
|
53
|
-
method: 'HEAD',
|
|
54
|
-
mode: 'no-cors'
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=beacon.http-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"beacon.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/beacon.http-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC;;GAEG;AACH,IAAI,YAAY,GAAwB,SAAS,CAAC;AAElD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAe,KAAK,EAC/C,QAAQ,EACR,EAAE,UAAU,GAAG,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,EACpC,EAAE;IACF,MAAM,GAAG,GAAG,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC;IAEvD,IAAI,YAAY,KAAK,SAAS,EAAE;QAC9B,IAAI;YACF,MAAM,QAAQ,GAAG,MAAM,eAAe,EAAE,CAAC;YACzC,8FAA8F;YAC9F,8FAA8F;YAC9F,4FAA4F;YAC5F,kDAAkD;YAClD,YAAY,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;SAC7B;QAAC,OAAO,MAAM,EAAE;YACf,kDAAkD;YAClD,YAAY,GAAG,IAAI,CAAC;SACrB;KACF;IAED,IAAI,YAAY,EAAE;QAChB,OAAO,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;KAC/B;SAAM;QACL,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAE1B,OAAO,OAAO,CAAC,OAAO,CAAM,EAAE,CAAC,CAAC;KACjC;AACH,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,SAAS,eAAe;IACtB,OAAO,KAAK,CAAC,6CAA6C,EAAE;QAC1D,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,SAAS;KAChB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { flatObject } from '@empathyco/x-utils';\nimport { HttpClient } from './types';\nimport { buildUrl } from './utils';\n\n/**\n * Flag determining if the user has an ad-blocker installed.\n */\nlet hasAdBlocker: boolean | undefined = undefined;\n\n/**\n * The `beaconHttpClient()` function is a http client implementation using the WebAPI\n * `navigator.sendBeacon`.\n *\n * @remarks As a `navigation.sendBeacon` request might be cancelled by an ad-blocker, the function\n * firstly checks if an ad-blocker is installed. In case it is, the `fetch` API is used as fallback.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n *\n * @public\n */\nexport const beaconHttpClient: HttpClient = async (\n endpoint,\n { parameters = {}, properties } = {}\n) => {\n const url = buildUrl(endpoint, flatObject(parameters));\n\n if (hasAdBlocker === undefined) {\n try {\n const response = await detectAdBlocker();\n // The Promise returned from fetch() won't reject on HTTP error status even if the response is\n // an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it\n // will only reject on network failure or if anything prevented the request from completing.\n // eslint-disable-next-line require-atomic-updates\n hasAdBlocker = !response.ok;\n } catch (_error) {\n // eslint-disable-next-line require-atomic-updates\n hasAdBlocker = true;\n }\n }\n\n if (hasAdBlocker) {\n return fetch(url, properties);\n } else {\n navigator.sendBeacon(url);\n\n return Promise.resolve<any>({});\n }\n};\n\n/**\n * The `detectAdBlocker()` function checks if the user has an ad-blocker installed.\n *\n * @returns A Promise object. If the `response` is `ok`, no ad-blocker is installed. Otherwise, an\n * ad-blocker is active.\n *\n * @internal\n */\nfunction detectAdBlocker(): Promise<Response> {\n return fetch('https://google.com/pagead/js/adsbygoogle.js', {\n method: 'HEAD',\n mode: 'no-cors'\n });\n}\n"]}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { HttpClient } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* The `beaconHttpClient()` function is a http client implementation using the WebAPI
|
|
4
|
-
* `navigator.sendBeacon`.
|
|
5
|
-
*
|
|
6
|
-
* @remarks As a `navigation.sendBeacon` request might be cancelled by an ad-blocker, the function
|
|
7
|
-
* firstly checks if an ad-blocker is installed. In case it is, the `fetch` API is used as fallback.
|
|
8
|
-
*
|
|
9
|
-
* @param endpoint - The endpoint to make the request to.
|
|
10
|
-
* @param options - The request options.
|
|
11
|
-
*
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export declare const beaconHttpClient: HttpClient;
|