You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
carbon/lib/api.js

69 lines
1.8 KiB
JavaScript

import axios from 'axios'
import debounce from 'lodash.debounce'
import ms from 'ms'
8 years ago
const DOMAIN = process.browser ? document.location.origin : ''
const RATE_LIMIT_CODE = 420
8 years ago
const gistClient = axios.create({
baseURL: 'https://api.github.com',
timeout: 5000,
headers: {
Accept: 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
}
})
8 years ago
7 years ago
async function tweet(encodedImage) {
const processedData = encodedImage.split(',')[1]
7 years ago
return axios
.post(`${DOMAIN}/twitter`, { data: processedData })
.then(res => res.data.url)
.then(url => encodeURIComponent(`Built with #Carbon, by @dawn_labs ${url}`))
.then(uri => `https://twitter.com/intent/tweet?text=${uri}`)
.then(openTwitterUrl)
.catch(checkIfRateLimited)
}
async function image(url) {
return axios.post(`${DOMAIN}/image`, { url }).then(res => res.data.dataUrl)
}
7 years ago
const getGist = uid => {
7 years ago
return gistClient
.get(`/gists/${uid}`)
8 years ago
.then(res => res.data)
.then(gist => gist.files)
.then(files => files[Object.keys(files)[0]])
.then(file => file.content)
}
8 years ago
// private
7 years ago
function openTwitterUrl(twitterUrl) {
const width = 575,
height = 400
const left = (window.outerWidth - width) / 2
const right = (window.outerHeight - height) / 2
const opts = `status=1,width=${width},height=${height},top=${top},left=${left}`
7 years ago
window.open(twitterUrl, 'twitter', opts)
8 years ago
}
7 years ago
function checkIfRateLimited(err) {
if (err.response.status === RATE_LIMIT_CODE) {
7 years ago
alert(
"Oh no! Looks like to many people are trying to tweet right now and we've been rate limited. Try again soon or save and upload manually!"
)
return
}
throw err
7 years ago
}
export default {
getGist,
tweet: debounce(tweet, ms('5s'), { leading: true, trailing: true }),
image: debounce(image, ms('5s'), { leading: true, trailing: true })
}