Comparing version 0.0.8 to 1.0.0
@@ -21,3 +21,3 @@ #! /usr/bin/env node | ||
.reset() | ||
.usage('Usage: gitio shrink <long url> [-c code] [-f] [--help]') | ||
.usage('Usage: gitio shrink <long url> [-c code] [--help]') | ||
.demand(2) | ||
@@ -30,9 +30,2 @@ .options({ | ||
type: 'string' | ||
}, | ||
f: { | ||
alias: 'force', | ||
description: 'Try to shorten link even if the custom code has been used previously.', | ||
demand: false, | ||
type: 'boolean', | ||
default: false | ||
} | ||
@@ -49,11 +42,12 @@ }) | ||
url: argv._[1], | ||
code: argv.code, | ||
force: argv.force | ||
}).then(function (result) { | ||
copyPaste.copy(result, function () { | ||
console.log('\n%s %s', result, chalk.dim('✔ copied to clipboard')); | ||
}); | ||
}).catch(function (err) { | ||
console.error(chalk.red('\n%s'), err); | ||
code: argv.code | ||
}, (err, result) => { | ||
if (err) { | ||
console.error(chalk.red('\n%s'), err); | ||
} else { | ||
copyPaste.copy(result, function () { | ||
console.log('\n%s %s', result, chalk.dim('✔ copied to clipboard')); | ||
}); | ||
} | ||
}); | ||
}; |
/** | ||
* The shrink command logic. Supports both Promises and callbacks. | ||
* | ||
* @module lib/shrink | ||
* @fileoverview The shrink command logic. | ||
* @author Nathan Buchar | ||
@@ -10,6 +8,11 @@ */ | ||
var Promise = require('promise'); | ||
let noop = require('no-op'); | ||
let request = require('request'); | ||
let schema = require('validate'); | ||
var extend = require('extend'); | ||
var request = require('request'); | ||
/** | ||
* @const {string} GITIO_API | ||
* @description The Git.io API location. | ||
*/ | ||
const GITIO_API = 'https://git.io/create'; | ||
@@ -19,43 +22,30 @@ /** | ||
* | ||
* @param {Object} payload - The form payload that will be sent to the API. | ||
* * {string} url - The long url to be shortened. | ||
* * {string} [code] - The custom code for the shortened URL. | ||
* * {boolean} [force] | ||
* @param {Function} [callback] | ||
* @returns {Promise} | ||
* @param {Object} payload | ||
* @param {Function} [callback=noop] | ||
*/ | ||
module.exports = function shrink(payload, callback) { | ||
return new Promise(function (resolve, reject) { | ||
if (payload.url && typeof payload.url === 'string') { | ||
return request.post({ | ||
url: 'http://git.io', | ||
form: extend(payload, { | ||
url: parseUrl(payload.url) | ||
}) | ||
}, function (err, res, body) { | ||
var statusCode = Number(res.headers.status.split(' ')[0]); | ||
var error; | ||
var result; | ||
callback = callback || noop; | ||
// Determine if there was an error. | ||
if (err) { | ||
error = new Error(err); | ||
} else if (statusCode !== 201) { | ||
error = new Error(res.body); | ||
} else { | ||
result = res.headers.location; | ||
} | ||
// Validate payload and return any errors. | ||
let errors = validatePayload(payload); | ||
// Handle Promise resolutions. | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
if (errors.length) { | ||
callback(new Error(errors[0].message)); | ||
return; | ||
} | ||
// Handle callback. | ||
if (callback) { | ||
callback(error, result); | ||
} | ||
}); | ||
// Make the shrink request. | ||
request.post({ | ||
url: GITIO_API, | ||
form: payload | ||
}, (err, res, body) => { | ||
let statusCode = Number(res.headers.status.split(' ')[0]); | ||
// Determine if there was an error. | ||
if (err) { | ||
callback(new Error(err)); | ||
} else if (statusCode !== 200) { | ||
callback(new Error(res.body)); | ||
} else { | ||
callback(null, 'https://git.io/' + res.body); | ||
} | ||
@@ -66,23 +56,30 @@ }); | ||
/** | ||
* Parses the URL so that it is in compliance with Git.io's backend. This | ||
* functions ensures that the URLs always starts wth `http://`, as the git.io | ||
* will not accept URLs that start with `//`, `http://`, or neither. | ||
* Validates the payload and verifies that there are no errors. | ||
* | ||
* @param {string} url | ||
* @returns {string} | ||
* @param {Object} payload | ||
* @returns {Array} | ||
*/ | ||
function parseUrl(url) { | ||
// Handle "//github.com". | ||
if (url.indexOf('//') === 0) { | ||
return 'https:' + url; | ||
} | ||
function validatePayload(payload) { | ||
let errors = schema({ | ||
url: { | ||
type: 'string', | ||
required: true, | ||
message: '"url" is required and must be a string.' | ||
}, | ||
code: { | ||
type: 'string', | ||
required: false, | ||
message: '"code" must be a string' | ||
} | ||
}).validate(payload); | ||
// Handle "http://github.com" or "github.com". | ||
if (url.indexOf('http') === 0) { | ||
return url.replace('http://', 'https://'); | ||
} else { | ||
return 'https://' + url; | ||
// Validate that the URL is HTTPS. | ||
if (payload.url && !payload.url.match(/^https:\/\/.+/)) { | ||
errors.push({ | ||
name: 'url', | ||
message: 'Only HTTPS is supported.' | ||
}); | ||
} | ||
return url; | ||
return errors; | ||
} |
{ | ||
"name": "node-gitio", | ||
"version": "0.0.8", | ||
"version": "1.0.0", | ||
"description": "A simple Node CLI wrapper for git.io.", | ||
@@ -13,2 +13,5 @@ "main": "index.js", | ||
], | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"repository": { | ||
@@ -26,5 +29,5 @@ "type": "git", | ||
"copy-paste": "^1.1.3", | ||
"extend": "^3.0.0", | ||
"promise": "^7.0.4", | ||
"no-op": "^1.0.3", | ||
"request": "^2.61.0", | ||
"validate": "^3.0.1", | ||
"yargs": "^3.25.0" | ||
@@ -31,0 +34,0 @@ }, |
# node-gitio [](https://travis-ci.org/nathanbuchar/node-gitio) | ||
A simple Node CLI wrapper for [http://git.io](http://git.io/). In Terminal, simply enter `gitio shrink` followed by the GitHub URL you would like to shrink and the shortened URL will be copied automatically to your clipboard. Read more about git.io [here](https://github.com/blog/985-git-io-github-url-shortener). | ||
A simple Node CLI wrapper for [http://git.io](http://git.io/). | ||
In Terminal, simply enter `gitio shrink` followed by the GitHub URL you would like to shrink and the shortened URL will be copied automatically to your clipboard. Read more about git.io [here](https://github.com/blog/985-git-io-github-url-shortener). | ||
Copying to clipboard is implemented via the [copy-paste](https://www.npmjs.com/package/copy-paste) package which supports OSX, Windows, Linux, and OpenBSD. | ||
@@ -10,11 +12,15 @@ | ||
*** | ||
### Install | ||
Install | ||
------- | ||
```bash | ||
$ npm install -g node-gitio | ||
``` | ||
npm install -g node-gitio | ||
``` | ||
### Quick Visual Guide | ||
Quick Visual Guide | ||
------------------ | ||
@@ -33,5 +39,5 @@ ```bash | ||
Usage | ||
----- | ||
### Usage | ||
``` | ||
@@ -51,6 +57,7 @@ Usage: gitio [--version] [--help] <command> [<args>] | ||
``` | ||
#### Shrink | ||
``` | ||
Usage: gitio shrink <long url> [-c code] [-f] [--help] | ||
Usage: gitio shrink <long url> [-c code] [--help] | ||
@@ -61,5 +68,2 @@ Options: | ||
--force, -f Try to shorten link even if the custom code has been used previously. | ||
(Optional) | ||
--help, -h Show the help menu. | ||
@@ -70,13 +74,9 @@ | ||
Node JS API | ||
----------- | ||
### Node JS API | ||
```javascript | ||
var gitio = require('node-gitio'); | ||
/** | ||
* Using callbacks. | ||
*/ | ||
gitio.shrink(payload, function (err, result) { | ||
gitio.shrink(payload, (err, result) => { | ||
if (!err) { | ||
@@ -87,12 +87,2 @@ console.log(result); | ||
/** | ||
* Using promises. | ||
*/ | ||
gitio.shrink(payload).then(function (result) { | ||
console.log(result); | ||
}, function (err) { | ||
console.error(err); | ||
}); | ||
``` | ||
@@ -104,9 +94,8 @@ | ||
|:---|:--:|:----------|:------:| | ||
|`url`|`string`|The GitHub URL to shorten.|Yes| | ||
|`code`|`string`|A custom code for the short link, e.g. http://git.io/mycode.|No| | ||
|`force`|`boolean`|Try to shorten link even if the custom code has been used previously.|No| | ||
|`url`|`string`|The GitHub URL to shorten. Only HTTPS is supported.|✓| | ||
|`code`|`string`|A custom code for the short link, e.g. http://git.io/mycode.|| | ||
### Examples | ||
#### Examples | ||
@@ -118,3 +107,3 @@ 1. Shorten `github.com/nathanbuchar/node-gitio`. | ||
# => http://git.io/vZ9RJ ✔ copied to clipboard | ||
# => https://git.io/vZ9RJ ✔ copied to clipboard | ||
``` | ||
@@ -127,3 +116,3 @@ | ||
# => http://git.io/nathan-gitio ✔ copied to clipboard | ||
# => https://git.io/nathan-gitio ✔ copied to clipboard | ||
``` | ||
@@ -138,7 +127,21 @@ | ||
url: 'github.com/nathanbuchar/node-gitio' | ||
}, function (err, result) { | ||
}, (err, result) => { | ||
if (!err) { | ||
console.log(result); | ||
// => https://git.io/vZ9RJ | ||
} | ||
}); | ||
``` | ||
*** | ||
Authors | ||
------- | ||
* [Nathan Buchar](mailto:hello@nathanbuchar.com) | ||
License | ||
------- | ||
MIT |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
138
8607
148
+ Addedno-op@^1.0.3
+ Addedvalidate@^3.0.1
+ Addedcomponent-type@1.0.0(transitive)
+ Addedeivindfjeldstad-dot@0.0.1(transitive)
+ Addedno-op@1.0.3(transitive)
+ Addedtypecast@0.0.1(transitive)
+ Addedvalidate@3.1.0(transitive)
- Removedextend@^3.0.0
- Removedpromise@^7.0.4
- Removedasap@2.0.6(transitive)
- Removedpromise@7.3.1(transitive)