Skip to content

Commit

Permalink
fix: lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
ifyour committed Nov 5, 2023
1 parent ab18ae2 commit c0d0da1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const API_URL = "https://www2.deepl.com/jsonrpc";
export const DEFAULT_LANGUAGE = "AUTO";
export const REQUEST_ALTERNATIVES = 3;
export const API_URL = 'https://www2.deepl.com/jsonrpc';
export const DEFAULT_LANGUAGE = 'AUTO';
export const REQUEST_ALTERNATIVES = 3;
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { query } from './query'
import { query } from './query';

export { query }
export { query };
30 changes: 15 additions & 15 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { RequestParams, ResponseParams } from './types';
import { RequestParams, ResponseParams } from './types';
import { API_URL, DEFAULT_LANGUAGE, REQUEST_ALTERNATIVES } from './const';

function buildRequestParams(sourceLang: string, targetLang: string) {
return {
jsonrpc: "2.0",
method: "LMT_handle_texts",
jsonrpc: '2.0',
method: 'LMT_handle_texts',
id: Math.floor(Math.random() * 100000 + 100000) * 1000,
params: {
texts: [{ text: "", requestAlternatives: REQUEST_ALTERNATIVES }],
texts: [{ text: '', requestAlternatives: REQUEST_ALTERNATIVES }],
timestamp: 0,
splitting: "newlines",
splitting: 'newlines',
lang: {
source_lang_user_selected: sourceLang,
target_lang: targetLang,
Expand All @@ -19,7 +19,7 @@ function buildRequestParams(sourceLang: string, targetLang: string) {
}

function countLetterI(translateText: string) {
return translateText.split("i").length - 1;
return translateText.split('i').length - 1;
}

function getTimestamp(letterCount: number) {
Expand All @@ -41,8 +41,8 @@ function buildRequestBody(data: RequestParams) {

let requestString = JSON.stringify(requestData);
if (
[0, 3].includes((requestData["id"] + 5) % 29) ||
(requestData["id"] + 3) % 13 === 0
[0, 3].includes((requestData['id'] + 5) % 29) ||
(requestData['id'] + 3) % 13 === 0
) {
requestString = requestString.replace('"method":"', '"method" : "');
} else {
Expand All @@ -55,21 +55,21 @@ function buildRequestBody(data: RequestParams) {
async function query(params: RequestParams) {
const response = await fetch(API_URL, {
headers: {
"Content-Type": "application/json; charset=utf-8",
'Content-Type': 'application/json; charset=utf-8',
},
method: "POST",
method: 'POST',
body: buildRequestBody(params),
});

if (response.ok) {
const { id, result } = await response.json() as ResponseParams;
const { id, result } = (await response.json()) as ResponseParams;
return {
id,
code: 200,
data: result?.texts?.[0]?.text,
source_lang: params?.source_lang,
target_lang: params?.target_lang,
alternatives: result.texts?.[0]?.alternatives?.map?.(item => item.text)
alternatives: result.texts?.[0]?.alternatives?.map?.(item => item.text),
};
}

Expand All @@ -78,9 +78,9 @@ async function query(params: RequestParams) {
code: response.status,
data:
response.status === 429
? "Too many requests, please try again later."
: "Unknown error.",
? 'Too many requests, please try again later.'
: 'Unknown error.',
};
}

export { query }
export { query };
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

export type RequestParams = {
text: string;
source_lang: string;
target_lang: string
}
target_lang: string;
};

export type ResponseParams = {
jsonrpc: string;
Expand All @@ -19,4 +18,4 @@ export type ResponseParams = {
lang_is_confident: boolean;
detectedLanguages: { unsupported: number } & Record<string, number>;
};
}
};
40 changes: 25 additions & 15 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@ import { API_URL } from '../src/const';
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({
id: 1,
result: {
texts: [{
text: 'Translated text',
alternatives: [{ text: 'Translated text' }]
}]
}
}),
json: () =>
Promise.resolve({
id: 1,
result: {
texts: [
{
text: 'Translated text',
alternatives: [{ text: 'Translated text' }],
},
],
},
}),
})
);

describe('query', () => {
it('sends a post request and returns the response', async () => {
const response = await query({ text: '翻译文本', source_lang: 'zh', target_lang: 'en' });
const response = await query({
text: '翻译文本',
source_lang: 'zh',
target_lang: 'en',
});
expect(global.fetch).toHaveBeenCalledTimes(1);
expect(global.fetch).toHaveBeenCalledWith(API_URL, expect.objectContaining({
method: 'POST',
headers: { "Content-Type": "application/json; charset=utf-8" },
}));
expect(global.fetch).toHaveBeenCalledWith(
API_URL,
expect.objectContaining({
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=utf-8' },
})
);
expect(response.id).toEqual(1);
expect(response.code).toEqual(200);
expect(response.data).toEqual('Translated text');
expect(response.alternatives).toEqual(["Translated text"]);
expect(response.alternatives).toEqual(['Translated text']);
expect(response.source_lang).toBe('zh');
expect(response.target_lang).toBe('en');
});
Expand Down

0 comments on commit c0d0da1

Please sign in to comment.