From c53f08570dac263b0f0d1a5f985af5976b9ce806 Mon Sep 17 00:00:00 2001 From: Aex Date: Mon, 31 May 2021 23:34:51 +0800 Subject: [PATCH] Prettier format --- README.md | 22 +++++++----- package.json | 2 +- src/index.ts | 1 - src/lib/AliSlsBin.ts | 40 ++++++++++------------ src/lib/AliSlsQuery.ts | 66 +++++++++++++++--------------------- src/service/AliSlsService.ts | 19 ++++++----- src/test/demo.ts | 4 +-- src/typings.ts | 4 +-- tsconfig.json | 4 +-- 9 files changed, 76 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 83afc87..6f29dec 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,18 @@ [![npm downloads](https://img.shields.io/npm/dm/coa-ali-sls.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-ali-sls) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-ali-sls/pulls) -轻量的阿里云日志服务SDK for Node.js +轻量的阿里云日志服务 SDK for Node.js -来源于一个实际生产项目,将用到的API和业务解耦后封装成此库。 +来源于一个实际生产项目,将用到的 API 和业务解耦后封装成此库。 -后续会根据实际使用情况优化和扩充新的服务。如果急需用到其他接口,可以直接使用阿里云官方SDK [aliyun-sdk/sls](https://github.com/aliyun-UED/aliyun-sdk-js/tree/master/samples/sls) 或查看阿里云官方 [帮助文档](https://help.aliyun.com/document_detail/141789.html)。 +后续会根据实际使用情况优化和扩充新的服务。如果急需用到其他接口,可以直接使用阿里云官方 SDK [aliyun-sdk/sls](https://github.com/aliyun-UED/aliyun-sdk-js/tree/master/samples/sls) 或查看阿里云官方 [帮助文档](https://help.aliyun.com/document_detail/141789.html)。 ## 特征 - 覆盖了绝大多数使用场景 -- 相对于官方的SDK,无任何第三方依赖,更轻量简洁 +- 相对于官方的 SDK,无任何第三方依赖,更轻量简洁 - 统一了异步表现形式,全部返回 Promise -- 内置类型引用,无需额外查看文档,开箱即用,IDE友好 +- 内置类型引用,无需额外查看文档,开箱即用,IDE 友好 ## 快速开始 @@ -44,7 +44,8 @@ const config = { const service = new AliSlsService(config) // 定义查询的开始时间和结束时间 -const from = dayjs('2020-01-01'), to = dayjs('2020-01-02') +const from = dayjs('2020-01-01') +const to = dayjs('2020-01-02') // 查询指定 Project 下某个 Logstore 中的日志数据 service.getLogs('project-name', from, to) @@ -62,10 +63,13 @@ import { dayjs } from 'coa-helper' import { AliSlsQuery, AliSlsService } from 'coa-ali-sls' // 创建service实例 -const service = new AliSlsService({ /* ... */}) +const service = new AliSlsService({ + /* ... */ +}) // 定义查询的开始时间和结束时间 -const from = dayjs('2020-01-01'), to = dayjs('2020-01-02') +const from = dayjs('2020-01-01'), + to = dayjs('2020-01-02') // 定义查询条件(全部用法如下) const query = new AliSlsQuery() @@ -86,4 +90,4 @@ const query = new AliSlsQuery() // 按照查询条件查询指定 Project 下某个 Logstore 中的日志数据 await service.getLogs('project-name', from, to, { query }) -``` \ No newline at end of file +``` diff --git a/package.json b/package.json index 506c470..b62ee2d 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "scripts": { "dev": "tsc -w", "build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test", - "lint": "eslint . ", + "lint": "eslint .", "prettier": "prettier -w .", "sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-ali-sls?sync_upstream=true'" }, diff --git a/src/index.ts b/src/index.ts index 7047eb7..e0e6b4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,4 +2,3 @@ export { AliSlsBin } from './lib/AliSlsBin' export { AliSlsQuery } from './lib/AliSlsQuery' export { AliSlsService } from './service/AliSlsService' export { AliSls } from './typings' - diff --git a/src/lib/AliSlsBin.ts b/src/lib/AliSlsBin.ts index 8010a3a..2aedaa7 100644 --- a/src/lib/AliSlsBin.ts +++ b/src/lib/AliSlsBin.ts @@ -4,15 +4,13 @@ import { secure } from 'coa-secure' import { AliSls } from '../typings' export class AliSlsBin { - private readonly config: AliSls.Config - constructor (config: AliSls.Config) { + constructor(config: AliSls.Config) { this.config = config } - async get (url: string, params: AliSls.Dic) { - + async get(url: string, params: AliSls.Dic) { const path = url + '?' + this.getSortQueryString(params) const headers = this.getHeaders('GET', path) @@ -23,30 +21,30 @@ export class AliSlsBin { return this.resultResponse(result) } - async getBody (url: string, params: AliSls.Dic) { + async getBody(url: string, params: AliSls.Dic) { const { body } = await this.get(url, params) return body } - protected resultError (e: any) { + protected resultError(e: any) { const data = e.response.data die.hint(data.errorMessage, 400, data.errorCode) } - private resultResponse (result: any) { + private resultResponse(result: any) { const body = result.data as AliSls.Dic[] - const info = {} as AliSls.Dic + const info: AliSls.Dic = {} _.forEach(result.headers, (v, k) => { if (k.startsWith('x-log-')) info[_.camelCase(k.substr(6))] = v }) - _.forEach(body, v => { - delete v['__time__'] - delete v['__source__'] + _.forEach(body, (v) => { + delete v.__time__ + delete v.__source__ }) return { body, info } } - private getSortQueryString (params: AliSls.Dic) { + private getSortQueryString(params: AliSls.Dic) { const list = [] as string[] _.forEach(params, (v, k) => { list.push(k + '=' + v) @@ -54,25 +52,24 @@ export class AliSlsBin { return list.sort().join('&') } - private getHeaders (method: string, path: string, headers: AliSls.Dic = {}) { - + private getHeaders(method: string, path: string, headers: AliSls.Dic = {}) { _.defaults(headers, { 'x-log-bodyrawsize': '0', 'x-log-apiversion': '0.6.0', 'x-log-signaturemethod': 'hmac-sha1', }) - headers['Date'] = new Date().toUTCString() + headers.Date = new Date().toUTCString() headers['Content-MD5'] = '' headers['Content-Type'] = '' - headers['Authorization'] = this.signature(method, path, headers) + headers.Authorization = this.signature(method, path, headers) return headers } - private signature (method: string, path: string, headers: AliSls.Dic) { - - const x_list = [] as string[], signs = [] as string[] + private signature(method: string, path: string, headers: AliSls.Dic) { + const x_list = [] as string[] + const signs = [] as string[] _.forEach(headers, (v, k) => { k.startsWith('x-log') && x_list.push(k + ':' + v) @@ -81,7 +78,7 @@ export class AliSlsBin { signs.push(method || '') signs.push(headers['Content-Md5'] || '') signs.push(headers['Content-Type'] || '') - signs.push(headers['Date'] || '') + signs.push(headers.Date || '') signs.push(...x_list.sort()) signs.push(path) @@ -89,5 +86,4 @@ export class AliSlsBin { return `LOG ${this.config.accessKeyId}:${signature}` } - -} \ No newline at end of file +} diff --git a/src/lib/AliSlsQuery.ts b/src/lib/AliSlsQuery.ts index e693829..53b871c 100644 --- a/src/lib/AliSlsQuery.ts +++ b/src/lib/AliSlsQuery.ts @@ -4,14 +4,13 @@ import { _ } from 'coa-helper' // SLS 极简的SLS SQLBuilder export class AliSlsQuery { - - private _column = [] as string [] - private _where = [] as string[] - private _groupBy = [] as string[] - private _orderBy = [] as string[] + private readonly _column = [] as string[] + private readonly _where = [] as string[] + private readonly _groupBy = [] as string[] + private readonly _orderBy = [] as string[] private _limit = 0 as number - constructor () { + constructor() { return this } @@ -20,9 +19,8 @@ export class AliSlsQuery { * @param field 列名 * @param alias 输出别名 */ - column (field: string, alias?: string) { - if (alias) - field += ' as ' + alias + column(field: string, alias?: string) { + if (alias) field += ' as ' + alias this._column.push(field) return this } @@ -33,7 +31,7 @@ export class AliSlsQuery { * @param format 格式化 * @param alias 输出别名 */ - dateFormat (field: string, format: string, alias?: string) { + dateFormat(field: string, format: string, alias?: string) { return this.column(`date_format(${field},'${format}')`, alias) } @@ -42,7 +40,7 @@ export class AliSlsQuery { * @param field 列名 * @param alias 输出别名 */ - approxDistinct (field: string, alias?: string) { + approxDistinct(field: string, alias?: string) { const stmt = `approx_distinct(${field})` return this.column(stmt, alias) } @@ -55,7 +53,7 @@ export class AliSlsQuery { * @param field 列名 * @param alias 输出别名 */ - timeSeries (field = '__time__', alias: string, grainSize: string, format = '%Y-%m-%d %H:%i', padding = 0) { + timeSeries(field: string, alias: string, grainSize: string, format = '%Y-%m-%d %H:%i', padding = 0) { return this.column(`time_series(${field}, '${grainSize}', '${format}', '${padding}')`, alias) } @@ -65,7 +63,7 @@ export class AliSlsQuery { * @param value 值 * @param checkEmpty true进行空值检查,为空的不查询 false 不进行空值检查 */ - eq (field: string, value: string, checkEmpty = false) { + eq(field: string, value: string, checkEmpty = false) { return this.where(field, '=', value, true, checkEmpty) } @@ -75,7 +73,7 @@ export class AliSlsQuery { * @param value 值 * @param checkEmpty true进行空值检查,为空的不查询 false 不进行空值检查 */ - ne (field: string, value: string, checkEmpty = false) { + ne(field: string, value: string, checkEmpty = false) { return this.where(field, '<>', value, true, checkEmpty) } @@ -84,9 +82,8 @@ export class AliSlsQuery { * @param field 字段名 * @param values 字符串数组 */ - notIn (field: string, values: string[]) { - if (!values.length) - return this + notIn(field: string, values: string[]) { + if (!values.length) return this _.forEach(values, (value, i) => { values[i] = `'${value}'` @@ -101,7 +98,7 @@ export class AliSlsQuery { * @param likes 带%的数据库语句 * @param checkEmpty true进行空值检查,为空的不查询 false 不进行空值检查 */ - notLike (field: string, likes: string, checkEmpty = false) { + notLike(field: string, likes: string, checkEmpty = false) { return this.where(field, 'not like', likes, true, checkEmpty) } @@ -109,7 +106,7 @@ export class AliSlsQuery { * 根据字段分组 * @param fields 字段名 */ - groupBy (...fields: string[]) { + groupBy(...fields: string[]) { this._groupBy.push(...fields) return this } @@ -118,7 +115,7 @@ export class AliSlsQuery { * 根据字段排序 * @param fields 字段名 */ - orderBy (...fields: string[]) { + orderBy(...fields: string[]) { this._orderBy.push(...fields) return this } @@ -127,7 +124,7 @@ export class AliSlsQuery { * 获取前n条记录 * @param count 前n项记录 */ - limit (count: number) { + limit(count: number) { this._limit = count return this } @@ -135,25 +132,20 @@ export class AliSlsQuery { /** * 获取sql语句 */ - toQuery () { + toQuery() { let sql = '* | ' this._column.length || die.hint('缺少column') sql += 'select ' + this._column.join(', ') - if (this._where.length) - sql += ' where ' + this._where.join(' and ') + if (this._where.length) sql += ' where ' + this._where.join(' and ') - if (this._groupBy.length) - sql += ' group by ' + this._groupBy.join(',') + if (this._groupBy.length) sql += ' group by ' + this._groupBy.join(',') - if (this._orderBy.length) - sql += ' order by ' + this._orderBy.join(',') + if (this._orderBy.length) sql += ' order by ' + this._orderBy.join(',') - if (this._limit > 0) - sql += ' limit ' + this._limit - else - echo.warn('SLS查询的结果行数默认为') + if (this._limit > 0) sql += ' limit ' + this._limit + else echo.warn('SLS查询的结果行数默认为') return sql } @@ -162,7 +154,7 @@ export class AliSlsQuery { * @param field 字段名 * @param alias 输出别名 */ - count (field = '*', alias?: string) { + count(field = '*', alias?: string) { return this.column(`count(${field})`, alias) } @@ -174,13 +166,11 @@ export class AliSlsQuery { * @param wrapValue 是否把值当作字符串处理 是:在查询值两边添加单引号 否:不添加单引号 * @param checkEmpty true进行空值检查,false不进行空值检查 */ - private where (field: string, operate: string, value: string, wrapValue: boolean, checkEmpty: boolean) { + private where(field: string, operate: string, value: string, wrapValue: boolean, checkEmpty: boolean) { // 当开启空值检查 并且当前值为空的时候跳过该值 - if (checkEmpty && !value) - return this + if (checkEmpty && !value) return this const str = wrapValue ? `"${field}" ${operate} '${value}'` : `"${field}" ${operate} ${value}` this._where.push(str) return this } - -} \ No newline at end of file +} diff --git a/src/service/AliSlsService.ts b/src/service/AliSlsService.ts index 055affb..8881f26 100644 --- a/src/service/AliSlsService.ts +++ b/src/service/AliSlsService.ts @@ -4,17 +4,21 @@ import { AliSlsBin } from '../lib/AliSlsBin' import { AliSls } from '../typings' export class AliSlsService { - private readonly config: AliSls.Config private readonly bin: AliSlsBin - constructor (config: AliSls.Config) { + constructor(config: AliSls.Config) { this.config = config this.bin = new AliSlsBin(config) } // GetLogs 接口查询指定 Project 下某个 Logstore 中的日志数据 - async getLogsBody> (name: string, from: number, to: number, param?: { query?: string, topic?: string, offset?: number, line?: number, reverse?: boolean }) { + async getLogsBody>( + name: string, + from: number, + to: number, + param?: { query?: string; topic?: string; offset?: number; line?: number; reverse?: boolean } + ) { const params = { type: 'log', from: _.toInteger(from / 1000), @@ -29,7 +33,7 @@ export class AliSlsService { } // GetLogs 接口查询指定 Project 下某个 Logstore 中的日志数据 - async getLogs (name: string, from: Dayjs, to: Dayjs, param?: { query?: string, topic?: string, offset?: number, line?: number, reverse?: boolean }) { + async getLogs(name: string, from: Dayjs, to: Dayjs, param?: { query?: string; topic?: string; offset?: number; line?: number; reverse?: boolean }) { const params = { type: 'log', from: from.unix(), @@ -41,16 +45,15 @@ export class AliSlsService { } // 查询指定 Project 下某个 Logstore 中日志的分布情况 - async getHistograms (name: string, from: Dayjs, to: Dayjs, query?: string, topic?: string) { + async getHistograms(name: string, from: Dayjs, to: Dayjs, query?: string, topic?: string) { const params = { type: 'histogram', from: from.unix(), to: to.unix(), query, - topic + topic, } const url = `/logstores/${name}` return await this.bin.get(url, params) } - -} \ No newline at end of file +} diff --git a/src/test/demo.ts b/src/test/demo.ts index 4854c08..3d46dda 100644 --- a/src/test/demo.ts +++ b/src/test/demo.ts @@ -14,7 +14,8 @@ const config = { const service = new AliSlsService(config) // 定义查询的开始时间和结束时间 -const from = dayjs('2020-01-01'), to = dayjs('2020-01-02') +const from = dayjs('2020-01-01') +const to = dayjs('2020-01-02') // 查询指定 Project 下某个 Logstore 中的日志数据 await service.getLogs('project-name', from, to) @@ -22,7 +23,6 @@ await service.getLogs('project-name', from, to) // 查询指定 Project 下某个 Logstore 中日志的分布情况 await service.getHistograms('project-name', from, to) - // 定义查询条件(全部用法如下) const query = new AliSlsQuery() .dateFormat('__time__', '%m-%d %H:%i:%s', 'time') diff --git a/src/typings.ts b/src/typings.ts index 504440c..3056186 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -8,6 +8,6 @@ const config = { export declare namespace AliSls { type Config = typeof config interface Dic { - [key: string]: any + [key: string]: T } -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index ce34ce5..8a42583 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,5 @@ "outDir": "dist", "declaration": true }, - "include": [ - "src" - ] + "include": ["src"] }