@odata2ts/http-client-fetch 0.7.2 → 0.8.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,12 @@
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.8.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.7.2...@odata2ts/http-client-fetch@0.8.0) (2024-08-24)
7
+
8
+ ### Features
9
+
10
+ * **fetch:** FetchClientError with full responseData object ([#22](https://github.com/odata2ts/http-client/issues/22)) ([e66fa95](https://github.com/odata2ts/http-client/commit/e66fa952909383d55555eed23d1a8e55fe0081f2))
11
+
6
12
  ## [0.7.2](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-fetch@0.7.1...@odata2ts/http-client-fetch@0.7.2) (2024-08-22)
7
13
 
8
14
  **Note:** Version bump only for package @odata2ts/http-client-fetch
@@ -52,7 +52,7 @@ class FetchClient extends http_client_base_1.BaseHttpClient {
52
52
  responseData = undefined;
53
53
  }
54
54
  const errMsg = this.retrieveErrorMessage(responseData);
55
- throw new FetchClientError_1.FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), response);
55
+ throw new FetchClientError_1.FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), responseData);
56
56
  }
57
57
  let responseData;
58
58
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"FetchClient.js","sourceRoot":"","sources":["../src/FetchClient.ts"],"names":[],"mappings":";;;;AACA,iEAKoC;AACpC,yDAAsD;AACtD,6DAA8F;AAIjF,QAAA,qBAAqB,GAAG,mBAAmB,CAAC;AACzD,MAAM,qBAAqB,GAA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\n BaseHttpClient,\r\n BaseHttpClientOptions,\r\n HttpMethods,\r\n InternalHttpClientConfig,\r\n} from \"@odata2ts/http-client-base\";\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 BLOB_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving blob 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> implements ODataHttpClient<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 = internalConfig.dataType === \"json\" ? JSON.stringify(data) : 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;\r\n try {\r\n responseData = await this.getResponseBody(response, internalConfig);\r\n } catch (e) {\r\n responseData = undefined;\r\n }\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 let responseData;\r\n try {\r\n responseData = noBodyEvaluation ? undefined : await this.getResponseBody(response, internalConfig);\r\n } catch (error) {\r\n const msg = internalConfig.dataType === \"blob\" ? BLOB_RETRIEVAL_FAILURE_MESSAGE : JSON_RETRIEVAL_FAILURE_MESSAGE;\r\n throw new FetchClientError(\r\n buildErrorMessage(msg, error),\r\n response.status,\r\n this.mapHeaders(response.headers),\r\n error as Error,\r\n );\r\n }\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, options: InternalHttpClientConfig) {\r\n if (response.status === 204) {\r\n return undefined;\r\n }\r\n switch (options.dataType) {\r\n case \"json\":\r\n return response.json();\r\n case \"blob\":\r\n return response.blob();\r\n case \"stream\":\r\n return response.body;\r\n }\r\n\r\n return undefined;\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":[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vD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAxGD,kCAwGC","sourcesContent":["import { HttpResponseModel, ODataHttpClient } from \"@odata2ts/http-client-api\";\r\nimport {\r\n BaseHttpClient,\r\n BaseHttpClientOptions,\r\n HttpMethods,\r\n InternalHttpClientConfig,\r\n} from \"@odata2ts/http-client-base\";\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 BLOB_RETRIEVAL_FAILURE_MESSAGE = \"Retrieving blob 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> implements ODataHttpClient<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 = internalConfig.dataType === \"json\" ? JSON.stringify(data) : 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;\r\n try {\r\n responseData = await this.getResponseBody(response, internalConfig);\r\n } catch (e) {\r\n responseData = undefined;\r\n }\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 responseData,\r\n );\r\n }\r\n\r\n let responseData;\r\n try {\r\n responseData = noBodyEvaluation ? undefined : await this.getResponseBody(response, internalConfig);\r\n } catch (error) {\r\n const msg = internalConfig.dataType === \"blob\" ? BLOB_RETRIEVAL_FAILURE_MESSAGE : JSON_RETRIEVAL_FAILURE_MESSAGE;\r\n throw new FetchClientError(\r\n buildErrorMessage(msg, error),\r\n response.status,\r\n this.mapHeaders(response.headers),\r\n error as Error,\r\n );\r\n }\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, options: InternalHttpClientConfig) {\r\n if (response.status === 204) {\r\n return undefined;\r\n }\r\n switch (options.dataType) {\r\n case \"json\":\r\n return response.json();\r\n case \"blob\":\r\n return response.blob();\r\n case \"stream\":\r\n return response.body;\r\n }\r\n\r\n return undefined;\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"]}
@@ -3,6 +3,6 @@ export declare class FetchClientError extends Error implements ODataClientError
3
3
  readonly status?: number | undefined;
4
4
  readonly headers?: Record<string, string> | undefined;
5
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);
6
+ readonly responseData?: unknown;
7
+ constructor(message: string, status?: number | undefined, headers?: Record<string, string> | undefined, cause?: Error | undefined, responseData?: unknown);
8
8
  }
@@ -2,13 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FetchClientError = void 0;
4
4
  class FetchClientError extends Error {
5
- constructor(message, status, headers, cause, response) {
5
+ constructor(message, status, headers, cause, responseData) {
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
9
  this.headers = headers;
10
10
  this.cause = cause;
11
- this.response = response;
11
+ this.responseData = responseData;
12
12
  this.name = this.constructor.name;
13
13
  }
14
14
  }
@@ -1 +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\";\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
+ {"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,YAAsB;QAEtC,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,iBAAY,GAAZ,YAAY,CAAU;QAItC,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 responseData?: unknown,\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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odata2ts/http-client-fetch",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "description": "HTTP client based on fetch and consumable by odata2ts",
5
5
  "keywords": [
6
6
  "http client",
@@ -25,12 +25,13 @@
25
25
  "check-circular-deps": "madge ./src --extensions ts --circular",
26
26
  "clean": "rimraf lib coverage",
27
27
  "compile": "tsc",
28
- "int-test": "vitest run int-test/**",
29
- "prepublish": "yarn build",
30
- "test": "vitest run test/**"
28
+ "unit-test": "vitest run --dir test",
29
+ "int-test": "vitest run --dir int-test",
30
+ "test": "vitest run",
31
+ "prepublish": "yarn build"
31
32
  },
32
33
  "dependencies": {
33
- "@odata2ts/http-client-base": "^0.5.2"
34
+ "@odata2ts/http-client-base": "^0.5.3"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@odata2ts/odata-core": "^0.3.7",
@@ -41,5 +42,5 @@
41
42
  "publishConfig": {
42
43
  "access": "public"
43
44
  },
44
- "gitHead": "20d77b211d6c7a44e7f1d14e9b63be531bb30e87"
45
+ "gitHead": "d74b162906b89826cce7944308bc5950e325389f"
45
46
  }