Comparing version 1.0.0 to 1.0.1
102
index.js
@@ -6,2 +6,14 @@ const URL = require("url").URL; | ||
const cuttlyErrorCode = [ | ||
"Not an url", | ||
"the link has already been shortened", | ||
"the entered link is not a link", | ||
"the preferred link name is already taken", | ||
"Invalid API key", | ||
"the link has not passed the validation. Includes invalid characters", | ||
"The link provided is from a blocked domain", | ||
"OK - the link has been shortened", | ||
"Link is local", | ||
]; | ||
const stringIsAValidUrl = (s) => { | ||
@@ -16,21 +28,29 @@ try { | ||
function CuttlyError(code) { | ||
this.name = "CuttlyError"; | ||
this.code = code; | ||
this.message = cuttlyErrorCode[code]; | ||
this.stack = new Error().stack; | ||
function CuttlyError(code, url) { | ||
var instance = new Error(cuttlyErrorCode[code]); | ||
instance.code = code; | ||
instance.cuttlyUrl = url; | ||
Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(instance, CuttlyError); | ||
} | ||
return instance; | ||
} | ||
CuttlyError.prototype = new Error(); | ||
const cuttlyErrorCode = [ | ||
"Not an url", | ||
"the link has already been shortened", | ||
"the entered link is not a link", | ||
"the preferred link name is already taken", | ||
"Invalid API key", | ||
"the link has not passed the validation. Includes invalid characters", | ||
"The link provided is from a blocked domain", | ||
"OK - the link has been shortened", | ||
]; | ||
CuttlyError.prototype = Object.create(Error.prototype, { | ||
constructor: { | ||
value: Error, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
}, | ||
}); | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(CuttlyError, Error); | ||
} else { | ||
CuttlyError.__proto__ = Error; | ||
} | ||
const buildShareUrl = (cuttlyKey, longurl) => { | ||
@@ -42,4 +62,4 @@ const url = cuttlyURL; | ||
querystring.stringify({ | ||
short: encodeURIComponent(longurl), | ||
key: cuttlyKey, | ||
short: encodeURI(longurl), | ||
}) | ||
@@ -62,22 +82,38 @@ ); | ||
const isLocalhost = /http(s?):\/\/(localhost|127.0.0.1)(.+)/; | ||
if (isLocalhost.test(longurl)) { | ||
resolve({ | ||
date: new Date(), | ||
shortLink: longurl, | ||
fullLink: longurl, | ||
title: "", | ||
status: 8, | ||
}); | ||
return; | ||
} | ||
const instance = axios.create({ | ||
headers: { Accept: "application/json, text/plain, */*" }, | ||
}); | ||
instance.get(buildShareUrl(key, longurl)).then( | ||
(response) => { | ||
const answer = response.data; | ||
if (answer.url.status === 7) { | ||
resolve(answer.url); | ||
} else if (answer.url.status === 1) { | ||
console.log(answer.url); | ||
reject(new CuttlyError(answer.url.status)); | ||
} else { | ||
reject(new CuttlyError(answer.url.status)); | ||
try { | ||
const destUrl = buildShareUrl(key, longurl); | ||
return instance.get(destUrl).then( | ||
(response) => { | ||
const answer = response.data; | ||
if (answer.url.status === 7) { | ||
resolve(answer.url); | ||
} else if (answer.url.status === 1) { | ||
reject(new CuttlyError(answer.url.status, destUrl)); | ||
} else { | ||
reject(new CuttlyError(answer.url.status, destUrl)); | ||
// reject(new Error(cuttlyErrorCode[answer.url.status])); | ||
} | ||
}, | ||
(error) => { | ||
reject(error); | ||
} | ||
}, | ||
(error) => { | ||
reject(error); | ||
} | ||
); | ||
); | ||
} catch (ex) { | ||
reject(ex); | ||
} | ||
}); |
{ | ||
"name": "cuttly", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Unofficial API for cutt.ly url shortener", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39376
105