@secondlayer/sdk 16.1.0 → 16.2.1

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/README.md CHANGED
@@ -19,7 +19,7 @@ const sl = new SecondLayer({
19
19
  });
20
20
  ```
21
21
 
22
- Hosted: set `SECONDLAYER_API_KEY` (`sk-sl_*`) and the client uses
22
+ Hosted: set `SECONDLAYER_API_KEY` or pass `apiKey` (`sk-sl_*`) and the client uses
23
23
  `https://api.secondlayer.tools`. Self-host: use `INSTANCE_TOKEN` as below, which
24
24
  keeps the client on your instance even with an account key set. The key follows the host.
25
25
 
package/dist/index.d.ts CHANGED
@@ -305,7 +305,12 @@ declare const LOCAL_API_URL = "http://127.0.0.1:3800";
305
305
  declare const INSTANCE_TOKEN_ENV = "INSTANCE_TOKEN";
306
306
  /** Hosted account key (`sk-sl_*`) for api.secondlayer.tools. */
307
307
  declare const ACCOUNT_KEY_ENV = "SECONDLAYER_API_KEY";
308
- declare function resolveBaseUrl(explicit?: string): string;
308
+ /** The API to call. A named URL wins (`explicit`, then `SECONDLAYER_API_URL`).
309
+ * Otherwise the key picks the host, so a credential never reaches the wrong
310
+ * one: `apiKey` if the caller passed one, else a lone account key in the env.
311
+ * Self-hosters often hold an account key for archive credits too, so an env
312
+ * instance token keeps them on their instance. */
313
+ declare function resolveBaseUrl(explicit?: string, apiKey?: string): string;
309
314
  /** True when `url` is the hosted Secondlayer API (`api.secondlayer.tools`). */
310
315
  declare function isHostedApiUrl(url: string): boolean;
311
316
  /** Resolve the bearer credential; the key follows the host. Precedence:
@@ -3770,9 +3775,9 @@ declare class Webhooks extends BaseClient {
3770
3775
  /** Get a webhook's hourly delivery counts for the last 7 days.
3771
3776
  *
3772
3777
  * Each hour has delivered, waiting, and gave-up counts, plus the current
3773
- * queue depth, failed attempts per hour, and the usual band. Pass
3774
- * `{ days: 30 }` to also get 30 daily totals. This is the events chart on
3775
- * the detail page. */
3778
+ * queue depth, failed attempts per hour, the usual band, and 30 daily
3779
+ * totals. This is the events chart on the detail page. `{ days: 30 }` is
3780
+ * accepted for older servers, which only send the daily totals when asked. */
3776
3781
  activity(id: string, opts?: {
3777
3782
  days?: 30;
3778
3783
  }): Promise<WebhookActivity>;
package/dist/index.js CHANGED
@@ -60,7 +60,9 @@ ${detailBlocks.join(`
60
60
  };
61
61
  }
62
62
  }
63
- function statusRetryable(status) {
63
+ function statusRetryable(status, code) {
64
+ if (code === "STACK_ASLEEP")
65
+ return false;
64
66
  return status === 429 || status >= 500;
65
67
  }
66
68
  function docsFor(status, code) {
@@ -77,7 +79,7 @@ class ApiError extends SecondLayerError {
77
79
  constructor(status, message, body, code, options = {}) {
78
80
  super(message, {
79
81
  code,
80
- retryable: options.retryable ?? statusRetryable(status),
82
+ retryable: options.retryable ?? statusRetryable(status, code),
81
83
  docsUrl: options.docsUrl ?? docsFor(status, code),
82
84
  ...options.cause ? { cause: options.cause } : {},
83
85
  ...options.details ? { details: options.details } : {},
@@ -217,18 +219,15 @@ var LOCAL_API_URL = "http://127.0.0.1:3800";
217
219
  var HOSTED_API_URL = "https://api.secondlayer.tools";
218
220
  var INSTANCE_TOKEN_ENV = "INSTANCE_TOKEN";
219
221
  var ACCOUNT_KEY_ENV = "SECONDLAYER_API_KEY";
220
- function resolveBaseUrl(explicit) {
222
+ function resolveBaseUrl(explicit, apiKey) {
221
223
  if (explicit && explicit.length > 0)
222
224
  return explicit.replace(/\/+$/, "");
223
- if (typeof process !== "undefined") {
224
- const primary = process.env?.SECONDLAYER_API_URL || undefined;
225
- if (primary)
226
- return primary.replace(/\/+$/, "");
227
- if (process.env?.[ACCOUNT_KEY_ENV]?.startsWith("sk-sl_") && !process.env?.[INSTANCE_TOKEN_ENV]) {
228
- return HOSTED_API_URL;
229
- }
230
- }
231
- return LOCAL_API_URL;
225
+ const env = typeof process !== "undefined" ? process.env : undefined;
226
+ const primary = env?.SECONDLAYER_API_URL || undefined;
227
+ if (primary)
228
+ return primary.replace(/\/+$/, "");
229
+ const key = apiKey ?? (env?.[INSTANCE_TOKEN_ENV] ? undefined : env?.[ACCOUNT_KEY_ENV]);
230
+ return key?.startsWith("sk-sl_") ? HOSTED_API_URL : LOCAL_API_URL;
232
231
  }
233
232
  function isHostedApiUrl(url) {
234
233
  try {
@@ -300,7 +299,7 @@ class BaseClient {
300
299
  requestTimeoutMs;
301
300
  etagCache;
302
301
  constructor(options = {}) {
303
- this.baseUrl = resolveBaseUrl(options.baseUrl);
302
+ this.baseUrl = resolveBaseUrl(options.baseUrl, options.apiKey);
304
303
  this.apiKey = resolveApiKey(options.apiKey, this.baseUrl);
305
304
  this.origin = options.origin ?? "cli";
306
305
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
@@ -2075,7 +2074,7 @@ function isLoopbackUrl(baseUrl) {
2075
2074
  }
2076
2075
  }
2077
2076
  function createStreamsClient(options) {
2078
- const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl));
2077
+ const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl, options.apiKey));
2079
2078
  const apiKey = resolveApiKey(options.apiKey, baseUrl);
2080
2079
  const origin = options.origin ?? "cli";
2081
2080
  const authHeaders = () => ({
@@ -3422,5 +3421,5 @@ export {
3422
3421
  verifyWebhookSignature
3423
3422
  };
3424
3423
 
3425
- //# debugId=8E37F0B7E094194F64756E2164756E21
3424
+ //# debugId=F50384CF029DB60E64756E2164756E21
3426
3425
  //# sourceMappingURL=index.js.map