@odata2ts/http-client-fetch 0.5.0 → 0.6.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 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.6.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.4.0...@odata2ts/http-client-fetch@0.6.0) (2023-09-13)
7
+
8
+ ### Code Refactoring
9
+
10
+ * expand additionalHeaders param to internalConfig ([#15](https://github.com/odata2ts/http-client/issues/15)) ([7fe1d73](https://github.com/odata2ts/http-client/commit/7fe1d73a7436f64b84a060bd1dbf9e121ef901ce))
11
+
12
+ ### Features
13
+
14
+ * **fetch:** allow for query params ([#13](https://github.com/odata2ts/http-client/issues/13)) ([1507ed1](https://github.com/odata2ts/http-client/commit/1507ed13c2020de051827db516ae1fc9c7f4b0ac))
15
+
16
+ ### BREAKING CHANGES
17
+
18
+ * additional headers are now part of the config parameter
19
+
20
+ * fix: don't lose configuration when CSRF token is active
21
+
22
+ * add new option which prevents FetchClient from evaluating response body (not needed for fetching csrf tokens & makes trouble with SAP's mockServer)
23
+
24
+ * fix: add headers for content-type and accept for main methods
25
+
6
26
  # [0.5.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.4.0...@odata2ts/http-client-fetch@0.5.0) (2023-09-05)
7
27
 
8
28
  ### Features
@@ -38,6 +58,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
38
58
  ### Features
39
59
 
40
60
  * switch to http-client-api ([52d1b86](https://github.com/odata2ts/http-client/commit/52d1b868ee82dbaf45486da6b22fdcf4c773dfb8))
61
+
41
62
  * switch to http-client-api ([5a6da23](https://github.com/odata2ts/http-client/commit/5a6da23053b3ea5adb866bb7e30b469f1b8ed260))
42
63
 
43
64
  # 0.1.0 (2023-05-29)
package/README.md CHANGED
@@ -1,50 +1,50 @@
1
- [![npm (scoped)](https://img.shields.io/npm/v/@odata2ts/http-client-fetch?style=for-the-badge)](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
+ [![npm (scoped)](https://img.shields.io/npm/v/@odata2ts/http-client-fetch?style=for-the-badge)](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,4 +1,4 @@
1
- import { HttpResponseModel } from "@odata2ts/http-client-api";
1
+ import { HttpResponseModel, InternalHttpClientConfig } from "@odata2ts/http-client-api";
2
2
  import { BaseHttpClient, BaseHttpClientOptions, HttpMethods } from "@odata2ts/http-client-base";
3
3
  import { FetchRequestConfig } from "./FetchRequestConfig";
4
4
  export interface ClientOptions extends BaseHttpClientOptions {
@@ -7,8 +7,7 @@ export declare const DEFAULT_ERROR_MESSAGE = "No error message!";
7
7
  export declare class FetchClient extends BaseHttpClient<FetchRequestConfig> {
8
8
  protected readonly config: RequestInit;
9
9
  constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions);
10
- protected addHeaderToRequestConfig(headers: Record<string, string>, config: FetchRequestConfig | undefined): FetchRequestConfig;
11
- protected executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, requestConfig?: FetchRequestConfig | undefined): Promise<HttpResponseModel<ResponseModel>>;
10
+ protected executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, requestConfig?: FetchRequestConfig | undefined, internalConfig?: InternalHttpClientConfig): Promise<HttpResponseModel<ResponseModel>>;
12
11
  protected getResponseBody(response: Response, isFailedJsonFatal: boolean): Promise<any>;
13
12
  protected mapHeaders(headers: Headers): Record<string, string>;
14
13
  }
@@ -18,12 +18,10 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
18
18
  super(clientOptions);
19
19
  this.config = (0, FetchRequestConfig_1.getDefaultConfig)(config);
20
20
  }
21
- addHeaderToRequestConfig(headers, config) {
22
- return (0, FetchRequestConfig_1.mergeFetchConfig)({ headers }, config);
23
- }
24
- executeRequest(method, url, data, requestConfig = {}) {
21
+ executeRequest(method, url, data, requestConfig = {}, internalConfig = {}) {
25
22
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
26
- const _a = (0, FetchRequestConfig_1.mergeFetchConfig)(this.config, requestConfig), { params } = _a, config = tslib_1.__rest(_a, ["params"]);
23
+ const { headers, noBodyEvaluation } = internalConfig;
24
+ const _a = (0, FetchRequestConfig_1.mergeFetchConfig)(this.config, { headers }, requestConfig), { params } = _a, config = tslib_1.__rest(_a, ["params"]);
27
25
  config.method = method;
28
26
  if (typeof data !== "undefined") {
29
27
  config.body = JSON.stringify(data);
@@ -49,7 +47,7 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
49
47
  const errMsg = this.retrieveErrorMessage(responseData);
50
48
  throw new FetchClientError_1.FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), response);
51
49
  }
52
- const responseData = yield this.getResponseBody(response, true);
50
+ const responseData = noBodyEvaluation ? undefined : yield this.getResponseBody(response, true);
53
51
  return {
54
52
  status: response.status,
55
53
  statusText: response.statusText,
@@ -1 +1 @@
1
- {"version":3,"file":"FetchClient.js","sourceRoot":"","sources":["../src/FetchClient.ts"],"names":[],"mappings":";;;;AACA,iEAAgG;AAEhG,yDAAsD;AACtD,6DAA8F;AAIjF,QAAA,qBAAqB,GAAG,mBAAmB,CAAC;AACzD,MAAM,qBAAqB,GAAG,iCAAiC,CAAC;AAChE,MAAM,8BAA8B,GAAG,mDAAmD,CAAC;AAC3F,MAAM,wBAAwB,GAAG,qCAAqC,CAAC;AAEvE,SAAS,iBAAiB,CAAC,MAAc,EAAE,KAAU;IACnD,MAAM,GAAG,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,KAAe,aAAf,KAAK,uBAAL,KAAK,CAAY,OAAO,CAAC;IAC1E,OAAO,MAAM,GAAG,CAAC,GAAG,IAAI,6BAAqB,CAAC,CAAC;AACjD,CAAC;AAED,MAAa,WAAY,SAAQ,iCAAkC;IAGjE,YAAY,MAA2B,EAAE,aAA6B;QACpE,KAAK,CAAC,aAAa,CAAC,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,IAAA,qCAAgB,EAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAES,wBAAwB,CAChC,OAA+B,EAC/B,MAAsC;QAEtC,OAAO,IAAA,qCAAgB,EAAC,EAAE,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;IAEe,cAAc,CAC5B,MAAmB,EACnB,GAAW,EACX,IAAS,EACT,gBAAgD,EAAE;;YAElD,MAAM,KAAwB,IAAA,qCAAgB,EAAC,IAAI,CAAC,MAAM,EAAE,aAAa,CAAC,EAApE,EAAE,MAAM,OAA4D,EAAvD,MAAM,sBAAnB,UAAqB,CAA+C,CAAC;YAC3E,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;YACvB,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE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,EAAE,QAAQ,CAAC,MAAM;gBACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;gBAC/B,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAC1C,IAAI,EAAE,YAAY;aACnB,CAAC;QACJ,CAAC;KAAA;IAEe,eAAe,CAAC,QAAkB,EAAE,iBAA0B;;YAC5E,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;gBAC3B,OAAO,SAAS,CAAC;aAClB;YACD,IAAI;gBACF,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;aAC9B;YAAC,OAAO,KAAK,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;AAhGD,kCAgGC","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, 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({ headers }, config);\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 { params, ...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 let finalUrl = url;\r\n if (params && Object.values(params).length) {\r\n finalUrl +=\r\n (url.match(/\\?/) ? \"&\" : \"?\") +\r\n // @ts-ignore\r\n new URLSearchParams(params).toString();\r\n }\r\n\r\n // the actual request\r\n let response: Response;\r\n try {\r\n response = await fetch(finalUrl, 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"]}
1
+ {"version":3,"file":"FetchClient.js","sourceRoot":"","sources":["../src/FetchClient.ts"],"names":[],"mappings":";;;;AACA,iEAAgG;AAEhG,yDAAsD;AACtD,6DAA8F;AAIjF,QAAA,qBAAqB,GAAG,mBAAmB,CAAC;AACzD,MAAM,qBAAqB,GAAG,iCAAiC,CAAC;AAChE,MAAM,8BAA8B,GAAG,mDAAmD,CAAC;AAC3F,MAAM,wBAAwB,GAAG,qCAAqC,CAAC;AAEvE,SAAS,iBAAiB,CAAC,MAAc,EAAE,KAAU;IACnD,MAAM,GAAG,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,KAAe,aAAf,KAAK,uBAAL,KAAK,CAAY,OAAO,CAAC;IAC1E,OAAO,MAAM,GAAG,CAAC,GAAG,IAAI,6BAAqB,CAAC,CAAC;AACjD,CAAC;AAED,MAAa,WAAY,SAAQ,iCAAkC;IAGjE,YAAY,MAA2B,EAAE,aAA6B;QACpE,KAAK,CAAC,aAA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AgB;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;AA3FD,kCA2FC","sourcesContent":["import { HttpResponseModel, InternalHttpClientConfig } 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, clientOptions?: ClientOptions) {\r\n super(clientOptions);\r\n this.config = getDefaultConfig(config);\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 internalConfig: InternalHttpClientConfig = {}\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n const { headers, noBodyEvaluation } = internalConfig;\r\n const { params, ...config } = mergeFetchConfig(this.config, { headers }, requestConfig);\r\n config.method = method;\r\n if (typeof data !== \"undefined\") {\r\n config.body = JSON.stringify(data);\r\n }\r\n let finalUrl = url;\r\n if (params && Object.values(params).length) {\r\n finalUrl +=\r\n (url.match(/\\?/) ? \"&\" : \"?\") +\r\n // @ts-ignore\r\n new URLSearchParams(params).toString();\r\n }\r\n\r\n // the actual request\r\n let response: Response;\r\n try {\r\n response = await fetch(finalUrl, 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 = noBodyEvaluation ? undefined : 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"]}
package/lib/index.js.map CHANGED
@@ -1 +1 @@
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"]}
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\";\r\nexport { FetchClientError } from \"./FetchClientError\";\r\nexport { FetchRequestConfig } from \"./FetchRequestConfig\";\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odata2ts/http-client-fetch",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -31,7 +31,7 @@
31
31
  "odata2ts"
32
32
  ],
33
33
  "dependencies": {
34
- "@odata2ts/http-client-base": "^0.3.0"
34
+ "@odata2ts/http-client-base": "^0.4.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": "ed6b01821c2dc161f05144e3b4d8f8d899f954ae"
47
+ "gitHead": "67c7fc10f25461c33baf4e35694a457a03b81b78"
48
48
  }