Skip to content

Commit fcf2be3

Browse files
committed
Migrate from got HTTP client to ky
1 parent 374584d commit fcf2be3

File tree

3 files changed

+35
-202
lines changed

3 files changed

+35
-202
lines changed

lib/api-client.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const jsSHA = require("jssha");
4-
const got = require('got');
54

65
const api_errors = require('./errors');
76
const requests = require('./requests');
@@ -42,18 +41,18 @@ class ApiClient {
4241
let url = this._buildRequestUrl(request);
4342
let options = {
4443
method: request.method,
45-
url: url,
4644
headers: {'Accept': 'application/json',
4745
'Content-Type': 'application/json',
48-
'User-Agent': 'recombee-node-api-client/4.1.3'},
46+
'User-Agent': 'recombee-node-api-client/4.1.5'},
4947
timeout: request.timeout,
5048
agent: this.options.agent
5149
};
5250

5351
if (Object.entries(request.bodyParameters()).length > 0)
5452
options.json = request.bodyParameters();
5553

56-
return got(options)
54+
return import('ky').then((ky) => {
55+
return ky.default(url, options)
5756
.json()
5857
.then((response)=> {
5958
return new Promise( (resolve) => {
@@ -62,17 +61,23 @@ class ApiClient {
6261
});
6362
})
6463
.catch((error) => {
65-
if (error instanceof got.HTTPError) {
66-
error = new api_errors.ResponseError(request, error.response.statusCode, error.response.body);
67-
}
68-
else if (error instanceof got.RequestError) {
69-
if(error.code === 'ETIMEDOUT' || error.code === 'ESOCKETTIMEDOUT') {
70-
error = new api_errors.TimeoutError(request, error);
71-
}
72-
}
73-
if (callback) {return callback(error)};
74-
throw error;
75-
});
64+
return error.response.text().then((body) => {
65+
if (error instanceof ky.HTTPError) {
66+
error = new api_errors.ResponseError(
67+
request,
68+
error.response.status,
69+
body
70+
);
71+
} else if (error instanceof ky.TimeoutError) {
72+
error = new api_errors.TimeoutError(request, error);
73+
}
74+
if (callback) {
75+
return callback(error);
76+
}
77+
throw error;
78+
});
79+
});
80+
})
7681
}
7782

7883
_getRegionalBaseUri(region) {

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
"API",
2121
"SDK"
2222
],
23+
"engines": {
24+
"node": ">=13.2.0"
25+
},
2326
"author": "Ondrej Fiedler <[email protected]> (https://www.recombee.com/)",
2427
"license": "MIT",
2528
"bugs": {
2629
"url": "https://github.com/Recombee/node-api-client/issues"
2730
},
2831
"homepage": "https://github.com/Recombee/node-api-client#readme",
2932
"dependencies": {
30-
"got": "^11.8.2",
31-
"jssha": "^2.3.0"
33+
"jssha": "^2.3.0",
34+
"ky": "^1.7.2"
3235
},
3336
"devDependencies": {
3437
"chai": "^3.5.0",
35-
"mocha": "~10.2.0",
36-
"typescript": "~5.3.3"
38+
"mocha": "~10.2.0",
39+
"typescript": "~5.3.3"
3740
}
3841
}

0 commit comments

Comments
 (0)