Skip to content

Instantly share code, notes, and snippets.

@apertureless
Created April 19, 2017 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apertureless/15c2089053eb854e2ca3fedb19dd7112 to your computer and use it in GitHub Desktop.
Save apertureless/15c2089053eb854e2ca3fedb19dd7112 to your computer and use it in GitHub Desktop.
medium post
<script>
import axios from 'axios'
import LineChart from '@/components/LineChart'
export default {
components: {
LineChart
},
props: {},
data () {
return {
package: null,
packageName: '',
period: 'last-month',
loaded: false,
downloads: [],
labels: [],
showError: false,
errorMessage: 'Please enter a package name'
}
},
mounted () {
if (this.$route.params.package) {
this.package = this.$route.params.package
this.requestData()
}
},
methods: {
resetState () {
this.loaded = false
this.showError = false
},
requestData () {
if (this.package === null || this.package === '' || this.package === 'undefined') {
this.showError = true
return
}
this.resetState()
axios.get(`https://api.npmjs.org/downloads/range/${this.period}/${this.package}`)
.then(response => {
console.log(response.data)
this.downloads = response.data.downloads.map(download => download.downloads)
this.labels = response.data.downloads.map(download => download.day)
this.packageName = response.data.package
this.setURL()
this.loaded = true
})
.catch(err => {
this.errorMessage = err.response.data.error
this.showError = true
})
},
setURL () {
history.pushState({ info: `npm-stats ${this.package}` }, this.package, `/#/${this.package}`)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment