@odata2ts/http-client-base 0.4.2 → 0.5.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/CHANGELOG.md +20 -0
- package/README.md +74 -74
- package/lib/BaseHttpClient.d.ts +13 -8
- package/lib/BaseHttpClient.js +36 -24
- package/lib/BaseHttpClient.js.map +1 -1
- package/lib/ErrorMessageRetriever.js +1 -5
- package/lib/ErrorMessageRetriever.js.map +1 -1
- package/lib/HttpMethods.js +2 -5
- package/lib/HttpMethods.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -9
- package/lib/index.js.map +1 -1
- package/package.json +21 -24
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,26 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.5.1](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-base@0.5.0...@odata2ts/http-client-base@0.5.1) (2024-08-14)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* add ".js" suffix for all relative imports ([#20](https://github.com/odata2ts/http-client/issues/20)) ([961c910](https://github.com/odata2ts/http-client/commit/961c91002c8b1e9a7a6256cccd6b6d0ec9c142cd))
|
|
11
|
+
|
|
12
|
+
# [0.5.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-base@0.4.2...@odata2ts/http-client-base@0.5.0) (2024-08-13)
|
|
13
|
+
|
|
14
|
+
### Code Refactoring
|
|
15
|
+
|
|
16
|
+
* switch to vitest & ESM ([#18](https://github.com/odata2ts/http-client/issues/18)) ([748558f](https://github.com/odata2ts/http-client/commit/748558f1e3f699085ade1058b1459c843f60994f))
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* blob and stream support ([#12](https://github.com/odata2ts/http-client/issues/12)) ([ae6f062](https://github.com/odata2ts/http-client/commit/ae6f062371a0ad11707fa3f9edff9571998edb5b))
|
|
21
|
+
|
|
22
|
+
### BREAKING CHANGES
|
|
23
|
+
|
|
24
|
+
* switch to ESM tends to break stuff
|
|
25
|
+
|
|
6
26
|
## [0.4.2](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-base@0.4.1...@odata2ts/http-client-base@0.4.2) (2023-09-13)
|
|
7
27
|
|
|
8
28
|
**Note:** Version bump only for package @odata2ts/http-client-base
|
package/README.md
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@odata2ts/http-client-base)
|
|
2
|
-
|
|
3
|
-
# OData HTTP Client Base
|
|
4
|
-
|
|
5
|
-
Base implementation for [odata2ts](https://github.com/odata2ts/odata2ts) compatible HTTP clients:
|
|
6
|
-
|
|
7
|
-
- implements automatic CSRF token handling
|
|
8
|
-
- allows user to set custom CSRF token header key (default: `x-csrf-token`)
|
|
9
|
-
- implements standard error message retrieval method for OData error responses
|
|
10
|
-
- works for V2 & V4
|
|
11
|
-
- allows user to set custom retrieval method
|
|
12
|
-
- streamlines all HTTP calls (POST, GET, ...) into one method
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
Install package `@odata2ts/http-client-base` as dependency:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm install --save @odata2ts/http-client-base
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
Extend the class `BaseHttpClient` to simplify your HttpClient implementation.
|
|
25
|
-
You need to implement two abstract methods:
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { BaseHttpClient, BaseHttpClientOptions, InternalHttpClientConfig } from "@odata2ts/http-client-base";
|
|
29
|
-
|
|
30
|
-
export interface MyRequestConfig {}
|
|
31
|
-
|
|
32
|
-
export class MyHttpClient extends BaseHttpClient<MyRequestConfig> {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
Compare any of the existing clients.
|
|
50
|
-
|
|
51
|
-
## Documentation
|
|
52
|
-
|
|
53
|
-
[HTTP Client Documentation](https://odata2ts.github.io/docs/http-client)
|
|
54
|
-
|
|
55
|
-
Main documentation for the odata2ts eco system:
|
|
56
|
-
[https://odata2ts.github.io](https://odata2ts.github.io/)
|
|
57
|
-
|
|
58
|
-
## Tests
|
|
59
|
-
|
|
60
|
-
See folder [test](https://github.com/odata2ts/http-client/tree/main/packages/core/test)
|
|
61
|
-
for unit tests.
|
|
62
|
-
|
|
63
|
-
## Support, Feedback, Contributing
|
|
64
|
-
|
|
65
|
-
This project is open to feature requests, suggestions, bug reports, usage questions etc.
|
|
66
|
-
via [GitHub issues](https://github.com/odata2ts/http-client/issues).
|
|
67
|
-
|
|
68
|
-
Contributions and feedback are encouraged and always welcome.
|
|
69
|
-
|
|
70
|
-
See the [contribution guidelines](https://github.com/odata2ts/http-client/blob/main/CONTRIBUTING.md) for further information.
|
|
71
|
-
|
|
72
|
-
## License
|
|
73
|
-
|
|
74
|
-
MIT - see [License](./LICENSE).
|
|
1
|
+
[](https://www.npmjs.com/package/@odata2ts/http-client-base)
|
|
2
|
+
|
|
3
|
+
# OData HTTP Client Base
|
|
4
|
+
|
|
5
|
+
Base implementation for [odata2ts](https://github.com/odata2ts/odata2ts) compatible HTTP clients:
|
|
6
|
+
|
|
7
|
+
- implements automatic CSRF token handling
|
|
8
|
+
- allows user to set custom CSRF token header key (default: `x-csrf-token`)
|
|
9
|
+
- implements standard error message retrieval method for OData error responses
|
|
10
|
+
- works for V2 & V4
|
|
11
|
+
- allows user to set custom retrieval method
|
|
12
|
+
- streamlines all HTTP calls (POST, GET, ...) into one method
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install package `@odata2ts/http-client-base` as dependency:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install --save @odata2ts/http-client-base
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Extend the class `BaseHttpClient` to simplify your HttpClient implementation.
|
|
25
|
+
You need to implement two abstract methods:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { BaseHttpClient, BaseHttpClientOptions, InternalHttpClientConfig } from "@odata2ts/http-client-base";
|
|
29
|
+
|
|
30
|
+
export interface MyRequestConfig {}
|
|
31
|
+
|
|
32
|
+
export class MyHttpClient extends BaseHttpClient<MyRequestConfig> {
|
|
33
|
+
constructor(clientOptions: BaseHttpClientOptions) {
|
|
34
|
+
super(clientOptions);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected async executeRequest<ResponseModel>(
|
|
38
|
+
method: HttpMethods,
|
|
39
|
+
url: string,
|
|
40
|
+
data: any,
|
|
41
|
+
requestConfig: AxiosRequestConfig | undefined = {},
|
|
42
|
+
internalConfig?: InternalHttpClientConfig,
|
|
43
|
+
): Promise<HttpResponseModel<ResponseModel>> {
|
|
44
|
+
// your implementation
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Compare any of the existing clients.
|
|
50
|
+
|
|
51
|
+
## Documentation
|
|
52
|
+
|
|
53
|
+
[HTTP Client Documentation](https://odata2ts.github.io/docs/http-client)
|
|
54
|
+
|
|
55
|
+
Main documentation for the odata2ts eco system:
|
|
56
|
+
[https://odata2ts.github.io](https://odata2ts.github.io/)
|
|
57
|
+
|
|
58
|
+
## Tests
|
|
59
|
+
|
|
60
|
+
See folder [test](https://github.com/odata2ts/http-client/tree/main/packages/core/test)
|
|
61
|
+
for unit tests.
|
|
62
|
+
|
|
63
|
+
## Support, Feedback, Contributing
|
|
64
|
+
|
|
65
|
+
This project is open to feature requests, suggestions, bug reports, usage questions etc.
|
|
66
|
+
via [GitHub issues](https://github.com/odata2ts/http-client/issues).
|
|
67
|
+
|
|
68
|
+
Contributions and feedback are encouraged and always welcome.
|
|
69
|
+
|
|
70
|
+
See the [contribution guidelines](https://github.com/odata2ts/http-client/blob/main/CONTRIBUTING.md) for further information.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT - see [License](./LICENSE).
|
package/lib/BaseHttpClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HttpResponseModel } from "@odata2ts/http-client-api";
|
|
2
|
-
import { ErrorMessageRetriever } from "./ErrorMessageRetriever";
|
|
3
|
-
import { HttpMethods } from "./HttpMethods";
|
|
1
|
+
import { HttpResponseModel, ODataResponse } from "@odata2ts/http-client-api";
|
|
2
|
+
import { ErrorMessageRetriever } from "./ErrorMessageRetriever.js";
|
|
3
|
+
import { HttpMethods } from "./HttpMethods.js";
|
|
4
4
|
export interface BaseHttpClientOptions {
|
|
5
5
|
/**
|
|
6
6
|
* Enable automatic CSRF token handling.
|
|
@@ -14,6 +14,7 @@ export interface BaseHttpClientOptions {
|
|
|
14
14
|
csrfTokenFetchUrl?: string;
|
|
15
15
|
}
|
|
16
16
|
export interface InternalHttpClientConfig {
|
|
17
|
+
dataType?: "json" | "blob" | "stream";
|
|
17
18
|
/**
|
|
18
19
|
* Additional headers set internally by services or HttpClient implementation.
|
|
19
20
|
*/
|
|
@@ -35,11 +36,11 @@ export declare abstract class BaseHttpClient<RequestConfigType> {
|
|
|
35
36
|
* As it name suggests, the request gets executed in this method.
|
|
36
37
|
* Additionally, failures should be handled and errors of type <code>HttpClientError</code> should be thrown.
|
|
37
38
|
*
|
|
38
|
-
* @param method
|
|
39
|
-
* @param url
|
|
40
|
-
* @param data
|
|
41
|
-
* @param config
|
|
42
|
-
* @param internalConfig
|
|
39
|
+
* @param method the http method to use
|
|
40
|
+
* @param url the URL to use
|
|
41
|
+
* @param data data for the request body if any
|
|
42
|
+
* @param config request configuration from end user which should override default settings
|
|
43
|
+
* @param internalConfig request configuration from base client including additional headers which should override end user configurations
|
|
43
44
|
*/
|
|
44
45
|
protected abstract executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, config?: RequestConfigType, internalConfig?: InternalHttpClientConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
45
46
|
getCsrfTokenKey(): string;
|
|
@@ -62,5 +63,9 @@ export declare abstract class BaseHttpClient<RequestConfigType> {
|
|
|
62
63
|
post<ResponseModel>(url: string, data: any, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): Promise<HttpResponseModel<ResponseModel>>;
|
|
63
64
|
put<ResponseModel>(url: string, data: any, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): Promise<HttpResponseModel<ResponseModel>>;
|
|
64
65
|
patch<ResponseModel>(url: string, data: any, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): Promise<HttpResponseModel<ResponseModel>>;
|
|
66
|
+
private getAdditionalHeaders;
|
|
65
67
|
delete(url: string, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): Promise<HttpResponseModel<void>>;
|
|
68
|
+
getBlob(url: string, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): ODataResponse<Blob>;
|
|
69
|
+
getStream(url: string, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): ODataResponse<ReadableStream>;
|
|
70
|
+
updateBlob(url: string, data: Blob, mimeType: string, requestConfig?: RequestConfigType, additionalHeaders?: Record<string, string>): ODataResponse<void | Blob>;
|
|
66
71
|
}
|
package/lib/BaseHttpClient.js
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const HttpMethods_1 = require("./HttpMethods");
|
|
7
|
-
exports.DEFAULT_CSRF_TOKEN_KEY = "x-csrf-token";
|
|
8
|
-
const EDIT_METHODS = [HttpMethods_1.HttpMethods.Post, HttpMethods_1.HttpMethods.Put, HttpMethods_1.HttpMethods.Patch, HttpMethods_1.HttpMethods.Delete];
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { retrieveErrorMessage } from "./ErrorMessageRetriever.js";
|
|
3
|
+
import { HttpMethods } from "./HttpMethods.js";
|
|
4
|
+
export const DEFAULT_CSRF_TOKEN_KEY = "x-csrf-token";
|
|
5
|
+
const EDIT_METHODS = [HttpMethods.Post, HttpMethods.Put, HttpMethods.Patch, HttpMethods.Delete];
|
|
9
6
|
const FAILURE_MISSING_CSRF_URL = "When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!";
|
|
10
7
|
const FAILURE_MISSING_URL = "Value for URL must be provided!";
|
|
11
8
|
const JSON_VALUE = "application/json";
|
|
12
|
-
function
|
|
9
|
+
function getInternalConfigWithJsonHeaders(headers, setContentType = true) {
|
|
13
10
|
return {
|
|
14
11
|
headers: Object.assign(Object.assign({ Accept: JSON_VALUE }, (setContentType ? { "Content-Type": JSON_VALUE } : undefined)), headers),
|
|
12
|
+
dataType: "json",
|
|
15
13
|
};
|
|
16
14
|
}
|
|
17
|
-
class BaseHttpClient {
|
|
15
|
+
export class BaseHttpClient {
|
|
18
16
|
constructor(baseOptions = { useCsrfProtection: false }) {
|
|
19
17
|
var _a;
|
|
20
18
|
this.baseOptions = baseOptions;
|
|
21
|
-
this.csrfTokenKey =
|
|
22
|
-
this.retrieveErrorMessage =
|
|
19
|
+
this.csrfTokenKey = DEFAULT_CSRF_TOKEN_KEY;
|
|
20
|
+
this.retrieveErrorMessage = retrieveErrorMessage;
|
|
23
21
|
if (baseOptions.useCsrfProtection && !((_a = baseOptions.csrfTokenFetchUrl) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
24
22
|
throw new Error(FAILURE_MISSING_CSRF_URL);
|
|
25
23
|
}
|
|
@@ -28,13 +26,13 @@ class BaseHttpClient {
|
|
|
28
26
|
return this.csrfTokenKey;
|
|
29
27
|
}
|
|
30
28
|
setCsrfTokenKey(newKey) {
|
|
31
|
-
this.csrfTokenKey = newKey ||
|
|
29
|
+
this.csrfTokenKey = newKey || DEFAULT_CSRF_TOKEN_KEY;
|
|
32
30
|
}
|
|
33
31
|
setErrorMessageRetriever(getErrorMsg) {
|
|
34
32
|
this.retrieveErrorMessage = getErrorMsg;
|
|
35
33
|
}
|
|
36
34
|
setupSecurityToken() {
|
|
37
|
-
return
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
36
|
if (!this.csrfToken) {
|
|
39
37
|
this.csrfToken = yield this.fetchSecurityToken();
|
|
40
38
|
}
|
|
@@ -42,9 +40,9 @@ class BaseHttpClient {
|
|
|
42
40
|
});
|
|
43
41
|
}
|
|
44
42
|
fetchSecurityToken() {
|
|
45
|
-
return
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
44
|
const fetchUrl = this.baseOptions.csrfTokenFetchUrl;
|
|
47
|
-
const response = yield this.sendRequest(
|
|
45
|
+
const response = yield this.sendRequest(HttpMethods.Get, fetchUrl, undefined, undefined, {
|
|
48
46
|
noBodyEvaluation: true,
|
|
49
47
|
headers: { [this.csrfTokenKey]: "Fetch", Accept: JSON_VALUE },
|
|
50
48
|
});
|
|
@@ -62,7 +60,7 @@ class BaseHttpClient {
|
|
|
62
60
|
* @private
|
|
63
61
|
*/
|
|
64
62
|
sendRequest(method, url, data, requestConfig, internalConfig = {}) {
|
|
65
|
-
return
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
64
|
// noinspection SuspiciousTypeOfGuard
|
|
67
65
|
if (typeof url !== "string") {
|
|
68
66
|
throw new Error(FAILURE_MISSING_URL);
|
|
@@ -78,7 +76,7 @@ class BaseHttpClient {
|
|
|
78
76
|
}
|
|
79
77
|
}
|
|
80
78
|
try {
|
|
81
|
-
return yield this.executeRequest(method, url, data, requestConfig, internalConfig);
|
|
79
|
+
return yield this.executeRequest(method, url, data, requestConfig, Object.keys(internalConfig).length ? internalConfig : undefined);
|
|
82
80
|
}
|
|
83
81
|
catch (e) {
|
|
84
82
|
const clientError = e;
|
|
@@ -96,20 +94,34 @@ class BaseHttpClient {
|
|
|
96
94
|
});
|
|
97
95
|
}
|
|
98
96
|
get(url, requestConfig, additionalHeaders) {
|
|
99
|
-
return this.sendRequest(
|
|
97
|
+
return this.sendRequest(HttpMethods.Get, url, undefined, requestConfig, getInternalConfigWithJsonHeaders(additionalHeaders, false));
|
|
100
98
|
}
|
|
101
99
|
post(url, data, requestConfig, additionalHeaders) {
|
|
102
|
-
return this.sendRequest(
|
|
100
|
+
return this.sendRequest(HttpMethods.Post, url, data, requestConfig, getInternalConfigWithJsonHeaders(additionalHeaders));
|
|
103
101
|
}
|
|
104
102
|
put(url, data, requestConfig, additionalHeaders) {
|
|
105
|
-
return this.sendRequest(
|
|
103
|
+
return this.sendRequest(HttpMethods.Put, url, data, requestConfig, getInternalConfigWithJsonHeaders(additionalHeaders));
|
|
106
104
|
}
|
|
107
105
|
patch(url, data, requestConfig, additionalHeaders) {
|
|
108
|
-
return this.sendRequest(
|
|
106
|
+
return this.sendRequest(HttpMethods.Patch, url, data, requestConfig, getInternalConfigWithJsonHeaders(additionalHeaders));
|
|
107
|
+
}
|
|
108
|
+
getAdditionalHeaders(additionalHeaders) {
|
|
109
|
+
return additionalHeaders ? { headers: additionalHeaders } : undefined;
|
|
109
110
|
}
|
|
110
111
|
delete(url, requestConfig, additionalHeaders) {
|
|
111
|
-
return this.sendRequest(
|
|
112
|
+
return this.sendRequest(HttpMethods.Delete, url, undefined, requestConfig, this.getAdditionalHeaders(additionalHeaders));
|
|
113
|
+
}
|
|
114
|
+
getBlob(url, requestConfig, additionalHeaders) {
|
|
115
|
+
return this.sendRequest(HttpMethods.Get, url, undefined, requestConfig, Object.assign(Object.assign({}, this.getAdditionalHeaders(additionalHeaders)), { dataType: "blob" }));
|
|
116
|
+
}
|
|
117
|
+
getStream(url, requestConfig, additionalHeaders) {
|
|
118
|
+
return this.sendRequest(HttpMethods.Get, url, undefined, requestConfig, Object.assign(Object.assign({}, this.getAdditionalHeaders(additionalHeaders)), { dataType: "stream" }));
|
|
119
|
+
}
|
|
120
|
+
updateBlob(url, data, mimeType, requestConfig, additionalHeaders) {
|
|
121
|
+
return this.sendRequest(HttpMethods.Put, url, data, requestConfig, {
|
|
122
|
+
headers: Object.assign(Object.assign({}, additionalHeaders), { Accept: mimeType, "Content-Type": mimeType }),
|
|
123
|
+
dataType: "blob",
|
|
124
|
+
});
|
|
112
125
|
}
|
|
113
126
|
}
|
|
114
|
-
exports.BaseHttpClient = BaseHttpClient;
|
|
115
127
|
//# sourceMappingURL=BaseHttpClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseHttpClient.js","sourceRoot":"","sources":["../src/BaseHttpClient.ts"],"names":[],"mappings":";;;;AAEA,mEAAsF;AACtF,+CAA4C;AA0B/B,QAAA,sBAAsB,GAAG,cAAc,CAAC;AAErD,MAAM,YAAY,GAAG,CAAC,yBAAW,CAAC,IAAI,EAAE,yBAAW,CAAC,GAAG,EAAE,yBAAW,CAAC,KAAK,EAAE,yBAAW,CAAC,MAAM,CAAC,CAAC;AAChG,MAAM,wBAAwB,GAC5B,8GAA8G,CAAC;AACjH,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;AAC9D,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAEtC,SAAS,iBAAiB,CAAC,OAAgC,EAAE,iBAA0B,IAAI;IACzF,OAAO;QACL,OAAO,gCACL,MAAM,EAAE,UAAU,IACf,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,GAC7D,OAAO,CACX;KACF,CAAC;AACJ,CAAC;AAED,MAAsB,cAAc;IAMlC,YAA8B,cAAqC,EAAE,iBAAiB,EAAE,KAAK,EAAE;;QAAjE,gBAAW,GAAX,WAAW,CAAsD;Q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EACf,GAAG,EACH,SAAS,EACT,aAAa,EACb,iBAAiB,CAAC,iBAAiB,EAAE,KAAK,CAAC,CAC5C,CAAC;IACJ,CAAC;IAEM,IAAI,CACT,GAAW,EACX,IAAS,EACT,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CACrB,yBAAW,CAAC,IAAI,EAChB,GAAG,EACH,IAAI,EACJ,aAAa,EACb,iBAAiB,CAAC,iBAAiB,CAAC,CACrC,CAAC;IACJ,CAAC;IAEM,GAAG,CACR,GAAW,EACX,IAAS,EACT,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CACrB,yBAAW,CAAC,GAAG,EACf,GAAG,EACH,IAAI,EACJ,aAAa,EACb,iBAAiB,CAAC,iBAAiB,CAAC,CACrC,CAAC;IACJ,CAAC;IAEM,KAAK,CACV,GAAW,EACX,IAAS,EACT,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CACrB,yBAAW,CAAC,KAAK,EACjB,GAAG,EACH,IAAI,EACJ,aAAa,EACb,iBAAiB,CAAC,iBAAiB,CAAC,CACrC,CAAC;IACJ,CAAC;IAEM,MAAM,CACX,GAAW,EACX,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CAAO,yBAAW,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAE,EAAE,OAAO,EAAE,iBAAiB,EAAE,CAAC,CAAC;IACnH,CAAC;CACF;AApLD,wCAoLC","sourcesContent":["import { HttpResponseModel, ODataClientError } from \"@odata2ts/http-client-api\";\r\n\r\nimport { ErrorMessageRetriever, retrieveErrorMessage } from \"./ErrorMessageRetriever\";\r\nimport { HttpMethods } from \"./HttpMethods\";\r\n\r\nexport interface BaseHttpClientOptions {\r\n /**\r\n * Enable automatic CSRF token handling.\r\n */\r\n useCsrfProtection?: boolean;\r\n /**\r\n * Specify the URL from which the token is fetched.\r\n * This could be any path to your OData service, since the token is exchanged via HTTP request headers.\r\n * However, it should be a fast response and usually the root URL to the OData service is a good choice.\r\n */\r\n csrfTokenFetchUrl?: string;\r\n}\r\n\r\nexport interface InternalHttpClientConfig {\r\n /**\r\n * Additional headers set internally by services or HttpClient implementation.\r\n */\r\n headers?: Record<string, string>;\r\n /**\r\n * Very special option needed for FetchClient to not evaluate the response body in certain situations.\r\n */\r\n noBodyEvaluation?: boolean;\r\n}\r\n\r\nexport const DEFAULT_CSRF_TOKEN_KEY = \"x-csrf-token\";\r\n\r\nconst EDIT_METHODS = [HttpMethods.Post, HttpMethods.Put, HttpMethods.Patch, HttpMethods.Delete];\r\nconst FAILURE_MISSING_CSRF_URL =\r\n \"When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!\";\r\nconst FAILURE_MISSING_URL = \"Value for URL must be provided!\";\r\nconst JSON_VALUE = \"application/json\";\r\n\r\nfunction getInternalConfig(headers?: Record<string, string>, setContentType: boolean = true) {\r\n return {\r\n headers: {\r\n Accept: JSON_VALUE,\r\n ...(setContentType ? { \"Content-Type\": JSON_VALUE } : undefined),\r\n ...headers,\r\n },\r\n };\r\n}\r\n\r\nexport abstract class BaseHttpClient<RequestConfigType> {\r\n private csrfToken: string | undefined;\r\n private csrfTokenKey = DEFAULT_CSRF_TOKEN_KEY;\r\n\r\n protected retrieveErrorMessage: ErrorMessageRetriever = retrieveErrorMessage;\r\n\r\n protected constructor(private baseOptions: BaseHttpClientOptions = { useCsrfProtection: false }) {\r\n if (baseOptions.useCsrfProtection && !baseOptions.csrfTokenFetchUrl?.trim()) {\r\n throw new Error(FAILURE_MISSING_CSRF_URL);\r\n }\r\n }\r\n\r\n /**\r\n * Main function to implement by any extending http client.\r\n * As it name suggests, the request gets executed in this method.\r\n * Additionally, failures should be handled and errors of type <code>HttpClientError</code> should be thrown.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param config\r\n * @param internalConfig\r\n */\r\n protected abstract executeRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n config?: RequestConfigType,\r\n internalConfig?: InternalHttpClientConfig\r\n ): Promise<HttpResponseModel<ResponseModel>>;\r\n\r\n public getCsrfTokenKey() {\r\n return this.csrfTokenKey;\r\n }\r\n\r\n public setCsrfTokenKey(newKey: string) {\r\n this.csrfTokenKey = newKey || DEFAULT_CSRF_TOKEN_KEY;\r\n }\r\n\r\n public setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever) {\r\n this.retrieveErrorMessage = getErrorMsg;\r\n }\r\n\r\n protected async setupSecurityToken(): Promise<[string, string | undefined]> {\r\n if (!this.csrfToken) {\r\n this.csrfToken = await this.fetchSecurityToken();\r\n }\r\n return [this.csrfTokenKey, this.csrfToken];\r\n }\r\n\r\n protected async fetchSecurityToken(): Promise<string | undefined> {\r\n const fetchUrl = this.baseOptions!.csrfTokenFetchUrl!;\r\n const response = await this.sendRequest(HttpMethods.Get, fetchUrl, undefined, undefined, {\r\n noBodyEvaluation: true,\r\n headers: { [this.csrfTokenKey]: \"Fetch\", Accept: JSON_VALUE },\r\n });\r\n\r\n return response.headers[this.csrfTokenKey];\r\n }\r\n\r\n /**\r\n * Follows the template pattern.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param requestConfig\r\n * @param internalConfig\r\n * @private\r\n */\r\n private async sendRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n internalConfig: InternalHttpClientConfig = {}\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n // noinspection SuspiciousTypeOfGuard\r\n if (typeof url !== \"string\") {\r\n throw new Error(FAILURE_MISSING_URL);\r\n }\r\n\r\n // setup automatic CSRF token handling\r\n if (this.baseOptions.useCsrfProtection && EDIT_METHODS.includes(method)) {\r\n const [tokenKey, tokenValue] = await this.setupSecurityToken();\r\n if (tokenValue) {\r\n if (!internalConfig.headers) {\r\n internalConfig.headers = {};\r\n }\r\n internalConfig.headers[tokenKey] = tokenValue;\r\n }\r\n }\r\n\r\n try {\r\n return await this.executeRequest<ResponseModel>(method, url, data, requestConfig, internalConfig);\r\n } catch (e) {\r\n const clientError = e as ODataClientError;\r\n\r\n // automatic CSRF token handling\r\n if (\r\n !!this.baseOptions.useCsrfProtection &&\r\n clientError.status === 403 &&\r\n !!clientError.headers &&\r\n clientError.headers[\"x-csrf-token\"] === \"Required\"\r\n ) {\r\n // token has expired: reset csrf token & perform the original request again\r\n this.csrfToken = undefined;\r\n return this.sendRequest<ResponseModel>(method, url, data, requestConfig);\r\n }\r\n\r\n throw e;\r\n }\r\n }\r\n\r\n public get<ResponseModel>(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Get,\r\n url,\r\n undefined,\r\n requestConfig,\r\n getInternalConfig(additionalHeaders, false)\r\n );\r\n }\r\n\r\n public post<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Post,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfig(additionalHeaders)\r\n );\r\n }\r\n\r\n public put<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Put,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfig(additionalHeaders)\r\n );\r\n }\r\n\r\n public patch<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Patch,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfig(additionalHeaders)\r\n );\r\n }\r\n\r\n public delete(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>\r\n ): Promise<HttpResponseModel<void>> {\r\n return this.sendRequest<void>(HttpMethods.Delete, url, undefined, requestConfig, { headers: additionalHeaders });\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"BaseHttpClient.js","sourceRoot":"","sources":["../src/BaseHttpClient.ts"],"names":[],"mappings":";AACA,OAAO,EAAyB,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AACzF,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AA2B/C,MAAM,CAAC,MAAM,sBAAsB,GAAG,cAAc,CAAC;AAErD,MAAM,YAAY,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;AAChG,MAAM,wBAAwB,GAC5B,8GAA8G,CAAC;AACjH,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;AAC9D,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAEtC,SAAS,gCAAgC,CACvC,OAAgC,EAChC,iBAA0B,IAAI;IAE9B,OAAO;QACL,OAAO,gCACL,MAAM,EAAE,UAAU,IACf,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,GAC7D,OAAO,CACX;QACD,QAAQ,EAAE,MAAM;KACjB,CAAC;AACJ,CAAC;AAED,MAAM,OAAgB,cAAc;IAMlC,YAA8B,cAAqC,EAAE,iBAAiB,EAAE,KAAK,EAAE;;QAAjE,gBAAW,GAAX,WAAW,CAAsD;QAJvF,iBAAY,GAAG,sBAAsB,CAAC;QAEpC,yBAAoB,GAA0B,oBAAoB,CAAC;QAG3E,IAAI,WAAW,CAAC,iBAAiB,IAAI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iB,CAAC,KAC/C,QAAQ,EAAE,MAAM,IAChB,CAAC;IACL,CAAC;IAEM,SAAS,CACd,GAAW,EACX,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,kCACjE,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,KAC/C,QAAQ,EAAE,QAAQ,IAClB,CAAC;IACL,CAAC;IAEM,UAAU,CACf,GAAW,EACX,IAAU,EACV,QAAgB,EAChB,aAAiC,EACjC,iBAA0C;QAE1C,OAAO,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,EAAE;YACjE,OAAO,kCAAO,iBAAiB,KAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,GAAE;YAC7E,QAAQ,EAAE,MAAM;SACjB,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { HttpResponseModel, ODataClientError, ODataResponse } from \"@odata2ts/http-client-api\";\r\nimport { ErrorMessageRetriever, retrieveErrorMessage } from \"./ErrorMessageRetriever.js\";\r\nimport { HttpMethods } from \"./HttpMethods.js\";\r\n\r\nexport interface BaseHttpClientOptions {\r\n /**\r\n * Enable automatic CSRF token handling.\r\n */\r\n useCsrfProtection?: boolean;\r\n /**\r\n * Specify the URL from which the token is fetched.\r\n * This could be any path to your OData service, since the token is exchanged via HTTP request headers.\r\n * However, it should be a fast response and usually the root URL to the OData service is a good choice.\r\n */\r\n csrfTokenFetchUrl?: string;\r\n}\r\n\r\nexport interface InternalHttpClientConfig {\r\n dataType?: \"json\" | \"blob\" | \"stream\";\r\n /**\r\n * Additional headers set internally by services or HttpClient implementation.\r\n */\r\n headers?: Record<string, string>;\r\n /**\r\n * Very special option needed for FetchClient to not evaluate the response body in certain situations.\r\n */\r\n noBodyEvaluation?: boolean;\r\n}\r\n\r\nexport const DEFAULT_CSRF_TOKEN_KEY = \"x-csrf-token\";\r\n\r\nconst EDIT_METHODS = [HttpMethods.Post, HttpMethods.Put, HttpMethods.Patch, HttpMethods.Delete];\r\nconst FAILURE_MISSING_CSRF_URL =\r\n \"When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!\";\r\nconst FAILURE_MISSING_URL = \"Value for URL must be provided!\";\r\nconst JSON_VALUE = \"application/json\";\r\n\r\nfunction getInternalConfigWithJsonHeaders(\r\n headers?: Record<string, string>,\r\n setContentType: boolean = true,\r\n): InternalHttpClientConfig {\r\n return {\r\n headers: {\r\n Accept: JSON_VALUE,\r\n ...(setContentType ? { \"Content-Type\": JSON_VALUE } : undefined),\r\n ...headers,\r\n },\r\n dataType: \"json\",\r\n };\r\n}\r\n\r\nexport abstract class BaseHttpClient<RequestConfigType> {\r\n private csrfToken: string | undefined;\r\n private csrfTokenKey = DEFAULT_CSRF_TOKEN_KEY;\r\n\r\n protected retrieveErrorMessage: ErrorMessageRetriever = retrieveErrorMessage;\r\n\r\n protected constructor(private baseOptions: BaseHttpClientOptions = { useCsrfProtection: false }) {\r\n if (baseOptions.useCsrfProtection && !baseOptions.csrfTokenFetchUrl?.trim()) {\r\n throw new Error(FAILURE_MISSING_CSRF_URL);\r\n }\r\n }\r\n\r\n /**\r\n * Main function to implement by any extending http client.\r\n * As it name suggests, the request gets executed in this method.\r\n * Additionally, failures should be handled and errors of type <code>HttpClientError</code> should be thrown.\r\n *\r\n * @param method the http method to use\r\n * @param url the URL to use\r\n * @param data data for the request body if any\r\n * @param config request configuration from end user which should override default settings\r\n * @param internalConfig request configuration from base client including additional headers which should override end user configurations\r\n */\r\n protected abstract executeRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n config?: RequestConfigType,\r\n internalConfig?: InternalHttpClientConfig,\r\n ): Promise<HttpResponseModel<ResponseModel>>;\r\n\r\n public getCsrfTokenKey() {\r\n return this.csrfTokenKey;\r\n }\r\n\r\n public setCsrfTokenKey(newKey: string) {\r\n this.csrfTokenKey = newKey || DEFAULT_CSRF_TOKEN_KEY;\r\n }\r\n\r\n public setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever) {\r\n this.retrieveErrorMessage = getErrorMsg;\r\n }\r\n\r\n protected async setupSecurityToken(): Promise<[string, string | undefined]> {\r\n if (!this.csrfToken) {\r\n this.csrfToken = await this.fetchSecurityToken();\r\n }\r\n return [this.csrfTokenKey, this.csrfToken];\r\n }\r\n\r\n protected async fetchSecurityToken(): Promise<string | undefined> {\r\n const fetchUrl = this.baseOptions!.csrfTokenFetchUrl!;\r\n const response = await this.sendRequest(HttpMethods.Get, fetchUrl, undefined, undefined, {\r\n noBodyEvaluation: true,\r\n headers: { [this.csrfTokenKey]: \"Fetch\", Accept: JSON_VALUE },\r\n });\r\n\r\n return response.headers[this.csrfTokenKey];\r\n }\r\n\r\n /**\r\n * Follows the template pattern.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param requestConfig\r\n * @param internalConfig\r\n * @private\r\n */\r\n private async sendRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n internalConfig: InternalHttpClientConfig = {},\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n // noinspection SuspiciousTypeOfGuard\r\n if (typeof url !== \"string\") {\r\n throw new Error(FAILURE_MISSING_URL);\r\n }\r\n\r\n // setup automatic CSRF token handling\r\n if (this.baseOptions.useCsrfProtection && EDIT_METHODS.includes(method)) {\r\n const [tokenKey, tokenValue] = await this.setupSecurityToken();\r\n if (tokenValue) {\r\n if (!internalConfig.headers) {\r\n internalConfig.headers = {};\r\n }\r\n internalConfig.headers[tokenKey] = tokenValue;\r\n }\r\n }\r\n\r\n try {\r\n return await this.executeRequest<ResponseModel>(\r\n method,\r\n url,\r\n data,\r\n requestConfig,\r\n Object.keys(internalConfig).length ? internalConfig : undefined,\r\n );\r\n } catch (e) {\r\n const clientError = e as ODataClientError;\r\n\r\n // automatic CSRF token handling\r\n if (\r\n !!this.baseOptions.useCsrfProtection &&\r\n clientError.status === 403 &&\r\n !!clientError.headers &&\r\n clientError.headers[\"x-csrf-token\"] === \"Required\"\r\n ) {\r\n // token has expired: reset csrf token & perform the original request again\r\n this.csrfToken = undefined;\r\n return this.sendRequest<ResponseModel>(method, url, data, requestConfig);\r\n }\r\n\r\n throw e;\r\n }\r\n }\r\n\r\n public get<ResponseModel>(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Get,\r\n url,\r\n undefined,\r\n requestConfig,\r\n getInternalConfigWithJsonHeaders(additionalHeaders, false),\r\n );\r\n }\r\n\r\n public post<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Post,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfigWithJsonHeaders(additionalHeaders),\r\n );\r\n }\r\n\r\n public put<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Put,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfigWithJsonHeaders(additionalHeaders),\r\n );\r\n }\r\n\r\n public patch<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Patch,\r\n url,\r\n data,\r\n requestConfig,\r\n getInternalConfigWithJsonHeaders(additionalHeaders),\r\n );\r\n }\r\n\r\n private getAdditionalHeaders(additionalHeaders?: Record<string, string>) {\r\n return additionalHeaders ? { headers: additionalHeaders } : undefined;\r\n }\r\n\r\n public delete(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): Promise<HttpResponseModel<void>> {\r\n return this.sendRequest<void>(\r\n HttpMethods.Delete,\r\n url,\r\n undefined,\r\n requestConfig,\r\n this.getAdditionalHeaders(additionalHeaders),\r\n );\r\n }\r\n\r\n public getBlob(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): ODataResponse<Blob> {\r\n return this.sendRequest(HttpMethods.Get, url, undefined, requestConfig, {\r\n ...this.getAdditionalHeaders(additionalHeaders),\r\n dataType: \"blob\",\r\n });\r\n }\r\n\r\n public getStream(\r\n url: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): ODataResponse<ReadableStream> {\r\n return this.sendRequest(HttpMethods.Get, url, undefined, requestConfig, {\r\n ...this.getAdditionalHeaders(additionalHeaders),\r\n dataType: \"stream\",\r\n });\r\n }\r\n\r\n public updateBlob(\r\n url: string,\r\n data: Blob,\r\n mimeType: string,\r\n requestConfig?: RequestConfigType,\r\n additionalHeaders?: Record<string, string>,\r\n ): ODataResponse<void | Blob> {\r\n return this.sendRequest(HttpMethods.Put, url, data, requestConfig, {\r\n headers: { ...additionalHeaders, Accept: mimeType, \"Content-Type\": mimeType },\r\n dataType: \"blob\",\r\n });\r\n }\r\n}\r\n"]}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.retrieveErrorMessage = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Retrieves the OData error message from the response (V2 and V4 are supported).
|
|
6
3
|
* The structure of the error message is specified by OData.
|
|
7
4
|
*
|
|
8
5
|
* @param errorResponse
|
|
9
6
|
*/
|
|
10
|
-
const retrieveErrorMessage = (errorResponse) => {
|
|
7
|
+
export const retrieveErrorMessage = (errorResponse) => {
|
|
11
8
|
var _a;
|
|
12
9
|
const eMsg = (_a = errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.error) === null || _a === void 0 ? void 0 : _a.message;
|
|
13
10
|
return typeof (eMsg === null || eMsg === void 0 ? void 0 : eMsg.value) === "string" ? eMsg.value : eMsg;
|
|
14
11
|
};
|
|
15
|
-
exports.retrieveErrorMessage = retrieveErrorMessage;
|
|
16
12
|
//# sourceMappingURL=ErrorMessageRetriever.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ErrorMessageRetriever.js","sourceRoot":"","sources":["../src/ErrorMessageRetriever.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ErrorMessageRetriever.js","sourceRoot":"","sources":["../src/ErrorMessageRetriever.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA0B,CAAC,aAAkB,EAAsB,EAAE;;IACpG,MAAM,IAAI,GAAG,MAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,KAAK,0CAAE,OAAO,CAAC;IAC3C,OAAO,OAAO,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,CAAA,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7D,CAAC,CAAC","sourcesContent":["/**\n * Function type used to retrieve the error message from the response payload.\n *\n * @return error message or <code>undefined</code> if retrieval failed / had no outcome\n */\nexport type ErrorMessageRetriever = (errorResponse: any) => string | undefined;\n\n/**\n * Retrieves the OData error message from the response (V2 and V4 are supported).\n * The structure of the error message is specified by OData.\n *\n * @param errorResponse\n */\nexport const retrieveErrorMessage: ErrorMessageRetriever = (errorResponse: any): string | undefined => {\n const eMsg = errorResponse?.error?.message;\n return typeof eMsg?.value === \"string\" ? eMsg.value : eMsg;\n};\n"]}
|
package/lib/HttpMethods.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpMethods = void 0;
|
|
4
|
-
var HttpMethods;
|
|
1
|
+
export var HttpMethods;
|
|
5
2
|
(function (HttpMethods) {
|
|
6
3
|
HttpMethods["Get"] = "GET";
|
|
7
4
|
HttpMethods["Post"] = "POST";
|
|
8
5
|
HttpMethods["Put"] = "PUT";
|
|
9
6
|
HttpMethods["Patch"] = "PATCH";
|
|
10
7
|
HttpMethods["Delete"] = "DELETE";
|
|
11
|
-
})(HttpMethods
|
|
8
|
+
})(HttpMethods || (HttpMethods = {}));
|
|
12
9
|
//# sourceMappingURL=HttpMethods.js.map
|
package/lib/HttpMethods.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpMethods.js","sourceRoot":"","sources":["../src/HttpMethods.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"HttpMethods.js","sourceRoot":"","sources":["../src/HttpMethods.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,WAMX;AAND,WAAY,WAAW;IACrB,0BAAW,CAAA;IACX,4BAAa,CAAA;IACb,0BAAW,CAAA;IACX,8BAAe,CAAA;IACf,gCAAiB,CAAA;AACnB,CAAC,EANW,WAAW,KAAX,WAAW,QAMtB","sourcesContent":["export enum HttpMethods {\n Get = \"GET\",\n Post = \"POST\",\n Put = \"PUT\",\n Patch = \"PATCH\",\n Delete = \"DELETE\",\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./ErrorMessageRetriever";
|
|
2
|
-
export { BaseHttpClient, BaseHttpClientOptions, InternalHttpClientConfig } from "./BaseHttpClient";
|
|
3
|
-
export { HttpMethods } from "./HttpMethods";
|
|
1
|
+
export * from "./ErrorMessageRetriever.js";
|
|
2
|
+
export { BaseHttpClient, BaseHttpClientOptions, InternalHttpClientConfig } from "./BaseHttpClient.js";
|
|
3
|
+
export { HttpMethods } from "./HttpMethods.js";
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./ErrorMessageRetriever"), exports);
|
|
6
|
-
var BaseHttpClient_1 = require("./BaseHttpClient");
|
|
7
|
-
Object.defineProperty(exports, "BaseHttpClient", { enumerable: true, get: function () { return BaseHttpClient_1.BaseHttpClient; } });
|
|
8
|
-
var HttpMethods_1 = require("./HttpMethods");
|
|
9
|
-
Object.defineProperty(exports, "HttpMethods", { enumerable: true, get: function () { return HttpMethods_1.HttpMethods; } });
|
|
1
|
+
export * from "./ErrorMessageRetriever.js";
|
|
2
|
+
export { BaseHttpClient } from "./BaseHttpClient.js";
|
|
3
|
+
export { HttpMethods } from "./HttpMethods.js";
|
|
10
4
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAmD,MAAM,qBAAqB,CAAC;AACtG,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC","sourcesContent":["export * from \"./ErrorMessageRetriever.js\";\r\nexport { BaseHttpClient, BaseHttpClientOptions, InternalHttpClientConfig } from \"./BaseHttpClient.js\";\r\nexport { HttpMethods } from \"./HttpMethods.js\";\r\n"]}
|
package/package.json
CHANGED
|
@@ -1,46 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odata2ts/http-client-base",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"publishConfig": {
|
|
5
|
-
"access": "public"
|
|
6
|
-
},
|
|
3
|
+
"version": "0.5.1",
|
|
7
4
|
"description": "Core functionality for odata2ts HTTP clients",
|
|
8
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"http client",
|
|
7
|
+
"odata",
|
|
8
|
+
"ts",
|
|
9
|
+
"odata2ts"
|
|
10
|
+
],
|
|
9
11
|
"repository": "git@github.com:odata2ts/http-client.git",
|
|
12
|
+
"license": "MIT",
|
|
10
13
|
"author": "texttechne",
|
|
14
|
+
"type": "module",
|
|
11
15
|
"main": "./lib/index.js",
|
|
16
|
+
"types": "./lib/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"*.md",
|
|
19
|
+
"lib",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
12
22
|
"scripts": {
|
|
13
23
|
"build": "yarn clean && yarn compile",
|
|
14
24
|
"check-circular-deps": "madge ./src --extensions ts --circular",
|
|
15
25
|
"clean": "rimraf lib coverage",
|
|
16
26
|
"compile": "tsc",
|
|
17
27
|
"prepublish": "yarn build",
|
|
18
|
-
"test": "
|
|
28
|
+
"test": "vitest run ./test"
|
|
19
29
|
},
|
|
20
|
-
"files": [
|
|
21
|
-
"*.md",
|
|
22
|
-
"lib",
|
|
23
|
-
"LICENSE"
|
|
24
|
-
],
|
|
25
|
-
"keywords": [
|
|
26
|
-
"http client",
|
|
27
|
-
"odata",
|
|
28
|
-
"ts",
|
|
29
|
-
"odata2ts"
|
|
30
|
-
],
|
|
31
30
|
"dependencies": {
|
|
32
|
-
"@odata2ts/http-client-api": "^0.
|
|
31
|
+
"@odata2ts/http-client-api": "^0.6.1"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@odata2ts/odata-core": "^0.3.7",
|
|
36
|
-
"@types/jest": "^29.5.2",
|
|
37
35
|
"@types/node": "^20.2.5",
|
|
38
|
-
"jest": "^29.5.0",
|
|
39
36
|
"rimraf": "^5.0.1",
|
|
40
|
-
"ts-jest": "^29.1.0",
|
|
41
|
-
"ts-node": "10.9.1",
|
|
42
37
|
"typescript": "5.0.4"
|
|
43
38
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "232521cbfae692d00900237845bd317b11e442d2"
|
|
46
43
|
}
|