@leancodepl/axios-cqrs-client 7.4.0 → 7.6.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/index.cjs.js CHANGED
@@ -4,11 +4,23 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var axios = require('axios');
6
6
  var validation = require('@leancodepl/validation');
7
+ var utils = require('@leancodepl/utils');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
10
11
  var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
11
12
 
13
+ function _extends() {
14
+ _extends = Object.assign || function assign(target) {
15
+ for(var i = 1; i < arguments.length; i++){
16
+ var source = arguments[i];
17
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
18
+ }
19
+ return target;
20
+ };
21
+ return _extends.apply(this, arguments);
22
+ }
23
+
12
24
  function createSuccess(result) {
13
25
  return {
14
26
  isSuccess: true,
@@ -22,10 +34,10 @@ function createError(error) {
22
34
  error
23
35
  };
24
36
  }
25
- function mkCqrsClient(cqrsEndpoint, tokenProvider) {
26
- const apiAxios = axios__default["default"].create({
37
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions }) {
38
+ const apiAxios = axios__default["default"].create(_extends({
27
39
  baseURL: cqrsEndpoint
28
- });
40
+ }, axiosOptions));
29
41
  apiAxios.interceptors.request.use(async (config)=>{
30
42
  const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
31
43
  if (token) {
@@ -96,4 +108,27 @@ function mkCqrsClient(cqrsEndpoint, tokenProvider) {
96
108
  };
97
109
  }
98
110
 
111
+ function uncapitalizeResponse(response) {
112
+ if (!response.isSuccess) {
113
+ return response;
114
+ }
115
+ return _extends({}, response, {
116
+ result: utils.uncapitalizeDeep(response.result)
117
+ });
118
+ }
119
+ function mkUncapitalizedCqrsClient(params) {
120
+ const baseClient = mkCqrsClient(params);
121
+ return _extends({}, baseClient, {
122
+ createQuery (type) {
123
+ const query = baseClient.createQuery(type);
124
+ return (dto)=>query(dto).then(uncapitalizeResponse);
125
+ },
126
+ createOperation (type) {
127
+ const operation = baseClient.createOperation(type);
128
+ return (dto)=>operation(dto).then(uncapitalizeResponse);
129
+ }
130
+ });
131
+ }
132
+
99
133
  exports.mkCqrsClient = mkCqrsClient;
134
+ exports.mkUncapitalizedCqrsClient = mkUncapitalizedCqrsClient;
package/index.esm.js CHANGED
@@ -1,5 +1,17 @@
1
1
  import axios from 'axios';
2
2
  import { handleResponse } from '@leancodepl/validation';
3
+ import { uncapitalizeDeep } from '@leancodepl/utils';
4
+
5
+ function _extends() {
6
+ _extends = Object.assign || function assign(target) {
7
+ for(var i = 1; i < arguments.length; i++){
8
+ var source = arguments[i];
9
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
10
+ }
11
+ return target;
12
+ };
13
+ return _extends.apply(this, arguments);
14
+ }
3
15
 
4
16
  function createSuccess(result) {
5
17
  return {
@@ -14,10 +26,10 @@ function createError(error) {
14
26
  error
15
27
  };
16
28
  }
17
- function mkCqrsClient(cqrsEndpoint, tokenProvider) {
18
- const apiAxios = axios.create({
29
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions }) {
30
+ const apiAxios = axios.create(_extends({
19
31
  baseURL: cqrsEndpoint
20
- });
32
+ }, axiosOptions));
21
33
  apiAxios.interceptors.request.use(async (config)=>{
22
34
  const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
23
35
  if (token) {
@@ -88,4 +100,26 @@ function mkCqrsClient(cqrsEndpoint, tokenProvider) {
88
100
  };
89
101
  }
90
102
 
91
- export { mkCqrsClient };
103
+ function uncapitalizeResponse(response) {
104
+ if (!response.isSuccess) {
105
+ return response;
106
+ }
107
+ return _extends({}, response, {
108
+ result: uncapitalizeDeep(response.result)
109
+ });
110
+ }
111
+ function mkUncapitalizedCqrsClient(params) {
112
+ const baseClient = mkCqrsClient(params);
113
+ return _extends({}, baseClient, {
114
+ createQuery (type) {
115
+ const query = baseClient.createQuery(type);
116
+ return (dto)=>query(dto).then(uncapitalizeResponse);
117
+ },
118
+ createOperation (type) {
119
+ const operation = baseClient.createOperation(type);
120
+ return (dto)=>operation(dto).then(uncapitalizeResponse);
121
+ }
122
+ });
123
+ }
124
+
125
+ export { mkCqrsClient, mkUncapitalizedCqrsClient };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@leancodepl/axios-cqrs-client",
3
- "version": "7.4.0",
3
+ "version": "7.6.0",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/cqrs-client-base": "7.4.0",
7
- "@leancodepl/validation": "7.4.0",
6
+ "@leancodepl/cqrs-client-base": "7.6.0",
7
+ "@leancodepl/validation": "7.6.0",
8
8
  "axios": ">=1.6.0"
9
9
  },
10
10
  "exports": {
package/src/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./lib/mkCqrsClient";
2
+ export * from "./lib/mkUncapitalizedCqrsClient";
@@ -1,5 +1,11 @@
1
+ import { CreateAxiosDefaults } from "axios";
1
2
  import { ApiError, ApiResponse, ApiSuccess, CommandResult, TokenProvider } from "@leancodepl/cqrs-client-base";
2
- export declare function mkCqrsClient(cqrsEndpoint: string, tokenProvider?: TokenProvider): {
3
+ export type MkCqrsClientParameters = {
4
+ cqrsEndpoint: string;
5
+ tokenProvider?: TokenProvider;
6
+ axiosOptions?: CreateAxiosDefaults;
7
+ };
8
+ export declare function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions }: MkCqrsClientParameters): {
3
9
  createQuery<TQuery, TResult>(type: string): (dto: TQuery) => Promise<ApiError | ApiSuccess<TResult>>;
4
10
  createOperation<TOperation, TResult_1>(type: string): (dto: TOperation) => Promise<ApiError | ApiSuccess<TResult_1>>;
5
11
  createCommand<TCommand, TErrorCodes extends {
@@ -0,0 +1,21 @@
1
+ import type { ApiResponse } from "@leancodepl/cqrs-client-base";
2
+ import { type MkCqrsClientParameters } from "./mkCqrsClient";
3
+ export declare function mkUncapitalizedCqrsClient(params: MkCqrsClientParameters): {
4
+ createQuery<TQuery, TResult>(type: string): (dto: TQuery) => Promise<import("@leancodepl/cqrs-client-base").ApiError | {
5
+ result: import("@leancodepl/utils").TransformDeep<TResult, "uncapitalize">;
6
+ isSuccess: true;
7
+ }>;
8
+ createOperation<TOperation, TResult_1>(type: string): (dto: TOperation) => Promise<import("@leancodepl/cqrs-client-base").ApiError | {
9
+ result: import("@leancodepl/utils").TransformDeep<TResult_1, "uncapitalize">;
10
+ isSuccess: true;
11
+ }>;
12
+ createCommand<TCommand, TErrorCodes extends {
13
+ [name: string]: number;
14
+ }>(type: string, errorCodesMap: TErrorCodes): {
15
+ (dto: TCommand): Promise<ApiResponse<import("@leancodepl/cqrs-client-base").CommandResult<TErrorCodes>>>;
16
+ handle(dto: TCommand): Promise<import("dist/packages/validation/src").ValidationErrorsHandler<TErrorCodes & {
17
+ readonly success: -1;
18
+ readonly failure: -2;
19
+ }, never>>;
20
+ };
21
+ };