@faable/sdk-base 1.3.0 → 1.4.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.
package/dist/FaableApi.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { AuthInterface } from "./types/AuthInterface.js";
|
|
2
2
|
import type { Fetcher, FetcherCreateParams } from "./fetcher/Fetcher.js";
|
|
3
3
|
import type { Paginator, PaginatorOptions } from "./helpers/paginator.js";
|
|
4
|
+
export type RequestObserver = (info: {
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
status: number;
|
|
8
|
+
durationMs: number;
|
|
9
|
+
}) => void;
|
|
4
10
|
export type ApiParams = {
|
|
5
11
|
baseURL?: string;
|
|
6
12
|
fetcher?: FetcherCreateParams;
|
|
@@ -22,6 +28,13 @@ export type ApiParams = {
|
|
|
22
28
|
etagCache?: boolean | {
|
|
23
29
|
maxEntries?: number;
|
|
24
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Pluggable per-response observer. Called once per completed HTTP response
|
|
33
|
+
* with `{ method, url, status, durationMs }`. Use it to collect metrics
|
|
34
|
+
* (calls per endpoint, `304` ETag-hit rate, latency, …). Errors thrown by the
|
|
35
|
+
* observer are swallowed so instrumentation can never break a request.
|
|
36
|
+
*/
|
|
37
|
+
requestObserver?: RequestObserver;
|
|
25
38
|
};
|
|
26
39
|
export declare abstract class FaableApi<Params extends ApiParams = ApiParams> {
|
|
27
40
|
fetcher: Fetcher;
|
package/dist/FaableApi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FaableApi.d.ts","sourceRoot":"","sources":["../src/FaableApi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAGzE,OAAO,KAAK,EAAE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,GAAG,GAAG,EACtE,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,MAAM,GACd,YAAY,CAAC,CAAC,CAAC;CAInB"}
|
|
@@ -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,OAmJtD,CAAC"}
|
|
@@ -22,6 +22,9 @@ export const fetcher_axios = (params = {}) => {
|
|
|
22
22
|
instance.interceptors.request.use(async (req) => {
|
|
23
23
|
const auth_data = params.auth && (await params.auth.hook());
|
|
24
24
|
req.headers.set(auth_data?.headers || {});
|
|
25
|
+
// Stamp a start time so the response interceptor can report latency to the
|
|
26
|
+
// pluggable observer.
|
|
27
|
+
req.__startTime = Date.now();
|
|
25
28
|
return req;
|
|
26
29
|
});
|
|
27
30
|
// Add base interceptor
|
|
@@ -30,6 +33,21 @@ export const fetcher_axios = (params = {}) => {
|
|
|
30
33
|
const queryparams = new URLSearchParams(res.config.params);
|
|
31
34
|
console.log(`[${res.status}] ${res.config.url}${queryparams.size > 0 ? `?${queryparams.toString()}` : ""}`);
|
|
32
35
|
}
|
|
36
|
+
// Pluggable per-response hook (metrics, etc.). Never let it break a request.
|
|
37
|
+
if (params.requestObserver) {
|
|
38
|
+
try {
|
|
39
|
+
const start = res.config.__startTime;
|
|
40
|
+
params.requestObserver({
|
|
41
|
+
method: (res.config.method ?? "get").toUpperCase(),
|
|
42
|
+
url: res.config.url ?? "",
|
|
43
|
+
status: res.status,
|
|
44
|
+
durationMs: start ? Date.now() - start : 0,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// ignore observer errors
|
|
49
|
+
}
|
|
50
|
+
}
|
|
33
51
|
return res;
|
|
34
52
|
}, handleErrorInterceptor);
|
|
35
53
|
// Opt-in in-memory ETag cache (per fetcher instance). Keyed by url + params
|