Skip to content

Commit 3bca9a1

Browse files
committed
Update to v3 API
1 parent 2af3886 commit 3bca9a1

9 files changed

Lines changed: 102 additions & 17 deletions

File tree

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"extends": "airbnb/base",
44
"env": {
55
"mocha": true
6+
},
7+
"rules": {
8+
"no-unused-expressions": "off"
69
}
710
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ var DetectLanguage = require('../lib/');
22

33
var detectlanguage = new DetectLanguage(process.env.DETECTLANGUAGE_API_KEY);
44

5-
detectlanguage.userStatus().then(function(result) {
5+
detectlanguage.accountStatus().then(function(result) {
66
console.log(JSON.stringify(result, null, 2));
77
});

examples/detectBatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ var detectlanguage = new DetectLanguage(process.env.DETECTLANGUAGE_API_KEY);
44

55
var texts = ['šešios žąsys', 'Strč prst skrz krk'];
66

7-
detectlanguage.detect(texts).then(function(result) {
7+
detectlanguage.detectBatch(texts).then(function(result) {
88
console.log(JSON.stringify(result, null, 2));
99
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"eslint-plugin-import": "^2.22.0",
5353
"mocha": "^8.1.3",
5454
"nyc": "^15.1.0",
55-
"rimraf": "^3.0.2"
55+
"rimraf": "^3.0.2",
56+
"sinon": "^21.0.0"
5657
}
5758
}

src/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
timeout: 60,
55
protocol: 'https',
66
host: 'ws.detectlanguage.com',
7-
apiVersion: '0.2',
7+
apiVersion: 'v3',
88
userAgent: `detectlanguage-node/${version}`,
99
};

src/index.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ export interface Options {
77

88
export interface DetectionResult {
99
language: string,
10-
isReliable: boolean,
11-
confidence: number,
10+
score: number,
1211
}
1312

1413
export interface Language {
1514
code: string,
1615
name: string,
1716
}
1817

19-
export interface UserStatus {
18+
export interface AccountStatus {
2019
status: string,
2120
date: string,
2221
requests: number,
@@ -36,8 +35,8 @@ export default class DetectLanguage {
3635
client: Client;
3736
constructor (apiKey: string, options?: Options);
3837
detect(text: string): Promise<DetectionResult[]>
39-
detect(text: string[]): Promise<DetectionResult[][]>;
38+
detectBatch(texts: string[]): Promise<DetectionResult[][]>;
4039
detectCode(text: string): Promise<string | null>;
4140
languages(): Promise<Language[]>;
42-
userStatus(): Promise<UserStatus>;
41+
accountStatus(): Promise<AccountStatus>;
4342
}

src/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ class API {
66
}
77

88
async detect(text) {
9-
const response = await this.client.post('detect', { q: text });
9+
if (Array.isArray(text)) {
10+
process.emitWarning(
11+
'detect() called with an array is deprecated. Use detectBatch() instead.',
12+
'DeprecationWarning',
13+
'DETECT_ARRAY_DEPRECATION',
14+
);
15+
return this.detectBatch(text);
16+
}
17+
18+
return this.client.post('detect', { q: text });
19+
}
1020

11-
return response.data.detections;
21+
async detectBatch(texts) {
22+
return this.client.post('detect-batch', { q: texts });
1223
}
1324

1425
async detectCode(text) {
@@ -21,8 +32,19 @@ class API {
2132
return this.client.get('languages');
2233
}
2334

35+
async accountStatus() {
36+
return this.client.get('account/status');
37+
}
38+
39+
// @deprecated
2440
async userStatus() {
25-
return this.client.get('user/status');
41+
process.emitWarning(
42+
'userStatus() is deprecated. Use accountStatus() instead.',
43+
'DeprecationWarning',
44+
'USER_STATUS_DEPRECATION',
45+
);
46+
47+
return this.accountStatus();
2648
}
2749
}
2850

test/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import chai, { expect } from 'chai';
22
import chaiAsPromised from 'chai-as-promised';
3+
import sinon from 'sinon';
34

45
import DetectLanguage from '../src';
56

67
chai.use(chaiAsPromised);
78

89
let detectLanguage;
10+
let emitWarningSpy;
911

1012
beforeEach(() => {
1113
detectLanguage = new DetectLanguage(process.env.DETECTLANGUAGE_API_KEY || '');
14+
emitWarningSpy = sinon.spy(process, 'emitWarning');
15+
});
16+
17+
afterEach(() => {
18+
emitWarningSpy.restore();
1219
});
1320

1421
describe('detect', () => {
1522
it('detects language', async () => {
1623
const result = await detectLanguage.detect('labas rytas');
1724

1825
expect(result[0].language).to.eq('lt');
19-
expect(result[0].isReliable).to.eq(true);
20-
expect(result[0].confidence).to.be.a('number');
26+
expect(result[0].score).to.be.a('number');
2127
});
2228

2329
it('detects language', async () => {
@@ -29,6 +35,16 @@ describe('detect', () => {
2935
it('works with batch', async () => {
3036
const result = await detectLanguage.detect(['šešios žąsys', 'Strč prst skrz krk']);
3137

38+
expect(emitWarningSpy.calledOnce).to.be.true;
39+
expect(result[0][0].language).to.eq('lt');
40+
expect(result[1][0].language).to.eq('cs');
41+
});
42+
});
43+
44+
describe('detectBatch', () => {
45+
it('works with batch', async () => {
46+
const result = await detectLanguage.detectBatch(['šešios žąsys', 'Strč prst skrz krk']);
47+
3248
expect(result[0][0].language).to.eq('lt');
3349
expect(result[1][0].language).to.eq('cs');
3450
});
@@ -42,7 +58,7 @@ describe('detectCode', () => {
4258
});
4359

4460
it('handles not detected', async () => {
45-
const result = await detectLanguage.detectCode('?');
61+
const result = await detectLanguage.detectCode(' ');
4662

4763
expect(result).to.be.a('null');
4864
});
@@ -61,6 +77,7 @@ describe('userStatus', () => {
6177
it('fetches user status', async () => {
6278
const result = await detectLanguage.userStatus();
6379

80+
expect(emitWarningSpy.calledOnce).to.be.true;
6481
expect(result.status).to.be.a('string');
6582
expect(result.requests).to.be.a('number');
6683
});

yarn.lock

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,28 @@
887887
resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd"
888888
integrity sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==
889889

890+
"@sinonjs/commons@^3.0.1":
891+
version "3.0.1"
892+
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.1.tgz#1029357e44ca901a615585f6d27738dbc89084cd"
893+
integrity sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==
894+
dependencies:
895+
type-detect "4.0.8"
896+
897+
"@sinonjs/fake-timers@^13.0.5":
898+
version "13.0.5"
899+
resolved "https://registry.yarnpkg.com/@sinonjs/fake-timers/-/fake-timers-13.0.5.tgz#36b9dbc21ad5546486ea9173d6bea063eb1717d5"
900+
integrity sha512-36/hTbH2uaWuGVERyC6da9YwGWnzUZXuPro/F2LfsdOsLnCojz/iSH8MxUt/FD2S5XBSVPhmArFUXcpCQ2Hkiw==
901+
dependencies:
902+
"@sinonjs/commons" "^3.0.1"
903+
904+
"@sinonjs/samsam@^8.0.1":
905+
version "8.0.3"
906+
resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-8.0.3.tgz#eb6ffaef421e1e27783cc9b52567de20cb28072d"
907+
integrity sha512-hw6HbX+GyVZzmaYNh82Ecj1vdGZrqVIn/keDTg63IgAwiQPO+xCz99uG6Woqgb4tM0mUiFENKZ4cqd7IX94AXQ==
908+
dependencies:
909+
"@sinonjs/commons" "^3.0.1"
910+
type-detect "^4.1.0"
911+
890912
"@types/color-name@^1.1.1":
891913
version "1.1.1"
892914
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
@@ -1699,6 +1721,11 @@ diff@4.0.2:
16991721
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
17001722
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
17011723

1724+
diff@^7.0.0:
1725+
version "7.0.0"
1726+
resolved "https://registry.yarnpkg.com/diff/-/diff-7.0.0.tgz#3fb34d387cd76d803f6eebea67b921dab0182a9a"
1727+
integrity sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==
1728+
17021729
doctrine@1.5.0:
17031730
version "1.5.0"
17041731
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
@@ -3612,6 +3639,17 @@ signal-exit@^3.0.2:
36123639
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
36133640
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==
36143641

3642+
sinon@^21.0.0:
3643+
version "21.0.0"
3644+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-21.0.0.tgz#dbda73abc7e6cb803fef3368cfbecbb5936e8a9e"
3645+
integrity sha512-TOgRcwFPbfGtpqvZw+hyqJDvqfapr1qUlOizROIk4bBLjlsjlB00Pg6wMFXNtJRpu+eCZuVOaLatG7M8105kAw==
3646+
dependencies:
3647+
"@sinonjs/commons" "^3.0.1"
3648+
"@sinonjs/fake-timers" "^13.0.5"
3649+
"@sinonjs/samsam" "^8.0.1"
3650+
diff "^7.0.0"
3651+
supports-color "^7.2.0"
3652+
36153653
slash@^2.0.0:
36163654
version "2.0.0"
36173655
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
@@ -3852,7 +3890,7 @@ supports-color@^5.3.0:
38523890
dependencies:
38533891
has-flag "^3.0.0"
38543892

3855-
supports-color@^7.1.0:
3893+
supports-color@^7.1.0, supports-color@^7.2.0:
38563894
version "7.2.0"
38573895
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da"
38583896
integrity sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==
@@ -3937,11 +3975,16 @@ type-check@^0.4.0, type-check@~0.4.0:
39373975
dependencies:
39383976
prelude-ls "^1.2.1"
39393977

3940-
type-detect@^4.0.0, type-detect@^4.0.5:
3978+
type-detect@4.0.8, type-detect@^4.0.0, type-detect@^4.0.5:
39413979
version "4.0.8"
39423980
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
39433981
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
39443982

3983+
type-detect@^4.1.0:
3984+
version "4.1.0"
3985+
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.1.0.tgz#deb2453e8f08dcae7ae98c626b13dddb0155906c"
3986+
integrity sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==
3987+
39453988
type-fest@^0.8.0, type-fest@^0.8.1:
39463989
version "0.8.1"
39473990
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"

0 commit comments

Comments
 (0)