@secondlayer/sdk 10.2.0 → 10.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.
@@ -172,33 +172,55 @@ function raceAbort(promise, signal) {
172
172
  });
173
173
  }
174
174
  var LOCAL_API_URL = "http://127.0.0.1:3800";
175
+ var INSTANCE_TOKEN_ENV = "INSTANCE_TOKEN";
176
+ var ACCOUNT_KEY_ENV = "SECONDLAYER_API_KEY";
177
+ var warnedUrlAlias = false;
175
178
  function resolveBaseUrl(explicit) {
176
179
  if (explicit && explicit.length > 0)
177
180
  return explicit.replace(/\/+$/, "");
178
181
  if (typeof process !== "undefined") {
179
- const fromEnv = process.env?.SL_API_URL || process.env?.SECONDLAYER_API_URL;
180
- if (fromEnv)
181
- return fromEnv.replace(/\/+$/, "");
182
+ const primary = process.env?.SECONDLAYER_API_URL || undefined;
183
+ if (primary)
184
+ return primary.replace(/\/+$/, "");
185
+ const legacy = process.env?.SL_API_URL || undefined;
186
+ if (legacy) {
187
+ if (!warnedUrlAlias) {
188
+ warnedUrlAlias = true;
189
+ console.warn("[secondlayer] SL_API_URL is a legacy alias of SECONDLAYER_API_URL; using the alias this once.");
190
+ }
191
+ return legacy.replace(/\/+$/, "");
192
+ }
182
193
  }
183
194
  return LOCAL_API_URL;
184
195
  }
185
- var CREDENTIAL_ENV_VARS = ["INSTANCE_TOKEN", "SL_API_KEY"];
186
- var warnedConflicts = new Set;
187
196
  function resolveApiKey(apiKey) {
188
197
  if (apiKey !== undefined)
189
198
  return apiKey;
190
199
  if (typeof process === "undefined")
191
200
  return;
192
- const instanceToken = process.env?.INSTANCE_TOKEN || undefined;
193
- const legacyKey = process.env?.SL_API_KEY || undefined;
194
- if (instanceToken && legacyKey && instanceToken !== legacyKey) {
195
- const pair = `${instanceToken}\x00${legacyKey}`;
196
- if (!warnedConflicts.has(pair)) {
197
- warnedConflicts.add(pair);
198
- console.warn("[secondlayer] INSTANCE_TOKEN and SL_API_KEY are set to different values — using INSTANCE_TOKEN (SL_API_KEY is a legacy alias for it).");
199
- }
201
+ const token = process.env?.INSTANCE_TOKEN || undefined;
202
+ return token;
203
+ }
204
+ var warnedArchiveAlias = false;
205
+ function resolveAccountKey(accountKey) {
206
+ if (accountKey !== undefined)
207
+ return accountKey;
208
+ if (typeof process === "undefined")
209
+ return;
210
+ const primary = process.env?.SECONDLAYER_API_KEY || undefined;
211
+ const slAlias = process.env?.SL_API_KEY || undefined;
212
+ const archiveAlias = process.env?.SL_ARCHIVE_API_KEY || undefined;
213
+ if (primary)
214
+ return primary;
215
+ const legacy = slAlias ?? archiveAlias;
216
+ if (legacy) {
217
+ if (!warnedArchiveAlias) {
218
+ warnedArchiveAlias = true;
219
+ console.warn("[secondlayer] SL_API_KEY / SL_ARCHIVE_API_KEY are legacy aliases of SECONDLAYER_API_KEY; using the alias this once.");
220
+ }
221
+ return legacy;
200
222
  }
201
- return instanceToken ?? legacyKey;
223
+ return;
202
224
  }
203
225
  function seg(value) {
204
226
  return encodeURIComponent(String(value));
@@ -857,6 +879,7 @@ var OFFICIAL_ARCHIVE_HOST = "archive.secondlayer.tools";
857
879
  var DEFAULT_ARCHIVE_BASE_URL = "https://archive.secondlayer.tools";
858
880
  var DEFAULT_ARCHIVE_OPS_URL = "https://api.secondlayer.tools";
859
881
  var MAX_FETCH_BATCH = 64;
882
+ var ARCHIVE_CREDENTIAL_RE = /^s[ks]-sl_/;
860
883
  function quoteFromBody(body) {
861
884
  return {
862
885
  partitions: body.partitions,
@@ -873,7 +896,7 @@ function stripSlash(url) {
873
896
  return url.replace(/\/+$/, "");
874
897
  }
875
898
  function createArchiveClient(options = {}) {
876
- const apiKey = resolveApiKey(options.apiKey);
899
+ const accountKey = resolveAccountKey(options.accountKey ?? options.apiKey);
877
900
  const fetchImpl = options.fetchImpl ?? ((input, init) => fetch(input, init));
878
901
  const archiveBaseUrl = stripSlash(options.archiveBaseUrl ?? DEFAULT_ARCHIVE_BASE_URL);
879
902
  const archiveOpsUrl = stripSlash(options.archiveOpsUrl ?? DEFAULT_ARCHIVE_OPS_URL);
@@ -921,8 +944,12 @@ function createArchiveClient(options = {}) {
921
944
  const headers = {
922
945
  "Content-Type": "application/json"
923
946
  };
924
- if (authed && apiKey)
925
- headers.Authorization = `Bearer ${apiKey}`;
947
+ if (authed && accountKey) {
948
+ if (!ARCHIVE_CREDENTIAL_RE.test(accountKey)) {
949
+ throw new ArchiveAuthError("archive ops need SECONDLAYER_API_KEY (sk-sl_*), not INSTANCE_TOKEN");
950
+ }
951
+ headers.Authorization = `Bearer ${accountKey}`;
952
+ }
926
953
  const response = await fetchImpl(`${archiveOpsUrl}${path}`, {
927
954
  method,
928
955
  headers,
@@ -2518,7 +2545,7 @@ class SecondLayer extends BaseClient {
2518
2545
  const request = (method, path, body) => this.request(method, path, body);
2519
2546
  this.archive = {
2520
2547
  ...createArchiveClient({
2521
- apiKey: this.apiKey,
2548
+ accountKey: options.accountKey,
2522
2549
  fetchImpl: options.fetchImpl,
2523
2550
  archiveBaseUrl: options.archiveBaseUrl,
2524
2551
  archiveOpsUrl: options.archiveOpsUrl
@@ -2617,5 +2644,5 @@ export {
2617
2644
  Subgraphs
2618
2645
  };
2619
2646
 
2620
- //# debugId=59EE80259E1C9AEC64756E2164756E21
2647
+ //# debugId=B1E5E37D297ADB5664756E2164756E21
2621
2648
  //# sourceMappingURL=index.js.map