@dalongrong/nacos-config
Version:
nacos config client
285 lines (284 loc) • 9.33 kB
TypeScript
/**
* 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.
*/
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<string>} currentUnit
*/
getCurrentUnit(): Promise<string>;
/**
* @description 获取所有单元信息
*/
getAllUnits(): Promise<string[]>;
/**
* 将配置发布到所有单元
* @param {String} dataId - id of the data
* @param {String} group - group name of the data
* @param {String} content - config value
* @returns {Promise<boolean>} true | false
*/
publishToAllUnit(dataId: string, group: string, content: string): Promise<boolean>;
/**
* @description 将配置从所有单元中删除
* @param {String} dataId - id of the data
* @param {String} group - group name of the data
* @returns {Promise<boolean>} true | false
*/
removeToAllUnit(dataId: string, group: string): Promise<boolean>;
}
/**
* 每个 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<string>} value
*/
getConfig(dataId: string, group: string, options?: UnitOptions): Promise<string>;
/**
* @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<boolean>} true | false
*/
publishSingle(dataId: string, group: string, content: string, options?: UnitOptions): Promise<boolean>;
/**
* @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<boolean>} true | false
*/
remove(dataId: string, group: string, options?: UnitOptions): Promise<boolean>;
/**
* @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<boolean>} true | false
*/
publishAggr(dataId: string, group: string, datumId: string, content: string, options?: UnitOptions): Promise<boolean>;
/**
* @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<boolean>} true | false
*/
removeAggr(dataId: string, group: string, datumId: string, options?: UnitOptions): Promise<boolean>;
/**
* @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<object>} result
*/
batchGetConfig(dataIds: string[], group: string, options?: UnitOptions): Promise<object>;
/**
* @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<object>} result
*/
batchQuery(dataIds: string[], group: string, options?: UnitOptions): Promise<object>;
/**
* @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): any;
/**
* @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): any;
/**
* @description 查询租户下的所有的配置
*/
getConfigs(): Promise<Array<string>>;
/**
* @description close connection
*/
close(): void;
on?(evt: string, fn: (err: Error) => void): void;
}
/**
* 服务列表管理器
*/
export interface IServerListManager {
/**
* 获取当前单元
*/
getCurrentUnit(): Promise<string>;
/**
* 获取单元列表
*/
fetchUnitLists(): Promise<Array<string>>;
/**
* 更新当前服务器
*/
updateCurrentServer(unit?: string): Promise<void>;
/**
* 获取一个服务器地址
* @param unit
*/
getCurrentServerAddr(unit?: string): Promise<string>;
/**
* @description close connection
*/
close(): any;
}
export interface ISnapshot {
cacheDir: any;
get(key: string): any;
save(key: string, value: any): any;
delete(key: string): any;
batchSave(arr: Array<SnapShotData>): any;
}
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;
serverPort?: number;
namespace?: string;
accessKey?: string;
secretKey?: string;
httpclient?: any;
httpAgent?: any;
appName?: string;
ssl?: boolean;
refreshInterval?: number;
contextPath?: string;
clusterName?: string;
requestTimeout?: number;
defaultEncoding?: string;
serverAddr?: string;
unit?: string;
nameServerAddr?: string;
username?: string;
password?: string;
cacheDir?: string;
identityKey?: string;
identityValue?: string;
endpointQueryParams?: string;
decodeRes?: (res: any, method?: string, encoding?: string) => any;
}
export declare 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",
IDENTITY_KEY = "identityKey",
IDENTITY_VALUE = "identityValue",
DECODE_RES = "decodeRes",
ENDPOINT_QUERY_PARAMS = "endpointQueryParams"
}
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;
LOGIN: string;
REMOVE_ALL: string;
LISTENER: string;
}
export {};