@secondlayer/sdk 14.1.0 → 14.2.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.
@@ -6,10 +6,13 @@ type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<
6
6
  interface SecondLayerOptions {
7
7
  /** Base URL of the instance API (trailing slashes are stripped). */
8
8
  baseUrl: string;
9
- /** Bearer token for authenticated instance requests (`INSTANCE_TOKEN`). */
9
+ /** Bearer token for authenticated requests. Defaults to
10
+ * `SECONDLAYER_API_KEY` on `api.secondlayer.tools`, `INSTANCE_TOKEN`
11
+ * elsewhere. */
10
12
  apiKey?: string;
11
13
  /** Hosted account key (`sk-sl_*`). Env `SECONDLAYER_API_KEY`. Used by
12
- * `sl.archive` quote/fetch/credits, never by instance `/v1` or `/api`. */
14
+ * `sl.archive` quote/fetch/credits; reaches `/v1` and `/api` only through
15
+ * `apiKey` when the base URL is the hosted API. */
13
16
  accountKey?: string;
14
17
  /** Fetch implementation. Tests and edge runtimes can provide their own. */
15
18
  fetchImpl?: FetchLike;
@@ -208,13 +208,20 @@ function resolveBaseUrl(explicit) {
208
208
  }
209
209
  return LOCAL_API_URL;
210
210
  }
211
- function resolveApiKey(apiKey) {
211
+ function isHostedApiUrl(url) {
212
+ try {
213
+ return new URL(url).hostname === "api.secondlayer.tools";
214
+ } catch {
215
+ return false;
216
+ }
217
+ }
218
+ function resolveApiKey(apiKey, baseUrl) {
212
219
  if (apiKey !== undefined)
213
220
  return apiKey;
214
221
  if (typeof process === "undefined")
215
222
  return;
216
- const token = process.env?.INSTANCE_TOKEN || undefined;
217
- return token;
223
+ const name = baseUrl !== undefined && isHostedApiUrl(baseUrl) ? ACCOUNT_KEY_ENV : INSTANCE_TOKEN_ENV;
224
+ return process.env?.[name] || undefined;
218
225
  }
219
226
  function resolveAccountKey(accountKey) {
220
227
  if (accountKey !== undefined)
@@ -272,7 +279,7 @@ class BaseClient {
272
279
  etagCache;
273
280
  constructor(options = {}) {
274
281
  this.baseUrl = resolveBaseUrl(options.baseUrl);
275
- this.apiKey = resolveApiKey(options.apiKey);
282
+ this.apiKey = resolveApiKey(options.apiKey, this.baseUrl);
276
283
  this.origin = options.origin ?? "cli";
277
284
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
278
285
  this.fetchImpl = options.fetchImpl ?? ((...args) => fetch(...args));
@@ -2303,7 +2310,7 @@ function isLoopbackUrl(baseUrl) {
2303
2310
  }
2304
2311
  function createStreamsClient(options) {
2305
2312
  const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl));
2306
- const apiKey = resolveApiKey(options.apiKey);
2313
+ const apiKey = resolveApiKey(options.apiKey, baseUrl);
2307
2314
  const origin = options.origin ?? "cli";
2308
2315
  const authHeaders = () => ({
2309
2316
  "x-sl-origin": origin,
@@ -2799,5 +2806,5 @@ export {
2799
2806
  getSubgraph
2800
2807
  };
2801
2808
 
2802
- //# debugId=553F4BC532FBAFDA64756E2164756E21
2809
+ //# debugId=DD76B5EA212EEC4D64756E2164756E21
2803
2810
  //# sourceMappingURL=index.js.map