Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit beac66c

Browse files
committed
feat: add support for DeepL free
1 parent 1a073ee commit beac66c

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

src/common/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Client {
1717
if (error.response) {
1818
const { data, status } = error.response
1919

20-
if (data.message) {
20+
if (data?.message) {
2121
error.message = `${data.message} (${status})`
2222
}
2323
}
@@ -49,6 +49,8 @@ class Client {
4949

5050
private getAPI(): string {
5151
switch (this.region) {
52+
case 'free':
53+
return 'https://api-free.deepl.com'
5254
default:
5355
return 'https://api.deepl.com'
5456
}

src/common/constant.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ export const supportedLanguages = {
1313
PL: 'Polish',
1414
RU: 'Russian',
1515
}
16+
17+
export const supportedRegions = {
18+
default: 'DeepL Pro',
19+
free: '免费',
20+
}

src/common/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { supportedLanguages } from './constant'
1+
import { supportedLanguages, supportedRegions } from './constant'
22

33
export interface Config {
44
token: string
@@ -10,8 +10,9 @@ export interface Config {
1010
}
1111

1212
export type SupportLanguageKeys = keyof typeof supportedLanguages
13+
export type SupportRegionKeys = keyof typeof supportedRegions
1314

14-
export type APIRegions = 'default'
15+
export type APIRegions = 'default' | 'free'
1516

1617
export type TranslateResult = {
1718
translations: Array<{

src/pages/Options/Options.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import cc from 'chrome-call'
1010
import { useSnackbar } from 'notistack'
1111

1212
import Client from '../../common/api'
13-
import { supportedLanguages } from '../../common/constant'
14-
import { APIRegions, Config, SupportLanguageKeys } from '../../common/types'
13+
import { supportedLanguages, supportedRegions } from '../../common/constant'
14+
import {
15+
APIRegions,
16+
Config,
17+
SupportLanguageKeys,
18+
SupportRegionKeys,
19+
} from '../../common/types'
1520
import OptionSection from './components/OptionSection'
1621

1722
const InputGroup = styled('div')`
@@ -110,7 +115,10 @@ const Options: React.FC = () => {
110115
<div tw="space-y-6 p-5 overflow-auto">
111116
<OptionSection title={'目标语言'}>
112117
<select
113-
tw="px-4 py-3 rounded-md"
118+
tw="px-4 pl-3 pr-8 rounded-md"
119+
css={css`
120+
background-position: right 0.3rem center;
121+
`}
114122
name="target-lang"
115123
value={targetLang}
116124
onChange={(e) => setTargetLang(e.target.value)}>
@@ -122,7 +130,26 @@ const Options: React.FC = () => {
122130
</select>
123131
</OptionSection>
124132

125-
<OptionSection title={'API Token'}>
133+
<OptionSection title={'API 类型'}>
134+
<select
135+
tw="px-4 pl-3 pr-8 rounded-md"
136+
css={css`
137+
background-position: right 0.3rem center;
138+
`}
139+
name="region"
140+
value={region}
141+
onChange={(e) =>
142+
setRegion(e.target.value as SupportRegionKeys)
143+
}>
144+
{Object.keys(supportedRegions).map((region, index) => (
145+
<option value={region} key={index}>
146+
{supportedRegions[region as SupportRegionKeys]}
147+
</option>
148+
))}
149+
</select>
150+
</OptionSection>
151+
152+
<OptionSection title={'API 秘钥'}>
126153
<input
127154
tw="rounded-md w-full"
128155
type="text"

0 commit comments

Comments
 (0)