ChatClient.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Request from 'request'
  2. const request = Request.defaults({
  3. baseUrl: 'http://127.0.0.1:3010',
  4. })
  5. async function asyncRequest(options) {
  6. return new Promise((resolve, reject) => {
  7. request(options, (err, response, body) => {
  8. if (err) {
  9. reject(err)
  10. } else {
  11. resolve({ response, body })
  12. }
  13. })
  14. })
  15. }
  16. export async function sendGlobalMessage(projectId, userId, content) {
  17. return asyncRequest({
  18. method: 'post',
  19. url: `/project/${projectId}/messages`,
  20. json: {
  21. user_id: userId,
  22. content,
  23. },
  24. })
  25. }
  26. export async function getGlobalMessages(projectId) {
  27. return asyncRequest({
  28. method: 'get',
  29. url: `/project/${projectId}/messages`,
  30. json: true,
  31. })
  32. }
  33. export async function sendMessage(projectId, threadId, userId, content) {
  34. return asyncRequest({
  35. method: 'post',
  36. url: `/project/${projectId}/thread/${threadId}/messages`,
  37. json: {
  38. user_id: userId,
  39. content,
  40. },
  41. })
  42. }
  43. export async function getThreads(projectId) {
  44. return asyncRequest({
  45. method: 'get',
  46. url: `/project/${projectId}/threads`,
  47. json: true,
  48. })
  49. }
  50. export async function resolveThread(projectId, threadId, userId) {
  51. return asyncRequest({
  52. method: 'post',
  53. url: `/project/${projectId}/thread/${threadId}/resolve`,
  54. json: {
  55. user_id: userId,
  56. },
  57. })
  58. }
  59. export async function editMessage(projectId, threadId, messageId, content) {
  60. return asyncRequest({
  61. method: 'post',
  62. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  63. json: {
  64. content,
  65. },
  66. })
  67. }
  68. export async function editMessageWithUser(
  69. projectId,
  70. threadId,
  71. messageId,
  72. userId,
  73. content
  74. ) {
  75. return asyncRequest({
  76. method: 'post',
  77. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  78. json: {
  79. content,
  80. userId,
  81. },
  82. })
  83. }
  84. export async function checkStatus() {
  85. return asyncRequest({
  86. method: 'get',
  87. url: `/status`,
  88. json: true,
  89. })
  90. }
  91. export async function getMetric(matcher) {
  92. const { body } = await asyncRequest({
  93. method: 'get',
  94. url: `/metrics`,
  95. })
  96. const found = body.split('\n').find(matcher)
  97. if (!found) return 0
  98. return parseInt(found.split(' ')[1], 0)
  99. }
  100. export async function reopenThread(projectId, threadId) {
  101. return asyncRequest({
  102. method: 'post',
  103. url: `/project/${projectId}/thread/${threadId}/reopen`,
  104. })
  105. }
  106. export async function deleteThread(projectId, threadId) {
  107. return asyncRequest({
  108. method: 'delete',
  109. url: `/project/${projectId}/thread/${threadId}`,
  110. })
  111. }
  112. export async function deleteMessage(projectId, threadId, messageId) {
  113. return asyncRequest({
  114. method: 'delete',
  115. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  116. })
  117. }
  118. export async function destroyProject(projectId) {
  119. return asyncRequest({
  120. method: 'delete',
  121. url: `/project/${projectId}`,
  122. })
  123. }