Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added possibility to set request header #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ client.milestones.list({id: 1})

@see [Gitlab API document](https://github.com/gitlabhq/gitlabhq/tree/master/doc/api).

### Header

#### client.addHeader(key, value)

Set a header (for example the SUDO header) which will be added to every request.

```js
client.addHeader('SUDO', 'johnd');

// The note is added by user 'johnd'
client.issues.createNote({
issue_id: 'issue_id',
id: 'project_id',
body: 'Note from user johnd.'
});
```
#### client.removeHeader(key)

Remove a set header value

```js
client.removeHeader('SUDO');
```

### Project

https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/projects.md
Expand Down
44 changes: 42 additions & 2 deletions lib/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,42 @@ function Gitlab(options) {
this.merge_requests = this.mergeRequests;
// members => projectMembers
this.members = this.projectMembers;
// headers
this.headers = {};
}

util.inherits(Gitlab, RESTFulClient);

/**
* @method addHeader
* Add a header to the headers object
*
* @param {string} key The header key to set
* @param {string} value The header value to set
*/
var addHeader = function(key, value) {
this.headers[key] = value;
}

/**
* @method removeHeader
* Removes a header from the headers object
*
* @param {string} key The key of the header to remove
*/
var removeHeader = function(key) {
if (this.headers[key] !== undefined) {
delete this.headers[key];
}
}

Gitlab.prototype.setAuthentication = function (req) {
req.params.data.private_token = req.params.data.private_token || this.privateToken;

for (let prop in this.headers) {
req.params.headers[prop] = this.headers[prop];
}

return req;
};

Expand All @@ -55,10 +85,20 @@ Gitlab.create = function (options) {

Gitlab.createPromise = function (options) {
var client = Gitlab.create(options);
return require('./promisify')(client);
var promisified = require('./promisify')(client);

promisified.addHeader = addHeader.bind(client);
promisified.removeHeader = removeHeader.bind(client);

return promisified;
};

Gitlab.createThunk = function (options) {
var client = Gitlab.create(options);
return require('./thunkify')(client);
var thunked = require('./thunkify')(client);

thunked.addHeader = addHeader.bind(client);
thunked.removeHeader = removeHeader.bind(client);

return thunked;
};