ChatClient.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 await 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 await 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 await 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 await 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 await asyncRequest({
  45. method: 'get',
  46. url: `/project/${projectId}/threads`,
  47. json: true,
  48. })
  49. }
  50. export async function resolveThread(projectId, threadId, userId) {
  51. return await asyncRequest({
  52. method: 'post',
  53. url: `/project/${projectId}/thread/${threadId}/resolve`,
  54. json: {
  55. user_id: userId,
  56. },
  57. })
  58. }
  59. export async function getResolvedThreadIds(projectId) {
  60. return await asyncRequest({
  61. method: 'get',
  62. url: `/project/${projectId}/resolved-thread-ids`,
  63. json: true,
  64. })
  65. }
  66. export async function editMessage(projectId, threadId, messageId, content) {
  67. return await asyncRequest({
  68. method: 'post',
  69. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  70. json: {
  71. content,
  72. },
  73. })
  74. }
  75. export async function editMessageWithUser(
  76. projectId,
  77. threadId,
  78. messageId,
  79. userId,
  80. content
  81. ) {
  82. return await asyncRequest({
  83. method: 'post',
  84. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  85. json: {
  86. content,
  87. userId,
  88. },
  89. })
  90. }
  91. export async function checkStatus() {
  92. return await asyncRequest({
  93. method: 'get',
  94. url: `/status`,
  95. json: true,
  96. })
  97. }
  98. export async function getMetric(matcher) {
  99. const { body } = await asyncRequest({
  100. method: 'get',
  101. url: `/metrics`,
  102. })
  103. const found = body.split('\n').find(matcher)
  104. if (!found) return 0
  105. return parseInt(found.split(' ')[1], 0)
  106. }
  107. export async function reopenThread(projectId, threadId) {
  108. return await asyncRequest({
  109. method: 'post',
  110. url: `/project/${projectId}/thread/${threadId}/reopen`,
  111. })
  112. }
  113. export async function deleteThread(projectId, threadId) {
  114. return await asyncRequest({
  115. method: 'delete',
  116. url: `/project/${projectId}/thread/${threadId}`,
  117. })
  118. }
  119. export async function deleteMessage(projectId, threadId, messageId) {
  120. return await asyncRequest({
  121. method: 'delete',
  122. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  123. })
  124. }
  125. export async function destroyProject(projectId) {
  126. return await asyncRequest({
  127. method: 'delete',
  128. url: `/project/${projectId}`,
  129. })
  130. }