From 98942d96cf53652adf9be965a78e8158d77499ab Mon Sep 17 00:00:00 2001 From: renxia Date: Thu, 18 Jan 2024 11:17:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E7=BB=84=E4=BB=B6=E7=9A=84=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/index.ts | 3 +-- src/common/{ => lib}/LRUCache.ts | 0 src/common/{ => lib}/Logger.ts | 8 ++++---- src/common/{ => lib}/ReqFetch.ts | 17 +++++++++++++---- src/common/lib/index.ts | 3 +++ src/node/get-logger.ts | 2 +- src/node/lib/NLogger.ts | 2 +- src/node/lib/WXWork.ts | 10 ++++------ src/node/lib/request.ts | 2 +- 9 files changed, 28 insertions(+), 19 deletions(-) rename src/common/{ => lib}/LRUCache.ts (100%) rename src/common/{ => lib}/Logger.ts (97%) rename src/common/{ => lib}/ReqFetch.ts (89%) create mode 100644 src/common/lib/index.ts diff --git a/src/common/index.ts b/src/common/index.ts index c2ea622..624296a 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -1,10 +1,9 @@ +export * from './lib'; export * from './async'; export * from './cookie'; export * from './date'; export * from './helper'; export * from './is'; -export * from './Logger'; export * from './objects'; -export * from './ReqFetch'; export * from './semver'; export * from './uuid'; diff --git a/src/common/LRUCache.ts b/src/common/lib/LRUCache.ts similarity index 100% rename from src/common/LRUCache.ts rename to src/common/lib/LRUCache.ts diff --git a/src/common/Logger.ts b/src/common/lib/Logger.ts similarity index 97% rename from src/common/Logger.ts rename to src/common/lib/Logger.ts index 164f6a5..4e7b37d 100644 --- a/src/common/Logger.ts +++ b/src/common/lib/Logger.ts @@ -1,14 +1,14 @@ /* * @Author: lzw * @Date: 2022-04-08 10:30:02 - * @LastEditors: lzw - * @LastEditTime: 2022-11-15 16:16:45 + * @LastEditors: renxia + * @LastEditTime: 2024-01-18 11:05:29 * @Description: */ /* eslint no-console: 0 */ -import { type GeneralFn } from '../types'; -import { safeStringify } from './objects'; +import { type GeneralFn } from '../../types'; +import { safeStringify } from '../objects'; /** 日志级别 */ export enum LogLevel { diff --git a/src/common/ReqFetch.ts b/src/common/lib/ReqFetch.ts similarity index 89% rename from src/common/ReqFetch.ts rename to src/common/lib/ReqFetch.ts index 6436f8b..5f382fc 100644 --- a/src/common/ReqFetch.ts +++ b/src/common/lib/ReqFetch.ts @@ -1,8 +1,15 @@ +/* + * @Author: renxia + * @Date: 2024-01-15 11:26:52 + * @LastEditors: renxia + * @LastEditTime: 2024-01-18 11:15:16 + * @Description: + */ import type { OutgoingHttpHeaders } from 'node:http'; -import { urlFormat } from './url'; -import { toLowcaseKeyObject } from './objects'; -import type { AnyObject } from '../types'; -import { cookieParse, cookieStringfiy } from './cookie'; +import { urlFormat } from '../url'; +import { toLowcaseKeyObject } from '../objects'; +import type { AnyObject } from '../../types'; +import { cookieParse, cookieStringfiy } from '../cookie'; interface ReqOptions extends Omit { headers?: OutgoingHttpHeaders; @@ -93,3 +100,5 @@ export class ReqFetch extends ReqBase { return this.request('POST', url, parameters, { ...options, headers: { ...options?.headers, ...headers } }); } } + +// new ReqFetch().get('https://www.baidu.com').then(d => console.log(d.response.status, d.data.length)); diff --git a/src/common/lib/index.ts b/src/common/lib/index.ts new file mode 100644 index 0000000..e9c955a --- /dev/null +++ b/src/common/lib/index.ts @@ -0,0 +1,3 @@ +export * from './LRUCache'; +export * from './Logger'; +export * from './ReqFetch'; diff --git a/src/node/get-logger.ts b/src/node/get-logger.ts index e4cf816..40030b9 100644 --- a/src/node/get-logger.ts +++ b/src/node/get-logger.ts @@ -1,6 +1,6 @@ import { color } from 'console-log-colors'; import { NLogger } from './lib/NLogger'; -import { type LogLevelType } from '../common/Logger'; +import { type LogLevelType } from '../common/lib/Logger'; export function getLogger(tag = '[FEUTILS]', levelType?: LogLevelType): NLogger { return NLogger.getLogger(tag, { levelType, color }); diff --git a/src/node/lib/NLogger.ts b/src/node/lib/NLogger.ts index f4cc7b1..a5b1420 100644 --- a/src/node/lib/NLogger.ts +++ b/src/node/lib/NLogger.ts @@ -11,7 +11,7 @@ import type { WriteStream } from 'node:fs'; import { clearScreenDown, cursorTo } from 'node:readline'; import { color } from 'console-log-colors'; import { fs } from '../fs-system'; -import { Logger, type LoggerOptions } from '../../common/Logger'; +import { Logger, type LoggerOptions } from '../../common/lib/Logger'; const fsStreamCache: { [logPath: string]: WriteStream } = {}; diff --git a/src/node/lib/WXWork.ts b/src/node/lib/WXWork.ts index 9bfba74..25696a7 100644 --- a/src/node/lib/WXWork.ts +++ b/src/node/lib/WXWork.ts @@ -2,16 +2,13 @@ /* * @Author: lzw * @Date: 2021-12-24 13:01:39 - * @LastEditors: lzw - * @LastEditTime: 2022-07-26 21:01:43 + * @LastEditors: renxia + * @LastEditTime: 2024-01-18 10:59:55 * @Description: 企业微信机器人通知 */ import { Request } from './request'; -const api = new Request(); -const webhook = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='; - export interface WxWorkReqParams { msgtype: string; text?: { @@ -130,6 +127,7 @@ export function wxWorkNotify(params: string | WxWorkReqParams, webhookUrl: strin return Promise.resolve({ errcode: -2, errmsg: '[wxWorkNotify][webhook]invalid format' }); } + const webhook = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='; if (!webhookUrl.startsWith('http')) webhookUrl = webhook + webhookUrl; if (typeof params === 'string') { @@ -141,7 +139,7 @@ export function wxWorkNotify(params: string | WxWorkReqParams, webhookUrl: strin }; } - return api.post(webhookUrl, params, { 'content-type': 'application/json' }).then(d => { + return new Request().post(webhookUrl, params, { 'content-type': 'application/json' }).then(d => { if (debug) console.log(`[wxWorkNotify][${d.data.errcode}]`, JSON.stringify(d.data)); return d.data; }); diff --git a/src/node/lib/request.ts b/src/node/lib/request.ts index b1e197d..14722c6 100644 --- a/src/node/lib/request.ts +++ b/src/node/lib/request.ts @@ -3,7 +3,7 @@ import zlib from 'node:zlib'; import http, { type IncomingMessage, type IncomingHttpHeaders, type OutgoingHttpHeaders } from 'node:http'; import https, { type RequestOptions } from 'node:https'; import { urlFormat } from '../../common/url'; -import { ReqBase } from '../../common/ReqFetch'; +import { ReqBase } from '../../common/lib/ReqFetch'; import type { AnyObject } from '../../types'; export class Request extends ReqBase {