UNPKG

textiot

Version:

A framework for building web and native (IoT) Dapps on the IPFS network

58 lines (57 loc) 2.67 kB
"use strict"; 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 isomorphic_form_data_1 = __importDefault(require("isomorphic-form-data")); /** * Mills is an API module for processing Textile mills * * @extends API */ class Mills extends api_1.API { /** * Run a mill over a given payload * * Currently supports: * * `/blob` - Process raw data blobs * * `/image/exif` - Extract EXIF data from an image * * `/image/resize` - Resize an image * * `/json` - Process (and validate according to schema.org definition) input JSON data * * `/schema` - Validate, add, and pin a new JSON-based Schema * * @param {string} name Name of the mill. (Relative uri). See above description for details * @param {KeyValue} options Schema options for the mill * @param {any} payload A multi-part form containing the payload * @param {KeyValue} [headers] Extra headers to send in the request * @returns The generated FileIndex object */ run(name, options, payload, headers) { return __awaiter(this, void 0, void 0, function* () { let data; if (payload && name !== '/json' && name !== '/schema') { // Unless explicitly using a json-based mill, assume binary data data = new isomorphic_form_data_1.default(); data.append('file', payload, payload.name || 'milled'); } else if (payload) { // Otherwise, assume JSON data data = JSON.stringify(payload); } const response = yield this.sendPost(`mills${name}`, [], options, data, headers, true // Don't strinify on the other end ); return response.json(); }); } } exports.default = Mills;