@onyx.dev/onyx-database 2.0.1 → 2.1.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/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // package.json
4
4
  var name = "@onyx.dev/onyx-database";
5
- var version = "2.0.1";
5
+ var version = "2.1.0";
6
6
 
7
7
  // src/config/defaults.ts
8
8
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
@@ -2125,20 +2125,38 @@ var AiChatClientImpl = class {
2125
2125
  }
2126
2126
  };
2127
2127
  function createOnyxFacade(resolveConfig2) {
2128
- let cachedCfg = null;
2128
+ const cachedCfgs = /* @__PURE__ */ new Map();
2129
+ const cacheKey = (databaseId, apiKey) => {
2130
+ const id = typeof databaseId === "string" && databaseId.trim() !== "" ? databaseId.trim() : null;
2131
+ const key = typeof apiKey === "string" && apiKey.trim() !== "" ? apiKey.trim() : null;
2132
+ return id && key ? `${id}-${key}` : null;
2133
+ };
2129
2134
  function resolveConfigWithCache(config) {
2130
2135
  const ttl = config?.ttl ?? DEFAULT_CACHE_TTL;
2131
2136
  const now = Date.now();
2132
- if (cachedCfg && cachedCfg.expires > now) {
2133
- return cachedCfg.promise;
2137
+ const hintKey = cacheKey(config?.databaseId, config?.apiKey) ?? "__default__";
2138
+ const existing = cachedCfgs.get(hintKey);
2139
+ if (existing && existing.expires > now) {
2140
+ return existing.promise;
2134
2141
  }
2142
+ if (existing) cachedCfgs.delete(hintKey);
2135
2143
  const { ttl: _ttl, requestLoggingEnabled: _reqLog, responseLoggingEnabled: _resLog, ...rest } = config ?? {};
2136
- const promise = resolveConfig2(rest);
2137
- cachedCfg = { promise, expires: now + ttl };
2144
+ const expires = now + ttl;
2145
+ const promise = resolveConfig2(rest).then((resolved) => {
2146
+ const resolvedKey = cacheKey(resolved.databaseId, resolved.apiKey) ?? hintKey;
2147
+ const nextExpires = Date.now() + ttl;
2148
+ cachedCfgs.set(resolvedKey, { promise, expires: nextExpires });
2149
+ if (resolvedKey !== hintKey) {
2150
+ const stale = cachedCfgs.get(hintKey);
2151
+ if (stale && stale.promise === promise) cachedCfgs.delete(hintKey);
2152
+ }
2153
+ return resolved;
2154
+ });
2155
+ cachedCfgs.set(hintKey, { promise, expires });
2138
2156
  return promise;
2139
2157
  }
2140
2158
  function clearCacheConfig() {
2141
- cachedCfg = null;
2159
+ cachedCfgs.clear();
2142
2160
  }
2143
2161
  return {
2144
2162
  init(config) {