textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
112 lines (111 loc) • 4.66 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.imageContentForMinWidth = exports.content = exports.list = exports.file = exports.shareFiles = exports.addFiles = exports.addData = void 0;
const react_native_1 = require("react-native");
const buffer_1 = require("buffer");
const model_1 = require("./model");
const { FilesBridge } = react_native_1.NativeModules;
/**
* Add raw data to a Textile thread
* @param base64 Raw data as a base64 string
* @param threadId The thread id the data will be added to
* @param caption A caption to associate with the data
* @returns A Promise that will resolve with the Block result
*/
function addData(base64, threadId, caption) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield FilesBridge.addData(base64, threadId, caption);
return model_1.Block.decode(buffer_1.Buffer.from(result, 'base64'));
});
}
exports.addData = addData;
/**
* Add files to a Textile thread
* @param paths Comma separated list of file paths to add, can be file system paths, IPFS hashes, or existing hashes
* @param threadId The thread id the files will be added to
* @param caption A caption to associate with the files
* @returns A Promise that will resolve with the Block result
*/
function addFiles(paths, threadId, caption) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield FilesBridge.addFiles(paths, threadId, caption);
return model_1.Block.decode(buffer_1.Buffer.from(result, 'base64'));
});
}
exports.addFiles = addFiles;
/**
* Share files already aded to a Textile thread to a Textile thread
* @param target The source thread target of the files to share
* @param threadId The thread id the files will be shared to
* @param caption A caption to associate with the files
* @returns A Promise that will resolve with the Block result
*/
function shareFiles(target, threadId, caption) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield FilesBridge.shareFiles(target, threadId, caption);
return model_1.Block.decode(buffer_1.Buffer.from(result, 'base64'));
});
}
exports.shareFiles = shareFiles;
/**
* Get a Files object by block id
* @param blockId The block id of the Files to get
* @returns A Promise that will resolve with the Files result
*/
function file(blockId) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield FilesBridge.file(blockId);
return model_1.Files.decode(buffer_1.Buffer.from(result, 'base64'));
});
}
exports.file = file;
/**
* List all files or files in a known Thread.
* ```typescript
* Textile.files.list(threadId, offset, limit);
* ```
*/
function list(threadId, offset, limit) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield FilesBridge.list(threadId, offset, limit);
return model_1.FilesList.decode(buffer_1.Buffer.from(result, 'base64'));
});
}
exports.list = list;
/**
* Get the raw data for a file at an IPFS hash.
* ```typescript
* Textile.files.content(hash);
* ```
*/
function content(hash) {
return __awaiter(this, void 0, void 0, function* () {
const { data, mediaType } = yield FilesBridge.content(hash);
return { data: buffer_1.Buffer.from(data, 'base64'), mediaType };
});
}
exports.content = content;
/**
* Get the best size image from a Thread with MEDIA type thread given a minimum width.
*
* Note: pth is <target>/<index>, e.g., "Qm.../0"
* ```typescript
* Textile.files.imageContentForMinWidth(path, minWidth);
* ```
*/
function imageContentForMinWidth(pth, minWidth) {
return __awaiter(this, void 0, void 0, function* () {
const { data, mediaType } = yield FilesBridge.imageContentForMinWidth(pth, minWidth);
return { data: buffer_1.Buffer.from(data, 'base64'), mediaType };
});
}
exports.imageContentForMinWidth = imageContentForMinWidth;
;