@leancodepl/axios-cqrs-client 9.7.1 → 9.7.3

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.esm.js DELETED
@@ -1,191 +0,0 @@
1
- import axios, { AxiosError, AxiosHeaders } 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
- }
15
-
16
- function createSuccess(result) {
17
- return {
18
- isSuccess: true,
19
- result
20
- };
21
- }
22
- function createError(error, options) {
23
- return {
24
- isSuccess: false,
25
- isAborted: !!(options == null ? void 0 : options.isAborted),
26
- error
27
- };
28
- }
29
- /**
30
- * Creates CQRS client with Axios for HTTP communication and command/query handling.
31
- *
32
- * Provides type-safe methods for creating queries, operations, and commands with automatic
33
- * token management, retry logic, and response handling. Supports validation error handling
34
- * and HTTP status code management.
35
- *
36
- * @param cqrsEndpoint - Base URL for CQRS API endpoints
37
- * @param tokenProvider - Optional token provider for authentication
38
- * @param axiosOptions - Optional Axios configuration options
39
- * @param tokenHeader - Header name for authentication token (default: "Authorization")
40
- * @returns Object with `createQuery`, `createOperation`, and `createCommand` methods
41
- * @example
42
- * ```typescript
43
- * const client = mkCqrsClient({
44
- * cqrsEndpoint: 'https://api.example.com',
45
- * tokenProvider: { getToken: () => Promise.resolve('token') }
46
- * });
47
- * ```
48
- */ function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions, tokenHeader = "Authorization" }) {
49
- const apiAxios = axios.create(_extends({
50
- baseURL: cqrsEndpoint
51
- }, axiosOptions));
52
- apiAxios.interceptors.request.use(async (config)=>{
53
- const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
54
- if (token) {
55
- var _config_headers;
56
- (_config_headers = config.headers) == null ? void 0 : _config_headers.set(tokenHeader, `Bearer ${token}`);
57
- }
58
- return config;
59
- });
60
- apiAxios.interceptors.response.use((response)=>{
61
- response.data = createSuccess(response.data);
62
- return response;
63
- }, async (error)=>{
64
- if (!(error instanceof AxiosError)) {
65
- return {
66
- data: createError(`Unknown error ${error}`)
67
- };
68
- }
69
- if (error.code === "ERR_CANCELED") {
70
- return {
71
- data: createError(error, {
72
- isAborted: true
73
- })
74
- };
75
- }
76
- if (!error.response) {
77
- return {
78
- data: createError(error)
79
- };
80
- }
81
- const response = error.response;
82
- switch(error.response.status){
83
- case 401:
84
- {
85
- var _config_params;
86
- var _config;
87
- let config = error.config;
88
- if (config == null ? void 0 : (_config_params = config.params) == null ? void 0 : _config_params.isRetry) {
89
- response.data = createError("The request has not been authorized and token refresh did not help");
90
- break;
91
- }
92
- if (!(tokenProvider == null ? void 0 : tokenProvider.invalidateToken)) {
93
- response.data = createError("User needs to be authenticated to execute the command/query/operation");
94
- break;
95
- }
96
- if (!await tokenProvider.invalidateToken()) {
97
- response.data = createError("Cannot refresh access token after the server returned 401 Unauthorized");
98
- break;
99
- }
100
- config != null ? config : config = {
101
- headers: new AxiosHeaders()
102
- };
103
- var _params;
104
- (_params = (_config = config).params) != null ? _params : _config.params = {};
105
- config.params.isRetry = true;
106
- return await apiAxios.request(config);
107
- }
108
- case 400:
109
- response.data = createError("The request was malformed");
110
- break;
111
- case 403:
112
- response.data = createError("User is not authorized to execute the command/query/operation");
113
- break;
114
- case 404:
115
- response.data = createError("Command/query/operation not found");
116
- break;
117
- case 422:
118
- response.data = createSuccess(error.response.data);
119
- break;
120
- default:
121
- response.data = createError(`Cannot execute command/query/operation, server returned a ${error.response.status} code`);
122
- break;
123
- }
124
- return response;
125
- });
126
- return {
127
- createQuery (type) {
128
- return (dto)=>{
129
- const abortController = new AbortController();
130
- const promise = apiAxios.post("query/" + type, dto, {
131
- signal: abortController.signal
132
- }).then((r)=>r.data);
133
- promise.abort = abortController.abort.bind(abortController);
134
- return promise;
135
- };
136
- },
137
- createOperation (type) {
138
- return (dto)=>apiAxios.post("operation/" + type, dto).then((r)=>r.data);
139
- },
140
- createCommand (type, errorCodesMap) {
141
- async function call(dto) {
142
- const response = await apiAxios.post("command/" + type, dto);
143
- return response.data;
144
- }
145
- call.handle = (dto)=>call(dto).then((response)=>handleResponse(response, errorCodesMap));
146
- return call;
147
- }
148
- };
149
- }
150
-
151
- function uncapitalizeResponse(response) {
152
- if (!response.isSuccess) {
153
- return response;
154
- }
155
- return _extends({}, response, {
156
- result: uncapitalizeDeep(response.result)
157
- });
158
- }
159
- /**
160
- * Creates CQRS client with automatic response key uncapitalization using "@leancodepl/utils".
161
- *
162
- * Extends the base CQRS client to automatically transform response object keys from
163
- * PascalCase to camelCase using deep transformation.
164
- *
165
- * @param params - Configuration object for CQRS client
166
- * @param params.cqrsEndpoint - Base URL for CQRS API endpoints
167
- * @param params.tokenProvider - Optional token provider for authentication
168
- * @param params.axiosOptions - Optional Axios configuration options
169
- * @param params.tokenHeader - Header name for authentication token (default: "Authorization")
170
- * @returns CQRS client with response key transformation
171
- * @example
172
- * ```typescript
173
- * const client = mkUncapitalizedCqrsClient({
174
- * cqrsEndpoint: 'https://api.example.com'
175
- * });
176
- * ```
177
- */ function mkUncapitalizedCqrsClient(params) {
178
- const baseClient = mkCqrsClient(params);
179
- return _extends({}, baseClient, {
180
- createQuery (type) {
181
- const query = baseClient.createQuery(type);
182
- return (dto)=>query(dto).then(uncapitalizeResponse);
183
- },
184
- createOperation (type) {
185
- const operation = baseClient.createOperation(type);
186
- return (dto)=>operation(dto).then(uncapitalizeResponse);
187
- }
188
- });
189
- }
190
-
191
- export { mkCqrsClient, mkUncapitalizedCqrsClient };
package/src/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./lib/mkCqrsClient";
2
- export * from "./lib/mkUncapitalizedCqrsClient";