ChatClient.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const request = require('request').defaults({
  2. baseUrl: 'http://localhost:3010',
  3. })
  4. async function asyncRequest(options) {
  5. return new Promise((resolve, reject) => {
  6. request(options, (err, response, body) => {
  7. if (err) {
  8. reject(err)
  9. } else {
  10. resolve({ response, body })
  11. }
  12. })
  13. })
  14. }
  15. async function sendGlobalMessage(projectId, userId, content) {
  16. return asyncRequest({
  17. method: 'post',
  18. url: `/project/${projectId}/messages`,
  19. json: {
  20. user_id: userId,
  21. content,
  22. },
  23. })
  24. }
  25. async function getGlobalMessages(projectId) {
  26. return asyncRequest({
  27. method: 'get',
  28. url: `/project/${projectId}/messages`,
  29. json: true,
  30. })
  31. }
  32. async function sendMessage(projectId, threadId, userId, content) {
  33. return asyncRequest({
  34. method: 'post',
  35. url: `/project/${projectId}/thread/${threadId}/messages`,
  36. json: {
  37. user_id: userId,
  38. content,
  39. },
  40. })
  41. }
  42. async function getThreads(projectId) {
  43. return asyncRequest({
  44. method: 'get',
  45. url: `/project/${projectId}/threads`,
  46. json: true,
  47. })
  48. }
  49. async function resolveThread(projectId, threadId, userId) {
  50. return asyncRequest({
  51. method: 'post',
  52. url: `/project/${projectId}/thread/${threadId}/resolve`,
  53. json: {
  54. user_id: userId,
  55. },
  56. })
  57. }
  58. async function reopenThread(projectId, threadId) {
  59. return asyncRequest({
  60. method: 'post',
  61. url: `/project/${projectId}/thread/${threadId}/reopen`,
  62. })
  63. }
  64. async function deleteThread(projectId, threadId) {
  65. return asyncRequest({
  66. method: 'delete',
  67. url: `/project/${projectId}/thread/${threadId}`,
  68. })
  69. }
  70. async function editMessage(projectId, threadId, messageId, content) {
  71. return asyncRequest({
  72. method: 'post',
  73. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  74. json: {
  75. content,
  76. },
  77. })
  78. }
  79. async function deleteMessage(projectId, threadId, messageId) {
  80. return asyncRequest({
  81. method: 'delete',
  82. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  83. })
  84. }
  85. module.exports = {
  86. sendGlobalMessage,
  87. getGlobalMessages,
  88. sendMessage,
  89. getThreads,
  90. resolveThread,
  91. reopenThread,
  92. deleteThread,
  93. editMessage,
  94. deleteMessage,
  95. }