Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 808602c

Browse files
authored
Merge pull request #186 from Alfresco/development
1.2.0
2 parents 95b754b + d15eba1 commit 808602c

File tree

11 files changed

+665
-474
lines changed

11 files changed

+665
-474
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
</p>
44

55
Alfresco JS API
6+
<a name="1.2.0"></a>
7+
# [1.2.0](https://github.com/Alfresco/alfresco-js-api/releases/tag/1.2.0) (xx-xx-2017)
8+
## Fix
9+
- [Upgrade superagent library to 3.4.1 and provide responseType override #180](https://github.com/Alfresco/alfresco-js-api/pull/180)
10+
- [Username should be trimmed in the login #184](https://github.com/Alfresco/alfresco-js-api/pull/184)
611

712
<a name="1.1.1"></a>
813
# [1.1.1](https://github.com/Alfresco/alfresco-js-api/releases/tag/1.1.1) (27-01-2017)
@@ -33,7 +38,7 @@ _This project provides a JavaScript client API into the v1 Alfresco REST API_
3338
hostBpm: 'http://127.0.0.1:9999',
3439
contextRootBpm: 'activiti-custom-root'
3540
});
36-
41+
3742
this.bpmAuth.login('admin', 'admin');
3843
```
3944

@@ -275,7 +280,7 @@ Before:
275280
```javascript
276281
this.alfrescoJsApi = new AlfrescoApi({ host :''http://127.0.0.1:8080', ticket :'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1'});
277282
```
278-
283+
279284
After:
280285
281286
```javascript
@@ -286,7 +291,7 @@ this.alfrescoJsApi = new AlfrescoApi({ ticketEcm:'TICKET_4479f4d3bb155195879bfbb
286291
//Login ticket BPM
287292
this.alfrescoJsApi = new AlfrescoApi({ ticketBpm: 'Basic YWRtaW46YWRtaW4=', hostBpm:'http://127.0.0.1:9999'});
288293

289-
//Login ticket ECM and BPM
294+
//Login ticket ECM and BPM
290295
this.alfrescoJsApi = new AlfrescoApi({ ticketEcm:'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1', ticketBpm: 'Basic YWRtaW46YWRtaW4=', hostEcm:'http://127.0.0.1:8080', hostBpm:'http://127.0.0.1:9999'});
291296
```
292297

dist/alfresco-js-api.js

Lines changed: 480 additions & 381 deletions
Large diffs are not rendered by default.

dist/alfresco-js-api.min.js

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "alfresco-js-api",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "JavaScript client library for the Alfresco REST API",
55
"author": "Alfresco Software, Ltd.",
66
"main": "main.js",
@@ -27,7 +27,7 @@
2727
"url": "https://github.com/Alfresco/alfresco-js-api/issues"
2828
},
2929
"dependencies": {
30-
"superagent": "1.7.1",
30+
"superagent": "3.4.1",
3131
"event-emitter": "^0.3.4",
3232
"lodash": "^4.13.1",
3333
"nock": "8.1.0"

src/alfresco-activiti-rest-api/src/api/ContentApi.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,13 @@
477477
var contentTypes = ['application/json'];
478478
var accepts = ['application/json'];
479479
var returnType = Object;
480+
var contextRoot = null;
481+
var responseType = 'blob';
480482

481483
return this.apiClient.callApi(
482484
'/api/enterprise/content/{contentId}/raw', 'GET',
483485
pathParams, queryParams, headerParams, formParams, postBody,
484-
authNames, contentTypes, accepts, returnType
486+
authNames, contentTypes, accepts, returnType, contextRoot, responseType
485487
);
486488
}
487489

src/alfrescoApi.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class AlfrescoApi {
131131
* @returns {Promise} A promise that returns {new authentication ticket} if resolved and {error} if rejected.
132132
* */
133133
login(username, password) {
134+
135+
if (username) {
136+
username = username.trim();
137+
}
138+
134139
if (this._isBpmConfiguration()) {
135140
var bpmPromise = this.bpmAuth.login(username, password);
136141

src/alfrescoApiClient.js

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class AlfrescoApiClient extends ApiClient {
3030
* @param {Array.<String>} accepts An array of acceptable response MIME types.
3131
* @param {(String|Array|ObjectFunction)} returnType The required type to return; can be a string for simple types or the
3232
* @param {(String)} contextRoot alternative contextRoot
33+
* @param {(String)} responseType is an enumerated value that returns the type of the response.
34+
* It also lets the author change the response type to one "arraybuffer", "blob", "document",
35+
* "json", or "text".
36+
* If an empty string is set as the value of responseType, it is assumed as type "text".
3337
* constructor for a complex type. * @returns {Promise} A Promise object.
3438
*/
3539
callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, authNames,
36-
contentTypes, accepts, returnType, contextRoot) {
40+
contentTypes, accepts, returnType, contextRoot, responseType) {
3741

3842
var eventEmitter = {};
3943
Emitter(eventEmitter); // jshint ignore:line
@@ -47,72 +51,8 @@ class AlfrescoApiClient extends ApiClient {
4751
url = this.buildUrl(path, pathParams);
4852
}
4953

50-
var request = superagent(httpMethod, url);
51-
52-
// apply authentications
53-
this.applyAuthToRequest(request, ['basicAuth']);
54-
55-
// set query parameters
56-
request.query(this.normalizeParams(queryParams));
57-
58-
// set header parameters
59-
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
60-
61-
if (this.isBpmRequest() && this.isCsrfEnabled()) {
62-
this.setCsrfToken(request);
63-
}
64-
65-
// add cookie for activiti
66-
if (this.isBpmRequest()) {
67-
request._withCredentials = true;
68-
if (this.authentications.cookie) {
69-
if (this.isNodeEnv()) {
70-
request.set('Cookie', this.authentications.cookie);
71-
}
72-
}
73-
}
74-
75-
// set request timeout
76-
request.timeout(this.timeout);
77-
78-
var contentType = this.jsonPreferredMime(contentTypes);
79-
80-
if (contentType && contentType !== 'multipart/form-data') {
81-
request.type(contentType);
82-
} else if (!request.header['Content-Type'] && contentType !== 'multipart/form-data') {
83-
request.type('application/json');
84-
}
85-
86-
if (contentType === 'application/x-www-form-urlencoded') {
87-
request.send(this.normalizeParams(formParams)).on('progress', (event)=> {
88-
this.progress(event, eventEmitter);
89-
});
90-
} else if (contentType === 'multipart/form-data') {
91-
var _formParams = this.normalizeParams(formParams);
92-
for (var key in _formParams) {
93-
if (_formParams.hasOwnProperty(key)) {
94-
if (this.isFileParam(_formParams[key])) {
95-
// file field
96-
request.attach(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
97-
this.progress(event, eventEmitter);
98-
});
99-
} else {
100-
request.field(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
101-
this.progress(event, eventEmitter);
102-
});
103-
}
104-
}
105-
}
106-
} else if (bodyParam) {
107-
request.send(bodyParam).on('progress', (event)=> {
108-
this.progress(event, eventEmitter);
109-
});
110-
}
111-
112-
var accept = this.jsonPreferredMime(accepts);
113-
if (accept) {
114-
request.accept(accept);
115-
}
54+
var request = this.buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
55+
contentTypes, accepts, responseType, eventEmitter);
11656

11757
this.promise = new Promise((resolve, reject) => {
11858
request.end((error, response) => {
@@ -250,6 +190,82 @@ class AlfrescoApiClient extends ApiClient {
250190
});
251191
return url;
252192
}
193+
194+
buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
195+
contentTypes, accepts, responseType, eventEmitter) {
196+
var request = superagent(httpMethod, url);
197+
198+
// apply authentications
199+
this.applyAuthToRequest(request, ['basicAuth']);
200+
201+
// set query parameters
202+
request.query(this.normalizeParams(queryParams));
203+
204+
// set header parameters
205+
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
206+
207+
if (this.isBpmRequest() && this.isCsrfEnabled()) {
208+
this.setCsrfToken(request);
209+
}
210+
211+
// add cookie for activiti
212+
if (this.isBpmRequest()) {
213+
request._withCredentials = true;
214+
if (this.authentications.cookie) {
215+
if (this.isNodeEnv()) {
216+
request.set('Cookie', this.authentications.cookie);
217+
}
218+
}
219+
}
220+
221+
// set request timeout
222+
request.timeout(this.timeout);
223+
224+
if (responseType) {
225+
request._responseType = responseType;
226+
}
227+
228+
var contentType = this.jsonPreferredMime(contentTypes);
229+
230+
if (contentType && contentType !== 'multipart/form-data') {
231+
request.type(contentType);
232+
} else if (!request.header['Content-Type'] && contentType !== 'multipart/form-data') {
233+
request.type('application/json');
234+
}
235+
236+
if (contentType === 'application/x-www-form-urlencoded') {
237+
request.send(this.normalizeParams(formParams)).on('progress', (event)=> {
238+
this.progress(event, eventEmitter);
239+
});
240+
} else if (contentType === 'multipart/form-data') {
241+
var _formParams = this.normalizeParams(formParams);
242+
for (var key in _formParams) {
243+
if (_formParams.hasOwnProperty(key)) {
244+
if (this.isFileParam(_formParams[key])) {
245+
// file field
246+
request.attach(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
247+
this.progress(event, eventEmitter);
248+
});
249+
} else {
250+
request.field(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
251+
this.progress(event, eventEmitter);
252+
});
253+
}
254+
}
255+
}
256+
} else if (bodyParam) {
257+
request.send(bodyParam).on('progress', (event)=> {
258+
this.progress(event, eventEmitter);
259+
});
260+
}
261+
262+
var accept = this.jsonPreferredMime(accepts);
263+
if (accept) {
264+
request.accept(accept);
265+
}
266+
267+
return request;
268+
}
253269
}
254270

255271
Emitter(AlfrescoApiClient.prototype); // jshint ignore:line

src/bpmAuth.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class BpmAuth extends AlfrescoApiClient {
136136
* */
137137
setTicket(ticket) {
138138
this.authentications.basicAuth.ticket = ticket;
139+
this.authentications.basicAuth.password = null;
139140
this.ticket = ticket;
140141
}
141142

test/alfrescoApiClient.spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*global describe, it */
22

3-
var ApiClient = require('../src/alfresco-core-rest-api/src/ApiClient');
3+
var ApiClient = require('../src/alfrescoApiClient');
44
var chai = require('chai');
55
var expect = chai.expect;
66

@@ -10,6 +10,34 @@ describe('Alfresco Core API Client', function () {
1010

1111
describe('type conversion', function() {
1212

13+
var client = new ApiClient();
14+
15+
it('should create a request with response type blob', function() {
16+
17+
var bodyParam = null;
18+
19+
var queryParams = {
20+
};
21+
var headerParams = {
22+
};
23+
var formParams = {
24+
};
25+
26+
var contentTypes = ['application/json'];
27+
var accepts = ['application/json'];
28+
var responseType = 'blob';
29+
var url = '/fake-api/enterprise/process-instances/';
30+
var httpMethod = 'GET';
31+
32+
var response = client.buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
33+
contentTypes, accepts, responseType, null);
34+
35+
expect(response.url).equal('/fake-api/enterprise/process-instances/');
36+
expect(response.header.Accept).equal('application/json');
37+
expect(response.header['Content-Type']).equal('application/json');
38+
expect(response._responseType).equal('blob');
39+
});
40+
1341
it('should return strings as a string', function() {
1442
var testData = 'Example String';
1543
expect(ApiClient.convertToType(testData, 'String')).equal(testData);

test/bpmAuth.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ describe('Bpm Auth test', function () {
3131

3232
});
3333

34+
it('login password should be removed after login', function (done) {
35+
36+
this.authBpmMock.get200Response();
37+
38+
this.bpmAuth = new BpmAuth({
39+
hostBpm: this.hostBpm,
40+
contextRootBpm: 'activiti-app'
41+
});
42+
43+
this.bpmAuth.login('admin', 'admin').then((data) => {
44+
expect(data).to.be.equal('Basic YWRtaW46YWRtaW4=');
45+
expect(this.bpmAuth.authentications.basicAuth.password).to.be.not.equal('admin');
46+
done();
47+
}, function () {
48+
});
49+
50+
});
51+
3452
it('isLoggedIn should return true if the api is logged in', function (done) {
3553

3654
this.authBpmMock.get200Response();

0 commit comments

Comments
 (0)