textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
111 lines (110 loc) • 4.47 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../core/api");
const schema_miller_1 = __importDefault(require("../helpers/schema-miller"));
const mills_1 = __importDefault(require("./mills"));
const threads_1 = __importDefault(require("./threads"));
/**
* Files is an API module for managing Textile files
*
* @extends {API}
*/
class Files extends api_1.API {
constructor(opts = api_1.DEFAULT_API_OPTIONS) {
super(opts);
this.mills = new mills_1.default(opts);
this.threads = new threads_1.default(opts);
}
/**
* Get a paginated array of files.
*
* @param thread Thread ID (can also use ‘default’). Omit for all
* @param offset Offset ID to start listing from. Omit for latest
* @param limit List page size (default 5)
* @returns An array of Thread objects
*/
list(thread, offset, limit) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('files', undefined, {
thread: thread || '',
offset: offset || '',
limit: limit || 5
});
return response.json();
});
}
/**
* Retrieves file encryption/decryption keys under the given target
*
* Note that the target id is _not_ the same as the block id. The target is the actual target
* file object.
*
* @param target ID of the target file
* @returns An array of file keys
*/
keys(target) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`keys/${target}`);
return response.json();
});
}
/**
* Ignores a thread file by its block ID
*
* This adds an 'ignore' thread block targeted at the file.
* Ignored blocks are by default not returned when listing.
*
* @param id ID of the thread file
* @returns The added ignore block
*/
ignore(id) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendDelete(`blocks/${id}`);
return response.json();
});
}
/**
* Add a file to a thread in your Textile node
*
* @param thread Id of the thread
* @param file A FormData object or a function for creating a FormData object
* @param caption Caption to associated with the added file object
* @returns An array of created File objects
*/
add(file, caption, thread) {
return __awaiter(this, void 0, void 0, function* () {
if (!thread) {
thread = 'default';
}
// Fetch schema (will throw if it doesn't have a schema node)
const schemaNode = (yield this.threads.get(thread)).schema_node;
if (!schemaNode) {
throw new Error('A thread schema is required before adding files to a thread.');
}
// Mill the file(s) before adding it
const dir = yield schema_miller_1.default.mill(file, schemaNode, (mill, opts, form, headers) => __awaiter(this, void 0, void 0, function* () {
const file = yield this.mills.run(mill, opts, form, headers);
return file;
}));
// TODO: Do more than just wrap dirs in list
const dirs = {
items: [dir]
};
const response = yield this.sendPost(`threads/${thread}/files`, undefined, { caption }, dirs);
return response.json();
});
}
}
exports.default = Files;
;