@neetru/sdk 3.1.13 → 3.1.14

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.
Files changed (67) hide show
  1. package/dist/ai.cjs.map +1 -1
  2. package/dist/ai.d.cts +1 -1
  3. package/dist/ai.d.ts +1 -1
  4. package/dist/ai.mjs.map +1 -1
  5. package/dist/auth.cjs +106 -9
  6. package/dist/auth.cjs.map +1 -1
  7. package/dist/auth.d.cts +1 -1
  8. package/dist/auth.d.ts +1 -1
  9. package/dist/auth.mjs +106 -9
  10. package/dist/auth.mjs.map +1 -1
  11. package/dist/catalog.cjs.map +1 -1
  12. package/dist/catalog.d.cts +1 -1
  13. package/dist/catalog.d.ts +1 -1
  14. package/dist/catalog.mjs.map +1 -1
  15. package/dist/checkout.cjs.map +1 -1
  16. package/dist/checkout.d.cts +1 -1
  17. package/dist/checkout.d.ts +1 -1
  18. package/dist/checkout.mjs.map +1 -1
  19. package/dist/db-react.d.cts +1 -1
  20. package/dist/db-react.d.ts +1 -1
  21. package/dist/db.cjs.map +1 -1
  22. package/dist/db.d.cts +1 -1
  23. package/dist/db.d.ts +1 -1
  24. package/dist/db.mjs.map +1 -1
  25. package/dist/entitlements.cjs.map +1 -1
  26. package/dist/entitlements.d.cts +1 -1
  27. package/dist/entitlements.d.ts +1 -1
  28. package/dist/entitlements.mjs.map +1 -1
  29. package/dist/errors.cjs.map +1 -1
  30. package/dist/errors.mjs.map +1 -1
  31. package/dist/firestore-compat.d.cts +1 -1
  32. package/dist/firestore-compat.d.ts +1 -1
  33. package/dist/index.cjs +107 -10
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.cts +2 -2
  36. package/dist/index.d.ts +2 -2
  37. package/dist/index.mjs +107 -10
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/mocks.cjs.map +1 -1
  40. package/dist/mocks.d.cts +1 -1
  41. package/dist/mocks.d.ts +1 -1
  42. package/dist/mocks.mjs.map +1 -1
  43. package/dist/notifications.cjs.map +1 -1
  44. package/dist/notifications.d.cts +1 -1
  45. package/dist/notifications.d.ts +1 -1
  46. package/dist/notifications.mjs.map +1 -1
  47. package/dist/react.d.cts +1 -1
  48. package/dist/react.d.ts +1 -1
  49. package/dist/support.cjs.map +1 -1
  50. package/dist/support.d.cts +1 -1
  51. package/dist/support.d.ts +1 -1
  52. package/dist/support.mjs.map +1 -1
  53. package/dist/telemetry.cjs.map +1 -1
  54. package/dist/telemetry.d.cts +1 -1
  55. package/dist/telemetry.d.ts +1 -1
  56. package/dist/telemetry.mjs.map +1 -1
  57. package/dist/{types-DJAGWjf0.d.cts → types-PzHvWMbV.d.cts} +6 -3
  58. package/dist/{types-BtQv-s0m.d.ts → types-iLDjiqqk.d.ts} +6 -3
  59. package/dist/usage.cjs.map +1 -1
  60. package/dist/usage.d.cts +1 -1
  61. package/dist/usage.d.ts +1 -1
  62. package/dist/usage.mjs.map +1 -1
  63. package/dist/webhooks.cjs.map +1 -1
  64. package/dist/webhooks.d.cts +1 -1
  65. package/dist/webhooks.d.ts +1 -1
  66. package/dist/webhooks.mjs.map +1 -1
  67. package/package.json +1 -1
package/dist/auth.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { T as NeetruClientConfig, S as NeetruClient } from './types-DJAGWjf0.cjs';
1
+ import { T as NeetruClientConfig, S as NeetruClient } from './types-PzHvWMbV.cjs';
2
2
  import 'drizzle-orm/node-postgres';
3
3
  import './errors.cjs';
4
4
 
package/dist/auth.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { T as NeetruClientConfig, S as NeetruClient } from './types-BtQv-s0m.js';
1
+ import { T as NeetruClientConfig, S as NeetruClient } from './types-iLDjiqqk.js';
2
2
  import 'drizzle-orm/node-postgres';
3
3
  import './errors.js';
4
4
 
package/dist/auth.mjs CHANGED
@@ -753,6 +753,7 @@ function isInsecureBypassActive() {
753
753
 
754
754
  // src/types.ts
755
755
  var DEFAULT_BASE_URL = "https://api.neetru.com";
756
+ var DEV_LOCAL_BASE_URL = "http://localhost:9003";
756
757
 
757
758
  // src/catalog.ts
758
759
  init_errors();
@@ -6087,7 +6088,7 @@ function resolveEnv(configEnv) {
6087
6088
  try {
6088
6089
  const proc = globalThis.process;
6089
6090
  const val = proc?.env?.NEETRU_ENV;
6090
- if (val === "dev" || val === "workspace" || val === "prod") return val;
6091
+ if (val === "dev" || val === "workspace" || val === "prod" || val === "dev-local") return val;
6091
6092
  } catch {
6092
6093
  }
6093
6094
  try {
@@ -6173,9 +6174,11 @@ function cleanCallbackUrl(parsed) {
6173
6174
  }
6174
6175
  function createOidcAuthNamespace(config) {
6175
6176
  const STORAGE_KEY = "neetru_id_token";
6177
+ const REFRESH_STORAGE_KEY = "neetru_refresh_token";
6176
6178
  const listeners = /* @__PURE__ */ new Set();
6177
6179
  let cachedUser = null;
6178
6180
  let _expiryTimerId = null;
6181
+ let _refreshInFlight = null;
6179
6182
  function getStorage() {
6180
6183
  try {
6181
6184
  return typeof globalThis.localStorage !== "undefined" ? globalThis.localStorage : null;
@@ -6228,6 +6231,7 @@ function createOidcAuthNamespace(config) {
6228
6231
  const storage2 = getStorage();
6229
6232
  storage2?.removeItem(STORAGE_KEY);
6230
6233
  notify();
6234
+ attemptSilentRefresh();
6231
6235
  return null;
6232
6236
  }
6233
6237
  return cachedUser;
@@ -6235,14 +6239,18 @@ function createOidcAuthNamespace(config) {
6235
6239
  const storage = getStorage();
6236
6240
  if (!storage) return null;
6237
6241
  const token = storage.getItem(STORAGE_KEY);
6238
- if (!token) return null;
6242
+ if (!token) {
6243
+ attemptSilentRefresh();
6244
+ return null;
6245
+ }
6239
6246
  const user = tokenToUser(token);
6240
6247
  if (!user) {
6241
6248
  storage.removeItem(STORAGE_KEY);
6249
+ attemptSilentRefresh();
6242
6250
  return null;
6243
6251
  }
6244
6252
  cachedUser = user;
6245
- scheduleExpiryTimer(user);
6253
+ scheduleTokenRefresh(user);
6246
6254
  return cachedUser;
6247
6255
  }
6248
6256
  function notify() {
@@ -6259,12 +6267,93 @@ function createOidcAuthNamespace(config) {
6259
6267
  _expiryTimerId = null;
6260
6268
  }
6261
6269
  }
6262
- function scheduleExpiryTimer(user) {
6270
+ function getRefreshToken() {
6271
+ return getStorage()?.getItem(REFRESH_STORAGE_KEY) ?? null;
6272
+ }
6273
+ function clearSession() {
6274
+ cachedUser = null;
6275
+ const storage = getStorage();
6276
+ storage?.removeItem(STORAGE_KEY);
6277
+ storage?.removeItem(REFRESH_STORAGE_KEY);
6278
+ notify();
6279
+ }
6280
+ function refreshTokens() {
6281
+ if (_refreshInFlight) return _refreshInFlight;
6282
+ _refreshInFlight = (async () => {
6283
+ const refreshToken = getRefreshToken();
6284
+ if (!refreshToken) return false;
6285
+ let clientId;
6286
+ try {
6287
+ clientId = resolveClientId();
6288
+ } catch {
6289
+ return false;
6290
+ }
6291
+ const idpOrigin = resolveIdpOrigin(config.baseUrl);
6292
+ const tokenUrl = `${idpOrigin}/api/v1/oauth/token`;
6293
+ const form = new URLSearchParams({
6294
+ grant_type: "refresh_token",
6295
+ refresh_token: refreshToken,
6296
+ client_id: clientId
6297
+ });
6298
+ let res;
6299
+ try {
6300
+ res = await config.fetch(tokenUrl, {
6301
+ method: "POST",
6302
+ headers: { "content-type": "application/x-www-form-urlencoded" },
6303
+ body: form.toString()
6304
+ });
6305
+ } catch {
6306
+ return false;
6307
+ }
6308
+ let payload = null;
6309
+ try {
6310
+ payload = await res.json();
6311
+ } catch {
6312
+ payload = null;
6313
+ }
6314
+ if (!res.ok || typeof payload?.error === "string") return false;
6315
+ const idToken = payload?.id_token;
6316
+ if (typeof idToken !== "string" || idToken.length === 0) return false;
6317
+ const user = tokenToUser(idToken);
6318
+ if (!user) return false;
6319
+ const storage = getStorage();
6320
+ storage?.setItem(STORAGE_KEY, idToken);
6321
+ const rotated = payload?.refresh_token;
6322
+ if (typeof rotated === "string" && rotated.length > 0) {
6323
+ storage?.setItem(REFRESH_STORAGE_KEY, rotated);
6324
+ }
6325
+ cachedUser = user;
6326
+ scheduleTokenRefresh(user);
6327
+ notify();
6328
+ return true;
6329
+ })().finally(() => {
6330
+ _refreshInFlight = null;
6331
+ });
6332
+ return _refreshInFlight;
6333
+ }
6334
+ function attemptSilentRefresh() {
6335
+ if (!getRefreshToken()) return;
6336
+ void refreshTokens().then((ok) => {
6337
+ if (!ok) clearSession();
6338
+ });
6339
+ }
6340
+ function scheduleTokenRefresh(user) {
6263
6341
  cancelExpiryTimer();
6264
6342
  const exp = user.exp;
6265
6343
  if (typeof exp !== "number") return;
6266
6344
  const msUntilExpiry = exp * 1e3 - Date.now();
6267
6345
  if (msUntilExpiry <= 0) return;
6346
+ if (getRefreshToken()) {
6347
+ const lead = Math.min(msUntilExpiry, Math.max(3e4, msUntilExpiry * 0.2));
6348
+ const delay = Math.max(1, msUntilExpiry - lead);
6349
+ _expiryTimerId = setTimeout(() => {
6350
+ _expiryTimerId = null;
6351
+ void refreshTokens().then((ok) => {
6352
+ if (!ok) clearSession();
6353
+ });
6354
+ }, delay);
6355
+ return;
6356
+ }
6268
6357
  _expiryTimerId = setTimeout(() => {
6269
6358
  _expiryTimerId = null;
6270
6359
  loadCachedUser();
@@ -6406,9 +6495,14 @@ function createOidcAuthNamespace(config) {
6406
6495
  "id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
6407
6496
  );
6408
6497
  }
6409
- getStorage()?.setItem(STORAGE_KEY, idToken);
6498
+ const storage = getStorage();
6499
+ storage?.setItem(STORAGE_KEY, idToken);
6500
+ const refreshToken = payload?.refresh_token;
6501
+ if (typeof refreshToken === "string" && refreshToken.length > 0) {
6502
+ storage?.setItem(REFRESH_STORAGE_KEY, refreshToken);
6503
+ }
6410
6504
  cachedUser = user;
6411
- scheduleExpiryTimer(user);
6505
+ scheduleTokenRefresh(user);
6412
6506
  notify();
6413
6507
  return { user, postLoginRedirect: tx.postLoginRedirect };
6414
6508
  }
@@ -6467,7 +6561,10 @@ function createOidcAuthNamespace(config) {
6467
6561
  async signOut() {
6468
6562
  const storage = getStorage();
6469
6563
  const storedToken = storage?.getItem(STORAGE_KEY) ?? null;
6470
- if (storage) storage.removeItem(STORAGE_KEY);
6564
+ if (storage) {
6565
+ storage.removeItem(STORAGE_KEY);
6566
+ storage.removeItem(REFRESH_STORAGE_KEY);
6567
+ }
6471
6568
  cancelExpiryTimer();
6472
6569
  cachedUser = null;
6473
6570
  const overrideAuthUrl = typeof globalThis.NEETRU_AUTH_URL === "string" ? globalThis.NEETRU_AUTH_URL : null;
@@ -6571,10 +6668,10 @@ function createNeetruClient(config = {}) {
6571
6668
  "fetch is not available in this runtime. Pass `fetch` explicitly to createNeetruClient."
6572
6669
  );
6573
6670
  }
6574
- const rawBaseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
6575
- const baseUrl = validateBaseUrl(rawBaseUrl, config.allowedHosts ?? []);
6576
6671
  const apiKey = config.apiKey ?? readEnvApiKey();
6577
6672
  const env = resolveEnv(config.env);
6673
+ const rawBaseUrl = config.baseUrl ?? (env === "dev-local" ? DEV_LOCAL_BASE_URL : DEFAULT_BASE_URL);
6674
+ const baseUrl = validateBaseUrl(rawBaseUrl, config.allowedHosts ?? []);
6578
6675
  const resolved = Object.freeze({
6579
6676
  apiKey,
6580
6677
  oidcClientId: config.oidcClientId,