Created
August 28, 2019 15:54
-
-
Save nanotroy/79a6fbedaa6f5cbd46c70531b031def1 to your computer and use it in GitHub Desktop.
This file contains 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
const axios = require('axios'); | |
const querystring = require('querystring'); | |
const apiUrl = 'https://api.nytimes.com/svc'; | |
const apikey = 'your api key'; | |
export const nytMixin = { | |
methods: { | |
getArticles(section) { | |
return axios.get(`${apiUrl}/topstories/v2/${section}.json?api-key=${apikey}`); | |
}, | |
searchArticles(data) { | |
let params = Object.assign({}, data); | |
params['api-key'] = apikey; | |
Object.keys(params).forEach(key => { | |
if (!params[key]) { | |
delete params[key]; | |
} | |
}) | |
const queryString = querystring.stringify(params); | |
return axios.get(`${apiUrl}/search/v2/articlesearch.json?${queryString}`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment