@carto/api-client 0.4.0 → 0.4.1-alpha.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## Not released
4
+
5
+ - add cache control mechanism for sources and query APIs
6
+
3
7
  ## 0.4
4
8
 
5
9
  ### 0.4.0
@@ -1,8 +1,10 @@
1
1
  import { APIErrorContext } from './carto-api-error';
2
- export declare function requestWithParameters<T = any>({ baseUrl, parameters, headers: customHeaders, errorContext, maxLengthURL, }: {
2
+ import { LocalCacheOptions } from '../sources/types';
3
+ export declare function requestWithParameters<T = any>({ baseUrl, parameters, headers: customHeaders, errorContext, maxLengthURL, localCache, }: {
3
4
  baseUrl: string;
4
5
  parameters?: Record<string, unknown>;
5
6
  headers?: Record<string, string>;
6
7
  errorContext: APIErrorContext;
7
8
  maxLengthURL?: number;
9
+ localCache?: LocalCacheOptions;
8
10
  }): Promise<T>;
@@ -1148,7 +1148,8 @@ const requestWithParameters = function (_ref) {
1148
1148
  parameters = {},
1149
1149
  headers: customHeaders = {},
1150
1150
  errorContext,
1151
- maxLengthURL = DEFAULT_MAX_LENGTH_URL
1151
+ maxLengthURL = DEFAULT_MAX_LENGTH_URL,
1152
+ localCache
1152
1153
  } = _ref;
1153
1154
  try {
1154
1155
  // Parameters added to all requests issued with `requestWithParameters()`.
@@ -1164,7 +1165,12 @@ const requestWithParameters = function (_ref) {
1164
1165
  };
1165
1166
  baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
1166
1167
  const key = createCacheKey(baseUrl, parameters, customHeaders);
1167
- if (REQUEST_CACHE.has(key)) {
1168
+ const {
1169
+ cache: REQUEST_CACHE,
1170
+ canReadCache,
1171
+ canStoreInCache
1172
+ } = getCacheSettings(localCache);
1173
+ if (canReadCache && REQUEST_CACHE.has(key)) {
1168
1174
  return Promise.resolve(REQUEST_CACHE.get(key));
1169
1175
  }
1170
1176
  const url = createURLWithParameters(baseUrl, parameters);
@@ -1192,10 +1198,14 @@ const requestWithParameters = function (_ref) {
1192
1198
  }
1193
1199
  return json;
1194
1200
  }).catch(error => {
1195
- REQUEST_CACHE.delete(key);
1201
+ if (canStoreInCache) {
1202
+ REQUEST_CACHE.delete(key);
1203
+ }
1196
1204
  throw new CartoAPIError(error, errorContext, response, responseJson);
1197
1205
  });
1198
- REQUEST_CACHE.set(key, jsonPromise);
1206
+ if (canStoreInCache) {
1207
+ REQUEST_CACHE.set(key, jsonPromise);
1208
+ }
1199
1209
  return Promise.resolve(jsonPromise);
1200
1210
  } catch (e) {
1201
1211
  return Promise.reject(e);
@@ -1205,7 +1215,17 @@ const DEFAULT_HEADERS = {
1205
1215
  Accept: 'application/json',
1206
1216
  'Content-Type': 'application/json'
1207
1217
  };
1208
- const REQUEST_CACHE = new Map();
1218
+ const DEFAULT_REQUEST_CACHE = new Map();
1219
+ function getCacheSettings(localCache) {
1220
+ const canReadCache = localCache?.cacheControl?.includes('no-cache') ? false : true;
1221
+ const canStoreInCache = localCache?.cacheControl?.includes('no-store') ? false : true;
1222
+ const cache = localCache?.cache || DEFAULT_REQUEST_CACHE;
1223
+ return {
1224
+ cache,
1225
+ canReadCache,
1226
+ canStoreInCache
1227
+ };
1228
+ }
1209
1229
  function createCacheKey(baseUrl, parameters, headers) {
1210
1230
  const parameterEntries = Object.entries(parameters).sort((_ref2, _ref3) => {
1211
1231
  let [a] = _ref2;
@@ -1276,7 +1296,8 @@ const baseSource = function (endpoint, options, urlParameters) {
1276
1296
  const {
1277
1297
  clientId,
1278
1298
  maxLengthURL,
1279
- format
1299
+ format,
1300
+ localCache
1280
1301
  } = mergedOptions;
1281
1302
  const headers = {
1282
1303
  Authorization: `Bearer ${options.accessToken}`,
@@ -1297,7 +1318,8 @@ const baseSource = function (endpoint, options, urlParameters) {
1297
1318
  parameters,
1298
1319
  headers,
1299
1320
  errorContext,
1300
- maxLengthURL
1321
+ maxLengthURL,
1322
+ localCache
1301
1323
  })).then(function (mapInstantiation) {
1302
1324
  let _exit;
1303
1325
  function _temp2(_result) {
@@ -1305,7 +1327,8 @@ const baseSource = function (endpoint, options, urlParameters) {
1305
1327
  baseUrl: dataUrl,
1306
1328
  headers,
1307
1329
  errorContext,
1308
- maxLengthURL
1330
+ maxLengthURL,
1331
+ localCache
1309
1332
  }));
1310
1333
  }
1311
1334
  const dataUrl = mapInstantiation[format].url[0];
@@ -1319,7 +1342,8 @@ const baseSource = function (endpoint, options, urlParameters) {
1319
1342
  baseUrl: dataUrl,
1320
1343
  headers,
1321
1344
  errorContext,
1322
- maxLengthURL
1345
+ maxLengthURL,
1346
+ localCache
1323
1347
  })).then(function (json) {
1324
1348
  if (accessToken) {
1325
1349
  json.accessToken = accessToken;
@@ -1666,6 +1690,7 @@ const query = function (options) {
1666
1690
  apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
1667
1691
  clientId = SOURCE_DEFAULTS.clientId,
1668
1692
  maxLengthURL = SOURCE_DEFAULTS.maxLengthURL,
1693
+ localCache,
1669
1694
  connectionName,
1670
1695
  sqlQuery,
1671
1696
  queryParameters
@@ -1699,7 +1724,8 @@ const query = function (options) {
1699
1724
  parameters,
1700
1725
  headers,
1701
1726
  errorContext,
1702
- maxLengthURL
1727
+ maxLengthURL,
1728
+ localCache
1703
1729
  }));
1704
1730
  } catch (e) {
1705
1731
  return Promise.reject(e);