@faable/sdk-base 1.1.3 → 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,12 +1,27 @@
1
- import { AuthInterface } from "./types/AuthInterface.js";
2
- import { Fetcher, FetcherCreateParams } from "./fetcher/Fetcher.js";
3
- import { Paginator, PaginatorOptions } from "./helpers/paginator.js";
1
+ import type { AuthInterface } from "./types/AuthInterface.js";
2
+ import type { Fetcher, FetcherCreateParams } from "./fetcher/Fetcher.js";
3
+ import type { Paginator, PaginatorOptions } from "./helpers/paginator.js";
4
4
  export type ApiParams = {
5
5
  baseURL?: string;
6
6
  fetcher?: FetcherCreateParams;
7
7
  auth?: AuthInterface<any, any>;
8
8
  debug?: boolean;
9
9
  paginator?: Partial<PaginatorOptions>;
10
+ /**
11
+ * Opt-in in-memory ETag cache for GET requests. When enabled, the fetcher
12
+ * remembers each GET's `ETag` and replays it as `If-None-Match`; a `304 Not
13
+ * Modified` reuses the previously parsed body instead of re-downloading it.
14
+ * Correctness relies on the server changing the ETag whenever the resource
15
+ * changes (no client-side invalidation needed — a write bumps the server
16
+ * ETag, so the next GET gets a fresh `200`). Off by default.
17
+ *
18
+ * `true` uses the default cap; pass `{ maxEntries }` to size the bounded LRU
19
+ * for the client's key cardinality (e.g. a controller polling many apps +
20
+ * deployments wants a larger cap than a one-shot script).
21
+ */
22
+ etagCache?: boolean | {
23
+ maxEntries?: number;
24
+ };
10
25
  };
11
26
  export declare abstract class FaableApi<Params extends ApiParams = ApiParams> {
12
27
  fetcher: Fetcher;
@@ -1 +1 @@
1
- {"version":3,"file":"FaableApi.d.ts","sourceRoot":"","sources":["../src/FaableApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAEpE,OAAO,EAEL,SAAS,EACT,gBAAgB,EACjB,MA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AWG;IACH,SAAS,CAAC,EAAE,OAAO,GAAG;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/C,CAAC;AAEF,8BAAsB,SAAS,CAAC,MAAM,SAAS,SAAS,GAAG,SAAS;IAClE,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC;IAC/B,SAAS,aAAa,MAAM,CAAC,EAAE,MAAM;IAMrC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,QAAQ,MAAM,IAAI,EAAE,MAAM,KAAK,GAAG,EAAE,MAAM,GAAG,GAAG,EACtE,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,MAAM,GACd,YAAY,CAAC,CAAC,CAAC;CAInB"}
package/dist/FaableApi.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { fetcher_axios } from "./fetcher/fetcher_axios.js";
2
- import { buildPaginator, } from "./helpers/paginator.js";
2
+ import { buildPaginator } from "./helpers/paginator.js";
3
3
  export class FaableApi {
4
4
  fetcher;
5
5
  paginator;
@@ -1,4 +1,4 @@
1
- import { StrategyInterface } from "../types/StrategyInterface.js";
1
+ import type { StrategyInterface } from "../types/StrategyInterface.js";
2
2
  export type ApikeyAuthParams = Partial<[apikey: string]>;
3
3
  export declare const createApikeyAuth: StrategyInterface<ApikeyAuthParams>;
4
4
  //# sourceMappingURL=apikey.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"apikey.d.ts","sourceRoot":"","sources":["../../src/auth/apikey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAGlE,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEzD,eAAO,MAAM,gBAAgB,EAAE,iBAAiB,CAAC,gBAAgB,CAoBhE,CAAC"}
1
+ {"version":3,"file":"apikey.d.ts","sourceRoot":"","sources":["../../src/auth/apikey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAGvE,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEzD,eAAO,MAAM,gBAAgB,EAAE,iBAAiB,CAAC,gBAAgB,CAoBhE,CAAC"}
@@ -1,4 +1,4 @@
1
- import { StrategyInterface } from "../../types/StrategyInterface.js";
1
+ import type { StrategyInterface } from "../../types/StrategyInterface.js";
2
2
  type Params = {
3
3
  client_id: string;
4
4
  client_secret: string;
@@ -1 +1 @@
1
- {"version":3,"file":"client_credentials.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/client_credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAKrE,KAAK,MAAM,GAAG;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAWrE,eAAO,MAAM,uBAAuB,EAAE,iBAAiB,CACrD,2BAA2B,CAgG5B,CAAC"}
1
+ {"version":3,"file":"client_credentials.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/client_credentials.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAK1E,KAAK,MAAM,GAAG;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAWrE,eAAO,MAAM,uBAAuB,EAAE,iBAAiB,CACrD,2BAA2B,CAgG5B,CAAC"}
@@ -1,4 +1,4 @@
1
- import { ClientCredentialsResponse } from "./types.js";
1
+ import type { ClientCredentialsResponse } from "./types.js";
2
2
  type TokenStoreOptions = {
3
3
  debug?: boolean;
4
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvD,KAAK,iBAAiB,GAAG;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,gBAAgB,eAAe,iBAAiB;kCAStB,yBAAyB;kBAPlD,yBAAyB;aAC9B,MAAM;;;kBADD,yBAAyB;aAC9B,MAAM;;CAkBd,CAAC"}
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAE5D,KAAK,iBAAiB,GAAG;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,gBAAgB,eAAe,iBAAiB;kCAStB,yBAAyB;kBAPlD,yBAAyB;aAC9B,MAAM;;;kBADD,yBAAyB;aAC9B,MAAM;;CAkBd,CAAC"}
@@ -1,4 +1,4 @@
1
- import { Fetcher } from "./Fetcher.js";
2
- import { ApiParams } from "../FaableApi.js";
1
+ import type { Fetcher } from "./Fetcher.js";
2
+ import type { ApiParams } from "../FaableApi.js";
3
3
  export declare const fetcher_axios: (params?: ApiParams) => Fetcher;
4
4
  //# sourceMappingURL=fetcher_axios.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetcher_axios.d.ts","sourceRoot":"","sources":["../../src/fetcher/fetcher_axios.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAiB5C,eAAO,MAAM,aAAa,YAAY,SAAS,KAAQ,OAwEtD,CAAC"}
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"}
@@ -1,4 +1,4 @@
1
- import axios from "axios";
1
+ import axios, { AxiosError } from "axios";
2
2
  import { FaableApiError } from "../helpers/FaableApiError.js";
3
3
  const handleErrorInterceptor = async (e) => {
4
4
  if (!e.isAxiosError) {
@@ -32,13 +32,63 @@ export const fetcher_axios = (params = {}) => {
32
32
  }
33
33
  return res;
34
34
  }, handleErrorInterceptor);
35
+ // Opt-in in-memory ETag cache (per fetcher instance). Keyed by url + params
36
+ // so each list cursor / expanded GET caches independently. Server-driven
37
+ // invalidation only — a changed resource changes its ETag, so the
38
+ // conditional request returns a fresh 200 instead of 304.
39
+ //
40
+ // Bounded LRU: a long-lived client keeps minting distinct keys (e.g. a new
41
+ // `GET /deployment/:id` per deploy), so an unbounded map would leak. The Map
42
+ // preserves insertion order; every access re-inserts its key (moving it to
43
+ // the most-recent end) and inserts past the cap evict the least-recently-used
44
+ // (oldest) entry.
45
+ const etagOpt = params.etagCache;
46
+ const etagEnabled = etagOpt === true || (typeof etagOpt === "object" && etagOpt !== null);
47
+ const ETAG_CACHE_MAX = (typeof etagOpt === "object" && etagOpt?.maxEntries) || 1000;
48
+ const etagCache = new Map();
49
+ const etagKey = (url, p) => `${url}::${p ? JSON.stringify(p) : ""}`;
50
+ const etagTouch = (key, entry) => {
51
+ etagCache.delete(key);
52
+ etagCache.set(key, entry);
53
+ if (etagCache.size > ETAG_CACHE_MAX) {
54
+ etagCache.delete(etagCache.keys().next().value);
55
+ }
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
+ };
35
87
  return {
36
88
  get: async (url, config) => {
37
- const res = await instance.request({
38
- method: "GET",
39
- url,
40
- ...config,
41
- });
89
+ if (etagEnabled)
90
+ return etagGet({ method: "GET", url, ...config });
91
+ const res = await instance.request({ method: "GET", url, ...config });
42
92
  return res.data;
43
93
  },
44
94
  delete: async (url, config) => {
@@ -62,6 +112,12 @@ export const fetcher_axios = (params = {}) => {
62
112
  return res.data;
63
113
  },
64
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);
65
121
  const res = await instance.request(params);
66
122
  return res.data;
67
123
  },
@@ -1,4 +1,4 @@
1
- import { FetcherResponse } from "../fetcher/Fetcher.js";
1
+ import type { FetcherResponse } from "../fetcher/Fetcher.js";
2
2
  type Status = "not-found" | "created" | "ok" | "unauthorized" | "bad-request" | "internal-server-error" | "service-unavailable" | "bad-gateway";
3
3
  export declare const build_error_message: (message?: string, response?: FetcherResponse) => string;
4
4
  export declare class FaableApiError extends Error {
@@ -1 +1 @@
1
- {"version":3,"file":"FaableApiError.d.ts","sourceRoot":"","sources":["../../src/helpers/FaableApiError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,KAAK,MAAM,GACP,WAAW,GACX,SAAS,GACT,IAAI,GACJ,cAAc,GACd,aAAa,GACb,uBAAuB,GACvB,qBAAqB,GACrB,aAAa,CAAC;AAYlB,eAAO,MAAM,mBAAmB,aACpB,MAAM,aACL,eAAe,WAO3B,CAAC;AAEF,qBAAa,cAAe,SAAQ,KAAK;IACvC,gBAAgB,UAAQ;IACxB,QAAQ,CAAC,EAAE,eAAe,CAAC;gBACf,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,eAAe;IAOxD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAI/B"}
1
+ {"version":3,"file":"FaableApiError.d.ts","sourceRoot":"","sources":["../../src/helpers/FaableApiError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,KAAK,MAAM,GACP,WAAW,GACX,SAAS,GACT,IAAI,GACJ,cAAc,GACd,aAAa,GACb,uBAAuB,GACvB,qBAAqB,GACrB,aAAa,CAAC;AAYlB,eAAO,MAAM,mBAAmB,aACpB,MAAM,aACL,eAAe,WAO3B,CAAC;AAEF,qBAAa,cAAe,SAAQ,KAAK;IACvC,gBAAgB,UAAQ;IACxB,QAAQ,CAAC,EAAE,eAAe,CAAC;gBACf,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,eAAe;IAOxD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAI/B"}
@@ -1,4 +1,4 @@
1
- import { Fetcher, FetcherRequestParams } from "../fetcher/Fetcher.js";
1
+ import type { Fetcher, FetcherRequestParams } from "../fetcher/Fetcher.js";
2
2
  export interface Page<T> {
3
3
  next: string | null;
4
4
  results: T[];
@@ -1 +1 @@
1
- {"version":3,"file":"paginator.d.ts","sourceRoot":"","sources":["../../src/helpers/paginator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAGtE,MAAM,WAAW,IAAI,CAAC,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,CAAC,EAAE,CAAC;CACd;AACD,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAE1D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,eAAO,MAAM,cAAc,YAChB,OAAO,YACN,OAAO,CAAC,gBAAgB,CAAC,MAO3B,CAAC,OAAO,oBAAoB;;;oBAoCX;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE;CAWhE,CAAC"}
1
+ {"version":3,"file":"paginator.d.ts","sourceRoot":"","sources":["../../src/helpers/paginator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAG3E,MAAM,WAAW,IAAI,CAAC,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,CAAC,EAAE,CAAC;CACd;AACD,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAE1D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,eAAO,MAAM,cAAc,YAChB,OAAO,YACN,OAAO,CAAC,gBAAgB,CAAC,MAO3B,CAAC,OAAO,oBAAoB;;;oBAoCX;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE;CAWhE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/sdk-base",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
4
4
  "author": "Marc Pomar <marc@faable.com>",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",