@secondlayer/sdk 16.0.0 → 16.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.
package/README.md CHANGED
@@ -15,13 +15,13 @@ import { SecondLayer } from "@secondlayer/sdk";
15
15
 
16
16
  const sl = new SecondLayer({
17
17
  apiKey: process.env.INSTANCE_TOKEN, // from secondlayer init; read from the env when omitted
18
- // default baseUrl: http://127.0.0.1:3800 (or SECONDLAYER_API_URL)
18
+ // default baseUrl: SECONDLAYER_API_URL, else http://127.0.0.1:3800
19
19
  });
20
20
  ```
21
21
 
22
- Hosted: point the client at `https://api.secondlayer.tools` (`SECONDLAYER_API_URL`)
23
- and it sends `SECONDLAYER_API_KEY` (`sk-sl_*`) automatically. Self-host: use
24
- `INSTANCE_TOKEN` as below. The key follows the host.
22
+ Hosted: set `SECONDLAYER_API_KEY` or pass `apiKey` (`sk-sl_*`) and the client uses
23
+ `https://api.secondlayer.tools`. Self-host: use `INSTANCE_TOKEN` as below, which
24
+ keeps the client on your instance even with an account key set. The key follows the host.
25
25
 
26
26
  Auth, once (self-hosted instance): `sl.index`, `sl.streams`, `sl.subgraphs.rows`, and the typed
27
27
  `subscribe` read `/v1`, which is open on loopback and needs `INSTANCE_TOKEN`
@@ -585,7 +585,7 @@ Every delivery, any `format`, also carries a universal authenticity
585
585
  signature you can verify with one published key, no per-webhook secret. The
586
586
  headers are `webhook-id`, `x-secondlayer-signature`, and
587
587
  `x-secondlayer-signature-keyid`; the signed content is `` `${webhook-id}.${rawBody}` ``
588
- (ed25519). Fetch the public key from `GET /public/streams/signing-key`.
588
+ (ed25519). Fetch the public key from `GET /api/webhooks/signing-key`, which takes your account key.
589
589
 
590
590
  ```typescript
591
591
  import { verifySecondlayerSignature } from "@secondlayer/sdk";
package/dist/index.d.ts CHANGED
@@ -298,14 +298,19 @@ type RequestOptions = {
298
298
  * signal's reason, an `AbortError` by default, never an `ApiError`. */
299
299
  signal?: AbortSignal;
300
300
  };
301
- /** Product default: the local one-box API. Override with `baseUrl` or
302
- * `SECONDLAYER_API_URL`. */
301
+ /** Default when nothing points at the hosted API: the local one-box API.
302
+ * Override with `baseUrl` or `SECONDLAYER_API_URL`. */
303
303
  declare const LOCAL_API_URL = "http://127.0.0.1:3800";
304
304
  /** Instance credential. `secondlayer init` writes this. */
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:
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 } : {},
@@ -214,17 +216,18 @@ function raceAbort(promise, signal) {
214
216
  });
215
217
  }
216
218
  var LOCAL_API_URL = "http://127.0.0.1:3800";
219
+ var HOSTED_API_URL = "https://api.secondlayer.tools";
217
220
  var INSTANCE_TOKEN_ENV = "INSTANCE_TOKEN";
218
221
  var ACCOUNT_KEY_ENV = "SECONDLAYER_API_KEY";
219
- function resolveBaseUrl(explicit) {
222
+ function resolveBaseUrl(explicit, apiKey) {
220
223
  if (explicit && explicit.length > 0)
221
224
  return explicit.replace(/\/+$/, "");
222
- if (typeof process !== "undefined") {
223
- const primary = process.env?.SECONDLAYER_API_URL || undefined;
224
- if (primary)
225
- return primary.replace(/\/+$/, "");
226
- }
227
- 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;
228
231
  }
229
232
  function isHostedApiUrl(url) {
230
233
  try {
@@ -296,7 +299,7 @@ class BaseClient {
296
299
  requestTimeoutMs;
297
300
  etagCache;
298
301
  constructor(options = {}) {
299
- this.baseUrl = resolveBaseUrl(options.baseUrl);
302
+ this.baseUrl = resolveBaseUrl(options.baseUrl, options.apiKey);
300
303
  this.apiKey = resolveApiKey(options.apiKey, this.baseUrl);
301
304
  this.origin = options.origin ?? "cli";
302
305
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
@@ -2071,7 +2074,7 @@ function isLoopbackUrl(baseUrl) {
2071
2074
  }
2072
2075
  }
2073
2076
  function createStreamsClient(options) {
2074
- const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl));
2077
+ const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl, options.apiKey));
2075
2078
  const apiKey = resolveApiKey(options.apiKey, baseUrl);
2076
2079
  const origin = options.origin ?? "cli";
2077
2080
  const authHeaders = () => ({
@@ -3418,5 +3421,5 @@ export {
3418
3421
  verifyWebhookSignature
3419
3422
  };
3420
3423
 
3421
- //# debugId=9551CE26B3E76ABF64756E2164756E21
3424
+ //# debugId=F50384CF029DB60E64756E2164756E21
3422
3425
  //# sourceMappingURL=index.js.map