pinata 1.4.1 → 1.6.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
@@ -37,7 +37,10 @@ var testAuthentication = async (config) => {
37
37
  endpoint = config.endpointUrl;
38
38
  }
39
39
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
40
- headers = { ...config.customHeaders };
40
+ headers = {
41
+ Authorization: `Bearer ${config.pinataJwt}`,
42
+ ...config.customHeaders
43
+ };
41
44
  } else {
42
45
  headers = {
43
46
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -93,9 +96,15 @@ var uploadFile = async (config, file, options) => {
93
96
  if (options?.groupId) {
94
97
  data.append("group_id", options.groupId);
95
98
  }
99
+ if (options?.metadata?.keyvalues) {
100
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
101
+ }
96
102
  let headers;
97
103
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
98
- headers = { ...config.customHeaders };
104
+ headers = {
105
+ Authorization: `Bearer ${jwt}`,
106
+ ...config.customHeaders
107
+ };
99
108
  } else {
100
109
  headers = {
101
110
  Authorization: `Bearer ${jwt}`,
@@ -156,9 +165,15 @@ var uploadBase64 = async (config, base64String, options) => {
156
165
  if (options?.groupId) {
157
166
  data.append("group_id", options.groupId);
158
167
  }
168
+ if (options?.metadata?.keyvalues) {
169
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
170
+ }
159
171
  let headers;
160
172
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
161
- headers = { ...config.customHeaders };
173
+ headers = {
174
+ Authorization: `Bearer ${jwt}`,
175
+ ...config.customHeaders
176
+ };
162
177
  } else {
163
178
  headers = {
164
179
  Authorization: `Bearer ${jwt}`,
@@ -231,9 +246,15 @@ var uploadUrl = async (config, url, options) => {
231
246
  if (options?.groupId) {
232
247
  data.append("group_id", options.groupId);
233
248
  }
249
+ if (options?.metadata?.keyvalues) {
250
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
251
+ }
234
252
  let headers;
235
253
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
236
- headers = { ...config.customHeaders };
254
+ headers = {
255
+ Authorization: `Bearer ${jwt}`,
256
+ ...config.customHeaders
257
+ };
237
258
  } else {
238
259
  headers = {
239
260
  Authorization: `Bearer ${jwt}`,
@@ -294,9 +315,15 @@ var uploadJson = async (config, jsonData, options) => {
294
315
  if (options?.groupId) {
295
316
  data.append("group_id", options.groupId);
296
317
  }
318
+ if (options?.metadata?.keyvalues) {
319
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
320
+ }
297
321
  let headers;
298
322
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
299
- headers = { ...config.customHeaders };
323
+ headers = {
324
+ Authorization: `Bearer ${jwt}`,
325
+ ...config.customHeaders
326
+ };
300
327
  } else {
301
328
  headers = {
302
329
  Authorization: `Bearer ${jwt}`,
@@ -355,7 +382,10 @@ var deleteFile = async (config, files) => {
355
382
  const responses = [];
356
383
  let headers;
357
384
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
358
- headers = { ...config.customHeaders };
385
+ headers = {
386
+ Authorization: `Bearer ${config.pinataJwt}`,
387
+ ...config.customHeaders
388
+ };
359
389
  } else {
360
390
  headers = {
361
391
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -417,7 +447,18 @@ var listFiles = async (config, options) => {
417
447
  }
418
448
  const params = new URLSearchParams();
419
449
  if (options) {
420
- const { name, group, cid, order, limit, mimeType, pageToken, cidPending } = options;
450
+ const {
451
+ name,
452
+ group,
453
+ cid,
454
+ order,
455
+ limit,
456
+ mimeType,
457
+ pageToken,
458
+ cidPending,
459
+ metadata,
460
+ noGroup
461
+ } = options;
421
462
  if (limit)
422
463
  params.append("limit", limit.toString());
423
464
  if (name)
@@ -434,6 +475,13 @@ var listFiles = async (config, options) => {
434
475
  params.append("pageToken", pageToken);
435
476
  if (cidPending)
436
477
  params.append("cidPending", "true");
478
+ if (noGroup)
479
+ params.append("group", "null");
480
+ if (metadata && typeof metadata === "object") {
481
+ Object.entries(metadata).forEach(([key, value]) => {
482
+ params.append(`metadata[${key}]`, value.toString());
483
+ });
484
+ }
437
485
  }
438
486
  let endpoint = "https://api.pinata.cloud/v3";
439
487
  if (config.endpointUrl) {
@@ -443,7 +491,10 @@ var listFiles = async (config, options) => {
443
491
  try {
444
492
  let headers;
445
493
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
446
- headers = { ...config.customHeaders };
494
+ headers = {
495
+ Authorization: `Bearer ${config.pinataJwt}`,
496
+ ...config.customHeaders
497
+ };
447
498
  } else {
448
499
  headers = {
449
500
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -488,12 +539,26 @@ var updateFile = async (config, options) => {
488
539
  if (!config) {
489
540
  throw new ValidationError("Pinata configuration is missing");
490
541
  }
491
- const data = JSON.stringify({
492
- name: options.name
493
- });
542
+ if (!options.name && (!options.keyvalues || Object.keys(options.keyvalues).length === 0)) {
543
+ throw new ValidationError(
544
+ "At least one of 'name' or 'keyvalues' must be provided"
545
+ );
546
+ }
547
+ const data = {};
548
+ if (options.name !== void 0) {
549
+ data.name = options.name;
550
+ }
551
+ if (options.keyvalues && Object.keys(options.keyvalues).length > 0) {
552
+ data.keyvalues = options.keyvalues;
553
+ }
554
+ const body = JSON.stringify(data);
494
555
  let headers;
495
556
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
496
- headers = { ...config.customHeaders };
557
+ headers = {
558
+ Authorization: `Bearer ${config.pinataJwt}`,
559
+ "Content-Type": "application/json",
560
+ ...config.customHeaders
561
+ };
497
562
  } else {
498
563
  headers = {
499
564
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -509,7 +574,7 @@ var updateFile = async (config, options) => {
509
574
  const request = await fetch(`${endpoint}/files/${options.id}`, {
510
575
  method: "PUT",
511
576
  headers,
512
- body: data
577
+ body
513
578
  });
514
579
  if (!request.ok) {
515
580
  const errorData = await request.text();
@@ -589,7 +654,11 @@ var getCid = async (config, cid, options) => {
589
654
  }
590
655
  let headers;
591
656
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
592
- headers = { ...config.customHeaders };
657
+ headers = {
658
+ Authorization: `Bearer ${config.pinataJwt}`,
659
+ "Content-Type": "application/json",
660
+ ...config.customHeaders
661
+ };
593
662
  } else {
594
663
  headers = {
595
664
  "Content-Type": "application/json",
@@ -653,7 +722,11 @@ var createKey = async (config, options) => {
653
722
  }
654
723
  let headers;
655
724
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
656
- headers = { ...config.customHeaders };
725
+ headers = {
726
+ Authorization: `Bearer ${config.pinataJwt}`,
727
+ "Content-Type": "application/json",
728
+ ...config.customHeaders
729
+ };
657
730
  } else {
658
731
  headers = {
659
732
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -707,7 +780,11 @@ var listKeys = async (config, options) => {
707
780
  }
708
781
  let headers;
709
782
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
710
- headers = { ...config.customHeaders };
783
+ headers = {
784
+ Authorization: `Bearer ${config.pinataJwt}`,
785
+ "Content-Type": "application/json",
786
+ ...config.customHeaders
787
+ };
711
788
  } else {
712
789
  headers = {
713
790
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -781,6 +858,11 @@ var revokeKeys = async (config, keys) => {
781
858
  }
782
859
  let headers;
783
860
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
861
+ headers = {
862
+ Authorization: `Bearer ${config.pinataJwt}`,
863
+ "Content-Type": "application/json",
864
+ ...config.customHeaders
865
+ };
784
866
  headers = { ...config.customHeaders };
785
867
  } else {
786
868
  headers = {
@@ -850,7 +932,11 @@ var createGroup = async (config, options) => {
850
932
  });
851
933
  let headers;
852
934
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
853
- headers = { ...config.customHeaders };
935
+ headers = {
936
+ Authorization: `Bearer ${config.pinataJwt}`,
937
+ "Content-Type": "application/json",
938
+ ...config.customHeaders
939
+ };
854
940
  } else {
855
941
  headers = {
856
942
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -904,7 +990,11 @@ var listGroups = async (config, options) => {
904
990
  }
905
991
  let headers;
906
992
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
907
- headers = { ...config.customHeaders };
993
+ headers = {
994
+ Authorization: `Bearer ${config.pinataJwt}`,
995
+ "Content-Type": "application/json",
996
+ ...config.customHeaders
997
+ };
908
998
  } else {
909
999
  headers = {
910
1000
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -972,7 +1062,11 @@ var getGroup = async (config, options) => {
972
1062
  }
973
1063
  let headers;
974
1064
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
975
- headers = { ...config.customHeaders };
1065
+ headers = {
1066
+ Authorization: `Bearer ${config.pinataJwt}`,
1067
+ "Content-Type": "application/json",
1068
+ ...config.customHeaders
1069
+ };
976
1070
  } else {
977
1071
  headers = {
978
1072
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -1031,7 +1125,11 @@ var updateGroup = async (config, options) => {
1031
1125
  });
1032
1126
  let headers;
1033
1127
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1034
- headers = { ...config.customHeaders };
1128
+ headers = {
1129
+ Authorization: `Bearer ${config.pinataJwt}`,
1130
+ "Content-Type": "application/json",
1131
+ ...config.customHeaders
1132
+ };
1035
1133
  } else {
1036
1134
  headers = {
1037
1135
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -1085,6 +1183,11 @@ var deleteGroup = async (config, options) => {
1085
1183
  }
1086
1184
  let headers;
1087
1185
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1186
+ headers = {
1187
+ Authorization: `Bearer ${config.pinataJwt}`,
1188
+ "Content-Type": "application/json",
1189
+ ...config.customHeaders
1190
+ };
1088
1191
  headers = { ...config.customHeaders };
1089
1192
  } else {
1090
1193
  headers = {
@@ -1140,7 +1243,11 @@ var swapCid = async (config, options) => {
1140
1243
  });
1141
1244
  let headers;
1142
1245
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1143
- headers = { ...config.customHeaders };
1246
+ headers = {
1247
+ Authorization: `Bearer ${config.pinataJwt}`,
1248
+ "Content-Type": "application/json",
1249
+ ...config.customHeaders
1250
+ };
1144
1251
  } else {
1145
1252
  headers = {
1146
1253
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -1208,7 +1315,11 @@ var swapHistory = async (config, options) => {
1208
1315
  }
1209
1316
  let headers;
1210
1317
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1211
- headers = { ...config.customHeaders };
1318
+ headers = {
1319
+ Authorization: `Bearer ${config.pinataJwt}`,
1320
+ "Content-Type": "application/json",
1321
+ ...config.customHeaders
1322
+ };
1212
1323
  } else {
1213
1324
  headers = {
1214
1325
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -1273,7 +1384,11 @@ var deleteSwap = async (config, cid) => {
1273
1384
  }
1274
1385
  let headers;
1275
1386
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1276
- headers = { ...config.customHeaders };
1387
+ headers = {
1388
+ Authorization: `Bearer ${config.pinataJwt}`,
1389
+ "Content-Type": "application/json",
1390
+ ...config.customHeaders
1391
+ };
1277
1392
  } else {
1278
1393
  headers = {
1279
1394
  Authorization: `Bearer ${config.pinataJwt}`,
@@ -1385,7 +1500,11 @@ var createSignedURL = async (config, options, imgOpts) => {
1385
1500
  }
1386
1501
  let headers;
1387
1502
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1388
- headers = { ...config.customHeaders };
1503
+ headers = {
1504
+ Authorization: `Bearer ${config.pinataJwt}`,
1505
+ "Content-Type": "application/json",
1506
+ ...config.customHeaders
1507
+ };
1389
1508
  } else {
1390
1509
  headers = {
1391
1510
  "Content-Type": "application/json",
@@ -1461,6 +1580,17 @@ var PinataSDK = class {
1461
1580
  this.keys.updateConfig(this.config);
1462
1581
  this.groups.updateConfig(this.config);
1463
1582
  }
1583
+ setNewJwt(jwt) {
1584
+ if (!this.config) {
1585
+ this.config = { pinataJwt: "" };
1586
+ }
1587
+ this.config.pinataJwt = jwt;
1588
+ this.files.updateConfig(this.config);
1589
+ this.upload.updateConfig(this.config);
1590
+ this.gateways.updateConfig(this.config);
1591
+ this.keys.updateConfig(this.config);
1592
+ this.groups.updateConfig(this.config);
1593
+ }
1464
1594
  testAuthentication() {
1465
1595
  return testAuthentication(this.config);
1466
1596
  }
@@ -1595,6 +1725,14 @@ var FilterFiles = class {
1595
1725
  this.query.cidPending = cidPending;
1596
1726
  return this;
1597
1727
  }
1728
+ metadata(keyvalues) {
1729
+ this.query.metadata = keyvalues;
1730
+ return this;
1731
+ }
1732
+ noGroup(noGroup) {
1733
+ this.query.noGroup = noGroup;
1734
+ return this;
1735
+ }
1598
1736
  pageToken(pageToken) {
1599
1737
  this.query.pageToken = pageToken;
1600
1738
  return this;