textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
117 lines (116 loc) • 4.51 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 threads_1 = __importDefault(require("./threads"));
const dot = '%2E'; // needed as curl shortens /. to /
/**
* Blocks is an API module for managing Textile blocks
*
* Blocks are the raw components in a thread. Think of them as an append-only log of thread updates
* where each update is hash-linked to its parent(s). New / recovering peers can sync history by
* simply traversing the hash tree.
*
* There are several block types:
*
* - MERGE: 3-way merge added.
* - IGNORE: A block was ignored.
* - FLAG: A block was flagged.
* - JOIN: Peer joined.
* - ANNOUNCE: Peer set username / avatar / inbox addresses
* - LEAVE: Peer left.
* - TEXT: Text message added.
* - FILES: File(s) added.
* - COMMENT: Comment added to another block.
* - LIKE: Like added to another block.
*
* @extends {API}
*/
class Blocks extends api_1.API {
constructor(opts = api_1.DEFAULT_API_OPTIONS) {
super(opts);
this.threads = new threads_1.default(opts);
}
/**
* Retrieves a block by ID
*
* @param id ID of the target block
* @returns The block object
*/
meta(id) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`blocks/${id}/meta`);
return response.json();
});
}
/**
* 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 Block objects
*/
list(thread, offset, limit) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('blocks', undefined, {
thread: thread || '',
offset: offset || '',
limit: limit || 5
});
return response.json();
});
}
/**
* Ignores a block by its ID
*
* @param id ID of the block
* @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();
});
}
/**
* Get the decrypted file content of a file within a files block
*
* @param id ID of the target block
* @param index Index of the target file (defaults to '0')
* @param path Path of the target file under the index (e.g., 'small')
* @returns The file contents as an arrayBuffer (for a blob, use `file.content()`)
*/
fileContent(id, index, path) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`blocks/${id}/files/${index || '0'}/${path || dot}/content`);
return response.arrayBuffer();
});
}
/**
* Get the metadata of a file within a files block
*
* @param id ID of the target block
* @param index Index of the target file (defaults to '0')
* @param path Path of the target file under the index (e.g., 'small')
* @returns The file contents as an arrayBuffer (for a blob, use `file.meta()`)
*/
fileMeta(id, index, path) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`blocks/${id}/files/${index || '0'}/${path || dot}/meta`);
return response.arrayBuffer();
});
}
}
exports.default = Blocks;
;