adonis-kraken
Advanced tools
Comparing version 0.0.11 to 0.0.12
{ | ||
"name": "adonis-kraken", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Simplifies working with Kraken.io (image optimisation service) through Adonis", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,5 +14,16 @@ 'use strict' | ||
this.options = inputConfig.merge('kraken', Config.get('kraken')) | ||
this.auth = { | ||
api_key: this.options.api_key || '', | ||
api_secret: this.options.api_secret || '' | ||
} | ||
/** | ||
* Attaches authentication data to request body | ||
* | ||
* @param {Object} body | ||
*/ | ||
_attachAuth (body = {}) { | ||
body = { | ||
auth: { | ||
api_key: this.options.api_key || '', | ||
api_secret: this.options.api_secret || '' | ||
}, | ||
...body | ||
} | ||
@@ -22,2 +33,29 @@ } | ||
/** | ||
* Gets an streamable instance of a file | ||
* | ||
* @param {*} file | ||
*/ | ||
_getStreamedFile (file) { | ||
return (file && file instanceof stream.Stream) ? file : fs.createReadStream(file) | ||
} | ||
/** | ||
* Gets the filename from a streamed file | ||
* | ||
* @param {Stream} file | ||
*/ | ||
_getFileName (file) { | ||
return file.filename || 'file' | ||
} | ||
/** | ||
* Gets the auto-generated headers from a form | ||
* | ||
* @param {FormData} form | ||
*/ | ||
_getFormHeaders (form) { | ||
return { headers: form.getHeaders() } | ||
} | ||
/** | ||
* Rounds bytes to nearest KB, MB, GB etc | ||
@@ -44,5 +82,3 @@ * | ||
try { | ||
body.auth = this.auth | ||
const { data } = await axios.post('https://api.kraken.io/v1/url', body) | ||
return data | ||
return axios.post('https://api.kraken.io/v1/url', this._attachAuth(body)) | ||
} catch (error) { | ||
@@ -57,19 +93,15 @@ console.log(error) | ||
* | ||
* @param {Object} opts | ||
* @param {Object} body | ||
*/ | ||
async upload (opts = {}) { | ||
opts.auth = this.auth | ||
let form = new FormData() | ||
form.append('file', (opts.file && opts.file instanceof stream.Stream) | ||
? opts.file | ||
: fs.createReadStream(opts.file) | ||
) | ||
delete opts.file | ||
form.append('data', JSON.stringify(opts)) | ||
return axios.post('https://api.kraken.io/v1/upload', form, { headers: form.getHeaders() }) | ||
.then((response) => response) | ||
.catch((error) => { | ||
console.log(error) | ||
return error | ||
}) | ||
async upload (body = {}) { | ||
try { | ||
let form = new FormData() | ||
form.append('file', this._getStreamedFile(body.file), this._getFileName(this._getStreamedFile(body.file))) | ||
delete body.file | ||
form.append('data', JSON.stringify(this._attachAuth(body))) | ||
return axios.post('https://api.kraken.io/v1/upload', form, this._getFormHeaders(form)) | ||
} catch (error) { | ||
console.log(error) | ||
return error | ||
} | ||
} | ||
@@ -76,0 +108,0 @@ |
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
45905
201