From 218d2ccdbcf59f2944d2800e458f07c927e52890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20J=C3=BClke?= Date: Fri, 17 Nov 2017 10:03:33 +0100 Subject: [PATCH 1/2] Added possibility to add and remove header to requests --- lib/gitlab.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/gitlab.js b/lib/gitlab.js index 9871775..aa7ddca 100644 --- a/lib/gitlab.js +++ b/lib/gitlab.js @@ -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; }; @@ -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; }; From 0c4df113b9da525346b30256a674dfbf098eda05 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 17 Nov 2017 10:17:39 +0100 Subject: [PATCH 2/2] updated readme --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index cceec83..5a4a01e 100644 --- a/README.md +++ b/README.md @@ -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