Skip to content

Commit f65cf49

Browse files
committed
feat: support sub-deepl
1 parent 9056cbb commit f65cf49

File tree

8 files changed

+68
-41
lines changed

8 files changed

+68
-41
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"scripts": {
1010
"clean": "rimraf build && rimraf release",
1111
"build": "run-s clean && cross-env NODE_ENV=production rollup -c rollup.config.js",
12-
"bundle": "node scripts/bundle.js",
13-
"bundle:watch": "nodemon --watch build scripts/bundle.js",
12+
"bundle": "cross-env NODE_ENV=production node scripts/bundle.js",
13+
"bundle:watch": "cross-env NODE_ENV=development nodemon --watch build scripts/bundle.js",
1414
"make-release": "run-s build bundle update-appcast",
1515
"dev": "run-s clean && cross-env NODE_ENV=development rollup -c rollup.config.js --watch",
1616
"type-check": "tsc --noEmit",
@@ -25,6 +25,7 @@
2525
"@rollup/plugin-commonjs": "^16.0.0",
2626
"@rollup/plugin-json": "^4.1.0",
2727
"@rollup/plugin-node-resolve": "^10.0.0",
28+
"@rollup/plugin-replace": "^2.3.4",
2829
"@rollup/plugin-typescript": "^6.1.0",
2930
"@types/adm-zip": "^0.4.33",
3031
"@types/jsonfile": "^6.0.0",

rollup.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import commonjs from '@rollup/plugin-commonjs'
55
import resolve from '@rollup/plugin-node-resolve'
66
import nodePolyfills from 'rollup-plugin-node-polyfills'
77
import typescript from '@rollup/plugin-typescript'
8+
import replace from '@rollup/plugin-replace'
89

910
export default {
1011
input: join(__dirname, './src/main.ts'),
@@ -28,6 +29,11 @@ export default {
2829
commonjs({
2930
include: ['node_modules/**'],
3031
}),
32+
replace({
33+
'process.env.NODE_ENV': JSON.stringify(
34+
process.env.NODE_ENV || 'development',
35+
),
36+
}),
3137
nodePolyfills(),
3238
typescript({}),
3339
],

scripts/bundle.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function generateInfo() {
3535
title: 'Sub DeepL',
3636
value: 'sub-deepl',
3737
},
38+
...(process.env.NODE_ENV === 'development'
39+
? [
40+
{
41+
title: 'Local',
42+
value: 'local',
43+
},
44+
]
45+
: null),
3846
],
3947
},
4048
{

src/api.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
import { RequestCallbackResponse, RequestObject } from '../types/global'
22

33
export class Api {
4-
constructor(private provider: 'deepl' | 'sub-deepl', private token: string) {}
4+
constructor(
5+
private provider: 'deepl' | 'sub-deepl' | 'local',
6+
private token: string,
7+
) {}
58

69
private get baseUrl(): string {
710
switch (this.provider) {
811
case 'deepl':
912
return 'https://api.deepl.com'
1013
case 'sub-deepl':
1114
return 'https://sub-deepl-api.nerdynerd.org'
15+
case 'local':
16+
return 'http://localhost:1337'
1217
}
1318
}
1419

1520
async request<T = Record<string, unknown>>(
1621
requestObject: Omit<RequestObject, 'handler' | 'header'>,
1722
): Promise<RequestCallbackResponse<T>> {
1823
try {
24+
const body: Record<string, any> = {
25+
...requestObject.body,
26+
auth_key: this.token,
27+
}
28+
let url = `${this.baseUrl}${requestObject.url}`
29+
30+
if (this.provider !== 'deepl') {
31+
if (['GET', 'HEAD', 'DELETE'].includes(requestObject.method)) {
32+
body.token = body.auth_key
33+
} else {
34+
url = `${url}?token=${this.token}`
35+
}
36+
}
37+
1938
return await $http.request({
2039
...requestObject,
21-
url: `${this.baseUrl}${requestObject.url}`,
40+
url,
2241
header: {
2342
'Content-Type': 'application/x-www-form-urlencoded',
2443
},
25-
body: {
26-
...requestObject.body,
27-
auth_key: this.token,
28-
},
44+
body,
2945
})
3046
} catch (e) {
3147
Object.assign(e, {
3248
_type: 'network',
33-
_message: '接口请求错误',
49+
_message: '接口请求错误 ' + e.message,
3450
})
3551

3652
throw e

src/main.ts

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function translate(
3535
text: string
3636
}>
3737
}>({
38-
method: 'GET',
38+
method: 'POST',
3939
url: '/v2/translate',
4040
body: {
4141
text: query.text,
@@ -47,36 +47,24 @@ export function translate(
4747
})
4848

4949
if (response.error) {
50-
$log.error(response.error)
51-
completion({
52-
error: {
53-
type: 'api',
54-
message: '接口请求错误',
55-
},
56-
})
57-
} else {
5850
const { statusCode } = response.response
5951

60-
if (statusCode > 299) {
61-
let reason: ErrorType
52+
let reason: ErrorType
6253

63-
if (statusCode >= 400 && statusCode < 500) {
64-
reason = 'param'
65-
} else {
66-
reason = 'api'
67-
}
68-
69-
completion({
70-
error: {
71-
type: reason,
72-
message: translateStatusCode(statusCode),
73-
addtion: response.data,
74-
},
75-
})
76-
77-
return
54+
if (statusCode >= 400 && statusCode < 500) {
55+
reason = 'param'
56+
} else {
57+
reason = 'api'
7858
}
7959

60+
completion({
61+
error: {
62+
type: reason,
63+
message: `接口响应错误 ${translateStatusCode(statusCode)}`,
64+
addtion: JSON.stringify(response),
65+
},
66+
})
67+
} else {
8068
const translations = response.data?.translations
8169

8270
if (!translations || !translations.length) {
@@ -98,11 +86,11 @@ export function translate(
9886
})
9987
}
10088
})().catch((err) => {
101-
$log.error(err)
10289
completion({
10390
error: {
10491
type: err._type || 'unknown',
10592
message: err._message || '未知错误',
93+
addtion: err._addtion,
10694
},
10795
})
10896
})

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const translateStatusCode = (code: number): string => {
2222
case 400:
2323
return 'Bad request. Please check error message and your parameters.'
2424
case 403:
25-
return 'Authorization failed. Please supply a valid auth_key parameter.'
25+
return 'Authorization failed. Please supply a valid token.'
2626
case 404:
2727
return 'The requested resource could not be found.'
2828
case 413:

types/global.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ interface $info {
1212
}
1313

1414
interface $log {
15-
info(obj: string | Record<string, unknown>): void
16-
error(obj: string | Record<string, unknown>): void
15+
info(obj?: string | Record<string, unknown>): void
16+
error(obj?: string | Record<string, unknown>): void
1717
}
1818

1919
interface $http {
2020
request<T>(requestObject: RequestObject): Promise<RequestCallbackResponse<T>>
2121
}
2222

2323
export interface RequestObject {
24-
method: 'GET' | 'POST' | 'DELETE'
24+
method: 'GET' | 'POST' | 'DELETE' | 'HEAD'
2525
url: string
2626
header?: Record<string, string>
2727
body?: Record<string, unknown>
@@ -51,7 +51,7 @@ export interface RequestCallbackResponse<T = Record<string, unknown>> {
5151

5252
interface $option {
5353
token: string
54-
provider: 'deepl' | 'sub-deepl'
54+
provider: 'deepl' | 'sub-deepl' | 'local'
5555
formality: 'default' | 'more' | 'less'
5656
}
5757

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@
240240
is-module "^1.0.0"
241241
resolve "^1.17.0"
242242

243+
"@rollup/plugin-replace@^2.3.4":
244+
version "2.3.4"
245+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.4.tgz#7dd84c17755d62b509577f2db37eb524d7ca88ca"
246+
integrity sha512-waBhMzyAtjCL1GwZes2jaE9MjuQ/DQF2BatH3fRivUF3z0JBFrU0U6iBNC/4WR+2rLKhaAhPWDNPYp4mI6RqdQ==
247+
dependencies:
248+
"@rollup/pluginutils" "^3.1.0"
249+
magic-string "^0.25.7"
250+
243251
"@rollup/plugin-typescript@^6.1.0":
244252
version "6.1.0"
245253
resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz#289e7f0ea12fd659bd13ad59dda73b9055538b83"

0 commit comments

Comments
 (0)