Skip to content

Commit

Permalink
Add "eslint:recommended" and "plugin:@typescript-eslint/recommended" …
Browse files Browse the repository at this point in the history
…eslint configurations (#118)

- Add `eslint:recommended` and `plugin:@typescript-eslint/recommended`
to `extends` for `eslint`.
- Fix issue with TOC in `README.md`.
- Fix lint errors.
- Remove `;` from some files.
  • Loading branch information
fabasoad authored Feb 13, 2023
1 parent 2c7a73e commit c3ad8c9
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 56 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"es6": true
},
"extends": [
"google"
"eslint:recommended",
"google",
"plugin:@typescript-eslint/recommended"
],
"globals": {
"Atomics": "readonly",
Expand Down Expand Up @@ -35,5 +37,6 @@
"object-curly-spacing": "off",
"require-jsdoc": "off",
"semi": "off"
}
},
"root": true
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ more details for each provider below.

## Contents

<!-* TOC -->
<!-- TOC -->
* [Translation action](#translation-action)
* [Contents](#contents)
* [Inputs](#inputs)
Expand All @@ -27,7 +27,7 @@ more details for each provider below.
* [Microsoft](#microsoft)
* [MyMemory](#mymemory)
* [FunTranslations](#funtranslations)
<!-* TOC -->
<!-- TOC -->

## Inputs

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/__tests__/providers/DeeplProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import ProviderBase from '../../providers/ProviderBase'
import DeeplProvider from '../../providers/DeeplProvider'
import { config } from 'dotenv'

require('dotenv').config();
config()

describe('DeeplProvider', () => {
test('should get correct translation', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/providers/FunTranslationsProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ describe('FunTranslationsProvider', () => {
const translations = await provider.translate('Evening', 'vulcan')
expect(translations.length).toEqual(1)
expect(translations[0]).toEqual('Khru')
} catch (e) {
} catch (e: unknown) {
if (!(e instanceof ProviderError)) {
const { statusCode } = e as any
const { statusCode } = e as never
if (statusCode !== 429) {
throw e
}
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/providers/LinguaToolsProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LinguaToolsProvider from '../../providers/LinguaToolsProvider'
import ProviderBase from '../../providers/ProviderBase'
import ProviderBase, { ProviderError } from '../../providers/ProviderBase'

describe('LinguaToolsProvider', () => {
test.skip('should get correct translation', async () => {
Expand All @@ -13,8 +13,9 @@ describe('LinguaToolsProvider', () => {
const provider: ProviderBase = new LinguaToolsProvider()
try {
await provider.translate('Evening', 'abc123')
} catch (e) {
if ((<any>e).status && (<any>e).status === 404) {
} catch (e: unknown) {
const { status } = e as ProviderError
if (status === 404) {
throw e
}
expect(e).toBeTruthy()
Expand Down
33 changes: 17 additions & 16 deletions src/__tests__/providers/MyMemoryProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import itParam from 'mocha-param';
import MyMemoryProvider from '../../providers/MyMemoryProvider';
import ProviderBase from '../../providers/ProviderBase';
import itParam from 'mocha-param'
import MyMemoryProvider from '../../providers/MyMemoryProvider'
import ProviderBase from '../../providers/ProviderBase'
import { config } from 'dotenv'

require('dotenv').config();
config()

describe('MyMemoryProvider', () => {
itParam<string | undefined>('should get correct translation',
[undefined, process.env.MYMEMORY_API_KEY], async (apiKey) => {
const provider: ProviderBase = new MyMemoryProvider(apiKey);
const translations = await provider.translate('Evening', 'en|pt');
expect(translations.length).toBeGreaterThan(0);
expect(translations[0]).toEqual('NOITE');
});
const provider: ProviderBase = new MyMemoryProvider(apiKey)
const translations = await provider.translate('Evening', 'en|pt')
expect(translations.length).toBeGreaterThan(0)
expect(translations[0]).toEqual('NOITE')
})

test('should fail because of invalid lang', async () => {
const provider: ProviderBase = new MyMemoryProvider();
const provider: ProviderBase = new MyMemoryProvider()
try {
await provider.translate('Evening', 'abc123');
await provider.translate('Evening', 'abc123')
} catch (e) {
expect(e).toBeTruthy();
return;
expect(e).toBeTruthy()
return
}
fail();
});
});
fail()
})
})
18 changes: 9 additions & 9 deletions src/providers/FunTranslationsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import ProviderBase from './ProviderBase';
import ProviderBase from './ProviderBase'

type FunTranslationsResponse =
{ success: { total: number } | undefined, contents: { translated: string } };
{ success: { total: number } | undefined, contents: { translated: string } }

export default class FunTranslationsProvider extends ProviderBase {
constructor() {
super('https://api.funtranslations.com');
super('https://api.funtranslations.com')
}

async translate(text: string, lang: string): Promise<string[]> {
const url = `/translate/${lang}.json?text=${text}`;
const url = `/translate/${lang}.json?text=${text}`
return this.api<FunTranslationsResponse>({ url, method: 'GET' })
.then(({ success, contents }) => {
if (success && success.total > 0) {
return [contents.translated];
return [contents.translated]
}
console.warn(
'Result is either not success or doesn\'t have any translations');
return [text];
});
};
'Result is either not success or doesn\'t have any translations')
return [text]
})
}
}
15 changes: 8 additions & 7 deletions src/providers/LinguaToolsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/* eslint-disable camelcase */
import ProviderBase from './ProviderBase';

type LinguaToolsResponse = { freq: number, l1_text: string }[];
/* eslint-disable camelcase */
type LinguaToolsResponse = { freq: number, l1_text: string }[]

export default class LinguaToolsProvider extends ProviderBase {
constructor() {
super('https://lt-translate-test.herokuapp.com');
super('https://lt-translate-test.herokuapp.com')
}

translate(text: string, lang: string): Promise<string[]> {
const url: string = `/?langpair=${lang}&query=${text}`;
const url = `/?langpair=${lang}&query=${text}`
return this.api<LinguaToolsResponse>({ url, method: 'GET' })
.then((translations) => {
translations.sort((a, b) => a.freq > b.freq ? 1 : -1);
return translations.map(({ l1_text }) => l1_text);
});
translations.sort((a, b) => a.freq > b.freq ? 1 : -1)
return translations.map(({ l1_text }) => l1_text)
})
}
}
/* eslint-enable camelcase */
18 changes: 9 additions & 9 deletions src/providers/MicrosoftProvider.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import ProviderBase from './ProviderBase';
import ProviderBase from './ProviderBase'

type MicrosoftResponse = { translations: { text: string }[] }[];
type MicrosoftResponse = { translations: { text: string }[] }[]

export default class MicrosoftProvider extends ProviderBase {
private readonly apiKey: string;
private readonly addParam: string;
private readonly apiKey: string
private readonly addParam: string

constructor(apiKey: string, addParam: string) {
super('https://api.cognitive.microsofttranslator.com');
this.apiKey = apiKey;
this.addParam = addParam;
super('https://api.cognitive.microsofttranslator.com')
this.apiKey = apiKey
this.addParam = addParam
}

translate(text: string, lang: string): Promise<string[]> {
const url: string = `/translate?api-version=3.0&to=${lang}`;
const url = `/translate?api-version=3.0&to=${lang}`
return this.api<MicrosoftResponse>({
url,
headers: {
Expand All @@ -23,6 +23,6 @@ export default class MicrosoftProvider extends ProviderBase {
},
method: 'POST',
data: { Text: text }
}).then((resp) => resp[0].translations.map((t) => t.text));
}).then((resp) => resp[0].translations.map((t) => t.text))
}
}
8 changes: 4 additions & 4 deletions src/providers/ProviderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { IRestResponse, RestClient } from 'typed-rest-client/RestClient';
import { IHeaders } from 'typed-rest-client/Interfaces';

export class ProviderError extends Error {
private readonly status: number;
private readonly _status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
this._status = status;
}
public getStatus(): number {
return this.status;
public get status(): number {
return this._status;
}
}

Expand Down

0 comments on commit c3ad8c9

Please sign in to comment.