UNPKG

textiot

Version:

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

203 lines (202 loc) 7.95 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 }); exports.API = exports.createHeaders = exports.getOpts = exports.getArgs = exports.DEFAULT_API_OPTIONS = void 0; require("@ef-carbon/fetch/install"); const url_parse_1 = __importDefault(require("url-parse")); const url_toolkit_1 = require("url-toolkit"); exports.DEFAULT_API_OPTIONS = { url: 'http://127.0.0.1', port: 40600, version: 0 }; /** * Create 'args' like a CLI command would take * * @param {string[]} argsAr An array of arguments * @private */ exports.getArgs = (argsAr) => { if (!argsAr || !argsAr.length) { return ''; } return argsAr.map((ar) => encodeValue(ar)).join(','); }; /** * Create 'options' like a CLI command would take. * * @param {Object.<string, string>} opts A map of option keys and values * @private */ exports.getOpts = (opts) => { if (!opts) { return ''; } return Object.keys(opts) .map((key) => `${key}=${encodeValue(opts[key])}`) .join(','); }; const encodeValue = (val) => { return encodeURIComponent(val.toString()); }; exports.createHeaders = (args, opts, headers) => { const h = headers || {}; return Object.assign(Object.assign({}, h), { 'X-Textile-Args': exports.getArgs(args), 'X-Textile-Opts': exports.getOpts(opts) }); }; const handleErrors = (response) => { if (!response.ok) { throw Error(response.statusText); } return response; }; /** * API is the base class for all SDK modules. * * @params {ApiOptions] opts API options object */ class API { constructor(opts = exports.DEFAULT_API_OPTIONS) { this.opts = opts; const url = new url_parse_1.default(opts.url); if (opts.port) { url.set('port', opts.port); } url.set('pathname', `/api/v${opts.version || 0}/`); this.baseURL = url.toString(); const gateway = new url_parse_1.default(this.opts.url); gateway.set('port', 5050); gateway.set('pathname', `/ipfs/`); this.gatewayURL = gateway.toString(); } /** * Make a get request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers */ sendGatewayGet(path, headers) { return __awaiter(this, void 0, void 0, function* () { return fetch(url_toolkit_1.buildAbsoluteURL(this.gatewayURL, path), { method: 'GET', headers: exports.createHeaders([], {}, headers) }); }); } /** * Make a post request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers * @param data An object of data to post */ sendPost(url, args, opts, data, headers, raw) { return __awaiter(this, void 0, void 0, function* () { const h = exports.createHeaders(args, opts, headers); const response = yield fetch(url_toolkit_1.buildAbsoluteURL(this.baseURL, url), { method: 'POST', headers: new Headers(h), body: raw ? data : JSON.stringify(data) }); return handleErrors(response); }); } /** * Make a get request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers */ sendGet(url, args, opts, headers) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(url_toolkit_1.buildAbsoluteURL(this.baseURL, url), { method: 'GET', headers: exports.createHeaders(args, opts, headers) }); return handleErrors(response); }); } /** * Make a delete request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers */ sendDelete(url, args, opts, headers) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(url_toolkit_1.buildAbsoluteURL(this.baseURL, url), { method: 'DELETE', headers: exports.createHeaders(args, opts, headers) }); return handleErrors(response); }); } /** * Make a put request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers * @param data An object of data to put */ sendPut(url, args, opts, data, headers) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(url_toolkit_1.buildAbsoluteURL(this.baseURL, url), { method: 'PUT', headers: exports.createHeaders(args, opts, headers), body: JSON.stringify(data) }); return handleErrors(response); }); } /** * Make a patch request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as Textile args headers * @param opts An object of options to pass as Textile options headers * @param data An object of data to put */ sendPatch(url, args, opts, data, headers) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(url_toolkit_1.buildAbsoluteURL(this.baseURL, url), { method: 'patch', headers: exports.createHeaders(args, opts, headers), body: JSON.stringify(data) }); return handleErrors(response); }); } /** * Make an EventSource request to the Textile node * * @param url The relative URL of the API endpoint * @param args An array of arguments to pass as query in native EventSource or Textile args headers in EventSourcePolyfill * @param opts An object of options to pass as Textile options headers */ sendEventSource(url, args, opts, headers) { // native EventSource can't set header, but can CORS return new EventSource(url_toolkit_1.buildAbsoluteURL(this.baseURL, `${url}${opts ? `?${url_parse_1.default.qs.stringify(opts)}` : ''}`)); // EventSourcePolyfill of eventsource@1.0.7 can set header, but can't CORS // return new EventSourcePolyfill(buildAbsoluteURL(this.baseURL, url), { // headers: { // 'X-Textile-Opts': getOpts(opts), // } // }); } } exports.API = API;