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.
- package/cjs/ApiTypes.d.ts +2 -1
- package/cjs/backend/AxiosRequestBackend.js +36 -1
- package/cjs/backend/FetchRequestBackend.js +2 -1
- package/cjs/cache/ClientCacheBackend.d.ts +6 -0
- package/cjs/cache/ClientCaching.d.ts +10 -0
- package/cjs/cache/{Caching.js → ClientCaching.js} +17 -17
- package/cjs/cache/LocalForageClientCacheBackend.d.ts +9 -0
- package/cjs/{tests/state.test.js → cache/LocalForageClientCacheBackend.js} +52 -52
- package/cjs/cache/LocalStorageClientCacheBackend.d.ts +7 -0
- package/cjs/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +14 -14
- package/cjs/index.d.ts +4 -4
- package/cjs/index.js +10 -10
- package/cjs/middleware/ClientCacheMiddleware.d.ts +7 -0
- package/cjs/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +16 -14
- package/esm/ApiTypes.d.ts +2 -1
- package/esm/backend/AxiosRequestBackend.js +25 -1
- package/esm/backend/FetchRequestBackend.js +2 -1
- package/esm/cache/ClientCacheBackend.d.ts +6 -0
- package/esm/cache/ClientCaching.d.ts +10 -0
- package/esm/cache/{Caching.js → ClientCaching.js} +12 -12
- package/esm/cache/LocalForageClientCacheBackend.d.ts +9 -0
- package/esm/cache/LocalForageClientCacheBackend.js +36 -0
- package/esm/cache/LocalStorageClientCacheBackend.d.ts +7 -0
- package/esm/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +8 -8
- package/esm/index.d.ts +4 -4
- package/esm/index.js +4 -4
- package/esm/middleware/ClientCacheMiddleware.d.ts +7 -0
- package/esm/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +14 -12
- package/package.json +3 -3
- package/cjs/cache/CacheBackend.d.ts +0 -6
- package/cjs/cache/Caching.d.ts +0 -10
- package/cjs/cache/LocalForageCacheBackend.d.ts +0 -9
- package/cjs/cache/LocalForageCacheBackend.js +0 -23
- package/cjs/cache/LocalStorageCacheBackend.d.ts +0 -7
- package/cjs/middleware/CacheMiddleware.d.ts +0 -7
- package/cjs/tests/api.test.js +0 -166
- package/cjs/tests/axios.test.d.ts +0 -1
- package/cjs/tests/axios.test.js +0 -94
- package/cjs/tests/backend.test.d.ts +0 -1
- package/cjs/tests/backend.test.js +0 -92
- package/cjs/tests/bodyTypes.test.d.ts +0 -1
- package/cjs/tests/bodyTypes.test.js +0 -58
- package/cjs/tests/mock/MockApi.d.ts +0 -16
- package/cjs/tests/mock/MockApi.js +0 -67
- package/cjs/tests/params.test.d.ts +0 -1
- package/cjs/tests/params.test.js +0 -147
- package/cjs/tests/retry.test.d.ts +0 -1
- package/cjs/tests/retry.test.js +0 -134
- package/cjs/tests/state.test.d.ts +0 -1
- package/cjs/tests/streaming.test.d.ts +0 -1
- package/cjs/tests/streaming.test.js +0 -207
- package/cjs/tests/validation.test.d.ts +0 -1
- package/cjs/tests/validation.test.js +0 -124
- package/esm/cache/CacheBackend.d.ts +0 -6
- package/esm/cache/CacheBackend.js +0 -1
- package/esm/cache/Caching.d.ts +0 -10
- package/esm/cache/LocalForageCacheBackend.d.ts +0 -9
- package/esm/cache/LocalForageCacheBackend.js +0 -19
- package/esm/cache/LocalStorageCacheBackend.d.ts +0 -7
- package/esm/middleware/CacheMiddleware.d.ts +0 -7
- package/esm/tests/api.test.d.ts +0 -1
- package/esm/tests/api.test.js +0 -108
- package/esm/tests/axios.test.d.ts +0 -1
- package/esm/tests/axios.test.js +0 -51
- package/esm/tests/backend.test.d.ts +0 -1
- package/esm/tests/backend.test.js +0 -49
- package/esm/tests/bodyTypes.test.d.ts +0 -1
- package/esm/tests/bodyTypes.test.js +0 -21
- package/esm/tests/mock/MockApi.d.ts +0 -16
- package/esm/tests/mock/MockApi.js +0 -64
- package/esm/tests/params.test.d.ts +0 -1
- package/esm/tests/params.test.js +0 -88
- package/esm/tests/retry.test.d.ts +0 -1
- package/esm/tests/retry.test.js +0 -77
- package/esm/tests/state.test.d.ts +0 -1
- package/esm/tests/state.test.js +0 -52
- package/esm/tests/streaming.test.d.ts +0 -1
- package/esm/tests/streaming.test.js +0 -123
- package/esm/tests/validation.test.d.ts +0 -1
- package/esm/tests/validation.test.js +0 -81
- /package/cjs/cache/{CacheBackend.js → ClientCacheBackend.js} +0 -0
- /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
|
-
|
|
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 =
|
|
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,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.
|
|
40
|
-
var
|
|
41
|
-
var
|
|
42
|
-
var
|
|
43
|
-
|
|
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.
|
|
46
|
-
exports.
|
|
47
|
-
var
|
|
48
|
-
return
|
|
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.
|
|
51
|
-
var
|
|
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*/,
|
|
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.
|
|
68
|
-
var
|
|
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*/,
|
|
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*/,
|
|
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.
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
|
40
|
-
function
|
|
39
|
+
var LocalStorageClientCacheBackend = /** @class */ (function () {
|
|
40
|
+
function LocalStorageClientCacheBackend() {
|
|
41
41
|
}
|
|
42
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
55
|
-
return [2 /*return
|
|
54
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
55
|
+
return [2 /*return*/];
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
58
|
};
|
|
59
|
-
|
|
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
|
-
|
|
67
|
+
LocalStorageClientCacheBackend.prototype.clear = function () {
|
|
68
68
|
return __awaiter(this, void 0, void 0, function () {
|
|
69
69
|
return __generator(this, function (_a) {
|
|
70
|
-
localStorage.
|
|
70
|
+
localStorage.clear();
|
|
71
71
|
return [2 /*return*/];
|
|
72
72
|
});
|
|
73
73
|
});
|
|
74
74
|
};
|
|
75
|
-
return
|
|
75
|
+
return LocalStorageClientCacheBackend;
|
|
76
76
|
}());
|
|
77
|
-
exports.default =
|
|
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 {
|
|
10
|
-
export { default as
|
|
11
|
-
export { default as
|
|
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
|
|
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.
|
|
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
|
|
30
|
-
Object.defineProperty(exports, "
|
|
31
|
-
Object.defineProperty(exports, "
|
|
32
|
-
var
|
|
33
|
-
Object.defineProperty(exports, "
|
|
34
|
-
var
|
|
35
|
-
Object.defineProperty(exports, "
|
|
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
|
|
41
|
-
Object.defineProperty(exports, "
|
|
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
|
|
41
|
-
var
|
|
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
|
|
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
|
-
|
|
52
|
+
clientCache = (context.requestConfig || {}).clientCache;
|
|
53
53
|
shouldCache = !options.predicate || options.predicate();
|
|
54
|
-
if (!(
|
|
55
|
-
expiry = typeof
|
|
56
|
-
|
|
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
|
|
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
|
-
|
|
73
|
+
clientCache = (context.requestConfig || {}).clientCache;
|
|
72
74
|
shouldCache = !options.predicate || options.predicate();
|
|
73
|
-
if (!(
|
|
74
|
-
if (!
|
|
75
|
-
return [4 /*yield*/,
|
|
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 (
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
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 =
|
|
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,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
|
|
11
|
-
let
|
|
12
|
-
export const
|
|
13
|
-
|
|
10
|
+
import LocalStorageClientCacheBackend from "./LocalStorageClientCacheBackend";
|
|
11
|
+
let clientCacheBackend = new LocalStorageClientCacheBackend();
|
|
12
|
+
export const setClientCacheBackend = (backend) => {
|
|
13
|
+
clientCacheBackend = backend;
|
|
14
14
|
};
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
17
|
-
return
|
|
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
|
|
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
|
|
24
|
+
yield clientCacheBackend.setItem(key, entry);
|
|
25
25
|
return value;
|
|
26
26
|
});
|
|
27
|
-
export const
|
|
28
|
-
const entry = yield
|
|
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
|
|
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
|
+
}
|