rest-client-sdk 7.4.0 → 7.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/README.md CHANGED
@@ -106,6 +106,27 @@ browser environment and
106
106
  [React Native AsyncStorage](https://facebook.github.io/react-native/docs/asyncstorage.html)
107
107
  for React Native.
108
108
 
109
+ ##### Authorization code flow with PKCE
110
+
111
+ `AuthorizationCodeFlowTokenGenerator` supports [PKCE](https://datatracker.ietf.org/doc/html/rfc7636). The SDK does not generate the `code_verifier` nor the `code_challenge`: generate them before redirecting to the authorize endpoint, then pass the verifier with the received `code`. `clientSecret` is optional, omit it for a public client.
112
+
113
+ ```js
114
+ import {
115
+ AuthorizationCodeFlowTokenGenerator,
116
+ TokenStorage,
117
+ } from 'rest-client-sdk';
118
+
119
+ const tokenGenerator = new AuthorizationCodeFlowTokenGenerator({
120
+ path: 'oauth.me/token',
121
+ scheme: 'https',
122
+ clientId: 'my-client-id',
123
+ redirectUri: 'https://my.app/callback',
124
+ });
125
+ const tokenStorage = new TokenStorage(tokenGenerator, storage);
126
+
127
+ await tokenStorage.generateToken({ code, codeVerifier });
128
+ ```
129
+
109
130
  #### Configure the SDK
110
131
 
111
132
  ```js
@@ -122,6 +143,7 @@ const config = {
122
143
  onRefreshTokenFailure: () => {
123
144
  // do something when the refresh token fails
124
145
  },
146
+ fetch: myFetch, // optional, defaults to the global `fetch`
125
147
  }; // path and scheme are mandatory
126
148
 
127
149
  const sdk = new RestClientSdk(tokenStorage, config, mapping);
@@ -135,6 +157,26 @@ import RestClientSdk, { Token } from 'rest-client-sdk';
135
157
  const sdk = new RestClientSdk<TSMetadata>(tokenStorage, config, mapping);
136
158
  ```
137
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
+
138
180
  #### UnitOfWork
139
181
 
140
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
@@ -1090,7 +1090,7 @@
1090
1090
  url: input
1091
1091
  }, params));
1092
1092
  }
1093
- return fetch(input, params).then(function (response) {
1093
+ return this._fetch(input, params).then(function (response) {
1094
1094
  if (_this6.sdk.logger) {
1095
1095
  _this6.sdk.logger.logResponse(response, logId);
1096
1096
  }
@@ -1104,6 +1104,14 @@
1104
1104
  throw httpError;
1105
1105
  });
1106
1106
  }
1107
+ }, {
1108
+ key: "_fetch",
1109
+ value: function _fetch(input, init) {
1110
+ var _this$sdk$config$fetc;
1111
+ // eslint-disable-next-line no-restricted-globals -- default for the injectable fetch
1112
+ var fetchFn = (_this$sdk$config$fetc = this.sdk.config.fetch) !== null && _this$sdk$config$fetc !== void 0 ? _this$sdk$config$fetc : fetch;
1113
+ return fetchFn(input, init);
1114
+ }
1107
1115
  }, {
1108
1116
  key: "_getEntityIdentifier",
1109
1117
  value: function _getEntityIdentifier(object) {
@@ -1867,6 +1875,7 @@
1867
1875
  }]);
1868
1876
  }();
1869
1877
 
1878
+ // accepts any config type, but a `fetch` key must be a fetch function when there is one
1870
1879
  var AbstractTokenGenerator = /*#__PURE__*/function () {
1871
1880
  function AbstractTokenGenerator(tokenGeneratorConfig, logger) {
1872
1881
  _classCallCheck(this, AbstractTokenGenerator);
@@ -1881,9 +1890,20 @@
1881
1890
  }
1882
1891
  }
1883
1892
  return _createClass(AbstractTokenGenerator, [{
1893
+ key: "_fetch",
1894
+ value: function _fetch(input, init) {
1895
+ var _config$fetch;
1896
+ // WithOptionalFetch guarantees this type, but TypeScript cannot read a key from a conditional constraint
1897
+ var config = this.tokenGeneratorConfig;
1898
+ // eslint-disable-next-line no-restricted-globals -- default for the injectable fetch
1899
+ var fetchFn = (_config$fetch = config === null || config === void 0 ? void 0 : config.fetch) !== null && _config$fetch !== void 0 ? _config$fetch : fetch;
1900
+ return fetchFn(input, init);
1901
+ }
1902
+
1903
+ /** @deprecated */
1904
+ }, {
1884
1905
  key: "_manageOauthError",
1885
- value: /** @deprecated */
1886
- function _manageOauthError(response) {
1906
+ value: function _manageOauthError(response) {
1887
1907
  return response.clone().json().then(function (body) {
1888
1908
  if (body.error === 'invalid_grant') {
1889
1909
  throw new InvalidGrantError(body.error, getHttpErrorFromResponse(response));
@@ -1936,21 +1956,22 @@
1936
1956
  headers: {
1937
1957
  'Content-Type': 'application/x-www-form-urlencoded'
1938
1958
  },
1939
- body: new URLSearchParams({
1959
+ body: new URLSearchParams(_objectSpread2(_objectSpread2({
1940
1960
  grant_type: 'authorization_code',
1941
- client_id: this.tokenGeneratorConfig.clientId,
1942
- client_secret: this.tokenGeneratorConfig.clientSecret,
1943
- // TODO : secure this ?
1961
+ client_id: this.tokenGeneratorConfig.clientId
1962
+ }, this._clientSecretParameter()), {}, {
1944
1963
  redirect_uri: this.tokenGeneratorConfig.redirectUri,
1945
1964
  code: parameters.code
1946
- }).toString()
1965
+ }, parameters.codeVerifier ? {
1966
+ code_verifier: parameters.codeVerifier
1967
+ } : {})).toString()
1947
1968
  };
1948
1969
  if (this.logger) {
1949
1970
  this.logger.logRequest(_objectSpread2({
1950
1971
  url: url
1951
1972
  }, params));
1952
1973
  }
1953
- return fetch(url, params);
1974
+ return this._fetch(url, params);
1954
1975
  }
1955
1976
 
1956
1977
  /**
@@ -1972,19 +1993,19 @@
1972
1993
  headers: {
1973
1994
  'Content-Type': 'application/x-www-form-urlencoded'
1974
1995
  },
1975
- body: new URLSearchParams({
1996
+ body: new URLSearchParams(_objectSpread2(_objectSpread2({
1976
1997
  grant_type: 'refresh_token',
1977
- client_id: this.tokenGeneratorConfig.clientId,
1978
- client_secret: this.tokenGeneratorConfig.clientSecret,
1998
+ client_id: this.tokenGeneratorConfig.clientId
1999
+ }, this._clientSecretParameter()), {}, {
1979
2000
  refresh_token: oldAccessToken.refresh_token
1980
- }).toString()
2001
+ })).toString()
1981
2002
  };
1982
2003
  if (this.logger) {
1983
2004
  this.logger.logRequest(_objectSpread2({
1984
2005
  url: url
1985
2006
  }, params));
1986
2007
  }
1987
- return fetch(url, params);
2008
+ return this._fetch(url, params);
1988
2009
  }
1989
2010
  }, {
1990
2011
  key: "checkTokenGeneratorConfig",
@@ -1995,13 +2016,21 @@
1995
2016
  if (!(config.path && config.scheme)) {
1996
2017
  throw new RangeError('TokenGenerator config is not valid, it should contain a "path", a "scheme" parameter');
1997
2018
  }
1998
- if (!(config.clientId && config.clientSecret)) {
1999
- throw new RangeError('TokenGenerator config is not valid, it should contain a "clientId", a "clientSecret" parameter');
2019
+ if (!config.clientId) {
2020
+ throw new RangeError('TokenGenerator config is not valid, it should contain a "clientId" parameter');
2000
2021
  }
2001
2022
  if (!config.redirectUri) {
2002
2023
  throw new RangeError('TokenGenerator config is not valid, it should contain a "redirectUri" parameter');
2003
2024
  }
2004
2025
  }
2026
+ }, {
2027
+ key: "_clientSecretParameter",
2028
+ value: function _clientSecretParameter() {
2029
+ var clientSecret = this.tokenGeneratorConfig.clientSecret;
2030
+ return clientSecret ? {
2031
+ client_secret: clientSecret
2032
+ } : {};
2033
+ }
2005
2034
  }, {
2006
2035
  key: "_checkGenerateParameters",
2007
2036
  value: function _checkGenerateParameters(parameters) {
@@ -2089,7 +2118,7 @@
2089
2118
  url: url
2090
2119
  }, params));
2091
2120
  }
2092
- return fetch(url, params);
2121
+ return this._fetch(url, params);
2093
2122
  }
2094
2123
  }, {
2095
2124
  key: "refreshToken",
@@ -2154,7 +2183,7 @@
2154
2183
  url: url
2155
2184
  }, params));
2156
2185
  }
2157
- return fetch(url, params);
2186
+ return this._fetch(url, params);
2158
2187
  }
2159
2188
  }, {
2160
2189
  key: "refreshToken",
@@ -2185,7 +2214,7 @@
2185
2214
  url: url
2186
2215
  }, params));
2187
2216
  }
2188
- return fetch(url, params);
2217
+ return this._fetch(url, params);
2189
2218
  }
2190
2219
  }, {
2191
2220
  key: "checkTokenGeneratorConfig",