组件 - 通用图书馆

目录
说明
这个库以简单,可再用的方式提供一些最常用的组件开发功能.
要安装, 类型
npm install @elastic.io/component-commons-library
可用函数
API 客户端
一些平台API客户端类可供使用并扩展,为平台API创建客户端.
平台ApiRest客户端
平台ApiRest客户端 类扩展 BasicAuthRest 客户端 课. 此类中检查状态代码 200 的方法如果没有,则丢出一个错误. 并检查该响应是否带有JSON格式的正确数据和其他预期响应头.
构造器(emiters, cfg)
- 排放物 -- -- EIO排放环境.
- cfg - EIO 组件对象的配置.
const Client = new PlatformApiRestClient(emitter, cfg);
平台ApiLogic 客户端
平台ApiLogic 客户端 类扩展 平台ApiRest客户端 课. 包含用于操纵流量状态的有用方法, 以设置为活动运行或停止活动, 搜索流量, 工作空间, 证书等等 .
构造器(emiters, cfg)
- 排放物 -- -- EIO排放环境.
- cfg - EIO 组件对象的配置.
const Client = new PlatformApiLogicClient(emitter, cfg);
方法清单
- 获取 AllFlows ForWorkspace( 选项) - 获取给定工作空间的所有流程
- 获取所有证书用于工作空间( 选项) - 获取给定工作空间的所有证书
- 获取 AllSecrets ForWorkspace - 为给定的工作空间获取全部机密
- 获取SecretById - 通过 ID 获取特定工作空间的秘密
- 刷新 TokenBySecretId - 通过给定的工作空间的秘密代号刷新令牌
- 从合同( 选项) 获取组件可访问性 - 从给定的工作空间获取所有可访问组件
- 分解Parallelization( maxParallelization, 分解Factor) - 帮助计算并行呼叫的正确次数的方法
- 获取 FlowList( 选项) - 获取流量列表
- 获取 WorkspaceList( 选项) - 为用户获取所有合同中的所有工作空间列表
- 获取 WorkspaceId( Workspace UniqueCriteria) - 鉴于一组独特的标准,请找到匹配的工作空间
- 删除 NonWritable Properties( flow, 包括DataSamples) - 根据流量, 移除系统经常更改的流量属性, 如上次执行的时间
- 获取FlowId( flowId (flow UniqueCriteria) ) - 以它独特的标准获取流量
- 获取 FlowById( id) - 按其代码获取流量
- 获取 FlowByName And WorkspaceId( flowName, workspaceId) - 按流程名称和工作空间 ID 获取流量
- 更改 Flow state( 选项) - 给定流量, 将流量改为给定状态( 运行、 停止等)
- 启动 Flow( flowId, 选项) - 设置流到活动运行状态
- stop Flow( flowId, 选项) - 将流量设置为不活动已停止状态
- 水合物花(options) - 使用去除不可写入的Properties方法将流量水化,但会用所有数据样本,证书名称,命令和组件 Id 字段来进一步丰富流量.
JSON Schema 转换器
包含 JSON 元数据生成工具
convertJsonSchemaToEioSchema(keyToReturn, completeSchemaOriginal)- 将通用的JSON schema转换为平台友好的JSON元数据makeSchemaInline(json, availableSchemas)- 替换 (单位:美元) 递归, 提供完整的对象描述 乔森 对象使用 可用计划 计划图.
JSON 转换
包含转换包含 JSONata 表达式的平台数据的函数
jsonataTransform(msg, cfg)- 返回msg.body将 JSONata 表达式评价为数值
附件处理器
附件处理器功能可用于将附件存储在平台上. 它暴露了以下功能
uploadAttachment(getAttachment, retryOptions, contentType),它将把附件上传到平台Maester存储并返回结果对象. 地点:
getAttachment- 返回流的同步函数
retryOptions- (可选):如果请求失败,用于重试上传附件的参数。 参数为:retriesCount财务报告和审定财务报表requestTimeout(单位:百万美元)contentType- (可选):Content-Type被附着。 默认Content-Type将自动计算.getAttachment(url, contentType),将检索附件steward或者说maester存储。 指定存储 - 查询参数storage_type必须提供。 要从maester存储 -?storage_type=maester应添加到url参数。 默认附件从steward存储,所以?storage_type=steward没有义务添加到url参数.contentType- 说吧 [stream, (中文(简体) ),arraybuffer[ . ]
示例
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
const getAttachAsStream = async () => (
await axios.get('http://sample.pdf', { responseType: 'stream' })
).data;
const result = await new AttachmentProcessor().uploadAttachment(getAttachAsStream, 'application/pdf');
const { objectId } = result.data;
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
const result = await new AttachmentProcessor().getAttachment('http://example.com', 'stream'); // steward storage
const result = await new AttachmentProcessor().getAttachment('http://example.com?storage_type=steward', 'arraybuffer'); // steward storage
const result = await new AttachmentProcessor().getAttachment('http://example.com?storage_type=maester', 'stream'); // maester storage
外部 API
环境变量
- API RETRIES COUNT (英语) (默认为 3): 5xx 错误的最大重试量 。 如果服务器仍然响应 5xx, 错误会被丢出 .
- 请求执行 (默认为 15000, min: 500, 最大为 120000) : 在请求时间到期前指定毫秒数 。 如果请求需要超过超时,请求将被中止.
axiosReqWithRetryOnServerError(与.call()来通过上下文,执行它作为一种分类方法logger财务报告和审定财务报表cfg(当前动作的配置对象值) - 构造器中的值 -- -- 函数通过指定的请求-配置来进行轴请求,使记录和错误处理:- 如果发生 5xx 错误, 将进行最大重试
API_RETRIES_COUNT每次重试都会被延迟exponentialSleep函数. - 如果发生 4xx 错误 - 错误会被丢弃 .
- 如果动作
cfg已经doNotThrow404设置为真: 404 个错误不会被当作错误处理.
从下面的例子来看.
- 如果发生 5xx 错误, 将进行最大重试
getErrMsg- 从轴响应中形成错误消息.getRetryOptions- 返回嵌入的有效值API_RETRIES_COUNT财务报告和审定财务报表API_REQUEST_TIMEOUT。如果数值高于或低于极限,则会被默认值所覆盖.sleep- 返回承诺,在N时间之后解决.exponentialDelay- 根据当前重试返回毫秒数。 见 指数后退 为了解释.exponentialSleep- 返回承诺,在N时间之后解决。 以毫秒数表示的 NexponentialDelay执行.
示例 axiosReqWithRetryOnServerError 函数 :
class Client {
private logger: any;
private cfg: any;
constructor(emitter, cfg) {
this.logger = emitter.logger;
this.cfg = cfg;
}
public async apiRequest(options: AxiosRequestConfig): Promise<any> {
try {
const response = await axiosReq.axiosReqWithRetryOnServerError(this, requestOptions);
return response.data;
} catch (error) {
if (error.response?.status === 401) {
// update token
}
throw error;
}
}
public async getUserById(id) {
return this.apiRequest({
url: `/users/${id}`,
method: 'GET',
});
}
}
锁定器
在logger所建的以Bunyan Logger作为其基础执行. 可找到可用的日志方法 这里。 。 。 .
示例
const { Logger } = require('@elastic.io/component-commons-library');
const logger = Logger.getLogger();
logger.info('Hello, world');
这个 getLogger() 方法使用可选参数 loggerName 这样你就可以宣布多种不同的伐木者.
许可证
阿帕奇-2.0 © 弹性.io GmbH