@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.
@@ -1077,13 +1077,14 @@ const DEFAULT_HEADERS = {
1077
1077
  Accept: 'application/json',
1078
1078
  'Content-Type': 'application/json'
1079
1079
  };
1080
- const REQUEST_CACHE = new Map();
1080
+ const DEFAULT_REQUEST_CACHE = new Map();
1081
1081
  async function requestWithParameters({
1082
1082
  baseUrl,
1083
1083
  parameters = {},
1084
1084
  headers: customHeaders = {},
1085
1085
  errorContext,
1086
- maxLengthURL = DEFAULT_MAX_LENGTH_URL
1086
+ maxLengthURL = DEFAULT_MAX_LENGTH_URL,
1087
+ localCache
1087
1088
  }) {
1088
1089
  // Parameters added to all requests issued with `requestWithParameters()`.
1089
1090
  // These parameters override parameters already in the base URL, but not
@@ -1096,7 +1097,12 @@ async function requestWithParameters({
1096
1097
  }, parameters);
1097
1098
  baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
1098
1099
  const key = createCacheKey(baseUrl, parameters, customHeaders);
1099
- if (REQUEST_CACHE.has(key)) {
1100
+ const {
1101
+ cache: REQUEST_CACHE,
1102
+ canReadCache,
1103
+ canStoreInCache
1104
+ } = getCacheSettings(localCache);
1105
+ if (canReadCache && REQUEST_CACHE.has(key)) {
1100
1106
  return REQUEST_CACHE.get(key);
1101
1107
  }
1102
1108
  const url = createURLWithParameters(baseUrl, parameters);
@@ -1121,12 +1127,27 @@ async function requestWithParameters({
1121
1127
  }
1122
1128
  return json;
1123
1129
  }).catch(error => {
1124
- REQUEST_CACHE.delete(key);
1130
+ if (canStoreInCache) {
1131
+ REQUEST_CACHE.delete(key);
1132
+ }
1125
1133
  throw new CartoAPIError(error, errorContext, response, responseJson);
1126
1134
  });
1127
- REQUEST_CACHE.set(key, jsonPromise);
1135
+ if (canStoreInCache) {
1136
+ REQUEST_CACHE.set(key, jsonPromise);
1137
+ }
1128
1138
  return jsonPromise;
1129
1139
  }
1140
+ function getCacheSettings(localCache) {
1141
+ var _localCache$cacheCont, _localCache$cacheCont2;
1142
+ const canReadCache = localCache != null && (_localCache$cacheCont = localCache.cacheControl) != null && _localCache$cacheCont.includes('no-cache') ? false : true;
1143
+ const canStoreInCache = localCache != null && (_localCache$cacheCont2 = localCache.cacheControl) != null && _localCache$cacheCont2.includes('no-store') ? false : true;
1144
+ const cache = (localCache == null ? void 0 : localCache.cache) || DEFAULT_REQUEST_CACHE;
1145
+ return {
1146
+ cache,
1147
+ canReadCache,
1148
+ canStoreInCache
1149
+ };
1150
+ }
1130
1151
  function createCacheKey(baseUrl, parameters, headers) {
1131
1152
  const parameterEntries = Object.entries(parameters).sort(([a], [b]) => a > b ? 1 : -1);
1132
1153
  const headerEntries = Object.entries(headers).sort(([a], [b]) => a > b ? 1 : -1);
@@ -1194,7 +1215,8 @@ async function baseSource(endpoint, options, urlParameters) {
1194
1215
  const {
1195
1216
  clientId,
1196
1217
  maxLengthURL,
1197
- format
1218
+ format,
1219
+ localCache
1198
1220
  } = mergedOptions;
1199
1221
  const headers = _extends({
1200
1222
  Authorization: `Bearer ${options.accessToken}`
@@ -1213,7 +1235,8 @@ async function baseSource(endpoint, options, urlParameters) {
1213
1235
  parameters,
1214
1236
  headers,
1215
1237
  errorContext,
1216
- maxLengthURL
1238
+ maxLengthURL,
1239
+ localCache
1217
1240
  });
1218
1241
  const dataUrl = mapInstantiation[format].url[0];
1219
1242
  if (cache) {
@@ -1225,7 +1248,8 @@ async function baseSource(endpoint, options, urlParameters) {
1225
1248
  baseUrl: dataUrl,
1226
1249
  headers,
1227
1250
  errorContext,
1228
- maxLengthURL
1251
+ maxLengthURL,
1252
+ localCache
1229
1253
  });
1230
1254
  if (accessToken) {
1231
1255
  json.accessToken = accessToken;
@@ -1236,7 +1260,8 @@ async function baseSource(endpoint, options, urlParameters) {
1236
1260
  baseUrl: dataUrl,
1237
1261
  headers,
1238
1262
  errorContext,
1239
- maxLengthURL
1263
+ maxLengthURL,
1264
+ localCache
1240
1265
  });
1241
1266
  }
1242
1267
 
@@ -1501,6 +1526,7 @@ const query = async function query(options) {
1501
1526
  apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
1502
1527
  clientId = SOURCE_DEFAULTS.clientId,
1503
1528
  maxLengthURL = SOURCE_DEFAULTS.maxLengthURL,
1529
+ localCache,
1504
1530
  connectionName,
1505
1531
  sqlQuery,
1506
1532
  queryParameters
@@ -1532,7 +1558,8 @@ const query = async function query(options) {
1532
1558
  parameters,
1533
1559
  headers,
1534
1560
  errorContext,
1535
- maxLengthURL
1561
+ maxLengthURL,
1562
+ localCache
1536
1563
  });
1537
1564
  };
1538
1565