Skip to content

Commit

Permalink
Use xo for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Aug 18, 2016
1 parent 386cb2a commit 95347ce
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 214 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ Deletes an alias and returns a status.
| --- | --- | --- |
| id | <code>String</code> | ID of alias |
| [callback] | <code>function</code> | Callback will be called with `(err, status)` |

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/now');
module.exports = require('./dist/now')
34 changes: 13 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
"description": "Node.js wrapper for the 𝚫 now API",
"main": "./dist/now.js",
"scripts": {
"prepublish": "./node_modules/.bin/babel -d ./dist src && npm run gen-doc",
"eslint": "./node_modules/.bin/eslint ./src",
"gen-doc": "./node_modules/.bin/jsdoc2md --src src/now.js -t README.hbs > README.md",
"test": "./node_modules/.bin/mocha test/index.js --compilers js:babel-register"
"prepublish": "babel -d ./dist src && npm run gen-doc",
"gen-doc": "jsdoc2md --src src/now.js -t README.hbs > README.md",
"test": "xo && mocha test/index.js --compilers js:babel-register"
},
"files": [
"dist"
],
"repository": "zeit/now-api",
"eslintConfig": {
"parser": "babel-eslint",
"extends": "airbnb",
"rules": {
"no-console": 0
},
"plugins": [
"xo": {
"esnext": true,
"space": true,
"semicolon": false,
"env": [
"node",
"mocha"
],
"env": {
"mocha": true
"rules": {
"quote-props": 0
}
},
"keywords": [
Expand All @@ -41,18 +39,12 @@
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-eslint": "^6.0.5",
"babel-preset-es2015": "^6.9.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"eslint": "^3.3.1",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-import": "^1.9.2",
"eslint-plugin-jsx-a11y": "^1.5.3",
"eslint-plugin-mocha": "^3.0.0",
"eslint-plugin-react": "^6.1.2",
"jsdoc-to-markdown": "^1.3.6",
"mocha": "^2.5.3"
"mocha": "^2.5.3",
"xo": "^0.16.0"
},
"dependencies": {
"axios": "^0.12.0"
Expand Down
176 changes: 105 additions & 71 deletions src/now.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
const path = require('path');
const os = require('os');
const path = require('path')
const os = require('os')

const axios = require('axios');
const axios = require('axios')

const ERROR = {
MISSING_ID: {
code: 'missing_id',
message: 'Missing `id` parameter',
message: 'Missing `id` parameter'
},
MISSING_FILE_ID: {
code: 'missing_file_id',
message: 'Missing `fileId` parameter',
message: 'Missing `fileId` parameter'
},
MISSING_BODY: {
code: 'missing_body',
message: 'Missing `body` parameter',
message: 'Missing `body` parameter'
},
MISSING_PACKAGE: {
code: 'missing_package',
message: 'Missing `package` object in body',
message: 'Missing `package` object in body'
},
MISSING_ALIAS: {
code: 'missing_body',
message: 'Missing `alias` parameter',
},
};
message: 'Missing `alias` parameter'
}
}

/**
* Tries to obtain the API token and returns it.
* If NOW_TOKEN isn't defined, it will search in the user's home directory
* @return {String} – now API Token
*/
function _getToken() {
let token = process.env.NOW_TOKEN;
let token = process.env.NOW_TOKEN

if (!token) {
try {
const configPath = path.join(os.homedir(), '.now.json');
token = require(configPath).token; // eslint-disable-line global-require
} catch (e) {
console.error(`Error: ${e}`);
const configPath = path.join(os.homedir(), '.now.json')
token = require(configPath).token // eslint-disable-line global-require
} catch (err) {
console.error(`Error: ${err}`)
}
}
return token;
}

return token
}

/**
* Initializes the API. Looks for token in ~/.now.json if none is provided.
Expand All @@ -57,48 +57,56 @@ function Now(token = _getToken()) {
'No token found! ' +
'Supply it as argument or use the NOW_TOKEN env variable. ' +
'"~/.now.json" will be used, if it\'s found in your home directory.'
);
)
}

if (!(this instanceof Now)) {
return new Now(token)
}
if (!(this instanceof Now)) return new Now(token);
this.token = token;

this.token = token

this.axios = axios.create({
baseURL: 'https://api.zeit.co/now',
timeout: 5000,
headers: { Authorization: `Bearer ${token}` },
});
headers: {
Authorization: `Bearer ${token}`
}
})
}

Now.prototype = {
// Checks if callback is present and fires it
handleCallback: function handleCallback(callback, err, data) {
if (typeof callback === 'function') {
callback(err, data);
callback(err, data)
}
},

// Handles errors with Promise and callback support
handleError: function handleError(err, callback) {
return new Promise((resolve, reject) => {
reject(err);
this.handleCallback(callback, err);
});
reject(err)
this.handleCallback(callback, err)
})
},

// Processes requests
handleRequest: function handleRequest(config, callback, selector) {
return new Promise((resolve, reject) => {
this.axios.request(config)
.then((res) => {
const data = selector ? res.data[selector] : res.data;
resolve(data);
this.handleCallback(callback, undefined, data);
.then(res => {
const data = selector ? res.data[selector] : res.data
resolve(data)
this.handleCallback(callback, undefined, data)
})

.catch(err => {
const errData = err.data.err ? err.data.err : err.data
reject(errData)
this.handleCallback(callback, errData)
})
.catch((err) => {
const errData = err.data.err ? err.data.err : err.data;
reject(errData);
this.handleCallback(callback, errData);
});
});
})
},

/**
Expand All @@ -110,8 +118,8 @@ Now.prototype = {
getDeployments: function getDeployments(callback) {
return this.handleRequest({
url: '/deployments',
method: 'get',
}, callback, 'deployments');
method: 'get'
}, callback, 'deployments')
},

/**
Expand All @@ -122,12 +130,14 @@ Now.prototype = {
* @see https://zeit.co/api#get-endpoint
*/
getDeployment: function getDeployment(id, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

return this.handleRequest({
url: `/deployments/${id}`,
method: 'get',
}, callback);
method: 'get'
}, callback)
},

/**
Expand All @@ -140,14 +150,19 @@ Now.prototype = {
* @see https://zeit.co/api#instant-endpoint
*/
createDeployment: function createDeployment(body, callback) {
if (!body) return this.handleError(ERROR.MISSING_BODY, callback);
if (!body.package) return this.handleError(ERROR.MISSING_PACKAGE, callback);
if (!body) {
return this.handleError(ERROR.MISSING_BODY, callback)
}

if (!body.package) {
return this.handleError(ERROR.MISSING_PACKAGE, callback)
}

return this.handleRequest({
url: '/deployments',
method: 'post',
data: body,
}, callback);
data: body
}, callback)
},

/**
Expand All @@ -158,12 +173,14 @@ Now.prototype = {
* @see https://zeit.co/api#rm-endpoint
*/
deleteDeployment: function deleteDeployment(id, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

return this.handleRequest({
url: `/deployments/${id}`,
method: 'delete',
}, callback);
method: 'delete'
}, callback)
},

/**
Expand All @@ -174,12 +191,14 @@ Now.prototype = {
* @see https://zeit.co/api#file-structure-endpoint
*/
getFiles: function getFiles(id, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

return this.handleRequest({
url: `/deployments/${id}/files`,
method: 'get',
}, callback);
method: 'get'
}, callback)
},

/**
Expand All @@ -191,13 +210,18 @@ Now.prototype = {
* @see https://zeit.co/api#file--endpoint
*/
getFile: function getFile(id, fileId, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!fileId) return this.handleError(ERROR.MISSING_FILE_ID, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

if (!fileId) {
return this.handleError(ERROR.MISSING_FILE_ID, callback)
}

return this.handleRequest({
url: `/deployments/${id}/files/${fileId}`,
method: 'get',
}, callback);
method: 'get'
}, callback)
},

/**
Expand All @@ -208,18 +232,19 @@ Now.prototype = {
* @see https://zeit.co/api#user-aliases
*/
getAliases: function getAliases(id, callback) {
let url = '/aliases';
let _callback = callback; /* eslint no-underscore-dangle: 0 */
let url = '/aliases'
let _callback = callback /* eslint no-underscore-dangle: 0 */

if (typeof id === 'function') {
_callback = id;
_callback = id
} else if (typeof id === 'string') {
url = `/deployments/${id}/aliases`;
url = `/deployments/${id}/aliases`
}

return this.handleRequest({
url,
method: 'get',
}, _callback, 'aliases');
method: 'get'
}, _callback, 'aliases')
},

/**
Expand All @@ -231,14 +256,21 @@ Now.prototype = {
* @see https://zeit.co/api#create-alias
*/
createAlias: function createAlias(id, alias, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!alias) return this.handleError(ERROR.MISSING_ALIAS, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

if (!alias) {
return this.handleError(ERROR.MISSING_ALIAS, callback)
}

return this.handleRequest({
url: `/deployments/${id}/aliases`,
method: 'post',
data: { alias },
}, callback);
data: {
alias
}
}, callback)
},

/**
Expand All @@ -249,13 +281,15 @@ Now.prototype = {
* @see https://zeit.co/api#delete-user-aliases
*/
deleteAlias: function deleteAlias(id, callback) {
if (!id) return this.handleError(ERROR.MISSING_ID, callback);
if (!id) {
return this.handleError(ERROR.MISSING_ID, callback)
}

return this.handleRequest({
url: `/aliases/${id}`,
method: 'delete',
}, callback);
},
};
method: 'delete'
}, callback)
}
}

module.exports = Now;
module.exports = Now
Loading

0 comments on commit 95347ce

Please sign in to comment.