@secondlayer/sdk 6.27.0 → 6.27.2

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.d.ts CHANGED
@@ -24,6 +24,9 @@ declare abstract class BaseClient {
24
24
  constructor(options?: Partial<SecondLayerOptions>);
25
25
  static authHeaders(apiKey?: string): Record<string, string>;
26
26
  protected request<T>(method: string, path: string, body?: unknown): Promise<T>;
27
+ /** Like `request`, but maps a 404 to `null` instead of throwing — the one
28
+ * place that owns the "absent resource" rule for `get*` accessors. */
29
+ protected requestOrNull<T>(method: string, path: string, body?: unknown): Promise<T | null>;
27
30
  protected requestText(method: string, path: string, body?: unknown): Promise<string>;
28
31
  private fetchResponse;
29
32
  }
package/dist/index.js CHANGED
@@ -80,6 +80,15 @@ class BaseClient {
80
80
  }
81
81
  return response.json();
82
82
  }
83
+ async requestOrNull(method, path, body) {
84
+ try {
85
+ return await this.request(method, path, body);
86
+ } catch (err) {
87
+ if (err instanceof ApiError && err.status === 404)
88
+ return null;
89
+ throw err;
90
+ }
91
+ }
83
92
  async requestText(method, path, body) {
84
93
  const response = await this.fetchResponse(method, path, body);
85
94
  return response.text();
@@ -194,44 +203,24 @@ function resolveOrderByColumn(col) {
194
203
 
195
204
  // src/subgraphs/client.ts
196
205
  function buildSubgraphQueryString(params) {
197
- const qs = new URLSearchParams;
198
- if (params.sort)
199
- qs.set("_sort", params.sort);
200
- if (params.order)
201
- qs.set("_order", params.order);
202
- if (params.limit !== undefined)
203
- qs.set("_limit", String(params.limit));
204
- if (params.offset !== undefined)
205
- qs.set("_offset", String(params.offset));
206
- if (params.fields)
207
- qs.set("_fields", params.fields);
208
- if (params.filters) {
209
- for (const [key, value] of Object.entries(params.filters)) {
210
- qs.set(key, String(value));
211
- }
212
- }
213
- const str = qs.toString();
214
- return str ? `?${str}` : "";
206
+ return buildQuery({
207
+ _sort: params.sort,
208
+ _order: params.order,
209
+ _limit: params.limit,
210
+ _offset: params.offset,
211
+ _fields: params.fields,
212
+ ...params.filters
213
+ });
215
214
  }
216
215
  function buildAggregateQueryString(params) {
217
- const qs = new URLSearchParams;
218
- if (params.filters) {
219
- for (const [key, value] of Object.entries(params.filters)) {
220
- qs.set(key, String(value));
221
- }
222
- }
223
- if (params.count)
224
- qs.set("_count", "true");
225
- if (params.countDistinct?.length)
226
- qs.set("_countDistinct", params.countDistinct.join(","));
227
- if (params.sum?.length)
228
- qs.set("_sum", params.sum.join(","));
229
- if (params.min?.length)
230
- qs.set("_min", params.min.join(","));
231
- if (params.max?.length)
232
- qs.set("_max", params.max.join(","));
233
- const str = qs.toString();
234
- return str ? `?${str}` : "";
216
+ return buildQuery({
217
+ ...params.filters,
218
+ _count: params.count ? "true" : undefined,
219
+ _countDistinct: params.countDistinct,
220
+ _sum: params.sum,
221
+ _min: params.min,
222
+ _max: params.max
223
+ });
235
224
  }
236
225
  function buildSpecQueryString(options) {
237
226
  return buildQuery({ server: options?.serverUrl });
@@ -364,13 +353,11 @@ class Subgraphs extends BaseClient {
364
353
  },
365
354
  subscribe(onRow, options = {}) {
366
355
  const filters = options.where ? serializeWhere(options.where) : {};
367
- const qs = new URLSearchParams;
368
- for (const [k, v] of Object.entries(filters))
369
- qs.set(k, String(v));
370
- if (options.since != null)
371
- qs.set("since", String(options.since));
372
- const query = qs.toString();
373
- const url = `${self.baseUrl}/api/subgraphs/${subgraphName}/${tableName}/stream${query ? `?${query}` : ""}`;
356
+ const qs = buildQuery({
357
+ ...filters,
358
+ since: options.since ?? undefined
359
+ });
360
+ const url = `${self.baseUrl}/api/subgraphs/${subgraphName}/${tableName}/stream${qs}`;
374
361
  const ES = globalThis.EventSource;
375
362
  if (!ES) {
376
363
  throw new Error("subscribe() needs a global EventSource (available in browsers and Node >= 22).");
@@ -424,14 +411,8 @@ class Contracts extends BaseClient {
424
411
  })}`);
425
412
  }
426
413
  async get(contractId, opts = {}) {
427
- try {
428
- const { contract } = await this.request("GET", `/v1/contracts/${encodeURIComponent(contractId)}${opts.includeAbi ? "?include=abi" : ""}`);
429
- return contract;
430
- } catch (err) {
431
- if (err instanceof ApiError && err.status === 404)
432
- return null;
433
- throw err;
434
- }
414
+ const result = await this.requestOrNull("GET", `/v1/contracts/${encodeURIComponent(contractId)}${opts.includeAbi ? "?include=abi" : ""}`);
415
+ return result?.contract ?? null;
435
416
  }
436
417
  }
437
418
 
@@ -732,13 +713,7 @@ class Index extends BaseClient {
732
713
  return this.request("GET", "/v1/index");
733
714
  }
734
715
  async printSchema(contractId) {
735
- try {
736
- return await this.request("GET", `/v1/index/contracts/${encodeURIComponent(contractId)}/print-schema`);
737
- } catch (err) {
738
- if (err instanceof ApiError && err.status === 404)
739
- return null;
740
- throw err;
741
- }
716
+ return this.requestOrNull("GET", `/v1/index/contracts/${encodeURIComponent(contractId)}/print-schema`);
742
717
  }
743
718
  ftTransfers = Object.assign((params = {}) => this.listFtTransfers(params), {
744
719
  list: (params = {}) => this.listFtTransfers(params),
@@ -1033,13 +1008,7 @@ class Index extends BaseClient {
1033
1008
  })}`);
1034
1009
  }
1035
1010
  async getBlock(ref) {
1036
- try {
1037
- return await this.request("GET", `/v1/index/blocks/${encodeURIComponent(String(ref))}`);
1038
- } catch (err) {
1039
- if (err instanceof ApiError && err.status === 404)
1040
- return null;
1041
- throw err;
1042
- }
1011
+ return this.requestOrNull("GET", `/v1/index/blocks/${encodeURIComponent(String(ref))}`);
1043
1012
  }
1044
1013
  async* walkBlocks(params = {}) {
1045
1014
  const batchSize = params.batchSize ?? 200;
@@ -1079,22 +1048,10 @@ class Index extends BaseClient {
1079
1048
  })}`);
1080
1049
  }
1081
1050
  async getTransaction(txId) {
1082
- try {
1083
- return await this.request("GET", `/v1/index/transactions/${encodeURIComponent(txId)}`);
1084
- } catch (err) {
1085
- if (err instanceof ApiError && err.status === 404)
1086
- return null;
1087
- throw err;
1088
- }
1051
+ return this.requestOrNull("GET", `/v1/index/transactions/${encodeURIComponent(txId)}`);
1089
1052
  }
1090
1053
  async getTransactionProof(txId) {
1091
- try {
1092
- return await this.request("GET", `/v1/index/transactions/${encodeURIComponent(txId)}/proof`);
1093
- } catch (err) {
1094
- if (err instanceof ApiError && err.status === 404)
1095
- return null;
1096
- throw err;
1097
- }
1054
+ return this.requestOrNull("GET", `/v1/index/transactions/${encodeURIComponent(txId)}/proof`);
1098
1055
  }
1099
1056
  async* walkTransactions(params = {}) {
1100
1057
  const batchSize = params.batchSize ?? 200;
@@ -1169,13 +1126,7 @@ class Index extends BaseClient {
1169
1126
  })}`);
1170
1127
  }
1171
1128
  async getMempoolTx(txId) {
1172
- try {
1173
- return await this.request("GET", `/v1/index/mempool/${encodeURIComponent(txId)}`);
1174
- } catch (err) {
1175
- if (err instanceof ApiError && err.status === 404)
1176
- return null;
1177
- throw err;
1178
- }
1129
+ return this.requestOrNull("GET", `/v1/index/mempool/${encodeURIComponent(txId)}`);
1179
1130
  }
1180
1131
  async* walkMempool(params = {}) {
1181
1132
  const batchSize = params.batchSize ?? 200;
@@ -1239,13 +1190,7 @@ class Index extends BaseClient {
1239
1190
  }
1240
1191
  }
1241
1192
  async getSbtcDeposit(bitcoinTxid) {
1242
- try {
1243
- return await this.request("GET", `/v1/index/sbtc/deposits/${encodeURIComponent(bitcoinTxid)}`);
1244
- } catch (err) {
1245
- if (err instanceof ApiError && err.status === 404)
1246
- return null;
1247
- throw err;
1248
- }
1193
+ return this.requestOrNull("GET", `/v1/index/sbtc/deposits/${encodeURIComponent(bitcoinTxid)}`);
1249
1194
  }
1250
1195
  async listSbtcWithdrawals(params = {}) {
1251
1196
  return this.request("GET", `/v1/index/sbtc/withdrawals${buildQuery({
@@ -1286,13 +1231,7 @@ class Index extends BaseClient {
1286
1231
  }
1287
1232
  }
1288
1233
  async getSbtcWithdrawal(requestId) {
1289
- try {
1290
- return await this.request("GET", `/v1/index/sbtc/withdrawals/${requestId}`);
1291
- } catch (err) {
1292
- if (err instanceof ApiError && err.status === 404)
1293
- return null;
1294
- throw err;
1295
- }
1234
+ return this.requestOrNull("GET", `/v1/index/sbtc/withdrawals/${requestId}`);
1296
1235
  }
1297
1236
  async listSbtcEvents(params = {}) {
1298
1237
  return this.request("GET", `/v1/index/sbtc/events${buildQuery({
@@ -1360,13 +1299,7 @@ class Index extends BaseClient {
1360
1299
  }
1361
1300
  }
1362
1301
  async getPoxCycle(rewardCycle) {
1363
- try {
1364
- return await this.request("GET", `/v1/index/pox/cycles/${rewardCycle}`);
1365
- } catch (err) {
1366
- if (err instanceof ApiError && err.status === 404)
1367
- return null;
1368
- throw err;
1369
- }
1302
+ return this.requestOrNull("GET", `/v1/index/pox/cycles/${rewardCycle}`);
1370
1303
  }
1371
1304
  }
1372
1305
 
@@ -2701,5 +2634,5 @@ export {
2701
2634
  ApiError
2702
2635
  };
2703
2636
 
2704
- //# debugId=FEC0971346A0193B64756E2164756E21
2637
+ //# debugId=CD88F21B10F170CE64756E2164756E21
2705
2638
  //# sourceMappingURL=index.js.map