@leancodepl/axios-cqrs-client 7.3.7 → 7.5.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
@@ -2,33 +2,47 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var validation = require('@leancodepl/validation');
6
5
  var axios = require('axios');
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,
15
27
  result
16
28
  };
17
29
  }
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
31
  function createError(error) {
19
32
  return {
20
33
  isSuccess: false,
21
34
  error
22
35
  };
23
36
  }
24
- function mkCqrsClient(cqrsEndpoint, tokenProvider) {
25
- const apiAxios = axios__default["default"].create({
37
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions }) {
38
+ const apiAxios = axios__default["default"].create(_extends({
26
39
  baseURL: cqrsEndpoint
27
- });
40
+ }, axiosOptions));
28
41
  apiAxios.interceptors.request.use(async (config)=>{
29
42
  const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
30
43
  if (token) {
31
- config.headers.set("Authorization", `Bearer ${token}`);
44
+ var _config_headers;
45
+ (_config_headers = config.headers) == null ? void 0 : _config_headers.set("Authorization", `Bearer ${token}`);
32
46
  }
33
47
  return config;
34
48
  });
@@ -94,4 +108,27 @@ function mkCqrsClient(cqrsEndpoint, tokenProvider) {
94
108
  };
95
109
  }
96
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
+
97
133
  exports.mkCqrsClient = mkCqrsClient;
134
+ exports.mkUncapitalizedCqrsClient = mkUncapitalizedCqrsClient;
package/index.esm.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js CHANGED
@@ -1,5 +1,17 @@
1
- import { handleResponse } from '@leancodepl/validation';
2
1
  import axios from 'axios';
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 {
@@ -7,20 +19,22 @@ function createSuccess(result) {
7
19
  result
8
20
  };
9
21
  }
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
23
  function createError(error) {
11
24
  return {
12
25
  isSuccess: false,
13
26
  error
14
27
  };
15
28
  }
16
- function mkCqrsClient(cqrsEndpoint, tokenProvider) {
17
- const apiAxios = axios.create({
29
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions }) {
30
+ const apiAxios = axios.create(_extends({
18
31
  baseURL: cqrsEndpoint
19
- });
32
+ }, axiosOptions));
20
33
  apiAxios.interceptors.request.use(async (config)=>{
21
34
  const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
22
35
  if (token) {
23
- config.headers.set("Authorization", `Bearer ${token}`);
36
+ var _config_headers;
37
+ (_config_headers = config.headers) == null ? void 0 : _config_headers.set("Authorization", `Bearer ${token}`);
24
38
  }
25
39
  return config;
26
40
  });
@@ -86,4 +100,26 @@ function mkCqrsClient(cqrsEndpoint, tokenProvider) {
86
100
  };
87
101
  }
88
102
 
89
- 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,11 @@
1
1
  {
2
2
  "name": "@leancodepl/axios-cqrs-client",
3
- "version": "7.3.7",
3
+ "version": "7.5.0",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/validation": "7.3.7",
7
- "axios": ">=1.0.0"
6
+ "@leancodepl/cqrs-client-base": "7.5.0",
7
+ "@leancodepl/validation": "7.5.0",
8
+ "axios": ">=1.6.0"
8
9
  },
9
10
  "exports": {
10
11
  "./package.json": "./package.json",
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
+ };