@odata2ts/http-client-base 0.1.0 → 0.2.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.2.0](https://github.com/odata2ts/http-client/compare/@odata2ts/http-client-base@0.1.0...@odata2ts/http-client-base@0.2.0) (2023-07-26)
7
+
8
+ ### Features
9
+
10
+ * big numbers as string ([#7](https://github.com/odata2ts/http-client/issues/7)) ([5119923](https://github.com/odata2ts/http-client/commit/5119923a79c2e61ca7762d5cba01fbac8e9ae759))
11
+
6
12
  # 0.1.0 (2023-06-10)
7
13
 
8
14
  ### Features
@@ -2,14 +2,27 @@ import { HttpResponseModel, ODataHttpClient } from "@odata2ts/http-client-api";
2
2
  import { ErrorMessageRetriever } from "./ErrorMessageRetriever";
3
3
  import { HttpMethods } from "./HttpMethods";
4
4
  export interface BaseHttpClientOptions {
5
+ /**
6
+ * Enable automatic CSRF token handling.
7
+ */
5
8
  useCsrfProtection?: boolean;
9
+ /**
10
+ * Specify the URL from which the token is fetched.
11
+ * This could be any path to your OData service, since the token is exchanged via HTTP request headers.
12
+ * However, it should be a fast response and usually the root URL to the OData service is a good choice.
13
+ */
6
14
  csrfTokenFetchUrl?: string;
7
15
  }
8
16
  export declare const DEFAULT_CSRF_TOKEN_KEY = "x-csrf-token";
17
+ export declare const BIG_NUMBERS_CONFIG: {
18
+ Accept: string;
19
+ "Content-Type": string;
20
+ };
9
21
  export declare abstract class BaseHttpClient<RequestConfigType> implements ODataHttpClient<RequestConfigType> {
10
22
  private baseOptions;
11
23
  private csrfToken;
12
24
  private csrfTokenKey;
25
+ private bigNumbersAsString;
13
26
  protected retrieveErrorMessage: ErrorMessageRetriever;
14
27
  protected constructor(baseOptions?: BaseHttpClientOptions);
15
28
  /**
@@ -53,4 +66,5 @@ export declare abstract class BaseHttpClient<RequestConfigType> implements OData
53
66
  patch<ResponseModel>(url: string, data: any, requestConfig?: RequestConfigType): Promise<HttpResponseModel<ResponseModel>>;
54
67
  merge<ResponseModel>(url: string, data: any, requestConfig?: RequestConfigType): Promise<HttpResponseModel<ResponseModel>>;
55
68
  delete(url: string, requestConfig?: RequestConfigType): Promise<HttpResponseModel<void>>;
69
+ retrieveBigNumbersAsString(enabled: boolean): void;
56
70
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseHttpClient = exports.DEFAULT_CSRF_TOKEN_KEY = void 0;
3
+ exports.BaseHttpClient = exports.BIG_NUMBERS_CONFIG = exports.DEFAULT_CSRF_TOKEN_KEY = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ErrorMessageRetriever_1 = require("./ErrorMessageRetriever");
6
6
  const HttpMethods_1 = require("./HttpMethods");
7
7
  exports.DEFAULT_CSRF_TOKEN_KEY = "x-csrf-token";
8
+ const BIG_NUMBER_FORMAT = "application/json;IEEE754Compatible=true";
9
+ exports.BIG_NUMBERS_CONFIG = { Accept: BIG_NUMBER_FORMAT, "Content-Type": BIG_NUMBER_FORMAT };
8
10
  const EDIT_METHODS = ["POST", "PUT", "PATCH", "DELETE"];
9
11
  const FAILURE_MISSING_CSRF_URL = "When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!";
10
12
  const FAILURE_MISSING_URL = "Value for URL must be provided!";
@@ -13,6 +15,7 @@ class BaseHttpClient {
13
15
  var _a;
14
16
  this.baseOptions = baseOptions;
15
17
  this.csrfTokenKey = exports.DEFAULT_CSRF_TOKEN_KEY;
18
+ this.bigNumbersAsString = false;
16
19
  this.retrieveErrorMessage = ErrorMessageRetriever_1.retrieveErrorMessage;
17
20
  if (baseOptions.useCsrfProtection && !((_a = baseOptions.csrfTokenFetchUrl) === null || _a === void 0 ? void 0 : _a.trim())) {
18
21
  throw new Error(FAILURE_MISSING_CSRF_URL);
@@ -57,7 +60,7 @@ class BaseHttpClient {
57
60
  if (typeof url !== "string") {
58
61
  throw new Error(FAILURE_MISSING_URL);
59
62
  }
60
- let config = requestConfig;
63
+ let config = this.bigNumbersAsString ? this.addHeaderToRequestConfig(exports.BIG_NUMBERS_CONFIG) : requestConfig;
61
64
  // setup automatic CSRF token handling
62
65
  if (this.baseOptions.useCsrfProtection && EDIT_METHODS.includes(method)) {
63
66
  const [tokenKey, tokenValue] = yield this.setupSecurityToken();
@@ -101,6 +104,9 @@ class BaseHttpClient {
101
104
  delete(url, requestConfig) {
102
105
  return this.sendRequest(HttpMethods_1.HttpMethods.Delete, url, undefined, requestConfig);
103
106
  }
107
+ retrieveBigNumbersAsString(enabled) {
108
+ this.bigNumbersAsString = enabled;
109
+ }
104
110
  }
105
111
  exports.BaseHttpClient = BaseHttpClient;
106
112
  //# sourceMappingURL=BaseHttpClient.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseHttpClient.js","sourceRoot":"","sources":["../src/BaseHttpClient.ts"],"names":[],"mappings":";;;;AAEA,mEAAsF;AACtF,+CAA4C;AAO/B,QAAA,sBAAsB,GAAG,cAAc,CAAC;AAErD,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AACxD,MAAM,wBAAwB,GAC5B,8GAA8G,CAAC;AACjH,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;AAE9D,MAAsB,cAAc;IAMlC,YAA8B,cAAqC,EAAE,iBAAiB,EAAE,KAAK,EAAE;;QAAjE,gBAAW,GAAX,WAAW,CAAsD;QAJvF,iBAAY,GAAG,8BAAsB,CAAC;QAEpC,yBAAoB,GAA0B,4CAAoB,CAAC;QAG3E,IAAI,WAAW,CAAC,iBAAiB,IAAI,CAAC,CAAA,MAAA,WAAW,CAAC,iBAAiB,0CAAE,IAAI,EAAE,CAAA,EAAE;YAC3E,MAAM,IAAI,KAAK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EAAE,aAAa,CAAC,CAAC;IACpF,CAAC;IACM,KAAK,CACV,GAAW,EACX,IAAS,EACT,aAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CAAgB,yBAAW,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;IACtF,CAAC;IACM,KAAK,CACV,GAAW,EACX,IAAS,EACT,aAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CACrB,yBAAW,CAAC,IAAI,EAChB,GAAG,EACH,IAAI,EACJ,IAAI,CAAC,wBAAwB,CAAC,EAAE,eAAe,EAAE,OAAO,EAAE,EAAE,aAAa,CAAC,CAC3E,CAAC;IACJ,CAAC;IACM,MAAM,CAAC,GAAW,EAAE,aAAiC;QAC1D,OAAO,IAAI,CAAC,WAAW,CAAO,yBAAW,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;IACnF,CAAC;CACF;AA9JD,wCA8JC","sourcesContent":["import { HttpResponseModel, ODataClientError, ODataHttpClient } from \"@odata2ts/http-client-api\";\r\n\r\nimport { ErrorMessageRetriever, retrieveErrorMessage } from \"./ErrorMessageRetriever\";\r\nimport { HttpMethods } from \"./HttpMethods\";\r\n\r\nexport interface BaseHttpClientOptions {\r\n useCsrfProtection?: boolean;\r\n csrfTokenFetchUrl?: string;\r\n}\r\n\r\nexport const DEFAULT_CSRF_TOKEN_KEY = \"x-csrf-token\";\r\n\r\nconst EDIT_METHODS = [\"POST\", \"PUT\", \"PATCH\", \"DELETE\"];\r\nconst FAILURE_MISSING_CSRF_URL =\r\n \"When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!\";\r\nconst FAILURE_MISSING_URL = \"Value for URL must be provided!\";\r\n\r\nexport abstract class BaseHttpClient<RequestConfigType> implements ODataHttpClient<RequestConfigType> {\r\n private csrfToken: string | undefined;\r\n private csrfTokenKey = DEFAULT_CSRF_TOKEN_KEY;\r\n\r\n protected retrieveErrorMessage: ErrorMessageRetriever = retrieveErrorMessage;\r\n\r\n protected constructor(private baseOptions: BaseHttpClientOptions = { useCsrfProtection: false }) {\r\n if (baseOptions.useCsrfProtection && !baseOptions.csrfTokenFetchUrl?.trim()) {\r\n throw new Error(FAILURE_MISSING_CSRF_URL);\r\n }\r\n }\r\n\r\n /**\r\n * Use the given headers to either create an entire new request config or merge them into the given\r\n * request config.\r\n *\r\n * @param headers\r\n * @param config\r\n * @returns request configuration\r\n */\r\n protected abstract addHeaderToRequestConfig(\r\n headers: Record<string, string>,\r\n config?: RequestConfigType\r\n ): RequestConfigType;\r\n\r\n /**\r\n * Main function to implement by any extending http client.\r\n * As it name suggests, the request gets executed in this method.\r\n * Additionally, failures should be handled and errors of type <code>HttpClientError</code> should be thrown.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param config\r\n */\r\n protected abstract executeRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n config?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>>;\r\n\r\n public getCsrfTokenKey() {\r\n return this.csrfTokenKey;\r\n }\r\n\r\n public setCsrfTokenKey(newKey: string) {\r\n this.csrfTokenKey = newKey || DEFAULT_CSRF_TOKEN_KEY;\r\n }\r\n\r\n public setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever) {\r\n this.retrieveErrorMessage = getErrorMsg;\r\n }\r\n\r\n protected async setupSecurityToken(): Promise<[string, string | undefined]> {\r\n if (!this.csrfToken) {\r\n this.csrfToken = await this.fetchSecurityToken();\r\n }\r\n return [this.csrfTokenKey, this.csrfToken];\r\n }\r\n\r\n protected async fetchSecurityToken(): Promise<string | undefined> {\r\n const fetchUrl = this.baseOptions!.csrfTokenFetchUrl!;\r\n const response = await this.get(fetchUrl, this.addHeaderToRequestConfig({ [this.csrfTokenKey]: \"Fetch\" }));\r\n\r\n return response.headers[this.csrfTokenKey];\r\n }\r\n\r\n /**\r\n * Follows the template pattern.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param requestConfig\r\n * @private\r\n */\r\n private async sendRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n // noinspection SuspiciousTypeOfGuard\r\n if (typeof url !== \"string\") {\r\n throw new Error(FAILURE_MISSING_URL);\r\n }\r\n\r\n let config = requestConfig;\r\n\r\n // setup automatic CSRF token handling\r\n if (this.baseOptions.useCsrfProtection && EDIT_METHODS.includes(method)) {\r\n const [tokenKey, tokenValue] = await this.setupSecurityToken();\r\n if (tokenValue) {\r\n config = this.addHeaderToRequestConfig({ [tokenKey]: tokenValue }, requestConfig);\r\n }\r\n }\r\n\r\n try {\r\n return await this.executeRequest<ResponseModel>(method, url, data, config);\r\n } catch (e) {\r\n const clientError = e as ODataClientError;\r\n\r\n // automatic CSRF token handling\r\n if (\r\n !!this.baseOptions.useCsrfProtection &&\r\n clientError.status === 403 &&\r\n !!clientError.headers &&\r\n clientError.headers[\"x-csrf-token\"] === \"Required\"\r\n ) {\r\n // token has expired: reset csrf token & perform the original request again\r\n this.csrfToken = undefined;\r\n return this.sendRequest<ResponseModel>(method, url, data, requestConfig);\r\n }\r\n\r\n throw e;\r\n }\r\n }\r\n\r\n public get<ResponseModel>(url: string, requestConfig?: RequestConfigType): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Get, url, undefined, requestConfig);\r\n }\r\n public post<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Post, url, data, requestConfig);\r\n }\r\n public put<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Put, url, data, requestConfig);\r\n }\r\n public patch<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Patch, url, data, requestConfig);\r\n }\r\n public merge<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Post,\r\n url,\r\n data,\r\n this.addHeaderToRequestConfig({ \"X-Http-Method\": \"MERGE\" }, requestConfig)\r\n );\r\n }\r\n public delete(url: string, requestConfig?: RequestConfigType): Promise<HttpResponseModel<void>> {\r\n return this.sendRequest<void>(HttpMethods.Delete, url, undefined, requestConfig);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"BaseHttpClient.js","sourceRoot":"","sources":["../src/BaseHttpClient.ts"],"names":[],"mappings":";;;;AAEA,mEAAsF;AACtF,+CAA4C;AAe/B,QAAA,sBAAsB,GAAG,cAAc,CAAC;AACrD,MAAM,iBAAiB,GAAG,yCAAyC,CAAC;AACvD,QAAA,kBAAkB,GAAG,EAAE,MAAM,EAAE,iBAAiB,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC;AAEnG,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AACxD,MAAM,wBAAwB,GAC5B,8GAA8G,CAAC;AACjH,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;AAE9D,MAAsB,cAAc;IAOlC,YAA8B,cAAqC,EAAE,iBAAiB,EAAE,KAAK,EAAE;;QAAjE,gBAAW,GAAX,WAAW,CAAsD;QALvF,iBAAY,GAAG,8BAAsB,CAAC;QACtC,uBAAkB,GAAY,KAAK,CAAC;QAElC,yBAAoB,GAA0B,4CAAoB,CAAC;QAG3E,IAAI,WAAW,CAAC,iBAAiB,IAAI,CAAC,CAAA,MAAA,WAAW,CAAC,iBAAiB,0CAAE,I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CAAC;IACrF,CAAC;IACM,GAAG,CACR,GAAW,EACX,IAAS,EACT,aAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CAAgB,yBAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;IACpF,CAAC;IACM,KAAK,CACV,GAAW,EACX,IAAS,EACT,aAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CAAgB,yBAAW,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;IACtF,CAAC;IACM,KAAK,CACV,GAAW,EACX,IAAS,EACT,aAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CACrB,yBAAW,CAAC,IAAI,EAChB,GAAG,EACH,IAAI,EACJ,IAAI,CAAC,wBAAwB,CAAC,EAAE,eAAe,EAAE,OAAO,EAAE,EAAE,aAAa,CAAC,CAC3E,CAAC;IACJ,CAAC;IACM,MAAM,CAAC,GAAW,EAAE,aAAiC;QAC1D,OAAO,IAAI,CAAC,WAAW,CAAO,yBAAW,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;IACnF,CAAC;IAEM,0BAA0B,CAAC,OAAgB;QAChD,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC;IACpC,CAAC;CACF;AAnKD,wCAmKC","sourcesContent":["import { HttpResponseModel, ODataClientError, ODataHttpClient } from \"@odata2ts/http-client-api\";\r\n\r\nimport { ErrorMessageRetriever, retrieveErrorMessage } from \"./ErrorMessageRetriever\";\r\nimport { HttpMethods } from \"./HttpMethods\";\r\n\r\nexport interface BaseHttpClientOptions {\r\n /**\r\n * Enable automatic CSRF token handling.\r\n */\r\n useCsrfProtection?: boolean;\r\n /**\r\n * Specify the URL from which the token is fetched.\r\n * This could be any path to your OData service, since the token is exchanged via HTTP request headers.\r\n * However, it should be a fast response and usually the root URL to the OData service is a good choice.\r\n */\r\n csrfTokenFetchUrl?: string;\r\n}\r\n\r\nexport const DEFAULT_CSRF_TOKEN_KEY = \"x-csrf-token\";\r\nconst BIG_NUMBER_FORMAT = \"application/json;IEEE754Compatible=true\";\r\nexport const BIG_NUMBERS_CONFIG = { Accept: BIG_NUMBER_FORMAT, \"Content-Type\": BIG_NUMBER_FORMAT };\r\n\r\nconst EDIT_METHODS = [\"POST\", \"PUT\", \"PATCH\", \"DELETE\"];\r\nconst FAILURE_MISSING_CSRF_URL =\r\n \"When automatic CSRF token handling is activated, the URL must be supplied via attribute [csrfTokenFetchUrl]!\";\r\nconst FAILURE_MISSING_URL = \"Value for URL must be provided!\";\r\n\r\nexport abstract class BaseHttpClient<RequestConfigType> implements ODataHttpClient<RequestConfigType> {\r\n private csrfToken: string | undefined;\r\n private csrfTokenKey = DEFAULT_CSRF_TOKEN_KEY;\r\n private bigNumbersAsString: boolean = false;\r\n\r\n protected retrieveErrorMessage: ErrorMessageRetriever = retrieveErrorMessage;\r\n\r\n protected constructor(private baseOptions: BaseHttpClientOptions = { useCsrfProtection: false }) {\r\n if (baseOptions.useCsrfProtection && !baseOptions.csrfTokenFetchUrl?.trim()) {\r\n throw new Error(FAILURE_MISSING_CSRF_URL);\r\n }\r\n }\r\n\r\n /**\r\n * Use the given headers to either create an entire new request config or merge them into the given\r\n * request config.\r\n *\r\n * @param headers\r\n * @param config\r\n * @returns request configuration\r\n */\r\n protected abstract addHeaderToRequestConfig(\r\n headers: Record<string, string>,\r\n config?: RequestConfigType\r\n ): RequestConfigType;\r\n\r\n /**\r\n * Main function to implement by any extending http client.\r\n * As it name suggests, the request gets executed in this method.\r\n * Additionally, failures should be handled and errors of type <code>HttpClientError</code> should be thrown.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param config\r\n */\r\n protected abstract executeRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n config?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>>;\r\n\r\n public getCsrfTokenKey() {\r\n return this.csrfTokenKey;\r\n }\r\n\r\n public setCsrfTokenKey(newKey: string) {\r\n this.csrfTokenKey = newKey || DEFAULT_CSRF_TOKEN_KEY;\r\n }\r\n\r\n public setErrorMessageRetriever(getErrorMsg: ErrorMessageRetriever) {\r\n this.retrieveErrorMessage = getErrorMsg;\r\n }\r\n\r\n protected async setupSecurityToken(): Promise<[string, string | undefined]> {\r\n if (!this.csrfToken) {\r\n this.csrfToken = await this.fetchSecurityToken();\r\n }\r\n return [this.csrfTokenKey, this.csrfToken];\r\n }\r\n\r\n protected async fetchSecurityToken(): Promise<string | undefined> {\r\n const fetchUrl = this.baseOptions!.csrfTokenFetchUrl!;\r\n const response = await this.get(fetchUrl, this.addHeaderToRequestConfig({ [this.csrfTokenKey]: \"Fetch\" }));\r\n\r\n return response.headers[this.csrfTokenKey];\r\n }\r\n\r\n /**\r\n * Follows the template pattern.\r\n *\r\n * @param method\r\n * @param url\r\n * @param data\r\n * @param requestConfig\r\n * @private\r\n */\r\n private async sendRequest<ResponseModel>(\r\n method: HttpMethods,\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n // noinspection SuspiciousTypeOfGuard\r\n if (typeof url !== \"string\") {\r\n throw new Error(FAILURE_MISSING_URL);\r\n }\r\n\r\n let config = this.bigNumbersAsString ? this.addHeaderToRequestConfig(BIG_NUMBERS_CONFIG) : requestConfig;\r\n\r\n // setup automatic CSRF token handling\r\n if (this.baseOptions.useCsrfProtection && EDIT_METHODS.includes(method)) {\r\n const [tokenKey, tokenValue] = await this.setupSecurityToken();\r\n if (tokenValue) {\r\n config = this.addHeaderToRequestConfig({ [tokenKey]: tokenValue }, requestConfig);\r\n }\r\n }\r\n\r\n try {\r\n return await this.executeRequest<ResponseModel>(method, url, data, config);\r\n } catch (e) {\r\n const clientError = e as ODataClientError;\r\n\r\n // automatic CSRF token handling\r\n if (\r\n !!this.baseOptions.useCsrfProtection &&\r\n clientError.status === 403 &&\r\n !!clientError.headers &&\r\n clientError.headers[\"x-csrf-token\"] === \"Required\"\r\n ) {\r\n // token has expired: reset csrf token & perform the original request again\r\n this.csrfToken = undefined;\r\n return this.sendRequest<ResponseModel>(method, url, data, requestConfig);\r\n }\r\n\r\n throw e;\r\n }\r\n }\r\n\r\n public get<ResponseModel>(url: string, requestConfig?: RequestConfigType): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Get, url, undefined, requestConfig);\r\n }\r\n public post<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Post, url, data, requestConfig);\r\n }\r\n public put<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Put, url, data, requestConfig);\r\n }\r\n public patch<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(HttpMethods.Patch, url, data, requestConfig);\r\n }\r\n public merge<ResponseModel>(\r\n url: string,\r\n data: any,\r\n requestConfig?: RequestConfigType\r\n ): Promise<HttpResponseModel<ResponseModel>> {\r\n return this.sendRequest<ResponseModel>(\r\n HttpMethods.Post,\r\n url,\r\n data,\r\n this.addHeaderToRequestConfig({ \"X-Http-Method\": \"MERGE\" }, requestConfig)\r\n );\r\n }\r\n public delete(url: string, requestConfig?: RequestConfigType): Promise<HttpResponseModel<void>> {\r\n return this.sendRequest<void>(HttpMethods.Delete, url, undefined, requestConfig);\r\n }\r\n\r\n public retrieveBigNumbersAsString(enabled: boolean) {\r\n this.bigNumbersAsString = enabled;\r\n }\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odata2ts/http-client-base",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -29,7 +29,7 @@
29
29
  "odata2ts"
30
30
  ],
31
31
  "dependencies": {
32
- "@odata2ts/http-client-api": "^0.2.0"
32
+ "@odata2ts/http-client-api": "^0.3.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@odata2ts/odata-core": "^0.3.7",
@@ -42,5 +42,5 @@
42
42
  "typescript": "5.0.4"
43
43
  },
44
44
  "types": "./lib/index.d.ts",
45
- "gitHead": "485559dce9388b480c7d0b3ded2fe2f3f5f98fad"
45
+ "gitHead": "9babd2df3010673249e7e3fa26705a8ba84f2cef"
46
46
  }