Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #186 from Alfresco/development
Browse files Browse the repository at this point in the history
1.2.0
  • Loading branch information
magemello committed Feb 27, 2017
2 parents 95b754b + d15eba1 commit 808602c
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 474 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
</p>

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

<a name="1.1.1"></a>
# [1.1.1](https://github.com/Alfresco/alfresco-js-api/releases/tag/1.1.1) (27-01-2017)
Expand Down Expand Up @@ -33,7 +38,7 @@ _This project provides a JavaScript client API into the v1 Alfresco REST API_
hostBpm: 'http://127.0.0.1:9999',
contextRootBpm: 'activiti-custom-root'
});

this.bpmAuth.login('admin', 'admin');
```

Expand Down Expand Up @@ -275,7 +280,7 @@ Before:
```javascript
this.alfrescoJsApi = new AlfrescoApi({ host :''http://127.0.0.1:8080', ticket :'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1'});
```
After:
```javascript
Expand All @@ -286,7 +291,7 @@ this.alfrescoJsApi = new AlfrescoApi({ ticketEcm:'TICKET_4479f4d3bb155195879bfbb
//Login ticket BPM
this.alfrescoJsApi = new AlfrescoApi({ ticketBpm: 'Basic YWRtaW46YWRtaW4=', hostBpm:'http://127.0.0.1:9999'});

//Login ticket ECM and BPM
//Login ticket ECM and BPM
this.alfrescoJsApi = new AlfrescoApi({ ticketEcm:'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1', ticketBpm: 'Basic YWRtaW46YWRtaW4=', hostEcm:'http://127.0.0.1:8080', hostBpm:'http://127.0.0.1:9999'});
```
Expand Down
861 changes: 480 additions & 381 deletions dist/alfresco-js-api.js

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions dist/alfresco-js-api.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfresco-js-api",
"version": "1.1.1",
"version": "1.2.0",
"description": "JavaScript client library for the Alfresco REST API",
"author": "Alfresco Software, Ltd.",
"main": "main.js",
Expand All @@ -27,7 +27,7 @@
"url": "https://github.com/Alfresco/alfresco-js-api/issues"
},
"dependencies": {
"superagent": "1.7.1",
"superagent": "3.4.1",
"event-emitter": "^0.3.4",
"lodash": "^4.13.1",
"nock": "8.1.0"
Expand Down
4 changes: 3 additions & 1 deletion src/alfresco-activiti-rest-api/src/api/ContentApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@
var contentTypes = ['application/json'];
var accepts = ['application/json'];
var returnType = Object;
var contextRoot = null;
var responseType = 'blob';

return this.apiClient.callApi(
'/api/enterprise/content/{contentId}/raw', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts, returnType
authNames, contentTypes, accepts, returnType, contextRoot, responseType
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/alfrescoApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class AlfrescoApi {
* @returns {Promise} A promise that returns {new authentication ticket} if resolved and {error} if rejected.
* */
login(username, password) {

if (username) {
username = username.trim();
}

if (this._isBpmConfiguration()) {
var bpmPromise = this.bpmAuth.login(username, password);

Expand Down
150 changes: 83 additions & 67 deletions src/alfrescoApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ class AlfrescoApiClient extends ApiClient {
* @param {Array.<String>} accepts An array of acceptable response MIME types.
* @param {(String|Array|ObjectFunction)} returnType The required type to return; can be a string for simple types or the
* @param {(String)} contextRoot alternative contextRoot
* @param {(String)} responseType is an enumerated value that returns the type of the response.
* It also lets the author change the response type to one "arraybuffer", "blob", "document",
* "json", or "text".
* If an empty string is set as the value of responseType, it is assumed as type "text".
* constructor for a complex type. * @returns {Promise} A Promise object.
*/
callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, authNames,
contentTypes, accepts, returnType, contextRoot) {
contentTypes, accepts, returnType, contextRoot, responseType) {

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

var request = superagent(httpMethod, url);

// apply authentications
this.applyAuthToRequest(request, ['basicAuth']);

// set query parameters
request.query(this.normalizeParams(queryParams));

// set header parameters
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));

if (this.isBpmRequest() && this.isCsrfEnabled()) {
this.setCsrfToken(request);
}

// add cookie for activiti
if (this.isBpmRequest()) {
request._withCredentials = true;
if (this.authentications.cookie) {
if (this.isNodeEnv()) {
request.set('Cookie', this.authentications.cookie);
}
}
}

// set request timeout
request.timeout(this.timeout);

var contentType = this.jsonPreferredMime(contentTypes);

if (contentType && contentType !== 'multipart/form-data') {
request.type(contentType);
} else if (!request.header['Content-Type'] && contentType !== 'multipart/form-data') {
request.type('application/json');
}

if (contentType === 'application/x-www-form-urlencoded') {
request.send(this.normalizeParams(formParams)).on('progress', (event)=> {
this.progress(event, eventEmitter);
});
} else if (contentType === 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
if (_formParams.hasOwnProperty(key)) {
if (this.isFileParam(_formParams[key])) {
// file field
request.attach(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
this.progress(event, eventEmitter);
});
} else {
request.field(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
this.progress(event, eventEmitter);
});
}
}
}
} else if (bodyParam) {
request.send(bodyParam).on('progress', (event)=> {
this.progress(event, eventEmitter);
});
}

var accept = this.jsonPreferredMime(accepts);
if (accept) {
request.accept(accept);
}
var request = this.buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
contentTypes, accepts, responseType, eventEmitter);

this.promise = new Promise((resolve, reject) => {
request.end((error, response) => {
Expand Down Expand Up @@ -250,6 +190,82 @@ class AlfrescoApiClient extends ApiClient {
});
return url;
}

buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
contentTypes, accepts, responseType, eventEmitter) {
var request = superagent(httpMethod, url);

// apply authentications
this.applyAuthToRequest(request, ['basicAuth']);

// set query parameters
request.query(this.normalizeParams(queryParams));

// set header parameters
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));

if (this.isBpmRequest() && this.isCsrfEnabled()) {
this.setCsrfToken(request);
}

// add cookie for activiti
if (this.isBpmRequest()) {
request._withCredentials = true;
if (this.authentications.cookie) {
if (this.isNodeEnv()) {
request.set('Cookie', this.authentications.cookie);
}
}
}

// set request timeout
request.timeout(this.timeout);

if (responseType) {
request._responseType = responseType;
}

var contentType = this.jsonPreferredMime(contentTypes);

if (contentType && contentType !== 'multipart/form-data') {
request.type(contentType);
} else if (!request.header['Content-Type'] && contentType !== 'multipart/form-data') {
request.type('application/json');
}

if (contentType === 'application/x-www-form-urlencoded') {
request.send(this.normalizeParams(formParams)).on('progress', (event)=> {
this.progress(event, eventEmitter);
});
} else if (contentType === 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
if (_formParams.hasOwnProperty(key)) {
if (this.isFileParam(_formParams[key])) {
// file field
request.attach(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
this.progress(event, eventEmitter);
});
} else {
request.field(key, _formParams[key]).on('progress', (event)=> {// jshint ignore:line
this.progress(event, eventEmitter);
});
}
}
}
} else if (bodyParam) {
request.send(bodyParam).on('progress', (event)=> {
this.progress(event, eventEmitter);
});
}

var accept = this.jsonPreferredMime(accepts);
if (accept) {
request.accept(accept);
}

return request;
}
}

Emitter(AlfrescoApiClient.prototype); // jshint ignore:line
Expand Down
1 change: 1 addition & 0 deletions src/bpmAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class BpmAuth extends AlfrescoApiClient {
* */
setTicket(ticket) {
this.authentications.basicAuth.ticket = ticket;
this.authentications.basicAuth.password = null;
this.ticket = ticket;
}

Expand Down
30 changes: 29 additions & 1 deletion test/alfrescoApiClient.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*global describe, it */

var ApiClient = require('../src/alfresco-core-rest-api/src/ApiClient');
var ApiClient = require('../src/alfrescoApiClient');
var chai = require('chai');
var expect = chai.expect;

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

describe('type conversion', function() {

var client = new ApiClient();

it('should create a request with response type blob', function() {

var bodyParam = null;

var queryParams = {
};
var headerParams = {
};
var formParams = {
};

var contentTypes = ['application/json'];
var accepts = ['application/json'];
var responseType = 'blob';
var url = '/fake-api/enterprise/process-instances/';
var httpMethod = 'GET';

var response = client.buildRequest(httpMethod, url, queryParams, headerParams, formParams, bodyParam,
contentTypes, accepts, responseType, null);

expect(response.url).equal('/fake-api/enterprise/process-instances/');
expect(response.header.Accept).equal('application/json');
expect(response.header['Content-Type']).equal('application/json');
expect(response._responseType).equal('blob');
});

it('should return strings as a string', function() {
var testData = 'Example String';
expect(ApiClient.convertToType(testData, 'String')).equal(testData);
Expand Down
18 changes: 18 additions & 0 deletions test/bpmAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('Bpm Auth test', function () {

});

it('login password should be removed after login', function (done) {

this.authBpmMock.get200Response();

this.bpmAuth = new BpmAuth({
hostBpm: this.hostBpm,
contextRootBpm: 'activiti-app'
});

this.bpmAuth.login('admin', 'admin').then((data) => {
expect(data).to.be.equal('Basic YWRtaW46YWRtaW4=');
expect(this.bpmAuth.authentications.basicAuth.password).to.be.not.equal('admin');
done();
}, function () {
});

});

it('isLoggedIn should return true if the api is logged in', function (done) {

this.authBpmMock.get200Response();
Expand Down
17 changes: 17 additions & 0 deletions test/ecmAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ describe('Ecm Auth test', function () {

});

it('login password should be removed after login', function (done) {

this.authEcmMock.get201Response();

this.ecmAuth = new EcmAuth({
contextRoot: 'alfresco',
hostEcm: this.hostEcm
});

this.ecmAuth.login('admin', 'admin').then((data) => {
expect(this.ecmAuth.authentications.basicAuth.password).to.be.not.equal('admin');
done();
}, ()=> {
});

});

it('isLoggedIn should return true if the api is logged in', function (done) {

this.authEcmMock.get201Response();
Expand Down

0 comments on commit 808602c

Please sign in to comment.