@dereekb/calcom 13.32.0 → 13.34.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/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FetchResponseError, FetchRequestFactoryError, rateLimitedFetchHandler, fetchJsonFunction, returnNullHandleFetchJsonParseErrorFunction, fetchApiFetchService, makeUrlSearchParams } from '@dereekb/util/fetch';
2
- import { MS_IN_SECOND, resetPeriodPromiseRateLimiter, MS_IN_MINUTE } from '@dereekb/util';
2
+ import { MS_IN_SECOND, resetPeriodPromiseRateLimiter, MS_IN_DAY, MS_IN_MINUTE } from '@dereekb/util';
3
3
  import { BaseError } from 'make-error';
4
4
 
5
5
  function _assert_this_initialized$1(self) {
@@ -1336,10 +1336,7 @@ function _ts_generator$1(thisArg, body) {
1336
1336
  };
1337
1337
  // MARK: Make User Context
1338
1338
  var makeUserContext = function makeUserContext(input) {
1339
- var userAccessTokenFactory = oauthContext.makeUserAccessTokenFactory({
1340
- refreshToken: input.refreshToken,
1341
- userAccessTokenCache: input.accessTokenCache
1342
- });
1339
+ var userAccessTokenFactory = oauthContext.makeAccessTokenFactory(input);
1343
1340
  var userAccessTokenStringFactory = calcomAccessTokenStringFactory(userAccessTokenFactory);
1344
1341
  var userBaseFetch = fetchFactory({
1345
1342
  calcomAccessTokenStringFactory: userAccessTokenStringFactory
@@ -1814,6 +1811,62 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
1814
1811
  };
1815
1812
  }
1816
1813
 
1814
+ /**
1815
+ * The Cal.com OAuth API base URL.
1816
+ *
1817
+ * Endpoint paths are appended to this base, so it intentionally carries no endpoint segment of
1818
+ * its own. This is the single place the OAuth host and prefix are encoded.
1819
+ */ var CALCOM_OAUTH_API_URL = 'https://api.cal.com/v2/auth/oauth2';
1820
+ /**
1821
+ * The Cal.com OAuth token endpoint path, relative to {@link CALCOM_OAUTH_API_URL}.
1822
+ */ var CALCOM_OAUTH_TOKEN_PATH = '/token';
1823
+ /**
1824
+ * The Cal.com OAuth authorize URL.
1825
+ */ var CALCOM_OAUTH_AUTHORIZE_URL = 'https://app.cal.com/auth/oauth2/authorize';
1826
+ /**
1827
+ * Returns whether the credential is a {@link CalcomApiKeyCredential}.
1828
+ *
1829
+ * @param credential - The credential to check.
1830
+ * @returns True when the credential carries an api key.
1831
+ *
1832
+ * @__NO_SIDE_EFFECTS__
1833
+ */ function isCalcomApiKeyCredential(credential) {
1834
+ return 'apiKey' in credential;
1835
+ }
1836
+ /**
1837
+ * Builds a {@link CalcomAuthCredential} from flat, optional values, as an environment-facing
1838
+ * configuration provides them.
1839
+ *
1840
+ * An api key wins when both are present: it does not expire, so it skips the refresh loop entirely.
1841
+ * The cache attaches only to the refresh-token arm, since an api key has no token to cache. Empty
1842
+ * strings count as absent, so an unset environment variable read as `''` does not become a
1843
+ * credential that sends `Bearer `.
1844
+ *
1845
+ * @param values - The flat credential values.
1846
+ * @returns The equivalent credential, or undefined when neither value is present.
1847
+ *
1848
+ * @__NO_SIDE_EFFECTS__
1849
+ */ function calcomAuthCredentialFromValues(values) {
1850
+ var apiKey = values.apiKey, refreshToken = values.refreshToken, accessTokenCache = values.accessTokenCache;
1851
+ var result;
1852
+ if (apiKey) {
1853
+ result = {
1854
+ apiKey: apiKey
1855
+ };
1856
+ } else if (refreshToken) {
1857
+ result = {
1858
+ refreshToken: refreshToken,
1859
+ accessTokenCache: accessTokenCache
1860
+ };
1861
+ }
1862
+ return result;
1863
+ }
1864
+ // COMPAT: Deprecated aliases
1865
+ /**
1866
+ * @deprecated use {@link CALCOM_OAUTH_API_URL} instead. This was previously used as the fetch base
1867
+ * URL while the endpoint path `/oauth/token` was also appended, resolving to a doubly-pathed URL.
1868
+ */ var CALCOM_OAUTH_TOKEN_URL = "".concat(CALCOM_OAUTH_API_URL).concat(CALCOM_OAUTH_TOKEN_PATH);
1869
+
1817
1870
  /**
1818
1871
  * Refreshes an access token using a refresh token. Cal.com rotates refresh tokens
1819
1872
  * on every use, so the new `refresh_token` from the response must be persisted.
@@ -1821,7 +1874,7 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
1821
1874
  * Cal.com uses JSON body (not Basic Auth) for token requests.
1822
1875
  *
1823
1876
  * @param context - The Cal.com OAuth context providing client credentials and fetch capabilities.
1824
- * @returns Refreshes an access token using an optional refresh token override.
1877
+ * @returns Refreshes an access token using the given refresh token.
1825
1878
  *
1826
1879
  * @see https://cal.com/docs/api-reference/v2/oauth/refresh-an-existing-access-token
1827
1880
  *
@@ -1831,19 +1884,18 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
1831
1884
  * console.log(response.access_token, response.refresh_token);
1832
1885
  * ```
1833
1886
  */ function refreshAccessToken(context) {
1887
+ var client = context.config.client;
1834
1888
  return function(input) {
1835
- var _ref;
1836
- var refreshToken = (_ref = input === null || input === void 0 ? void 0 : input.refreshToken) !== null && _ref !== void 0 ? _ref : context.config.refreshToken;
1837
1889
  var fetchJsonInput = {
1838
1890
  method: 'POST',
1839
1891
  body: JSON.stringify({
1840
1892
  grant_type: 'refresh_token',
1841
- client_id: context.config.clientId,
1842
- client_secret: context.config.clientSecret,
1843
- refresh_token: refreshToken
1893
+ client_id: client === null || client === void 0 ? void 0 : client.clientId,
1894
+ client_secret: client === null || client === void 0 ? void 0 : client.clientSecret,
1895
+ refresh_token: input.refreshToken
1844
1896
  })
1845
1897
  };
1846
- return context.fetchJson('/oauth/token', fetchJsonInput);
1898
+ return context.fetchJson(CALCOM_OAUTH_TOKEN_PATH, fetchJsonInput);
1847
1899
  };
1848
1900
  }
1849
1901
  /**
@@ -1866,27 +1918,102 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
1866
1918
  * console.log(response.access_token, response.refresh_token);
1867
1919
  * ```
1868
1920
  */ function exchangeAuthorizationCode(context) {
1921
+ var client = context.config.client;
1869
1922
  return function(input) {
1870
1923
  var fetchJsonInput = {
1871
1924
  method: 'POST',
1872
1925
  body: JSON.stringify({
1873
1926
  grant_type: 'authorization_code',
1874
- client_id: context.config.clientId,
1875
- client_secret: context.config.clientSecret,
1927
+ client_id: client === null || client === void 0 ? void 0 : client.clientId,
1928
+ client_secret: client === null || client === void 0 ? void 0 : client.clientSecret,
1876
1929
  code: input.code,
1877
1930
  redirect_uri: input.redirectUri
1878
1931
  })
1879
1932
  };
1880
- return context.fetchJson('/oauth/token', fetchJsonInput);
1933
+ return context.fetchJson(CALCOM_OAUTH_TOKEN_PATH, fetchJsonInput);
1881
1934
  };
1882
1935
  }
1883
1936
 
1884
1937
  /**
1885
- * The Cal.com OAuth token endpoint URL.
1886
- */ var CALCOM_OAUTH_TOKEN_URL = 'https://api.cal.com/v2/oauth/token';
1938
+ * Every granular Cal.com OAuth scope.
1939
+ *
1940
+ * A runtime list rather than a bare type union, so a configured scope can be validated instead of
1941
+ * being passed through to the consent screen and refused there.
1942
+ *
1943
+ * @see https://cal.com/docs/api-reference/v2/oauth
1944
+ */ var ALL_CALCOM_OAUTH_SCOPES = [
1945
+ 'PROFILE_READ',
1946
+ 'PROFILE_WRITE',
1947
+ 'BOOKING_READ',
1948
+ 'BOOKING_WRITE',
1949
+ 'SCHEDULE_READ',
1950
+ 'SCHEDULE_WRITE',
1951
+ 'EVENT_TYPE_READ',
1952
+ 'EVENT_TYPE_WRITE',
1953
+ 'APPS_READ',
1954
+ 'APPS_WRITE',
1955
+ 'WEBHOOK_READ',
1956
+ 'WEBHOOK_WRITE'
1957
+ ];
1887
1958
  /**
1888
- * The Cal.com OAuth authorize URL.
1889
- */ var CALCOM_OAUTH_AUTHORIZE_URL = 'https://app.cal.com/auth/oauth2/authorize';
1959
+ * Returns whether the input is a known {@link CalcomOAuthScope}.
1960
+ *
1961
+ * @param value - The value to check.
1962
+ * @returns True when the value is a known Cal.com OAuth scope.
1963
+ */ function isCalcomOAuthScope(value) {
1964
+ return ALL_CALCOM_OAUTH_SCOPES.includes(value);
1965
+ }
1966
+ /**
1967
+ * The delimiter used to join scopes in the `scope` query parameter.
1968
+ *
1969
+ * OAuth2 specifies a space-delimited list. Cal.com's granular scopes are documented without an
1970
+ * explicit delimiter, so this is isolated here: if the consent screen rejects the `scope`
1971
+ * parameter, this is the only value that needs to change.
1972
+ */ var CALCOM_OAUTH_SCOPE_DELIMITER = ' ';
1973
+ /**
1974
+ * The `response_type` used by the authorization-code flow.
1975
+ */ var CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE = 'code';
1976
+ /**
1977
+ * Creates a {@link CalcomOAuthAuthorizeUrlFactory} that composes the Cal.com authorize URL that a
1978
+ * user's browser is redirected to in order to begin the authorization-code flow.
1979
+ *
1980
+ * The client id, redirect URI, and scopes are fixed by the config, since a consumer holds those
1981
+ * constant and varies only the per-request `state`.
1982
+ *
1983
+ * @param config - The client id, redirect URI, and scopes to request.
1984
+ * @returns A factory that builds an authorize URL for the given params.
1985
+ *
1986
+ * @see https://cal.com/docs/api-reference/v2/oauth
1987
+ *
1988
+ * @example
1989
+ * ```ts
1990
+ * const authorizeUrlFactory = calcomOAuthAuthorizeUrlFactory({
1991
+ * clientId: 'client-id',
1992
+ * redirectUri: 'http://localhost:9901/oauth/calcom/callback',
1993
+ * scopes: ['PROFILE_READ', 'BOOKING_READ']
1994
+ * });
1995
+ *
1996
+ * const url = authorizeUrlFactory({ state: 'signed-state' });
1997
+ * ```
1998
+ *
1999
+ * @__NO_SIDE_EFFECTS__
2000
+ */ function calcomOAuthAuthorizeUrlFactory(config) {
2001
+ var clientId = config.clientId, redirectUri = config.redirectUri, scopes = config.scopes, inputAuthorizeUrl = config.authorizeUrl;
2002
+ var authorizeUrl = inputAuthorizeUrl !== null && inputAuthorizeUrl !== void 0 ? inputAuthorizeUrl : CALCOM_OAUTH_AUTHORIZE_URL;
2003
+ var scope = scopes.join(CALCOM_OAUTH_SCOPE_DELIMITER);
2004
+ return function(params) {
2005
+ var url = new URL(authorizeUrl);
2006
+ var state = params === null || params === void 0 ? void 0 : params.state;
2007
+ url.searchParams.set('client_id', clientId);
2008
+ url.searchParams.set('redirect_uri', redirectUri);
2009
+ url.searchParams.set('response_type', CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE);
2010
+ url.searchParams.set('scope', scope);
2011
+ if (state != null) {
2012
+ url.searchParams.set('state', state);
2013
+ }
2014
+ return url.toString();
2015
+ };
2016
+ }
1890
2017
 
1891
2018
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1892
2019
  try {
@@ -2016,6 +2143,54 @@ function _ts_generator(thisArg, body) {
2016
2143
  };
2017
2144
  }
2018
2145
  }
2146
+ /**
2147
+ * Maps a {@link CalcomOAuthTokenResponse} to a {@link CalcomAccessToken}.
2148
+ *
2149
+ * Pure: the caller owns the rotated refresh token that comes back on the result, so each token
2150
+ * scope (server-level vs per-user) tracks its own rotation instead of sharing one variable.
2151
+ *
2152
+ * @param response - The token response returned by the Cal.com token endpoint.
2153
+ * @returns The equivalent CalcomAccessToken, with `expiresAt` resolved against the current time.
2154
+ *
2155
+ * @__NO_SIDE_EFFECTS__
2156
+ */ function calcomAccessTokenFromTokenResponse(response) {
2157
+ var createdAt = Date.now();
2158
+ var access_token = response.access_token, refresh_token = response.refresh_token, scope = response.scope, expires_in = response.expires_in;
2159
+ var accessToken = {
2160
+ accessToken: access_token,
2161
+ refreshToken: refresh_token,
2162
+ expiresIn: expires_in,
2163
+ expiresAt: new Date(createdAt + expires_in * MS_IN_SECOND),
2164
+ scope: scope !== null && scope !== void 0 ? scope : ''
2165
+ };
2166
+ return accessToken;
2167
+ }
2168
+ /**
2169
+ * The lifetime given to the synthetic access token an api key is wrapped in.
2170
+ *
2171
+ * Cal.com api keys do not expire; the value only has to outlive any process holding one, so the
2172
+ * token satisfies the same expiration check every other token goes through.
2173
+ */ var CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION = MS_IN_DAY * 365 * 100;
2174
+ /**
2175
+ * Wraps a {@link CalcomApiKey} as a static {@link CalcomAccessToken}.
2176
+ *
2177
+ * An api key is already a bearer token acting as the user who created it, so there is nothing to
2178
+ * exchange and nothing to refresh.
2179
+ *
2180
+ * @param apiKey - The Cal.com api key.
2181
+ * @returns The equivalent static CalcomAccessToken.
2182
+ *
2183
+ * @__NO_SIDE_EFFECTS__
2184
+ */ function calcomAccessTokenFromApiKey(apiKey) {
2185
+ var accessToken = {
2186
+ accessToken: apiKey,
2187
+ refreshToken: '',
2188
+ expiresIn: Number.MAX_SAFE_INTEGER,
2189
+ expiresAt: new Date(Date.now() + CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION),
2190
+ scope: ''
2191
+ };
2192
+ return accessToken;
2193
+ }
2019
2194
  /**
2020
2195
  * Creates a {@link CalcomOAuthFactory} that produces configured Cal.com OAuth instances.
2021
2196
  * Supports both API key authentication (static token, no refresh) and full OAuth
@@ -2026,10 +2201,11 @@ function _ts_generator(thisArg, body) {
2026
2201
  *
2027
2202
  * @__NO_SIDE_EFFECTS__
2028
2203
  */ function calcomOAuthFactory(factoryConfig) {
2029
- var fetchHandler = calcomRateLimitedFetchHandler();
2204
+ var _factoryConfig_fetchHandler;
2205
+ var fetchHandler = (_factoryConfig_fetchHandler = factoryConfig.fetchHandler) !== null && _factoryConfig_fetchHandler !== void 0 ? _factoryConfig_fetchHandler : calcomRateLimitedFetchHandler();
2030
2206
  var logCalcomServerErrorFunction = factoryConfig.logCalcomServerErrorFunction, _factoryConfig_fetchFactory = factoryConfig.fetchFactory, fetchFactory = _factoryConfig_fetchFactory === void 0 ? function(_) {
2031
2207
  return fetchApiFetchService.makeFetch({
2032
- baseUrl: CALCOM_OAUTH_TOKEN_URL,
2208
+ baseUrl: CALCOM_OAUTH_API_URL,
2033
2209
  baseRequest: {
2034
2210
  headers: {
2035
2211
  'Content-Type': 'application/json'
@@ -2042,138 +2218,100 @@ function _ts_generator(thisArg, body) {
2042
2218
  });
2043
2219
  } : _factoryConfig_fetchFactory;
2044
2220
  return function(config) {
2045
- var accessTokenFromTokenResponse = function accessTokenFromTokenResponse(result) {
2046
- var createdAt = Date.now();
2047
- var access_token = result.access_token, refresh_token = result.refresh_token, scope = result.scope, expires_in = result.expires_in;
2048
- // Store the new refresh token for next use
2049
- latestRefreshToken = refresh_token;
2050
- var accessToken = {
2051
- accessToken: access_token,
2052
- refreshToken: refresh_token,
2053
- expiresIn: expires_in,
2054
- expiresAt: new Date(createdAt + expires_in * MS_IN_SECOND),
2055
- scope: scope !== null && scope !== void 0 ? scope : ''
2056
- };
2057
- return accessToken;
2058
- };
2059
- var useApiKey = !!config.apiKey;
2060
- if (!useApiKey) {
2061
- if (!config.clientId) {
2062
- throw new Error('CalcomOAuthConfig missing clientId. Provide clientId+clientSecret for OAuth or apiKey for API key auth.');
2063
- } else if (!config.clientSecret) {
2064
- throw new Error('CalcomOAuthConfig missing clientSecret.');
2065
- }
2221
+ var defaultAuth = config.defaultAuth, client = config.client;
2222
+ var hasApiKeyDefault = defaultAuth != null && isCalcomApiKeyCredential(defaultAuth) && !!defaultAuth.apiKey;
2223
+ // an API key IS the token; every other credential is an exchange the token endpoint authenticates
2224
+ // with the client id and secret. With neither, no token could ever be produced
2225
+ if (!hasApiKeyDefault && client == null) {
2226
+ throw new Error('CalcomOAuthConfig can authenticate nothing. Provide `defaultAuth: { apiKey }` for ambient calls, `client` (clientId+clientSecret) to exchange any refresh token, or both.');
2066
2227
  }
2067
2228
  var baseFetch = fetchFactory();
2068
2229
  var fetch = handleCalcomOAuthErrorFetch(baseFetch, logCalcomServerErrorFunction);
2069
2230
  var fetchJson = fetchJsonFunction(fetch, {
2070
2231
  handleFetchJsonParseErrorFunction: returnNullHandleFetchJsonParseErrorFunction
2071
2232
  });
2072
- // MARK: API Key Auth (static token, no refresh)
2073
- if (useApiKey) {
2074
- var apiKeyToken = {
2075
- accessToken: config.apiKey,
2076
- refreshToken: '',
2077
- expiresIn: Number.MAX_SAFE_INTEGER,
2078
- expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 100),
2079
- scope: ''
2080
- };
2081
- var loadAccessToken = function loadAccessToken() {
2082
- return _async_to_generator(function() {
2083
- return _ts_generator(this, function(_state) {
2084
- return [
2085
- 2,
2086
- apiKeyToken
2087
- ];
2088
- });
2089
- })();
2090
- };
2091
- var makeUserAccessTokenFactory = function makeUserAccessTokenFactory() {
2092
- throw new Error('makeUserAccessTokenFactory is not available when using API key auth. Use OAuth for per-user contexts.');
2093
- };
2094
- var oauthContext = {
2095
- fetch: fetch,
2096
- fetchJson: fetchJson,
2097
- loadAccessToken: loadAccessToken,
2098
- makeUserAccessTokenFactory: makeUserAccessTokenFactory,
2099
- config: config
2100
- };
2101
- return {
2102
- oauthContext: oauthContext
2103
- };
2104
- }
2105
- // MARK: OAuth Auth (refresh token flow)
2106
- /**
2107
- * Tracks the latest refresh token since Cal.com rotates them.
2108
- */ var latestRefreshToken = config.refreshToken;
2109
- var tokenRefresher = function tokenRefresher() {
2110
- return _async_to_generator(function() {
2111
- var accessToken;
2112
- return _ts_generator(this, function(_state) {
2113
- switch(_state.label){
2114
- case 0:
2115
- return [
2116
- 4,
2117
- refreshAccessToken(oauthContext1)({
2118
- refreshToken: latestRefreshToken !== null && latestRefreshToken !== void 0 ? latestRefreshToken : undefined
2119
- })
2120
- ];
2121
- case 1:
2122
- accessToken = _state.sent();
2233
+ // MARK: Access Tokens
2234
+ var makeAccessTokenFactory = function makeAccessTokenFactory(credential) {
2235
+ var result;
2236
+ if (isCalcomApiKeyCredential(credential)) {
2237
+ var apiKey = credential.apiKey;
2238
+ // presence, not truthiness, is what discriminates the union — so without this an empty key
2239
+ // would be handed back as a valid static token and every call would send `Bearer `
2240
+ if (!apiKey) {
2241
+ throw new Error('CalcomApiKeyCredential.apiKey is empty.');
2242
+ }
2243
+ var apiKeyToken = calcomAccessTokenFromApiKey(apiKey);
2244
+ result = function result() {
2245
+ return _async_to_generator(function() {
2246
+ return _ts_generator(this, function(_state) {
2123
2247
  return [
2124
2248
  2,
2125
- accessTokenFromTokenResponse(accessToken)
2249
+ apiKeyToken
2126
2250
  ];
2127
- }
2251
+ });
2252
+ })();
2253
+ };
2254
+ } else {
2255
+ // a token for a specific grant can only come from that grant's refresh token, exchanged
2256
+ // against the OAuth client. An API key is a different user's identity and cannot stand in
2257
+ if (client == null) {
2258
+ throw new Error('makeAccessTokenFactory() requires a `client` (clientId+clientSecret) to exchange a refresh token credential. A Cal.com configuration with only an api key cannot create one.');
2259
+ }
2260
+ /**
2261
+ * Tracks THIS credential's rotated refresh token, since Cal.com rotates on every use.
2262
+ *
2263
+ * Declared per invocation, so every credential — the default one as much as any per-user one
2264
+ * — rotates in isolation and refreshing one never overwrites another's token.
2265
+ */ var latestRefreshToken = credential.refreshToken;
2266
+ var tokenRefresher = function tokenRefresher() {
2267
+ return _async_to_generator(function() {
2268
+ var response, accessToken;
2269
+ return _ts_generator(this, function(_state) {
2270
+ switch(_state.label){
2271
+ case 0:
2272
+ return [
2273
+ 4,
2274
+ refreshAccessToken(oauthContext)({
2275
+ refreshToken: latestRefreshToken
2276
+ })
2277
+ ];
2278
+ case 1:
2279
+ response = _state.sent();
2280
+ accessToken = calcomAccessTokenFromTokenResponse(response);
2281
+ latestRefreshToken = accessToken.refreshToken;
2282
+ return [
2283
+ 2,
2284
+ accessToken
2285
+ ];
2286
+ }
2287
+ });
2288
+ })();
2289
+ };
2290
+ result = calcomOAuthAccessTokenFactory({
2291
+ tokenRefresher: tokenRefresher,
2292
+ accessTokenCache: credential.accessTokenCache
2128
2293
  });
2129
- })();
2130
- };
2131
- var loadAccessToken1 = calcomOAuthAccessTokenFactory({
2132
- tokenRefresher: tokenRefresher,
2133
- accessTokenCache: config.accessTokenCache
2134
- });
2135
- // User Access Token
2136
- var makeUserAccessTokenFactory1 = function makeUserAccessTokenFactory(input) {
2137
- var userLatestRefreshToken = input.refreshToken;
2138
- var userTokenRefresher = function userTokenRefresher() {
2139
- return _async_to_generator(function() {
2140
- var tokenResponse, token;
2141
- return _ts_generator(this, function(_state) {
2142
- switch(_state.label){
2143
- case 0:
2144
- return [
2145
- 4,
2146
- refreshAccessToken(oauthContext1)({
2147
- refreshToken: userLatestRefreshToken
2148
- })
2149
- ];
2150
- case 1:
2151
- tokenResponse = _state.sent();
2152
- token = accessTokenFromTokenResponse(tokenResponse);
2153
- // Track the rotated refresh token for this user
2154
- userLatestRefreshToken = token.refreshToken;
2155
- return [
2156
- 2,
2157
- token
2158
- ];
2159
- }
2160
- });
2161
- })();
2162
- };
2163
- return calcomOAuthAccessTokenFactory({
2164
- tokenRefresher: userTokenRefresher,
2165
- accessTokenCache: input.userAccessTokenCache
2166
- });
2294
+ }
2295
+ return result;
2167
2296
  };
2168
- var oauthContext1 = {
2297
+ // built once, so the default credential's in-memory tier and its rotation are shared across the
2298
+ // whole context instead of restarting on every call
2299
+ var loadAccessToken = defaultAuth == null ? function() {
2300
+ return _async_to_generator(function() {
2301
+ return _ts_generator(this, function(_state) {
2302
+ throw new CalcomOAuthAuthFailureError('No `defaultAuth` is configured on this CalcomOAuthConfig, so there is no ambient credential to authenticate with. Use makeAccessTokenFactory(credential) for a named credential.');
2303
+ });
2304
+ })();
2305
+ } : makeAccessTokenFactory(defaultAuth);
2306
+ var oauthContext = {
2169
2307
  fetch: fetch,
2170
2308
  fetchJson: fetchJson,
2171
- loadAccessToken: loadAccessToken1,
2172
- makeUserAccessTokenFactory: makeUserAccessTokenFactory1,
2309
+ loadAccessToken: loadAccessToken,
2310
+ makeAccessTokenFactory: makeAccessTokenFactory,
2173
2311
  config: config
2174
2312
  };
2175
2313
  var calcomOAuth = {
2176
- oauthContext: oauthContext1
2314
+ oauthContext: oauthContext
2177
2315
  };
2178
2316
  return calcomOAuth;
2179
2317
  };
@@ -2287,4 +2425,4 @@ function _ts_generator(thisArg, body) {
2287
2425
  };
2288
2426
  }
2289
2427
 
2290
- export { CALCOM_API_URL, CALCOM_API_VERSION_BOOKINGS, CALCOM_API_VERSION_CALENDARS, CALCOM_API_VERSION_EVENT_TYPES, CALCOM_API_VERSION_HEADER, CALCOM_API_VERSION_ME, CALCOM_API_VERSION_SCHEDULES, CALCOM_API_VERSION_SLOTS, CALCOM_OAUTH_AUTHORIZE_URL, CALCOM_OAUTH_INVALID_GRANT_ERROR_CODE, CALCOM_OAUTH_TOKEN_URL, CALCOM_RATE_LIMIT_LIMIT_HEADER, CALCOM_RATE_LIMIT_REMAINING_HEADER, CALCOM_RATE_LIMIT_RESET_HEADER, CALCOM_TOO_MANY_REQUESTS_HTTP_STATUS_CODE, CalcomOAuthAccessTokenError, CalcomOAuthAuthFailureError, CalcomServerError, CalcomServerFetchResponseError, CalcomTooManyRequestsError, DEFAULT_CALCOM_API_RATE_LIMIT, DEFAULT_CALCOM_API_RATE_LIMIT_RESET_PERIOD, DEFAULT_CALCOM_RATE_LIMITED_TOO_MANY_REQUESTS_LOG_FUNCTION, calcomAccessTokenStringFactory, calcomApiVersionHeaders, calcomFactory, calcomOAuthAccessTokenFactory, calcomOAuthFactory, calcomRateLimitHeaderDetails, calcomRateLimitedFetchHandler, cancelBooking, createBooking, createEventType, createWebhook, deleteEventType, deleteWebhook, exchangeAuthorizationCode, getAvailableSlots, getBooking, getBusyTimes, getCalendars, getEventTypes, getMe, getSchedules, getWebhook, getWebhooks, handleCalcomErrorFetch, handleCalcomErrorFetchFactory, handleCalcomOAuthErrorFetch, logCalcomErrorToConsole, logCalcomOAuthErrorToConsole, logCalcomServerErrorFunction, parseCalcomApiError, parseCalcomApiServerErrorResponseData, parseCalcomOAuthError, parseCalcomOAuthServerErrorResponseData, parseCalcomServerErrorData, refreshAccessToken, updateEventType, updateWebhook };
2428
+ export { ALL_CALCOM_OAUTH_SCOPES, CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION, CALCOM_API_URL, CALCOM_API_VERSION_BOOKINGS, CALCOM_API_VERSION_CALENDARS, CALCOM_API_VERSION_EVENT_TYPES, CALCOM_API_VERSION_HEADER, CALCOM_API_VERSION_ME, CALCOM_API_VERSION_SCHEDULES, CALCOM_API_VERSION_SLOTS, CALCOM_OAUTH_API_URL, CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE, CALCOM_OAUTH_AUTHORIZE_URL, CALCOM_OAUTH_INVALID_GRANT_ERROR_CODE, CALCOM_OAUTH_SCOPE_DELIMITER, CALCOM_OAUTH_TOKEN_PATH, CALCOM_OAUTH_TOKEN_URL, CALCOM_RATE_LIMIT_LIMIT_HEADER, CALCOM_RATE_LIMIT_REMAINING_HEADER, CALCOM_RATE_LIMIT_RESET_HEADER, CALCOM_TOO_MANY_REQUESTS_HTTP_STATUS_CODE, CalcomOAuthAccessTokenError, CalcomOAuthAuthFailureError, CalcomServerError, CalcomServerFetchResponseError, CalcomTooManyRequestsError, DEFAULT_CALCOM_API_RATE_LIMIT, DEFAULT_CALCOM_API_RATE_LIMIT_RESET_PERIOD, DEFAULT_CALCOM_RATE_LIMITED_TOO_MANY_REQUESTS_LOG_FUNCTION, calcomAccessTokenFromApiKey, calcomAccessTokenFromTokenResponse, calcomAccessTokenStringFactory, calcomApiVersionHeaders, calcomAuthCredentialFromValues, calcomFactory, calcomOAuthAccessTokenFactory, calcomOAuthAuthorizeUrlFactory, calcomOAuthFactory, calcomRateLimitHeaderDetails, calcomRateLimitedFetchHandler, cancelBooking, createBooking, createEventType, createWebhook, deleteEventType, deleteWebhook, exchangeAuthorizationCode, getAvailableSlots, getBooking, getBusyTimes, getCalendars, getEventTypes, getMe, getSchedules, getWebhook, getWebhooks, handleCalcomErrorFetch, handleCalcomErrorFetchFactory, handleCalcomOAuthErrorFetch, isCalcomApiKeyCredential, isCalcomOAuthScope, logCalcomErrorToConsole, logCalcomOAuthErrorToConsole, logCalcomServerErrorFunction, parseCalcomApiError, parseCalcomApiServerErrorResponseData, parseCalcomOAuthError, parseCalcomOAuthServerErrorResponseData, parseCalcomServerErrorData, refreshAccessToken, updateEventType, updateWebhook };