From 4074d95f1e39045ea753aabf8c41236427f663d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Wed, 9 Jan 2019 16:15:32 +0100 Subject: [PATCH] added comments on some internal classes #79 --- src/ConfigProvider.ts | 14 +++++++- src/aquarius/Aquarius.ts | 29 ++++++++++++++--- src/aquarius/AquariusProvider.ts | 17 ++++++++-- src/brizo/Brizo.ts | 5 ++- src/brizo/BrizoProvider.ts | 17 ++++++++-- src/keeper/Keeper.ts | 55 +++++++++++++++++++++++++++++++- src/keeper/Web3Provider.ts | 10 +++++- src/utils/WebServiceConnector.ts | 13 +++++--- 8 files changed, 142 insertions(+), 18 deletions(-) diff --git a/src/ConfigProvider.ts b/src/ConfigProvider.ts index d133fc8..34242ee 100644 --- a/src/ConfigProvider.ts +++ b/src/ConfigProvider.ts @@ -1,15 +1,27 @@ import Config from "./models/Config" +/** + * Stores the configuration of the library. + */ export default class ConfigProvider { + /** + * @return {Config} Library config. + */ public static getConfig(): Config { return ConfigProvider.config } + /** + * @param {Config} Library config. + */ public static setConfig(config: Config) { - ConfigProvider.config = config } + /** + * Library config. + * @type {Config} + */ private static config: Config } diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index fba0110..abe61cb 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -8,17 +8,18 @@ import SearchQuery from "./query/SearchQuery" const apiPath = "/api/v1/aquarius/assets/ddo" +/** + * Provides a interface with Aquarius. + * Aquarius provides an off-chain database store for metadata about data assets. + */ export default class Aquarius { - private url: string constructor(config: Config) { - this.url = config.aquariusUri } public async getAccessUrl(accessToken: any, payload: any): Promise { - const accessUrl: string = await WebServiceConnectorProvider.getConnector() .post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload) .then((response: any): string => { @@ -40,8 +41,12 @@ export default class Aquarius { return accessUrl } + /** + * Search over the DDOs using a query. + * @param {SearchQuery} query Query to filter the DDOs. + * @return {Promise} + */ public async queryMetadata(query: SearchQuery): Promise { - const result: DDO[] = await WebServiceConnectorProvider.getConnector() .post(`${this.url}${apiPath}/query`, JSON.stringify(query)) .then((response: any) => { @@ -64,8 +69,12 @@ export default class Aquarius { return result } + /** + * Search over the DDOs using a query. + * @param {SearchQuery} query Query to filter the DDOs. + * @return {Promise} + */ public async queryMetadataByText(query: SearchQuery): Promise { - const fullUrl = new URL(`${this.url}${apiPath}/query`) fullUrl.searchParams.append("text", query.text) fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort))) @@ -93,6 +102,11 @@ export default class Aquarius { return result } + /** + * Stores a DDO in Aquarius. + * @param {DDO} ddo DDO to be stored. + * @return {Promise} Final DDO. + */ public async storeDDO(ddo: DDO): Promise { const fullUrl = `${this.url}${apiPath}` const result: DDO = await WebServiceConnectorProvider.getConnector() @@ -115,6 +129,11 @@ export default class Aquarius { return result } + /** + * Retrieves a DDO by DID. + * @param {DID} did DID of the asset. + * @return {Promise} DDO of the asset. + */ public async retrieveDDO(did: DID): Promise { const fullUrl = `${this.url}${apiPath}/${did.getDid()}` const result = await WebServiceConnectorProvider.getConnector() diff --git a/src/aquarius/AquariusProvider.ts b/src/aquarius/AquariusProvider.ts index e881643..3422aa7 100644 --- a/src/aquarius/AquariusProvider.ts +++ b/src/aquarius/AquariusProvider.ts @@ -1,20 +1,33 @@ import ConfigProvider from "../ConfigProvider" import Aquarius from "./Aquarius" +/** + * Provides the Aquarius instance. + */ export default class AquariusProvider { + /** + * Set an Aquarius instance. + * @param {Aquarius} aquarius New Aquarius instance. + */ public static setAquarius(aquarius: Aquarius) { - AquariusProvider.aquarius = aquarius } + /** + * Returns Acuarius instance. It creates a new one if it's not defined. + * @returns {Aquarius} Aquarius instance. + */ public static getAquarius() { - if (!AquariusProvider.aquarius) { AquariusProvider.aquarius = new Aquarius(ConfigProvider.getConfig()) } return AquariusProvider.aquarius } + /** + * Aquarius instance. + * @type {Aquarius} + */ private static aquarius: Aquarius = null } diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index 900225c..58c97f1 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -3,11 +3,14 @@ import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider" const apiPath = "/api/v1/brizo/services" +/** + * Provides a interface with Brizo. + * Brizo is the technical component executed by the Publishers allowing to them to provide extended data services. + */ export default class Brizo { private url: string constructor(config: Config) { - this.url = config.brizoUri } diff --git a/src/brizo/BrizoProvider.ts b/src/brizo/BrizoProvider.ts index f8e5c3d..1584e70 100644 --- a/src/brizo/BrizoProvider.ts +++ b/src/brizo/BrizoProvider.ts @@ -1,20 +1,33 @@ import ConfigProvider from "../ConfigProvider" import Brizo from "./Brizo" +/** + * Provides the Brizo instance. + */ export default class BrizoProvider { + /** + * Set an Brizo instance. + * @param {Brizo} brizo New Brizo instance. + */ public static setBrizo(brizo: Brizo) { - BrizoProvider.brizo = brizo } + /** + * Returns Acuarius instance. It creates a new one if it's not defined. + * @returns {Brizo} brizo instance. + */ public static getBrizo() { - if (!BrizoProvider.brizo) { BrizoProvider.brizo = new Brizo(ConfigProvider.getConfig()) } return BrizoProvider.brizo } + /** + * Brizo instance. + * @type {Brizo} + */ private static brizo: Brizo = null } diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 6c66ac2..a8f4fad 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -8,9 +8,20 @@ import OceanToken from "./contracts/Token" import Web3Provider from "./Web3Provider" +/** + * Interface with Ocean Keeper contracts. + * Ocean Keeper implementation where we put the following modules together: + * - TCRs: users create challenges and resolve them through voting to maintain registries. + * - Ocean Tokens: the intrinsic tokens circulated inside Ocean network, which is used in the voting of TCRs. + * - Marketplace: the core marketplace where people can transact with each other with Ocean tokens. + */ export default class Keeper { - public static async getInstance() { + /** + * Returns Keeper instance. + * @return {Promise} + */ + public static async getInstance(): Promise { if (Keeper.instance === null) { Keeper.instance = new Keeper() @@ -26,16 +37,58 @@ export default class Keeper { return Keeper.instance } + /** + * Keeper instance. + * @type {Keeper} + */ private static instance: Keeper = null + /** + * Ocean Token smart contract instance. + * @type {OceanToken} + */ public token: OceanToken + + /** + * Ocean Market smart contract instance. + * @type {OceanMarket} + */ public market: OceanMarket + + /** + * Ocean Auth smart contract instance. + * @type {OceanAuth} + */ public auth: OceanAuth + + /** + * Service agreement smart contract instance. + * @type {ServiceAgreement} + */ public serviceAgreement: ServiceAgreement + + /** + * Access conditions smart contract instance. + * @type {AccessConditions} + */ public accessConditions: AccessConditions + + /** + * Payment conditions smart contract instance. + * @type {PaymentConditions} + */ public paymentConditions: PaymentConditions + + /** + * DID registry smart contract instance. + * @type {DIDRegistry} + */ public didRegistry: DIDRegistry + /** + * Returns the network by name. + * @return {Promise} Network name. + */ public async getNetworkName(): Promise { return Web3Provider.getWeb3().eth.net.getId() .then((networkId) => { diff --git a/src/keeper/Web3Provider.ts b/src/keeper/Web3Provider.ts index 5f92759..be0472c 100644 --- a/src/keeper/Web3Provider.ts +++ b/src/keeper/Web3Provider.ts @@ -3,7 +3,11 @@ import ConfigProvider from "../ConfigProvider" export default class Web3Provider { - public static getWeb3() { + /** + * Returns Web3 instance. + * @return {Web3} + */ + public static getWeb3(): Web3 { if (Web3Provider.web3 === null) { const config = ConfigProvider.getConfig() const web3Provider = config.web3Provider || new Web3.providers.HttpProvider(config.nodeUri) @@ -12,5 +16,9 @@ export default class Web3Provider { return Web3Provider.web3 } + /** + * Web3 instance. + * @type {Web3} + */ private static web3: Web3 = null } diff --git a/src/utils/WebServiceConnector.ts b/src/utils/WebServiceConnector.ts index 3e1b5d2..8449e17 100644 --- a/src/utils/WebServiceConnector.ts +++ b/src/utils/WebServiceConnector.ts @@ -1,8 +1,11 @@ -import fetch from "node-fetch" +import fetch, { Response, RequestInit, BodyInit } from "node-fetch" +/** + * Provides a common interface to web services. + */ export default class WebServiceConnector { - public async post(url, payload): Promise { + public async post(url: string, payload: BodyInit): Promise { return this.fetch(url, { method: "POST", body: payload, @@ -12,7 +15,7 @@ export default class WebServiceConnector { }) } - public async get(url): Promise { + public async get(url: string): Promise { return this.fetch(url, { method: "GET", headers: { @@ -21,7 +24,7 @@ export default class WebServiceConnector { }) } - public async put(url, payload): Promise { + public async put(url: string, payload: BodyInit): Promise { return this.fetch(url, { method: "PUT", body: payload, @@ -31,7 +34,7 @@ export default class WebServiceConnector { }) } - private async fetch(url, opts): Promise { + private async fetch(url: string, opts: RequestInit): Promise { return fetch(url, opts) } }