textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
95 lines (94 loc) • 3.58 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 });
const api_1 = require("../core/api");
const handlers_1 = require("../helpers/handlers");
/**
* Contacts is an API module for managing local contacts and finding contacts on the network
*
* @extends API
*/
class Contacts extends api_1.API {
/**
* Adds or updates a contact directly, usually from a search
*
* @param contact JSON object representing a contact
* @returns Whether the operation was sucessfull
*/
add(address, contact) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPut(`contacts/${address}`, undefined, undefined, contact);
return response.status === 204;
});
}
/**
* Retrieve information about a known contact
*
* @param address Address of the contact
* @returns The associated contact object
*/
get(address) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`contacts/${address}`);
return response.json();
});
}
/**
* Retrieves a list of known contacts
* @returns An array of all known contacts
*/
list() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('contacts');
return response.json();
});
}
/**
* Remove a known contact
*
* @param address Address of the contact
* @returns Whether the operation was successfull
*/
remove(contactId) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendDelete(`contacts/${contactId}`);
return response.status === 204;
});
}
/**
* Searches locally and on the network for contacts by display name, peer id, or address
*
* @param name Search by display name string
* @param address Search by account address string
* @param options Additional options to control the query
* @returns A ReadableStream of QueryResult objects.
* })
*/
search(name, address, options) {
return __awaiter(this, void 0, void 0, function* () {
const opts = options || {};
const cleanOpts = {
name: name || '',
address: address || '',
local: opts.local || false,
remote: opts.remote || false,
limit: opts.limit || 5,
wait: opts.wait || 2
};
const response = yield this.sendPost('contacts/search', undefined, cleanOpts);
if (!response.body) {
throw Error('Empty response stream');
}
return handlers_1.streamHandler(response.body);
});
}
}
exports.default = Contacts;
;