restQiitaAPI.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. // Qiita API v2 documant https://qiita.com/api/v2/docs
  3. module.exports = function(Team, Token) {
  4. var restQiitaAPI = {};
  5. const https = require('https');
  6. const team = Team;
  7. const token = Token;
  8. var options = {
  9. protocol: 'https:',
  10. host: `${team}.qiita.com`,
  11. method: 'GET',
  12. headers: {
  13. 'Content-Type': 'application/json',
  14. 'authorization': `Bearer ${token}`
  15. }
  16. };
  17. function restAPI(path, options) {
  18. return new Promise((resolve, reject) => {
  19. options.path = `/api/v2/${path}`;
  20. const req = https.request(options, (res) => {
  21. res.on('data', (chunk) => {
  22. resolve(chunk);
  23. });
  24. });
  25. req.on('error', (e) => {
  26. console.error(`problem with request: ${e.message}`);
  27. });
  28. req.end();
  29. });
  30. };
  31. restQiitaAPI.getQiitaUser = function() {
  32. return new Promise((resolve, reject) => {
  33. restAPI('users', options)
  34. .then(function(user){
  35. resolve(JSON.parse(user.toString()));
  36. })
  37. .catch(function(err){
  38. reject(err);
  39. })
  40. });
  41. };
  42. restQiitaAPI.getQiitaPages = function() {
  43. return new Promise((resolve, reject) => {
  44. restAPI('items', options)
  45. .then(function(page){
  46. resolve(JSON.parse(page.toString()));
  47. })
  48. .catch(function(err){
  49. reject(err);
  50. })
  51. });
  52. };
  53. return restQiitaAPI;
  54. }