Skip to content

Commit dc08ced

Browse files
committed
docs: update upgrade docs with info about promises
1 parent a922875 commit dc08ced

4 files changed

Lines changed: 40 additions & 17 deletions

File tree

UPGRADE-4.0.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515

1616
## Breaking changes
1717
### Methods no longer return stream
18-
Previously, if a callback was not provided, each service method would return a pipeable stream. Now, this will no longer happen. This should not affect many users, as authenticating with IAM already prevented methods from returning streams.
18+
Previously, if a callback was not provided, each service method would return a pipeable stream. Now, this will no longer happen. A Promise is returned instead. This should not affect many users, as authenticating with IAM already prevented methods from returning streams.
19+
20+
### Response Handling Compatibility
21+
The entire response is received with the third callback (if using callbacks) or when using Promises with `return_response: true` set as a parameter. This response now has a different structure for consistency with the other Watson SDKs.
22+
23+
The object returned has the following properties:
24+
- `data`: The body of the response
25+
- `headers`: The HTTP response headers
26+
- `status`: The HTTP status code
27+
- `statusText`: The HTTP status message
28+
- `request`: The actual request instance sent to the service
29+
- `config`: The parameters used to build the request
30+
1931

2032
### Error Handling Compatibility
2133
The logic that formats errors returned from a service has been changed. That means that the Error object returned now has a different structure. If there is logic in your code that parses the error returned from service methods, this will need to change. The new structure is outlined below.

lib/requestwrapper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,13 @@ export function sendRequest(parameters, _callback) {
236236
paramsSerializer: params => {
237237
return querystring.stringify(params);
238238
},
239+
// a custom httpsAgent is needed to support ICP
239240
httpsAgent: new https.Agent({ rejectUnauthorized }),
240241
};
241242

242243
axios(requestParams)
243244
.then(res => {
245+
// the other sdks use the interface `result` for the body
244246
_callback(null, res.data, res);
245247
})
246248
.catch(error => {

test/integration/assistant.v1.test.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,19 +523,21 @@ describe('assistant_integration', function() {
523523
});
524524

525525
describe('getCounterexample()', function() {
526-
it('should return a counterexample', function(done) {
526+
it('should return a counterexample - using promise', function(done) {
527527
const params = {
528528
workspace_id: workspace1.workspace_id,
529529
text: counterexampleText,
530530
};
531531

532-
assistant.getCounterexample(params, function(err, result) {
533-
if (err) {
532+
assistant
533+
.getCounterexample(params)
534+
.then(result => {
535+
expect(result.text).toBe(counterexampleText);
536+
done();
537+
})
538+
.catch(err => {
534539
return done(err);
535-
}
536-
expect(result.text).toBe(counterexampleText);
537-
done();
538-
});
540+
});
539541
});
540542
});
541543

test/integration/text_to_speech.test.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,22 @@ describe('text_to_speech_integration', function() {
9292
);
9393
});
9494

95-
it('listVoiceModels()', function(done) {
96-
text_to_speech.listVoiceModels({}, function(err, response) {
97-
// console.log(JSON.stringify(err || response, null, 2));
98-
if (err) {
99-
return done(err);
100-
}
101-
expect(Array.isArray(response.customizations)).toBe(true);
102-
done();
103-
});
95+
it('should return promise with entire response if return_response is true (listVoiceModels)', function(done) {
96+
text_to_speech
97+
.listVoiceModels({ return_response: true })
98+
.then(response => {
99+
expect(Array.isArray(response.data.customizations)).toBe(true);
100+
expect(response.data).toBeDefined();
101+
expect(response.headers).toBeDefined();
102+
expect(response.status).toBeDefined();
103+
expect(response.statusText).toBeDefined();
104+
expect(response.request).toBeDefined();
105+
expect(response.config).toBeDefined();
106+
done();
107+
})
108+
.catch(err => {
109+
done(err);
110+
});
104111
});
105112

106113
it('listVoiceModels() with language', function(done) {

0 commit comments

Comments
 (0)