mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-28 15:21:33 +00:00
Interface types (#933)
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fluence",
|
"name": "fluence",
|
||||||
"version": "0.7.0",
|
"version": "0.7.3",
|
||||||
"description": "the browser js-libp2p client for the Fluence network",
|
"description": "the browser js-libp2p client for the Fluence network",
|
||||||
"main": "./dist/fluence.js",
|
"main": "./dist/fluence.js",
|
||||||
"typings": "./dist/fluence.d.ts",
|
"typings": "./dist/fluence.d.ts",
|
||||||
|
85
src/Interface.ts
Normal file
85
src/Interface.ts
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface Interface {
|
||||||
|
service_id: string,
|
||||||
|
modules: Module[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkFunction(f: any): f is Function {
|
||||||
|
if (!f.name) throw Error(`There is no 'name' field in Function struct: ${JSON.stringify(f)}`)
|
||||||
|
|
||||||
|
if (f.input_types) {
|
||||||
|
if (!(f.input_types instanceof Array)) {
|
||||||
|
throw Error(`'input_types' should be an array: ${JSON.stringify(f)}`)
|
||||||
|
}
|
||||||
|
f.input_types.forEach((i: any) => {
|
||||||
|
if ((typeof i) !== 'string') {
|
||||||
|
throw Error(`'input_types' should be a string: ${JSON.stringify(f)}`)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (f.output_types) {
|
||||||
|
if (!(f.output_types instanceof Array)) {
|
||||||
|
throw Error(`'output_types' should be an array: ${JSON.stringify(f)}`)
|
||||||
|
}
|
||||||
|
f.output_types.forEach((o: any) => {
|
||||||
|
if ((typeof o) !== 'string') {
|
||||||
|
throw Error(`'output_types' should be a string: ${JSON.stringify(f)}`)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkModule(module: any): module is Module {
|
||||||
|
if (!module.name) throw Error(`There is no 'name' field in Module struct: ${JSON.stringify(module)}`)
|
||||||
|
if (!module.functions) {
|
||||||
|
module.functions.forEach((f: any) => {
|
||||||
|
checkFunction(f)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an error if 'i' is not an Interface type.
|
||||||
|
*/
|
||||||
|
export function checkInterface(i: any): i is Interface {
|
||||||
|
if (!i.service_id) throw new Error(`There is no 'service_id' field in Interface struct: ${JSON.stringify(i)}`)
|
||||||
|
if (i.modules) {
|
||||||
|
i.modules.forEach((module: any) => {
|
||||||
|
checkModule(module);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Module {
|
||||||
|
name: string,
|
||||||
|
functions: Function[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Function {
|
||||||
|
name: string,
|
||||||
|
input_types: string[],
|
||||||
|
output_types: string[]
|
||||||
|
}
|
@ -29,6 +29,7 @@ import Multiaddr from "multiaddr"
|
|||||||
import {Subscriptions} from "./subscriptions";
|
import {Subscriptions} from "./subscriptions";
|
||||||
import * as PeerInfo from "peer-info";
|
import * as PeerInfo from "peer-info";
|
||||||
import {FluenceConnection} from "./fluence_connection";
|
import {FluenceConnection} from "./fluence_connection";
|
||||||
|
import {checkInterface, Interface} from "./Interface";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param target receiver
|
* @param target receiver
|
||||||
@ -290,20 +291,37 @@ export class FluenceClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getInterface(serviceId: string, peerId?: string): Promise<any> {
|
async getInterface(serviceId: string, peerId?: string): Promise<Interface> {
|
||||||
let resp;
|
let resp;
|
||||||
resp = await this.callPeer("get_interface", {service_id: serviceId}, undefined, peerId)
|
resp = await this.callPeer("get_interface", {service_id: serviceId}, undefined, peerId)
|
||||||
return resp.interface;
|
let i = resp.interface;
|
||||||
|
|
||||||
|
if (checkInterface(i)) {
|
||||||
|
return i;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unexpected");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getActiveInterfaces(peerId?: string): Promise<any> {
|
async getActiveInterfaces(peerId?: string): Promise<Interface[]> {
|
||||||
let resp;
|
let resp;
|
||||||
if (peerId) {
|
if (peerId) {
|
||||||
resp = await this.sendCallWaitResponse(createPeerAddress(peerId), {}, "get_active_interfaces");
|
resp = await this.sendCallWaitResponse(createPeerAddress(peerId), {}, "get_active_interfaces");
|
||||||
} else {
|
} else {
|
||||||
resp = await this.callPeer("get_active_interfaces", {}, undefined, peerId);
|
resp = await this.callPeer("get_active_interfaces", {}, undefined, peerId);
|
||||||
}
|
}
|
||||||
return resp.active_interfaces;
|
let interfaces = resp.active_interfaces;
|
||||||
|
if (interfaces && interfaces instanceof Array) {
|
||||||
|
return interfaces.map((i: any) => {
|
||||||
|
if (checkInterface(i)) {
|
||||||
|
return i;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unexpected");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error("Unexpected");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAvailableModules(peerId?: string): Promise<string[]> {
|
async getAvailableModules(peerId?: string): Promise<string[]> {
|
||||||
|
Reference in New Issue
Block a user