@secondlayer/sdk 6.27.0 → 6.27.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/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();
@@ -424,14 +433,8 @@ class Contracts extends BaseClient {
424
433
  })}`);
425
434
  }
426
435
  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
- }
436
+ const result = await this.requestOrNull("GET", `/v1/contracts/${encodeURIComponent(contractId)}${opts.includeAbi ? "?include=abi" : ""}`);
437
+ return result?.contract ?? null;
435
438
  }
436
439
  }
437
440
 
@@ -732,13 +735,7 @@ class Index extends BaseClient {
732
735
  return this.request("GET", "/v1/index");
733
736
  }
734
737
  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
- }
738
+ return this.requestOrNull("GET", `/v1/index/contracts/${encodeURIComponent(contractId)}/print-schema`);
742
739
  }
743
740
  ftTransfers = Object.assign((params = {}) => this.listFtTransfers(params), {
744
741
  list: (params = {}) => this.listFtTransfers(params),
@@ -1033,13 +1030,7 @@ class Index extends BaseClient {
1033
1030
  })}`);
1034
1031
  }
1035
1032
  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
- }
1033
+ return this.requestOrNull("GET", `/v1/index/blocks/${encodeURIComponent(String(ref))}`);
1043
1034
  }
1044
1035
  async* walkBlocks(params = {}) {
1045
1036
  const batchSize = params.batchSize ?? 200;
@@ -1079,22 +1070,10 @@ class Index extends BaseClient {
1079
1070
  })}`);
1080
1071
  }
1081
1072
  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
- }
1073
+ return this.requestOrNull("GET", `/v1/index/transactions/${encodeURIComponent(txId)}`);
1089
1074
  }
1090
1075
  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
- }
1076
+ return this.requestOrNull("GET", `/v1/index/transactions/${encodeURIComponent(txId)}/proof`);
1098
1077
  }
1099
1078
  async* walkTransactions(params = {}) {
1100
1079
  const batchSize = params.batchSize ?? 200;
@@ -1169,13 +1148,7 @@ class Index extends BaseClient {
1169
1148
  })}`);
1170
1149
  }
1171
1150
  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
- }
1151
+ return this.requestOrNull("GET", `/v1/index/mempool/${encodeURIComponent(txId)}`);
1179
1152
  }
1180
1153
  async* walkMempool(params = {}) {
1181
1154
  const batchSize = params.batchSize ?? 200;
@@ -1239,13 +1212,7 @@ class Index extends BaseClient {
1239
1212
  }
1240
1213
  }
1241
1214
  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
- }
1215
+ return this.requestOrNull("GET", `/v1/index/sbtc/deposits/${encodeURIComponent(bitcoinTxid)}`);
1249
1216
  }
1250
1217
  async listSbtcWithdrawals(params = {}) {
1251
1218
  return this.request("GET", `/v1/index/sbtc/withdrawals${buildQuery({
@@ -1286,13 +1253,7 @@ class Index extends BaseClient {
1286
1253
  }
1287
1254
  }
1288
1255
  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
- }
1256
+ return this.requestOrNull("GET", `/v1/index/sbtc/withdrawals/${requestId}`);
1296
1257
  }
1297
1258
  async listSbtcEvents(params = {}) {
1298
1259
  return this.request("GET", `/v1/index/sbtc/events${buildQuery({
@@ -1360,13 +1321,7 @@ class Index extends BaseClient {
1360
1321
  }
1361
1322
  }
1362
1323
  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
- }
1324
+ return this.requestOrNull("GET", `/v1/index/pox/cycles/${rewardCycle}`);
1370
1325
  }
1371
1326
  }
1372
1327
 
@@ -2701,5 +2656,5 @@ export {
2701
2656
  ApiError
2702
2657
  };
2703
2658
 
2704
- //# debugId=FEC0971346A0193B64756E2164756E21
2659
+ //# debugId=C1FDCE89B0E4893864756E2164756E21
2705
2660
  //# sourceMappingURL=index.js.map