@blockfrost/blockfrost-js
Version:
A JavaScript/TypeScript SDK for interacting with the https://blockfrost.io API
144 lines (143 loc) • 4.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pinRemove = exports.listByPath = exports.list = exports.pin = exports.gateway = exports.add = void 0;
const errors_1 = require("../../utils/errors");
const utils_1 = require("../../utils");
const form_data_1 = __importDefault(require("form-data"));
const fs_1 = __importDefault(require("fs"));
/**
* Adds a file to IPFS
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Add | API docs for Add a file to IPFS}
* @remarks
* You need to `pin` an object to avoid it being garbage collected. This usage is being counted in your user account quota.
*
* @param path - path to the file
* @returns information about added ipfs object
*/
async function add(path) {
const stream = fs_1.default.createReadStream(path);
const data = new form_data_1.default();
data.append('file', stream);
try {
const res = await this.instance.post(`ipfs/add`, {
body: data,
headers: {
'Content-Type': `multipart/form-data; boundary=${data.getBoundary()}`,
},
});
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.add = add;
/**
* Retrieve an object from the IPFS gateway
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Gateway/paths/~1ipfs~1gateway~1%7BIPFS_path%7D/get | API docs for Relay to an IPFS gateway}
* @remarks
* Useful if you do not want to rely on a public gateway, such as ipfs.blockfrost.dev.
*
* @param path - path to the file
* @returns the object content
*
*/
async function gateway(path) {
try {
const res = await this.instance.get(`ipfs/gateway`, {
searchParams: { path },
});
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.gateway = gateway;
/**
* Pins the IPFS resource.
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Pins/paths/~1ipfs~1pin~1add~1%7BIPFS_path%7D/post | API docs for Pin an object}
* @remarks
* IPFS pinning refers to the process of specifying data to be retained and persist on one or more IPFS nodes.
*
* @param path - path to the file
* @returns Pinned object
*/
async function pin(path, options) {
try {
const res = await this.instance.post(`ipfs/pin/add/${path}`, {
searchParams: {
...(options?.filecoin !== undefined && { filecoin: options.filecoin }),
},
});
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.pin = pin;
/**
* List pinned IPFS resources.
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Pins/paths/~1ipfs~1pin~1list/get | API docs for List pinned objects}
*
* @param pagination - Optional, Pagination options
* @returns List of pinned IPFS objects
*
*/
async function list(pagination) {
const paginationOptions = (0, utils_1.getPaginationOptions)(pagination);
try {
const res = await this.instance(`ipfs/pin/list`, {
searchParams: {
page: paginationOptions.page,
count: paginationOptions.count,
order: paginationOptions.order,
},
});
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.list = list;
/**
* Obtains information about locally pinned IPFS object
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Pins/paths/~1ipfs~1pin~1list~1%7BIPFS_path%7D/get | API docs for Details about pinned object}
*
* @param path - The path to the IPFS object (IPFS hash)
* @returns List of pinned IPFS objects
*
*/
async function listByPath(path) {
try {
const res = await this.instance(`ipfs/pin/list/${path}`);
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.listByPath = listByPath;
/**
* Removes pinned object from local storage
* @see {@link https://docs.blockfrost.io/#tag/IPFS-Pins/paths/~1ipfs~1pin~1remove~1%7BIPFS_path%7D/post | API docs for Remove a IPFS pin}
*
* @param path - The path to the IPFS object (IPFS hash)
* @returns List of pinned IPFS objects
*
*/
async function pinRemove(path) {
try {
const res = await this.instance.post(`ipfs/pin/remove/${path}`);
return res.body;
}
catch (error) {
throw (0, errors_1.handleError)(error);
}
}
exports.pinRemove = pinRemove;