Skip to content

Commit

Permalink
Fix linting errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
danyalaytekin committed Nov 9, 2023
1 parent 2081b23 commit 6553217
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 213 deletions.
121 changes: 61 additions & 60 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,55 @@
// This file is part of Pa11y Webservice Node.js Client.
//
//
// Pa11y Webservice Node.js Client is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// Pa11y Webservice Node.js Client is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with Pa11y Webservice Node.js Client. If not, see <http://www.gnu.org/licenses/>.

'use strict';

var request = require('request');

module.exports = client;
const request = require('request');

// Create a web-service client
function client (root) {
function client(root) {
return {

tasks: {

create: function (task, done) {
post(root + 'tasks', task, done);
create(task, done) {
post(`${root}tasks`, task, done);
},

get: function (query, done) {
get(root + 'tasks', query, done);
get(query, done) {
get(`${root}tasks`, query, done);
},

results: function (query, done) {
get(root + 'tasks/results', query, done);
results(query, done) {
get(`${root}tasks/results`, query, done);
}

},

task: function (id) {
task(id) {
return {

get: function (query, done) {
get(root + 'tasks/' + id, query, done);
get(query, done) {
get(`${root}tasks/${id}`, query, done);
},

edit: function (edits, done) {
patch(root + 'tasks/' + id, edits, done);
edit(edits, done) {
patch(`${root}tasks/${id}`, edits, done);
},

remove: function (done) {
del(root + 'tasks/' + id, null, done);
remove(done) {
del(`${root}tasks/${id}`, null, done);
},

run: function (done) {
post(root + 'tasks/' + id + '/run', null, done);
run(done) {
post(`${root}tasks/${id}/run`, null, done);
},

results: function (query, done) {
get(root + 'tasks/' + id + '/results', query, done);
results(query, done) {
get(`${root}tasks/${id}/results`, query, done);
},

result: function (rid) {
result(rid) {
return {

get: function (query, done) {
get(root + 'tasks/' + id + '/results/' + rid, query, done);
get(query, done) {
get(`${root}tasks/${id}/results/${rid}`, query, done);
}
};
}
Expand All @@ -75,37 +58,55 @@ function client (root) {
};
}

function del (url, query, done) {
req('DELETE', url, query, null, done);
function del(url, query, done) {
call({
method: 'DELETE',
url,
query
}, done);
}

function get (url, query, done) {
req('GET', url, query, null, done);
function get(url, query, done) {
call({
method: 'GET',
url,
query
}, done);
}

function patch (url, body, done) {
req('PATCH', url, null, body, done);
function patch(url, body, done) {
call({
method: 'PATCH',
url,
body
}, done);
}

function post (url, body, done) {
req('POST', url, null, body, done);
function post(url, body, done) {
call({
method: 'POST',
url,
body
}, done);
}

function req (method, url, query, body, done) {
function call({method, url, query, body}, done) {
request({
method: method,
url: url,
method,
url,
qs: query,
body: body,
body,
json: true
}, function (err, res, body) {
if (err) {
return done(err);
}, (error, response, responseBody) => {
if (error) {
return done(error);
}
if (res.statusCode > 299) {
var message = (body && body.message ? body.message : 'Error ' + res.statusCode);
if (response.statusCode >= 300) {
const message = (responseBody?.message || `Error ${response.statusCode}`);
return done(new Error(message));
}
done(null, body);
done(null, responseBody);
});
}

module.exports = client;
29 changes: 14 additions & 15 deletions test/mock/request-pa11y-webservice.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
// This file is part of Pa11y Webservice Node.js Client.
//
//
// Pa11y Webservice Node.js Client is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// Pa11y Webservice Node.js Client is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with Pa11y Webservice Node.js Client. If not, see <http://www.gnu.org/licenses/>.

'use strict';

const sinon = require('sinon');

module.exports = sinon.spy(function (opts, done) {
var endpoint = opts.method + ' ' + opts.url;
module.exports = sinon.spy((opts, done) => {
const endpoint = `${opts.method} ${opts.url}`;
if (mockEndpoints[endpoint]) {
mockEndpoints[endpoint](opts.qs, opts.body, done);
} else {
Expand All @@ -32,11 +31,11 @@ module.exports = sinon.spy(function (opts, done) {

const mockEndpoints = {

'GET http://pa11y-ws/tasks': function (query, body, done) {
'GET http://pa11y-ws/tasks'(query, body, done) {
return done(null, {statusCode: 200}, []);
},

'POST http://pa11y-ws/tasks': function (query, body, done) {
'POST http://pa11y-ws/tasks'(query, body, done) {
if (body.name && body.url && body.standard) {
return done(null, {statusCode: 201}, {
id: 'task1',
Expand All @@ -53,7 +52,7 @@ const mockEndpoints = {
});
},

'GET http://pa11y-ws/tasks/results': function (query, body, done) {
'GET http://pa11y-ws/tasks/results'(query, body, done) {
if (query.foo) {
return done(null, {statusCode: 400}, {
code: 400,
Expand All @@ -64,23 +63,23 @@ const mockEndpoints = {
return done(null, {statusCode: 200}, []);
},

'GET http://pa11y-ws/tasks/task1': function (query, body, done) {
'GET http://pa11y-ws/tasks/task1'(query, body, done) {
return done(null, {statusCode: 200}, mockTask);
},

'PATCH http://pa11y-ws/tasks/task1': function (query, body, done) {
'PATCH http://pa11y-ws/tasks/task1'(query, body, done) {
return done(null, {statusCode: 204}, mockTask);
},

'DELETE http://pa11y-ws/tasks/task1': function (query, body, done) {
'DELETE http://pa11y-ws/tasks/task1'(query, body, done) {
return done(null, {statusCode: 204}, null);
},

'POST http://pa11y-ws/tasks/task1/run': function (query, body, done) {
'POST http://pa11y-ws/tasks/task1/run'(query, body, done) {
return done(null, {statusCode: 202}, null);
},

'GET http://pa11y-ws/tasks/task1/results': function (query, body, done) {
'GET http://pa11y-ws/tasks/task1/results'(query, body, done) {
if (query.foo) {
return done(null, {statusCode: 400}, {
code: 400,
Expand All @@ -91,7 +90,7 @@ const mockEndpoints = {
return done(null, {statusCode: 200}, []);
},

'GET http://pa11y-ws/tasks/task1/results/result1': function (query, body, done) {
'GET http://pa11y-ws/tasks/task1/results/result1'(query, body, done) {
return done(null, {statusCode: 200}, mockResult);
}

Expand Down
Loading

0 comments on commit 6553217

Please sign in to comment.