/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ // import { EventEmitter } from 'events'; interface ListenFunc { (): void; } /** * @description The subscribe listener */ interface Subscriber { (content: any): void; } /** * @description common options */ export interface CommonInputOptions { dataId: string; group?: string; unit?: string; } export interface UnitOptions { unit: string; } /** * @description Diamond client interface */ export interface BaseClient extends IClientWorker { /** * @description 获取当前机器所在机房 * @returns {Promise} currentUnit */ getCurrentUnit(): Promise; /** * @description 获取所有单元信息 */ getAllUnits(): Promise; /** * 将配置发布到所有单元 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {String} content - config value * @returns {Promise} true | false */ publishToAllUnit(dataId: string, group: string, content: string): Promise; /** * @description 将配置从所有单元中删除 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @returns {Promise} true | false */ removeToAllUnit(dataId: string, group: string): Promise; } /** * 每个 diamond 环境实例 */ export interface IClientWorker { /** * @description 获取配置 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {Object} [options] * - {String} unit - which unit you want to connect, default is current unit * @returns {Promise} value */ getConfig(dataId: string, group: string, options?: UnitOptions): Promise; /** * @description 发布配置 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {String} content - config value * @param {Object} [options] * - {String} unit - which unit you want to connect, default is current unit * @returns {Promise} true | false */ publishSingle(dataId: string, group: string, content: string, options?: UnitOptions): Promise; /** * @description 删除配置 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {Object} [options] * - {String} unit - which unit you want to connect, default is current unit * @return {Promise} true | false */ remove(dataId: string, group: string, options?: UnitOptions): Promise; /** * @description 推送聚合数据 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {String} datumId - id of datum * @param {String} content * @param {Object} [options] * - {String} unit * @returns {Promise} true | false */ publishAggr(dataId: string, group: string, datumId: string, content: string, options?: UnitOptions): Promise; /** * @description 删除聚合数据 * @param {String} dataId - id of the data * @param {String} group - group name of the data * @param {String} datumId - id of datum * @param {Object} [options] * - {String} unit * @returns {Promise} true | false */ removeAggr(dataId: string, group: string, datumId: string, options?: UnitOptions): Promise; /** * @description 批量获取配置 * @param {Array} dataIds - data id array * @param {String} group - group name of the data * @param {Object} [options] * - {String} unit - which unit you want to connect, default is current unit * @returns {Promise} result */ batchGetConfig(dataIds: string[], group: string, options?: UnitOptions): Promise; /** * @description 批量查询 * @param {Array} dataIds - data id array * @param {String} group - group name of the data * @param {Object} [options] * - {String} unit - which unit you want to connect, default is current unit * @returns {Promise} result */ batchQuery(dataIds: string[], group: string, options?: UnitOptions): Promise; /** * @description 订阅 * @param {Object} reg * - {String} dataId - id of the data you want to subscribe * - {String} [group] - group name of the data * - {String} [unit] - which unit you want to connect, default is current unit * @param {Function} listener - listener(content: string) * @returns {InstanceType} DiamondClient */ subscribe(reg: CommonInputOptions, listener: Subscriber); /** * @description 取消订阅 * @param {Object} reg * - {String} dataId - id of the data you want to unsubscribe * - {String} [group] - group name of the data * - {String} [unit] - which unit you want to connect, default is current unit * @param {Function} [listener] * - listener(content: string) * @returns {InstanceType} DiamondClient */ unSubscribe(reg: CommonInputOptions, listener?: ListenFunc); /** * @description 查询租户下的所有的配置 */ getConfigs(): Promise>; /** * @description close connection */ close(): void; on?(evt: string, fn: (err: Error) => void): void; } /** * 服务列表管理器 */ export interface IServerListManager { /** * 获取当前单元 */ getCurrentUnit(): Promise; /** * 获取单元列表 */ fetchUnitLists(): Promise>; /** * 更新当前服务器 */ updateCurrentServer(unit?: string): Promise; /** * 获取一个服务器地址 * @param unit */ getCurrentServerAddr(unit?: string): Promise; /** * @description close connection */ close(); // on(evt: string, fn: (err: Error) => void): void; } export interface ISnapshot { cacheDir; get(key: string): any; save(key: string, value: any); delete(key: string); batchSave(arr: Array); } export interface NacosHttpError extends Error { url?: string; params?: any; body?: any; unit?: string; dataId?: string; group?: string; } export interface SnapShotData { key?: string; value?: string; } export interface ClientOptions { endpoint?: string; // 寻址模式下的对端 host serverPort?: number; // 对端端口 namespace?: string; // 阿里云的 namespace accessKey?: string; // 阿里云的 accessKey secretKey?: string; // 阿里云的 secretKey httpclient?: any; // http 请求客户端,默认为 urllib appName?: string; // 应用名,可选 ssl?: boolean; // 是否为 https 请求 refreshInterval?: number; // 重新拉去地址列表的间隔时间 contextPath?: string; // 请求的 contextPath clusterName?: string; // 请求的 path requestTimeout?: number; // 请求超时时间 defaultEncoding?: string; // 请求编码 serverAddr?: string; // 用于直连,包含端口 unit?: string; // 内部单元化用 nameServerAddr?: string; // 老的兼容参数,逐步废弃,同 endpoint cacheDir?: string; // 缓存文件的路径 } export enum ClientOptionKeys { ENDPOINT = 'endpoint', SERVER_PORT = 'serverPort', NAMESPACE = 'namespace', ACCESSKEY = 'accessKey', SECRETKEY = 'secretKey', HTTPCLIENT = 'httpclient', APPNAME = 'appName', SSL = 'ssl', SNAPSHOT = 'snapshot', CACHE_DIR = 'cacheDir', NAMESERVERADDR = 'nameServerAddr', SERVERADDR = 'serverAddr', UNIT = 'unit', REFRESH_INTERVAL = 'refreshInterval', CONTEXTPATH = 'contextPath', CLUSTER_NAME = 'clusterName', REQUEST_TIMEOUT = 'requestTimeout', HTTP_AGENT = 'httpAgent', SERVER_MGR = 'serverMgr', DEFAULT_ENCODING = 'defaultEncoding', } export interface IConfiguration { merge(config: any): IConfiguration; attach(config: any): IConfiguration; get(configKey?: ClientOptionKeys): any; has(configKey: ClientOptionKeys): boolean; set(configKey: ClientOptionKeys, target: any): IConfiguration; modify(configKey: ClientOptionKeys, changeHandler: (target: any) => any): IConfiguration; } export interface API_ROUTE { GET: string; BATCH_GET: string; BATCH_QUERY: string; PUBLISH: string; PUBLISH_ALL: string; REMOVE: string; REMOVE_ALL: string; LISTENER: string; }