pinata 0.3.0 → 0.3.2

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
@@ -840,12 +840,7 @@ var getCid = async (config, cid) => {
840
840
  newUrl = `${newUrl}?pinataGatewayToken=${config?.pinataGatewayKey}`;
841
841
  }
842
842
  try {
843
- const request = await fetch(newUrl, {
844
- method: "GET",
845
- headers: {
846
- Source: "sdk/getCid"
847
- }
848
- });
843
+ const request = await fetch(newUrl);
849
844
  if (!request.ok) {
850
845
  const errorData = await request.json();
851
846
  if (request.status === 401) {
@@ -2043,6 +2038,207 @@ var analyticsDateInterval = async (config, options) => {
2043
2038
  }
2044
2039
  };
2045
2040
 
2041
+ // src/core/gateway/swapCid.ts
2042
+ var swapCid = async (config, options) => {
2043
+ if (!config || !config.pinataJwt) {
2044
+ throw new ValidationError("Pinata configuration or JWT is missing");
2045
+ }
2046
+ const data = JSON.stringify({
2047
+ swapCid: options.swapCid
2048
+ });
2049
+ let headers;
2050
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
2051
+ headers = { ...config.customHeaders };
2052
+ } else {
2053
+ headers = {
2054
+ Authorization: `Bearer ${config.pinataJwt}`,
2055
+ "Content-Type": "application/json",
2056
+ Source: "sdk/swapCid"
2057
+ };
2058
+ }
2059
+ let endpoint = "https://api.pinata.cloud";
2060
+ if (config.endpointUrl) {
2061
+ endpoint = config.endpointUrl;
2062
+ }
2063
+ try {
2064
+ const request = await fetch(`${endpoint}/v3/ipfs/swap/${options.cid}`, {
2065
+ method: "PUT",
2066
+ headers,
2067
+ body: data
2068
+ });
2069
+ if (!request.ok) {
2070
+ const errorData = await request.json();
2071
+ if (request.status === 401) {
2072
+ throw new AuthenticationError(
2073
+ "Authentication failed",
2074
+ request.status,
2075
+ errorData
2076
+ );
2077
+ }
2078
+ if (request.status === 403) {
2079
+ throw new PinataError(
2080
+ "Unauthorized CID Swap",
2081
+ request.status,
2082
+ errorData
2083
+ );
2084
+ }
2085
+ if (request.status === 404) {
2086
+ throw new PinataError(
2087
+ "CID not pinned to account",
2088
+ request.status,
2089
+ errorData
2090
+ );
2091
+ }
2092
+ throw new NetworkError(
2093
+ `HTTP error! status: ${request.status}`,
2094
+ request.status,
2095
+ errorData
2096
+ );
2097
+ }
2098
+ const res = await request.json();
2099
+ const resData = res.data;
2100
+ return resData;
2101
+ } catch (error) {
2102
+ if (error instanceof PinataError) {
2103
+ throw error;
2104
+ }
2105
+ if (error instanceof Error) {
2106
+ throw new PinataError(`Error processing CID Swap: ${error.message}`);
2107
+ }
2108
+ throw new PinataError("An unknown error occurred while swapping CID");
2109
+ }
2110
+ };
2111
+
2112
+ // src/core/gateway/swapHistory.ts
2113
+ var swapHistory = async (config, options) => {
2114
+ if (!config || !config.pinataJwt) {
2115
+ throw new ValidationError("Pinata configuration or JWT is missing");
2116
+ }
2117
+ let headers;
2118
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
2119
+ headers = { ...config.customHeaders };
2120
+ } else {
2121
+ headers = {
2122
+ Authorization: `Bearer ${config.pinataJwt}`,
2123
+ "Content-Type": "application/json",
2124
+ Source: "sdk/swapHistory"
2125
+ };
2126
+ }
2127
+ let endpoint = "https://api.pinata.cloud";
2128
+ if (config.endpointUrl) {
2129
+ endpoint = config.endpointUrl;
2130
+ }
2131
+ try {
2132
+ const request = await fetch(
2133
+ `${endpoint}/v3/ipfs/swap/${options.cid}?domain=${options.domain}`,
2134
+ {
2135
+ method: "GET",
2136
+ headers
2137
+ }
2138
+ );
2139
+ if (!request.ok) {
2140
+ const errorData = await request.json();
2141
+ if (request.status === 401) {
2142
+ throw new AuthenticationError(
2143
+ "Authentication failed",
2144
+ request.status,
2145
+ errorData
2146
+ );
2147
+ }
2148
+ if (request.status === 404) {
2149
+ throw new PinataError(
2150
+ "CID does not have history",
2151
+ request.status,
2152
+ errorData
2153
+ );
2154
+ }
2155
+ throw new NetworkError(
2156
+ `HTTP error! status: ${request.status}`,
2157
+ request.status,
2158
+ errorData
2159
+ );
2160
+ }
2161
+ const res = await request.json();
2162
+ const resData = res.data;
2163
+ return resData;
2164
+ } catch (error) {
2165
+ if (error instanceof PinataError) {
2166
+ throw error;
2167
+ }
2168
+ if (error instanceof Error) {
2169
+ throw new PinataError(`Error fetching swap history: ${error.message}`);
2170
+ }
2171
+ throw new PinataError(
2172
+ "An unknown error occurred while fetching swap history"
2173
+ );
2174
+ }
2175
+ };
2176
+
2177
+ // src/core/gateway/deleteSwap.ts
2178
+ var deleteSwap = async (config, cid) => {
2179
+ if (!config || !config.pinataJwt) {
2180
+ throw new ValidationError("Pinata configuration or JWT is missing");
2181
+ }
2182
+ let headers;
2183
+ if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
2184
+ headers = { ...config.customHeaders };
2185
+ } else {
2186
+ headers = {
2187
+ Authorization: `Bearer ${config.pinataJwt}`,
2188
+ "Content-Type": "application/json",
2189
+ Source: "sdk/deleteSwap"
2190
+ };
2191
+ }
2192
+ let endpoint = "https://api.pinata.cloud";
2193
+ if (config.endpointUrl) {
2194
+ endpoint = config.endpointUrl;
2195
+ }
2196
+ try {
2197
+ const request = await fetch(`${endpoint}/v3/ipfs/swap/${cid}`, {
2198
+ method: "DELETE",
2199
+ headers
2200
+ });
2201
+ if (!request.ok) {
2202
+ const errorData = await request.json();
2203
+ if (request.status === 401) {
2204
+ throw new AuthenticationError(
2205
+ "Authentication failed",
2206
+ request.status,
2207
+ errorData
2208
+ );
2209
+ }
2210
+ if (request.status === 403) {
2211
+ throw new PinataError(
2212
+ "Unauthorized CID Swap Deletion",
2213
+ request.status,
2214
+ errorData
2215
+ );
2216
+ }
2217
+ if (request.status === 404) {
2218
+ throw new PinataError(
2219
+ "CID not pinned to account",
2220
+ request.status,
2221
+ errorData
2222
+ );
2223
+ }
2224
+ throw new NetworkError(
2225
+ `HTTP error! status: ${request.status}`,
2226
+ request.status,
2227
+ errorData
2228
+ );
2229
+ }
2230
+ return request.statusText;
2231
+ } catch (error) {
2232
+ if (error instanceof PinataError) {
2233
+ throw error;
2234
+ }
2235
+ if (error instanceof Error) {
2236
+ throw new PinataError(`Error processing deleteSwap: ${error.message}`);
2237
+ }
2238
+ throw new PinataError("An unknown error occurred while deleting swap");
2239
+ }
2240
+ };
2241
+
2046
2242
  // src/core/pinataSDK.ts
2047
2243
  var formatConfig = (config) => {
2048
2244
  let gateway = config?.pinataGateway;
@@ -2299,6 +2495,15 @@ var Gateways = class {
2299
2495
  options.interval
2300
2496
  );
2301
2497
  }
2498
+ swapCid(options) {
2499
+ return swapCid(this.config, options);
2500
+ }
2501
+ swapHistory(options) {
2502
+ return swapHistory(this.config, options);
2503
+ }
2504
+ deleteSwap(cid) {
2505
+ return deleteSwap(this.config, cid);
2506
+ }
2302
2507
  };
2303
2508
  var FilterPinJobs = class {
2304
2509
  constructor(config) {