rest-qiita-API.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const debug = require('debug')('growi:service:GlobalNotification');
  2. function getAxios(team, token) {
  3. return require('axios').create({
  4. baseURL: `https://${team}.qiita.com/api/v2`,
  5. headers: {
  6. 'Content-Type': 'application/json',
  7. 'X-Requested-With': 'XMLHttpRequest',
  8. 'authorization': `Bearer ${token}`
  9. },
  10. responseType: 'json'
  11. });
  12. }
  13. /**
  14. * the service class of restQiitaAPI
  15. * Qiita API v2 documant https://qiita.com/api/v2/docs
  16. */
  17. class RestQiitaAPIService {
  18. constructor(crowi) {
  19. this.crowi = crowi;
  20. this.config = crowi.getConfig();
  21. this.team = this.config.crowi['importer:qiita:team_name'];
  22. this.token = this.config.crowi['importer:qiita:access_token'];
  23. this.axios = getAxios(this.team, this.token);
  24. }
  25. /**
  26. * get Qiita API
  27. * @memberof RestQiitaAPI
  28. * @param {string} path
  29. */
  30. async restAPI(path) {
  31. return this.axios.get(path)
  32. .then(function(res) {
  33. const data = res.data;
  34. const total = res.headers['total-count'];
  35. return {data, total};
  36. });
  37. }
  38. /**
  39. * get Qiita user
  40. * @memberof RestQiitaAPI
  41. */
  42. async getQiitaUser() {
  43. const res = await this.restAPI('/users');
  44. const user = res.data;
  45. if (user.length > 0) {
  46. return user;
  47. }
  48. }
  49. /**
  50. * get Qiita pages
  51. * @memberof RestQiitaAPI
  52. * @param {string} pageNum
  53. * @param {string} per_page
  54. */
  55. async getQiitaPages(pageNum, per_page) {
  56. const res = await this.restAPI(`/items?page=${pageNum}&per_page=${per_page}`);
  57. const pages = res.data;
  58. const total = res.total;
  59. if (pages.length > 0) {
  60. return {pages, total};
  61. }
  62. }
  63. }
  64. module.exports = RestQiitaAPIService;