Skip to content

Instantly share code, notes, and snippets.

@nanotroy
Created August 28, 2019 15:54
Show Gist options
  • Save nanotroy/79a6fbedaa6f5cbd46c70531b031def1 to your computer and use it in GitHub Desktop.
Save nanotroy/79a6fbedaa6f5cbd46c70531b031def1 to your computer and use it in GitHub Desktop.
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