@empathyco/x-adapter
Advanced tools
Comparing version 8.0.0-alpha.2 to 8.0.0-alpha.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.beaconHttpClient = void 0; | ||
const x_utils_1 = require("@empathyco/x-utils"); | ||
const utils_1 = require("./utils"); | ||
@@ -21,4 +22,4 @@ /** | ||
*/ | ||
const beaconHttpClient = async (endpoint, { parameters, properties } = {}) => { | ||
const url = (0, utils_1.buildUrl)(endpoint, parameters); | ||
const beaconHttpClient = async (endpoint, { parameters = {}, properties } = {}) => { | ||
const url = (0, utils_1.buildUrl)(endpoint, (0, x_utils_1.flatObject)(parameters)); | ||
if (hasAdBlocker === undefined) { | ||
@@ -25,0 +26,0 @@ try { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fetchHttpClient = void 0; | ||
const x_utils_1 = require("@empathyco/x-utils"); | ||
const utils_1 = require("./utils"); | ||
/** | ||
* Dictionary with the request id as key and an `AbortController` as value. | ||
*/ | ||
const requestAbortControllers = {}; | ||
/** | ||
* The `fetchHttpClient()` function is a http client implementation using the `fetch` WebAPI. | ||
@@ -19,10 +16,32 @@ * | ||
*/ | ||
const fetchHttpClient = (endpoint, { id = endpoint, parameters, properties } = {}) => { | ||
const fetchHttpClient = (endpoint, { id = endpoint, parameters = {}, properties, sendParamsInBody = false } = {}) => { | ||
const signal = abortAndGetNewAbortSignal(id); | ||
const flatParameters = (0, x_utils_1.flatObject)(parameters); | ||
const url = sendParamsInBody ? endpoint : (0, utils_1.buildUrl)(endpoint, flatParameters); | ||
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(flatParameters) } : {}; | ||
return fetch(url, { | ||
...properties, | ||
...bodyParameters, | ||
signal | ||
}).then(utils_1.toJson); | ||
}; | ||
exports.fetchHttpClient = fetchHttpClient; | ||
/** | ||
* Dictionary with the request id as key and an `AbortController` as value. | ||
*/ | ||
const requestAbortControllers = {}; | ||
/** | ||
* Function that cancels previous request with the same `id` and returns a new `AbortSignal` for | ||
* the new request. | ||
* | ||
* @param id - The identifier of the request to cancel and create a new `AbortSignal`. | ||
* | ||
* @returns The new `AbortSignal`. | ||
*/ | ||
function abortAndGetNewAbortSignal(id) { | ||
var _a; | ||
const url = (0, utils_1.buildUrl)(endpoint, parameters); | ||
(_a = requestAbortControllers[id]) === null || _a === void 0 ? void 0 : _a.abort(); | ||
requestAbortControllers[id] = new AbortController(); | ||
return fetch(url, { ...properties, signal: requestAbortControllers[id].signal }).then(utils_1.toJson); | ||
}; | ||
exports.fetchHttpClient = fetchHttpClient; | ||
return requestAbortControllers[id].signal; | ||
} | ||
//# sourceMappingURL=fetch.http-client.js.map |
@@ -37,4 +37,3 @@ "use strict"; | ||
const url = new URL(endpoint); | ||
const flattenedParams = (0, x_utils_1.flatObject)(params); | ||
(0, x_utils_1.forEach)(flattenedParams, (key, value) => (Array.isArray(value) ? value : [value]).forEach(arrayItemValue => url.searchParams.append(key, String(arrayItemValue)))); | ||
(0, x_utils_1.forEach)(params, (key, value) => (Array.isArray(value) ? value : [value]).forEach(arrayItemValue => url.searchParams.append(key, String(arrayItemValue)))); | ||
return url.href; | ||
@@ -41,0 +40,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { flatObject } from '@empathyco/x-utils'; | ||
import { buildUrl } from './utils'; | ||
@@ -18,4 +19,4 @@ /** | ||
*/ | ||
export const beaconHttpClient = async (endpoint, { parameters, properties } = {}) => { | ||
const url = buildUrl(endpoint, parameters); | ||
export const beaconHttpClient = async (endpoint, { parameters = {}, properties } = {}) => { | ||
const url = buildUrl(endpoint, flatObject(parameters)); | ||
if (hasAdBlocker === undefined) { | ||
@@ -22,0 +23,0 @@ try { |
@@ -0,7 +1,4 @@ | ||
import { flatObject } from '@empathyco/x-utils'; | ||
import { buildUrl, toJson } from './utils'; | ||
/** | ||
* Dictionary with the request id as key and an `AbortController` as value. | ||
*/ | ||
const requestAbortControllers = {}; | ||
/** | ||
* The `fetchHttpClient()` function is a http client implementation using the `fetch` WebAPI. | ||
@@ -16,9 +13,31 @@ * | ||
*/ | ||
export const fetchHttpClient = (endpoint, { id = endpoint, parameters, properties } = {}) => { | ||
export const fetchHttpClient = (endpoint, { id = endpoint, parameters = {}, properties, sendParamsInBody = false } = {}) => { | ||
const signal = abortAndGetNewAbortSignal(id); | ||
const flatParameters = flatObject(parameters); | ||
const url = sendParamsInBody ? endpoint : buildUrl(endpoint, flatParameters); | ||
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(flatParameters) } : {}; | ||
return fetch(url, { | ||
...properties, | ||
...bodyParameters, | ||
signal | ||
}).then(toJson); | ||
}; | ||
/** | ||
* Dictionary with the request id as key and an `AbortController` as value. | ||
*/ | ||
const requestAbortControllers = {}; | ||
/** | ||
* Function that cancels previous request with the same `id` and returns a new `AbortSignal` for | ||
* the new request. | ||
* | ||
* @param id - The identifier of the request to cancel and create a new `AbortSignal`. | ||
* | ||
* @returns The new `AbortSignal`. | ||
*/ | ||
function abortAndGetNewAbortSignal(id) { | ||
var _a; | ||
const url = buildUrl(endpoint, parameters); | ||
(_a = requestAbortControllers[id]) === null || _a === void 0 ? void 0 : _a.abort(); | ||
requestAbortControllers[id] = new AbortController(); | ||
return fetch(url, { ...properties, signal: requestAbortControllers[id].signal }).then(toJson); | ||
}; | ||
return requestAbortControllers[id].signal; | ||
} | ||
//# sourceMappingURL=fetch.http-client.js.map |
@@ -1,2 +0,2 @@ | ||
import { flatObject, forEach } from '@empathyco/x-utils'; | ||
import { forEach } from '@empathyco/x-utils'; | ||
import { RequestError } from './errors/request-error'; | ||
@@ -33,6 +33,5 @@ /** | ||
const url = new URL(endpoint); | ||
const flattenedParams = flatObject(params); | ||
forEach(flattenedParams, (key, value) => (Array.isArray(value) ? value : [value]).forEach(arrayItemValue => url.searchParams.append(key, String(arrayItemValue)))); | ||
forEach(params, (key, value) => (Array.isArray(value) ? value : [value]).forEach(arrayItemValue => url.searchParams.append(key, String(arrayItemValue)))); | ||
return url.href; | ||
} | ||
//# sourceMappingURL=utils.js.map |
@@ -22,2 +22,6 @@ import { Dictionary } from '@empathyco/x-utils'; | ||
/** | ||
* A flag to send parameters in the body if true or in the url QueryString if false. | ||
*/ | ||
sendParamsInBody?: boolean; | ||
/** | ||
* A list of parameters to send to the API. | ||
@@ -24,0 +28,0 @@ */ |
{ | ||
"name": "@empathyco/x-adapter", | ||
"version": "8.0.0-alpha.2", | ||
"version": "8.0.0-alpha.3", | ||
"description": "A utils library to create a client for any API", | ||
@@ -51,3 +51,3 @@ "author": "Empathy Systems Corporation S.L.", | ||
}, | ||
"gitHead": "4f0b22a49c3fe9bfbba789012e168c04ec976265" | ||
"gitHead": "a94853e17793bab55cecf8dc9cd6ffedd1ac8812" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
176584
1777