@odata2ts/http-client-fetch 0.3.1 → 0.4.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 +14 -0
- package/lib/FetchClient.d.ts +1 -2
- package/lib/FetchClient.js +1 -2
- package/lib/FetchClient.js.map +1 -1
- package/lib/FetchRequestConfig.js +2 -2
- package/lib/FetchRequestConfig.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,20 @@
|
|
|
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.4.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.3.1...@odata2ts/http-client-fetch@0.4.0) (2023-08-03)
|
|
7
|
+
|
|
8
|
+
### Code Refactoring
|
|
9
|
+
|
|
10
|
+
* **fetch:** remove default headers ([39bea92](https://github.com/odata2ts/http-client/commit/39bea92a2b8335af8a1588a4156974fcbd5ae417))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* allow for additional headers for all operations ([#10](https://github.com/odata2ts/http-client/issues/10)) ([75eedd3](https://github.com/odata2ts/http-client/commit/75eedd3ebb8534188a5a644aee9e69e17f1f0c80))
|
|
15
|
+
|
|
16
|
+
### BREAKING CHANGES
|
|
17
|
+
|
|
18
|
+
* **fetch:** removed default accept & content-type headers; removed merge & retrieveBigNumbersAsString methods (base-lib); all of these settings can now be configured per operation via the `additionalHeaders` option.
|
|
19
|
+
|
|
6
20
|
## [0.3.1](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.3.0...@odata2ts/http-client-fetch@0.3.1) (2023-07-26)
|
|
7
21
|
|
|
8
22
|
**Note:** Version bump only for package @odata2ts/http-client-fetch
|
package/lib/FetchClient.d.ts
CHANGED
|
@@ -5,9 +5,8 @@ export interface ClientOptions extends BaseHttpClientOptions {
|
|
|
5
5
|
}
|
|
6
6
|
export declare const DEFAULT_ERROR_MESSAGE = "No error message!";
|
|
7
7
|
export declare class FetchClient extends BaseHttpClient<FetchRequestConfig> {
|
|
8
|
-
private clientOptions?;
|
|
9
8
|
protected readonly config: RequestInit;
|
|
10
|
-
constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions
|
|
9
|
+
constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions);
|
|
11
10
|
protected addHeaderToRequestConfig(headers: Record<string, string>, config: FetchRequestConfig | undefined): FetchRequestConfig;
|
|
12
11
|
protected executeRequest<ResponseModel>(method: HttpMethods, url: string, data: any, requestConfig?: FetchRequestConfig | undefined): Promise<HttpResponseModel<ResponseModel>>;
|
|
13
12
|
protected getResponseBody(response: Response, isFailedJsonFatal: boolean): Promise<any>;
|
package/lib/FetchClient.js
CHANGED
|
@@ -16,11 +16,10 @@ function buildErrorMessage(prefix, error) {
|
|
|
16
16
|
class FetchClient extends http_client_base_1.BaseHttpClient {
|
|
17
17
|
constructor(config, clientOptions) {
|
|
18
18
|
super(clientOptions);
|
|
19
|
-
this.clientOptions = clientOptions;
|
|
20
19
|
this.config = (0, FetchRequestConfig_1.getDefaultConfig)(config);
|
|
21
20
|
}
|
|
22
21
|
addHeaderToRequestConfig(headers, config) {
|
|
23
|
-
return (0, FetchRequestConfig_1.mergeFetchConfig)(
|
|
22
|
+
return (0, FetchRequestConfig_1.mergeFetchConfig)({ headers }, config);
|
|
24
23
|
}
|
|
25
24
|
executeRequest(method, url, data, requestConfig = {}) {
|
|
26
25
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
package/lib/FetchClient.js.map
CHANGED
|
@@ -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,M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AQ,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;AAzFD,kCAyFC","sourcesContent":["import { HttpResponseModel } from \"@odata2ts/http-client-api\";\nimport { BaseHttpClient, BaseHttpClientOptions, HttpMethods } from \"@odata2ts/http-client-base\";\n\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 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> {\n protected readonly config: RequestInit;\n\n constructor(config?: FetchRequestConfig, clientOptions?: ClientOptions) {\n super(clientOptions);\n this.config = getDefaultConfig(config);\n }\n\n protected addHeaderToRequestConfig(\n headers: Record<string, string>,\n config: FetchRequestConfig | undefined\n ): FetchRequestConfig {\n return mergeFetchConfig({ headers }, config);\n }\n\n protected async executeRequest<ResponseModel>(\n method: HttpMethods,\n url: string,\n data: any,\n requestConfig: FetchRequestConfig | undefined = {}\n ): Promise<HttpResponseModel<ResponseModel>> {\n const config = mergeFetchConfig(this.config, requestConfig);\n config.method = method;\n if (typeof data !== \"undefined\") {\n config.body = JSON.stringify(data);\n }\n\n // the actual request\n let response: Response;\n try {\n response = await fetch(url, 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 = await this.getResponseBody(response, false);\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 const responseData = await this.getResponseBody(response, true);\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, isFailedJsonFatal: boolean) {\n if (response.status === 204) {\n return undefined;\n }\n try {\n return await response.json();\n } catch (error) {\n if (isFailedJsonFatal) {\n throw new FetchClientError(\n buildErrorMessage(JSON_RETRIEVAL_FAILURE_MESSAGE, error),\n response.status,\n this.mapHeaders(response.headers),\n error as Error\n );\n }\n return undefined;\n }\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"]}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mergeFetchConfig = exports.getDefaultConfig = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const DEFAULT_CONFIG = {
|
|
6
|
-
headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
7
|
-
cache: "no-
|
|
6
|
+
// headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
7
|
+
cache: "no-store",
|
|
8
8
|
};
|
|
9
9
|
function getDefaultConfig(config) {
|
|
10
10
|
return mergeFetchConfig(DEFAULT_CONFIG, config);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchRequestConfig.js","sourceRoot":"","sources":["../src/FetchRequestConfig.ts"],"names":[],"mappings":";;;;AAAA,MAAM,cAAc,GAAgB;IAClC
|
|
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;AAcF,SAAgB,gBAAgB,CAAC,MAA2B;IAC1D,OAAO,gBAAgB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC;AAFD,4CAEC;AAID,SAAgB,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,kBAAK,OAAO,EAArC,WAA2B,CAAU,CAAC;QAC5C,MAAM,gBAAgB,GAAG,SAAS,CAAC,OAAkB,CAAC;QAEtD,4BAA4B;QAC5B,IAAI,OAAO,IAAI,OAAO,YAAY,OAAO,EAAE;YACzC,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;AAxBD,4CAwBC","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 Pick<RequestInit, \"credentials\" | \"cache\" | \"mode\" | \"redirect\" | \"referrerPolicy\"> {\n headers?: Record<string, string> | Headers;\n}\n\nexport interface InternalFetchConfig extends Omit<RequestInit, \"headers\"> {\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 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odata2ts/http-client-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.
|
|
34
|
+
"@odata2ts/http-client-base": "^0.3.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": "dc6e29cef27b2f5fca6e838e4a22766b90249e8e"
|
|
48
48
|
}
|