@odata2ts/http-client-fetch 0.6.1 → 0.7.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 +18 -0
- package/README.md +50 -50
- package/lib/FetchClient.d.ts +4 -4
- package/lib/FetchClient.js +39 -29
- package/lib/FetchClient.js.map +1 -1
- package/lib/FetchClientError.js +1 -5
- package/lib/FetchClientError.js.map +1 -1
- package/lib/FetchRequestConfig.d.ts +1 -1
- package/lib/FetchRequestConfig.js +5 -9
- package/lib/FetchRequestConfig.js.map +1 -1
- package/lib/index.js +2 -7
- package/lib/index.js.map +1 -1
- package/package.json +23 -26
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,24 @@
|
|
|
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.7.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.6.2...@odata2ts/http-client-fetch@0.7.0) (2024-08-13)
|
|
7
|
+
|
|
8
|
+
### Code Refactoring
|
|
9
|
+
|
|
10
|
+
* switch to vitest & ESM ([#18](https://github.com/odata2ts/http-client/issues/18)) ([748558f](https://github.com/odata2ts/http-client/commit/748558f1e3f699085ade1058b1459c843f60994f))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* blob and stream support ([#12](https://github.com/odata2ts/http-client/issues/12)) ([ae6f062](https://github.com/odata2ts/http-client/commit/ae6f062371a0ad11707fa3f9edff9571998edb5b))
|
|
15
|
+
|
|
16
|
+
### BREAKING CHANGES
|
|
17
|
+
|
|
18
|
+
* switch to ESM tends to break stuff
|
|
19
|
+
|
|
20
|
+
## [0.6.2](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.6.1...@odata2ts/http-client-fetch@0.6.2) (2023-09-13)
|
|
21
|
+
|
|
22
|
+
**Note:** Version bump only for package @odata2ts/http-client-fetch
|
|
23
|
+
|
|
6
24
|
## [0.6.1](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.6.0...@odata2ts/http-client-fetch@0.6.1) (2023-09-13)
|
|
7
25
|
|
|
8
26
|
**Note:** Version bump only for package @odata2ts/http-client-fetch
|
package/README.md
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@odata2ts/http-client-fetch)
|
|
2
|
-
|
|
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)
|
|
7
|
-
for realizing the HTTP communication.
|
|
8
|
-
|
|
9
|
-
It supports:
|
|
10
|
-
|
|
11
|
-
- request configuration
|
|
12
|
-
- automatic CSRF token handling
|
|
13
|
-
|
|
14
|
-
Works also for Node.js v18+, but is still marked as **experimental**.
|
|
15
|
-
|
|
16
|
-
## Installation
|
|
17
|
-
|
|
18
|
-
Install package `@odata2ts/http-client-fetch` as runtime dependency:
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install --save @odata2ts/http-client-fetch
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Documentation
|
|
25
|
-
|
|
26
|
-
[Fetch Client Documentation](https://odata2ts.github.io/docs/odata-client/http-client/fetch)
|
|
27
|
-
|
|
28
|
-
Main documentation for the odata2ts eco system:
|
|
29
|
-
[https://odata2ts.github.io](https://odata2ts.github.io/)
|
|
30
|
-
|
|
31
|
-
## Tests
|
|
32
|
-
|
|
33
|
-
See folder [test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/test)
|
|
34
|
-
for unit tests.
|
|
35
|
-
|
|
36
|
-
See folder [int-test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/int-test) for
|
|
37
|
-
integration tests.
|
|
38
|
-
|
|
39
|
-
## Support, Feedback, Contributing
|
|
40
|
-
|
|
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).
|
|
43
|
-
|
|
44
|
-
Contributions and feedback are encouraged and always welcome.
|
|
45
|
-
|
|
46
|
-
See the [contribution guidelines](https://github.com/odata2ts/http-client/blob/main/CONTRIBUTING.md) for further information.
|
|
47
|
-
|
|
48
|
-
## License
|
|
49
|
-
|
|
50
|
-
MIT - see [License](./LICENSE).
|
|
1
|
+
[](https://www.npmjs.com/package/@odata2ts/http-client-fetch)
|
|
2
|
+
|
|
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)
|
|
7
|
+
for realizing the HTTP communication.
|
|
8
|
+
|
|
9
|
+
It supports:
|
|
10
|
+
|
|
11
|
+
- request configuration
|
|
12
|
+
- automatic CSRF token handling
|
|
13
|
+
|
|
14
|
+
Works also for Node.js v18+, but is still marked as **experimental**.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Install package `@odata2ts/http-client-fetch` as runtime dependency:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install --save @odata2ts/http-client-fetch
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Documentation
|
|
25
|
+
|
|
26
|
+
[Fetch Client Documentation](https://odata2ts.github.io/docs/odata-client/http-client/fetch)
|
|
27
|
+
|
|
28
|
+
Main documentation for the odata2ts eco system:
|
|
29
|
+
[https://odata2ts.github.io](https://odata2ts.github.io/)
|
|
30
|
+
|
|
31
|
+
## Tests
|
|
32
|
+
|
|
33
|
+
See folder [test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/test)
|
|
34
|
+
for unit tests.
|
|
35
|
+
|
|
36
|
+
See folder [int-test](https://github.com/odata2ts/http-client/tree/main/packages/fetch/int-test) for
|
|
37
|
+
integration tests.
|
|
38
|
+
|
|
39
|
+
## Support, Feedback, Contributing
|
|
40
|
+
|
|
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).
|
|
43
|
+
|
|
44
|
+
Contributions and feedback are encouraged and always welcome.
|
|
45
|
+
|
|
46
|
+
See the [contribution guidelines](https://github.com/odata2ts/http-client/blob/main/CONTRIBUTING.md) for further information.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
MIT - see [License](./LICENSE).
|
package/lib/FetchClient.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { HttpResponseModel,
|
|
2
|
-
import { BaseHttpClient, BaseHttpClientOptions, HttpMethods } from "@odata2ts/http-client-base";
|
|
1
|
+
import { HttpResponseModel, ODataHttpClient } from "@odata2ts/http-client-api";
|
|
2
|
+
import { BaseHttpClient, BaseHttpClientOptions, HttpMethods, InternalHttpClientConfig } from "@odata2ts/http-client-base";
|
|
3
3
|
import { FetchRequestConfig } from "./FetchRequestConfig";
|
|
4
4
|
export interface ClientOptions extends BaseHttpClientOptions {
|
|
5
5
|
}
|
|
6
6
|
export declare const DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
7
|
-
export declare class FetchClient extends BaseHttpClient<FetchRequestConfig> {
|
|
7
|
+
export declare class FetchClient extends BaseHttpClient<FetchRequestConfig> implements ODataHttpClient<FetchRequestConfig> {
|
|
8
8
|
protected readonly config: RequestInit;
|
|
9
9
|
constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions);
|
|
10
10
|
protected executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, requestConfig?: FetchRequestConfig | undefined, internalConfig?: InternalHttpClientConfig): Promise<HttpResponseModel<ResponseModel>>;
|
|
11
|
-
protected getResponseBody(response: Response,
|
|
11
|
+
protected getResponseBody(response: Response, options: InternalHttpClientConfig): Promise<any>;
|
|
12
12
|
protected mapHeaders(headers: Headers): Record<string, string>;
|
|
13
13
|
}
|
package/lib/FetchClient.js
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const FetchClientError_1 = require("./FetchClientError");
|
|
7
|
-
const FetchRequestConfig_1 = require("./FetchRequestConfig");
|
|
8
|
-
exports.DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
1
|
+
import { __awaiter, __rest } from "tslib";
|
|
2
|
+
import { BaseHttpClient, } from "@odata2ts/http-client-base";
|
|
3
|
+
import { FetchClientError } from "./FetchClientError";
|
|
4
|
+
import { getDefaultConfig, mergeFetchConfig } from "./FetchRequestConfig";
|
|
5
|
+
export const DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
9
6
|
const FETCH_FAILURE_MESSAGE = "OData request failed entirely: ";
|
|
10
7
|
const JSON_RETRIEVAL_FAILURE_MESSAGE = "Retrieving JSON body from OData response failed: ";
|
|
8
|
+
const BLOB_RETRIEVAL_FAILURE_MESSAGE = "Retrieving blob from OData response failed: ";
|
|
11
9
|
const RESPONSE_FAILURE_MESSAGE = "OData server responded with error: ";
|
|
12
10
|
function buildErrorMessage(prefix, error) {
|
|
13
11
|
const msg = typeof error === "string" ? error : error === null || error === void 0 ? void 0 : error.message;
|
|
14
|
-
return prefix + (msg ||
|
|
12
|
+
return prefix + (msg || DEFAULT_ERROR_MESSAGE);
|
|
15
13
|
}
|
|
16
|
-
class FetchClient extends
|
|
14
|
+
export class FetchClient extends BaseHttpClient {
|
|
17
15
|
constructor(config, clientOptions) {
|
|
18
16
|
super(clientOptions);
|
|
19
|
-
this.config =
|
|
17
|
+
this.config = getDefaultConfig(config);
|
|
20
18
|
}
|
|
21
19
|
executeRequest(method, url, data, requestConfig = {}, internalConfig = {}) {
|
|
22
|
-
return
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
21
|
const { headers, noBodyEvaluation } = internalConfig;
|
|
24
|
-
const _a =
|
|
22
|
+
const _a = mergeFetchConfig(this.config, { headers }, requestConfig), { params } = _a, config = __rest(_a, ["params"]);
|
|
25
23
|
config.method = method;
|
|
26
24
|
if (typeof data !== "undefined") {
|
|
27
|
-
config.body = JSON.stringify(data);
|
|
25
|
+
config.body = internalConfig.dataType === "json" ? JSON.stringify(data) : data;
|
|
28
26
|
}
|
|
29
27
|
let finalUrl = url;
|
|
30
28
|
if (params && Object.values(params).length) {
|
|
@@ -39,15 +37,28 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
|
|
|
39
37
|
response = yield fetch(finalUrl, config);
|
|
40
38
|
}
|
|
41
39
|
catch (fetchError) {
|
|
42
|
-
throw new
|
|
40
|
+
throw new FetchClientError(buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError), undefined, undefined, fetchError);
|
|
43
41
|
}
|
|
44
42
|
// error response
|
|
45
43
|
if (!response.ok) {
|
|
46
|
-
let responseData
|
|
44
|
+
let responseData;
|
|
45
|
+
try {
|
|
46
|
+
responseData = yield this.getResponseBody(response, internalConfig);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
responseData = undefined;
|
|
50
|
+
}
|
|
47
51
|
const errMsg = this.retrieveErrorMessage(responseData);
|
|
48
|
-
throw new
|
|
52
|
+
throw new FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || DEFAULT_ERROR_MESSAGE), response);
|
|
53
|
+
}
|
|
54
|
+
let responseData;
|
|
55
|
+
try {
|
|
56
|
+
responseData = noBodyEvaluation ? undefined : yield this.getResponseBody(response, internalConfig);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const msg = internalConfig.dataType === "blob" ? BLOB_RETRIEVAL_FAILURE_MESSAGE : JSON_RETRIEVAL_FAILURE_MESSAGE;
|
|
60
|
+
throw new FetchClientError(buildErrorMessage(msg, error), response.status, this.mapHeaders(response.headers), error);
|
|
49
61
|
}
|
|
50
|
-
const responseData = noBodyEvaluation ? undefined : yield this.getResponseBody(response, true);
|
|
51
62
|
return {
|
|
52
63
|
status: response.status,
|
|
53
64
|
statusText: response.statusText,
|
|
@@ -56,20 +67,20 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
|
|
|
56
67
|
};
|
|
57
68
|
});
|
|
58
69
|
}
|
|
59
|
-
getResponseBody(response,
|
|
60
|
-
return
|
|
70
|
+
getResponseBody(response, options) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
72
|
if (response.status === 204) {
|
|
62
73
|
return undefined;
|
|
63
74
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return undefined;
|
|
75
|
+
switch (options.dataType) {
|
|
76
|
+
case "json":
|
|
77
|
+
return response.json();
|
|
78
|
+
case "blob":
|
|
79
|
+
return response.blob();
|
|
80
|
+
case "stream":
|
|
81
|
+
return response.body;
|
|
72
82
|
}
|
|
83
|
+
return undefined;
|
|
73
84
|
});
|
|
74
85
|
}
|
|
75
86
|
mapHeaders(headers) {
|
|
@@ -78,5 +89,4 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
|
|
|
78
89
|
return result;
|
|
79
90
|
}
|
|
80
91
|
}
|
|
81
|
-
exports.FetchClient = FetchClient;
|
|
82
92
|
//# sourceMappingURL=FetchClient.js.map
|
package/lib/FetchClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchClient.js","sourceRoot":"","sources":["../src/FetchClient.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FetchClient.js","sourceRoot":"","sources":["../src/FetchClient.ts"],"names":[],"mappings":";AACA,OAAO,EACL,cAAc,GAIf,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBA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G,KAAK,CAAC,CAAC,CAAC;QAEvD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { HttpResponseModel, ODataHttpClient } from \"@odata2ts/http-client-api\";\nimport {\n BaseHttpClient,\n BaseHttpClientOptions,\n HttpMethods,\n InternalHttpClientConfig,\n} from \"@odata2ts/http-client-base\";\nimport { FetchClientError } from \"./FetchClientError\";\nimport { FetchRequestConfig, getDefaultConfig, mergeFetchConfig } from \"./FetchRequestConfig\";\n\nexport interface ClientOptions extends BaseHttpClientOptions {}\n\nexport const DEFAULT_ERROR_MESSAGE = \"No error message!\";\nconst FETCH_FAILURE_MESSAGE = \"OData request failed entirely: \";\nconst JSON_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving JSON body from OData response failed: \";\nconst BLOB_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving blob from OData response failed: \";\nconst RESPONSE_FAILURE_MESSAGE = \"OData server responded with error: \";\n\nfunction buildErrorMessage(prefix: string, error: any) {\n const msg = typeof error === \"string\" ? error : (error as Error)?.message;\n return prefix + (msg || DEFAULT_ERROR_MESSAGE);\n}\n\nexport class FetchClient extends BaseHttpClient<FetchRequestConfig> implements ODataHttpClient<FetchRequestConfig> {\n protected readonly config: RequestInit;\n\n constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions) {\n super(clientOptions);\n this.config = getDefaultConfig(config);\n }\n\n protected async executeRequest<ResponseModel>(\n method: HttpMethods,\n url: string,\n data: any,\n requestConfig: FetchRequestConfig | undefined = {},\n internalConfig: InternalHttpClientConfig = {},\n ): Promise<HttpResponseModel<ResponseModel>> {\n const { headers, noBodyEvaluation } = internalConfig;\n const { params, ...config } = mergeFetchConfig(this.config, { headers }, requestConfig);\n config.method = method;\n if (typeof data !== \"undefined\") {\n config.body = internalConfig.dataType === \"json\" ? JSON.stringify(data) : data;\n }\n let finalUrl = url;\n if (params && Object.values(params).length) {\n finalUrl +=\n (url.match(/\\?/) ? \"&\" : \"?\") +\n // @ts-ignore\n new URLSearchParams(params).toString();\n }\n\n // the actual request\n let response: Response;\n try {\n response = await fetch(finalUrl, config);\n } catch (fetchError) {\n throw new FetchClientError(\n buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError),\n undefined,\n undefined,\n fetchError as Error,\n );\n }\n\n // error response\n if (!response.ok) {\n let responseData;\n try {\n responseData = await this.getResponseBody(response, internalConfig);\n } catch (e) {\n responseData = undefined;\n }\n const errMsg = this.retrieveErrorMessage(responseData);\n\n throw new FetchClientError(\n buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg),\n response.status,\n this.mapHeaders(response.headers),\n new Error(errMsg || DEFAULT_ERROR_MESSAGE),\n response,\n );\n }\n\n let responseData;\n try {\n responseData = noBodyEvaluation ? undefined : await this.getResponseBody(response, internalConfig);\n } catch (error) {\n const msg = internalConfig.dataType === \"blob\" ? BLOB_RETRIEVAL_FAILURE_MESSAGE : JSON_RETRIEVAL_FAILURE_MESSAGE;\n throw new FetchClientError(\n buildErrorMessage(msg, error),\n response.status,\n this.mapHeaders(response.headers),\n error as Error,\n );\n }\n\n return {\n status: response.status,\n statusText: response.statusText,\n headers: this.mapHeaders(response.headers),\n data: responseData,\n };\n }\n\n protected async getResponseBody(response: Response, options: InternalHttpClientConfig) {\n if (response.status === 204) {\n return undefined;\n }\n switch (options.dataType) {\n case \"json\":\n return response.json();\n case \"blob\":\n return response.blob();\n case \"stream\":\n return response.body;\n }\n\n return undefined;\n }\n\n protected mapHeaders(headers: Headers): Record<string, string> {\n const result: Record<string, string> = {};\n headers.forEach((value, key) => (result[key] = value));\n\n return result;\n }\n}\n"]}
|
package/lib/FetchClientError.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FetchClientError = void 0;
|
|
4
|
-
class FetchClientError extends Error {
|
|
1
|
+
export class FetchClientError extends Error {
|
|
5
2
|
constructor(message, status, headers, cause, response) {
|
|
6
3
|
// @ts-ignore: fetch requires lib "dom" or "webworker", but then the "cause" property becomes unknown to TS
|
|
7
4
|
super(message, { cause });
|
|
@@ -12,5 +9,4 @@ class FetchClientError extends Error {
|
|
|
12
9
|
this.name = this.constructor.name;
|
|
13
10
|
}
|
|
14
11
|
}
|
|
15
|
-
exports.FetchClientError = FetchClientError;
|
|
16
12
|
//# sourceMappingURL=FetchClientError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchClientError.js","sourceRoot":"","sources":["../src/FetchClientError.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FetchClientError.js","sourceRoot":"","sources":["../src/FetchClientError.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,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","sourcesContent":["import { ODataClientError } from \"@odata2ts/http-client-api\";\n\nexport class FetchClientError extends Error implements ODataClientError {\n constructor(\n message: string,\n public readonly status?: number,\n public readonly headers?: Record<string, string>,\n public readonly cause?: Error,\n public readonly response?: Response,\n ) {\n // @ts-ignore: fetch requires lib \"dom\" or \"webworker\", but then the \"cause\" property becomes unknown to TS\n super(message, { cause });\n this.name = this.constructor.name;\n }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Available config options for end user when making a given request.
|
|
3
3
|
*/
|
|
4
|
-
export interface FetchRequestConfig extends Pick<RequestInit, "credentials" | "cache" | "mode" | "redirect" | "referrerPolicy"
|
|
4
|
+
export interface FetchRequestConfig extends Partial<Pick<RequestInit, "credentials" | "cache" | "mode" | "redirect" | "referrerPolicy" | "signal">> {
|
|
5
5
|
headers?: Record<string, string> | Headers;
|
|
6
6
|
/**
|
|
7
7
|
* Add query params.
|
|
@@ -1,26 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mergeFetchConfig = exports.getDefaultConfig = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
1
|
+
import { __rest } from "tslib";
|
|
5
2
|
const DEFAULT_CONFIG = {
|
|
6
3
|
// headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
7
4
|
cache: "no-store",
|
|
8
5
|
};
|
|
9
|
-
function getDefaultConfig(config) {
|
|
6
|
+
export function getDefaultConfig(config) {
|
|
10
7
|
return mergeFetchConfig(DEFAULT_CONFIG, config);
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
function mergeFetchConfig(...configs) {
|
|
9
|
+
export function mergeFetchConfig(...configs) {
|
|
14
10
|
if (!configs.length) {
|
|
15
11
|
return undefined;
|
|
16
12
|
}
|
|
17
13
|
return configs
|
|
18
14
|
.filter((c) => !!c)
|
|
19
15
|
.reduce((collector, current) => {
|
|
20
|
-
const { headers } = current, passThrough =
|
|
16
|
+
const { headers } = current, passThrough = __rest(current, ["headers"]);
|
|
21
17
|
const collectedHeaders = collector.headers;
|
|
22
18
|
// headers as Headers object
|
|
23
19
|
if (headers && headers instanceof Headers) {
|
|
20
|
+
// @ts-ignore: fails on CI test
|
|
24
21
|
headers.forEach((val, key) => collectedHeaders.set(key, val));
|
|
25
22
|
}
|
|
26
23
|
// headers as plain Record<string,string>
|
|
@@ -30,5 +27,4 @@ function mergeFetchConfig(...configs) {
|
|
|
30
27
|
return Object.assign(Object.assign({}, collector), passThrough);
|
|
31
28
|
}, { headers: new Headers() });
|
|
32
29
|
}
|
|
33
|
-
exports.mergeFetchConfig = mergeFetchConfig;
|
|
34
30
|
//# sourceMappingURL=FetchRequestConfig.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchRequestConfig.js","sourceRoot":"","sources":["../src/FetchRequestConfig.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FetchRequestConfig.js","sourceRoot":"","sources":["../src/FetchRequestConfig.ts"],"names":[],"mappings":";AAAA,MAAM,cAAc,GAAgB;IAClC,+EAA+E;IAC/E,KAAK,EAAE,UAAU;CAClB,CAAC;AAkBF,MAAM,UAAU,gBAAgB,CAAC,MAA2B;IAC1D,OAAO,gBAAgB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC;AAID,MAAM,UAAU,gBAAgB,CAAC,GAAG,OAAuC;IACzE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;QACnB,OAAO,SAAS,CAAC;KAClB;IACD,OAAO,OAAO;SACX,MAAM,CAAC,CAAC,CAAC,EAAoB,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;SACpC,MAAM,CACL,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE;QACrB,MAAM,EAAE,OAAO,KAAqB,OAAO,EAAvB,WAAW,UAAK,OAAO,EAArC,WAA2B,CAAU,CAAC;QAC5C,MAAM,gBAAgB,GAAG,SAAS,CAAC,OAAkB,CAAC;QAEtD,4BAA4B;QAC5B,IAAI,OAAO,IAAI,OAAO,YAAY,OAAO,EAAE;YACzC,+BAA+B;YAC/B,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;SAC/D;QACD,yCAAyC;aACpC,IAAI,OAAO,EAAE;YAChB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;SACjF;QAED,uCAAY,SAAS,GAAK,WAAW,EAAG;IAC1C,CAAC,EACD,EAAE,OAAO,EAAE,IAAI,OAAO,EAAE,EAAE,CAC3B,CAAC;AACN,CAAC","sourcesContent":["const DEFAULT_CONFIG: RequestInit = {\n // headers: { Accept: \"application/json\", \"Content-Type\": \"application/json\" },\n cache: \"no-store\",\n};\n\n/**\n * Available config options for end user when making a given request.\n */\nexport interface FetchRequestConfig\n extends Partial<Pick<RequestInit, \"credentials\" | \"cache\" | \"mode\" | \"redirect\" | \"referrerPolicy\" | \"signal\">> {\n headers?: Record<string, string> | Headers;\n /**\n * Add query params.\n */\n params?: Record<string, string | number | boolean | Array<string | number | boolean>>;\n}\n\nexport interface InternalFetchConfig extends Omit<RequestInit, \"headers\">, Pick<FetchRequestConfig, \"params\"> {\n headers: Headers;\n}\n\nexport function getDefaultConfig(config?: FetchRequestConfig): RequestInit {\n return mergeFetchConfig(DEFAULT_CONFIG, config);\n}\n\nexport function mergeFetchConfig(): undefined;\nexport function mergeFetchConfig(...configs: Array<RequestInit | undefined>): InternalFetchConfig;\nexport function mergeFetchConfig(...configs: Array<RequestInit | undefined>) {\n if (!configs.length) {\n return undefined;\n }\n return configs\n .filter((c): c is RequestInit => !!c)\n .reduce<InternalFetchConfig>(\n (collector, current) => {\n const { headers, ...passThrough } = current;\n const collectedHeaders = collector.headers as Headers;\n\n // headers as Headers object\n if (headers && headers instanceof Headers) {\n // @ts-ignore: fails on CI test\n headers.forEach((val, key) => collectedHeaders.set(key, val));\n }\n // headers as plain Record<string,string>\n else if (headers) {\n Object.entries(headers).forEach(([key, val]) => collectedHeaders.set(key, val));\n }\n\n return { ...collector, ...passThrough };\n },\n { headers: new Headers() },\n );\n}\n"]}
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.FetchClientError = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
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; } });
|
|
1
|
+
export * from "./FetchClient";
|
|
2
|
+
export { FetchClientError } from "./FetchClientError";
|
|
8
3
|
//# 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,eAAe,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC","sourcesContent":["export * from \"./FetchClient\";\nexport { FetchClientError } from \"./FetchClientError\";\nexport { FetchRequestConfig } from \"./FetchRequestConfig\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,48 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odata2ts/http-client-fetch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"publishConfig": {
|
|
5
|
-
"access": "public"
|
|
6
|
-
},
|
|
3
|
+
"version": "0.7.0",
|
|
7
4
|
"description": "HTTP client based on fetch and consumable by odata2ts",
|
|
8
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"http client",
|
|
7
|
+
"fetch",
|
|
8
|
+
"odata",
|
|
9
|
+
"ts",
|
|
10
|
+
"odata2ts"
|
|
11
|
+
],
|
|
9
12
|
"repository": "git@github.com:odata2ts/http-client.git",
|
|
13
|
+
"license": "MIT",
|
|
10
14
|
"author": "texttechne",
|
|
15
|
+
"type": "module",
|
|
11
16
|
"main": "./lib/index.js",
|
|
17
|
+
"types": "./lib/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"*.md",
|
|
20
|
+
"lib",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
12
23
|
"scripts": {
|
|
13
24
|
"build": "yarn clean && yarn compile",
|
|
14
25
|
"check-circular-deps": "madge ./src --extensions ts --circular",
|
|
15
26
|
"clean": "rimraf lib coverage",
|
|
16
27
|
"compile": "tsc",
|
|
17
|
-
"int-test": "
|
|
28
|
+
"int-test": "vitest run int-test/**",
|
|
18
29
|
"prepublish": "yarn build",
|
|
19
|
-
"test": "
|
|
30
|
+
"test": "vitest run test/**"
|
|
20
31
|
},
|
|
21
|
-
"files": [
|
|
22
|
-
"*.md",
|
|
23
|
-
"lib",
|
|
24
|
-
"LICENSE"
|
|
25
|
-
],
|
|
26
|
-
"keywords": [
|
|
27
|
-
"http client",
|
|
28
|
-
"fetch",
|
|
29
|
-
"odata",
|
|
30
|
-
"ts",
|
|
31
|
-
"odata2ts"
|
|
32
|
-
],
|
|
33
32
|
"dependencies": {
|
|
34
|
-
"@odata2ts/http-client-base": "^0.
|
|
33
|
+
"@odata2ts/http-client-base": "^0.5.0"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@odata2ts/odata-core": "^0.3.7",
|
|
38
|
-
"@types/jest": "^27.4.1",
|
|
39
37
|
"@types/node": "^17.0.23",
|
|
40
|
-
"jest": "^29.5.0",
|
|
41
38
|
"rimraf": "^3.0.2",
|
|
42
|
-
"ts-jest": "^29.1.0",
|
|
43
|
-
"ts-node": "10.7.0",
|
|
44
39
|
"typescript": "5.0.4"
|
|
45
40
|
},
|
|
46
|
-
"
|
|
47
|
-
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "6a0905bd7f201a5c91d83a9319c2a0325a017651"
|
|
48
45
|
}
|