restQiitaAPI.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // console.log(`${chunk}`);
  23. console.log(`${chunk}`);
  24. resolve(chunk);
  25. });
  26. });
  27. req.on('error', (e) => {
  28. console.error(`problem with request: ${e.message}`);
  29. });
  30. req.end();
  31. });
  32. };
  33. restQiitaAPI.getQiitaUser = function() {
  34. return new Promise((resolve, reject) => {
  35. restAPI('users', options)
  36. .then(function(user){
  37. resolve(JSON.parse(user.toString()));
  38. })
  39. .catch(function(err){
  40. reject(err);
  41. })
  42. });
  43. };
  44. // tags: restAPI('tags'),
  45. // templates: restAPI('templates'),
  46. // projects: restAPI('projects'),
  47. // users: restAPI('users'),
  48. // comments: restAPI(`items/${item_id}/comments`),
  49. // project_comments: restAPI(`projects/${project_id}/comments`),
  50. // itemreactions: restAPI(`items/${item_id}/reactions`),
  51. // comment_reactions: restAPI(`comments/${comment_id}/reactions`),
  52. // project_reactions: restAPI(`projects/${project_id}/reactions`),
  53. // items: restAPI('items'),
  54. return restQiitaAPI;
  55. }