pinata 1.5.0 → 1.7.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
@@ -96,6 +96,9 @@ var uploadFile = async (config, file, options) => {
96
96
  if (options?.groupId) {
97
97
  data.append("group_id", options.groupId);
98
98
  }
99
+ if (options?.metadata?.keyvalues) {
100
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
101
+ }
99
102
  let headers;
100
103
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
101
104
  headers = {
@@ -162,6 +165,9 @@ var uploadBase64 = async (config, base64String, options) => {
162
165
  if (options?.groupId) {
163
166
  data.append("group_id", options.groupId);
164
167
  }
168
+ if (options?.metadata?.keyvalues) {
169
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
170
+ }
165
171
  let headers;
166
172
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
167
173
  headers = {
@@ -240,6 +246,9 @@ var uploadUrl = async (config, url, options) => {
240
246
  if (options?.groupId) {
241
247
  data.append("group_id", options.groupId);
242
248
  }
249
+ if (options?.metadata?.keyvalues) {
250
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
251
+ }
243
252
  let headers;
244
253
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
245
254
  headers = {
@@ -306,6 +315,9 @@ var uploadJson = async (config, jsonData, options) => {
306
315
  if (options?.groupId) {
307
316
  data.append("group_id", options.groupId);
308
317
  }
318
+ if (options?.metadata?.keyvalues) {
319
+ data.append("keyvalues", JSON.stringify(options.metadata.keyvalues));
320
+ }
309
321
  let headers;
310
322
  if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
311
323
  headers = {
@@ -444,7 +456,8 @@ var listFiles = async (config, options) => {
444
456
  mimeType,
445
457
  pageToken,
446
458
  cidPending,
447
- metadata
459
+ metadata,
460
+ noGroup
448
461
  } = options;
449
462
  if (limit)
450
463
  params.append("limit", limit.toString());
@@ -462,6 +475,8 @@ var listFiles = async (config, options) => {
462
475
  params.append("pageToken", pageToken);
463
476
  if (cidPending)
464
477
  params.append("cidPending", "true");
478
+ if (noGroup)
479
+ params.append("group", "null");
465
480
  if (metadata && typeof metadata === "object") {
466
481
  Object.entries(metadata).forEach(([key, value]) => {
467
482
  params.append(`metadata[${key}]`, value.toString());
@@ -1099,6 +1114,82 @@ var getGroup = async (config, options) => {
1099
1114
  }
1100
1115
  };
1101
1116
 
1117
+ // src/core/groups/addToGroup.ts
1118
+ var addToGroup = async (config, options) => {
1119
+ if (!config) {
1120
+ throw new ValidationError("Pinata configuration is missing");
1121
+ }
1122
+ const wait3 = (milliseconds) => {
1123
+ return new Promise((resolve) => {
1124
+ setTimeout(resolve, milliseconds);
1125
+ });
1126
+ };
1127
+ const responses = [];
1128
+ let headers;
1129
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1130
+ headers = {
1131
+ Authorization: `Bearer ${config.pinataJwt}`,
1132
+ "Content-Type": "application/json",
1133
+ ...config.customHeaders
1134
+ };
1135
+ } else {
1136
+ headers = {
1137
+ Authorization: `Bearer ${config.pinataJwt}`,
1138
+ "Content-Type": "application/json",
1139
+ Source: "sdk/addToGroup"
1140
+ };
1141
+ }
1142
+ let endpoint = "https://api.pinata.cloud/v3";
1143
+ if (config.endpointUrl) {
1144
+ endpoint = config.endpointUrl;
1145
+ }
1146
+ for (const id of options.files) {
1147
+ try {
1148
+ const response = await fetch(
1149
+ `${endpoint}/files/groups/${options.groupId}/ids/${id}`,
1150
+ {
1151
+ method: "PUT",
1152
+ headers
1153
+ }
1154
+ );
1155
+ await wait3(300);
1156
+ if (!response.ok) {
1157
+ const errorData = await response.text();
1158
+ if (response.status === 401) {
1159
+ throw new AuthenticationError(
1160
+ `Authentication failed: ${errorData}`,
1161
+ response.status,
1162
+ errorData
1163
+ );
1164
+ }
1165
+ throw new NetworkError(
1166
+ `HTTP error: ${errorData}`,
1167
+ response.status,
1168
+ errorData
1169
+ );
1170
+ }
1171
+ responses.push({
1172
+ id,
1173
+ status: response.statusText
1174
+ });
1175
+ } catch (error) {
1176
+ let errorMessage;
1177
+ if (error instanceof PinataError) {
1178
+ errorMessage = error.message;
1179
+ } else if (error instanceof Error) {
1180
+ errorMessage = `Error adding file ${id} to group: ${error.message}`;
1181
+ } else {
1182
+ errorMessage = `An unknown error occurred while adding file ${id} to group`;
1183
+ }
1184
+ responses.push({
1185
+ id,
1186
+ status: errorMessage
1187
+ });
1188
+ }
1189
+ }
1190
+ return responses;
1191
+ };
1192
+
1102
1193
  // src/core/groups/updateGroup.ts
1103
1194
  var updateGroup = async (config, options) => {
1104
1195
  if (!config) {
@@ -1161,6 +1252,82 @@ var updateGroup = async (config, options) => {
1161
1252
  }
1162
1253
  };
1163
1254
 
1255
+ // src/core/groups/removeFromGroup.ts
1256
+ var removeFromGroup = async (config, options) => {
1257
+ if (!config) {
1258
+ throw new ValidationError("Pinata configuration is missing");
1259
+ }
1260
+ const wait3 = (milliseconds) => {
1261
+ return new Promise((resolve) => {
1262
+ setTimeout(resolve, milliseconds);
1263
+ });
1264
+ };
1265
+ const responses = [];
1266
+ let headers;
1267
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
1268
+ headers = {
1269
+ Authorization: `Bearer ${config.pinataJwt}`,
1270
+ "Content-Type": "application/json",
1271
+ ...config.customHeaders
1272
+ };
1273
+ } else {
1274
+ headers = {
1275
+ Authorization: `Bearer ${config.pinataJwt}`,
1276
+ "Content-Type": "application/json",
1277
+ Source: "sdk/addToGroup"
1278
+ };
1279
+ }
1280
+ let endpoint = "https://api.pinata.cloud/v3";
1281
+ if (config.endpointUrl) {
1282
+ endpoint = config.endpointUrl;
1283
+ }
1284
+ for (const id of options.files) {
1285
+ try {
1286
+ const response = await fetch(
1287
+ `${endpoint}/files/groups/${options.groupId}/ids/${id}`,
1288
+ {
1289
+ method: "DELETE",
1290
+ headers
1291
+ }
1292
+ );
1293
+ await wait3(300);
1294
+ if (!response.ok) {
1295
+ const errorData = await response.text();
1296
+ if (response.status === 401) {
1297
+ throw new AuthenticationError(
1298
+ `Authentication failed: ${errorData}`,
1299
+ response.status,
1300
+ errorData
1301
+ );
1302
+ }
1303
+ throw new NetworkError(
1304
+ `HTTP error: ${errorData}`,
1305
+ response.status,
1306
+ errorData
1307
+ );
1308
+ }
1309
+ responses.push({
1310
+ id,
1311
+ status: response.statusText
1312
+ });
1313
+ } catch (error) {
1314
+ let errorMessage;
1315
+ if (error instanceof PinataError) {
1316
+ errorMessage = error.message;
1317
+ } else if (error instanceof Error) {
1318
+ errorMessage = `Error adding file ${id} to group: ${error.message}`;
1319
+ } else {
1320
+ errorMessage = `An unknown error occurred while adding file ${id} to group`;
1321
+ }
1322
+ responses.push({
1323
+ id,
1324
+ status: errorMessage
1325
+ });
1326
+ }
1327
+ }
1328
+ return responses;
1329
+ };
1330
+
1164
1331
  // src/core/groups/deleteGroup.ts
1165
1332
  var deleteGroup = async (config, options) => {
1166
1333
  if (!config) {
@@ -1714,6 +1881,10 @@ var FilterFiles = class {
1714
1881
  this.query.metadata = keyvalues;
1715
1882
  return this;
1716
1883
  }
1884
+ noGroup(noGroup) {
1885
+ this.query.noGroup = noGroup;
1886
+ return this;
1887
+ }
1717
1888
  pageToken(pageToken) {
1718
1889
  this.query.pageToken = pageToken;
1719
1890
  return this;
@@ -1945,12 +2116,12 @@ var Groups = class {
1945
2116
  get(options) {
1946
2117
  return getGroup(this.config, options);
1947
2118
  }
1948
- // addFiles(options: GroupCIDOptions): Promise<string> {
1949
- // return addToGroup(this.config, options);
1950
- // }
1951
- // removeFiles(options: GroupCIDOptions): Promise<string> {
1952
- // return removeFromGroup(this.config, options);
1953
- // }
2119
+ addFiles(options) {
2120
+ return addToGroup(this.config, options);
2121
+ }
2122
+ removeFiles(options) {
2123
+ return removeFromGroup(this.config, options);
2124
+ }
1954
2125
  update(options) {
1955
2126
  return updateGroup(this.config, options);
1956
2127
  }