api-def 0.12.0-alpha.39 → 0.12.0-alpha.40

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.
Files changed (82) hide show
  1. package/cjs/ApiTypes.d.ts +2 -1
  2. package/cjs/backend/AxiosRequestBackend.js +36 -1
  3. package/cjs/backend/FetchRequestBackend.js +2 -1
  4. package/cjs/cache/ClientCacheBackend.d.ts +6 -0
  5. package/cjs/cache/ClientCaching.d.ts +10 -0
  6. package/cjs/cache/{Caching.js → ClientCaching.js} +17 -17
  7. package/cjs/cache/LocalForageClientCacheBackend.d.ts +9 -0
  8. package/cjs/{tests/state.test.js → cache/LocalForageClientCacheBackend.js} +52 -52
  9. package/cjs/cache/LocalStorageClientCacheBackend.d.ts +7 -0
  10. package/cjs/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +14 -14
  11. package/cjs/index.d.ts +4 -4
  12. package/cjs/index.js +10 -10
  13. package/cjs/middleware/ClientCacheMiddleware.d.ts +7 -0
  14. package/cjs/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +16 -14
  15. package/esm/ApiTypes.d.ts +2 -1
  16. package/esm/backend/AxiosRequestBackend.js +25 -1
  17. package/esm/backend/FetchRequestBackend.js +2 -1
  18. package/esm/cache/ClientCacheBackend.d.ts +6 -0
  19. package/esm/cache/ClientCaching.d.ts +10 -0
  20. package/esm/cache/{Caching.js → ClientCaching.js} +12 -12
  21. package/esm/cache/LocalForageClientCacheBackend.d.ts +9 -0
  22. package/esm/cache/LocalForageClientCacheBackend.js +36 -0
  23. package/esm/cache/LocalStorageClientCacheBackend.d.ts +7 -0
  24. package/esm/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +8 -8
  25. package/esm/index.d.ts +4 -4
  26. package/esm/index.js +4 -4
  27. package/esm/middleware/ClientCacheMiddleware.d.ts +7 -0
  28. package/esm/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +14 -12
  29. package/package.json +3 -3
  30. package/cjs/cache/CacheBackend.d.ts +0 -6
  31. package/cjs/cache/Caching.d.ts +0 -10
  32. package/cjs/cache/LocalForageCacheBackend.d.ts +0 -9
  33. package/cjs/cache/LocalForageCacheBackend.js +0 -23
  34. package/cjs/cache/LocalStorageCacheBackend.d.ts +0 -7
  35. package/cjs/middleware/CacheMiddleware.d.ts +0 -7
  36. package/cjs/tests/api.test.js +0 -166
  37. package/cjs/tests/axios.test.d.ts +0 -1
  38. package/cjs/tests/axios.test.js +0 -94
  39. package/cjs/tests/backend.test.d.ts +0 -1
  40. package/cjs/tests/backend.test.js +0 -92
  41. package/cjs/tests/bodyTypes.test.d.ts +0 -1
  42. package/cjs/tests/bodyTypes.test.js +0 -58
  43. package/cjs/tests/mock/MockApi.d.ts +0 -16
  44. package/cjs/tests/mock/MockApi.js +0 -67
  45. package/cjs/tests/params.test.d.ts +0 -1
  46. package/cjs/tests/params.test.js +0 -147
  47. package/cjs/tests/retry.test.d.ts +0 -1
  48. package/cjs/tests/retry.test.js +0 -134
  49. package/cjs/tests/state.test.d.ts +0 -1
  50. package/cjs/tests/streaming.test.d.ts +0 -1
  51. package/cjs/tests/streaming.test.js +0 -207
  52. package/cjs/tests/validation.test.d.ts +0 -1
  53. package/cjs/tests/validation.test.js +0 -124
  54. package/esm/cache/CacheBackend.d.ts +0 -6
  55. package/esm/cache/CacheBackend.js +0 -1
  56. package/esm/cache/Caching.d.ts +0 -10
  57. package/esm/cache/LocalForageCacheBackend.d.ts +0 -9
  58. package/esm/cache/LocalForageCacheBackend.js +0 -19
  59. package/esm/cache/LocalStorageCacheBackend.d.ts +0 -7
  60. package/esm/middleware/CacheMiddleware.d.ts +0 -7
  61. package/esm/tests/api.test.d.ts +0 -1
  62. package/esm/tests/api.test.js +0 -108
  63. package/esm/tests/axios.test.d.ts +0 -1
  64. package/esm/tests/axios.test.js +0 -51
  65. package/esm/tests/backend.test.d.ts +0 -1
  66. package/esm/tests/backend.test.js +0 -49
  67. package/esm/tests/bodyTypes.test.d.ts +0 -1
  68. package/esm/tests/bodyTypes.test.js +0 -21
  69. package/esm/tests/mock/MockApi.d.ts +0 -16
  70. package/esm/tests/mock/MockApi.js +0 -64
  71. package/esm/tests/params.test.d.ts +0 -1
  72. package/esm/tests/params.test.js +0 -88
  73. package/esm/tests/retry.test.d.ts +0 -1
  74. package/esm/tests/retry.test.js +0 -77
  75. package/esm/tests/state.test.d.ts +0 -1
  76. package/esm/tests/state.test.js +0 -52
  77. package/esm/tests/streaming.test.d.ts +0 -1
  78. package/esm/tests/streaming.test.js +0 -123
  79. package/esm/tests/validation.test.d.ts +0 -1
  80. package/esm/tests/validation.test.js +0 -81
  81. /package/cjs/cache/{CacheBackend.js → ClientCacheBackend.js} +0 -0
  82. /package/{cjs/tests/api.test.d.ts → esm/cache/ClientCacheBackend.js} +0 -0
package/cjs/ApiTypes.d.ts CHANGED
@@ -32,7 +32,8 @@ export interface RetryOptions {
32
32
  }
33
33
  export interface BaseRequestConfig {
34
34
  debug?: boolean;
35
- cache?: number | boolean;
35
+ clientCache?: number | boolean;
36
+ browserCache?: RequestCache;
36
37
  lock?: RequestLock;
37
38
  credentials?: "omit" | "same-origin" | "include";
38
39
  retry?: number | false | RetryOptions;
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -79,6 +90,30 @@ var isAxiosError = function (error) {
79
90
  return "isAxiosError" in error;
80
91
  };
81
92
  exports.isAxiosError = isAxiosError;
93
+ var getCacheHeaders = function (browserCache) {
94
+ switch (browserCache) {
95
+ case "no-store":
96
+ return {
97
+ "Cache-Control": "no-store",
98
+ Pragma: "no-cache",
99
+ };
100
+ case "no-cache":
101
+ return {
102
+ "Cache-Control": "no-cache",
103
+ Pragma: "no-cache",
104
+ };
105
+ case "force-cache":
106
+ return {
107
+ "Cache-Control": "max-age=31536000",
108
+ };
109
+ case "only-if-cached":
110
+ return {
111
+ "Cache-Control": "only-if-cached",
112
+ };
113
+ default:
114
+ return {};
115
+ }
116
+ };
82
117
  var AxiosRequestBackend = /** @class */ (function () {
83
118
  function AxiosRequestBackend(axiosLibrary) {
84
119
  this.id = "axios";
@@ -162,7 +197,7 @@ var AxiosRequestBackend = /** @class */ (function () {
162
197
  method: context.method,
163
198
  url: url.href,
164
199
  data: context.getParsedBody(),
165
- headers: requestConfig.headers || {},
200
+ headers: __assign(__assign({}, getCacheHeaders(requestConfig.browserCache)), requestConfig.headers),
166
201
  responseType: context.responseType,
167
202
  withCredentials: requestConfig.credentials === "include" || requestConfig.credentials === "same-origin",
168
203
  cancelToken: new axios.CancelToken(function (cancellerFunc) {
@@ -173,12 +173,13 @@ var FetchRequestBackend = /** @class */ (function () {
173
173
  body: bodyJsonify ? JSON.stringify(body) : body,
174
174
  headers: parsedHeaders,
175
175
  signal: abortSignal,
176
+ cache: requestConfig.browserCache,
176
177
  };
177
178
  if ("mode" in Request.prototype) {
178
179
  fetchOptions.mode = "cors";
179
180
  }
180
181
  if ("credentials" in Request.prototype) {
181
- fetchOptions.credentials = context.requestConfig.credentials ? "include" : undefined;
182
+ fetchOptions.credentials = requestConfig.credentials ? "include" : undefined;
182
183
  }
183
184
  if (requestConfig.debug) {
184
185
  console.log("[api-def] Fetching '".concat(url.href, "' with options"), JSON.stringify(fetchOptions, null, 2));
@@ -0,0 +1,6 @@
1
+ export interface ClientCacheBackend {
2
+ getItem(key: string): Promise<any>;
3
+ setItem(key: string, value: any): Promise<void>;
4
+ removeItem(key: string): Promise<void>;
5
+ clear(): Promise<void>;
6
+ }
@@ -0,0 +1,10 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export declare const setClientCacheBackend: (backend: ClientCacheBackend) => void;
3
+ export declare const DEFAULT_CLIENT_CACHE_EXPIRY: number;
4
+ export declare const clearClientCache: () => Promise<void>;
5
+ export interface ClientCacheEntry {
6
+ data: any;
7
+ expiry: number | null;
8
+ }
9
+ export declare const setClientCachedItem: <T>(key: string, value: T, expiry?: number) => Promise<T>;
10
+ export declare const getClientCachedItem: <T = any>(key: string) => Promise<T | undefined>;
@@ -36,19 +36,19 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.getCachedItem = exports.setCachedItem = exports.clearCache = exports.DEFAULT_CACHE_EXPIRY = exports.setCacheBackend = void 0;
40
- var LocalStorageCacheBackend_1 = require("./LocalStorageCacheBackend");
41
- var cacheBackend = new LocalStorageCacheBackend_1.default();
42
- var setCacheBackend = function (backend) {
43
- cacheBackend = backend;
39
+ exports.getClientCachedItem = exports.setClientCachedItem = exports.clearClientCache = exports.DEFAULT_CLIENT_CACHE_EXPIRY = exports.setClientCacheBackend = void 0;
40
+ var LocalStorageClientCacheBackend_1 = require("./LocalStorageClientCacheBackend");
41
+ var clientCacheBackend = new LocalStorageClientCacheBackend_1.default();
42
+ var setClientCacheBackend = function (backend) {
43
+ clientCacheBackend = backend;
44
44
  };
45
- exports.setCacheBackend = setCacheBackend;
46
- exports.DEFAULT_CACHE_EXPIRY = 60 * 1000 * 15;
47
- var clearCache = function () {
48
- return cacheBackend.clear();
45
+ exports.setClientCacheBackend = setClientCacheBackend;
46
+ exports.DEFAULT_CLIENT_CACHE_EXPIRY = 60 * 1000 * 15;
47
+ var clearClientCache = function () {
48
+ return clientCacheBackend.clear();
49
49
  };
50
- exports.clearCache = clearCache;
51
- var setCachedItem = function (key, value, expiry) { return __awaiter(void 0, void 0, void 0, function () {
50
+ exports.clearClientCache = clearClientCache;
51
+ var setClientCachedItem = function (key, value, expiry) { return __awaiter(void 0, void 0, void 0, function () {
52
52
  var entry;
53
53
  return __generator(this, function (_a) {
54
54
  switch (_a.label) {
@@ -57,19 +57,19 @@ var setCachedItem = function (key, value, expiry) { return __awaiter(void 0, voi
57
57
  data: value,
58
58
  expiry: expiry === undefined || Number.isNaN(expiry) ? null : expiry,
59
59
  };
60
- return [4 /*yield*/, cacheBackend.setItem(key, entry)];
60
+ return [4 /*yield*/, clientCacheBackend.setItem(key, entry)];
61
61
  case 1:
62
62
  _a.sent();
63
63
  return [2 /*return*/, value];
64
64
  }
65
65
  });
66
66
  }); };
67
- exports.setCachedItem = setCachedItem;
68
- var getCachedItem = function (key) { return __awaiter(void 0, void 0, void 0, function () {
67
+ exports.setClientCachedItem = setClientCachedItem;
68
+ var getClientCachedItem = function (key) { return __awaiter(void 0, void 0, void 0, function () {
69
69
  var entry;
70
70
  return __generator(this, function (_a) {
71
71
  switch (_a.label) {
72
- case 0: return [4 /*yield*/, cacheBackend.getItem(key)];
72
+ case 0: return [4 /*yield*/, clientCacheBackend.getItem(key)];
73
73
  case 1:
74
74
  entry = _a.sent();
75
75
  if (!entry) {
@@ -77,7 +77,7 @@ var getCachedItem = function (key) { return __awaiter(void 0, void 0, void 0, fu
77
77
  }
78
78
  if (!(typeof entry.expiry === "number")) return [3 /*break*/, 3];
79
79
  if (!(Date.now() >= entry.expiry)) return [3 /*break*/, 3];
80
- return [4 /*yield*/, cacheBackend.removeItem(key)];
80
+ return [4 /*yield*/, clientCacheBackend.removeItem(key)];
81
81
  case 2:
82
82
  _a.sent();
83
83
  return [2 /*return*/, undefined];
@@ -85,4 +85,4 @@ var getCachedItem = function (key) { return __awaiter(void 0, void 0, void 0, fu
85
85
  }
86
86
  });
87
87
  }); };
88
- exports.getCachedItem = getCachedItem;
88
+ exports.getClientCachedItem = getClientCachedItem;
@@ -0,0 +1,9 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export default class LocalForageClientCacheBackend implements ClientCacheBackend {
3
+ private store;
4
+ constructor(localforage: any);
5
+ getItem(key: string): Promise<any>;
6
+ setItem(key: string, value: any): Promise<void>;
7
+ removeItem(key: string): Promise<void>;
8
+ clear(): Promise<void>;
9
+ }
@@ -36,55 +36,55 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var Api_1 = require("../Api");
40
- it("can provide extra state to a request that a middleware can use ", function () { return __awaiter(void 0, void 0, void 0, function () {
41
- var api, cookiesEndpoint, cookies;
42
- return __generator(this, function (_a) {
43
- switch (_a.label) {
44
- case 0:
45
- api = new Api_1.Api({
46
- baseUrl: "httpstat.us",
47
- name: "Http Status API",
48
- mocking: {
49
- enabled: true,
50
- },
51
- middleware: [
52
- {
53
- beforeSend: function (context) {
54
- if (context.requestConfig.state.cookies) {
55
- context.updateHeaders({
56
- Cookie: context.requestConfig.state.cookies,
57
- });
58
- }
59
- },
60
- },
61
- ],
62
- });
63
- cookiesEndpoint = api
64
- .endpoint()
65
- .stateOf()
66
- .responseOf()
67
- .build({
68
- id: "cookies",
69
- method: "get",
70
- path: "/200",
71
- mocking: {
72
- handler: function (req, res) {
73
- return res.status(200).send({
74
- cookies: req.headers.Cookie,
75
- });
76
- },
77
- },
78
- });
79
- return [4 /*yield*/, cookiesEndpoint.submit({
80
- state: {
81
- cookies: "test=123",
82
- },
83
- })];
84
- case 1:
85
- cookies = (_a.sent()).data.cookies;
86
- expect(cookies).toBe("test=123");
87
- return [2 /*return*/];
88
- }
89
- });
90
- }); });
39
+ var LocalForageClientCacheBackend = /** @class */ (function () {
40
+ function LocalForageClientCacheBackend(localforage) {
41
+ this.store = localforage.createInstance({
42
+ name: "requestClientCache",
43
+ });
44
+ }
45
+ LocalForageClientCacheBackend.prototype.getItem = function (key) {
46
+ return __awaiter(this, void 0, void 0, function () {
47
+ return __generator(this, function (_a) {
48
+ return [2 /*return*/, this.store.getItem(key)];
49
+ });
50
+ });
51
+ };
52
+ LocalForageClientCacheBackend.prototype.setItem = function (key, value) {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ return __generator(this, function (_a) {
55
+ switch (_a.label) {
56
+ case 0: return [4 /*yield*/, this.store.setItem(key, value)];
57
+ case 1:
58
+ _a.sent();
59
+ return [2 /*return*/];
60
+ }
61
+ });
62
+ });
63
+ };
64
+ LocalForageClientCacheBackend.prototype.removeItem = function (key) {
65
+ return __awaiter(this, void 0, void 0, function () {
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0: return [4 /*yield*/, this.store.removeItem(key)];
69
+ case 1:
70
+ _a.sent();
71
+ return [2 /*return*/];
72
+ }
73
+ });
74
+ });
75
+ };
76
+ LocalForageClientCacheBackend.prototype.clear = function () {
77
+ return __awaiter(this, void 0, void 0, function () {
78
+ return __generator(this, function (_a) {
79
+ switch (_a.label) {
80
+ case 0: return [4 /*yield*/, this.store.clear()];
81
+ case 1:
82
+ _a.sent();
83
+ return [2 /*return*/];
84
+ }
85
+ });
86
+ });
87
+ };
88
+ return LocalForageClientCacheBackend;
89
+ }());
90
+ exports.default = LocalForageClientCacheBackend;
@@ -0,0 +1,7 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export default class LocalStorageClientCacheBackend implements ClientCacheBackend {
3
+ getItem(key: string): Promise<any>;
4
+ setItem(key: string, value: any): Promise<void>;
5
+ removeItem(key: string): Promise<void>;
6
+ clear(): Promise<void>;
7
+ }
@@ -36,27 +36,27 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var LocalStorageCacheBackend = /** @class */ (function () {
40
- function LocalStorageCacheBackend() {
39
+ var LocalStorageClientCacheBackend = /** @class */ (function () {
40
+ function LocalStorageClientCacheBackend() {
41
41
  }
42
- LocalStorageCacheBackend.prototype.clear = function () {
42
+ LocalStorageClientCacheBackend.prototype.getItem = function (key) {
43
43
  return __awaiter(this, void 0, void 0, function () {
44
+ var item;
44
45
  return __generator(this, function (_a) {
45
- localStorage.clear();
46
- return [2 /*return*/];
46
+ item = localStorage.getItem(key);
47
+ return [2 /*return*/, item ? JSON.parse(item) : null];
47
48
  });
48
49
  });
49
50
  };
50
- LocalStorageCacheBackend.prototype.getItem = function (key) {
51
+ LocalStorageClientCacheBackend.prototype.setItem = function (key, value) {
51
52
  return __awaiter(this, void 0, void 0, function () {
52
- var value;
53
53
  return __generator(this, function (_a) {
54
- value = localStorage.getItem(key);
55
- return [2 /*return*/, value ? JSON.parse(value) : null];
54
+ localStorage.setItem(key, JSON.stringify(value));
55
+ return [2 /*return*/];
56
56
  });
57
57
  });
58
58
  };
59
- LocalStorageCacheBackend.prototype.removeItem = function (key) {
59
+ LocalStorageClientCacheBackend.prototype.removeItem = function (key) {
60
60
  return __awaiter(this, void 0, void 0, function () {
61
61
  return __generator(this, function (_a) {
62
62
  localStorage.removeItem(key);
@@ -64,14 +64,14 @@ var LocalStorageCacheBackend = /** @class */ (function () {
64
64
  });
65
65
  });
66
66
  };
67
- LocalStorageCacheBackend.prototype.setItem = function (key, value) {
67
+ LocalStorageClientCacheBackend.prototype.clear = function () {
68
68
  return __awaiter(this, void 0, void 0, function () {
69
69
  return __generator(this, function (_a) {
70
- localStorage.setItem(key, JSON.stringify(value));
70
+ localStorage.clear();
71
71
  return [2 /*return*/];
72
72
  });
73
73
  });
74
74
  };
75
- return LocalStorageCacheBackend;
75
+ return LocalStorageClientCacheBackend;
76
76
  }());
77
- exports.default = LocalStorageCacheBackend;
77
+ exports.default = LocalStorageClientCacheBackend;
package/cjs/index.d.ts CHANGED
@@ -6,10 +6,10 @@ export { default as retry } from "./util/retry";
6
6
  export type { RetryOptions } from "./util/retry/interfaces";
7
7
  export { isRequestError, type RequestError } from "./RequestError";
8
8
  export { default as Endpoint, type EndpointConfig } from "./Endpoint";
9
- export { clearCache, setCacheBackend } from "./cache/Caching";
10
- export { default as LocalStorageCacheBackend } from "./cache/LocalStorageCacheBackend";
11
- export { default as LocaleForageCacheBackend } from "./cache/LocalForageCacheBackend";
9
+ export { clearClientCache, setClientCacheBackend } from "./cache/ClientCaching";
10
+ export { default as LocalStorageClientCacheBackend } from "./cache/LocalStorageClientCacheBackend";
11
+ export { default as LocalForageClientCacheBackend } from "./cache/LocalForageClientCacheBackend";
12
12
  export { default as AxiosRequestBackend } from "./backend/AxiosRequestBackend";
13
13
  export { default as FetchRequestBackend } from "./backend/FetchRequestBackend";
14
- export { default as CacheMiddleware } from "./middleware/CacheMiddleware";
14
+ export { default as ClientCacheMiddleware } from "./middleware/ClientCacheMiddleware";
15
15
  export { default as LoggingMiddleware } from "./middleware/LoggingMiddleware";
package/cjs/index.js CHANGED
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.LoggingMiddleware = exports.CacheMiddleware = exports.FetchRequestBackend = exports.AxiosRequestBackend = exports.LocaleForageCacheBackend = exports.LocalStorageCacheBackend = exports.setCacheBackend = exports.clearCache = exports.Endpoint = exports.isRequestError = exports.retry = void 0;
18
+ exports.LoggingMiddleware = exports.ClientCacheMiddleware = exports.FetchRequestBackend = exports.AxiosRequestBackend = exports.LocalForageClientCacheBackend = exports.LocalStorageClientCacheBackend = exports.setClientCacheBackend = exports.clearClientCache = exports.Endpoint = exports.isRequestError = exports.retry = void 0;
19
19
  __exportStar(require("./Api"), exports);
20
20
  __exportStar(require("./ApiTypes"), exports);
21
21
  __exportStar(require("./UtilTypes"), exports);
@@ -26,18 +26,18 @@ var RequestError_1 = require("./RequestError");
26
26
  Object.defineProperty(exports, "isRequestError", { enumerable: true, get: function () { return RequestError_1.isRequestError; } });
27
27
  var Endpoint_1 = require("./Endpoint");
28
28
  Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return Endpoint_1.default; } });
29
- var Caching_1 = require("./cache/Caching");
30
- Object.defineProperty(exports, "clearCache", { enumerable: true, get: function () { return Caching_1.clearCache; } });
31
- Object.defineProperty(exports, "setCacheBackend", { enumerable: true, get: function () { return Caching_1.setCacheBackend; } });
32
- var LocalStorageCacheBackend_1 = require("./cache/LocalStorageCacheBackend");
33
- Object.defineProperty(exports, "LocalStorageCacheBackend", { enumerable: true, get: function () { return LocalStorageCacheBackend_1.default; } });
34
- var LocalForageCacheBackend_1 = require("./cache/LocalForageCacheBackend");
35
- Object.defineProperty(exports, "LocaleForageCacheBackend", { enumerable: true, get: function () { return LocalForageCacheBackend_1.default; } });
29
+ var ClientCaching_1 = require("./cache/ClientCaching");
30
+ Object.defineProperty(exports, "clearClientCache", { enumerable: true, get: function () { return ClientCaching_1.clearClientCache; } });
31
+ Object.defineProperty(exports, "setClientCacheBackend", { enumerable: true, get: function () { return ClientCaching_1.setClientCacheBackend; } });
32
+ var LocalStorageClientCacheBackend_1 = require("./cache/LocalStorageClientCacheBackend");
33
+ Object.defineProperty(exports, "LocalStorageClientCacheBackend", { enumerable: true, get: function () { return LocalStorageClientCacheBackend_1.default; } });
34
+ var LocalForageClientCacheBackend_1 = require("./cache/LocalForageClientCacheBackend");
35
+ Object.defineProperty(exports, "LocalForageClientCacheBackend", { enumerable: true, get: function () { return LocalForageClientCacheBackend_1.default; } });
36
36
  var AxiosRequestBackend_1 = require("./backend/AxiosRequestBackend");
37
37
  Object.defineProperty(exports, "AxiosRequestBackend", { enumerable: true, get: function () { return AxiosRequestBackend_1.default; } });
38
38
  var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
39
39
  Object.defineProperty(exports, "FetchRequestBackend", { enumerable: true, get: function () { return FetchRequestBackend_1.default; } });
40
- var CacheMiddleware_1 = require("./middleware/CacheMiddleware");
41
- Object.defineProperty(exports, "CacheMiddleware", { enumerable: true, get: function () { return CacheMiddleware_1.default; } });
40
+ var ClientCacheMiddleware_1 = require("./middleware/ClientCacheMiddleware");
41
+ Object.defineProperty(exports, "ClientCacheMiddleware", { enumerable: true, get: function () { return ClientCacheMiddleware_1.default; } });
42
42
  var LoggingMiddleware_1 = require("./middleware/LoggingMiddleware");
43
43
  Object.defineProperty(exports, "LoggingMiddleware", { enumerable: true, get: function () { return LoggingMiddleware_1.default; } });
@@ -0,0 +1,7 @@
1
+ import type { RequestMiddleware } from "../ApiTypes";
2
+ export interface ClientCacheMiddlewareOptions {
3
+ defaultExpiry?: number;
4
+ predicate?: () => boolean;
5
+ }
6
+ declare const ClientCacheMiddleware: (options?: ClientCacheMiddlewareOptions) => RequestMiddleware;
7
+ export default ClientCacheMiddleware;
@@ -37,23 +37,25 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var ApiConstants_1 = require("../ApiConstants");
40
- var Caching = require("../cache/Caching");
41
- var CacheMiddleware = function (options) {
40
+ var ClientCaching = require("../cache/ClientCaching");
41
+ var ClientCacheMiddleware = function (options) {
42
42
  var _a;
43
43
  if (options === void 0) { options = {}; }
44
44
  return _a = {},
45
45
  _a[ApiConstants_1.RequestEvent.SUCCESS] = function (context) { return __awaiter(void 0, void 0, void 0, function () {
46
- var cache, shouldCache, expiry;
46
+ var clientCache, shouldCache, expiry;
47
47
  return __generator(this, function (_a) {
48
48
  switch (_a.label) {
49
49
  case 0:
50
50
  if (context.method !== ApiConstants_1.RequestMethod.GET)
51
51
  return [2 /*return*/];
52
- cache = (context.requestConfig || {}).cache;
52
+ clientCache = (context.requestConfig || {}).clientCache;
53
53
  shouldCache = !options.predicate || options.predicate();
54
- if (!(cache && shouldCache)) return [3 /*break*/, 2];
55
- expiry = typeof cache === "number" ? cache : options.defaultExpiry || Caching.DEFAULT_CACHE_EXPIRY;
56
- return [4 /*yield*/, Caching.setCachedItem(context.key, context.response, expiry)];
54
+ if (!(clientCache && shouldCache)) return [3 /*break*/, 2];
55
+ expiry = typeof clientCache === "number"
56
+ ? clientCache
57
+ : options.defaultExpiry || ClientCaching.DEFAULT_CLIENT_CACHE_EXPIRY;
58
+ return [4 /*yield*/, ClientCaching.setClientCachedItem(context.key, context.response, expiry)];
57
59
  case 1:
58
60
  _a.sent();
59
61
  _a.label = 2;
@@ -62,17 +64,17 @@ var CacheMiddleware = function (options) {
62
64
  });
63
65
  }); },
64
66
  _a[ApiConstants_1.RequestEvent.BEFORE_SEND] = function (context) { return __awaiter(void 0, void 0, void 0, function () {
65
- var cache, shouldCache, cachedValue;
67
+ var clientCache, shouldCache, cachedValue;
66
68
  return __generator(this, function (_a) {
67
69
  switch (_a.label) {
68
70
  case 0:
69
71
  if (context.method !== ApiConstants_1.RequestMethod.GET)
70
72
  return [2 /*return*/];
71
- cache = (context.requestConfig || {}).cache;
73
+ clientCache = (context.requestConfig || {}).clientCache;
72
74
  shouldCache = !options.predicate || options.predicate();
73
- if (!(cache && shouldCache)) return [3 /*break*/, 2];
74
- if (!cache) return [3 /*break*/, 2];
75
- return [4 /*yield*/, Caching.getCachedItem(context.key)];
75
+ if (!(clientCache && shouldCache)) return [3 /*break*/, 2];
76
+ if (!clientCache) return [3 /*break*/, 2];
77
+ return [4 /*yield*/, ClientCaching.getClientCachedItem(context.key)];
76
78
  case 1:
77
79
  cachedValue = _a.sent();
78
80
  if (cachedValue) {
@@ -89,7 +91,7 @@ var CacheMiddleware = function (options) {
89
91
  }
90
92
  _a.label = 2;
91
93
  case 2:
92
- if (cache === false) {
94
+ if (clientCache === false) {
93
95
  context.updateQuery({
94
96
  _bust: Math.floor(Math.random() * 9000) + 1000,
95
97
  });
@@ -104,4 +106,4 @@ var CacheMiddleware = function (options) {
104
106
  }); },
105
107
  _a;
106
108
  };
107
- exports.default = CacheMiddleware;
109
+ exports.default = ClientCacheMiddleware;
package/esm/ApiTypes.d.ts CHANGED
@@ -32,7 +32,8 @@ export interface RetryOptions {
32
32
  }
33
33
  export interface BaseRequestConfig {
34
34
  debug?: boolean;
35
- cache?: number | boolean;
35
+ clientCache?: number | boolean;
36
+ browserCache?: RequestCache;
36
37
  lock?: RequestLock;
37
38
  credentials?: "omit" | "same-origin" | "include";
38
39
  retry?: number | false | RetryOptions;
@@ -37,6 +37,30 @@ let axios;
37
37
  export const isAxiosError = (error) => {
38
38
  return "isAxiosError" in error;
39
39
  };
40
+ const getCacheHeaders = (browserCache) => {
41
+ switch (browserCache) {
42
+ case "no-store":
43
+ return {
44
+ "Cache-Control": "no-store",
45
+ Pragma: "no-cache",
46
+ };
47
+ case "no-cache":
48
+ return {
49
+ "Cache-Control": "no-cache",
50
+ Pragma: "no-cache",
51
+ };
52
+ case "force-cache":
53
+ return {
54
+ "Cache-Control": "max-age=31536000",
55
+ };
56
+ case "only-if-cached":
57
+ return {
58
+ "Cache-Control": "only-if-cached",
59
+ };
60
+ default:
61
+ return {};
62
+ }
63
+ };
40
64
  export default class AxiosRequestBackend {
41
65
  constructor(axiosLibrary) {
42
66
  this.id = "axios";
@@ -100,7 +124,7 @@ export default class AxiosRequestBackend {
100
124
  method: context.method,
101
125
  url: url.href,
102
126
  data: context.getParsedBody(),
103
- headers: requestConfig.headers || {},
127
+ headers: Object.assign(Object.assign({}, getCacheHeaders(requestConfig.browserCache)), requestConfig.headers),
104
128
  responseType: context.responseType,
105
129
  withCredentials: requestConfig.credentials === "include" || requestConfig.credentials === "same-origin",
106
130
  cancelToken: new axios.CancelToken((cancellerFunc) => {
@@ -107,12 +107,13 @@ export default class FetchRequestBackend {
107
107
  body: bodyJsonify ? JSON.stringify(body) : body,
108
108
  headers: parsedHeaders,
109
109
  signal: abortSignal,
110
+ cache: requestConfig.browserCache,
110
111
  };
111
112
  if ("mode" in Request.prototype) {
112
113
  fetchOptions.mode = "cors";
113
114
  }
114
115
  if ("credentials" in Request.prototype) {
115
- fetchOptions.credentials = context.requestConfig.credentials ? "include" : undefined;
116
+ fetchOptions.credentials = requestConfig.credentials ? "include" : undefined;
116
117
  }
117
118
  if (requestConfig.debug) {
118
119
  console.log(`[api-def] Fetching '${url.href}' with options`, JSON.stringify(fetchOptions, null, 2));
@@ -0,0 +1,6 @@
1
+ export interface ClientCacheBackend {
2
+ getItem(key: string): Promise<any>;
3
+ setItem(key: string, value: any): Promise<void>;
4
+ removeItem(key: string): Promise<void>;
5
+ clear(): Promise<void>;
6
+ }
@@ -0,0 +1,10 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export declare const setClientCacheBackend: (backend: ClientCacheBackend) => void;
3
+ export declare const DEFAULT_CLIENT_CACHE_EXPIRY: number;
4
+ export declare const clearClientCache: () => Promise<void>;
5
+ export interface ClientCacheEntry {
6
+ data: any;
7
+ expiry: number | null;
8
+ }
9
+ export declare const setClientCachedItem: <T>(key: string, value: T, expiry?: number) => Promise<T>;
10
+ export declare const getClientCachedItem: <T = any>(key: string) => Promise<T | undefined>;
@@ -7,31 +7,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import LocalStorageCacheBackend from "./LocalStorageCacheBackend";
11
- let cacheBackend = new LocalStorageCacheBackend();
12
- export const setCacheBackend = (backend) => {
13
- cacheBackend = backend;
10
+ import LocalStorageClientCacheBackend from "./LocalStorageClientCacheBackend";
11
+ let clientCacheBackend = new LocalStorageClientCacheBackend();
12
+ export const setClientCacheBackend = (backend) => {
13
+ clientCacheBackend = backend;
14
14
  };
15
- export const DEFAULT_CACHE_EXPIRY = /* 15 mins */ 60 * 1000 * 15;
16
- export const clearCache = () => {
17
- return cacheBackend.clear();
15
+ export const DEFAULT_CLIENT_CACHE_EXPIRY = /* 15 mins */ 60 * 1000 * 15;
16
+ export const clearClientCache = () => {
17
+ return clientCacheBackend.clear();
18
18
  };
19
- export const setCachedItem = (key, value, expiry) => __awaiter(void 0, void 0, void 0, function* () {
19
+ export const setClientCachedItem = (key, value, expiry) => __awaiter(void 0, void 0, void 0, function* () {
20
20
  const entry = {
21
21
  data: value,
22
22
  expiry: expiry === undefined || Number.isNaN(expiry) ? null : expiry,
23
23
  };
24
- yield cacheBackend.setItem(key, entry);
24
+ yield clientCacheBackend.setItem(key, entry);
25
25
  return value;
26
26
  });
27
- export const getCachedItem = (key) => __awaiter(void 0, void 0, void 0, function* () {
28
- const entry = yield cacheBackend.getItem(key);
27
+ export const getClientCachedItem = (key) => __awaiter(void 0, void 0, void 0, function* () {
28
+ const entry = yield clientCacheBackend.getItem(key);
29
29
  if (!entry) {
30
30
  return undefined;
31
31
  }
32
32
  if (typeof entry.expiry === "number") {
33
33
  if (Date.now() >= entry.expiry) {
34
- yield cacheBackend.removeItem(key);
34
+ yield clientCacheBackend.removeItem(key);
35
35
  return undefined;
36
36
  }
37
37
  }
@@ -0,0 +1,9 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export default class LocalForageClientCacheBackend implements ClientCacheBackend {
3
+ private store;
4
+ constructor(localforage: any);
5
+ getItem(key: string): Promise<any>;
6
+ setItem(key: string, value: any): Promise<void>;
7
+ removeItem(key: string): Promise<void>;
8
+ clear(): Promise<void>;
9
+ }