Skip to content

Commit

Permalink
Add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderC committed Jun 25, 2019
1 parent fc9cc5a commit 4a05bc2
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 59 deletions.
42 changes: 42 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"env": {
"node": true
},
"plugins": [],
"extends": "airbnb-base",
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"comma-dangle": ["warn"],
"camelcase": [0],
"no-plusplus": [0],
"no-restricted-globals": [0],
"no-underscore-dangle": [0],
"no-trailing": 0,
"no-trailing-spaces": 0,
"radix": 0,
"import/no-dynamic-require": 0,
"template-curly-spacing": ["warn", "always"],
"no-restricted-syntax": 0,
"class-methods-use-this": 0,
"prefer-const": "warn",
"array-bracket-spacing": 0,
"no-await-in-loop": 0,
"global-require": 0,
"guard-for-in": 0,
"no-shadow": 0,
"no-param-reassign": 0,
"no-else-return": 0,
"consistent-return": 0,
"import/no-unresolved": 0,
"no-prototype-builtins": 0,
"max-len": [ "warn", { "code": 120 } ]
}
}
25 changes: 22 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "ubidots-node",
"version": "1.0.1",
"version": "1.2.0",
"description": "Unofficial Ubidots NodeJS API Client",
"main": "src/ubidots.js",
"scripts": {
"test": "$(npm bin)/mocha test --timeout 99999 --ui tdd --file test/_boot.js"
"test": "$(npm bin)/mocha test --timeout 99999 --ui tdd --file test/_boot.js",
"lint": "$(npm bin)/eslint src",
"lint-staged": "$(npm bin)/lint-staged"
},
"repository": {
"type": "git",
Expand All @@ -23,12 +25,29 @@
"homepage": "https://github.com/AlexanderC/ubidots#readme",
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.9.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"husky": "^2.3.0",
"lint-staged": "^8.1.7"
},
"dependencies": {
"axios": "^0.19.0",
"debug": "^4.1.0",
"merge-options": "^1.0.1",
"promise-retry": "^1.1.1"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
}
}
8 changes: 6 additions & 2 deletions src/api-definition/definition/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const variable = require('./variable');
const utils = require('./utils');

module.exports = {
auth, organization, user,
datasource, device, variable,
auth,
organization,
user,
datasource,
device,
variable,
utils,
};
44 changes: 21 additions & 23 deletions src/api-definition/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,30 @@ async function build(def, authToken = null, apiKey = null, ...args) {
opts.headers['X-Ubidots-ApiKey'] = apiKey;
}

if (opts.populate && typeof opts.populate == 'function') {
if (opts.populate && typeof opts.populate === 'function') {
await opts.populate.call(opts, ...args);
}

return opts;
}

module.exports = ext => {
return mergeOptions({
basepath: 'api/v1.6',
method: 'GET',
path: '',
useAPIKey: false,
headers: {
'X-Auth-Token': '',
'X-Ubidots-ApiKey': ''
},
async populate(opts) {
if (this.method.toLowerCase() === 'get') {
this.params = opts || {};
} else {
this.data = opts || {};
}
},
build,
validateStatus: Response.isStatusOk,
process: data => data,
}, ext);
};
module.exports = ext => mergeOptions({
basepath: 'api/v1.6',
method: 'GET',
path: '',
useAPIKey: false,
headers: {
'X-Auth-Token': '',
'X-Ubidots-ApiKey': '',
},
async populate(opts) {
if (this.method.toLowerCase() === 'get') {
this.params = opts || {};
} else {
this.data = opts || {};
}
},
build,
validateStatus: Response.isStatusOk,
process: data => data,
}, ext);
4 changes: 2 additions & 2 deletions src/api-definition/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ApiBase = {
};

module.exports.ApiBase = ApiBase;
module.exports.api = baseURL => {
module.exports.api = (baseURL) => {
const api = {};

for (const namespace in definitions) {
Expand All @@ -16,7 +16,7 @@ module.exports.api = baseURL => {
for (const name in definitions[namespace]) {
api[namespace][name] = mergeOptions(
{ baseURL },
definitions[namespace][name]
definitions[namespace][name],
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Api {
this.key = key;
this.opts = opts;

debug(`endpoints:${this.namespace}`, this.endpoints);
debug(`endpoints:${ this.namespace }`, this.endpoints);
}

/**
Expand All @@ -31,11 +31,11 @@ class Api {
}

return new Endpoint(
`${this.namespace}:${endpoint}`,
`${ this.namespace }:${ endpoint }`,
this._endpoints[endpoint],
this.token,
this.key,
this.opts
this.opts,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Endpoint {
this.definition,
this.token,
this.key,
...args
...args,
);

debug(`call:${ name }`, opts);
Expand Down
33 changes: 17 additions & 16 deletions src/error/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ApiError extends BaseError {
*/
constructor(endpoint, error) {
super(
`Request to "${endpoint.name}" ${endpoint.token ? 'w/' : 'wo/'} token failed: ` +
ApiError.details(error)
`Request to "${ endpoint.name }" ${ endpoint.token ? 'w/' : 'wo/' } token failed: ${
ApiError.details(error) }`,
);
}

Expand All @@ -25,23 +25,24 @@ class ApiError extends BaseError {
&& typeof error.response.data === 'object'
) {
switch (error.response.status) {
case 403:
msg = error.response.data.detail;
break;
case 400:
const issues = [];

for (const field in error.response.data) {
issues.push(`${field} (${error.response.data[field].join(', ')})`);
}

msg = `Invalid payload data- ${issues.join(', ')}`;
break;
default: msg = 'Unknown Error';
case 403:
msg = error.response.data.detail;
break;
case 400:
// eslint-disable-next-line no-case-declarations
const issues = [];

for (const field in error.response.data) {
issues.push(`${ field } (${ error.response.data[field].join(', ') })`);
}

msg = `Invalid payload data- ${ issues.join(', ') }`;
break;
default: msg = 'Unknown Error';
}
}

return `Failed with status ${error.response.status}. ${msg}`;
return `Failed with status ${ error.response.status }. ${ msg }`;
} else if (error.request) {
return 'No response received';
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/missing-api-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MissingApiEndpointError extends BaseError {
* @param {string} endpoint
*/
constructor(namespace, endpoint) {
super(`Missing API endpoint "${namespace}:${endpoint}"`);
super(`Missing API endpoint "${ namespace }:${ endpoint }"`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/error/missing-api-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MissingApiNamespaceError extends BaseError {
* @param {string} namespace
*/
constructor(namespace) {
super(`Missing API namespace "${namespace}"`);
super(`Missing API namespace "${ namespace }"`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Response {
const data = this._response.data || {};
const processor = this.endpoint.definition.process;

if (processor && typeof processor == 'function') {
if (processor && typeof processor === 'function') {
return processor(data);
}

Expand Down Expand Up @@ -54,7 +54,7 @@ class Response {

return new ApiError(
this.endpoint,
this._error || { response: this._response }
this._error || { response: this._response },
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ubidots.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Ubidots {
this._api[namespace],
this.token,
this.key,
this.opts
this.opts,
);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ class Ubidots {
retries: 5,
factor: 1.4,
minTimeout: 500,
}
},
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const debug = require('debug');
const pkg = require('../../package.json');
const path = require('path');
const pkg = require('../../package.json');

module.exports = file => debug(`${ pkg.name }:${ path.basename(file, '.js') }`);
5 changes: 3 additions & 2 deletions src/utils/string.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module.exports.interpolate = (template, vars) => {
for (const key in vars) {
template = template.replace(
new RegExp(`{\s*${key}\s*}`, 'ig'),
vars[key]
// eslint-disable-next-line no-useless-escape
new RegExp(`{\s*${ key }\s*}`, 'ig'),
vars[key],
);
}

Expand Down

0 comments on commit 4a05bc2

Please sign in to comment.