rest-client-sdk 7.5.0 → 7.6.1

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/README.md CHANGED
@@ -143,6 +143,7 @@ const config = {
143
143
  onRefreshTokenFailure: () => {
144
144
  // do something when the refresh token fails
145
145
  },
146
+ fetch: myFetch, // optional, defaults to the global `fetch`
146
147
  }; // path and scheme are mandatory
147
148
 
148
149
  const sdk = new RestClientSdk(tokenStorage, config, mapping);
@@ -156,6 +157,26 @@ import RestClientSdk, { Token } from 'rest-client-sdk';
156
157
  const sdk = new RestClientSdk<TSMetadata>(tokenStorage, config, mapping);
157
158
  ```
158
159
 
160
+ #### Custom fetch
161
+
162
+ By default, the SDK calls the global `fetch`. Pass a `fetch` key with the same signature to use your own implementation, for example in React Native, where you may want a `fetch` that does not rely on JS timers, which Android pauses when the app goes to the background. The `RestClientSdk` config covers the repository calls, and each token generator takes its own `fetch` key for the token calls:
163
+
164
+ ```js
165
+ const tokenGenerator = new PasswordGenerator({
166
+ path: 'oauth.me',
167
+ scheme: 'https',
168
+ clientId: 'my-client-id',
169
+ clientSecret: 'my-client-secret',
170
+ fetch: myFetch,
171
+ });
172
+
173
+ const sdk = new RestClientSdk(
174
+ new TokenStorage(tokenGenerator, storage),
175
+ { path: 'api.me', scheme: 'https', fetch: myFetch },
176
+ mapping
177
+ );
178
+ ```
179
+
159
180
  #### UnitOfWork
160
181
 
161
182
  Adding the key `unitOfWorkEnabled` to the `config` object passed to the `RestClientSdk` constructor will enable or disable the UnitOfWork
package/dist/index.cjs CHANGED
@@ -1063,7 +1063,11 @@
1063
1063
  var _this6 = this;
1064
1064
  var params = requestParams;
1065
1065
  var baseHeaders = {};
1066
- if (params.method !== 'GET' || params.method !== 'GET' && params.method !== 'DELETE') {
1066
+
1067
+ // a FormData body must let fetch set the Content-Type itself,
1068
+ // with the multipart/form-data boundary
1069
+ var isFormDataBody = FormData !== undefined && params.body instanceof FormData;
1070
+ if ((params.method !== 'GET' || params.method !== 'GET' && params.method !== 'DELETE') && !isFormDataBody) {
1067
1071
  baseHeaders = {
1068
1072
  'Content-Type': 'application/json'
1069
1073
  };
@@ -1090,7 +1094,7 @@
1090
1094
  url: input
1091
1095
  }, params));
1092
1096
  }
1093
- return fetch(input, params).then(function (response) {
1097
+ return this._fetch(input, params).then(function (response) {
1094
1098
  if (_this6.sdk.logger) {
1095
1099
  _this6.sdk.logger.logResponse(response, logId);
1096
1100
  }
@@ -1104,6 +1108,14 @@
1104
1108
  throw httpError;
1105
1109
  });
1106
1110
  }
1111
+ }, {
1112
+ key: "_fetch",
1113
+ value: function _fetch(input, init) {
1114
+ var _this$sdk$config$fetc;
1115
+ // eslint-disable-next-line no-restricted-globals -- default for the injectable fetch
1116
+ var fetchFn = (_this$sdk$config$fetc = this.sdk.config.fetch) !== null && _this$sdk$config$fetc !== void 0 ? _this$sdk$config$fetc : fetch;
1117
+ return fetchFn(input, init);
1118
+ }
1107
1119
  }, {
1108
1120
  key: "_getEntityIdentifier",
1109
1121
  value: function _getEntityIdentifier(object) {
@@ -1867,6 +1879,7 @@
1867
1879
  }]);
1868
1880
  }();
1869
1881
 
1882
+ // accepts any config type, but a `fetch` key must be a fetch function when there is one
1870
1883
  var AbstractTokenGenerator = /*#__PURE__*/function () {
1871
1884
  function AbstractTokenGenerator(tokenGeneratorConfig, logger) {
1872
1885
  _classCallCheck(this, AbstractTokenGenerator);
@@ -1881,9 +1894,20 @@
1881
1894
  }
1882
1895
  }
1883
1896
  return _createClass(AbstractTokenGenerator, [{
1897
+ key: "_fetch",
1898
+ value: function _fetch(input, init) {
1899
+ var _config$fetch;
1900
+ // WithOptionalFetch guarantees this type, but TypeScript cannot read a key from a conditional constraint
1901
+ var config = this.tokenGeneratorConfig;
1902
+ // eslint-disable-next-line no-restricted-globals -- default for the injectable fetch
1903
+ var fetchFn = (_config$fetch = config === null || config === void 0 ? void 0 : config.fetch) !== null && _config$fetch !== void 0 ? _config$fetch : fetch;
1904
+ return fetchFn(input, init);
1905
+ }
1906
+
1907
+ /** @deprecated */
1908
+ }, {
1884
1909
  key: "_manageOauthError",
1885
- value: /** @deprecated */
1886
- function _manageOauthError(response) {
1910
+ value: function _manageOauthError(response) {
1887
1911
  return response.clone().json().then(function (body) {
1888
1912
  if (body.error === 'invalid_grant') {
1889
1913
  throw new InvalidGrantError(body.error, getHttpErrorFromResponse(response));
@@ -1951,7 +1975,7 @@
1951
1975
  url: url
1952
1976
  }, params));
1953
1977
  }
1954
- return fetch(url, params);
1978
+ return this._fetch(url, params);
1955
1979
  }
1956
1980
 
1957
1981
  /**
@@ -1985,7 +2009,7 @@
1985
2009
  url: url
1986
2010
  }, params));
1987
2011
  }
1988
- return fetch(url, params);
2012
+ return this._fetch(url, params);
1989
2013
  }
1990
2014
  }, {
1991
2015
  key: "checkTokenGeneratorConfig",
@@ -2098,7 +2122,7 @@
2098
2122
  url: url
2099
2123
  }, params));
2100
2124
  }
2101
- return fetch(url, params);
2125
+ return this._fetch(url, params);
2102
2126
  }
2103
2127
  }, {
2104
2128
  key: "refreshToken",
@@ -2163,7 +2187,7 @@
2163
2187
  url: url
2164
2188
  }, params));
2165
2189
  }
2166
- return fetch(url, params);
2190
+ return this._fetch(url, params);
2167
2191
  }
2168
2192
  }, {
2169
2193
  key: "refreshToken",
@@ -2194,7 +2218,7 @@
2194
2218
  url: url
2195
2219
  }, params));
2196
2220
  }
2197
- return fetch(url, params);
2221
+ return this._fetch(url, params);
2198
2222
  }
2199
2223
  }, {
2200
2224
  key: "checkTokenGeneratorConfig",