@empathyco/x-adapter 8.1.0-alpha.9 → 8.1.1
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/README.md +2 -2
- package/dist/cjs/endpoint-adapter/endpoint-adapter.factory.js +2 -2
- package/dist/cjs/endpoint-adapter/endpoint-adapter.factory.js.map +1 -1
- package/dist/cjs/endpoint-adapter/types.js.map +1 -1
- package/dist/cjs/http-clients/__mocks__/fetch.mock.js +3 -2
- package/dist/cjs/http-clients/__mocks__/fetch.mock.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/mappers/combine-mappers.js +2 -2
- package/dist/cjs/mappers/combine-mappers.js.map +1 -1
- package/dist/cjs/mappers/schema-mapper.factory.js +1 -2
- package/dist/cjs/mappers/schema-mapper.factory.js.map +1 -1
- package/dist/cjs/schemas/utils.js +2 -3
- package/dist/cjs/schemas/utils.js.map +1 -1
- package/dist/esm/endpoint-adapter/endpoint-adapter.factory.js +1 -1
- package/dist/esm/endpoint-adapter/endpoint-adapter.factory.js.map +1 -1
- package/dist/esm/endpoint-adapter/types.js.map +1 -1
- package/dist/esm/http-clients/__mocks__/fetch.mock.js +3 -2
- package/dist/esm/http-clients/__mocks__/fetch.mock.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/mappers/combine-mappers.js +1 -1
- package/dist/esm/mappers/combine-mappers.js.map +1 -1
- package/dist/esm/mappers/schema-mapper.factory.js +1 -2
- package/dist/esm/mappers/schema-mapper.factory.js.map +1 -1
- package/dist/esm/schemas/utils.js +1 -2
- package/dist/esm/schemas/utils.js.map +1 -1
- package/dist/types/http-clients/__mocks__/fetch.mock.d.ts +3 -3
- package/package.json +13 -15
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Some features that it provides:
|
|
|
14
14
|
## Tech Stack
|
|
15
15
|
|
|
16
16
|
[](https://www.typescriptlang.org/)
|
|
17
|
-
[](https://vitest.dev/)
|
|
18
18
|
|
|
19
19
|
<br>
|
|
20
20
|
|
|
@@ -668,7 +668,7 @@ needs.
|
|
|
668
668
|
|
|
669
669
|
## Test
|
|
670
670
|
|
|
671
|
-
**Empathy Adapter** features are tested using [
|
|
671
|
+
**Empathy Adapter** features are tested using [Vitest](https://vitest.dev/). You will find a
|
|
672
672
|
`__tests__` folder inside each of the project's sections.
|
|
673
673
|
|
|
674
674
|
```
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.endpointAdapterFactory = void 0;
|
|
4
|
-
const
|
|
4
|
+
const x_utils_1 = require("@empathyco/x-utils");
|
|
5
5
|
const fetch_http_client_1 = require("../http-clients/fetch.http-client");
|
|
6
6
|
const identity_mapper_1 = require("../mappers/identity.mapper");
|
|
7
7
|
const interpolate_1 = require("../utils/interpolate");
|
|
@@ -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, (0,
|
|
23
|
+
return httpClient(endpoint, (0, x_utils_1.deepMerge)({}, defaultRequestOptions, requestOptions, { parameters: requestParameters })).then(response => responseMapper(response, { endpoint, requestParameters }));
|
|
24
24
|
};
|
|
25
25
|
endpointAdapter.extends = (extendedOptions) => (0, exports.endpointAdapterFactory)({
|
|
26
26
|
...options,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":";;;AAMA,
|
|
1
|
+
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":";;;AAMA,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,GAAG,mCAAe,EAC5B,aAAa,GAAG,gCAAc,EAC9B,cAAc,GAAG,gCAAc,EAC/B,qBAAqB,GAAG,EAAE,GAC3B,GAA8C,OAAO,CAAA;QAEtD,MAAM,QAAQ,GAAG,WAAW,CAAC,eAAe,aAAf,eAAe,cAAf,eAAe,GAAI,WAAW,EAAE,OAAO,CAAC,CAAA;QACrE,MAAM,iBAAiB,GAAG,aAAa,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAA;QAE9D,OAAO,UAAU,CACf,QAAQ,EACR,IAAA,mBAAS,EAAC,EAAE,EAAE,qBAAqB,EAAE,cAAc,EAAE,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAC,CACxF,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,cAAc,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAC,CAAC,CAAA;IAC/E,CAAC,CAAA;IAED,eAAe,CAAC,OAAO,GAAG,CACxB,eAAyE,EACzE,EAAE,CACF,IAAA,8BAAsB,EAA0B;QAC9C,GAAG,OAAO;QACV,GAAG,eAAe;KACgC,CAAC,CAAA;IAEvD,OAAO,eAAe,CAAA;AACxB,CAAC,CAAA;AAjCY,QAAA,sBAAsB,0BAiClC;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 { 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 deepMerge({}, 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 * 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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { HttpClient, RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\n\n/**\n * Connects with a given API endpoint. Transforms the request object into something the API\n * can understand, makes the request, and transforms back the API response into the desired shape.\n *\n * @public\n */\nexport interface EndpointAdapter<Request, Response> {\n /**\n * Triggers a request with the given data.\n *\n * @param request - The initial source request object.\n * @param options - Additional options to make the request with.\n * @returns A response promise.\n */\n (request: Request, options?: RequestOptions): Promise<Response>\n}\n\n/**\n * Adds extends functionality to an {@link EndpointAdapter}.\n *\n * @public\n */\nexport interface ExtendableEndpointAdapter<Request, Response
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { HttpClient, RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\n\n/**\n * Connects with a given API endpoint. Transforms the request object into something the API\n * can understand, makes the request, and transforms back the API response into the desired shape.\n *\n * @public\n */\nexport interface EndpointAdapter<Request, Response> {\n /**\n * Triggers a request with the given data.\n *\n * @param request - The initial source request object.\n * @param options - Additional options to make the request with.\n * @returns A response promise.\n */\n (request: Request, options?: RequestOptions): Promise<Response>\n}\n\n/**\n * Adds extends functionality to an {@link EndpointAdapter}.\n *\n * @public\n */\nexport interface ExtendableEndpointAdapter<Request, Response> extends EndpointAdapter<\n Request,\n Response\n> {\n /**\n * Extends the endpoint adapter, merging its options with the new ones, creating a new\n * {@link ExtendableEndpointAdapter} object.\n *\n * @param options - New options to extend the {@link ExtendableEndpointAdapter} with.\n * @returns A new adapter created by merging the new and old options.\n */\n extends: <NewRequest = Request, NewResponse = Response>(\n options: Partial<EndpointAdapterOptions<NewRequest, NewResponse>>,\n ) => ExtendableEndpointAdapter<NewRequest, NewResponse>\n}\n\n/**\n * Creates an {@link ExtendableEndpointAdapter} with the given options.\n *\n * @param options - Options to create the endpoint adapter with.\n * @returns A brand-new {@link ExtendableEndpointAdapter} instance.\n * @public\n */\nexport type EndpointAdapterFactory = <Request, Response>(\n options: EndpointAdapterOptions<Request, Response>,\n) => ExtendableEndpointAdapter<Request, Response>\n\n/**\n * Options to create an adapter with.\n *\n * @public\n */\nexport interface EndpointAdapterOptions<Request, Response> {\n /**\n * The endpoint to request the information to. If the endpoint can only be decided at run-time you\n * can ignore this property and use {@link RequestOptions.endpoint} parameter.\n */\n endpoint?: string | Mapper<Request, string>\n /**\n * The {@link HttpClient} to use for the requests.\n */\n httpClient?: HttpClient\n /**\n * The default {@link RequestOptions} to use every request.\n */\n defaultRequestOptions?: Omit<RequestOptions, 'endpoint'>\n /**\n * The {@link Mapper} in charge of adapting the request object so the backend API can\n * understand it.\n */\n requestMapper?: Mapper<Request, Record<string, any>>\n /**\n * The {@link Mapper} in charge of adapting the response object to whatever format or\n * shape it is needed.\n */\n responseMapper?: Mapper<any, Response>\n}\n"]}
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.koFetchMock = exports.okFetchMock = void 0;
|
|
4
4
|
exports.fetchMock = fetchMock;
|
|
5
|
-
|
|
6
|
-
exports.
|
|
5
|
+
const vitest_1 = require("vitest");
|
|
6
|
+
exports.okFetchMock = vitest_1.vi.fn(fetchMock({}));
|
|
7
|
+
exports.koFetchMock = vitest_1.vi.fn(async () => Promise.resolve({
|
|
7
8
|
ok: false,
|
|
8
9
|
json: async () => Promise.resolve({}),
|
|
9
10
|
status: 500,
|
|
@@ -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":";;;AAmBA,8BAmBC;AAtCD,mCAA2B;AACd,QAAA,WAAW,GAAG,WAAE,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAA;AAElC,QAAA,WAAW,GAAG,WAAE,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE,CAC1C,OAAO,CAAC,OAAO,CAAC;IACd,EAAE,EAAE,KAAK;IACT,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACrC,MAAM,EAAE,GAAG;IACX,UAAU,EAAE,kBAAkB;CAC/B,CAAC,CACH,CAAA;AACD;;;;;;;GAOG;AACH,SAAgB,SAAS,CACvB,QAAiB;IAEjB,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QAChC,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,CAAC;oBACpB,MAAM,CAAC,IAAI,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAA;gBACnD,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC;wBACN,EAAE,EAAE,IAAI;wBACR,MAAM,EAAE,GAAG;wBACX,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;wBAC3C,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;qBAChD,CAAC,CAAA;gBAChB,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC","sourcesContent":["import { vi } from 'vitest'\nexport const okFetchMock = vi.fn(fetchMock({}))\n\nexport const koFetchMock = vi.fn(async () =>\n Promise.resolve({\n ok: false,\n json: async () => 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 async (_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: async () => Promise.resolve(response),\n text: async () => Promise.resolve(JSON.stringify(response)),\n } as Response)\n }\n })\n })\n }\n}\n"]}
|
package/dist/cjs/index.js
CHANGED
|
@@ -6,4 +6,5 @@ tslib_1.__exportStar(require("./http-clients"), exports);
|
|
|
6
6
|
tslib_1.__exportStar(require("./mappers"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./schemas"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./utils"), exports);
|
|
9
|
+
// comment only for release testing purposes
|
|
9
10
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAkC;AAClC,yDAA8B;AAC9B,oDAAyB;AACzB,oDAAyB;AACzB,kDAAuB","sourcesContent":["export * from './endpoint-adapter'\nexport * from './http-clients'\nexport * from './mappers'\nexport * from './schemas'\nexport * from './utils'\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAkC;AAClC,yDAA8B;AAC9B,oDAAyB;AACzB,oDAAyB;AACzB,kDAAuB;AACvB,4CAA4C","sourcesContent":["export * from './endpoint-adapter'\nexport * from './http-clients'\nexport * from './mappers'\nexport * from './schemas'\nexport * from './utils'\n// comment only for release testing purposes\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.combineMappers = combineMappers;
|
|
4
|
-
const
|
|
4
|
+
const x_utils_1 = require("@empathyco/x-utils");
|
|
5
5
|
/**
|
|
6
6
|
* Combines the given {@link Mapper | mappers} into a single {@link Mapper | mapper function}.
|
|
7
7
|
*
|
|
@@ -13,7 +13,7 @@ const x_deep_merge_1 = require("@empathyco/x-deep-merge");
|
|
|
13
13
|
function combineMappers(...mappers) {
|
|
14
14
|
return (from, context) => mappers.reduce((value, mapper) => {
|
|
15
15
|
const mappedValue = mapper(from, context);
|
|
16
|
-
context.mappedValue = (0,
|
|
16
|
+
context.mappedValue = (0, x_utils_1.deepMerge)(value, mappedValue);
|
|
17
17
|
return value;
|
|
18
18
|
}, {});
|
|
19
19
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"combine-mappers.js","sourceRoot":"","sources":["../../../src/mappers/combine-mappers.ts"],"names":[],"mappings":";;AAYA,wCASC;AAnBD,
|
|
1
|
+
{"version":3,"file":"combine-mappers.js","sourceRoot":"","sources":["../../../src/mappers/combine-mappers.ts"],"names":[],"mappings":";;AAYA,wCASC;AAnBD,gDAA8C;AAE9C;;;;;;;GAOG;AACH,SAAgB,cAAc,CAC5B,GAAG,OAAwC;IAE3C,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QAC/B,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QACzC,OAAO,CAAC,WAAW,GAAG,IAAA,mBAAS,EAAC,KAAK,EAAE,WAAW,CAAC,CAAA;QACnD,OAAO,KAAK,CAAA;IACd,CAAC,EAAE,EAAE,CAAO,CAAA;AAChB,CAAC","sourcesContent":["import type { DeepPartial } from '@empathyco/x-utils'\nimport type { Mapper } from './types'\nimport { deepMerge } from '@empathyco/x-utils'\n\n/**\n * Combines the given {@link Mapper | mappers} into a single {@link Mapper | mapper function}.\n *\n * @param mappers - The {@link Mapper | mapper functions} to combine.\n * @returns The combined {@link Mapper | mapper function}.\n *\n * @public\n */\nexport function combineMappers<From, To>(\n ...mappers: Mapper<From, DeepPartial<To>>[]\n): Mapper<From, To> {\n return (from, context) =>\n mappers.reduce((value, mapper) => {\n const mappedValue = mapper(from, context)\n context.mappedValue = deepMerge(value, mappedValue)\n return value\n }, {}) as To\n}\n"]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.schemaMapperFactory = schemaMapperFactory;
|
|
4
|
-
const x_deep_merge_1 = require("@empathyco/x-deep-merge");
|
|
5
4
|
const x_utils_1 = require("@empathyco/x-utils");
|
|
6
5
|
const utils_1 = require("../schemas/utils");
|
|
7
6
|
/**
|
|
@@ -81,7 +80,7 @@ function applySubSchemaTransformer(source, { $subSchema, $path, $context }, rawC
|
|
|
81
80
|
: (0, x_utils_1.getSafePropertyChain)(source, value);
|
|
82
81
|
});
|
|
83
82
|
}
|
|
84
|
-
const context = (0,
|
|
83
|
+
const context = (0, x_utils_1.deepMerge)({}, rawContext, $context, extendedContext);
|
|
85
84
|
let subSchema;
|
|
86
85
|
if ($subSchema === '$self') {
|
|
87
86
|
subSchema = schema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-mapper.factory.js","sourceRoot":"","sources":["../../../src/mappers/schema-mapper.factory.ts"],"names":[],"mappings":";;AAsBA,kDAMC;AAzBD,
|
|
1
|
+
{"version":3,"file":"schema-mapper.factory.js","sourceRoot":"","sources":["../../../src/mappers/schema-mapper.factory.ts"],"names":[],"mappings":";;AAsBA,kDAMC;AAzBD,gDAQ2B;AAC3B,4CAAwE;AAExE;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CACjC,MAA8D;IAE9D,OAAO,SAAS,MAAM,CAAC,MAAc,EAAE,OAAsB;QAC3D,OAAO,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAA;IAC3C,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,SAAS,CAChB,MAAc,EACd,MAA8B,EAC9B,OAAsB;IAEtB,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,CAAC,IAAI,CAAC,+BAA+B,EAAE,IAAA,2BAAmB,EAAC,MAAM,CAAC,CAAC,CAAA;QAC1E,OAAO,SAAgB,CAAA;IACzB,CAAC;IACD,OAAO,IAAA,gBAAM,EACX,MAAM,EACN,CAAC,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAE3B,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,IAAA,gBAAM,EAAC,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC;YACnE,MAAM,CAAC,GAAG,CAAC,GAAG,IAAA,8BAAoB,EAAC,MAAM,EAAE,WAAW,CAAc,CAAA;QACtE,CAAC;aAAM,IAAI,IAAA,oBAAU,EAAC,WAAW,CAAC,IAAI,CAAC,IAAA,wBAAgB,EAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;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AE,KAAmC,CAAC,CAAA;QACvE,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,IAAA,mBAAS,EAAC,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAA;IACpE,IAAI,SAA4C,CAAA;IAChD,IAAI,UAAU,KAAK,OAAO,EAAE,CAAC;QAC3B,SAAS,GAAG,MAAM,CAAA;IACpB,CAAC;SAAM,IAAI,IAAA,oBAAU,EAAC,UAAU,CAAC,EAAE,CAAC;QAClC,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAChC,CAAC;SAAM,CAAC;QACN,SAAS,GAAG,UAAU,CAAA;IACxB,CAAC;IACD,OAAO,IAAA,iBAAO,EAAC,SAAS,CAAC;QACvB,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,CAAW,CAAC;QACtE,CAAC,CAAC,SAAS,CACP,SAAS,EACT,SAA6C,EAC7C,OAAO,CACR,CAAA;AACP,CAAC","sourcesContent":["import type { Dictionary, ExtractPath } from '@empathyco/x-utils'\nimport type { MutableSchema, Schema, SubSchemaTransformer } from '../schemas/types'\nimport type { Mapper, MapperContext } from './types'\nimport {\n deepMerge,\n getSafePropertyChain,\n isArray,\n isFunction,\n isObject,\n isPath,\n reduce,\n} from '@empathyco/x-utils'\nimport { createMutableSchema, isInternalMethod } from '../schemas/utils'\n\n/**\n * The 'schemaMapperFactory' function creates a {@link Mapper | mapper function} for a given\n * {@link Schema | schema}.\n *\n * @param schema - The {@link Schema | schema} to apply in the {@link Mapper | mapper function}.\n * @returns A {@link Mapper | mapper function} that applies the given {@link Schema | schema}.\n * @public\n */\nexport function schemaMapperFactory<Source, Target>(\n schema: Schema<Source, Target> | MutableSchema<Source, Target>,\n): Mapper<Source, Target> {\n return function mapper(source: Source, context: MapperContext): Target {\n return mapSchema(source, schema, context)\n }\n}\n\n/**\n * The `mapSchema()` function creates a new object populated with the transformations defined by a\n * {@link Schema} applied to a source object.\n *\n * @param source - The object to apply the transformations to.\n * @param schema - The object that defines the transformations to apply.\n * @param context - The {@link MapperContext | mapper context} to feed the transformations with.\n * @returns A new object with each element being the result of the applied transformation.\n * @internal\n */\nfunction mapSchema<Source, Target>(\n source: Source,\n schema: Schema<Source, Target>,\n context: MapperContext,\n): Target {\n if (!source) {\n console.warn('This schema cannot be applied', createMutableSchema(schema))\n return undefined as any\n }\n return reduce(\n schema,\n (target, key, transformer) => {\n type TargetKey = Target[keyof Target]\n if (typeof transformer === 'string' && isPath(source, transformer)) {\n target[key] = getSafePropertyChain(source, transformer) as TargetKey\n } else if (isFunction(transformer) && !isInternalMethod(transformer.name)) {\n target[key] = transformer(source, context)\n } else if (isObject(transformer)) {\n const value =\n '$subSchema' in transformer\n ? (applySubSchemaTransformer<Source, TargetKey>(\n source,\n transformer as SubSchemaTransformer<Source, TargetKey>,\n context,\n schema as unknown as Schema<Source, TargetKey>,\n ) as TargetKey)\n : mapSchema<Source, TargetKey>(\n source,\n transformer as Schema<Source, TargetKey>,\n context,\n )\n\n if (value) {\n target[key] = value\n }\n }\n return target\n },\n {} as Target,\n )\n}\n\n/**\n * The `applySubSchemaTransformer()` function executes a `mapSchema()` function applying the defined\n * {@link SubSchemaTransformer.$subSchema}.\n *\n * @param source - The object to feed the schema.\n * @param subSchemaTransformer - The {@link SubSchemaTransformer} object with a $path, $subSchema\n * and $context options.\n * @param subSchemaTransformer.$path\n * @param subSchemaTransformer.$subSchema\n * @param subSchemaTransformer.$context\n * @param rawContext - The {@link MapperContext | mapper context} to feed the mapSchema function.\n * @param schema - The {@link Schema} to apply.\n * @returns The result of calling `mapSchema()` with the source, schema and context arguments.\n * @internal\n */\nfunction applySubSchemaTransformer<Source, Target>(\n source: Source,\n { $subSchema, $path, $context }: SubSchemaTransformer<Source, Target>,\n rawContext: MapperContext,\n schema: Schema<Source, Target>,\n): Target | Target[] | undefined {\n const subSource = getSafePropertyChain(source, $path)\n\n if (!subSource) {\n return\n }\n\n const extendedContext: Dictionary = {}\n if ($context) {\n Object.entries($context).forEach(([key, value]) => {\n if (['requestParameters', 'endpoint', 'mappedValue'].includes(key)) {\n return\n }\n extendedContext[key] = isFunction(value)\n ? value(source)\n : getSafePropertyChain(source, value as ExtractPath<typeof source>)\n })\n }\n\n const context = deepMerge({}, rawContext, $context, extendedContext)\n let subSchema: typeof $subSchema | typeof schema\n if ($subSchema === '$self') {\n subSchema = schema\n } else if (isFunction($subSchema)) {\n subSchema = $subSchema(source)\n } else {\n subSchema = $subSchema\n }\n return isArray(subSource)\n ? subSource.map(item => mapSchema(item, subSchema, context) as Target)\n : mapSchema<typeof subSource, Target>(\n subSource,\n subSchema as Schema<typeof subSource, Target>,\n context,\n )\n}\n"]}
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createMutableSchema = createMutableSchema;
|
|
4
4
|
exports.isInternalMethod = isInternalMethod;
|
|
5
|
-
const x_deep_merge_1 = require("@empathyco/x-deep-merge");
|
|
6
5
|
const x_utils_1 = require("@empathyco/x-utils");
|
|
7
6
|
/**
|
|
8
7
|
* Collection of internal method names for {@link MutableSchema | mutable schemas}.
|
|
@@ -33,10 +32,10 @@ function createMutableSchema(schema) {
|
|
|
33
32
|
return this;
|
|
34
33
|
},
|
|
35
34
|
$override(newSchema) {
|
|
36
|
-
return (0,
|
|
35
|
+
return (0, x_utils_1.deepMerge)(this, newSchema);
|
|
37
36
|
},
|
|
38
37
|
$extends(newSchema) {
|
|
39
|
-
return (0,
|
|
38
|
+
return (0, x_utils_1.deepMerge)({}, this, newSchema);
|
|
40
39
|
},
|
|
41
40
|
toString(includeInternalMethods = false) {
|
|
42
41
|
return serialize(this, !!includeInternalMethods);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/schemas/utils.ts"],"names":[],"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,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACrC,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,IAAA,iBAAO,EAAC,IAAI,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;QAC3B,IAAI,IAAA,kBAAQ,EAAC,KAAK,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,GAAG,WAAW,GAAG,GAAG,QAAQ,SAAS,CAC7C,KAAK,EACL,sBAAsB,EACtB,EAAE,IAAI,CACP,GAAG,WAAW,MAAM,CAAA;QACvB,CAAC;aAAM,IAAI,CAAC,IAAA,oBAAU,EAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,sBAAsB,EAAE,CAAC;YAClF,MAAM,IAAI,GAAG,WAAW,GAAG,GAAG,KAAK,KAAY,KAAK,CAAA;QACtD,CAAC;IACH,CAAC,CAAC,CAAA;IACF,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import type { MutableSchema, Schema } from './types'\nimport { deepMerge, forEach, isFunction, isObject } from '@empathyco/x-utils'\n\n/**\n * Collection of internal method names for {@link MutableSchema | mutable schemas}.\n */\nconst mutableSchemasInternalMethods: string[] = ['$replace', '$override', '$extends', 'toString']\n\n/**\n * Creates a {@link MutableSchema | mutable schema } version of a given {@link Schema | schema}.\n *\n * @param schema - The {@link Schema | schema} to make mutable.\n *\n * @returns A {@link MutableSchema | mutable schema} version of the given {@link Schema | schema}.\n *\n * @public\n */\nexport function createMutableSchema<Source, Target>(\n schema: Schema<Source, Target>,\n): MutableSchema<Source, Target> {\n return {\n ...schema,\n $replace(newSchema) {\n forEach(this, key => {\n if (isInternalMethod(key as string)) {\n return\n }\n delete this[key]\n })\n Object.assign(this, newSchema)\n /* We are replacing the schema with a completely new schema , so it makes sense that TS\n complains that the old schema and the new one are not the same. */\n return this as any\n },\n $override(newSchema) {\n return deepMerge(this, newSchema)\n },\n $extends(newSchema: unknown) {\n return deepMerge({}, this, newSchema)\n },\n toString(includeInternalMethods = false) {\n return serialize(this, !!includeInternalMethods)\n },\n }\n}\n\n/**\n * Checks if the given key is a {@link MutableSchema | mutableSchema} method.\n *\n * @param name - The key to check.\n *\n * @returns True if it is a {@link MutableSchema | mutableSchema} method,\n * false otherwise.\n *\n * @public\n */\nexport function isInternalMethod(name: string): boolean {\n return mutableSchemasInternalMethods.includes(name)\n}\n\n/**\n * Returns a string representing of the given object.\n *\n * @param data - The object to get the string representation from.\n * @param includeInternalMethods - Flag to include in the string representation\n * the internal methods. Disabled by default.\n * @param deep - The level of indentation.\n * @returns The string representation.\n */\nfunction serialize(\n data: Record<string, unknown>,\n includeInternalMethods: boolean,\n deep = 0,\n): string {\n const indentation = ' '.repeat(deep)\n let output = ''\n forEach(data, (key, value) => {\n if (isObject(value)) {\n output += `${indentation}${key}: {\\n${serialize(\n value,\n includeInternalMethods,\n ++deep,\n )}${indentation}},\\n`\n } else if (!isFunction(value) || !isInternalMethod(key) || includeInternalMethods) {\n output += `${indentation}${key}: ${value as any},\\n`\n }\n })\n return output\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { deepMerge } from '@empathyco/x-
|
|
1
|
+
import { deepMerge } from '@empathyco/x-utils';
|
|
2
2
|
import { fetchHttpClient } from '../http-clients/fetch.http-client';
|
|
3
3
|
import { identityMapper } from '../mappers/identity.mapper';
|
|
4
4
|
import { interpolate } from '../utils/interpolate';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"endpoint-adapter.factory.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/endpoint-adapter.factory.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,eAAe,E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,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 { 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 deepMerge({}, 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 * 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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { HttpClient, RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\n\n/**\n * Connects with a given API endpoint. Transforms the request object into something the API\n * can understand, makes the request, and transforms back the API response into the desired shape.\n *\n * @public\n */\nexport interface EndpointAdapter<Request, Response> {\n /**\n * Triggers a request with the given data.\n *\n * @param request - The initial source request object.\n * @param options - Additional options to make the request with.\n * @returns A response promise.\n */\n (request: Request, options?: RequestOptions): Promise<Response>\n}\n\n/**\n * Adds extends functionality to an {@link EndpointAdapter}.\n *\n * @public\n */\nexport interface ExtendableEndpointAdapter<Request, Response
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/endpoint-adapter/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { HttpClient, RequestOptions } from '../http-clients/types'\nimport type { Mapper } from '../mappers/types'\n\n/**\n * Connects with a given API endpoint. Transforms the request object into something the API\n * can understand, makes the request, and transforms back the API response into the desired shape.\n *\n * @public\n */\nexport interface EndpointAdapter<Request, Response> {\n /**\n * Triggers a request with the given data.\n *\n * @param request - The initial source request object.\n * @param options - Additional options to make the request with.\n * @returns A response promise.\n */\n (request: Request, options?: RequestOptions): Promise<Response>\n}\n\n/**\n * Adds extends functionality to an {@link EndpointAdapter}.\n *\n * @public\n */\nexport interface ExtendableEndpointAdapter<Request, Response> extends EndpointAdapter<\n Request,\n Response\n> {\n /**\n * Extends the endpoint adapter, merging its options with the new ones, creating a new\n * {@link ExtendableEndpointAdapter} object.\n *\n * @param options - New options to extend the {@link ExtendableEndpointAdapter} with.\n * @returns A new adapter created by merging the new and old options.\n */\n extends: <NewRequest = Request, NewResponse = Response>(\n options: Partial<EndpointAdapterOptions<NewRequest, NewResponse>>,\n ) => ExtendableEndpointAdapter<NewRequest, NewResponse>\n}\n\n/**\n * Creates an {@link ExtendableEndpointAdapter} with the given options.\n *\n * @param options - Options to create the endpoint adapter with.\n * @returns A brand-new {@link ExtendableEndpointAdapter} instance.\n * @public\n */\nexport type EndpointAdapterFactory = <Request, Response>(\n options: EndpointAdapterOptions<Request, Response>,\n) => ExtendableEndpointAdapter<Request, Response>\n\n/**\n * Options to create an adapter with.\n *\n * @public\n */\nexport interface EndpointAdapterOptions<Request, Response> {\n /**\n * The endpoint to request the information to. If the endpoint can only be decided at run-time you\n * can ignore this property and use {@link RequestOptions.endpoint} parameter.\n */\n endpoint?: string | Mapper<Request, string>\n /**\n * The {@link HttpClient} to use for the requests.\n */\n httpClient?: HttpClient\n /**\n * The default {@link RequestOptions} to use every request.\n */\n defaultRequestOptions?: Omit<RequestOptions, 'endpoint'>\n /**\n * The {@link Mapper} in charge of adapting the request object so the backend API can\n * understand it.\n */\n requestMapper?: Mapper<Request, Record<string, any>>\n /**\n * The {@link Mapper} in charge of adapting the response object to whatever format or\n * shape it is needed.\n */\n responseMapper?: Mapper<any, Response>\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export const
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
export const okFetchMock = vi.fn(fetchMock({}));
|
|
3
|
+
export const koFetchMock = vi.fn(async () => Promise.resolve({
|
|
3
4
|
ok: false,
|
|
4
5
|
json: async () => Promise.resolve({}),
|
|
5
6
|
status: 500,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,
|
|
1
|
+
{"version":3,"file":"fetch.mock.js","sourceRoot":"","sources":["../../../../src/http-clients/__mocks__/fetch.mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC3B,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAA;AAE/C,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE,CAC1C,OAAO,CAAC,OAAO,CAAC;IACd,EAAE,EAAE,KAAK;IACT,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACrC,MAAM,EAAE,GAAG;IACX,UAAU,EAAE,kBAAkB;CAC/B,CAAC,CACH,CAAA;AACD;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CACvB,QAAiB;IAEjB,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QAChC,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,CAAC;oBACpB,MAAM,CAAC,IAAI,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAA;gBACnD,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC;wBACN,EAAE,EAAE,IAAI;wBACR,MAAM,EAAE,GAAG;wBACX,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;wBAC3C,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;qBAChD,CAAC,CAAA;gBAChB,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC","sourcesContent":["import { vi } from 'vitest'\nexport const okFetchMock = vi.fn(fetchMock({}))\n\nexport const koFetchMock = vi.fn(async () =>\n Promise.resolve({\n ok: false,\n json: async () => 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 async (_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: async () => Promise.resolve(response),\n text: async () => Promise.resolve(JSON.stringify(response)),\n } as Response)\n }\n })\n })\n }\n}\n"]}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA","sourcesContent":["export * from './endpoint-adapter'\nexport * from './http-clients'\nexport * from './mappers'\nexport * from './schemas'\nexport * from './utils'\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA;AACvB,4CAA4C","sourcesContent":["export * from './endpoint-adapter'\nexport * from './http-clients'\nexport * from './mappers'\nexport * from './schemas'\nexport * from './utils'\n// comment only for release testing purposes\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"combine-mappers.js","sourceRoot":"","sources":["../../../src/mappers/combine-mappers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"combine-mappers.js","sourceRoot":"","sources":["../../../src/mappers/combine-mappers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAE9C;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC5B,GAAG,OAAwC;IAE3C,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QAC/B,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QACzC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,CAAA;QACnD,OAAO,KAAK,CAAA;IACd,CAAC,EAAE,EAAE,CAAO,CAAA;AAChB,CAAC","sourcesContent":["import type { DeepPartial } from '@empathyco/x-utils'\nimport type { Mapper } from './types'\nimport { deepMerge } from '@empathyco/x-utils'\n\n/**\n * Combines the given {@link Mapper | mappers} into a single {@link Mapper | mapper function}.\n *\n * @param mappers - The {@link Mapper | mapper functions} to combine.\n * @returns The combined {@link Mapper | mapper function}.\n *\n * @public\n */\nexport function combineMappers<From, To>(\n ...mappers: Mapper<From, DeepPartial<To>>[]\n): Mapper<From, To> {\n return (from, context) =>\n mappers.reduce((value, mapper) => {\n const mappedValue = mapper(from, context)\n context.mappedValue = deepMerge(value, mappedValue)\n return value\n }, {}) as To\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { deepMerge } from '@empathyco/x-
|
|
2
|
-
import { getSafePropertyChain, isArray, isFunction, isObject, isPath, reduce, } from '@empathyco/x-utils';
|
|
1
|
+
import { deepMerge, getSafePropertyChain, isArray, isFunction, isObject, isPath, reduce, } from '@empathyco/x-utils';
|
|
3
2
|
import { createMutableSchema, isInternalMethod } from '../schemas/utils';
|
|
4
3
|
/**
|
|
5
4
|
* The 'schemaMapperFactory' function creates a {@link Mapper | mapper function} for a given
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-mapper.factory.js","sourceRoot":"","sources":["../../../src/mappers/schema-mapper.factory.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"schema-mapper.factory.js","sourceRoot":"","sources":["../../../src/mappers/schema-mapper.factory.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,OAAO,EACP,UAAU,EACV,QAAQ,EACR,MAAM,EACN,MAAM,GACP,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAExE;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,MAA8D;IAE9D,OAAO,SAAS,MAAM,CAAC,MAAc,EAAE,OAAsB;QAC3D,OAAO,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAA;IAC3C,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,SAAS,CAChB,MAAc,EACd,MAA8B,EAC9B,OAAsB;IAEtB,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,CAAC,IAAI,CAAC,+BAA+B,EAAE,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAA;QAC1E,OAAO,SAAgB,CAAA;IACzB,CAAC;IACD,OAAO,MAAM,CACX,MAAM,EACN,CAAC,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAE3B,IAA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AAmB,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBACnE,OAAM;YACR,CAAC;YACD,eAAe,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC;gBACtC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;gBACf,CAAC,CAAC,oBAAoB,CAAC,MAAM,EAAE,KAAmC,CAAC,CAAA;QACvE,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,SAAS,CAAC,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAA;IACpE,IAAI,SAA4C,CAAA;IAChD,IAAI,UAAU,KAAK,OAAO,EAAE,CAAC;QAC3B,SAAS,GAAG,MAAM,CAAA;IACpB,CAAC;SAAM,IAAI,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAClC,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAChC,CAAC;SAAM,CAAC;QACN,SAAS,GAAG,UAAU,CAAA;IACxB,CAAC;IACD,OAAO,OAAO,CAAC,SAAS,CAAC;QACvB,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,CAAW,CAAC;QACtE,CAAC,CAAC,SAAS,CACP,SAAS,EACT,SAA6C,EAC7C,OAAO,CACR,CAAA;AACP,CAAC","sourcesContent":["import type { Dictionary, ExtractPath } from '@empathyco/x-utils'\nimport type { MutableSchema, Schema, SubSchemaTransformer } from '../schemas/types'\nimport type { Mapper, MapperContext } from './types'\nimport {\n deepMerge,\n getSafePropertyChain,\n isArray,\n isFunction,\n isObject,\n isPath,\n reduce,\n} from '@empathyco/x-utils'\nimport { createMutableSchema, isInternalMethod } from '../schemas/utils'\n\n/**\n * The 'schemaMapperFactory' function creates a {@link Mapper | mapper function} for a given\n * {@link Schema | schema}.\n *\n * @param schema - The {@link Schema | schema} to apply in the {@link Mapper | mapper function}.\n * @returns A {@link Mapper | mapper function} that applies the given {@link Schema | schema}.\n * @public\n */\nexport function schemaMapperFactory<Source, Target>(\n schema: Schema<Source, Target> | MutableSchema<Source, Target>,\n): Mapper<Source, Target> {\n return function mapper(source: Source, context: MapperContext): Target {\n return mapSchema(source, schema, context)\n }\n}\n\n/**\n * The `mapSchema()` function creates a new object populated with the transformations defined by a\n * {@link Schema} applied to a source object.\n *\n * @param source - The object to apply the transformations to.\n * @param schema - The object that defines the transformations to apply.\n * @param context - The {@link MapperContext | mapper context} to feed the transformations with.\n * @returns A new object with each element being the result of the applied transformation.\n * @internal\n */\nfunction mapSchema<Source, Target>(\n source: Source,\n schema: Schema<Source, Target>,\n context: MapperContext,\n): Target {\n if (!source) {\n console.warn('This schema cannot be applied', createMutableSchema(schema))\n return undefined as any\n }\n return reduce(\n schema,\n (target, key, transformer) => {\n type TargetKey = Target[keyof Target]\n if (typeof transformer === 'string' && isPath(source, transformer)) {\n target[key] = getSafePropertyChain(source, transformer) as TargetKey\n } else if (isFunction(transformer) && !isInternalMethod(transformer.name)) {\n target[key] = transformer(source, context)\n } else if (isObject(transformer)) {\n const value =\n '$subSchema' in transformer\n ? (applySubSchemaTransformer<Source, TargetKey>(\n source,\n transformer as SubSchemaTransformer<Source, TargetKey>,\n context,\n schema as unknown as Schema<Source, TargetKey>,\n ) as TargetKey)\n : mapSchema<Source, TargetKey>(\n source,\n transformer as Schema<Source, TargetKey>,\n context,\n )\n\n if (value) {\n target[key] = value\n }\n }\n return target\n },\n {} as Target,\n )\n}\n\n/**\n * The `applySubSchemaTransformer()` function executes a `mapSchema()` function applying the defined\n * {@link SubSchemaTransformer.$subSchema}.\n *\n * @param source - The object to feed the schema.\n * @param subSchemaTransformer - The {@link SubSchemaTransformer} object with a $path, $subSchema\n * and $context options.\n * @param subSchemaTransformer.$path\n * @param subSchemaTransformer.$subSchema\n * @param subSchemaTransformer.$context\n * @param rawContext - The {@link MapperContext | mapper context} to feed the mapSchema function.\n * @param schema - The {@link Schema} to apply.\n * @returns The result of calling `mapSchema()` with the source, schema and context arguments.\n * @internal\n */\nfunction applySubSchemaTransformer<Source, Target>(\n source: Source,\n { $subSchema, $path, $context }: SubSchemaTransformer<Source, Target>,\n rawContext: MapperContext,\n schema: Schema<Source, Target>,\n): Target | Target[] | undefined {\n const subSource = getSafePropertyChain(source, $path)\n\n if (!subSource) {\n return\n }\n\n const extendedContext: Dictionary = {}\n if ($context) {\n Object.entries($context).forEach(([key, value]) => {\n if (['requestParameters', 'endpoint', 'mappedValue'].includes(key)) {\n return\n }\n extendedContext[key] = isFunction(value)\n ? value(source)\n : getSafePropertyChain(source, value as ExtractPath<typeof source>)\n })\n }\n\n const context = deepMerge({}, rawContext, $context, extendedContext)\n let subSchema: typeof $subSchema | typeof schema\n if ($subSchema === '$self') {\n subSchema = schema\n } else if (isFunction($subSchema)) {\n subSchema = $subSchema(source)\n } else {\n subSchema = $subSchema\n }\n return isArray(subSource)\n ? subSource.map(item => mapSchema(item, subSchema, context) as Target)\n : mapSchema<typeof subSource, Target>(\n subSource,\n subSchema as Schema<typeof subSource, Target>,\n context,\n )\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { deepMerge } from '@empathyco/x-
|
|
2
|
-
import { forEach, isFunction, isObject } from '@empathyco/x-utils';
|
|
1
|
+
import { deepMerge, forEach, isFunction, isObject } from '@empathyco/x-utils';
|
|
3
2
|
/**
|
|
4
3
|
* Collection of internal method names for {@link MutableSchema | mutable schemas}.
|
|
5
4
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/schemas/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/schemas/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,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AC,GAAG,EAAE,KAAK,EAAE,EAAE;QAC3B,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,GAAG,WAAW,GAAG,GAAG,QAAQ,SAAS,CAC7C,KAAK,EACL,sBAAsB,EACtB,EAAE,IAAI,CACP,GAAG,WAAW,MAAM,CAAA;QACvB,CAAC;aAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,sBAAsB,EAAE,CAAC;YAClF,MAAM,IAAI,GAAG,WAAW,GAAG,GAAG,KAAK,KAAY,KAAK,CAAA;QACtD,CAAC;IACH,CAAC,CAAC,CAAA;IACF,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import type { MutableSchema, Schema } from './types'\nimport { deepMerge, forEach, isFunction, isObject } from '@empathyco/x-utils'\n\n/**\n * Collection of internal method names for {@link MutableSchema | mutable schemas}.\n */\nconst mutableSchemasInternalMethods: string[] = ['$replace', '$override', '$extends', 'toString']\n\n/**\n * Creates a {@link MutableSchema | mutable schema } version of a given {@link Schema | schema}.\n *\n * @param schema - The {@link Schema | schema} to make mutable.\n *\n * @returns A {@link MutableSchema | mutable schema} version of the given {@link Schema | schema}.\n *\n * @public\n */\nexport function createMutableSchema<Source, Target>(\n schema: Schema<Source, Target>,\n): MutableSchema<Source, Target> {\n return {\n ...schema,\n $replace(newSchema) {\n forEach(this, key => {\n if (isInternalMethod(key as string)) {\n return\n }\n delete this[key]\n })\n Object.assign(this, newSchema)\n /* We are replacing the schema with a completely new schema , so it makes sense that TS\n complains that the old schema and the new one are not the same. */\n return this as any\n },\n $override(newSchema) {\n return deepMerge(this, newSchema)\n },\n $extends(newSchema: unknown) {\n return deepMerge({}, this, newSchema)\n },\n toString(includeInternalMethods = false) {\n return serialize(this, !!includeInternalMethods)\n },\n }\n}\n\n/**\n * Checks if the given key is a {@link MutableSchema | mutableSchema} method.\n *\n * @param name - The key to check.\n *\n * @returns True if it is a {@link MutableSchema | mutableSchema} method,\n * false otherwise.\n *\n * @public\n */\nexport function isInternalMethod(name: string): boolean {\n return mutableSchemasInternalMethods.includes(name)\n}\n\n/**\n * Returns a string representing of the given object.\n *\n * @param data - The object to get the string representation from.\n * @param includeInternalMethods - Flag to include in the string representation\n * the internal methods. Disabled by default.\n * @param deep - The level of indentation.\n * @returns The string representation.\n */\nfunction serialize(\n data: Record<string, unknown>,\n includeInternalMethods: boolean,\n deep = 0,\n): string {\n const indentation = ' '.repeat(deep)\n let output = ''\n forEach(data, (key, value) => {\n if (isObject(value)) {\n output += `${indentation}${key}: {\\n${serialize(\n value,\n includeInternalMethods,\n ++deep,\n )}${indentation}},\\n`\n } else if (!isFunction(value) || !isInternalMethod(key) || includeInternalMethods) {\n output += `${indentation}${key}: ${value as any},\\n`\n }\n })\n return output\n}\n"]}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export declare const okFetchMock:
|
|
2
|
-
export declare const koFetchMock:
|
|
1
|
+
export declare const okFetchMock: import("vitest").Mock<(url: string, params: RequestInit) => Promise<Response>>;
|
|
2
|
+
export declare const koFetchMock: import("vitest").Mock<() => Promise<{
|
|
3
3
|
ok: boolean;
|
|
4
4
|
json: () => Promise<{}>;
|
|
5
5
|
status: number;
|
|
6
6
|
statusText: string;
|
|
7
|
-
}
|
|
7
|
+
}>>;
|
|
8
8
|
/**
|
|
9
9
|
* The `fetchMock()` method mocks a `fetch` API call.
|
|
10
10
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empathyco/x-adapter",
|
|
3
|
-
"version": "8.1.
|
|
3
|
+
"version": "8.1.1",
|
|
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",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"node": ">=22"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"prebuild": "
|
|
30
|
-
"build": "
|
|
29
|
+
"prebuild": "rm -rf dist *.tgz",
|
|
30
|
+
"build": "pnpm run /^build:/",
|
|
31
31
|
"build:cjs": "tsc --project tsconfig.cjs.json",
|
|
32
32
|
"build:esm": "tsc --project tsconfig.esm.json",
|
|
33
33
|
"pack": "pnpm pack",
|
|
@@ -36,25 +36,23 @@
|
|
|
36
36
|
"lint:inspect": "eslint --inspect-config .",
|
|
37
37
|
"format": "prettier --write .",
|
|
38
38
|
"format:check": "prettier --check .",
|
|
39
|
-
"test": "
|
|
40
|
-
"test:unit": "
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:unit": "vitest run",
|
|
41
|
+
"test:unit-coverage": "vitest run --coverage",
|
|
42
|
+
"test:unit-watch": "vitest",
|
|
41
43
|
"prepublishOnly": "pnpm run build"
|
|
42
44
|
},
|
|
43
45
|
"dependencies": {
|
|
44
|
-
"@empathyco/x-
|
|
45
|
-
"@empathyco/x-utils": "^1.0.3-alpha.8",
|
|
46
|
-
"tslib": "~2.8.1"
|
|
46
|
+
"@empathyco/x-utils": "1.0.3"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"ts-jest": "29.4.0",
|
|
54
|
-
"typescript": "5.8.3"
|
|
49
|
+
"@vitest/coverage-v8": "4.1.2",
|
|
50
|
+
"jsdom": "28.1.0",
|
|
51
|
+
"typescript": "5.9.3",
|
|
52
|
+
"vitest": "4.1.2"
|
|
55
53
|
},
|
|
56
54
|
"publishConfig": {
|
|
57
55
|
"access": "public"
|
|
58
56
|
},
|
|
59
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "04a3fab59e580e07fed7e4152fe017b02680a4ac"
|
|
60
58
|
}
|