Skip to content

Commit 65855bf

Browse files
feat: add proxy support with node-fetch-native/proxy (#40)
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
1 parent 4b07662 commit 65855bf

File tree

8 files changed

+51
-145
lines changed

8 files changed

+51
-145
lines changed

.changeset/great-steaks-melt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@deeplx/cli": patch
3+
"@deeplx/core": patch
4+
---
5+
6+
feat: add `proxy` support with `node-fetch-native/proxy`

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@deeplx/core": "workspace:*",
3636
"@octokit/request": "^9.2.3",
3737
"@pkgr/rollup": "^6.0.3",
38-
"@types/web": "^0.0.219",
3938
"@vercel/node": "^5.1.14",
4039
"@vitest/coverage-istanbul": "^3.1.1",
4140
"eslint": "^9.24.0",
@@ -51,10 +50,13 @@
5150
"typescript": "^5.8.3",
5251
"vite-tsconfig-paths": "^5.1.4",
5352
"vitest": "^3.1.1",
54-
"yarn-berry-deduplicate": "^6.1.1"
53+
"yarn-berry-deduplicate": "^6.1.3"
5554
},
5655
"resolutions": {
57-
"prettier": "^3.5.3"
56+
"node-fetch": "npm:node-fetch-native@^1.6.6",
57+
"prettier": "^3.5.3",
58+
"undici": "npm:undici@^7.8.0",
59+
"undici-types": "npm:undici@^7.8.0"
5860
},
5961
"typeCoverage": {
6062
"atLeast": 100,

packages/@deeplx/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dependencies": {
4141
"@deeplx/core": "^0.1.1",
4242
"commander": "^13.1.0",
43-
"node-fetch": "^3.3.2"
43+
"node-fetch-native": "^1.6.6"
4444
},
4545
"publishConfig": {
4646
"access": "public"

packages/@deeplx/cli/src/fetch.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import fetch, { Headers } from 'node-fetch'
1+
import fetch, { Headers } from 'node-fetch-native'
22

3-
// @ts-expect-error -- incompatible types
43
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
54
globalThis.fetch ??= fetch
65

7-
// @ts-expect-error -- incompatible types
86
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
97
globalThis.Headers ??= Headers

packages/@deeplx/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"translator"
4949
],
5050
"dependencies": {
51+
"node-fetch-native": "^1.6.6",
5152
"whatlang-node": "^0.1.0",
5253
"x-fetch": "^0.2.6"
5354
},

packages/@deeplx/core/src/constants.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { HeadersInit } from 'undici'
2+
13
import type { ValueOf } from './types.ts'
24

35
export const SUPPORTED_LANGUAGES = [
@@ -64,7 +66,7 @@ export const HTTP_STATUS_NOT_ALLOWED = 405
6466
export const HTTP_STATUS_INTERNAL_ERROR = 500
6567
export const HTTP_STATUS_SERVICE_UNAVAILABLE = 503
6668

67-
export const COMMON_HEADERS: HeadersInit = {
69+
export const COMMON_HEADERS = {
6870
'Content-Type': 'application/json',
6971
'User-Agent': 'DeepL/1627620 CFNetwork/3826.500.62.2.1 Darwin/24.4.0',
7072
Accept: '*/*',
@@ -77,4 +79,4 @@ export const COMMON_HEADERS: HeadersInit = {
7779
'X-Product': 'translator',
7880
'X-App-Build': '1627620',
7981
'X-App-Version': '25.1',
80-
}
82+
} as const satisfies HeadersInit

packages/@deeplx/core/src/translate.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { createProxy } from 'node-fetch-native/proxy'
12
import { detectLang } from 'whatlang-node'
2-
import { fetchApi } from 'x-fetch'
3+
import { xfetch } from 'x-fetch'
34

45
import {
56
API_URL,
@@ -29,15 +30,16 @@ import {
2930
// makeRequest makes an HTTP request to DeepL API
3031
const makeRequest = async (
3132
postData: PostData,
32-
_proxyUrl?: string, // unsupported yet
33+
proxyUrl?: string,
3334
dlSession?: string,
3435
) => {
35-
return fetchApi<TranslationResponse>(API_URL, {
36+
return xfetch<TranslationResponse>(API_URL, {
3637
method: 'POST',
3738
body: formatPostString(postData),
3839
headers: dlSession
3940
? { ...COMMON_HEADERS, Cookie: `dl_session=${dlSession}` }
4041
: COMMON_HEADERS,
42+
...createProxy({ url: proxyUrl }),
4143
})
4244
}
4345

0 commit comments

Comments
 (0)