Created
June 13, 2018 13:57
-
-
Save hypervillain/a83e957d85cd45e801a600def352e418 to your computer and use it in GitHub Desktop.
Call Uploadcare with cross-fetch, failing on GCF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// line 41: public & private are your Uploadcare API keys | |
/* | |
Call is nodeJS script (pass) : | |
async function main() { | |
const res = await pushBatchUrlsToUploadCare(['123']); | |
console.log(res); // { err: { '123': 'Invalid' } } | |
} | |
*/ | |
/* | |
Call in Graphcool resolver (fail) : | |
module.exports = async function(event) { | |
try { | |
const res = await pushBatchUrlsToUploadCare(batch); | |
return { data: res }; | |
} catch (e) { | |
... | |
} | |
} | |
*/ | |
const fetch = require('cross-fetch'); | |
async function pushBatchUrlsToUploadCare(batch = ['uuid-example']) { | |
try { | |
if (!batch || !batch.length) { | |
return { data: [] }; | |
} | |
const batchStr = JSON.stringify(batch); | |
const res = await fetch('https://api.uploadcare.com/files/storage/', { | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Uploadcare.Simple ${public}:${private}` | |
}, | |
body: batchStr, | |
}); | |
const responseData = res.json(); | |
return responseData.then(data => { | |
if (!data.problems || !data.result) { | |
return { err: 'Misformatted response and/or Batch string' }; | |
} | |
return { data: data.result }; | |
}) | |
} catch (err) { | |
console.error(err); | |
return { err: 'Network request to Uploadcare failed' }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment