@odata2ts/http-client-fetch 0.1.0 → 0.3.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/CHANGELOG.md +13 -0
- package/README.md +12 -7
- package/lib/FetchClient.d.ts +15 -0
- package/lib/FetchClient.js +78 -0
- package/lib/FetchClient.js.map +1 -0
- package/lib/FetchClientError.d.ts +8 -0
- package/lib/{FetchODataClientError.js → FetchClientError.js} +6 -5
- package/lib/FetchClientError.js.map +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/lib/FetchODataClient.d.ts +0 -28
- package/lib/FetchODataClient.js +0 -148
- package/lib/FetchODataClient.js.map +0 -1
- package/lib/FetchODataClientError.d.ts +0 -6
- package/lib/FetchODataClientError.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,19 @@
|
|
|
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.3.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.2.0...@odata2ts/http-client-fetch@0.3.0) (2023-06-10)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* conventionalize client errors ([#5](https://github.com/odata2ts/http-client/issues/5)) ([a8e8912](https://github.com/odata2ts/http-client/commit/a8e89125eeda47436d48507d6a71efc90953f878))
|
|
11
|
+
|
|
12
|
+
# [0.2.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.1.0...@odata2ts/http-client-fetch@0.2.0) (2023-06-03)
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* switch to http-client-api ([52d1b86](https://github.com/odata2ts/http-client/commit/52d1b868ee82dbaf45486da6b22fdcf4c773dfb8))
|
|
17
|
+
* switch to http-client-api ([5a6da23](https://github.com/odata2ts/http-client/commit/5a6da23053b3ea5adb866bb7e30b469f1b8ed260))
|
|
18
|
+
|
|
6
19
|
# 0.1.0 (2023-05-29)
|
|
7
20
|
|
|
8
21
|
### Features
|
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
[](https://www.npmjs.com/package/@odata2ts/http-client-fetch)
|
|
2
2
|
|
|
3
|
-
# Fetch
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
# Fetch HTTP Client
|
|
4
|
+
|
|
5
|
+
Fetch based HTTP client for [odata2ts](https://github.com/odata2ts/odata2ts).
|
|
6
|
+
This client uses - as its name suggests - [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
|
|
6
7
|
for realizing the HTTP communication.
|
|
7
8
|
|
|
8
9
|
It supports:
|
|
@@ -10,6 +11,8 @@ It supports:
|
|
|
10
11
|
- request configuration
|
|
11
12
|
- automatic CSRF token handling
|
|
12
13
|
|
|
14
|
+
Works also for Node.js v18+, but is still marked as **experimental**.
|
|
15
|
+
|
|
13
16
|
## Installation
|
|
14
17
|
|
|
15
18
|
Install package `@odata2ts/http-client-fetch` as runtime dependency:
|
|
@@ -19,27 +22,29 @@ npm install --save @odata2ts/http-client-fetch
|
|
|
19
22
|
```
|
|
20
23
|
|
|
21
24
|
## Documentation
|
|
22
|
-
|
|
25
|
+
|
|
26
|
+
[Fetch Client Documentation](https://odata2ts.github.io/docs/odata-client/http-client/fetch)
|
|
23
27
|
|
|
24
28
|
Main documentation for the odata2ts eco system:
|
|
25
29
|
[https://odata2ts.github.io](https://odata2ts.github.io/)
|
|
26
30
|
|
|
27
31
|
## Tests
|
|
32
|
+
|
|
28
33
|
See folder [test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/test)
|
|
29
34
|
for unit tests.
|
|
30
35
|
|
|
31
36
|
See folder [int-test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/int-test) for
|
|
32
37
|
integration tests.
|
|
33
38
|
|
|
34
|
-
|
|
35
39
|
## Support, Feedback, Contributing
|
|
36
40
|
|
|
37
|
-
This project is open to feature requests, suggestions, bug reports, usage questions etc.
|
|
38
|
-
via [GitHub issues](https://github.com/odata2ts/http-client/issues).
|
|
41
|
+
This project is open to feature requests, suggestions, bug reports, usage questions etc.
|
|
42
|
+
via [GitHub issues](https://github.com/odata2ts/http-client/issues).
|
|
39
43
|
|
|
40
44
|
Contributions and feedback are encouraged and always welcome.
|
|
41
45
|
|
|
42
46
|
See the [contribution guidelines](https://github.com/odata2ts/http-client/blob/main/CONTRIBUTING.md) for further information.
|
|
43
47
|
|
|
44
48
|
## License
|
|
49
|
+
|
|
45
50
|
MIT - see [License](./LICENSE).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HttpResponseModel } from "@odata2ts/http-client-api";
|
|
2
|
+
import { BaseHttpClient, BaseHttpClientOptions, HttpMethods } from "@odata2ts/http-client-base";
|
|
3
|
+
import { FetchRequestConfig } from "./FetchRequestConfig";
|
|
4
|
+
export interface ClientOptions extends BaseHttpClientOptions {
|
|
5
|
+
}
|
|
6
|
+
export declare const DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
7
|
+
export declare class FetchClient extends BaseHttpClient<FetchRequestConfig> {
|
|
8
|
+
private clientOptions?;
|
|
9
|
+
protected readonly config: RequestInit;
|
|
10
|
+
constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions | undefined);
|
|
11
|
+
protected addHeaderToRequestConfig(headers: Record<string, string>, config: FetchRequestConfig | undefined): FetchRequestConfig;
|
|
12
|
+
protected executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, requestConfig?: FetchRequestConfig | undefined): Promise<HttpResponseModel<ResponseModel>>;
|
|
13
|
+
protected getResponseBody(response: Response, isFailedJsonFatal: boolean): Promise<any>;
|
|
14
|
+
protected mapHeaders(headers: Headers): Record<string, string>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FetchClient = exports.DEFAULT_ERROR_MESSAGE = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const http_client_base_1 = require("@odata2ts/http-client-base");
|
|
6
|
+
const FetchClientError_1 = require("./FetchClientError");
|
|
7
|
+
const FetchRequestConfig_1 = require("./FetchRequestConfig");
|
|
8
|
+
exports.DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
9
|
+
const FETCH_FAILURE_MESSAGE = "OData request failed entirely: ";
|
|
10
|
+
const JSON_RETRIEVAL_FAILURE_MESSAGE = "Retrieving JSON body from OData response failed: ";
|
|
11
|
+
const RESPONSE_FAILURE_MESSAGE = "OData server responded with error: ";
|
|
12
|
+
function buildErrorMessage(prefix, error) {
|
|
13
|
+
const msg = typeof error === "string" ? error : error === null || error === void 0 ? void 0 : error.message;
|
|
14
|
+
return prefix + (msg || exports.DEFAULT_ERROR_MESSAGE);
|
|
15
|
+
}
|
|
16
|
+
class FetchClient extends http_client_base_1.BaseHttpClient {
|
|
17
|
+
constructor(config, clientOptions) {
|
|
18
|
+
super(clientOptions);
|
|
19
|
+
this.clientOptions = clientOptions;
|
|
20
|
+
this.config = (0, FetchRequestConfig_1.getDefaultConfig)(config);
|
|
21
|
+
}
|
|
22
|
+
addHeaderToRequestConfig(headers, config) {
|
|
23
|
+
return (0, FetchRequestConfig_1.mergeFetchConfig)(config, { headers });
|
|
24
|
+
}
|
|
25
|
+
executeRequest(method, url, data, requestConfig = {}) {
|
|
26
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const config = (0, FetchRequestConfig_1.mergeFetchConfig)(this.config, requestConfig);
|
|
28
|
+
config.method = method;
|
|
29
|
+
if (typeof data !== "undefined") {
|
|
30
|
+
config.body = JSON.stringify(data);
|
|
31
|
+
}
|
|
32
|
+
// the actual request
|
|
33
|
+
let response;
|
|
34
|
+
try {
|
|
35
|
+
response = yield fetch(url, config);
|
|
36
|
+
}
|
|
37
|
+
catch (fetchError) {
|
|
38
|
+
throw new FetchClientError_1.FetchClientError(buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError), undefined, undefined, fetchError);
|
|
39
|
+
}
|
|
40
|
+
// error response
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
let responseData = yield this.getResponseBody(response, false);
|
|
43
|
+
const errMsg = this.retrieveErrorMessage(responseData);
|
|
44
|
+
throw new FetchClientError_1.FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), response);
|
|
45
|
+
}
|
|
46
|
+
const responseData = yield this.getResponseBody(response, true);
|
|
47
|
+
return {
|
|
48
|
+
status: response.status,
|
|
49
|
+
statusText: response.statusText,
|
|
50
|
+
headers: this.mapHeaders(response.headers),
|
|
51
|
+
data: responseData,
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
getResponseBody(response, isFailedJsonFatal) {
|
|
56
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
if (response.status === 204) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return yield response.json();
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (isFailedJsonFatal) {
|
|
65
|
+
throw new FetchClientError_1.FetchClientError(buildErrorMessage(JSON_RETRIEVAL_FAILURE_MESSAGE, error), response.status, this.mapHeaders(response.headers), error);
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
mapHeaders(headers) {
|
|
72
|
+
const result = {};
|
|
73
|
+
headers.forEach((value, key) => (result[key] = value));
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.FetchClient = FetchClient;
|
|
78
|
+
//# sourceMappingURL=FetchClient.js.map
|
|
@@ -0,0 +1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,EAAE;gBACd,IAAI,iBAAiB,EAAE;oBACrB,MAAM,IAAI,mCAAgB,CACxB,iBAAiB,CAAC,8BAA8B,EAAE,KAAK,CAAC,EACxD,QAAQ,CAAC,MAAM,EACf,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,EACjC,KAAc,CACf,CAAC;iBACH;gBACD,OAAO,SAAS,CAAC;aAClB;QACH,CAAC;KAAA;IAES,UAAU,CAAC,OAAgB;QACnC,MAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;QAEvD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAzFD,kCAyFC","sourcesContent":["import { HttpResponseModel } from \"@odata2ts/http-client-api\";\r\nimport { BaseHttpClient, BaseHttpClientOptions, HttpMethods } from \"@odata2ts/http-client-base\";\r\n\r\nimport { FetchClientError } from \"./FetchClientError\";\r\nimport { FetchRequestConfig, getDefaultConfig, mergeFetchConfig } from \"./FetchRequestConfig\";\r\n\r\nexport interface ClientOptions extends BaseHttpClientOptions {}\r\n\r\nexport const DEFAULT_ERROR_MESSAGE = \"No error message!\";\r\nconst FETCH_FAILURE_MESSAGE = \"OData request failed entirely: \";\r\nconst JSON_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving JSON body from OData response failed: \";\r\nconst RESPONSE_FAILURE_MESSAGE = \"OData server responded with error: \";\r\n\r\nfunction buildErrorMessage(prefix: string, error: any) {\r\n const msg = typeof error === \"string\" ? error : (error as Error)?.message;\r\n return prefix + (msg || DEFAULT_ERROR_MESSAGE);\r\n}\r\n\r\nexport class FetchClient extends BaseHttpClient<FetchRequestConfig> {\r\n protected readonly config: RequestInit;\r\n\r\n constructor(config?: FetchRequestConfig, private clientOptions?: ClientOptions) {\r\n super(clientOptions);\r\n this.config = getDefaultConfig(config);\r\n }\r\n\r\n protected addHeaderToRequestConfig(\r\n headers: Record<string, string>,\r\n config: FetchRequestConfig | undefined\r\n ): FetchRequestConfig {\r\n return mergeFetchConfig(config, { headers });\r\n }\r\n\r\n protected async executeRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n requestConfig: FetchRequestConfig | undefined = {}\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n const config = mergeFetchConfig(this.config, requestConfig);\r\n config.method = method;\r\n if (typeof data !== \"undefined\") {\r\n config.body = JSON.stringify(data);\r\n }\r\n\r\n // the actual request\r\n let response: Response;\r\n try {\r\n response = await fetch(url, config);\r\n } catch (fetchError) {\r\n throw new FetchClientError(\r\n buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError),\r\n undefined,\r\n undefined,\r\n fetchError as Error\r\n );\r\n }\r\n\r\n // error response\r\n if (!response.ok) {\r\n let responseData = await this.getResponseBody(response, false);\r\n const errMsg = this.retrieveErrorMessage(responseData);\r\n\r\n throw new FetchClientError(\r\n buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg),\r\n response.status,\r\n this.mapHeaders(response.headers),\r\n new Error(errMsg || DEFAULT_ERROR_MESSAGE),\r\n response\r\n );\r\n }\r\n\r\n const responseData = await this.getResponseBody(response, true);\r\n\r\n return {\r\n status: response.status,\r\n statusText: response.statusText,\r\n headers: this.mapHeaders(response.headers),\r\n data: responseData,\r\n };\r\n }\r\n\r\n protected async getResponseBody(response: Response, isFailedJsonFatal: boolean) {\r\n if (response.status === 204) {\r\n return undefined;\r\n }\r\n try {\r\n return await response.json();\r\n } catch (error) {\r\n if (isFailedJsonFatal) {\r\n throw new FetchClientError(\r\n buildErrorMessage(JSON_RETRIEVAL_FAILURE_MESSAGE, error),\r\n response.status,\r\n this.mapHeaders(response.headers),\r\n error as Error\r\n );\r\n }\r\n return undefined;\r\n }\r\n }\r\n\r\n protected mapHeaders(headers: Headers): Record<string, string> {\r\n const result: Record<string, string> = {};\r\n headers.forEach((value, key) => (result[key] = value));\r\n\r\n return result;\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ODataClientError } from "@odata2ts/http-client-api";
|
|
2
|
+
export declare class FetchClientError extends Error implements ODataClientError {
|
|
3
|
+
readonly status?: number | undefined;
|
|
4
|
+
readonly headers?: Record<string, string> | undefined;
|
|
5
|
+
readonly cause?: Error | undefined;
|
|
6
|
+
readonly response?: Response | undefined;
|
|
7
|
+
constructor(message: string, status?: number | undefined, headers?: Record<string, string> | undefined, cause?: Error | undefined, response?: Response | undefined);
|
|
8
|
+
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
5
|
-
constructor(message, status, cause, response) {
|
|
3
|
+
exports.FetchClientError = void 0;
|
|
4
|
+
class FetchClientError extends Error {
|
|
5
|
+
constructor(message, status, headers, cause, response) {
|
|
6
6
|
// @ts-ignore: fetch requires lib "dom" or "webworker", but then the "cause" property becomes unknown to TS
|
|
7
7
|
super(message, { cause });
|
|
8
8
|
this.status = status;
|
|
9
|
+
this.headers = headers;
|
|
9
10
|
this.cause = cause;
|
|
10
11
|
this.response = response;
|
|
11
12
|
this.name = this.constructor.name;
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
|
-
exports.
|
|
15
|
-
//# sourceMappingURL=
|
|
15
|
+
exports.FetchClientError = FetchClientError;
|
|
16
|
+
//# sourceMappingURL=FetchClientError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FetchClientError.js","sourceRoot":"","sources":["../src/FetchClientError.ts"],"names":[],"mappings":";;;AAEA,MAAa,gBAAiB,SAAQ,KAAK;IACzC,YACE,OAAe,EACC,MAAe,EACf,OAAgC,EAChC,KAAa,EACb,QAAmB;QAEnC,2GAA2G;QAC3G,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QANV,WAAM,GAAN,MAAM,CAAS;QACf,YAAO,GAAP,OAAO,CAAyB;QAChC,UAAK,GAAL,KAAK,CAAQ;QACb,aAAQ,GAAR,QAAQ,CAAW;QAInC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IACpC,CAAC;CACF;AAZD,4CAYC","sourcesContent":["import { ODataClientError } from \"@odata2ts/http-client-api\";\r\n\r\nexport class FetchClientError extends Error implements ODataClientError {\r\n constructor(\r\n message: string,\r\n public readonly status?: number,\r\n public readonly headers?: Record<string, string>,\r\n public readonly cause?: Error,\r\n public readonly response?: Response\r\n ) {\r\n // @ts-ignore: fetch requires lib \"dom\" or \"webworker\", but then the \"cause\" property becomes unknown to TS\r\n super(message, { cause });\r\n this.name = this.constructor.name;\r\n }\r\n}\r\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export {
|
|
1
|
+
export * from "./FetchClient";
|
|
2
|
+
export { FetchClientError } from "./FetchClientError";
|
|
3
3
|
export { FetchRequestConfig } from "./FetchRequestConfig";
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.FetchClientError = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./
|
|
6
|
-
var
|
|
7
|
-
Object.defineProperty(exports, "
|
|
5
|
+
tslib_1.__exportStar(require("./FetchClient"), exports);
|
|
6
|
+
var FetchClientError_1 = require("./FetchClientError");
|
|
7
|
+
Object.defineProperty(exports, "FetchClientError", { enumerable: true, get: function () { return FetchClientError_1.FetchClientError; } });
|
|
8
8
|
//# 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":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,wDAA8B;AAC9B,uDAAsD;AAA7C,oHAAA,gBAAgB,OAAA","sourcesContent":["export * from \"./FetchClient\";\nexport { FetchClientError } from \"./FetchClientError\";\nexport { FetchRequestConfig } from \"./FetchRequestConfig\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odata2ts/http-client-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"odata2ts"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@odata2ts/
|
|
34
|
+
"@odata2ts/http-client-base": "^0.1.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@odata2ts/odata-core": "^0.3.7",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"typescript": "5.0.4"
|
|
45
45
|
},
|
|
46
46
|
"types": "./lib/index.d.ts",
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "485559dce9388b480c7d0b3ded2fe2f3f5f98fad"
|
|
48
48
|
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { HttpResponseModel, ODataClient } from "@odata2ts/odata-client-api";
|
|
2
|
-
import { FetchRequestConfig } from "./FetchRequestConfig";
|
|
3
|
-
export type ErrorMessageRetriever = (errorResponse: any) => string | undefined;
|
|
4
|
-
export interface ClientOptions {
|
|
5
|
-
useCsrfProtection?: boolean;
|
|
6
|
-
csrfTokenFetchUrl?: string;
|
|
7
|
-
}
|
|
8
|
-
export declare const getV2OrV4ErrorMessage: ErrorMessageRetriever;
|
|
9
|
-
export declare const DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
10
|
-
export declare class FetchODataClient implements ODataClient<FetchRequestConfig> {
|
|
11
|
-
private clientOptions?;
|
|
12
|
-
private readonly config;
|
|
13
|
-
private csrfToken;
|
|
14
|
-
private retrieveErrorMessage;
|
|
15
|
-
constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions | undefined);
|
|
16
|
-
setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever): void;
|
|
17
|
-
private setupSecurityToken;
|
|
18
|
-
private fetchSecurityToken;
|
|
19
|
-
private sendRequest;
|
|
20
|
-
private getResponseBody;
|
|
21
|
-
private prepareData;
|
|
22
|
-
get<ResponseModel>(url: string, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
23
|
-
post<ResponseModel>(url: string, data: any, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
24
|
-
put<ResponseModel>(url: string, data: any, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
25
|
-
patch<ResponseModel>(url: string, data: any, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
26
|
-
merge<ResponseModel>(url: string, data: any, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
27
|
-
delete(url: string, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<void>>;
|
|
28
|
-
}
|
package/lib/FetchODataClient.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FetchODataClient = exports.DEFAULT_ERROR_MESSAGE = exports.getV2OrV4ErrorMessage = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const FetchODataClientError_1 = require("./FetchODataClientError");
|
|
6
|
-
const FetchRequestConfig_1 = require("./FetchRequestConfig");
|
|
7
|
-
const getV2OrV4ErrorMessage = (errorResponse) => {
|
|
8
|
-
var _a;
|
|
9
|
-
const eMsg = (_a = errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.error) === null || _a === void 0 ? void 0 : _a.message;
|
|
10
|
-
return typeof (eMsg === null || eMsg === void 0 ? void 0 : eMsg.value) === "string" ? eMsg.value : eMsg;
|
|
11
|
-
};
|
|
12
|
-
exports.getV2OrV4ErrorMessage = getV2OrV4ErrorMessage;
|
|
13
|
-
exports.DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
14
|
-
const FETCH_FAILURE_MESSAGE = "OData request failed entirely: ";
|
|
15
|
-
const JSON_RETRIEVAL_FAILURE_MESSAGE = "Retrieving JSON body from OData response failed: ";
|
|
16
|
-
const RESPONSE_FAILURE_MESSAGE = "OData server responded with error: ";
|
|
17
|
-
function buildErrorMessage(prefix, error) {
|
|
18
|
-
const msg = typeof error === "string" ? error : error === null || error === void 0 ? void 0 : error.message;
|
|
19
|
-
return prefix + (msg || exports.DEFAULT_ERROR_MESSAGE);
|
|
20
|
-
}
|
|
21
|
-
class FetchODataClient {
|
|
22
|
-
constructor(config, clientOptions) {
|
|
23
|
-
var _a;
|
|
24
|
-
this.clientOptions = clientOptions;
|
|
25
|
-
this.retrieveErrorMessage = exports.getV2OrV4ErrorMessage;
|
|
26
|
-
this.config = (0, FetchRequestConfig_1.getDefaultConfig)(config);
|
|
27
|
-
if (clientOptions && clientOptions.useCsrfProtection && !((_a = clientOptions.csrfTokenFetchUrl) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
28
|
-
throw new Error("When automatic CSRF token fetching is activated, the URL must be supplied with attribute [csrfTokenFetchUrl]!");
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
setErrorMessageRetriever(getErrorMsg) {
|
|
32
|
-
this.retrieveErrorMessage = getErrorMsg;
|
|
33
|
-
}
|
|
34
|
-
setupSecurityToken() {
|
|
35
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
if (!this.csrfToken) {
|
|
37
|
-
this.csrfToken = yield this.fetchSecurityToken();
|
|
38
|
-
}
|
|
39
|
-
return this.csrfToken;
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
fetchSecurityToken() {
|
|
43
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
const fetchUrl = this.clientOptions.csrfTokenFetchUrl;
|
|
45
|
-
const response = yield this.get(fetchUrl, { headers: { "x-csrf-token": "Fetch" } });
|
|
46
|
-
return response.headers["x-csrf-token"];
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
sendRequest(url, config, requestConfig) {
|
|
50
|
-
var _a, _b;
|
|
51
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
// noinspection SuspiciousTypeOfGuard
|
|
53
|
-
if (typeof url !== "string") {
|
|
54
|
-
throw new Error("Value for URL must be provided!");
|
|
55
|
-
}
|
|
56
|
-
const mergedConfig = (0, FetchRequestConfig_1.mergeFetchConfig)(this.config, requestConfig, config);
|
|
57
|
-
// setup automatic CSRF token handling
|
|
58
|
-
if (((_a = this.clientOptions) === null || _a === void 0 ? void 0 : _a.useCsrfProtection) &&
|
|
59
|
-
mergedConfig.method &&
|
|
60
|
-
["POST", "PUT", "PATCH", "DELETE"].includes(mergedConfig.method.toUpperCase())) {
|
|
61
|
-
const csrfToken = yield this.setupSecurityToken();
|
|
62
|
-
if (typeof csrfToken === "string") {
|
|
63
|
-
mergedConfig.headers.set("x-csrf-token", csrfToken);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
// the actual request
|
|
67
|
-
let response;
|
|
68
|
-
try {
|
|
69
|
-
response = yield fetch(url, mergedConfig);
|
|
70
|
-
}
|
|
71
|
-
catch (fetchError) {
|
|
72
|
-
throw new FetchODataClientError_1.FetchODataClientError(buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError), undefined, fetchError);
|
|
73
|
-
}
|
|
74
|
-
// error response
|
|
75
|
-
if (!response.ok) {
|
|
76
|
-
// automatic CSRF token handling
|
|
77
|
-
if (((_b = this.clientOptions) === null || _b === void 0 ? void 0 : _b.useCsrfProtection) &&
|
|
78
|
-
response.status === 403 &&
|
|
79
|
-
response.headers.get("x-csrf-token") === "Required") {
|
|
80
|
-
// csrf token expired, let's reset it and perform the original request again
|
|
81
|
-
this.csrfToken = undefined;
|
|
82
|
-
return this.sendRequest(url, config, requestConfig);
|
|
83
|
-
}
|
|
84
|
-
let data = yield this.getResponseBody(response, false);
|
|
85
|
-
const errMsg = this.retrieveErrorMessage(data);
|
|
86
|
-
throw new FetchODataClientError_1.FetchODataClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), response);
|
|
87
|
-
}
|
|
88
|
-
const data = yield this.getResponseBody(response, true);
|
|
89
|
-
// header
|
|
90
|
-
// Impl Note: No entries prop available, otherwise as one liner Array.from(response.headers.entries)
|
|
91
|
-
const headers = {};
|
|
92
|
-
response.headers.forEach((value, key) => {
|
|
93
|
-
headers[key] = value;
|
|
94
|
-
});
|
|
95
|
-
return {
|
|
96
|
-
status: response.status,
|
|
97
|
-
statusText: response.statusText,
|
|
98
|
-
headers,
|
|
99
|
-
data,
|
|
100
|
-
};
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
getResponseBody(response, isFailedJsonFatal) {
|
|
104
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
if (response.status === 204) {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
try {
|
|
109
|
-
return yield response.json();
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
if (isFailedJsonFatal) {
|
|
113
|
-
throw new FetchODataClientError_1.FetchODataClientError(buildErrorMessage(JSON_RETRIEVAL_FAILURE_MESSAGE, error), response.status, error);
|
|
114
|
-
}
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
prepareData(data) {
|
|
120
|
-
return JSON.stringify(data);
|
|
121
|
-
}
|
|
122
|
-
get(url, requestConfig) {
|
|
123
|
-
return this.sendRequest(url, { method: "GET" }, requestConfig);
|
|
124
|
-
}
|
|
125
|
-
post(url, data, requestConfig) {
|
|
126
|
-
return this.sendRequest(url, { body: this.prepareData(data), method: "POST" }, requestConfig);
|
|
127
|
-
}
|
|
128
|
-
put(url, data, requestConfig) {
|
|
129
|
-
return this.sendRequest(url, { body: this.prepareData(data), method: "PUT" }, requestConfig);
|
|
130
|
-
}
|
|
131
|
-
patch(url, data, requestConfig) {
|
|
132
|
-
return this.sendRequest(url, { body: this.prepareData(data), method: "PATCH" }, requestConfig);
|
|
133
|
-
}
|
|
134
|
-
merge(url, data, requestConfig) {
|
|
135
|
-
return this.sendRequest(url, {
|
|
136
|
-
body: this.prepareData(data),
|
|
137
|
-
method: "POST",
|
|
138
|
-
headers: {
|
|
139
|
-
"X-Http-Method": "MERGE",
|
|
140
|
-
},
|
|
141
|
-
}, requestConfig);
|
|
142
|
-
}
|
|
143
|
-
delete(url, requestConfig) {
|
|
144
|
-
return this.sendRequest(url, { method: "DELETE" }, requestConfig);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
exports.FetchODataClient = FetchODataClient;
|
|
148
|
-
//# sourceMappingURL=FetchODataClient.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FetchODataClient.js","sourceRoot":"","sources":["../src/FetchODataClient.ts"],"names":[],"mappings":";;;;AAEA,mEAAgE;AAChE,6DAA8F;AASvF,MAAM,qBAAqB,GAA0B,CAAC,aAAkB,EAAsB,EAAE;;IACrG,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,CAA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kC;QAElC,OAAO,IAAI,CAAC,WAAW,CACrB,GAAG,EACH;YACE,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;YAC5B,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,eAAe,EAAE,OAAO;aACzB;SACF,EACD,aAAa,CACd,CAAC;IACJ,CAAC;IACM,MAAM,CAAC,GAAW,EAAE,aAAkC;QAC3D,OAAO,IAAI,CAAC,WAAW,CAAO,GAAG,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,aAAa,CAAC,CAAC;IAC1E,CAAC;CACF;AAlLD,4CAkLC","sourcesContent":["import { HttpResponseModel, ODataClient } from \"@odata2ts/odata-client-api\";\r\n\r\nimport { FetchODataClientError } from \"./FetchODataClientError\";\r\nimport { FetchRequestConfig, getDefaultConfig, mergeFetchConfig } from \"./FetchRequestConfig\";\r\n\r\nexport type ErrorMessageRetriever = (errorResponse: any) => string | undefined;\r\n\r\nexport interface ClientOptions {\r\n useCsrfProtection?: boolean;\r\n csrfTokenFetchUrl?: string;\r\n}\r\n\r\nexport const getV2OrV4ErrorMessage: ErrorMessageRetriever = (errorResponse: any): string | undefined => {\r\n const eMsg = errorResponse?.error?.message;\r\n return typeof eMsg?.value === \"string\" ? eMsg.value : eMsg;\r\n};\r\n\r\nexport const DEFAULT_ERROR_MESSAGE = \"No error message!\";\r\nconst FETCH_FAILURE_MESSAGE = \"OData request failed entirely: \";\r\nconst JSON_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving JSON body from OData response failed: \";\r\nconst RESPONSE_FAILURE_MESSAGE = \"OData server responded with error: \";\r\n\r\nfunction buildErrorMessage(prefix: string, error: any) {\r\n const msg = typeof error === \"string\" ? error : (error as Error)?.message;\r\n return prefix + (msg || DEFAULT_ERROR_MESSAGE);\r\n}\r\n\r\nexport class FetchODataClient implements ODataClient<FetchRequestConfig> {\r\n private readonly config: RequestInit;\r\n private csrfToken: string | undefined;\r\n private retrieveErrorMessage: ErrorMessageRetriever = getV2OrV4ErrorMessage;\r\n\r\n constructor(config?: FetchRequestConfig, private clientOptions?: ClientOptions) {\r\n this.config = getDefaultConfig(config);\r\n if (clientOptions && clientOptions.useCsrfProtection && !clientOptions.csrfTokenFetchUrl?.trim()) {\r\n throw new Error(\r\n \"When automatic CSRF token fetching is activated, the URL must be supplied with attribute [csrfTokenFetchUrl]!\"\r\n );\r\n }\r\n }\r\n\r\n public setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever) {\r\n this.retrieveErrorMessage = getErrorMsg;\r\n }\r\n\r\n private async setupSecurityToken() {\r\n if (!this.csrfToken) {\r\n this.csrfToken = await this.fetchSecurityToken();\r\n }\r\n return this.csrfToken;\r\n }\r\n\r\n private async fetchSecurityToken(): Promise<string | undefined> {\r\n const fetchUrl = this.clientOptions!.csrfTokenFetchUrl!;\r\n const response = await this.get(fetchUrl, { headers: { \"x-csrf-token\": \"Fetch\" } });\r\n\r\n return response.headers[\"x-csrf-token\"];\r\n }\r\n\r\n private async sendRequest<ResponseType>(\r\n url: string,\r\n config: RequestInit,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseType>> {\r\n // noinspection SuspiciousTypeOfGuard\r\n if (typeof url !== \"string\") {\r\n throw new Error(\"Value for URL must be provided!\");\r\n }\r\n\r\n const mergedConfig = mergeFetchConfig(this.config, requestConfig, config);\r\n\r\n // setup automatic CSRF token handling\r\n if (\r\n this.clientOptions?.useCsrfProtection &&\r\n mergedConfig.method &&\r\n [\"POST\", \"PUT\", \"PATCH\", \"DELETE\"].includes(mergedConfig.method.toUpperCase())\r\n ) {\r\n const csrfToken = await this.setupSecurityToken();\r\n if (typeof csrfToken === \"string\") {\r\n mergedConfig.headers.set(\"x-csrf-token\", csrfToken);\r\n }\r\n }\r\n\r\n // the actual request\r\n let response: Response;\r\n try {\r\n response = await fetch(url, mergedConfig);\r\n } catch (fetchError) {\r\n throw new FetchODataClientError(\r\n buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError),\r\n undefined,\r\n fetchError as Error\r\n );\r\n }\r\n\r\n // error response\r\n if (!response.ok) {\r\n // automatic CSRF token handling\r\n if (\r\n this.clientOptions?.useCsrfProtection &&\r\n response.status === 403 &&\r\n response.headers.get(\"x-csrf-token\") === \"Required\"\r\n ) {\r\n // csrf token expired, let's reset it and perform the original request again\r\n this.csrfToken = undefined;\r\n return this.sendRequest<ResponseType>(url, config, requestConfig);\r\n }\r\n\r\n let data = await this.getResponseBody(response, false);\r\n const errMsg = this.retrieveErrorMessage(data);\r\n\r\n throw new FetchODataClientError(\r\n buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg),\r\n response.status,\r\n new Error(errMsg || DEFAULT_ERROR_MESSAGE),\r\n response\r\n );\r\n }\r\n\r\n const data = await this.getResponseBody(response, true);\r\n\r\n // header\r\n // Impl Note: No entries prop available, otherwise as one liner Array.from(response.headers.entries)\r\n const headers: { [key: string]: string } = {};\r\n response.headers.forEach((value, key) => {\r\n headers[key] = value;\r\n });\r\n\r\n return {\r\n status: response.status,\r\n statusText: response.statusText,\r\n headers,\r\n data,\r\n };\r\n }\r\n\r\n private async getResponseBody(response: Response, isFailedJsonFatal: boolean) {\r\n if (response.status === 204) {\r\n return undefined;\r\n }\r\n try {\r\n return await response.json();\r\n } catch (error) {\r\n if (isFailedJsonFatal) {\r\n throw new FetchODataClientError(\r\n buildErrorMessage(JSON_RETRIEVAL_FAILURE_MESSAGE, error),\r\n response.status,\r\n error as Error\r\n );\r\n }\r\n return undefined;\r\n }\r\n }\r\n\r\n private prepareData(data: any): string {\r\n return JSON.stringify(data);\r\n }\r\n\r\n public get<ResponseModel>(\r\n url: string,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(url, { method: \"GET\" }, requestConfig);\r\n }\r\n public post<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(url, { body: this.prepareData(data), method: \"POST\" }, requestConfig);\r\n }\r\n public put<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(url, { body: this.prepareData(data), method: \"PUT\" }, requestConfig);\r\n }\r\n public patch<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(url, { body: this.prepareData(data), method: \"PATCH\" }, requestConfig);\r\n }\r\n public merge<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: FetchRequestConfig\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n url,\r\n {\r\n body: this.prepareData(data),\r\n method: \"POST\",\r\n headers: {\r\n \"X-Http-Method\": \"MERGE\",\r\n },\r\n },\r\n requestConfig\r\n );\r\n }\r\n public delete(url: string, requestConfig?: FetchRequestConfig): Promise<HttpResponseModel<void>> {\r\n return this.sendRequest<void>(url, { method: \"DELETE\" }, requestConfig);\r\n }\r\n}\r\n"]}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare class FetchODataClientError extends Error {
|
|
2
|
-
readonly status?: number | undefined;
|
|
3
|
-
readonly cause?: Error | undefined;
|
|
4
|
-
readonly response?: Response | undefined;
|
|
5
|
-
constructor(message: string, status?: number | undefined, cause?: Error | undefined, response?: Response | undefined);
|
|
6
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FetchODataClientError.js","sourceRoot":"","sources":["../src/FetchODataClientError.ts"],"names":[],"mappings":";;;AAAA,MAAa,qBAAsB,SAAQ,KAAK;IAC9C,YACE,OAAe,EACC,MAAe,EACf,KAAa,EACb,QAAmB;QAEnC,2GAA2G;QAC3G,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QALV,WAAM,GAAN,MAAM,CAAS;QACf,UAAK,GAAL,KAAK,CAAQ;QACb,aAAQ,GAAR,QAAQ,CAAW;QAInC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IACpC,CAAC;CACF;AAXD,sDAWC","sourcesContent":["export class FetchODataClientError extends Error {\r\n constructor(\r\n message: string,\r\n public readonly status?: number,\r\n public readonly cause?: Error,\r\n public readonly response?: Response\r\n ) {\r\n // @ts-ignore: fetch requires lib \"dom\" or \"webworker\", but then the \"cause\" property becomes unknown to TS\r\n super(message, { cause });\r\n this.name = this.constructor.name;\r\n }\r\n}\r\n"]}
|