From beb9bdb9aeb40515e75b9705a03f74106171cd56 Mon Sep 17 00:00:00 2001 From: Wayne Liang Date: Tue, 6 Feb 2024 13:50:04 +0800 Subject: [PATCH] style: update eslint config --- README.md | 9 +++- eslint.config.js | 21 ++++---- lib/esm/alimt/index.js | 80 +++++++++++++++--------------- lib/esm/types/alimt/index.d.ts.map | 2 +- readme.zh-CN.md | 9 +++- src/alimt/index.ts | 7 ++- 6 files changed, 73 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 0ed5a8d..4c76931 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ -# Translate Hub +

+ Translate Hub +

+
Utilize the translation API or AI capabilities on the cloud platform, combined with the Alfred workflow, to achieve rapid translation and enhance your work efficiency. +[![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config) [![Alfy](https://img.shields.io/badge/Alfy-Sindre%20Sorhus-blue?color=black)](https://github.com/sindresorhus/alfy) + +
+ **English** | [中文](./README.zh-CN.md) ## Installation diff --git a/eslint.config.js b/eslint.config.js index c8f3ac2..b146a7e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,12 +1,13 @@ import antfu from '@antfu/eslint-config' -export default antfu({ - typescript: true, - ignores: [ - '*.sh', - 'node_modules', - '.vscode', - 'lib/**/*', - 'public', - ], -}) +export default antfu( + { + typescript: true, + ignores: ['*.sh', 'node_modules', '.vscode', 'lib', 'public'] + }, + { + rules: { + 'style/comma-dangle': ['error', 'never'] + } + } +) diff --git a/lib/esm/alimt/index.js b/lib/esm/alimt/index.js index 25eba51..cb4d75c 100644 --- a/lib/esm/alimt/index.js +++ b/lib/esm/alimt/index.js @@ -1,9 +1,9 @@ -import { env } from 'node:process'; -import Client, { TranslateGeneralRequest } from '@alicloud/alimt20181012'; -import * as $OpenApi from '@alicloud/openapi-client'; -import * as $Util from '@alicloud/tea-util'; -import alfy from 'alfy'; -let client; +import { env } from 'node:process' +import Client, { TranslateGeneralRequest } from '@alicloud/alimt20181012' +import * as $OpenApi from '@alicloud/openapi-client' +import * as $Util from '@alicloud/tea-util' +import alfy from 'alfy' +let client /** * use AK&SK create Client * @param accessKeyId @@ -12,41 +12,41 @@ let client; * @throws Exception */ function createClient(accessKeyId, accessKeySecret) { - const config = new $OpenApi.Config({ - // required, your AccessKey ID - accessKeyId, - // required, your AccessKey Secret - accessKeySecret - }); - // Endpoint please reference https://api.aliyun.com/product/alimt - config.endpoint = `mt.aliyuncs.com`; - return new Client.default(config); + const config = new $OpenApi.Config({ + // required, your AccessKey ID + accessKeyId, + // required, your AccessKey Secret + accessKeySecret + }) + // Endpoint please reference https://api.aliyun.com/product/alimt + config.endpoint = `mt.aliyuncs.com` + const ClientDefault = Client.default + return new ClientDefault(config) } export async function doTranslate(params) { - if (!client) { - if (!env.ALIBABA_CLOUD_ACCESS_KEY_ID || !env.ALIBABA_CLOUD_ACCESS_KEY_SECRET) { - alfy.log('Please set up your access key id and access key secret'); - return; - } - // Please ensure that the environment variable is set for the code running environment ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - // Engineering code leakage may lead to AccessKey leakage and threaten the security of all resources under the account. The following code example uses the environment variable to obtain the AccessKey. It is for reference only. It is recommended to use the more secure STS method. For more authentication access methods, see:https://help.aliyun.com/document_detail/378664.html - client = createClient(env.ALIBABA_CLOUD_ACCESS_KEY_ID, env.ALIBABA_CLOUD_ACCESS_KEY_SECRET); - } - const translateGeneralRequest = new TranslateGeneralRequest({}); - translateGeneralRequest.formatType = 'text'; - translateGeneralRequest.sourceLanguage = params.sourceLanguage || 'auto'; - translateGeneralRequest.targetLanguage = params.targetLanguage; - translateGeneralRequest.sourceText = params.sourceText; - translateGeneralRequest.scene = 'general'; - const runtime = new $Util.RuntimeOptions({}); - try { - const res = await client?.translateGeneralWithOptions(translateGeneralRequest, runtime); - return res; - } - catch (error) { - // error message - console.error(error.message); - // Diagnostic Address - console.error(error.data.Recommend); + if (!client) { + if (!env.ALIBABA_CLOUD_ACCESS_KEY_ID || !env.ALIBABA_CLOUD_ACCESS_KEY_SECRET) { + alfy.log('Please set up your access key id and access key secret') + return } + // Please ensure that the environment variable is set for the code running environment ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + // Engineering code leakage may lead to AccessKey leakage and threaten the security of all resources under the account. The following code example uses the environment variable to obtain the AccessKey. It is for reference only. It is recommended to use the more secure STS method. For more authentication access methods, see:https://help.aliyun.com/document_detail/378664.html + client = createClient(env.ALIBABA_CLOUD_ACCESS_KEY_ID, env.ALIBABA_CLOUD_ACCESS_KEY_SECRET) + } + const translateGeneralRequest = new TranslateGeneralRequest({}) + translateGeneralRequest.formatType = 'text' + translateGeneralRequest.sourceLanguage = params.sourceLanguage || 'auto' + translateGeneralRequest.targetLanguage = params.targetLanguage + translateGeneralRequest.sourceText = params.sourceText + translateGeneralRequest.scene = 'general' + const runtime = new $Util.RuntimeOptions({}) + try { + const res = await client?.translateGeneralWithOptions(translateGeneralRequest, runtime) + return res + } catch (error) { + // error message + console.error(error.message) + // Diagnostic Address + console.error(error.data.Recommend) + } } diff --git a/lib/esm/types/alimt/index.d.ts.map b/lib/esm/types/alimt/index.d.ts.map index bbed736..ad68992 100644 --- a/lib/esm/types/alimt/index.d.ts.map +++ b/lib/esm/types/alimt/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/alimt/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,uBAAuB,IAAI,2BAA2B,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAA;AA2B/H,MAAM,MAAM,WAAW,GAAG,IAAI,CAAC,2BAA2B,EAAE,gBAAgB,GAAG,YAAY,GAAG,gBAAgB,CAAC,CAAA;AAC/G,wBAAsB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC,CA2B/F"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/alimt/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,uBAAuB,IAAI,2BAA2B,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAA;AA2B/H,MAAM,MAAM,WAAW,GAAG,IAAI,CAAC,2BAA2B,EAAE,gBAAgB,GAAG,YAAY,GAAG,gBAAgB,CAAC,CAAA;AAC/G,wBAAsB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC,CA4B/F"} \ No newline at end of file diff --git a/readme.zh-CN.md b/readme.zh-CN.md index 3d40870..95ab6c0 100644 --- a/readme.zh-CN.md +++ b/readme.zh-CN.md @@ -1,7 +1,14 @@ -# Translate Hub +

+Translate Hub +

+
利用云平台上的翻译 API 或 AI 功能,结合 Alfred 工作流程,实现快速翻译,提升您的工作效率。 +[![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config) [![Alfy](https://img.shields.io/badge/Alfy-Sindre%20Sorhus-blue?color=black)](https://github.com/sindresorhus/alfy) + +
+ [English](./README.md) | **中文** ## 安装 diff --git a/src/alimt/index.ts b/src/alimt/index.ts index 8f2b472..75646bc 100644 --- a/src/alimt/index.ts +++ b/src/alimt/index.ts @@ -1,6 +1,9 @@ import { env } from 'node:process' import Client, { TranslateGeneralRequest } from '@alicloud/alimt20181012' -import type { TranslateGeneralRequest as TranslateGeneralRequestType, TranslateGeneralResponse } from '@alicloud/alimt20181012' +import type { + TranslateGeneralRequest as TranslateGeneralRequestType, + TranslateGeneralResponse +} from '@alicloud/alimt20181012' import * as $OpenApi from '@alicloud/openapi-client' import * as $Util from '@alicloud/tea-util' import alfy from 'alfy' @@ -19,7 +22,7 @@ function createClient(accessKeyId: string, accessKeySecret: string) { // required, your AccessKey ID accessKeyId, // required, your AccessKey Secret - accessKeySecret, + accessKeySecret }) // Endpoint please reference https://api.aliyun.com/product/alimt config.endpoint = `mt.aliyuncs.com`