pinata 0.1.9 → 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.mjs CHANGED
@@ -31,21 +31,24 @@ var testAuthentication = async (config) => {
31
31
  if (!config || !config.pinataJwt) {
32
32
  throw new ValidationError("Pinata configuration or JWT is missing");
33
33
  }
34
- const headers = {
35
- Authorization: `Bearer ${config?.pinataJwt}`
36
- };
37
- if (config.customHeaders) {
38
- Object.assign(headers, config.customHeaders);
34
+ let headers;
35
+ let endpoint = "https://api.pinata.cloud";
36
+ if (config.endpointUrl) {
37
+ endpoint = config.endpointUrl;
38
+ }
39
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
40
+ headers = { ...config.customHeaders };
41
+ } else {
42
+ headers = {
43
+ Authorization: `Bearer ${config.pinataJwt}`,
44
+ Source: "sdk/testAuthentication"
45
+ };
39
46
  }
40
- headers["Source"] = headers["Source"] || "sdk/testAuthentication";
41
47
  try {
42
- const request = await fetch(
43
- "https://api.pinata.cloud/data/testAuthentication",
44
- {
45
- method: "GET",
46
- headers
47
- }
48
- );
48
+ const request = await fetch(`${endpoint}/data/testAuthentication`, {
49
+ method: "GET",
50
+ headers
51
+ });
49
52
  if (!request.ok) {
50
53
  const errorData = await request.json();
51
54
  if (request.status === 401) {
@@ -96,26 +99,29 @@ var uploadFile = async (config, file, options) => {
96
99
  data.append(
97
100
  "pinataMetadata",
98
101
  JSON.stringify({
99
- name: options?.metadata ? options.metadata.name : file.name,
102
+ name: options?.metadata?.name || file.name || "File from SDK",
100
103
  keyvalues: options?.metadata?.keyValues
101
104
  })
102
105
  );
103
- const headers = {
104
- Authorization: `Bearer ${jwt}`
105
- };
106
- if (config.customHeaders) {
107
- Object.assign(headers, config.customHeaders);
106
+ let headers;
107
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
108
+ headers = { ...config.customHeaders };
109
+ } else {
110
+ headers = {
111
+ Authorization: `Bearer ${jwt}`,
112
+ Source: "sdk/file"
113
+ };
114
+ }
115
+ let endpoint = "https://api.pinata.cloud";
116
+ if (config.endpointUrl) {
117
+ endpoint = config.endpointUrl;
108
118
  }
109
- headers["Source"] = headers["Source"] || "sdk/file";
110
119
  try {
111
- const request = await fetch(
112
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
113
- {
114
- method: "POST",
115
- headers,
116
- body: data
117
- }
118
- );
120
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
121
+ method: "POST",
122
+ headers,
123
+ body: data
124
+ });
119
125
  if (!request.ok) {
120
126
  const errorData = await request.json();
121
127
  if (request.status === 401) {
@@ -150,7 +156,7 @@ var uploadFileArray = async (config, files, options) => {
150
156
  throw new ValidationError("Pinata configuration or JWT is missing");
151
157
  }
152
158
  const jwt = options?.keys || config?.pinataJwt;
153
- const folder = options?.metadata?.name ?? "folder_from_sdk";
159
+ const folder = options?.metadata?.name || "folder_from_sdk";
154
160
  const data = new FormData();
155
161
  for (const file of Array.from(files)) {
156
162
  data.append("file", file, `${folder}/${file.name}`);
@@ -169,22 +175,25 @@ var uploadFileArray = async (config, files, options) => {
169
175
  groupId: options?.groupId
170
176
  })
171
177
  );
172
- const headers = {
173
- Authorization: `Bearer ${jwt}`
174
- };
175
- if (config.customHeaders) {
176
- Object.assign(headers, config.customHeaders);
178
+ let headers;
179
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
180
+ headers = { ...config.customHeaders };
181
+ } else {
182
+ headers = {
183
+ Authorization: `Bearer ${jwt}`,
184
+ Source: "sdk/fileArray"
185
+ };
186
+ }
187
+ let endpoint = "https://api.pinata.cloud";
188
+ if (config.endpointUrl) {
189
+ endpoint = config.endpointUrl;
177
190
  }
178
- headers["Source"] = headers["Source"] || "sdk/fileArray";
179
191
  try {
180
- const request = await fetch(
181
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
182
- {
183
- method: "POST",
184
- headers,
185
- body: data
186
- }
187
- );
192
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
193
+ method: "POST",
194
+ headers,
195
+ body: data
196
+ });
188
197
  if (!request.ok) {
189
198
  const errorData = await request.json();
190
199
  if (request.status === 401) {
@@ -240,22 +249,25 @@ var uploadBase64 = async (config, base64String, options) => {
240
249
  keyvalues: options?.metadata?.keyValues
241
250
  })
242
251
  );
243
- const headers = {
244
- Authorization: `Bearer ${jwt}`
245
- };
246
- if (config.customHeaders) {
247
- Object.assign(headers, config.customHeaders);
252
+ let headers;
253
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
254
+ headers = { ...config.customHeaders };
255
+ } else {
256
+ headers = {
257
+ Authorization: `Bearer ${jwt}`,
258
+ Source: "sdk/base64"
259
+ };
260
+ }
261
+ let endpoint = "https://api.pinata.cloud";
262
+ if (config.endpointUrl) {
263
+ endpoint = config.endpointUrl;
248
264
  }
249
- headers["Source"] = headers["Source"] || "sdk/base64";
250
265
  try {
251
- const request = await fetch(
252
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
253
- {
254
- method: "POST",
255
- headers,
256
- body: data
257
- }
258
- );
266
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
267
+ method: "POST",
268
+ headers,
269
+ body: data
270
+ });
259
271
  if (!request.ok) {
260
272
  const errorData = await request.json();
261
273
  if (request.status === 401) {
@@ -321,22 +333,25 @@ var uploadUrl = async (config, url, options) => {
321
333
  keyvalues: options?.metadata?.keyValues
322
334
  })
323
335
  );
324
- const headers = {
325
- Authorization: `Bearer ${jwt}`
326
- };
327
- if (config.customHeaders) {
328
- Object.assign(headers, config.customHeaders);
336
+ let headers;
337
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
338
+ headers = { ...config.customHeaders };
339
+ } else {
340
+ headers = {
341
+ Authorization: `Bearer ${jwt}`,
342
+ Source: "sdk/url"
343
+ };
344
+ }
345
+ let endpoint = "https://api.pinata.cloud";
346
+ if (config.endpointUrl) {
347
+ endpoint = config.endpointUrl;
329
348
  }
330
- headers["Source"] = headers["Source"] || "sdk/url";
331
349
  try {
332
- const request = await fetch(
333
- "https://api.pinata.cloud/pinning/pinFileToIPFS",
334
- {
335
- method: "POST",
336
- headers,
337
- body: data
338
- }
339
- );
350
+ const request = await fetch(`${endpoint}/pinning/pinFileToIPFS`, {
351
+ method: "POST",
352
+ headers,
353
+ body: data
354
+ });
340
355
  if (!request.ok) {
341
356
  const errorData = await request.json();
342
357
  if (request.status === 401) {
@@ -378,27 +393,30 @@ var uploadJson = async (config, jsonData, options) => {
378
393
  groupId: options?.groupId
379
394
  },
380
395
  pinataMetadata: {
381
- name: options?.metadata ? options.metadata.name : "json",
396
+ name: options?.metadata?.name || "json",
382
397
  keyvalues: options?.metadata?.keyValues
383
398
  }
384
399
  });
385
- const headers = {
386
- "Content-Type": "application/json",
387
- Authorization: `Bearer ${jwt}`
388
- };
389
- if (config.customHeaders) {
390
- Object.assign(headers, config.customHeaders);
400
+ let headers;
401
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
402
+ headers = { ...config.customHeaders };
403
+ } else {
404
+ headers = {
405
+ Authorization: `Bearer ${jwt}`,
406
+ "Content-Type": "application/json",
407
+ Source: "sdk/json"
408
+ };
409
+ }
410
+ let endpoint = "https://api.pinata.cloud";
411
+ if (config.endpointUrl) {
412
+ endpoint = config.endpointUrl;
391
413
  }
392
- headers["Source"] = headers["Source"] || "sdk/json";
393
414
  try {
394
- const request = await fetch(
395
- "https://api.pinata.cloud/pinning/pinJSONToIPFS",
396
- {
397
- method: "POST",
398
- headers,
399
- body: data
400
- }
401
- );
415
+ const request = await fetch(`${endpoint}/pinning/pinJSONToIPFS`, {
416
+ method: "POST",
417
+ headers,
418
+ body: data
419
+ });
402
420
  if (!request.ok) {
403
421
  const errorData = await request.json();
404
422
  if (request.status === 401) {
@@ -428,23 +446,25 @@ var uploadJson = async (config, jsonData, options) => {
428
446
  };
429
447
 
430
448
  // src/core/pinning/cid.ts
431
- var uploadCid = async (config, cid2, options) => {
449
+ var uploadCid = async (config, cid, options) => {
432
450
  if (!config || !config.pinataJwt) {
433
451
  throw new ValidationError("Pinata configuration or JWT is missing");
434
452
  }
435
453
  const jwt = options?.keys || config?.pinataJwt;
436
- const headers = {
437
- "Content-Type": "application/json",
438
- Authorization: `Bearer ${jwt}`
439
- };
440
- if (config.customHeaders) {
441
- Object.assign(headers, config.customHeaders);
454
+ let headers;
455
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
456
+ headers = { ...config.customHeaders };
457
+ } else {
458
+ headers = {
459
+ Authorization: `Bearer ${jwt}`,
460
+ "Content-Type": "application/json",
461
+ Source: "sdk/cid"
462
+ };
442
463
  }
443
- headers["Source"] = headers["Source"] || "sdk/cid";
444
464
  const data = JSON.stringify({
445
- hashToPin: cid2,
465
+ hashToPin: cid,
446
466
  pinataMetadata: {
447
- name: options?.metadata ? options?.metadata?.name : cid2,
467
+ name: options?.metadata ? options?.metadata?.name : cid,
448
468
  keyvalues: options?.metadata?.keyValues
449
469
  },
450
470
  pinataOptions: {
@@ -452,8 +472,12 @@ var uploadCid = async (config, cid2, options) => {
452
472
  groupId: options?.groupId
453
473
  }
454
474
  });
475
+ let endpoint = "https://api.pinata.cloud";
476
+ if (config.endpointUrl) {
477
+ endpoint = config.endpointUrl;
478
+ }
455
479
  try {
456
- const request = await fetch("https://api.pinata.cloud/pinning/pinByHash", {
480
+ const request = await fetch(`${endpoint}/pinning/pinByHash`, {
457
481
  method: "POST",
458
482
  headers,
459
483
  body: data
@@ -497,23 +521,25 @@ var unpinFile = async (config, files) => {
497
521
  throw new ValidationError("Pinata configuration or JWT is missing");
498
522
  }
499
523
  const responses = [];
500
- const headers = {
501
- "Content-Type": "application/json",
502
- Authorization: `Bearer ${config?.pinataJwt}`
503
- };
504
- if (config.customHeaders) {
505
- Object.assign(headers, config.customHeaders);
524
+ let headers;
525
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
526
+ headers = { ...config.customHeaders };
527
+ } else {
528
+ headers = {
529
+ Authorization: `Bearer ${config.pinataJwt}`,
530
+ Source: "sdk/unpin"
531
+ };
532
+ }
533
+ let endpoint = "https://api.pinata.cloud";
534
+ if (config.endpointUrl) {
535
+ endpoint = config.endpointUrl;
506
536
  }
507
- headers["Source"] = headers["Source"] || "sdk/unpin";
508
537
  for (const hash of files) {
509
538
  try {
510
- const response = await fetch(
511
- `https://api.pinata.cloud/pinning/unpin/${hash}`,
512
- {
513
- method: "DELETE",
514
- headers
515
- }
516
- );
539
+ const response = await fetch(`${endpoint}/pinning/unpin/${hash}`, {
540
+ method: "DELETE",
541
+ headers
542
+ });
517
543
  await wait(300);
518
544
  if (!response.ok) {
519
545
  const errorData = await response.json();
@@ -563,7 +589,7 @@ var listFiles = async (config, options) => {
563
589
  });
564
590
  if (options) {
565
591
  const {
566
- cid: cid2,
592
+ cid,
567
593
  pinStart,
568
594
  pinEnd,
569
595
  pinSizeMin,
@@ -576,8 +602,8 @@ var listFiles = async (config, options) => {
576
602
  operator,
577
603
  groupId
578
604
  } = options;
579
- if (cid2)
580
- params.append("cid", cid2);
605
+ if (cid)
606
+ params.append("cid", cid);
581
607
  if (pinStart)
582
608
  params.append("pinStart", pinStart);
583
609
  if (pinEnd)
@@ -601,15 +627,21 @@ var listFiles = async (config, options) => {
601
627
  params.append("metadata[keyvalues]", keyValueParam);
602
628
  }
603
629
  }
604
- const url = `https://api.pinata.cloud/data/pinList?status=pinned&${params.toString()}`;
630
+ let endpoint = "https://api.pinata.cloud";
631
+ if (config.endpointUrl) {
632
+ endpoint = config.endpointUrl;
633
+ }
634
+ const url = `${endpoint}/data/pinList?status=pinned&${params.toString()}`;
605
635
  try {
606
- const headers = {
607
- Authorization: `Bearer ${config?.pinataJwt}`
608
- };
609
- if (config.customHeaders) {
610
- Object.assign(headers, config.customHeaders);
636
+ let headers;
637
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
638
+ headers = { ...config.customHeaders };
639
+ } else {
640
+ headers = {
641
+ Authorization: `Bearer ${config.pinataJwt}`,
642
+ Source: "sdk/listFiles"
643
+ };
611
644
  }
612
- headers["Source"] = headers["Source"] || "sdk/listFiles";
613
645
  const request = await fetch(url, {
614
646
  method: "GET",
615
647
  headers
@@ -652,23 +684,26 @@ var updateMetadata = async (config, options) => {
652
684
  name: options.name,
653
685
  keyvalues: options.keyValues
654
686
  };
655
- const headers = {
656
- "Content-Type": "application/json",
657
- Authorization: `Bearer ${config?.pinataJwt}`
658
- };
659
- if (config.customHeaders) {
660
- Object.assign(headers, config.customHeaders);
687
+ let headers;
688
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
689
+ headers = { ...config.customHeaders };
690
+ } else {
691
+ headers = {
692
+ Authorization: `Bearer ${config.pinataJwt}`,
693
+ "Content-Type": "application/json",
694
+ Source: "sdk/updateMetadata"
695
+ };
696
+ }
697
+ let endpoint = "https://api.pinata.cloud";
698
+ if (config.endpointUrl) {
699
+ endpoint = config.endpointUrl;
661
700
  }
662
- headers["Source"] = headers["Source"] || "sdk/updateMetadata";
663
701
  try {
664
- const request = await fetch(
665
- "https://api.pinata.cloud/pinning/hashMetadata",
666
- {
667
- method: "PUT",
668
- headers,
669
- body: JSON.stringify(data)
670
- }
671
- );
702
+ const request = await fetch(`${endpoint}/pinning/hashMetadata`, {
703
+ method: "PUT",
704
+ headers,
705
+ body: JSON.stringify(data)
706
+ });
672
707
  if (!request.ok) {
673
708
  const errorData = await request.json();
674
709
  if (request.status === 401) {
@@ -700,11 +735,18 @@ var updateMetadata = async (config, options) => {
700
735
  };
701
736
 
702
737
  // src/utils/gateway-tools.ts
703
- import * as isIPFS from "is-ipfs";
704
- function containsCID(input) {
738
+ var isIPFSModule;
739
+ async function getIsIPFS() {
740
+ if (!isIPFSModule) {
741
+ isIPFSModule = await import("is-ipfs");
742
+ }
743
+ return isIPFSModule;
744
+ }
745
+ async function containsCID(input) {
705
746
  if (typeof input !== "string") {
706
747
  throw new Error("Input is not a string");
707
748
  }
749
+ const isIPFS = await getIsIPFS();
708
750
  const startsWithCID = (str) => {
709
751
  const parts = str.split("/");
710
752
  return isIPFS.cid(parts[0]) ? parts[0] : null;
@@ -722,11 +764,11 @@ function containsCID(input) {
722
764
  } catch (error) {
723
765
  const parts = input.split(/\/|\?/);
724
766
  for (const part of parts) {
725
- const cid2 = startsWithCID(part);
726
- if (cid2) {
767
+ const cid = startsWithCID(part);
768
+ if (cid) {
727
769
  return {
728
770
  containsCid: true,
729
- cid: cid2
771
+ cid
730
772
  };
731
773
  }
732
774
  }
@@ -746,11 +788,11 @@ function containsCID(input) {
746
788
  }
747
789
  const pathParts = url.pathname.split("/");
748
790
  for (const part of pathParts) {
749
- const cid2 = startsWithCID(part);
750
- if (cid2) {
791
+ const cid = startsWithCID(part);
792
+ if (cid) {
751
793
  return {
752
794
  containsCid: true,
753
- cid: cid2
795
+ cid
754
796
  };
755
797
  }
756
798
  }
@@ -759,8 +801,8 @@ function containsCID(input) {
759
801
  cid: null
760
802
  };
761
803
  }
762
- function convertToDesiredGateway(sourceUrl, desiredGatewayPrefix) {
763
- const results = containsCID(sourceUrl);
804
+ async function convertToDesiredGateway(sourceUrl, desiredGatewayPrefix) {
805
+ const results = await containsCID(sourceUrl);
764
806
  if (results.containsCid !== true) {
765
807
  throw new Error("url does not contain CID");
766
808
  }
@@ -787,13 +829,13 @@ function convertToDesiredGateway(sourceUrl, desiredGatewayPrefix) {
787
829
  }
788
830
 
789
831
  // src/core/gateway/getCid.ts
790
- var getCid = async (config, cid2) => {
832
+ var getCid = async (config, cid) => {
791
833
  if (!config || !config.pinataJwt) {
792
834
  throw new ValidationError("Pinata configuration or JWT is missing");
793
835
  }
794
836
  let data;
795
837
  let newUrl;
796
- newUrl = convertToDesiredGateway(cid2, config?.pinataGateway);
838
+ newUrl = await convertToDesiredGateway(cid, config?.pinataGateway);
797
839
  if (config?.pinataGatewayKey) {
798
840
  newUrl = `${newUrl}?pinataGatewayToken=${config?.pinataGatewayKey}`;
799
841
  }
@@ -846,9 +888,9 @@ var getCid = async (config, cid2) => {
846
888
  };
847
889
 
848
890
  // src/core/gateway/convertIPFSUrl.ts
849
- var convertIPFSUrl = (config, url) => {
891
+ var convertIPFSUrl = async (config, url) => {
850
892
  let newUrl;
851
- newUrl = convertToDesiredGateway(url, config?.pinataGateway);
893
+ newUrl = await convertToDesiredGateway(url, config?.pinataGateway);
852
894
  if (config?.pinataGatewayKey) {
853
895
  `${newUrl}?pinataGatewayToken=${config?.pinataGatewayKey}`;
854
896
  }
@@ -864,9 +906,9 @@ var pinJobs = async (config, options) => {
864
906
  includesCount: "false"
865
907
  });
866
908
  if (options) {
867
- const { ipfs_pin_hash: cid2, status, sort, limit, offset } = options;
868
- if (cid2)
869
- params.append("ipfs_pin_hash", cid2.toString());
909
+ const { ipfs_pin_hash: cid, status, sort, limit, offset } = options;
910
+ if (cid)
911
+ params.append("ipfs_pin_hash", cid.toString());
870
912
  if (status)
871
913
  params.append("status", status.toString());
872
914
  if (sort)
@@ -876,14 +918,20 @@ var pinJobs = async (config, options) => {
876
918
  if (offset)
877
919
  params.append("offset", offset.toString());
878
920
  }
879
- const url = `https://api.pinata.cloud/pinning/pinJobs?${params.toString()}`;
880
- const headers = {
881
- Authorization: `Bearer ${config?.pinataJwt}`
882
- };
883
- if (config.customHeaders) {
884
- Object.assign(headers, config.customHeaders);
921
+ let endpoint = "https://api.pinata.cloud";
922
+ if (config.endpointUrl) {
923
+ endpoint = config.endpointUrl;
924
+ }
925
+ const url = `${endpoint}/pinning/pinJobs?${params.toString()}`;
926
+ let headers;
927
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
928
+ headers = { ...config.customHeaders };
929
+ } else {
930
+ headers = {
931
+ Authorization: `Bearer ${config.pinataJwt}`,
932
+ Source: "sdk/pinJobs"
933
+ };
885
934
  }
886
- headers["Source"] = headers["Source"] || "sdk/pinJobs";
887
935
  try {
888
936
  const request = await fetch(url, {
889
937
  method: "GET",
@@ -922,16 +970,21 @@ var pinnedFileCount = async (config) => {
922
970
  if (!config || !config.pinataJwt) {
923
971
  throw new ValidationError("Pinata configuration or JWT is missing");
924
972
  }
925
- const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
926
- const headers = {
927
- Authorization: `Bearer ${config?.pinataJwt}`
928
- };
929
- if (config.customHeaders) {
930
- Object.assign(headers, config.customHeaders);
973
+ let endpoint = "https://api.pinata.cloud";
974
+ if (config.endpointUrl) {
975
+ endpoint = config.endpointUrl;
976
+ }
977
+ let headers;
978
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
979
+ headers = { ...config.customHeaders };
980
+ } else {
981
+ headers = {
982
+ Authorization: `Bearer ${config.pinataJwt}`,
983
+ Source: "sdk/pinnedFileUsage"
984
+ };
931
985
  }
932
- headers["Source"] = headers["Source"] || "sdk/pinnedFileUsage";
933
986
  try {
934
- const request = await fetch(url, {
987
+ const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
935
988
  method: "GET",
936
989
  headers
937
990
  });
@@ -972,16 +1025,21 @@ var totalStorageUsage = async (config) => {
972
1025
  if (!config || !config.pinataJwt) {
973
1026
  throw new ValidationError("Pinata configuration or JWT is missing");
974
1027
  }
975
- const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
976
- const headers = {
977
- Authorization: `Bearer ${config?.pinataJwt}`
978
- };
979
- if (config.customHeaders) {
980
- Object.assign(headers, config.customHeaders);
1028
+ let endpoint = "https://api.pinata.cloud";
1029
+ if (config.endpointUrl) {
1030
+ endpoint = config.endpointUrl;
1031
+ }
1032
+ let headers;
1033
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1034
+ headers = { ...config.customHeaders };
1035
+ } else {
1036
+ headers = {
1037
+ Authorization: `Bearer ${config.pinataJwt}`,
1038
+ Source: "sdk/totalStorageUsage"
1039
+ };
981
1040
  }
982
- headers["Source"] = headers["Source"] || "sdk/totalStorageUsage";
983
1041
  try {
984
- const request = await fetch(url, {
1042
+ const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
985
1043
  method: "GET",
986
1044
  headers
987
1045
  });
@@ -1022,17 +1080,23 @@ var createKey = async (config, options) => {
1022
1080
  if (!config || !config.pinataJwt) {
1023
1081
  throw new ValidationError("Pinata configuration or JWT is missing");
1024
1082
  }
1025
- const headers = {
1026
- "Content-Type": "application/json",
1027
- Authorization: `Bearer ${config?.pinataJwt}`
1028
- };
1029
- if (config.customHeaders) {
1030
- Object.assign(headers, config.customHeaders);
1083
+ let headers;
1084
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1085
+ headers = { ...config.customHeaders };
1086
+ } else {
1087
+ headers = {
1088
+ Authorization: `Bearer ${config.pinataJwt}`,
1089
+ "Content-Type": "application/json",
1090
+ Source: "sdk/createKey"
1091
+ };
1031
1092
  }
1032
- headers["Source"] = headers["Source"] || "sdk/createKey";
1033
1093
  const data = JSON.stringify(options);
1094
+ let endpoint = "https://api.pinata.cloud";
1095
+ if (config.endpointUrl) {
1096
+ endpoint = config.endpointUrl;
1097
+ }
1034
1098
  try {
1035
- const request = await fetch("https://api.pinata.cloud/v3/pinata/keys", {
1099
+ const request = await fetch(`${endpoint}/v3/pinata/keys`, {
1036
1100
  method: "POST",
1037
1101
  headers,
1038
1102
  body: data
@@ -1070,14 +1134,16 @@ var listKeys = async (config, options) => {
1070
1134
  if (!config || !config.pinataJwt) {
1071
1135
  throw new ValidationError("Pinata configuration or JWT is missing");
1072
1136
  }
1073
- const headers = {
1074
- "Content-Type": "application/json",
1075
- Authorization: `Bearer ${config?.pinataJwt}`
1076
- };
1077
- if (config.customHeaders) {
1078
- Object.assign(headers, config.customHeaders);
1137
+ let headers;
1138
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1139
+ headers = { ...config.customHeaders };
1140
+ } else {
1141
+ headers = {
1142
+ Authorization: `Bearer ${config.pinataJwt}`,
1143
+ "Content-Type": "application/json",
1144
+ Source: "sdk/listKeys"
1145
+ };
1079
1146
  }
1080
- headers["Source"] = headers["Source"] || "sdk/listKeys";
1081
1147
  const params = new URLSearchParams();
1082
1148
  if (options) {
1083
1149
  const { offset, name, revoked, limitedUse, exhausted } = options;
@@ -1092,12 +1158,18 @@ var listKeys = async (config, options) => {
1092
1158
  if (name)
1093
1159
  params.append("name", name);
1094
1160
  }
1095
- const url = `https://api.pinata.cloud/v3/pinata/keys?${params.toString()}`;
1161
+ let endpoint = "https://api.pinata.cloud";
1162
+ if (config.endpointUrl) {
1163
+ endpoint = config.endpointUrl;
1164
+ }
1096
1165
  try {
1097
- const request = await fetch(url, {
1098
- method: "GET",
1099
- headers
1100
- });
1166
+ const request = await fetch(
1167
+ `${endpoint}/v3/pinata/keys?${params.toString()}`,
1168
+ {
1169
+ method: "GET",
1170
+ headers
1171
+ }
1172
+ );
1101
1173
  if (!request.ok) {
1102
1174
  const errorData = await request.json();
1103
1175
  if (request.status === 401) {
@@ -1136,24 +1208,27 @@ var revokeKeys = async (config, keys) => {
1136
1208
  if (!config || !config.pinataJwt) {
1137
1209
  throw new ValidationError("Pinata configuration or JWT is missing");
1138
1210
  }
1139
- const headers = {
1140
- "Content-Type": "application/json",
1141
- Authorization: `Bearer ${config?.pinataJwt}`
1142
- };
1143
- if (config.customHeaders) {
1144
- Object.assign(headers, config.customHeaders);
1211
+ let headers;
1212
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1213
+ headers = { ...config.customHeaders };
1214
+ } else {
1215
+ headers = {
1216
+ Authorization: `Bearer ${config.pinataJwt}`,
1217
+ "Content-Type": "application/json",
1218
+ Source: "sdk/revokeKeys"
1219
+ };
1145
1220
  }
1146
- headers["Source"] = headers["Source"] || "sdk/revokeKeys";
1147
1221
  const responses = [];
1222
+ let endpoint = "https://api.pinata.cloud";
1223
+ if (config.endpointUrl) {
1224
+ endpoint = config.endpointUrl;
1225
+ }
1148
1226
  for (const key of keys) {
1149
1227
  try {
1150
- const request = await fetch(
1151
- `https://api.pinata.cloud/v3/pinata/keys/${key}`,
1152
- {
1153
- method: "PUT",
1154
- headers
1155
- }
1156
- );
1228
+ const request = await fetch(`${endpoint}/v3/pinata/keys/${key}`, {
1229
+ method: "PUT",
1230
+ headers
1231
+ });
1157
1232
  await wait2(300);
1158
1233
  if (!request.ok) {
1159
1234
  const errorData = await request.json();
@@ -1199,16 +1274,22 @@ var createGroup = async (config, options) => {
1199
1274
  throw new ValidationError("Pinata configuration or JWT is missing");
1200
1275
  }
1201
1276
  const data = JSON.stringify(options);
1202
- const headers = {
1203
- "Content-Type": "application/json",
1204
- Authorization: `Bearer ${config?.pinataJwt}`
1205
- };
1206
- if (config.customHeaders) {
1207
- Object.assign(headers, config.customHeaders);
1277
+ let headers;
1278
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1279
+ headers = { ...config.customHeaders };
1280
+ } else {
1281
+ headers = {
1282
+ Authorization: `Bearer ${config.pinataJwt}`,
1283
+ "Content-Type": "application/json",
1284
+ Source: "sdk/createGroup"
1285
+ };
1286
+ }
1287
+ let endpoint = "https://api.pinata.cloud";
1288
+ if (config.endpointUrl) {
1289
+ endpoint = config.endpointUrl;
1208
1290
  }
1209
- headers["Source"] = headers["Source"] || "sdk/createGroup";
1210
1291
  try {
1211
- const request = await fetch("https://api.pinata.cloud/groups", {
1292
+ const request = await fetch(`${endpoint}/groups`, {
1212
1293
  method: "POST",
1213
1294
  headers,
1214
1295
  body: data
@@ -1246,14 +1327,16 @@ var listGroups = async (config, options) => {
1246
1327
  if (!config || !config.pinataJwt) {
1247
1328
  throw new ValidationError("Pinata configuration or JWT is missing");
1248
1329
  }
1249
- const headers = {
1250
- "Content-Type": "application/json",
1251
- Authorization: `Bearer ${config?.pinataJwt}`
1252
- };
1253
- if (config.customHeaders) {
1254
- Object.assign(headers, config.customHeaders);
1330
+ let headers;
1331
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1332
+ headers = { ...config.customHeaders };
1333
+ } else {
1334
+ headers = {
1335
+ Authorization: `Bearer ${config.pinataJwt}`,
1336
+ "Content-Type": "application/json",
1337
+ Source: "sdk/listGroups"
1338
+ };
1255
1339
  }
1256
- headers["Source"] = headers["Source"] || "sdk/listGroups";
1257
1340
  const params = new URLSearchParams();
1258
1341
  if (options) {
1259
1342
  const { offset, nameContains, limit } = options;
@@ -1264,9 +1347,12 @@ var listGroups = async (config, options) => {
1264
1347
  if (limit !== void 0)
1265
1348
  params.append("limit", limit.toString());
1266
1349
  }
1267
- const url = `https://api.pinata.cloud/groups?${params.toString()}`;
1350
+ let endpoint = "https://api.pinata.cloud";
1351
+ if (config.endpointUrl) {
1352
+ endpoint = config.endpointUrl;
1353
+ }
1268
1354
  try {
1269
- const request = await fetch(url, {
1355
+ const request = await fetch(`${endpoint}/groups?${params.toString()}`, {
1270
1356
  method: "GET",
1271
1357
  headers
1272
1358
  });
@@ -1303,22 +1389,25 @@ var getGroup = async (config, options) => {
1303
1389
  if (!config || !config.pinataJwt) {
1304
1390
  throw new ValidationError("Pinata configuration or JWT is missing");
1305
1391
  }
1306
- const headers = {
1307
- "Content-Type": "application/json",
1308
- Authorization: `Bearer ${config?.pinataJwt}`
1309
- };
1310
- if (config.customHeaders) {
1311
- Object.assign(headers, config.customHeaders);
1392
+ let headers;
1393
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1394
+ headers = { ...config.customHeaders };
1395
+ } else {
1396
+ headers = {
1397
+ Authorization: `Bearer ${config.pinataJwt}`,
1398
+ "Content-Type": "application/json",
1399
+ Source: "sdk/getGroup"
1400
+ };
1401
+ }
1402
+ let endpoint = "https://api.pinata.cloud";
1403
+ if (config.endpointUrl) {
1404
+ endpoint = config.endpointUrl;
1312
1405
  }
1313
- headers["Source"] = headers["Source"] || "sdk/getGroup";
1314
1406
  try {
1315
- const request = await fetch(
1316
- `https://api.pinata.cloud/groups/${options.groupId}`,
1317
- {
1318
- method: "GET",
1319
- headers
1320
- }
1321
- );
1407
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1408
+ method: "GET",
1409
+ headers
1410
+ });
1322
1411
  if (!request.ok) {
1323
1412
  const errorData = await request.json();
1324
1413
  if (request.status === 401) {
@@ -1357,23 +1446,26 @@ var addToGroup = async (config, options) => {
1357
1446
  const data = JSON.stringify({
1358
1447
  cids: options.cids
1359
1448
  });
1360
- const headers = {
1361
- "Content-Type": "application/json",
1362
- Authorization: `Bearer ${config?.pinataJwt}`
1363
- };
1364
- if (config.customHeaders) {
1365
- Object.assign(headers, config.customHeaders);
1449
+ let headers;
1450
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1451
+ headers = { ...config.customHeaders };
1452
+ } else {
1453
+ headers = {
1454
+ Authorization: `Bearer ${config.pinataJwt}`,
1455
+ "Content-Type": "application/json",
1456
+ Source: "sdk/addToGroup"
1457
+ };
1458
+ }
1459
+ let endpoint = "https://api.pinata.cloud";
1460
+ if (config.endpointUrl) {
1461
+ endpoint = config.endpointUrl;
1366
1462
  }
1367
- headers["Source"] = headers["Source"] || "sdk/addToGroup";
1368
1463
  try {
1369
- const request = await fetch(
1370
- `https://api.pinata.cloud/groups/${options.groupId}/cids`,
1371
- {
1372
- method: "PUT",
1373
- headers,
1374
- body: data
1375
- }
1376
- );
1464
+ const request = await fetch(`${endpoint}/groups/${options.groupId}/cids`, {
1465
+ method: "PUT",
1466
+ headers,
1467
+ body: data
1468
+ });
1377
1469
  if (!request.ok) {
1378
1470
  const errorData = await request.json();
1379
1471
  if (request.status === 401) {
@@ -1412,23 +1504,26 @@ var updateGroup = async (config, options) => {
1412
1504
  const data = JSON.stringify({
1413
1505
  name: options.name
1414
1506
  });
1415
- const headers = {
1416
- "Content-Type": "application/json",
1417
- Authorization: `Bearer ${config?.pinataJwt}`
1418
- };
1419
- if (config.customHeaders) {
1420
- Object.assign(headers, config.customHeaders);
1507
+ let headers;
1508
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1509
+ headers = { ...config.customHeaders };
1510
+ } else {
1511
+ headers = {
1512
+ Authorization: `Bearer ${config.pinataJwt}`,
1513
+ "Content-Type": "application/json",
1514
+ Source: "sdk/updateGroup"
1515
+ };
1516
+ }
1517
+ let endpoint = "https://api.pinata.cloud";
1518
+ if (config.endpointUrl) {
1519
+ endpoint = config.endpointUrl;
1421
1520
  }
1422
- headers["Source"] = headers["Source"] || "sdk/updateGroup";
1423
1521
  try {
1424
- const request = await fetch(
1425
- `https://api.pinata.cloud/groups/${options.groupId}`,
1426
- {
1427
- method: "PUT",
1428
- headers,
1429
- body: data
1430
- }
1431
- );
1522
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1523
+ method: "PUT",
1524
+ headers,
1525
+ body: data
1526
+ });
1432
1527
  if (!request.ok) {
1433
1528
  const errorData = await request.json();
1434
1529
  if (request.status === 401) {
@@ -1462,26 +1557,29 @@ var removeFromGroup = async (config, options) => {
1462
1557
  if (!config || !config.pinataJwt) {
1463
1558
  throw new ValidationError("Pinata configuration or JWT is missing");
1464
1559
  }
1465
- const headers = {
1466
- "Content-Type": "application/json",
1467
- Authorization: `Bearer ${config?.pinataJwt}`
1468
- };
1469
- if (config.customHeaders) {
1470
- Object.assign(headers, config.customHeaders);
1560
+ let headers;
1561
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1562
+ headers = { ...config.customHeaders };
1563
+ } else {
1564
+ headers = {
1565
+ Authorization: `Bearer ${config.pinataJwt}`,
1566
+ "Content-Type": "application/json",
1567
+ Source: "sdk/removeFromGroup"
1568
+ };
1471
1569
  }
1472
- headers["Source"] = headers["Source"] || "sdk/removeFromGroup";
1473
1570
  const data = JSON.stringify({
1474
1571
  cids: options.cids
1475
1572
  });
1573
+ let endpoint = "https://api.pinata.cloud";
1574
+ if (config.endpointUrl) {
1575
+ endpoint = config.endpointUrl;
1576
+ }
1476
1577
  try {
1477
- const request = await fetch(
1478
- `https://api.pinata.cloud/groups/${options.groupId}/cids`,
1479
- {
1480
- method: "DELETE",
1481
- headers,
1482
- body: data
1483
- }
1484
- );
1578
+ const request = await fetch(`${endpoint}/groups/${options.groupId}/cids`, {
1579
+ method: "DELETE",
1580
+ headers,
1581
+ body: data
1582
+ });
1485
1583
  if (!request.ok) {
1486
1584
  const errorData = await request.json();
1487
1585
  if (request.status === 401) {
@@ -1519,22 +1617,25 @@ var deleteGroup = async (config, options) => {
1519
1617
  if (!config || !config.pinataJwt) {
1520
1618
  throw new ValidationError("Pinata configuration or JWT is missing");
1521
1619
  }
1522
- const headers = {
1523
- "Content-Type": "application/json",
1524
- Authorization: `Bearer ${config?.pinataJwt}`
1525
- };
1526
- if (config.customHeaders) {
1527
- Object.assign(headers, config.customHeaders);
1620
+ let headers;
1621
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1622
+ headers = { ...config.customHeaders };
1623
+ } else {
1624
+ headers = {
1625
+ Authorization: `Bearer ${config.pinataJwt}`,
1626
+ "Content-Type": "application/json",
1627
+ Source: "sdk/deleteGroup"
1628
+ };
1629
+ }
1630
+ let endpoint = "https://api.pinata.cloud";
1631
+ if (config.endpointUrl) {
1632
+ endpoint = config.endpointUrl;
1528
1633
  }
1529
- headers["Source"] = headers["Source"] || "sdk/deleteGroup";
1530
1634
  try {
1531
- const request = await fetch(
1532
- `https://api.pinata.cloud/groups/${options.groupId}`,
1533
- {
1534
- method: "DELETE",
1535
- headers
1536
- }
1537
- );
1635
+ const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
1636
+ method: "DELETE",
1637
+ headers
1638
+ });
1538
1639
  if (!request.ok) {
1539
1640
  const errorData = await request.json();
1540
1641
  if (request.status === 401) {
@@ -1571,17 +1672,23 @@ var addSignature = async (config, options) => {
1571
1672
  const data = JSON.stringify({
1572
1673
  signature: options.signature
1573
1674
  });
1574
- const headers = {
1575
- "Content-Type": "application/json",
1576
- Authorization: `Bearer ${config?.pinataJwt}`
1577
- };
1578
- if (config.customHeaders) {
1579
- Object.assign(headers, config.customHeaders);
1675
+ let headers;
1676
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1677
+ headers = { ...config.customHeaders };
1678
+ } else {
1679
+ headers = {
1680
+ Authorization: `Bearer ${config.pinataJwt}`,
1681
+ "Content-Type": "application/json",
1682
+ Source: "sdk/addSignature"
1683
+ };
1684
+ }
1685
+ let endpoint = "https://api.pinata.cloud";
1686
+ if (config.endpointUrl) {
1687
+ endpoint = config.endpointUrl;
1580
1688
  }
1581
- headers["Source"] = headers["Source"] || "sdk/addSignature";
1582
1689
  try {
1583
1690
  const request = await fetch(
1584
- `https://api.pinata.cloud/v3/ipfs/signature/${options.cid}`,
1691
+ `${endpoint}/v3/ipfs/signature/${options.cid}`,
1585
1692
  {
1586
1693
  method: "POST",
1587
1694
  headers,
@@ -1626,26 +1733,29 @@ var addSignature = async (config, options) => {
1626
1733
  };
1627
1734
 
1628
1735
  // src/core/signatures/getSignature.ts
1629
- var getSignature = async (config, cid2) => {
1736
+ var getSignature = async (config, cid) => {
1630
1737
  if (!config || !config.pinataJwt) {
1631
1738
  throw new ValidationError("Pinata configuration or JWT is missing");
1632
1739
  }
1633
- const headers = {
1634
- "Content-Type": "application/json",
1635
- Authorization: `Bearer ${config?.pinataJwt}`
1636
- };
1637
- if (config.customHeaders) {
1638
- Object.assign(headers, config.customHeaders);
1740
+ let headers;
1741
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1742
+ headers = { ...config.customHeaders };
1743
+ } else {
1744
+ headers = {
1745
+ Authorization: `Bearer ${config.pinataJwt}`,
1746
+ "Content-Type": "application/json",
1747
+ Source: "sdk/getSignature"
1748
+ };
1749
+ }
1750
+ let endpoint = "https://api.pinata.cloud";
1751
+ if (config.endpointUrl) {
1752
+ endpoint = config.endpointUrl;
1639
1753
  }
1640
- headers["Source"] = headers["Source"] || "sdk/getSignature";
1641
1754
  try {
1642
- const request = await fetch(
1643
- `https://api.pinata.cloud/v3/ipfs/signature/${cid2}`,
1644
- {
1645
- method: "GET",
1646
- headers
1647
- }
1648
- );
1755
+ const request = await fetch(`${endpoint}/v3/ipfs/signature/${cid}`, {
1756
+ method: "GET",
1757
+ headers
1758
+ });
1649
1759
  if (!request.ok) {
1650
1760
  const errorData = await request.json();
1651
1761
  if (request.status === 401) {
@@ -1677,26 +1787,29 @@ var getSignature = async (config, cid2) => {
1677
1787
  };
1678
1788
 
1679
1789
  // src/core/signatures/removeSignature.ts
1680
- var removeSignature = async (config, cid2) => {
1790
+ var removeSignature = async (config, cid) => {
1681
1791
  if (!config || !config.pinataJwt) {
1682
1792
  throw new ValidationError("Pinata configuration or JWT is missing");
1683
1793
  }
1684
- const headers = {
1685
- "Content-Type": "application/json",
1686
- Authorization: `Bearer ${config?.pinataJwt}`
1687
- };
1688
- if (config.customHeaders) {
1689
- Object.assign(headers, config.customHeaders);
1794
+ let headers;
1795
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1796
+ headers = { ...config.customHeaders };
1797
+ } else {
1798
+ headers = {
1799
+ Authorization: `Bearer ${config.pinataJwt}`,
1800
+ "Content-Type": "application/json",
1801
+ Source: "sdk/removeSignature"
1802
+ };
1803
+ }
1804
+ let endpoint = "https://api.pinata.cloud";
1805
+ if (config.endpointUrl) {
1806
+ endpoint = config.endpointUrl;
1690
1807
  }
1691
- headers["Source"] = headers["Source"] || "sdk/removeSignature";
1692
1808
  try {
1693
- const request = await fetch(
1694
- `https://api.pinata.cloud/v3/ipfs/signature/${cid2}`,
1695
- {
1696
- method: "DELETE",
1697
- headers
1698
- }
1699
- );
1809
+ const request = await fetch(`${endpoint}/v3/ipfs/signature/${cid}`, {
1810
+ method: "DELETE",
1811
+ headers
1812
+ });
1700
1813
  if (!request.ok) {
1701
1814
  const errorData = await request.json();
1702
1815
  if (request.status === 401) {
@@ -1726,6 +1839,210 @@ var removeSignature = async (config, cid2) => {
1726
1839
  }
1727
1840
  };
1728
1841
 
1842
+ // src/core/gateway/analyticsTopUsage.ts
1843
+ var analyticsTopUsage = async (config, options) => {
1844
+ if (!config || !config.pinataJwt) {
1845
+ throw new ValidationError("Pinata configuration or JWT is missing");
1846
+ }
1847
+ const params = new URLSearchParams({
1848
+ includesCount: "false"
1849
+ });
1850
+ if (options) {
1851
+ const {
1852
+ cid,
1853
+ gateway_domain,
1854
+ start_date,
1855
+ end_date,
1856
+ file_name,
1857
+ user_agent,
1858
+ country,
1859
+ region,
1860
+ referer,
1861
+ limit,
1862
+ sort_order,
1863
+ sort_by,
1864
+ attribute
1865
+ } = options;
1866
+ if (cid)
1867
+ params.append("cid", cid);
1868
+ if (gateway_domain)
1869
+ params.append("gateway_domain", gateway_domain);
1870
+ if (start_date)
1871
+ params.append("start_date", start_date);
1872
+ if (end_date)
1873
+ params.append("end_date", end_date);
1874
+ if (file_name)
1875
+ params.append("file_name", file_name);
1876
+ if (user_agent)
1877
+ params.append("user_agent", user_agent.toString());
1878
+ if (country)
1879
+ params.append("country", country.toString());
1880
+ if (region)
1881
+ params.append("region", region);
1882
+ if (referer)
1883
+ params.append("referer", referer.toString());
1884
+ if (limit)
1885
+ params.append("limit", limit.toString());
1886
+ if (sort_order)
1887
+ params.append("sort_order", sort_order);
1888
+ if (sort_by)
1889
+ params.append("sort_by", sort_by);
1890
+ if (attribute)
1891
+ params.append("by", attribute);
1892
+ }
1893
+ let endpoint = "https://api.pinata.cloud";
1894
+ if (config.endpointUrl) {
1895
+ endpoint = config.endpointUrl;
1896
+ }
1897
+ const url = `${endpoint}/v3/ipfs/gateway_analytics_top?${params.toString()}`;
1898
+ try {
1899
+ let headers;
1900
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1901
+ headers = { ...config.customHeaders };
1902
+ } else {
1903
+ headers = {
1904
+ Authorization: `Bearer ${config.pinataJwt}`,
1905
+ Source: "sdk/analyticsTopUsage"
1906
+ };
1907
+ }
1908
+ const request = await fetch(url, {
1909
+ method: "GET",
1910
+ headers
1911
+ });
1912
+ if (!request.ok) {
1913
+ const errorData = await request.json();
1914
+ if (request.status === 401) {
1915
+ throw new AuthenticationError(
1916
+ "Authentication failed",
1917
+ request.status,
1918
+ errorData
1919
+ );
1920
+ }
1921
+ throw new NetworkError(
1922
+ `HTTP error! status: ${request.status}`,
1923
+ request.status,
1924
+ errorData
1925
+ );
1926
+ }
1927
+ const res = await request.json();
1928
+ const resData = res.data;
1929
+ return resData;
1930
+ } catch (error) {
1931
+ if (error instanceof PinataError) {
1932
+ throw error;
1933
+ }
1934
+ if (error instanceof Error) {
1935
+ throw new PinataError(
1936
+ `Error processing anaytics usage: ${error.message}`
1937
+ );
1938
+ }
1939
+ throw new PinataError(
1940
+ "An unknown error occurred while fetching gateway usage"
1941
+ );
1942
+ }
1943
+ };
1944
+
1945
+ // src/core/gateway/analyticsDateInterval.ts
1946
+ var analyticsDateInterval = async (config, options) => {
1947
+ if (!config || !config.pinataJwt) {
1948
+ throw new ValidationError("Pinata configuration or JWT is missing");
1949
+ }
1950
+ const params = new URLSearchParams();
1951
+ if (options) {
1952
+ const {
1953
+ cid,
1954
+ gateway_domain,
1955
+ start_date,
1956
+ end_date,
1957
+ file_name,
1958
+ user_agent,
1959
+ country,
1960
+ region,
1961
+ referer,
1962
+ limit,
1963
+ sort_order,
1964
+ date_interval,
1965
+ sort_by
1966
+ } = options;
1967
+ if (cid)
1968
+ params.append("cid", cid);
1969
+ if (gateway_domain)
1970
+ params.append("gateway_domain", gateway_domain);
1971
+ if (start_date)
1972
+ params.append("start_date", start_date);
1973
+ if (end_date)
1974
+ params.append("end_date", end_date);
1975
+ if (file_name)
1976
+ params.append("file_name", file_name);
1977
+ if (user_agent)
1978
+ params.append("user_agent", user_agent.toString());
1979
+ if (country)
1980
+ params.append("country", country.toString());
1981
+ if (region)
1982
+ params.append("region", region);
1983
+ if (referer)
1984
+ params.append("referer", referer.toString());
1985
+ if (limit)
1986
+ params.append("limit", limit.toString());
1987
+ if (sort_order)
1988
+ params.append("sort_order", sort_order);
1989
+ if (sort_by)
1990
+ params.append("sort_by", sort_by);
1991
+ if (date_interval)
1992
+ params.append("by", date_interval);
1993
+ }
1994
+ let endpoint = "https://api.pinata.cloud";
1995
+ if (config.endpointUrl) {
1996
+ endpoint = config.endpointUrl;
1997
+ }
1998
+ const url = `${endpoint}/v3/ipfs/gateway_analytics_time_series?${params.toString()}`;
1999
+ try {
2000
+ let headers;
2001
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
2002
+ headers = { ...config.customHeaders };
2003
+ } else {
2004
+ headers = {
2005
+ Authorization: `Bearer ${config.pinataJwt}`,
2006
+ Source: "sdk/analyticsDateInterval"
2007
+ };
2008
+ }
2009
+ const request = await fetch(url, {
2010
+ method: "GET",
2011
+ headers
2012
+ });
2013
+ if (!request.ok) {
2014
+ const errorData = await request.json();
2015
+ if (request.status === 401) {
2016
+ throw new AuthenticationError(
2017
+ "Authentication failed",
2018
+ request.status,
2019
+ errorData
2020
+ );
2021
+ }
2022
+ throw new NetworkError(
2023
+ `HTTP error! status: ${request.status}`,
2024
+ request.status,
2025
+ errorData
2026
+ );
2027
+ }
2028
+ const res = await request.json();
2029
+ const resData = res.data;
2030
+ return resData;
2031
+ } catch (error) {
2032
+ if (error instanceof PinataError) {
2033
+ throw error;
2034
+ }
2035
+ if (error instanceof Error) {
2036
+ throw new PinataError(
2037
+ `Error processing anaytics usage: ${error.message}`
2038
+ );
2039
+ }
2040
+ throw new PinataError(
2041
+ "An unknown error occurred while fetching gateway usage"
2042
+ );
2043
+ }
2044
+ };
2045
+
1729
2046
  // src/core/pinataSDK.ts
1730
2047
  var formatConfig = (config) => {
1731
2048
  let gateway = config?.pinataGateway;
@@ -1747,6 +2064,18 @@ var PinataSDK = class {
1747
2064
  this.groups = new Groups(this.config);
1748
2065
  this.signatures = new Signatures(this.config);
1749
2066
  }
2067
+ setNewHeaders(headers) {
2068
+ if (!this.config) {
2069
+ this.config = { pinataJwt: "", customHeaders: {} };
2070
+ }
2071
+ this.config.customHeaders = { ...this.config.customHeaders, ...headers };
2072
+ this.upload.updateConfig(this.config);
2073
+ this.gateways.updateConfig(this.config);
2074
+ this.usage.updateConfig(this.config);
2075
+ this.keys.updateConfig(this.config);
2076
+ this.groups.updateConfig(this.config);
2077
+ this.signatures.updateConfig(this.config);
2078
+ }
1750
2079
  testAuthentication() {
1751
2080
  return testAuthentication(this.config);
1752
2081
  }
@@ -1818,6 +2147,9 @@ var Upload = class {
1818
2147
  constructor(config) {
1819
2148
  this.config = formatConfig(config);
1820
2149
  }
2150
+ updateConfig(newConfig) {
2151
+ this.config = newConfig;
2152
+ }
1821
2153
  file(file, options) {
1822
2154
  return new UploadBuilder(this.config, uploadFile, file, options);
1823
2155
  }
@@ -1833,8 +2165,8 @@ var Upload = class {
1833
2165
  json(data, options) {
1834
2166
  return new UploadBuilder(this.config, uploadJson, data, options);
1835
2167
  }
1836
- cid(cid2, options) {
1837
- return new UploadBuilder(this.config, uploadCid, cid2, options);
2168
+ cid(cid, options) {
2169
+ return new UploadBuilder(this.config, uploadCid, cid, options);
1838
2170
  }
1839
2171
  };
1840
2172
  var FilterFiles = class {
@@ -1847,8 +2179,8 @@ var FilterFiles = class {
1847
2179
  this.MINUTE_IN_MS = 6e4;
1848
2180
  this.config = config;
1849
2181
  }
1850
- cid(cid2) {
1851
- this.query.cid = cid2;
2182
+ cid(cid) {
2183
+ this.query.cid = cid;
1852
2184
  return this;
1853
2185
  }
1854
2186
  pinStart(date) {
@@ -1939,12 +2271,34 @@ var Gateways = class {
1939
2271
  constructor(config) {
1940
2272
  this.config = formatConfig(config);
1941
2273
  }
1942
- get(cid2) {
1943
- return getCid(this.config, cid2);
2274
+ updateConfig(newConfig) {
2275
+ this.config = newConfig;
2276
+ }
2277
+ get(cid) {
2278
+ return getCid(this.config, cid);
1944
2279
  }
1945
2280
  convert(url) {
1946
2281
  return convertIPFSUrl(this.config, url);
1947
2282
  }
2283
+ topUsageAnalytics(options) {
2284
+ return new TopGatewayAnalyticsBuilder(
2285
+ this.config,
2286
+ options.domain,
2287
+ options.start,
2288
+ options.end,
2289
+ options.sortBy,
2290
+ options.attribute
2291
+ );
2292
+ }
2293
+ dateIntervalAnalytics(options) {
2294
+ return new TimeIntervalGatewayAnalyticsBuilder(
2295
+ this.config,
2296
+ options.domain,
2297
+ options.start,
2298
+ options.end,
2299
+ options.interval
2300
+ );
2301
+ }
1948
2302
  };
1949
2303
  var FilterPinJobs = class {
1950
2304
  constructor(config) {
@@ -1956,8 +2310,8 @@ var FilterPinJobs = class {
1956
2310
  this.MINUTE_IN_MS = 6e4;
1957
2311
  this.config = config;
1958
2312
  }
1959
- cid(cid2) {
1960
- this.query.ipfs_pin_hash = cid2;
2313
+ cid(cid) {
2314
+ this.query.ipfs_pin_hash = cid;
1961
2315
  return this;
1962
2316
  }
1963
2317
  status(status) {
@@ -2024,6 +2378,9 @@ var Usage = class {
2024
2378
  constructor(config) {
2025
2379
  this.config = formatConfig(config);
2026
2380
  }
2381
+ updateConfig(newConfig) {
2382
+ this.config = newConfig;
2383
+ }
2027
2384
  pinnedFileCount() {
2028
2385
  return pinnedFileCount(this.config);
2029
2386
  }
@@ -2035,6 +2392,9 @@ var Keys = class {
2035
2392
  constructor(config) {
2036
2393
  this.config = formatConfig(config);
2037
2394
  }
2395
+ updateConfig(newConfig) {
2396
+ this.config = newConfig;
2397
+ }
2038
2398
  create(options) {
2039
2399
  return createKey(this.config, options);
2040
2400
  }
@@ -2121,6 +2481,9 @@ var Groups = class {
2121
2481
  constructor(config) {
2122
2482
  this.config = formatConfig(config);
2123
2483
  }
2484
+ updateConfig(newConfig) {
2485
+ this.config = newConfig;
2486
+ }
2124
2487
  create(options) {
2125
2488
  return createGroup(this.config, options);
2126
2489
  }
@@ -2211,14 +2574,116 @@ var Signatures = class {
2211
2574
  constructor(config) {
2212
2575
  this.config = formatConfig(config);
2213
2576
  }
2577
+ updateConfig(newConfig) {
2578
+ this.config = newConfig;
2579
+ }
2214
2580
  add(options) {
2215
2581
  return addSignature(this.config, options);
2216
2582
  }
2217
- get(cid2) {
2218
- return getSignature(this.config, cid2);
2583
+ get(cid) {
2584
+ return getSignature(this.config, cid);
2219
2585
  }
2220
- delete(cid2) {
2221
- return removeSignature(this.config, cid2);
2586
+ delete(cid) {
2587
+ return removeSignature(this.config, cid);
2588
+ }
2589
+ };
2590
+ var GatewayAnalyticsBuilder = class {
2591
+ constructor(config, query) {
2592
+ this.requestCount = 0;
2593
+ this.lastRequestTime = 0;
2594
+ this.MAX_REQUESTS_PER_MINUTE = 30;
2595
+ this.MINUTE_IN_MS = 6e4;
2596
+ this.config = config;
2597
+ this.query = query;
2598
+ }
2599
+ cid(cid) {
2600
+ this.query.cid = cid;
2601
+ return this;
2602
+ }
2603
+ fileName(fileName) {
2604
+ this.query.file_name = fileName;
2605
+ return this;
2606
+ }
2607
+ userAgent(userAgent) {
2608
+ this.query.user_agent = userAgent;
2609
+ return this;
2610
+ }
2611
+ country(country) {
2612
+ this.query.country = country;
2613
+ return this;
2614
+ }
2615
+ region(region) {
2616
+ this.query.region = region;
2617
+ return this;
2618
+ }
2619
+ referer(referer) {
2620
+ this.query.referer = referer;
2621
+ return this;
2622
+ }
2623
+ limit(limit) {
2624
+ this.query.limit = limit;
2625
+ return this;
2626
+ }
2627
+ sort(order) {
2628
+ this.query.sort_order = order;
2629
+ return this;
2630
+ }
2631
+ async rateLimit() {
2632
+ this.requestCount++;
2633
+ const now = Date.now();
2634
+ if (this.requestCount >= this.MAX_REQUESTS_PER_MINUTE) {
2635
+ const timePassedSinceLastRequest = now - this.lastRequestTime;
2636
+ if (timePassedSinceLastRequest < this.MINUTE_IN_MS) {
2637
+ const delayTime = this.MINUTE_IN_MS - timePassedSinceLastRequest;
2638
+ await new Promise((resolve) => setTimeout(resolve, delayTime));
2639
+ }
2640
+ this.requestCount = 0;
2641
+ }
2642
+ this.lastRequestTime = Date.now();
2643
+ }
2644
+ async getAnalytics() {
2645
+ await this.rateLimit();
2646
+ throw new Error("getAnalytics method must be implemented in derived class");
2647
+ }
2648
+ then(onfulfilled) {
2649
+ return this.getAnalytics().then(onfulfilled);
2650
+ }
2651
+ };
2652
+ var TopGatewayAnalyticsBuilder = class extends GatewayAnalyticsBuilder {
2653
+ constructor(config, domain, start, end, sortBy, attribute) {
2654
+ super(config, {
2655
+ gateway_domain: domain,
2656
+ start_date: start,
2657
+ end_date: end,
2658
+ sort_by: sortBy,
2659
+ attribute
2660
+ });
2661
+ }
2662
+ async getAnalytics() {
2663
+ return analyticsTopUsage(this.config, this.query);
2664
+ }
2665
+ async all() {
2666
+ return this.getAnalytics();
2667
+ }
2668
+ };
2669
+ var TimeIntervalGatewayAnalyticsBuilder = class extends GatewayAnalyticsBuilder {
2670
+ constructor(config, domain, start, end, dateInterval) {
2671
+ super(config, {
2672
+ gateway_domain: domain,
2673
+ start_date: start,
2674
+ end_date: end,
2675
+ date_interval: dateInterval
2676
+ });
2677
+ }
2678
+ sortBy(sortBy) {
2679
+ this.query.sort_by = sortBy;
2680
+ return this;
2681
+ }
2682
+ async getAnalytics() {
2683
+ return analyticsDateInterval(this.config, this.query);
2684
+ }
2685
+ async all() {
2686
+ return this.getAnalytics();
2222
2687
  }
2223
2688
  };
2224
2689
  export {