@empathyco/x-adapter 17.0.0 → 18.0.0
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/endpoint-adapter/endpoint-adapter.factory.js +27 -1
- package/dist/cjs/endpoint-adapter/endpoint-adapter.factory.js.map +1 -1
- package/dist/cjs/http-clients/fetch.http-client.js +3 -1
- package/dist/cjs/http-clients/fetch.http-client.js.map +1 -1
- package/dist/esm/endpoint-adapter/endpoint-adapter.factory.js +27 -1
- package/dist/esm/endpoint-adapter/endpoint-adapter.factory.js.map +1 -1
- package/dist/esm/http-clients/fetch.http-client.js +3 -1
- package/dist/esm/http-clients/fetch.http-client.js.map +1 -1
- package/dist/types/http-clients/fetch.http-client.d.ts +2 -0
- package/package.json +3 -3
|
@@ -20,7 +20,7 @@ const endpointAdapterFactory = (options) => {
|
|
|
20
20
|
const { endpoint: rawEndpoint, httpClient = fetch_http_client_1.fetchHttpClient, requestMapper = identity_mapper_1.identityMapper, responseMapper = identity_mapper_1.identityMapper, defaultRequestOptions = {}, } = options;
|
|
21
21
|
const endpoint = getEndpoint(requestEndpoint !== null && requestEndpoint !== void 0 ? requestEndpoint : rawEndpoint, request);
|
|
22
22
|
const requestParameters = requestMapper(request, { endpoint });
|
|
23
|
-
return httpClient(endpoint, (
|
|
23
|
+
return httpClient(endpoint, mergeRequestOptions(defaultRequestOptions, requestOptions, { parameters: requestParameters })).then(response => responseMapper(response, { endpoint, requestParameters }));
|
|
24
24
|
};
|
|
25
25
|
endpointAdapter.extends = (extendedOptions) => (0, exports.endpointAdapterFactory)({
|
|
26
26
|
...options,
|
|
@@ -29,6 +29,32 @@ const endpointAdapterFactory = (options) => {
|
|
|
29
29
|
return endpointAdapter;
|
|
30
30
|
};
|
|
31
31
|
exports.endpointAdapterFactory = endpointAdapterFactory;
|
|
32
|
+
/**
|
|
33
|
+
* Deep merges the request options. The `signal` of the request properties is kept by reference,
|
|
34
|
+
* as a deep merge would clone the `AbortSignal` into a plain object. The last signal wins.
|
|
35
|
+
*
|
|
36
|
+
* @param options - The request options to merge, from lower to higher priority.
|
|
37
|
+
*
|
|
38
|
+
* @returns The merged request options.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
function mergeRequestOptions(...options) {
|
|
42
|
+
let signal;
|
|
43
|
+
const optionsWithoutSignal = options.map(option => {
|
|
44
|
+
var _a;
|
|
45
|
+
if (((_a = option.properties) === null || _a === void 0 ? void 0 : _a.signal) === undefined) {
|
|
46
|
+
return option;
|
|
47
|
+
}
|
|
48
|
+
const { signal: optionSignal, ...properties } = option.properties;
|
|
49
|
+
signal = optionSignal;
|
|
50
|
+
return { ...option, properties };
|
|
51
|
+
});
|
|
52
|
+
const mergedOptions = (0, x_utils_1.deepMerge)({}, ...optionsWithoutSignal);
|
|
53
|
+
if (signal !== undefined) {
|
|
54
|
+
mergedOptions.properties = { ...mergedOptions.properties, signal };
|
|
55
|
+
}
|
|
56
|
+
return mergedOptions;
|
|
57
|
+
}
|
|
32
58
|
/**
|
|
33
59
|
* Returns an endpoint.
|
|
34
60
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":";;;AAOA,gDAA8C;AAC9C,yEAAmE;AACnE,gEAA2D;AAC3D,sDAAkD;AAElD;;;;;;;;;GASG;AACI,MAAM,sBAAsB,GAA2B,CAC5D,OAAkD,EAClD,EAAE;IACF,MAAM,eAAe,GAAiD,KAAK,EACzE,OAAO,EACP,EAAE,QAAQ,EAAE,eAAe,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,EACrD,EAAE;QACF,MAAM,EACJ,QAAQ,EAAE,WAAW,EACrB,UAAU,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C,CAAC,CAAA;IACF,MAAM,aAAa,GAAqC,IAAA,mBAAS,EAAC,EAAE,EAAE,GAAG,oBAAoB,CAAC,CAAA;IAC9F,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,aAAa,CAAC,UAAU,GAAG,EAAE,GAAG,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,CAAA;IACpE,CAAC;IACD,OAAO,aAAa,CAAA;AACtB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,WAAW,CAClB,QAAsD,EACtD,OAAgB;IAEhB,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;IAChE,CAAC;IAED,OAAO,OAAO,QAAQ,KAAK,UAAU;QACnC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QACvB,CAAC,CAAC,IAAA,yBAAW,EAAC,QAAQ,EAAE,OAAkC,CAAC,CAAA;AAC/D,CAAC","sourcesContent":["import type { RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\nimport type {\n EndpointAdapterFactory,\n EndpointAdapterOptions,\n ExtendableEndpointAdapter,\n} from './types'\nimport { deepMerge } from '@empathyco/x-utils'\nimport { fetchHttpClient } from '../http-clients/fetch.http-client'\nimport { identityMapper } from '../mappers/identity.mapper'\nimport { interpolate } from '../utils/interpolate'\n\n/**\n * Factory to create {@link ExtendableEndpointAdapter | endpoint adapters} with the given\n * {@link EndpointAdapterOptions | options}.\n *\n * @param options - The {@link EndpointAdapterOptions | options} to create a new\n * {@link ExtendableEndpointAdapter} with.\n *\n * @returns A brand new {@link ExtendableEndpointAdapter} object.\n * @public\n */\nexport const endpointAdapterFactory: EndpointAdapterFactory = <Request, Response>(\n options: EndpointAdapterOptions<Request, Response>,\n) => {\n const endpointAdapter: ExtendableEndpointAdapter<Request, Response> = async (\n request,\n { endpoint: requestEndpoint, ...requestOptions } = {},\n ) => {\n const {\n endpoint: rawEndpoint,\n httpClient = fetchHttpClient,\n requestMapper = identityMapper,\n responseMapper = identityMapper,\n defaultRequestOptions = {},\n }: EndpointAdapterOptions<Request, Response> = options\n\n const endpoint = getEndpoint(requestEndpoint ?? rawEndpoint, request)\n const requestParameters = requestMapper(request, { endpoint })\n\n return httpClient(\n endpoint,\n mergeRequestOptions(defaultRequestOptions, requestOptions, { parameters: requestParameters }),\n ).then(response => responseMapper(response, { endpoint, requestParameters }))\n }\n\n endpointAdapter.extends = <NewRequest, NewResponse>(\n extendedOptions: Partial<EndpointAdapterOptions<NewRequest, NewResponse>>,\n ) =>\n endpointAdapterFactory<NewRequest, NewResponse>({\n ...options,\n ...extendedOptions,\n } as EndpointAdapterOptions<NewRequest, NewResponse>)\n\n return endpointAdapter\n}\n\n/**\n * Deep merges the request options. The `signal` of the request properties is kept by reference,\n * as a deep merge would clone the `AbortSignal` into a plain object. The last signal wins.\n *\n * @param options - The request options to merge, from lower to higher priority.\n *\n * @returns The merged request options.\n * @internal\n */\nfunction mergeRequestOptions(\n ...options: Omit<RequestOptions, 'endpoint'>[]\n): Omit<RequestOptions, 'endpoint'> {\n let signal: AbortSignal | null | undefined\n const optionsWithoutSignal = options.map(option => {\n if (option.properties?.signal === undefined) {\n return option\n }\n const { signal: optionSignal, ...properties } = option.properties\n signal = optionSignal\n return { ...option, properties }\n })\n const mergedOptions: Omit<RequestOptions, 'endpoint'> = deepMerge({}, ...optionsWithoutSignal)\n if (signal !== undefined) {\n mergedOptions.properties = { ...mergedOptions.properties, signal }\n }\n return mergedOptions\n}\n\n/**\n * Returns an endpoint.\n *\n * @param endpoint - The endpoint to process.\n * @param request - The request object.\n *\n * @returns The endpoint.\n * @internal\n */\nfunction getEndpoint<Request>(\n endpoint: string | Mapper<Request, string> | undefined,\n request: Request,\n): string {\n if (!endpoint) {\n throw new Error('Tried to make a request without an endpoint')\n }\n\n return typeof endpoint === 'function'\n ? endpoint(request, {})\n : interpolate(endpoint, request as Record<string, unknown>)\n}\n"]}
|
|
@@ -7,6 +7,8 @@ const utils_1 = require("./utils");
|
|
|
7
7
|
* The `fetchRawHttpClient()` function is a http client implementation using the `fetch` WebAPI.
|
|
8
8
|
*
|
|
9
9
|
* @remarks This fucntion will replace the current `fetchHttpClient` in a possible future breaking change.
|
|
10
|
+
* When the request properties have a `signal`, the caller owns the cancellation of the request, so
|
|
11
|
+
* it is not cancelled by id.
|
|
10
12
|
*
|
|
11
13
|
* @param endpoint - The endpoint to make the request to.
|
|
12
14
|
* @param options - The request options.
|
|
@@ -22,7 +24,7 @@ const utils_1 = require("./utils");
|
|
|
22
24
|
* @public
|
|
23
25
|
*/
|
|
24
26
|
const fetchRawHttpClient = async (endpoint, { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false, sendEmptyParams = false, } = {}) => {
|
|
25
|
-
const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
27
|
+
const signal = (properties === null || properties === void 0 ? void 0 : properties.signal) != null ? {} : cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
26
28
|
if (!sendEmptyParams) {
|
|
27
29
|
parameters = (0, x_utils_1.cleanEmpty)(parameters);
|
|
28
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":";;;AAEA,gDAA2D;AAC3D,mCAA0C;AAE1C
|
|
1
|
+
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":";;;AAEA,gDAA2D;AAC3D,mCAA0C;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACI,MAAM,kBAAkB,GAAe,KAAK,EACjD,QAAQ,EACR,EACE,EAAE,GAAG,QAAQ,EACb,UAAU,GAAG,IAAI,EACjB,UAAU,GAAG,EAAE,EACf,UAAU,EACV,gBAAgB,GAAG,KAAK,EACxB,eAAe,GAAG,KAAK,GACxB,GAAG,E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AuB,CAAC,EAAE,CAAC,GAAG,IAAI,eAAe,EAAE,CAAA;IACnD,OAAO,uBAAuB,CAAC,EAAE,CAAC,CAAC,MAAM,CAAA;AAC3C,CAAC","sourcesContent":["import type { Dictionary } from '@empathyco/x-utils'\nimport type { HttpClient } from './types'\nimport { cleanEmpty, flatObject } from '@empathyco/x-utils'\nimport { buildUrl, toJson } from './utils'\n\n/**\n * The `fetchRawHttpClient()` function is a http client implementation using the `fetch` WebAPI.\n *\n * @remarks This fucntion will replace the current `fetchHttpClient` in a possible future breaking change.\n * When the request properties have a `signal`, the caller owns the cancellation of the request, so\n * it is not cancelled by id.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n * @param options.cancelable - Cancelable option.\n * @param options.id - ID option.\n * @param options.parameters - Parameters option.\n * @param options.properties - Properties option.\n * @param options.sendEmptyParams - SendEmptyParams option.\n * @param options.sendParamsInBody - SendParamsInBody option.\n *\n * @returns A `Promise` object.\n *\n * @public\n */\nexport const fetchRawHttpClient: HttpClient = async (\n endpoint,\n {\n id = endpoint,\n cancelable = true,\n parameters = {},\n properties,\n sendParamsInBody = false,\n sendEmptyParams = false,\n } = {},\n) => {\n const signal =\n properties?.signal != null ? {} : cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {}\n if (!sendEmptyParams) {\n parameters = cleanEmpty(parameters)\n }\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 })\n}\n\n/**\n * The `fetchHttpClient()` function is wrapper of `fetchRawHttpClient()` function that parses\n * the response with `toJson` function.\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 = async (endpoint, options = {}) =>\n fetchRawHttpClient(endpoint, options).then(toJson)\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"]}
|
|
@@ -17,7 +17,7 @@ export const endpointAdapterFactory = (options) => {
|
|
|
17
17
|
const { endpoint: rawEndpoint, httpClient = fetchHttpClient, requestMapper = identityMapper, responseMapper = identityMapper, defaultRequestOptions = {}, } = options;
|
|
18
18
|
const endpoint = getEndpoint(requestEndpoint !== null && requestEndpoint !== void 0 ? requestEndpoint : rawEndpoint, request);
|
|
19
19
|
const requestParameters = requestMapper(request, { endpoint });
|
|
20
|
-
return httpClient(endpoint,
|
|
20
|
+
return httpClient(endpoint, mergeRequestOptions(defaultRequestOptions, requestOptions, { parameters: requestParameters })).then(response => responseMapper(response, { endpoint, requestParameters }));
|
|
21
21
|
};
|
|
22
22
|
endpointAdapter.extends = (extendedOptions) => endpointAdapterFactory({
|
|
23
23
|
...options,
|
|
@@ -25,6 +25,32 @@ export const endpointAdapterFactory = (options) => {
|
|
|
25
25
|
});
|
|
26
26
|
return endpointAdapter;
|
|
27
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Deep merges the request options. The `signal` of the request properties is kept by reference,
|
|
30
|
+
* as a deep merge would clone the `AbortSignal` into a plain object. The last signal wins.
|
|
31
|
+
*
|
|
32
|
+
* @param options - The request options to merge, from lower to higher priority.
|
|
33
|
+
*
|
|
34
|
+
* @returns The merged request options.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function mergeRequestOptions(...options) {
|
|
38
|
+
let signal;
|
|
39
|
+
const optionsWithoutSignal = options.map(option => {
|
|
40
|
+
var _a;
|
|
41
|
+
if (((_a = option.properties) === null || _a === void 0 ? void 0 : _a.signal) === undefined) {
|
|
42
|
+
return option;
|
|
43
|
+
}
|
|
44
|
+
const { signal: optionSignal, ...properties } = option.properties;
|
|
45
|
+
signal = optionSignal;
|
|
46
|
+
return { ...option, properties };
|
|
47
|
+
});
|
|
48
|
+
const mergedOptions = deepMerge({}, ...optionsWithoutSignal);
|
|
49
|
+
if (signal !== undefined) {
|
|
50
|
+
mergedOptions.properties = { ...mergedOptions.properties, signal };
|
|
51
|
+
}
|
|
52
|
+
return mergedOptions;
|
|
53
|
+
}
|
|
28
54
|
/**
|
|
29
55
|
* Returns an endpoint.
|
|
30
56
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAElD;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAA2B,CAC5D,OAAkD,EAClD,EAAE;IACF,MAAM,eAAe,GAAiD,KAAK,EACzE,OAAO,EACP,EAAE,QAAQ,EAAE,eAAe,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,EACrD,EAAE;QACF,MAAM,EACJ,QAAQ,EAAE,WAAW,EACrB,UAAU,GAAG,eAAe,EAC5B,aAAa,GAAG,cAAc,EAC9B,cAAc,GAAG,cAAc,EAC/B,qBAAqB,GAAG,EAAE,GAC3B,GAA8C,OAAO,CAAA;QAEtD,MAAM,Q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AG,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,CAAA;IACpE,CAAC;IACD,OAAO,aAAa,CAAA;AACtB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,WAAW,CAClB,QAAsD,EACtD,OAAgB;IAEhB,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;IAChE,CAAC;IAED,OAAO,OAAO,QAAQ,KAAK,UAAU;QACnC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QACvB,CAAC,CAAC,WAAW,CAAC,QAAQ,EAAE,OAAkC,CAAC,CAAA;AAC/D,CAAC","sourcesContent":["import type { RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\nimport type {\n EndpointAdapterFactory,\n EndpointAdapterOptions,\n ExtendableEndpointAdapter,\n} from './types'\nimport { deepMerge } from '@empathyco/x-utils'\nimport { fetchHttpClient } from '../http-clients/fetch.http-client'\nimport { identityMapper } from '../mappers/identity.mapper'\nimport { interpolate } from '../utils/interpolate'\n\n/**\n * Factory to create {@link ExtendableEndpointAdapter | endpoint adapters} with the given\n * {@link EndpointAdapterOptions | options}.\n *\n * @param options - The {@link EndpointAdapterOptions | options} to create a new\n * {@link ExtendableEndpointAdapter} with.\n *\n * @returns A brand new {@link ExtendableEndpointAdapter} object.\n * @public\n */\nexport const endpointAdapterFactory: EndpointAdapterFactory = <Request, Response>(\n options: EndpointAdapterOptions<Request, Response>,\n) => {\n const endpointAdapter: ExtendableEndpointAdapter<Request, Response> = async (\n request,\n { endpoint: requestEndpoint, ...requestOptions } = {},\n ) => {\n const {\n endpoint: rawEndpoint,\n httpClient = fetchHttpClient,\n requestMapper = identityMapper,\n responseMapper = identityMapper,\n defaultRequestOptions = {},\n }: EndpointAdapterOptions<Request, Response> = options\n\n const endpoint = getEndpoint(requestEndpoint ?? rawEndpoint, request)\n const requestParameters = requestMapper(request, { endpoint })\n\n return httpClient(\n endpoint,\n mergeRequestOptions(defaultRequestOptions, requestOptions, { parameters: requestParameters }),\n ).then(response => responseMapper(response, { endpoint, requestParameters }))\n }\n\n endpointAdapter.extends = <NewRequest, NewResponse>(\n extendedOptions: Partial<EndpointAdapterOptions<NewRequest, NewResponse>>,\n ) =>\n endpointAdapterFactory<NewRequest, NewResponse>({\n ...options,\n ...extendedOptions,\n } as EndpointAdapterOptions<NewRequest, NewResponse>)\n\n return endpointAdapter\n}\n\n/**\n * Deep merges the request options. The `signal` of the request properties is kept by reference,\n * as a deep merge would clone the `AbortSignal` into a plain object. The last signal wins.\n *\n * @param options - The request options to merge, from lower to higher priority.\n *\n * @returns The merged request options.\n * @internal\n */\nfunction mergeRequestOptions(\n ...options: Omit<RequestOptions, 'endpoint'>[]\n): Omit<RequestOptions, 'endpoint'> {\n let signal: AbortSignal | null | undefined\n const optionsWithoutSignal = options.map(option => {\n if (option.properties?.signal === undefined) {\n return option\n }\n const { signal: optionSignal, ...properties } = option.properties\n signal = optionSignal\n return { ...option, properties }\n })\n const mergedOptions: Omit<RequestOptions, 'endpoint'> = deepMerge({}, ...optionsWithoutSignal)\n if (signal !== undefined) {\n mergedOptions.properties = { ...mergedOptions.properties, signal }\n }\n return mergedOptions\n}\n\n/**\n * Returns an endpoint.\n *\n * @param endpoint - The endpoint to process.\n * @param request - The request object.\n *\n * @returns The endpoint.\n * @internal\n */\nfunction getEndpoint<Request>(\n endpoint: string | Mapper<Request, string> | undefined,\n request: Request,\n): string {\n if (!endpoint) {\n throw new Error('Tried to make a request without an endpoint')\n }\n\n return typeof endpoint === 'function'\n ? endpoint(request, {})\n : interpolate(endpoint, request as Record<string, unknown>)\n}\n"]}
|
|
@@ -4,6 +4,8 @@ import { buildUrl, toJson } from './utils';
|
|
|
4
4
|
* The `fetchRawHttpClient()` function is a http client implementation using the `fetch` WebAPI.
|
|
5
5
|
*
|
|
6
6
|
* @remarks This fucntion will replace the current `fetchHttpClient` in a possible future breaking change.
|
|
7
|
+
* When the request properties have a `signal`, the caller owns the cancellation of the request, so
|
|
8
|
+
* it is not cancelled by id.
|
|
7
9
|
*
|
|
8
10
|
* @param endpoint - The endpoint to make the request to.
|
|
9
11
|
* @param options - The request options.
|
|
@@ -19,7 +21,7 @@ import { buildUrl, toJson } from './utils';
|
|
|
19
21
|
* @public
|
|
20
22
|
*/
|
|
21
23
|
export const fetchRawHttpClient = async (endpoint, { id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false, sendEmptyParams = false, } = {}) => {
|
|
22
|
-
const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
24
|
+
const signal = (properties === null || properties === void 0 ? void 0 : properties.signal) != null ? {} : cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
|
|
23
25
|
if (!sendEmptyParams) {
|
|
24
26
|
parameters = cleanEmpty(parameters);
|
|
25
27
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAE1C
|
|
1
|
+
{"version":3,"file":"fetch.http-client.js","sourceRoot":"","sources":["../../../src/http-clients/fetch.http-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAe,KAAK,EACjD,QAAQ,EACR,EACE,EAAE,GAAG,QAAQ,EACb,UAAU,GAAG,IAAI,EACjB,UAAU,GAAG,EAAE,EACf,UAAU,EACV,gBAAgB,GAAG,KAAK,EACxB,eAAe,GAAG,KAAK,GACx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O,uBAAuB,CAAC,EAAE,CAAC,CAAC,MAAM,CAAA;AAC3C,CAAC","sourcesContent":["import type { Dictionary } from '@empathyco/x-utils'\nimport type { HttpClient } from './types'\nimport { cleanEmpty, flatObject } from '@empathyco/x-utils'\nimport { buildUrl, toJson } from './utils'\n\n/**\n * The `fetchRawHttpClient()` function is a http client implementation using the `fetch` WebAPI.\n *\n * @remarks This fucntion will replace the current `fetchHttpClient` in a possible future breaking change.\n * When the request properties have a `signal`, the caller owns the cancellation of the request, so\n * it is not cancelled by id.\n *\n * @param endpoint - The endpoint to make the request to.\n * @param options - The request options.\n * @param options.cancelable - Cancelable option.\n * @param options.id - ID option.\n * @param options.parameters - Parameters option.\n * @param options.properties - Properties option.\n * @param options.sendEmptyParams - SendEmptyParams option.\n * @param options.sendParamsInBody - SendParamsInBody option.\n *\n * @returns A `Promise` object.\n *\n * @public\n */\nexport const fetchRawHttpClient: HttpClient = async (\n endpoint,\n {\n id = endpoint,\n cancelable = true,\n parameters = {},\n properties,\n sendParamsInBody = false,\n sendEmptyParams = false,\n } = {},\n) => {\n const signal =\n properties?.signal != null ? {} : cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {}\n if (!sendEmptyParams) {\n parameters = cleanEmpty(parameters)\n }\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 })\n}\n\n/**\n * The `fetchHttpClient()` function is wrapper of `fetchRawHttpClient()` function that parses\n * the response with `toJson` function.\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 = async (endpoint, options = {}) =>\n fetchRawHttpClient(endpoint, options).then(toJson)\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"]}
|
|
@@ -3,6 +3,8 @@ import type { HttpClient } from './types';
|
|
|
3
3
|
* The `fetchRawHttpClient()` function is a http client implementation using the `fetch` WebAPI.
|
|
4
4
|
*
|
|
5
5
|
* @remarks This fucntion will replace the current `fetchHttpClient` in a possible future breaking change.
|
|
6
|
+
* When the request properties have a `signal`, the caller owns the cancellation of the request, so
|
|
7
|
+
* it is not cancelled by id.
|
|
6
8
|
*
|
|
7
9
|
* @param endpoint - The endpoint to make the request to.
|
|
8
10
|
* @param options - The request options.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empathyco/x-adapter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "18.0.0",
|
|
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",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"prepublishOnly": "pnpm run build"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@empathyco/x-utils": "1.
|
|
47
|
+
"@empathyco/x-utils": "1.20.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@vitest/coverage-v8": "4.1.12",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"publishConfig": {
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "45e4d9262ab6ade110b37f0b0d4feb254d90ca12"
|
|
60
60
|
}
|