@djangocfg/monitor 2.1.331 → 2.1.332

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.mjs CHANGED
@@ -368,15 +368,15 @@ function getValidRequestBody(options) {
368
368
  __name(getValidRequestBody, "getValidRequestBody");
369
369
 
370
370
  // src/_api/generated/core/auth.gen.ts
371
- var getAuthToken = /* @__PURE__ */ __name(async (auth, callback) => {
372
- const token = typeof callback === "function" ? await callback(auth) : callback;
371
+ var getAuthToken = /* @__PURE__ */ __name(async (auth2, callback) => {
372
+ const token = typeof callback === "function" ? await callback(auth2) : callback;
373
373
  if (!token) {
374
374
  return;
375
375
  }
376
- if (auth.scheme === "bearer") {
376
+ if (auth2.scheme === "bearer") {
377
377
  return `Bearer ${token}`;
378
378
  }
379
- if (auth.scheme === "basic") {
379
+ if (auth2.scheme === "basic") {
380
380
  return `Basic ${btoa(token)}`;
381
381
  }
382
382
  return token;
@@ -465,16 +465,16 @@ var setAuthParams = /* @__PURE__ */ __name(async ({
465
465
  security,
466
466
  ...options
467
467
  }) => {
468
- for (const auth of security) {
469
- if (checkForExistence(options, auth.name)) {
468
+ for (const auth2 of security) {
469
+ if (checkForExistence(options, auth2.name)) {
470
470
  continue;
471
471
  }
472
- const token = await getAuthToken(auth, options.auth);
472
+ const token = await getAuthToken(auth2, options.auth);
473
473
  if (!token) {
474
474
  continue;
475
475
  }
476
- const name = auth.name ?? "Authorization";
477
- switch (auth.in) {
476
+ const name = auth2.name ?? "Authorization";
477
+ switch (auth2.in) {
478
478
  case "query":
479
479
  if (!options.query) {
480
480
  options.query = {};
@@ -803,59 +803,206 @@ var createClient = /* @__PURE__ */ __name((config = {}) => {
803
803
  // src/_api/generated/client.gen.ts
804
804
  var client = createClient(createConfig({ baseUrl: "http://localhost:8000" }));
805
805
 
806
- // src/_api/generated/helpers/storage.ts
807
- var _LocalStorageAdapter = class _LocalStorageAdapter {
808
- getItem(key) {
809
- if (typeof window === "undefined") return null;
806
+ // src/_api/generated/helpers/auth.ts
807
+ var ACCESS_KEY = "cfg.access_token";
808
+ var REFRESH_KEY = "cfg.refresh_token";
809
+ var API_KEY_KEY = "cfg.api_key";
810
+ var isBrowser = typeof window !== "undefined";
811
+ var localStorageBackend = {
812
+ get(key) {
813
+ if (!isBrowser) return null;
810
814
  try {
811
815
  return window.localStorage.getItem(key);
812
816
  } catch {
813
817
  return null;
814
818
  }
815
- }
816
- setItem(key, value) {
817
- if (typeof window === "undefined") return;
819
+ },
820
+ set(key, value) {
821
+ if (!isBrowser) return;
818
822
  try {
819
- window.localStorage.setItem(key, value);
823
+ if (value === null) window.localStorage.removeItem(key);
824
+ else window.localStorage.setItem(key, value);
820
825
  } catch {
821
826
  }
822
827
  }
823
- removeItem(key) {
824
- if (typeof window === "undefined") return;
828
+ };
829
+ var COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
830
+ var cookieBackend = {
831
+ get(key) {
832
+ if (!isBrowser) return null;
825
833
  try {
826
- window.localStorage.removeItem(key);
834
+ const re = new RegExp(`(?:^|;\\s*)${encodeURIComponent(key)}=([^;]*)`);
835
+ const m = document.cookie.match(re);
836
+ return m ? decodeURIComponent(m[1]) : null;
827
837
  } catch {
838
+ return null;
828
839
  }
829
- }
830
- clear() {
831
- if (typeof window === "undefined") return;
840
+ },
841
+ set(key, value) {
842
+ if (!isBrowser) return;
832
843
  try {
833
- window.localStorage.clear();
844
+ const k = encodeURIComponent(key);
845
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
846
+ if (value === null) {
847
+ document.cookie = `${k}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
848
+ } else {
849
+ const v = encodeURIComponent(value);
850
+ document.cookie = `${k}=${v}; Path=/; Max-Age=${COOKIE_MAX_AGE}; SameSite=Lax${secure}`;
851
+ }
834
852
  } catch {
835
853
  }
836
854
  }
837
855
  };
838
- __name(_LocalStorageAdapter, "LocalStorageAdapter");
839
- var LocalStorageAdapter = _LocalStorageAdapter;
840
- var _MemoryStorageAdapter = class _MemoryStorageAdapter {
841
- constructor() {
842
- __publicField(this, "store", /* @__PURE__ */ new Map());
843
- }
844
- getItem(key) {
845
- return this.store.get(key) ?? null;
856
+ var _storage = localStorageBackend;
857
+ var _storageMode = "localStorage";
858
+ function detectLocale() {
859
+ try {
860
+ if (typeof document !== "undefined") {
861
+ const m = document.cookie.match(/(?:^|;\s*)NEXT_LOCALE=([^;]*)/);
862
+ if (m) return decodeURIComponent(m[1]);
863
+ }
864
+ if (typeof navigator !== "undefined" && navigator.language) {
865
+ return navigator.language;
866
+ }
867
+ } catch {
846
868
  }
847
- setItem(key, value) {
848
- this.store.set(key, value);
869
+ return null;
870
+ }
871
+ __name(detectLocale, "detectLocale");
872
+ function defaultBaseUrl() {
873
+ try {
874
+ if (typeof process !== "undefined" && process.env) {
875
+ if (process.env.NEXT_PUBLIC_STATIC_BUILD === "true") return "";
876
+ return process.env.NEXT_PUBLIC_API_URL || "";
877
+ }
878
+ } catch {
849
879
  }
850
- removeItem(key) {
851
- this.store.delete(key);
880
+ return "";
881
+ }
882
+ __name(defaultBaseUrl, "defaultBaseUrl");
883
+ function defaultApiKey() {
884
+ try {
885
+ if (typeof process !== "undefined" && process.env?.NEXT_PUBLIC_API_KEY) {
886
+ return process.env.NEXT_PUBLIC_API_KEY;
887
+ }
888
+ } catch {
852
889
  }
853
- clear() {
854
- this.store.clear();
890
+ return null;
891
+ }
892
+ __name(defaultApiKey, "defaultApiKey");
893
+ var _localeOverride = null;
894
+ var _apiKeyOverride = null;
895
+ var _baseUrlOverride = null;
896
+ var _withCredentials = true;
897
+ var _onUnauthorized = null;
898
+ var auth = {
899
+ // ── Storage mode ──────────────────────────────────────────────────
900
+ getStorageMode() {
901
+ return _storageMode;
902
+ },
903
+ /**
904
+ * Switch the storage backend. Existing values in the *previous*
905
+ * backend are NOT migrated — set fresh values after switching.
906
+ */
907
+ setStorageMode(mode) {
908
+ _storageMode = mode;
909
+ _storage = mode === "cookie" ? cookieBackend : localStorageBackend;
910
+ },
911
+ // ── Bearer token ──────────────────────────────────────────────────
912
+ getToken() {
913
+ return _storage.get(ACCESS_KEY);
914
+ },
915
+ setToken(token) {
916
+ _storage.set(ACCESS_KEY, token);
917
+ },
918
+ getRefreshToken() {
919
+ return _storage.get(REFRESH_KEY);
920
+ },
921
+ setRefreshToken(token) {
922
+ _storage.set(REFRESH_KEY, token);
923
+ },
924
+ clearTokens() {
925
+ _storage.set(ACCESS_KEY, null);
926
+ _storage.set(REFRESH_KEY, null);
927
+ },
928
+ isAuthenticated() {
929
+ return _storage.get(ACCESS_KEY) !== null;
930
+ },
931
+ // ── API key ───────────────────────────────────────────────────────
932
+ /** In-memory API key. Falls back to storage, then NEXT_PUBLIC_API_KEY. */
933
+ getApiKey() {
934
+ return _apiKeyOverride ?? _storage.get(API_KEY_KEY) ?? defaultApiKey();
935
+ },
936
+ /** In-memory only (cleared on reload). */
937
+ setApiKey(key) {
938
+ _apiKeyOverride = key;
939
+ },
940
+ /** Persist to active storage backend (localStorage or cookie). */
941
+ setApiKeyPersist(key) {
942
+ _apiKeyOverride = key;
943
+ _storage.set(API_KEY_KEY, key);
944
+ },
945
+ clearApiKey() {
946
+ _apiKeyOverride = null;
947
+ _storage.set(API_KEY_KEY, null);
948
+ },
949
+ // ── Locale ────────────────────────────────────────────────────────
950
+ /** Override locale → falls back to NEXT_LOCALE cookie / navigator.language. */
951
+ getLocale() {
952
+ return _localeOverride ?? detectLocale();
953
+ },
954
+ setLocale(locale) {
955
+ _localeOverride = locale;
956
+ },
957
+ // ── Base URL ──────────────────────────────────────────────────────
958
+ getBaseUrl() {
959
+ const url = _baseUrlOverride ?? defaultBaseUrl();
960
+ return url.replace(/\/$/, "");
961
+ },
962
+ setBaseUrl(url) {
963
+ _baseUrlOverride = url ? url.replace(/\/$/, "") : null;
964
+ client.setConfig({ baseUrl: this.getBaseUrl() });
965
+ },
966
+ // ── Credentials toggle (Django session/CSRF cross-origin) ─────────
967
+ getWithCredentials() {
968
+ return _withCredentials;
969
+ },
970
+ setWithCredentials(value) {
971
+ _withCredentials = value;
972
+ client.setConfig({ credentials: value ? "include" : "same-origin" });
973
+ },
974
+ // ── 401 handler ───────────────────────────────────────────────────
975
+ /**
976
+ * Register a callback fired on every 401 response. Use this to wire
977
+ * a token-refresh flow or a forced logout. Setting `null` removes
978
+ * the handler.
979
+ */
980
+ onUnauthorized(cb) {
981
+ _onUnauthorized = cb;
855
982
  }
856
983
  };
857
- __name(_MemoryStorageAdapter, "MemoryStorageAdapter");
858
- var MemoryStorageAdapter = _MemoryStorageAdapter;
984
+ client.setConfig({
985
+ baseUrl: auth.getBaseUrl(),
986
+ credentials: _withCredentials ? "include" : "same-origin"
987
+ });
988
+ client.interceptors.request.use((request) => {
989
+ const token = auth.getToken();
990
+ if (token) request.headers.set("Authorization", `Bearer ${token}`);
991
+ const locale = auth.getLocale();
992
+ if (locale) request.headers.set("Accept-Language", locale);
993
+ const apiKey = auth.getApiKey();
994
+ if (apiKey) request.headers.set("X-API-Key", apiKey);
995
+ return request;
996
+ });
997
+ client.interceptors.response.use((response) => {
998
+ if (response.status === 401 && _onUnauthorized) {
999
+ try {
1000
+ _onUnauthorized(response);
1001
+ } catch {
1002
+ }
1003
+ }
1004
+ return response;
1005
+ });
859
1006
 
860
1007
  // src/_api/generated/helpers/logger.ts
861
1008
  import { createConsola } from "consola";
@@ -951,94 +1098,60 @@ var APILogger = _APILogger;
951
1098
  var defaultLogger = new APILogger();
952
1099
 
953
1100
  // src/_api/generated/_cfg_monitor/api.ts
954
- var ACCESS_KEY = "cfg.access_token";
955
- var REFRESH_KEY = "cfg.refresh_token";
956
- function detectLocale() {
957
- try {
958
- if (typeof document !== "undefined") {
959
- const m = document.cookie.match(/(?:^|;\s*)NEXT_LOCALE=([^;]*)/);
960
- if (m) return decodeURIComponent(m[1]);
961
- }
962
- if (typeof navigator !== "undefined" && navigator.language) {
963
- return navigator.language;
964
- }
965
- } catch {
966
- }
967
- return null;
968
- }
969
- __name(detectLocale, "detectLocale");
970
1101
  var _API = class _API {
971
- constructor(baseUrl, opts = {}) {
972
- __publicField(this, "baseUrl");
973
- __publicField(this, "storage");
974
- __publicField(this, "locale");
975
- __publicField(this, "apiKey");
1102
+ constructor(_baseUrl, opts = {}) {
976
1103
  __publicField(this, "logger");
977
- this.baseUrl = baseUrl.replace(/\/$/, "");
978
- this.storage = opts.storage ?? new LocalStorageAdapter();
979
1104
  this.logger = new APILogger(opts.logger);
980
- this.locale = opts.locale ?? null;
981
- this.apiKey = opts.apiKey ?? (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_API_KEY ?? null : null);
982
- const credentials = opts.withCredentials ?? true ? "include" : "same-origin";
983
- client.setConfig({ baseUrl: this.baseUrl, credentials });
984
- client.interceptors.request.use((request) => {
985
- const access = this.getToken();
986
- if (access) request.headers.set("Authorization", `Bearer ${access}`);
987
- const locale = this.locale ?? detectLocale();
988
- if (locale) request.headers.set("Accept-Language", locale);
989
- if (this.apiKey) request.headers.set("X-API-Key", this.apiKey);
990
- return request;
991
- });
1105
+ if (_baseUrl) auth.setBaseUrl(_baseUrl);
1106
+ if (opts.locale !== void 0) auth.setLocale(opts.locale);
1107
+ if (opts.apiKey !== void 0) auth.setApiKey(opts.apiKey);
1108
+ if (opts.withCredentials !== void 0) auth.setWithCredentials(opts.withCredentials);
992
1109
  }
993
1110
  // ── Base URL ────────────────────────────────────────────────────────────
994
1111
  getBaseUrl() {
995
- return this.baseUrl;
1112
+ return auth.getBaseUrl();
996
1113
  }
997
1114
  setBaseUrl(url) {
998
- this.baseUrl = url.replace(/\/$/, "");
999
- client.setConfig({ baseUrl: this.baseUrl });
1115
+ auth.setBaseUrl(url);
1000
1116
  }
1001
1117
  // ── Tokens ──────────────────────────────────────────────────────────────
1002
1118
  getToken() {
1003
- return this.storage.getItem(ACCESS_KEY);
1119
+ return auth.getToken();
1004
1120
  }
1005
1121
  setToken(token) {
1006
- if (token) this.storage.setItem(ACCESS_KEY, token);
1007
- else this.storage.removeItem(ACCESS_KEY);
1122
+ auth.setToken(token);
1008
1123
  }
1009
1124
  getRefreshToken() {
1010
- return this.storage.getItem(REFRESH_KEY);
1125
+ return auth.getRefreshToken();
1011
1126
  }
1012
1127
  setRefreshToken(token) {
1013
- if (token) this.storage.setItem(REFRESH_KEY, token);
1014
- else this.storage.removeItem(REFRESH_KEY);
1128
+ auth.setRefreshToken(token);
1015
1129
  }
1016
1130
  clearToken() {
1017
- this.storage.removeItem(ACCESS_KEY);
1018
- this.storage.removeItem(REFRESH_KEY);
1131
+ auth.clearTokens();
1019
1132
  }
1020
1133
  isAuthenticated() {
1021
- return this.getToken() !== null;
1134
+ return auth.isAuthenticated();
1022
1135
  }
1023
1136
  // ── Locale / API key ────────────────────────────────────────────────────
1024
1137
  getLocale() {
1025
- return this.locale ?? detectLocale();
1138
+ return auth.getLocale();
1026
1139
  }
1027
1140
  setLocale(locale) {
1028
- this.locale = locale;
1141
+ auth.setLocale(locale);
1029
1142
  }
1030
1143
  getApiKey() {
1031
- return this.apiKey;
1144
+ return auth.getApiKey();
1032
1145
  }
1033
1146
  setApiKey(key) {
1034
- this.apiKey = key;
1147
+ auth.setApiKey(key);
1035
1148
  }
1036
1149
  };
1037
1150
  __name(_API, "API");
1038
1151
  var API = _API;
1039
1152
 
1040
1153
  // src/_api/BaseClient.ts
1041
- var monitorApi = new API("", { storage: new MemoryStorageAdapter() });
1154
+ var monitorApi = new API("");
1042
1155
 
1043
1156
  // src/_api/generated/types.gen.ts
1044
1157
  var EventTypeEnum = /* @__PURE__ */ ((EventTypeEnum2) => {