@leancodepl/axios-cqrs-client 8.4.0 → 8.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/package.json +43 -5
- package/index.cjs.d.ts +0 -1
- package/index.cjs.default.js +0 -1
- package/index.cjs.js +0 -134
- package/index.cjs.mjs +0 -2
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -131
- package/src/index.d.ts +0 -2
- package/src/lib/mkCqrsClient.d.ts +0 -25
- package/src/lib/mkUncapitalizedCqrsClient.d.ts +0 -21
package/package.json
CHANGED
|
@@ -1,22 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leancodepl/axios-cqrs-client",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.5.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"registry": "https://registry.npmjs.org/"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18.0.0"
|
|
11
|
+
},
|
|
5
12
|
"dependencies": {
|
|
6
|
-
"@leancodepl/cqrs-client-base": "8.
|
|
7
|
-
"@leancodepl/validation": "8.
|
|
13
|
+
"@leancodepl/cqrs-client-base": "8.5.0",
|
|
14
|
+
"@leancodepl/validation": "8.5.0",
|
|
8
15
|
"axios": ">=1.6.0"
|
|
9
16
|
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/leancodepl/js_corelibrary.git",
|
|
20
|
+
"directory": "packages/cqrs-clients/axios-cqrs-client"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/leancodepl/js_corelibrary",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/leancodepl/js_corelibrary/issues"
|
|
25
|
+
},
|
|
26
|
+
"description": "HTTP CQRS client implementation using Axios",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"cqrs",
|
|
29
|
+
"http",
|
|
30
|
+
"axios",
|
|
31
|
+
"commands",
|
|
32
|
+
"queries",
|
|
33
|
+
"api",
|
|
34
|
+
"typescript",
|
|
35
|
+
"javascript",
|
|
36
|
+
"leancode"
|
|
37
|
+
],
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "LeanCode",
|
|
40
|
+
"url": "https://leancode.co"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md",
|
|
45
|
+
"CHANGELOG.md"
|
|
46
|
+
],
|
|
47
|
+
"sideEffects": false,
|
|
10
48
|
"exports": {
|
|
11
49
|
"./package.json": "./package.json",
|
|
12
50
|
".": {
|
|
13
51
|
"module": "./index.esm.js",
|
|
14
|
-
"types": "./index.
|
|
52
|
+
"types": "./index.d.ts",
|
|
15
53
|
"import": "./index.cjs.mjs",
|
|
16
54
|
"default": "./index.cjs.js"
|
|
17
55
|
}
|
|
18
56
|
},
|
|
19
57
|
"module": "./index.esm.js",
|
|
20
58
|
"main": "./index.cjs.js",
|
|
21
|
-
"types": "./index.
|
|
59
|
+
"types": "./index.d.ts"
|
|
22
60
|
}
|
package/index.cjs.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
package/index.cjs.default.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var axios = require('axios');
|
|
4
|
-
var validation = require('@leancodepl/validation');
|
|
5
|
-
var utils = require('@leancodepl/utils');
|
|
6
|
-
|
|
7
|
-
function _extends() {
|
|
8
|
-
_extends = Object.assign || function assign(target) {
|
|
9
|
-
for(var i = 1; i < arguments.length; i++){
|
|
10
|
-
var source = arguments[i];
|
|
11
|
-
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
12
|
-
}
|
|
13
|
-
return target;
|
|
14
|
-
};
|
|
15
|
-
return _extends.apply(this, arguments);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function createSuccess(result) {
|
|
19
|
-
return {
|
|
20
|
-
isSuccess: true,
|
|
21
|
-
result
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function createError(error) {
|
|
25
|
-
return {
|
|
26
|
-
isSuccess: false,
|
|
27
|
-
error
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions, tokenHeader = "Authorization" }) {
|
|
31
|
-
const apiAxios = axios.create(_extends({
|
|
32
|
-
baseURL: cqrsEndpoint
|
|
33
|
-
}, axiosOptions));
|
|
34
|
-
apiAxios.interceptors.request.use(async (config)=>{
|
|
35
|
-
const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
|
|
36
|
-
if (token) {
|
|
37
|
-
var _config_headers;
|
|
38
|
-
(_config_headers = config.headers) == null ? void 0 : _config_headers.set(tokenHeader, `Bearer ${token}`);
|
|
39
|
-
}
|
|
40
|
-
return config;
|
|
41
|
-
});
|
|
42
|
-
apiAxios.interceptors.response.use((response)=>{
|
|
43
|
-
response.data = createSuccess(response.data);
|
|
44
|
-
return response;
|
|
45
|
-
}, async (error)=>{
|
|
46
|
-
const response = error.response;
|
|
47
|
-
switch(error.response.status){
|
|
48
|
-
case 401:
|
|
49
|
-
{
|
|
50
|
-
var _config_params;
|
|
51
|
-
const config = error.config;
|
|
52
|
-
if ((_config_params = config.params) == null ? void 0 : _config_params.isRetry) {
|
|
53
|
-
response.data = createError("The request has not been authorized and token refresh did not help");
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
if (!(tokenProvider == null ? void 0 : tokenProvider.invalidateToken)) {
|
|
57
|
-
response.data = createError("User needs to be authenticated to execute the command/query/operation");
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
if (!await tokenProvider.invalidateToken()) {
|
|
61
|
-
response.data = createError("Cannot refresh access token after the server returned 401 Unauthorized");
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
config.params = error.config.params || {};
|
|
65
|
-
config.params.isRetry = true;
|
|
66
|
-
return await apiAxios.request(config);
|
|
67
|
-
}
|
|
68
|
-
case 400:
|
|
69
|
-
response.data = createError("The request was malformed");
|
|
70
|
-
break;
|
|
71
|
-
case 403:
|
|
72
|
-
response.data = createError("User is not authorized to execute the command/query/operation");
|
|
73
|
-
break;
|
|
74
|
-
case 404:
|
|
75
|
-
response.data = createError("Command/query/operation not found");
|
|
76
|
-
break;
|
|
77
|
-
case 422:
|
|
78
|
-
response.data = createSuccess(error.response.data);
|
|
79
|
-
break;
|
|
80
|
-
default:
|
|
81
|
-
response.data = createError(`Cannot execute command/query/operation, server returned a ${error.response.status} code`);
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
return response;
|
|
85
|
-
});
|
|
86
|
-
return {
|
|
87
|
-
createQuery (type) {
|
|
88
|
-
return (dto)=>{
|
|
89
|
-
const abortController = new AbortController();
|
|
90
|
-
const promise = apiAxios.post("query/" + type, dto, {
|
|
91
|
-
signal: abortController.signal
|
|
92
|
-
}).then((r)=>r.data);
|
|
93
|
-
promise.abort = abortController.abort.bind(abortController);
|
|
94
|
-
return promise;
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
createOperation (type) {
|
|
98
|
-
return (dto)=>apiAxios.post("operation/" + type, dto).then((r)=>r.data);
|
|
99
|
-
},
|
|
100
|
-
createCommand (type, errorCodesMap) {
|
|
101
|
-
async function call(dto) {
|
|
102
|
-
const response = await apiAxios.post("command/" + type, dto);
|
|
103
|
-
return response.data;
|
|
104
|
-
}
|
|
105
|
-
call.handle = (dto)=>call(dto).then((response)=>validation.handleResponse(response, errorCodesMap));
|
|
106
|
-
return call;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
}
|
|
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
|
-
|
|
133
|
-
exports.mkCqrsClient = mkCqrsClient;
|
|
134
|
-
exports.mkUncapitalizedCqrsClient = mkUncapitalizedCqrsClient;
|
package/index.cjs.mjs
DELETED
package/index.esm.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
package/index.esm.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
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
|
-
}
|
|
15
|
-
|
|
16
|
-
function createSuccess(result) {
|
|
17
|
-
return {
|
|
18
|
-
isSuccess: true,
|
|
19
|
-
result
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
function createError(error) {
|
|
23
|
-
return {
|
|
24
|
-
isSuccess: false,
|
|
25
|
-
error
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions, tokenHeader = "Authorization" }) {
|
|
29
|
-
const apiAxios = axios.create(_extends({
|
|
30
|
-
baseURL: cqrsEndpoint
|
|
31
|
-
}, axiosOptions));
|
|
32
|
-
apiAxios.interceptors.request.use(async (config)=>{
|
|
33
|
-
const token = await (tokenProvider == null ? void 0 : tokenProvider.getToken());
|
|
34
|
-
if (token) {
|
|
35
|
-
var _config_headers;
|
|
36
|
-
(_config_headers = config.headers) == null ? void 0 : _config_headers.set(tokenHeader, `Bearer ${token}`);
|
|
37
|
-
}
|
|
38
|
-
return config;
|
|
39
|
-
});
|
|
40
|
-
apiAxios.interceptors.response.use((response)=>{
|
|
41
|
-
response.data = createSuccess(response.data);
|
|
42
|
-
return response;
|
|
43
|
-
}, async (error)=>{
|
|
44
|
-
const response = error.response;
|
|
45
|
-
switch(error.response.status){
|
|
46
|
-
case 401:
|
|
47
|
-
{
|
|
48
|
-
var _config_params;
|
|
49
|
-
const config = error.config;
|
|
50
|
-
if ((_config_params = config.params) == null ? void 0 : _config_params.isRetry) {
|
|
51
|
-
response.data = createError("The request has not been authorized and token refresh did not help");
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
if (!(tokenProvider == null ? void 0 : tokenProvider.invalidateToken)) {
|
|
55
|
-
response.data = createError("User needs to be authenticated to execute the command/query/operation");
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
if (!await tokenProvider.invalidateToken()) {
|
|
59
|
-
response.data = createError("Cannot refresh access token after the server returned 401 Unauthorized");
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
config.params = error.config.params || {};
|
|
63
|
-
config.params.isRetry = true;
|
|
64
|
-
return await apiAxios.request(config);
|
|
65
|
-
}
|
|
66
|
-
case 400:
|
|
67
|
-
response.data = createError("The request was malformed");
|
|
68
|
-
break;
|
|
69
|
-
case 403:
|
|
70
|
-
response.data = createError("User is not authorized to execute the command/query/operation");
|
|
71
|
-
break;
|
|
72
|
-
case 404:
|
|
73
|
-
response.data = createError("Command/query/operation not found");
|
|
74
|
-
break;
|
|
75
|
-
case 422:
|
|
76
|
-
response.data = createSuccess(error.response.data);
|
|
77
|
-
break;
|
|
78
|
-
default:
|
|
79
|
-
response.data = createError(`Cannot execute command/query/operation, server returned a ${error.response.status} code`);
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
return response;
|
|
83
|
-
});
|
|
84
|
-
return {
|
|
85
|
-
createQuery (type) {
|
|
86
|
-
return (dto)=>{
|
|
87
|
-
const abortController = new AbortController();
|
|
88
|
-
const promise = apiAxios.post("query/" + type, dto, {
|
|
89
|
-
signal: abortController.signal
|
|
90
|
-
}).then((r)=>r.data);
|
|
91
|
-
promise.abort = abortController.abort.bind(abortController);
|
|
92
|
-
return promise;
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
createOperation (type) {
|
|
96
|
-
return (dto)=>apiAxios.post("operation/" + type, dto).then((r)=>r.data);
|
|
97
|
-
},
|
|
98
|
-
createCommand (type, errorCodesMap) {
|
|
99
|
-
async function call(dto) {
|
|
100
|
-
const response = await apiAxios.post("command/" + type, dto);
|
|
101
|
-
return response.data;
|
|
102
|
-
}
|
|
103
|
-
call.handle = (dto)=>call(dto).then((response)=>handleResponse(response, errorCodesMap));
|
|
104
|
-
return call;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function uncapitalizeResponse(response) {
|
|
110
|
-
if (!response.isSuccess) {
|
|
111
|
-
return response;
|
|
112
|
-
}
|
|
113
|
-
return _extends({}, response, {
|
|
114
|
-
result: uncapitalizeDeep(response.result)
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
function mkUncapitalizedCqrsClient(params) {
|
|
118
|
-
const baseClient = mkCqrsClient(params);
|
|
119
|
-
return _extends({}, baseClient, {
|
|
120
|
-
createQuery (type) {
|
|
121
|
-
const query = baseClient.createQuery(type);
|
|
122
|
-
return (dto)=>query(dto).then(uncapitalizeResponse);
|
|
123
|
-
},
|
|
124
|
-
createOperation (type) {
|
|
125
|
-
const operation = baseClient.createOperation(type);
|
|
126
|
-
return (dto)=>operation(dto).then(uncapitalizeResponse);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export { mkCqrsClient, mkUncapitalizedCqrsClient };
|
package/src/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { CreateAxiosDefaults } from "axios";
|
|
2
|
-
import { ApiError, ApiResponse, ApiSuccess, CommandResult, TokenProvider } from "@leancodepl/cqrs-client-base";
|
|
3
|
-
export type MkCqrsClientParameters = {
|
|
4
|
-
cqrsEndpoint: string;
|
|
5
|
-
tokenProvider?: TokenProvider;
|
|
6
|
-
axiosOptions?: CreateAxiosDefaults;
|
|
7
|
-
tokenHeader?: string;
|
|
8
|
-
};
|
|
9
|
-
export declare function mkCqrsClient({ cqrsEndpoint, tokenProvider, axiosOptions, tokenHeader, }: MkCqrsClientParameters): {
|
|
10
|
-
createQuery<TQuery, TResult>(type: string): (dto: TQuery) => QueryPromise<TResult>;
|
|
11
|
-
createOperation<TOperation, TResult>(type: string): (dto: TOperation) => Promise<ApiError | ApiSuccess<TResult>>;
|
|
12
|
-
createCommand<TCommand, TErrorCodes extends {
|
|
13
|
-
[name: string]: number;
|
|
14
|
-
}>(type: string, errorCodesMap: TErrorCodes): {
|
|
15
|
-
(dto: TCommand): Promise<ApiResponse<CommandResult<TErrorCodes>>>;
|
|
16
|
-
handle(dto: TCommand): Promise<import("@leancodepl/validation").ValidationErrorsHandler<TErrorCodes & {
|
|
17
|
-
readonly success: -1;
|
|
18
|
-
readonly failure: -2;
|
|
19
|
-
}, never>>;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
export type QueryAbort = {
|
|
23
|
-
abort: AbortController["abort"];
|
|
24
|
-
};
|
|
25
|
-
export type QueryPromise<TResult> = Promise<ApiResponse<TResult>> & QueryAbort;
|
|
@@ -1,21 +0,0 @@
|
|
|
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>(type: string): (dto: TOperation) => Promise<import("@leancodepl/cqrs-client-base").ApiError | {
|
|
9
|
-
result: import("@leancodepl/utils").TransformDeep<TResult, "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
|
-
};
|