@faable/sdk-base 1.2.0 → 1.3.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher_axios.d.ts","sourceRoot":"","sources":["../../src/fetcher/fetcher_axios.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAiBjD,eAAO,MAAM,aAAa,YAAY,SAAS,KAAQ,
|
|
1
|
+
{"version":3,"file":"fetcher_axios.d.ts","sourceRoot":"","sources":["../../src/fetcher/fetcher_axios.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAiBjD,eAAO,MAAM,aAAa,YAAY,SAAS,KAAQ,OAkItD,CAAC"}
|
|
@@ -54,38 +54,41 @@ export const fetcher_axios = (params = {}) => {
|
|
|
54
54
|
etagCache.delete(etagCache.keys().next().value);
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
|
+
// Conditional GET against the cache. Replays the stored ETag as
|
|
58
|
+
// `If-None-Match`; a `304` serves the cached parsed body, a `200` refreshes
|
|
59
|
+
// it. Shared by `get` and the GET path of `request` so paginated lists
|
|
60
|
+
// (the paginator calls `request`) cache page-by-page too — each page is a
|
|
61
|
+
// distinct (url + params incl. cursor) key, and the API folds a per-resource
|
|
62
|
+
// generation counter into every page's ETag, so any write invalidates all
|
|
63
|
+
// pages (server-driven, never stale).
|
|
64
|
+
const etagGet = async (cfg) => {
|
|
65
|
+
const key = etagKey(cfg.url ?? "", cfg.params);
|
|
66
|
+
const cached = etagCache.get(key);
|
|
67
|
+
const res = await instance.request({
|
|
68
|
+
...cfg,
|
|
69
|
+
headers: {
|
|
70
|
+
...cfg.headers,
|
|
71
|
+
...(cached ? { "If-None-Match": cached.etag } : {}),
|
|
72
|
+
},
|
|
73
|
+
// Accept 304 so axios resolves (instead of routing it through the
|
|
74
|
+
// error interceptor) and we can serve the cached body.
|
|
75
|
+
validateStatus: (s) => (s >= 200 && s < 300) || s === 304,
|
|
76
|
+
});
|
|
77
|
+
if (res.status === 304 && cached) {
|
|
78
|
+
etagTouch(key, cached); // mark as recently used
|
|
79
|
+
return cached.data;
|
|
80
|
+
}
|
|
81
|
+
const etag = res.headers?.etag;
|
|
82
|
+
if (etag) {
|
|
83
|
+
etagTouch(key, { etag, data: res.data });
|
|
84
|
+
}
|
|
85
|
+
return res.data;
|
|
86
|
+
};
|
|
57
87
|
return {
|
|
58
88
|
get: async (url, config) => {
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
url,
|
|
63
|
-
...config,
|
|
64
|
-
});
|
|
65
|
-
return res.data;
|
|
66
|
-
}
|
|
67
|
-
const key = etagKey(url, config?.params);
|
|
68
|
-
const cached = etagCache.get(key);
|
|
69
|
-
const res = await instance.request({
|
|
70
|
-
method: "GET",
|
|
71
|
-
url,
|
|
72
|
-
...config,
|
|
73
|
-
headers: {
|
|
74
|
-
...config?.headers,
|
|
75
|
-
...(cached ? { "If-None-Match": cached.etag } : {}),
|
|
76
|
-
},
|
|
77
|
-
// Accept 304 so axios resolves (instead of routing it through the
|
|
78
|
-
// error interceptor) and we can serve the cached body.
|
|
79
|
-
validateStatus: (s) => (s >= 200 && s < 300) || s === 304,
|
|
80
|
-
});
|
|
81
|
-
if (res.status === 304 && cached) {
|
|
82
|
-
etagTouch(key, cached); // mark as recently used
|
|
83
|
-
return cached.data;
|
|
84
|
-
}
|
|
85
|
-
const etag = res.headers?.etag;
|
|
86
|
-
if (etag) {
|
|
87
|
-
etagTouch(key, { etag, data: res.data });
|
|
88
|
-
}
|
|
89
|
+
if (etagEnabled)
|
|
90
|
+
return etagGet({ method: "GET", url, ...config });
|
|
91
|
+
const res = await instance.request({ method: "GET", url, ...config });
|
|
89
92
|
return res.data;
|
|
90
93
|
},
|
|
91
94
|
delete: async (url, config) => {
|
|
@@ -109,6 +112,12 @@ export const fetcher_axios = (params = {}) => {
|
|
|
109
112
|
return res.data;
|
|
110
113
|
},
|
|
111
114
|
request: async (params) => {
|
|
115
|
+
// The paginator issues its page requests through `request` (method
|
|
116
|
+
// omitted → axios defaults to GET), so route GETs through the ETag cache
|
|
117
|
+
// too — otherwise lists would never be cached.
|
|
118
|
+
const method = (params.method ?? "GET").toString().toUpperCase();
|
|
119
|
+
if (etagEnabled && method === "GET")
|
|
120
|
+
return etagGet(params);
|
|
112
121
|
const res = await instance.request(params);
|
|
113
122
|
return res.data;
|
|
114
123
|
},
|