pinata 0.1.10 → 0.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/dist/index.js CHANGED
@@ -67,21 +67,24 @@ var testAuthentication = async (config) => {
67
67
  if (!config || !config.pinataJwt) {
68
68
  throw new ValidationError("Pinata configuration or JWT is missing");
69
69
  }
70
- const headers = {
71
- Authorization: `Bearer ${config?.pinataJwt}`
72
- };
73
- if (config.customHeaders) {
74
- Object.assign(headers, config.customHeaders);
70
+ let headers;
71
+ let endpoint = "https://api.pinata.cloud";
72
+ if (config.endpointUrl) {
73
+ endpoint = config.endpointUrl;
74
+ }
75
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
76
+ headers = { ...config.customHeaders };
77
+ } else {
78
+ headers = {
79
+ Authorization: `Bearer ${config.pinataJwt}`,
80
+ Source: "sdk/testAuthentication"
81
+ };
75
82
  }
76
- headers["Source"] = headers["Source"] || "sdk/testAuthentication";
77
83
  try {
78
- const request = await fetch(
79
- "https://api.pinata.cloud/data/testAuthentication",
80
- {
81
- method: "GET",
82
- headers
83
- }
84
- );
84
+ const request = await fetch(`${endpoint}/data/testAuthentication`, {
85
+ method: "GET",
86
+ headers
87
+ });
85
88
  if (!request.ok) {
86
89
  const errorData = await request.json();
87
90
  if (request.status === 401) {
@@ -132,26 +135,29 @@ var uploadFile = async (config, file, options) => {
132
135
  data.append(
133
136
  "pinataMetadata",
134
137
  JSON.stringify({
135
- name: options?.metadata ? options.metadata.name : file.name,
138
+ name: options?.metadata?.name || file.name || "File from SDK",
136
139
  keyvalues: options?.metadata?.keyValues
137
140
  })
138
141
  );
139
- const headers = {
140
- Authorization: `Bearer ${jwt}`
141
- };
142
- if (config.customHeaders) {
143
- Object.assign(headers, config.customHeaders);
142
+ let headers;
143
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
144
+ headers = { ...config.customHeaders };
145
+ } else {
146
+ headers = {
147
+ Authorization: `Bearer ${jwt}`,
148
+ Source: "sdk/file"
149
+ };
150
+ }
151
+ let endpoint = "https://api.pinata.cloud";
152
+ if (config.endpointUrl) {
153
+ endpoint = config.endpointUrl;
144
154
  }
145
- headers["Source"] = headers["Source"] || "sdk/file";
146
155
  try {
147
- const request = await fetch(
148
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
149
- {
150
- method: "POST",
151
- headers,
152
- body: data
153
- }
154
- );
156
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
157
+ method: "POST",
158
+ headers,
159
+ body: data
160
+ });
155
161
  if (!request.ok) {
156
162
  const errorData = await request.json();
157
163
  if (request.status === 401) {
@@ -186,7 +192,7 @@ var uploadFileArray = async (config, files, options) => {
186
192
  throw new ValidationError("Pinata configuration or JWT is missing");
187
193
  }
188
194
  const jwt = options?.keys || config?.pinataJwt;
189
- const folder = options?.metadata?.name ?? "folder_from_sdk";
195
+ const folder = options?.metadata?.name || "folder_from_sdk";
190
196
  const data = new FormData();
191
197
  for (const file of Array.from(files)) {
192
198
  data.append("file", file, `${folder}/${file.name}`);
@@ -205,22 +211,25 @@ var uploadFileArray = async (config, files, options) => {
205
211
  groupId: options?.groupId
206
212
  })
207
213
  );
208
- const headers = {
209
- Authorization: `Bearer ${jwt}`
210
- };
211
- if (config.customHeaders) {
212
- Object.assign(headers, config.customHeaders);
214
+ let headers;
215
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
216
+ headers = { ...config.customHeaders };
217
+ } else {
218
+ headers = {
219
+ Authorization: `Bearer ${jwt}`,
220
+ Source: "sdk/fileArray"
221
+ };
222
+ }
223
+ let endpoint = "https://api.pinata.cloud";
224
+ if (config.endpointUrl) {
225
+ endpoint = config.endpointUrl;
213
226
  }
214
- headers["Source"] = headers["Source"] || "sdk/fileArray";
215
227
  try {
216
- const request = await fetch(
217
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
218
- {
219
- method: "POST",
220
- headers,
221
- body: data
222
- }
223
- );
228
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
229
+ method: "POST",
230
+ headers,
231
+ body: data
232
+ });
224
233
  if (!request.ok) {
225
234
  const errorData = await request.json();
226
235
  if (request.status === 401) {
@@ -276,22 +285,25 @@ var uploadBase64 = async (config, base64String, options) => {
276
285
  keyvalues: options?.metadata?.keyValues
277
286
  })
278
287
  );
279
- const headers = {
280
- Authorization: `Bearer ${jwt}`
281
- };
282
- if (config.customHeaders) {
283
- Object.assign(headers, config.customHeaders);
288
+ let headers;
289
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
290
+ headers = { ...config.customHeaders };
291
+ } else {
292
+ headers = {
293
+ Authorization: `Bearer ${jwt}`,
294
+ Source: "sdk/base64"
295
+ };
296
+ }
297
+ let endpoint = "https://api.pinata.cloud";
298
+ if (config.endpointUrl) {
299
+ endpoint = config.endpointUrl;
284
300
  }
285
- headers["Source"] = headers["Source"] || "sdk/base64";
286
301
  try {
287
- const request = await fetch(
288
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
289
- {
290
- method: "POST",
291
- headers,
292
- body: data
293
- }
294
- );
302
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
303
+ method: "POST",
304
+ headers,
305
+ body: data
306
+ });
295
307
  if (!request.ok) {
296
308
  const errorData = await request.json();
297
309
  if (request.status === 401) {
@@ -357,22 +369,25 @@ var uploadUrl = async (config, url, options) => {
357
369
  keyvalues: options?.metadata?.keyValues
358
370
  })
359
371
  );
360
- const headers = {
361
- Authorization: `Bearer ${jwt}`
362
- };
363
- if (config.customHeaders) {
364
- Object.assign(headers, config.customHeaders);
372
+ let headers;
373
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
374
+ headers = { ...config.customHeaders };
375
+ } else {
376
+ headers = {
377
+ Authorization: `Bearer ${jwt}`,
378
+ Source: "sdk/url"
379
+ };
380
+ }
381
+ let endpoint = "https://api.pinata.cloud";
382
+ if (config.endpointUrl) {
383
+ endpoint = config.endpointUrl;
365
384
  }
366
- headers["Source"] = headers["Source"] || "sdk/url";
367
385
  try {
368
- const request = await fetch(
369
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
370
- {
371
- method: "POST",
372
- headers,
373
- body: data
374
- }
375
- );
386
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
387
+ method: "POST",
388
+ headers,
389
+ body: data
390
+ });
376
391
  if (!request.ok) {
377
392
  const errorData = await request.json();
378
393
  if (request.status === 401) {
@@ -414,27 +429,30 @@ var uploadJson = async (config, jsonData, options) => {
414
429
  groupId: options?.groupId
415
430
  },
416
431
  pinataMetadata: {
417
- name: options?.metadata ? options.metadata.name : "json",
432
+ name: options?.metadata?.name || "json",
418
433
  keyvalues: options?.metadata?.keyValues
419
434
  }
420
435
  });
421
- const headers = {
422
- "Content-Type": "application/json",
423
- Authorization: `Bearer ${jwt}`
424
- };
425
- if (config.customHeaders) {
426
- Object.assign(headers, config.customHeaders);
436
+ let headers;
437
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
438
+ headers = { ...config.customHeaders };
439
+ } else {
440
+ headers = {
441
+ Authorization: `Bearer ${jwt}`,
442
+ "Content-Type": "application/json",
443
+ Source: "sdk/json"
444
+ };
445
+ }
446
+ let endpoint = "https://api.pinata.cloud";
447
+ if (config.endpointUrl) {
448
+ endpoint = config.endpointUrl;
427
449
  }
428
- headers["Source"] = headers["Source"] || "sdk/json";
429
450
  try {
430
- const request = await fetch(
431
- "https://api.pinata.cloud/pinning/pinJSONToIPFS",
432
- {
433
- method: "POST",
434
- headers,
435
- body: data
436
- }
437
- );
451
+ const request = await fetch(`${endpoint}/pinning/pinJSONToIPFS`, {
452
+ method: "POST",
453
+ headers,
454
+ body: data
455
+ });
438
456
  if (!request.ok) {
439
457
  const errorData = await request.json();
440
458
  if (request.status === 401) {
@@ -469,14 +487,16 @@ var uploadCid = async (config, cid, options) => {
469
487
  throw new ValidationError("Pinata configuration or JWT is missing");
470
488
  }
471
489
  const jwt = options?.keys || config?.pinataJwt;
472
- const headers = {
473
- "Content-Type": "application/json",
474
- Authorization: `Bearer ${jwt}`
475
- };
476
- if (config.customHeaders) {
477
- Object.assign(headers, config.customHeaders);
490
+ let headers;
491
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
492
+ headers = { ...config.customHeaders };
493
+ } else {
494
+ headers = {
495
+ Authorization: `Bearer ${jwt}`,
496
+ "Content-Type": "application/json",
497
+ Source: "sdk/cid"
498
+ };
478
499
  }
479
- headers["Source"] = headers["Source"] || "sdk/cid";
480
500
  const data = JSON.stringify({
481
501
  hashToPin: cid,
482
502
  pinataMetadata: {
@@ -488,8 +508,12 @@ var uploadCid = async (config, cid, options) => {
488
508
  groupId: options?.groupId
489
509
  }
490
510
  });
511
+ let endpoint = "https://api.pinata.cloud";
512
+ if (config.endpointUrl) {
513
+ endpoint = config.endpointUrl;
514
+ }
491
515
  try {
492
- const request = await fetch("https://api.pinata.cloud/pinning/pinByHash", {
516
+ const request = await fetch(`${endpoint}/pinning/pinByHash`, {
493
517
  method: "POST",
494
518
  headers,
495
519
  body: data
@@ -533,23 +557,25 @@ var unpinFile = async (config, files) => {
533
557
  throw new ValidationError("Pinata configuration or JWT is missing");
534
558
  }
535
559
  const responses = [];
536
- const headers = {
537
- "Content-Type": "application/json",
538
- Authorization: `Bearer ${config?.pinataJwt}`
539
- };
540
- if (config.customHeaders) {
541
- Object.assign(headers, config.customHeaders);
560
+ let headers;
561
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
562
+ headers = { ...config.customHeaders };
563
+ } else {
564
+ headers = {
565
+ Authorization: `Bearer ${config.pinataJwt}`,
566
+ Source: "sdk/unpin"
567
+ };
568
+ }
569
+ let endpoint = "https://api.pinata.cloud";
570
+ if (config.endpointUrl) {
571
+ endpoint = config.endpointUrl;
542
572
  }
543
- headers["Source"] = headers["Source"] || "sdk/unpin";
544
573
  for (const hash of files) {
545
574
  try {
546
- const response = await fetch(
547
- `https://api.pinata.cloud/pinning/unpin/${hash}`,
548
- {
549
- method: "DELETE",
550
- headers
551
- }
552
- );
575
+ const response = await fetch(`${endpoint}/pinning/unpin/${hash}`, {
576
+ method: "DELETE",
577
+ headers
578
+ });
553
579
  await wait(300);
554
580
  if (!response.ok) {
555
581
  const errorData = await response.json();
@@ -637,15 +663,21 @@ var listFiles = async (config, options) => {
637
663
  params.append("metadata[keyvalues]", keyValueParam);
638
664
  }
639
665
  }
640
- const url = `https://api.pinata.cloud/data/pinList?status=pinned&${params.toString()}`;
666
+ let endpoint = "https://api.pinata.cloud";
667
+ if (config.endpointUrl) {
668
+ endpoint = config.endpointUrl;
669
+ }
670
+ const url = `${endpoint}/data/pinList?status=pinned&${params.toString()}`;
641
671
  try {
642
- const headers = {
643
- Authorization: `Bearer ${config?.pinataJwt}`
644
- };
645
- if (config.customHeaders) {
646
- Object.assign(headers, config.customHeaders);
672
+ let headers;
673
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
674
+ headers = { ...config.customHeaders };
675
+ } else {
676
+ headers = {
677
+ Authorization: `Bearer ${config.pinataJwt}`,
678
+ Source: "sdk/listFiles"
679
+ };
647
680
  }
648
- headers["Source"] = headers["Source"] || "sdk/listFiles";
649
681
  const request = await fetch(url, {
650
682
  method: "GET",
651
683
  headers
@@ -688,23 +720,26 @@ var updateMetadata = async (config, options) => {
688
720
  name: options.name,
689
721
  keyvalues: options.keyValues
690
722
  };
691
- const headers = {
692
- "Content-Type": "application/json",
693
- Authorization: `Bearer ${config?.pinataJwt}`
694
- };
695
- if (config.customHeaders) {
696
- Object.assign(headers, config.customHeaders);
723
+ let headers;
724
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
725
+ headers = { ...config.customHeaders };
726
+ } else {
727
+ headers = {
728
+ Authorization: `Bearer ${config.pinataJwt}`,
729
+ "Content-Type": "application/json",
730
+ Source: "sdk/updateMetadata"
731
+ };
732
+ }
733
+ let endpoint = "https://api.pinata.cloud";
734
+ if (config.endpointUrl) {
735
+ endpoint = config.endpointUrl;
697
736
  }
698
- headers["Source"] = headers["Source"] || "sdk/updateMetadata";
699
737
  try {
700
- const request = await fetch(
701
- "https://api.pinata.cloud/pinning/hashMetadata",
702
- {
703
- method: "PUT",
704
- headers,
705
- body: JSON.stringify(data)
706
- }
707
- );
738
+ const request = await fetch(`${endpoint}/pinning/hashMetadata`, {
739
+ method: "PUT",
740
+ headers,
741
+ body: JSON.stringify(data)
742
+ });
708
743
  if (!request.ok) {
709
744
  const errorData = await request.json();
710
745
  if (request.status === 401) {
@@ -919,14 +954,20 @@ var pinJobs = async (config, options) => {
919
954
  if (offset)
920
955
  params.append("offset", offset.toString());
921
956
  }
922
- const url = `https://api.pinata.cloud/pinning/pinJobs?${params.toString()}`;
923
- const headers = {
924
- Authorization: `Bearer ${config?.pinataJwt}`
925
- };
926
- if (config.customHeaders) {
927
- Object.assign(headers, config.customHeaders);
957
+ let endpoint = "https://api.pinata.cloud";
958
+ if (config.endpointUrl) {
959
+ endpoint = config.endpointUrl;
960
+ }
961
+ const url = `${endpoint}/pinning/pinJobs?${params.toString()}`;
962
+ let headers;
963
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
964
+ headers = { ...config.customHeaders };
965
+ } else {
966
+ headers = {
967
+ Authorization: `Bearer ${config.pinataJwt}`,
968
+ Source: "sdk/pinJobs"
969
+ };
928
970
  }
929
- headers["Source"] = headers["Source"] || "sdk/pinJobs";
930
971
  try {
931
972
  const request = await fetch(url, {
932
973
  method: "GET",
@@ -965,16 +1006,21 @@ var pinnedFileCount = async (config) => {
965
1006
  if (!config || !config.pinataJwt) {
966
1007
  throw new ValidationError("Pinata configuration or JWT is missing");
967
1008
  }
968
- const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
969
- const headers = {
970
- Authorization: `Bearer ${config?.pinataJwt}`
971
- };
972
- if (config.customHeaders) {
973
- Object.assign(headers, config.customHeaders);
1009
+ let endpoint = "https://api.pinata.cloud";
1010
+ if (config.endpointUrl) {
1011
+ endpoint = config.endpointUrl;
1012
+ }
1013
+ let headers;
1014
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1015
+ headers = { ...config.customHeaders };
1016
+ } else {
1017
+ headers = {
1018
+ Authorization: `Bearer ${config.pinataJwt}`,
1019
+ Source: "sdk/pinnedFileUsage"
1020
+ };
974
1021
  }
975
- headers["Source"] = headers["Source"] || "sdk/pinnedFileUsage";
976
1022
  try {
977
- const request = await fetch(url, {
1023
+ const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
978
1024
  method: "GET",
979
1025
  headers
980
1026
  });
@@ -1015,16 +1061,21 @@ var totalStorageUsage = async (config) => {
1015
1061
  if (!config || !config.pinataJwt) {
1016
1062
  throw new ValidationError("Pinata configuration or JWT is missing");
1017
1063
  }
1018
- const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
1019
- const headers = {
1020
- Authorization: `Bearer ${config?.pinataJwt}`
1021
- };
1022
- if (config.customHeaders) {
1023
- Object.assign(headers, config.customHeaders);
1064
+ let endpoint = "https://api.pinata.cloud";
1065
+ if (config.endpointUrl) {
1066
+ endpoint = config.endpointUrl;
1067
+ }
1068
+ let headers;
1069
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1070
+ headers = { ...config.customHeaders };
1071
+ } else {
1072
+ headers = {
1073
+ Authorization: `Bearer ${config.pinataJwt}`,
1074
+ Source: "sdk/totalStorageUsage"
1075
+ };
1024
1076
  }
1025
- headers["Source"] = headers["Source"] || "sdk/totalStorageUsage";
1026
1077
  try {
1027
- const request = await fetch(url, {
1078
+ const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
1028
1079
  method: "GET",
1029
1080
  headers
1030
1081
  });
@@ -1065,17 +1116,23 @@ var createKey = async (config, options) => {
1065
1116
  if (!config || !config.pinataJwt) {
1066
1117
  throw new ValidationError("Pinata configuration or JWT is missing");
1067
1118
  }
1068
- const headers = {
1069
- "Content-Type": "application/json",
1070
- Authorization: `Bearer ${config?.pinataJwt}`
1071
- };
1072
- if (config.customHeaders) {
1073
- Object.assign(headers, config.customHeaders);
1119
+ let headers;
1120
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1121
+ headers = { ...config.customHeaders };
1122
+ } else {
1123
+ headers = {
1124
+ Authorization: `Bearer ${config.pinataJwt}`,
1125
+ "Content-Type": "application/json",
1126
+ Source: "sdk/createKey"
1127
+ };
1074
1128
  }
1075
- headers["Source"] = headers["Source"] || "sdk/createKey";
1076
1129
  const data = JSON.stringify(options);
1130
+ let endpoint = "https://api.pinata.cloud";
1131
+ if (config.endpointUrl) {
1132
+ endpoint = config.endpointUrl;
1133
+ }
1077
1134
  try {
1078
- const request = await fetch("https://api.pinata.cloud/v3/pinata/keys", {
1135
+ const request = await fetch(`${endpoint}/v3/pinata/keys`, {
1079
1136
  method: "POST",
1080
1137
  headers,
1081
1138
  body: data
@@ -1113,14 +1170,16 @@ var listKeys = async (config, options) => {
1113
1170
  if (!config || !config.pinataJwt) {
1114
1171
  throw new ValidationError("Pinata configuration or JWT is missing");
1115
1172
  }
1116
- const headers = {
1117
- "Content-Type": "application/json",
1118
- Authorization: `Bearer ${config?.pinataJwt}`
1119
- };
1120
- if (config.customHeaders) {
1121
- Object.assign(headers, config.customHeaders);
1173
+ let headers;
1174
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1175
+ headers = { ...config.customHeaders };
1176
+ } else {
1177
+ headers = {
1178
+ Authorization: `Bearer ${config.pinataJwt}`,
1179
+ "Content-Type": "application/json",
1180
+ Source: "sdk/listKeys"
1181
+ };
1122
1182
  }
1123
- headers["Source"] = headers["Source"] || "sdk/listKeys";
1124
1183
  const params = new URLSearchParams();
1125
1184
  if (options) {
1126
1185
  const { offset, name, revoked, limitedUse, exhausted } = options;
@@ -1135,12 +1194,18 @@ var listKeys = async (config, options) => {
1135
1194
  if (name)
1136
1195
  params.append("name", name);
1137
1196
  }
1138
- const url = `https://api.pinata.cloud/v3/pinata/keys?${params.toString()}`;
1197
+ let endpoint = "https://api.pinata.cloud";
1198
+ if (config.endpointUrl) {
1199
+ endpoint = config.endpointUrl;
1200
+ }
1139
1201
  try {
1140
- const request = await fetch(url, {
1141
- method: "GET",
1142
- headers
1143
- });
1202
+ const request = await fetch(
1203
+ `${endpoint}/v3/pinata/keys?${params.toString()}`,
1204
+ {
1205
+ method: "GET",
1206
+ headers
1207
+ }
1208
+ );
1144
1209
  if (!request.ok) {
1145
1210
  const errorData = await request.json();
1146
1211
  if (request.status === 401) {
@@ -1179,24 +1244,27 @@ var revokeKeys = async (config, keys) => {
1179
1244
  if (!config || !config.pinataJwt) {
1180
1245
  throw new ValidationError("Pinata configuration or JWT is missing");
1181
1246
  }
1182
- const headers = {
1183
- "Content-Type": "application/json",
1184
- Authorization: `Bearer ${config?.pinataJwt}`
1185
- };
1186
- if (config.customHeaders) {
1187
- Object.assign(headers, config.customHeaders);
1247
+ let headers;
1248
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1249
+ headers = { ...config.customHeaders };
1250
+ } else {
1251
+ headers = {
1252
+ Authorization: `Bearer ${config.pinataJwt}`,
1253
+ "Content-Type": "application/json",
1254
+ Source: "sdk/revokeKeys"
1255
+ };
1188
1256
  }
1189
- headers["Source"] = headers["Source"] || "sdk/revokeKeys";
1190
1257
  const responses = [];
1258
+ let endpoint = "https://api.pinata.cloud";
1259
+ if (config.endpointUrl) {
1260
+ endpoint = config.endpointUrl;
1261
+ }
1191
1262
  for (const key of keys) {
1192
1263
  try {
1193
- const request = await fetch(
1194
- `https://api.pinata.cloud/v3/pinata/keys/${key}`,
1195
- {
1196
- method: "PUT",
1197
- headers
1198
- }
1199
- );
1264
+ const request = await fetch(`${endpoint}/v3/pinata/keys/${key}`, {
1265
+ method: "PUT",
1266
+ headers
1267
+ });
1200
1268
  await wait2(300);
1201
1269
  if (!request.ok) {
1202
1270
  const errorData = await request.json();
@@ -1242,16 +1310,22 @@ var createGroup = async (config, options) => {
1242
1310
  throw new ValidationError("Pinata configuration or JWT is missing");
1243
1311
  }
1244
1312
  const data = JSON.stringify(options);
1245
- const headers = {
1246
- "Content-Type": "application/json",
1247
- Authorization: `Bearer ${config?.pinataJwt}`
1248
- };
1249
- if (config.customHeaders) {
1250
- Object.assign(headers, config.customHeaders);
1313
+ let headers;
1314
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1315
+ headers = { ...config.customHeaders };
1316
+ } else {
1317
+ headers = {
1318
+ Authorization: `Bearer ${config.pinataJwt}`,
1319
+ "Content-Type": "application/json",
1320
+ Source: "sdk/createGroup"
1321
+ };
1322
+ }
1323
+ let endpoint = "https://api.pinata.cloud";
1324
+ if (config.endpointUrl) {
1325
+ endpoint = config.endpointUrl;
1251
1326
  }
1252
- headers["Source"] = headers["Source"] || "sdk/createGroup";
1253
1327
  try {
1254
- const request = await fetch("https://api.pinata.cloud/groups", {
1328
+ const request = await fetch(`${endpoint}/groups`, {
1255
1329
  method: "POST",
1256
1330
  headers,
1257
1331
  body: data
@@ -1289,14 +1363,16 @@ var listGroups = async (config, options) => {
1289
1363
  if (!config || !config.pinataJwt) {
1290
1364
  throw new ValidationError("Pinata configuration or JWT is missing");
1291
1365
  }
1292
- const headers = {
1293
- "Content-Type": "application/json",
1294
- Authorization: `Bearer ${config?.pinataJwt}`
1295
- };
1296
- if (config.customHeaders) {
1297
- Object.assign(headers, config.customHeaders);
1366
+ let headers;
1367
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1368
+ headers = { ...config.customHeaders };
1369
+ } else {
1370
+ headers = {
1371
+ Authorization: `Bearer ${config.pinataJwt}`,
1372
+ "Content-Type": "application/json",
1373
+ Source: "sdk/listGroups"
1374
+ };
1298
1375
  }
1299
- headers["Source"] = headers["Source"] || "sdk/listGroups";
1300
1376
  const params = new URLSearchParams();
1301
1377
  if (options) {
1302
1378
  const { offset, nameContains, limit } = options;
@@ -1307,9 +1383,12 @@ var listGroups = async (config, options) => {
1307
1383
  if (limit !== void 0)
1308
1384
  params.append("limit", limit.toString());
1309
1385
  }
1310
- const url = `https://api.pinata.cloud/groups?${params.toString()}`;
1386
+ let endpoint = "https://api.pinata.cloud";
1387
+ if (config.endpointUrl) {
1388
+ endpoint = config.endpointUrl;
1389
+ }
1311
1390
  try {
1312
- const request = await fetch(url, {
1391
+ const request = await fetch(`${endpoint}/groups?${params.toString()}`, {
1313
1392
  method: "GET",
1314
1393
  headers
1315
1394
  });
@@ -1346,22 +1425,25 @@ var getGroup = async (config, options) => {
1346
1425
  if (!config || !config.pinataJwt) {
1347
1426
  throw new ValidationError("Pinata configuration or JWT is missing");
1348
1427
  }
1349
- const headers = {
1350
- "Content-Type": "application/json",
1351
- Authorization: `Bearer ${config?.pinataJwt}`
1352
- };
1353
- if (config.customHeaders) {
1354
- Object.assign(headers, config.customHeaders);
1428
+ let headers;
1429
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1430
+ headers = { ...config.customHeaders };
1431
+ } else {
1432
+ headers = {
1433
+ Authorization: `Bearer ${config.pinataJwt}`,
1434
+ "Content-Type": "application/json",
1435
+ Source: "sdk/getGroup"
1436
+ };
1437
+ }
1438
+ let endpoint = "https://api.pinata.cloud";
1439
+ if (config.endpointUrl) {
1440
+ endpoint = config.endpointUrl;
1355
1441
  }
1356
- headers["Source"] = headers["Source"] || "sdk/getGroup";
1357
1442
  try {
1358
- const request = await fetch(
1359
- `https://api.pinata.cloud/groups/${options.groupId}`,
1360
- {
1361
- method: "GET",
1362
- headers
1363
- }
1364
- );
1443
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1444
+ method: "GET",
1445
+ headers
1446
+ });
1365
1447
  if (!request.ok) {
1366
1448
  const errorData = await request.json();
1367
1449
  if (request.status === 401) {
@@ -1400,23 +1482,26 @@ var addToGroup = async (config, options) => {
1400
1482
  const data = JSON.stringify({
1401
1483
  cids: options.cids
1402
1484
  });
1403
- const headers = {
1404
- "Content-Type": "application/json",
1405
- Authorization: `Bearer ${config?.pinataJwt}`
1406
- };
1407
- if (config.customHeaders) {
1408
- Object.assign(headers, config.customHeaders);
1485
+ let headers;
1486
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1487
+ headers = { ...config.customHeaders };
1488
+ } else {
1489
+ headers = {
1490
+ Authorization: `Bearer ${config.pinataJwt}`,
1491
+ "Content-Type": "application/json",
1492
+ Source: "sdk/addToGroup"
1493
+ };
1494
+ }
1495
+ let endpoint = "https://api.pinata.cloud";
1496
+ if (config.endpointUrl) {
1497
+ endpoint = config.endpointUrl;
1409
1498
  }
1410
- headers["Source"] = headers["Source"] || "sdk/addToGroup";
1411
1499
  try {
1412
- const request = await fetch(
1413
- `https://api.pinata.cloud/groups/${options.groupId}/cids`,
1414
- {
1415
- method: "PUT",
1416
- headers,
1417
- body: data
1418
- }
1419
- );
1500
+ const request = await fetch(`${endpoint}/groups/${options.groupId}/cids`, {
1501
+ method: "PUT",
1502
+ headers,
1503
+ body: data
1504
+ });
1420
1505
  if (!request.ok) {
1421
1506
  const errorData = await request.json();
1422
1507
  if (request.status === 401) {
@@ -1455,23 +1540,26 @@ var updateGroup = async (config, options) => {
1455
1540
  const data = JSON.stringify({
1456
1541
  name: options.name
1457
1542
  });
1458
- const headers = {
1459
- "Content-Type": "application/json",
1460
- Authorization: `Bearer ${config?.pinataJwt}`
1461
- };
1462
- if (config.customHeaders) {
1463
- Object.assign(headers, config.customHeaders);
1543
+ let headers;
1544
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1545
+ headers = { ...config.customHeaders };
1546
+ } else {
1547
+ headers = {
1548
+ Authorization: `Bearer ${config.pinataJwt}`,
1549
+ "Content-Type": "application/json",
1550
+ Source: "sdk/updateGroup"
1551
+ };
1552
+ }
1553
+ let endpoint = "https://api.pinata.cloud";
1554
+ if (config.endpointUrl) {
1555
+ endpoint = config.endpointUrl;
1464
1556
  }
1465
- headers["Source"] = headers["Source"] || "sdk/updateGroup";
1466
1557
  try {
1467
- const request = await fetch(
1468
- `https://api.pinata.cloud/groups/${options.groupId}`,
1469
- {
1470
- method: "PUT",
1471
- headers,
1472
- body: data
1473
- }
1474
- );
1558
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1559
+ method: "PUT",
1560
+ headers,
1561
+ body: data
1562
+ });
1475
1563
  if (!request.ok) {
1476
1564
  const errorData = await request.json();
1477
1565
  if (request.status === 401) {
@@ -1505,26 +1593,29 @@ var removeFromGroup = async (config, options) => {
1505
1593
  if (!config || !config.pinataJwt) {
1506
1594
  throw new ValidationError("Pinata configuration or JWT is missing");
1507
1595
  }
1508
- const headers = {
1509
- "Content-Type": "application/json",
1510
- Authorization: `Bearer ${config?.pinataJwt}`
1511
- };
1512
- if (config.customHeaders) {
1513
- Object.assign(headers, config.customHeaders);
1596
+ let headers;
1597
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1598
+ headers = { ...config.customHeaders };
1599
+ } else {
1600
+ headers = {
1601
+ Authorization: `Bearer ${config.pinataJwt}`,
1602
+ "Content-Type": "application/json",
1603
+ Source: "sdk/removeFromGroup"
1604
+ };
1514
1605
  }
1515
- headers["Source"] = headers["Source"] || "sdk/removeFromGroup";
1516
1606
  const data = JSON.stringify({
1517
1607
  cids: options.cids
1518
1608
  });
1609
+ let endpoint = "https://api.pinata.cloud";
1610
+ if (config.endpointUrl) {
1611
+ endpoint = config.endpointUrl;
1612
+ }
1519
1613
  try {
1520
- const request = await fetch(
1521
- `https://api.pinata.cloud/groups/${options.groupId}/cids`,
1522
- {
1523
- method: "DELETE",
1524
- headers,
1525
- body: data
1526
- }
1527
- );
1614
+ const request = await fetch(`${endpoint}/groups/${options.groupId}/cids`, {
1615
+ method: "DELETE",
1616
+ headers,
1617
+ body: data
1618
+ });
1528
1619
  if (!request.ok) {
1529
1620
  const errorData = await request.json();
1530
1621
  if (request.status === 401) {
@@ -1562,22 +1653,25 @@ var deleteGroup = async (config, options) => {
1562
1653
  if (!config || !config.pinataJwt) {
1563
1654
  throw new ValidationError("Pinata configuration or JWT is missing");
1564
1655
  }
1565
- const headers = {
1566
- "Content-Type": "application/json",
1567
- Authorization: `Bearer ${config?.pinataJwt}`
1568
- };
1569
- if (config.customHeaders) {
1570
- Object.assign(headers, config.customHeaders);
1656
+ let headers;
1657
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1658
+ headers = { ...config.customHeaders };
1659
+ } else {
1660
+ headers = {
1661
+ Authorization: `Bearer ${config.pinataJwt}`,
1662
+ "Content-Type": "application/json",
1663
+ Source: "sdk/deleteGroup"
1664
+ };
1665
+ }
1666
+ let endpoint = "https://api.pinata.cloud";
1667
+ if (config.endpointUrl) {
1668
+ endpoint = config.endpointUrl;
1571
1669
  }
1572
- headers["Source"] = headers["Source"] || "sdk/deleteGroup";
1573
1670
  try {
1574
- const request = await fetch(
1575
- `https://api.pinata.cloud/groups/${options.groupId}`,
1576
- {
1577
- method: "DELETE",
1578
- headers
1579
- }
1580
- );
1671
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1672
+ method: "DELETE",
1673
+ headers
1674
+ });
1581
1675
  if (!request.ok) {
1582
1676
  const errorData = await request.json();
1583
1677
  if (request.status === 401) {
@@ -1614,17 +1708,23 @@ var addSignature = async (config, options) => {
1614
1708
  const data = JSON.stringify({
1615
1709
  signature: options.signature
1616
1710
  });
1617
- const headers = {
1618
- "Content-Type": "application/json",
1619
- Authorization: `Bearer ${config?.pinataJwt}`
1620
- };
1621
- if (config.customHeaders) {
1622
- Object.assign(headers, config.customHeaders);
1711
+ let headers;
1712
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1713
+ headers = { ...config.customHeaders };
1714
+ } else {
1715
+ headers = {
1716
+ Authorization: `Bearer ${config.pinataJwt}`,
1717
+ "Content-Type": "application/json",
1718
+ Source: "sdk/addSignature"
1719
+ };
1720
+ }
1721
+ let endpoint = "https://api.pinata.cloud";
1722
+ if (config.endpointUrl) {
1723
+ endpoint = config.endpointUrl;
1623
1724
  }
1624
- headers["Source"] = headers["Source"] || "sdk/addSignature";
1625
1725
  try {
1626
1726
  const request = await fetch(
1627
- `https://api.pinata.cloud/v3/ipfs/signature/${options.cid}`,
1727
+ `${endpoint}/v3/ipfs/signature/${options.cid}`,
1628
1728
  {
1629
1729
  method: "POST",
1630
1730
  headers,
@@ -1673,22 +1773,25 @@ var getSignature = async (config, cid) => {
1673
1773
  if (!config || !config.pinataJwt) {
1674
1774
  throw new ValidationError("Pinata configuration or JWT is missing");
1675
1775
  }
1676
- const headers = {
1677
- "Content-Type": "application/json",
1678
- Authorization: `Bearer ${config?.pinataJwt}`
1679
- };
1680
- if (config.customHeaders) {
1681
- Object.assign(headers, config.customHeaders);
1776
+ let headers;
1777
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1778
+ headers = { ...config.customHeaders };
1779
+ } else {
1780
+ headers = {
1781
+ Authorization: `Bearer ${config.pinataJwt}`,
1782
+ "Content-Type": "application/json",
1783
+ Source: "sdk/getSignature"
1784
+ };
1785
+ }
1786
+ let endpoint = "https://api.pinata.cloud";
1787
+ if (config.endpointUrl) {
1788
+ endpoint = config.endpointUrl;
1682
1789
  }
1683
- headers["Source"] = headers["Source"] || "sdk/getSignature";
1684
1790
  try {
1685
- const request = await fetch(
1686
- `https://api.pinata.cloud/v3/ipfs/signature/${cid}`,
1687
- {
1688
- method: "GET",
1689
- headers
1690
- }
1691
- );
1791
+ const request = await fetch(`${endpoint}/v3/ipfs/signature/${cid}`, {
1792
+ method: "GET",
1793
+ headers
1794
+ });
1692
1795
  if (!request.ok) {
1693
1796
  const errorData = await request.json();
1694
1797
  if (request.status === 401) {
@@ -1724,22 +1827,25 @@ var removeSignature = async (config, cid) => {
1724
1827
  if (!config || !config.pinataJwt) {
1725
1828
  throw new ValidationError("Pinata configuration or JWT is missing");
1726
1829
  }
1727
- const headers = {
1728
- "Content-Type": "application/json",
1729
- Authorization: `Bearer ${config?.pinataJwt}`
1730
- };
1731
- if (config.customHeaders) {
1732
- Object.assign(headers, config.customHeaders);
1830
+ let headers;
1831
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1832
+ headers = { ...config.customHeaders };
1833
+ } else {
1834
+ headers = {
1835
+ Authorization: `Bearer ${config.pinataJwt}`,
1836
+ "Content-Type": "application/json",
1837
+ Source: "sdk/removeSignature"
1838
+ };
1839
+ }
1840
+ let endpoint = "https://api.pinata.cloud";
1841
+ if (config.endpointUrl) {
1842
+ endpoint = config.endpointUrl;
1733
1843
  }
1734
- headers["Source"] = headers["Source"] || "sdk/removeSignature";
1735
1844
  try {
1736
- const request = await fetch(
1737
- `https://api.pinata.cloud/v3/ipfs/signature/${cid}`,
1738
- {
1739
- method: "DELETE",
1740
- headers
1741
- }
1742
- );
1845
+ const request = await fetch(`${endpoint}/v3/ipfs/signature/${cid}`, {
1846
+ method: "DELETE",
1847
+ headers
1848
+ });
1743
1849
  if (!request.ok) {
1744
1850
  const errorData = await request.json();
1745
1851
  if (request.status === 401) {
@@ -1769,6 +1875,210 @@ var removeSignature = async (config, cid) => {
1769
1875
  }
1770
1876
  };
1771
1877
 
1878
+ // src/core/gateway/analyticsTopUsage.ts
1879
+ var analyticsTopUsage = async (config, options) => {
1880
+ if (!config || !config.pinataJwt) {
1881
+ throw new ValidationError("Pinata configuration or JWT is missing");
1882
+ }
1883
+ const params = new URLSearchParams({
1884
+ includesCount: "false"
1885
+ });
1886
+ if (options) {
1887
+ const {
1888
+ cid,
1889
+ gateway_domain,
1890
+ start_date,
1891
+ end_date,
1892
+ file_name,
1893
+ user_agent,
1894
+ country,
1895
+ region,
1896
+ referer,
1897
+ limit,
1898
+ sort_order,
1899
+ sort_by,
1900
+ attribute
1901
+ } = options;
1902
+ if (cid)
1903
+ params.append("cid", cid);
1904
+ if (gateway_domain)
1905
+ params.append("gateway_domain", gateway_domain);
1906
+ if (start_date)
1907
+ params.append("start_date", start_date);
1908
+ if (end_date)
1909
+ params.append("end_date", end_date);
1910
+ if (file_name)
1911
+ params.append("file_name", file_name);
1912
+ if (user_agent)
1913
+ params.append("user_agent", user_agent.toString());
1914
+ if (country)
1915
+ params.append("country", country.toString());
1916
+ if (region)
1917
+ params.append("region", region);
1918
+ if (referer)
1919
+ params.append("referer", referer.toString());
1920
+ if (limit)
1921
+ params.append("limit", limit.toString());
1922
+ if (sort_order)
1923
+ params.append("sort_order", sort_order);
1924
+ if (sort_by)
1925
+ params.append("sort_by", sort_by);
1926
+ if (attribute)
1927
+ params.append("by", attribute);
1928
+ }
1929
+ let endpoint = "https://api.pinata.cloud";
1930
+ if (config.endpointUrl) {
1931
+ endpoint = config.endpointUrl;
1932
+ }
1933
+ const url = `${endpoint}/v3/ipfs/gateway_analytics_top?${params.toString()}`;
1934
+ try {
1935
+ let headers;
1936
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1937
+ headers = { ...config.customHeaders };
1938
+ } else {
1939
+ headers = {
1940
+ Authorization: `Bearer ${config.pinataJwt}`,
1941
+ Source: "sdk/analyticsTopUsage"
1942
+ };
1943
+ }
1944
+ const request = await fetch(url, {
1945
+ method: "GET",
1946
+ headers
1947
+ });
1948
+ if (!request.ok) {
1949
+ const errorData = await request.json();
1950
+ if (request.status === 401) {
1951
+ throw new AuthenticationError(
1952
+ "Authentication failed",
1953
+ request.status,
1954
+ errorData
1955
+ );
1956
+ }
1957
+ throw new NetworkError(
1958
+ `HTTP error! status: ${request.status}`,
1959
+ request.status,
1960
+ errorData
1961
+ );
1962
+ }
1963
+ const res = await request.json();
1964
+ const resData = res.data;
1965
+ return resData;
1966
+ } catch (error) {
1967
+ if (error instanceof PinataError) {
1968
+ throw error;
1969
+ }
1970
+ if (error instanceof Error) {
1971
+ throw new PinataError(
1972
+ `Error processing anaytics usage: ${error.message}`
1973
+ );
1974
+ }
1975
+ throw new PinataError(
1976
+ "An unknown error occurred while fetching gateway usage"
1977
+ );
1978
+ }
1979
+ };
1980
+
1981
+ // src/core/gateway/analyticsDateInterval.ts
1982
+ var analyticsDateInterval = async (config, options) => {
1983
+ if (!config || !config.pinataJwt) {
1984
+ throw new ValidationError("Pinata configuration or JWT is missing");
1985
+ }
1986
+ const params = new URLSearchParams();
1987
+ if (options) {
1988
+ const {
1989
+ cid,
1990
+ gateway_domain,
1991
+ start_date,
1992
+ end_date,
1993
+ file_name,
1994
+ user_agent,
1995
+ country,
1996
+ region,
1997
+ referer,
1998
+ limit,
1999
+ sort_order,
2000
+ date_interval,
2001
+ sort_by
2002
+ } = options;
2003
+ if (cid)
2004
+ params.append("cid", cid);
2005
+ if (gateway_domain)
2006
+ params.append("gateway_domain", gateway_domain);
2007
+ if (start_date)
2008
+ params.append("start_date", start_date);
2009
+ if (end_date)
2010
+ params.append("end_date", end_date);
2011
+ if (file_name)
2012
+ params.append("file_name", file_name);
2013
+ if (user_agent)
2014
+ params.append("user_agent", user_agent.toString());
2015
+ if (country)
2016
+ params.append("country", country.toString());
2017
+ if (region)
2018
+ params.append("region", region);
2019
+ if (referer)
2020
+ params.append("referer", referer.toString());
2021
+ if (limit)
2022
+ params.append("limit", limit.toString());
2023
+ if (sort_order)
2024
+ params.append("sort_order", sort_order);
2025
+ if (sort_by)
2026
+ params.append("sort_by", sort_by);
2027
+ if (date_interval)
2028
+ params.append("by", date_interval);
2029
+ }
2030
+ let endpoint = "https://api.pinata.cloud";
2031
+ if (config.endpointUrl) {
2032
+ endpoint = config.endpointUrl;
2033
+ }
2034
+ const url = `${endpoint}/v3/ipfs/gateway_analytics_time_series?${params.toString()}`;
2035
+ try {
2036
+ let headers;
2037
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
2038
+ headers = { ...config.customHeaders };
2039
+ } else {
2040
+ headers = {
2041
+ Authorization: `Bearer ${config.pinataJwt}`,
2042
+ Source: "sdk/analyticsDateInterval"
2043
+ };
2044
+ }
2045
+ const request = await fetch(url, {
2046
+ method: "GET",
2047
+ headers
2048
+ });
2049
+ if (!request.ok) {
2050
+ const errorData = await request.json();
2051
+ if (request.status === 401) {
2052
+ throw new AuthenticationError(
2053
+ "Authentication failed",
2054
+ request.status,
2055
+ errorData
2056
+ );
2057
+ }
2058
+ throw new NetworkError(
2059
+ `HTTP error! status: ${request.status}`,
2060
+ request.status,
2061
+ errorData
2062
+ );
2063
+ }
2064
+ const res = await request.json();
2065
+ const resData = res.data;
2066
+ return resData;
2067
+ } catch (error) {
2068
+ if (error instanceof PinataError) {
2069
+ throw error;
2070
+ }
2071
+ if (error instanceof Error) {
2072
+ throw new PinataError(
2073
+ `Error processing anaytics usage: ${error.message}`
2074
+ );
2075
+ }
2076
+ throw new PinataError(
2077
+ "An unknown error occurred while fetching gateway usage"
2078
+ );
2079
+ }
2080
+ };
2081
+
1772
2082
  // src/core/pinataSDK.ts
1773
2083
  var formatConfig = (config) => {
1774
2084
  let gateway = config?.pinataGateway;
@@ -1790,6 +2100,18 @@ var PinataSDK = class {
1790
2100
  this.groups = new Groups(this.config);
1791
2101
  this.signatures = new Signatures(this.config);
1792
2102
  }
2103
+ setNewHeaders(headers) {
2104
+ if (!this.config) {
2105
+ this.config = { pinataJwt: "", customHeaders: {} };
2106
+ }
2107
+ this.config.customHeaders = { ...this.config.customHeaders, ...headers };
2108
+ this.upload.updateConfig(this.config);
2109
+ this.gateways.updateConfig(this.config);
2110
+ this.usage.updateConfig(this.config);
2111
+ this.keys.updateConfig(this.config);
2112
+ this.groups.updateConfig(this.config);
2113
+ this.signatures.updateConfig(this.config);
2114
+ }
1793
2115
  testAuthentication() {
1794
2116
  return testAuthentication(this.config);
1795
2117
  }
@@ -1861,6 +2183,9 @@ var Upload = class {
1861
2183
  constructor(config) {
1862
2184
  this.config = formatConfig(config);
1863
2185
  }
2186
+ updateConfig(newConfig) {
2187
+ this.config = newConfig;
2188
+ }
1864
2189
  file(file, options) {
1865
2190
  return new UploadBuilder(this.config, uploadFile, file, options);
1866
2191
  }
@@ -1982,12 +2307,34 @@ var Gateways = class {
1982
2307
  constructor(config) {
1983
2308
  this.config = formatConfig(config);
1984
2309
  }
2310
+ updateConfig(newConfig) {
2311
+ this.config = newConfig;
2312
+ }
1985
2313
  get(cid) {
1986
2314
  return getCid(this.config, cid);
1987
2315
  }
1988
2316
  convert(url) {
1989
2317
  return convertIPFSUrl(this.config, url);
1990
2318
  }
2319
+ topUsageAnalytics(options) {
2320
+ return new TopGatewayAnalyticsBuilder(
2321
+ this.config,
2322
+ options.domain,
2323
+ options.start,
2324
+ options.end,
2325
+ options.sortBy,
2326
+ options.attribute
2327
+ );
2328
+ }
2329
+ dateIntervalAnalytics(options) {
2330
+ return new TimeIntervalGatewayAnalyticsBuilder(
2331
+ this.config,
2332
+ options.domain,
2333
+ options.start,
2334
+ options.end,
2335
+ options.interval
2336
+ );
2337
+ }
1991
2338
  };
1992
2339
  var FilterPinJobs = class {
1993
2340
  constructor(config) {
@@ -2067,6 +2414,9 @@ var Usage = class {
2067
2414
  constructor(config) {
2068
2415
  this.config = formatConfig(config);
2069
2416
  }
2417
+ updateConfig(newConfig) {
2418
+ this.config = newConfig;
2419
+ }
2070
2420
  pinnedFileCount() {
2071
2421
  return pinnedFileCount(this.config);
2072
2422
  }
@@ -2078,6 +2428,9 @@ var Keys = class {
2078
2428
  constructor(config) {
2079
2429
  this.config = formatConfig(config);
2080
2430
  }
2431
+ updateConfig(newConfig) {
2432
+ this.config = newConfig;
2433
+ }
2081
2434
  create(options) {
2082
2435
  return createKey(this.config, options);
2083
2436
  }
@@ -2164,6 +2517,9 @@ var Groups = class {
2164
2517
  constructor(config) {
2165
2518
  this.config = formatConfig(config);
2166
2519
  }
2520
+ updateConfig(newConfig) {
2521
+ this.config = newConfig;
2522
+ }
2167
2523
  create(options) {
2168
2524
  return createGroup(this.config, options);
2169
2525
  }
@@ -2254,6 +2610,9 @@ var Signatures = class {
2254
2610
  constructor(config) {
2255
2611
  this.config = formatConfig(config);
2256
2612
  }
2613
+ updateConfig(newConfig) {
2614
+ this.config = newConfig;
2615
+ }
2257
2616
  add(options) {
2258
2617
  return addSignature(this.config, options);
2259
2618
  }
@@ -2264,6 +2623,105 @@ var Signatures = class {
2264
2623
  return removeSignature(this.config, cid);
2265
2624
  }
2266
2625
  };
2626
+ var GatewayAnalyticsBuilder = class {
2627
+ constructor(config, query) {
2628
+ this.requestCount = 0;
2629
+ this.lastRequestTime = 0;
2630
+ this.MAX_REQUESTS_PER_MINUTE = 30;
2631
+ this.MINUTE_IN_MS = 6e4;
2632
+ this.config = config;
2633
+ this.query = query;
2634
+ }
2635
+ cid(cid) {
2636
+ this.query.cid = cid;
2637
+ return this;
2638
+ }
2639
+ fileName(fileName) {
2640
+ this.query.file_name = fileName;
2641
+ return this;
2642
+ }
2643
+ userAgent(userAgent) {
2644
+ this.query.user_agent = userAgent;
2645
+ return this;
2646
+ }
2647
+ country(country) {
2648
+ this.query.country = country;
2649
+ return this;
2650
+ }
2651
+ region(region) {
2652
+ this.query.region = region;
2653
+ return this;
2654
+ }
2655
+ referer(referer) {
2656
+ this.query.referer = referer;
2657
+ return this;
2658
+ }
2659
+ limit(limit) {
2660
+ this.query.limit = limit;
2661
+ return this;
2662
+ }
2663
+ sort(order) {
2664
+ this.query.sort_order = order;
2665
+ return this;
2666
+ }
2667
+ async rateLimit() {
2668
+ this.requestCount++;
2669
+ const now = Date.now();
2670
+ if (this.requestCount >= this.MAX_REQUESTS_PER_MINUTE) {
2671
+ const timePassedSinceLastRequest = now - this.lastRequestTime;
2672
+ if (timePassedSinceLastRequest < this.MINUTE_IN_MS) {
2673
+ const delayTime = this.MINUTE_IN_MS - timePassedSinceLastRequest;
2674
+ await new Promise((resolve) => setTimeout(resolve, delayTime));
2675
+ }
2676
+ this.requestCount = 0;
2677
+ }
2678
+ this.lastRequestTime = Date.now();
2679
+ }
2680
+ async getAnalytics() {
2681
+ await this.rateLimit();
2682
+ throw new Error("getAnalytics method must be implemented in derived class");
2683
+ }
2684
+ then(onfulfilled) {
2685
+ return this.getAnalytics().then(onfulfilled);
2686
+ }
2687
+ };
2688
+ var TopGatewayAnalyticsBuilder = class extends GatewayAnalyticsBuilder {
2689
+ constructor(config, domain, start, end, sortBy, attribute) {
2690
+ super(config, {
2691
+ gateway_domain: domain,
2692
+ start_date: start,
2693
+ end_date: end,
2694
+ sort_by: sortBy,
2695
+ attribute
2696
+ });
2697
+ }
2698
+ async getAnalytics() {
2699
+ return analyticsTopUsage(this.config, this.query);
2700
+ }
2701
+ async all() {
2702
+ return this.getAnalytics();
2703
+ }
2704
+ };
2705
+ var TimeIntervalGatewayAnalyticsBuilder = class extends GatewayAnalyticsBuilder {
2706
+ constructor(config, domain, start, end, dateInterval) {
2707
+ super(config, {
2708
+ gateway_domain: domain,
2709
+ start_date: start,
2710
+ end_date: end,
2711
+ date_interval: dateInterval
2712
+ });
2713
+ }
2714
+ sortBy(sortBy) {
2715
+ this.query.sort_by = sortBy;
2716
+ return this;
2717
+ }
2718
+ async getAnalytics() {
2719
+ return analyticsDateInterval(this.config, this.query);
2720
+ }
2721
+ async all() {
2722
+ return this.getAnalytics();
2723
+ }
2724
+ };
2267
2725
  // Annotate the CommonJS export names for ESM import in node:
2268
2726
  0 && (module.exports = {
2269
2727
  PinataSDK