You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.2 KiB

  1. import MockAdatper from 'axios-mock-adapter';
  2. import HTTP, { defaultHttpClient } from '../http';
  3. function newId() {
  4. return Number((Math.random() * 100000).toFixed(0));
  5. }
  6. function parseOffset(url) {
  7. const offset = url.match(/offset=(\d+)/);
  8. return offset ? Number(offset[1]) : 0;
  9. }
  10. function parseDocId(url) {
  11. return Number(url.split('/')[5]);
  12. }
  13. function parseAnnotationId(url) {
  14. return Number(url.split('/')[7]);
  15. }
  16. export default class DemoApi {
  17. constructor(data, labelField) {
  18. this.data = data;
  19. this.labelField = labelField;
  20. this.mocks = [new MockAdatper(HTTP), new MockAdatper(defaultHttpClient)];
  21. this.pageSize = 5;
  22. }
  23. getMe() {
  24. return [200, this.data.me];
  25. }
  26. getLabels() {
  27. return [200, this.data.labels];
  28. }
  29. getStatistics() {
  30. return [200, {
  31. total: this.data.docs.length,
  32. remaining: this.data.docs.filter(doc => doc.annotations.length === 0).length,
  33. }];
  34. }
  35. getDocs(config) {
  36. const offset = parseOffset(config.url);
  37. return [200, {
  38. results: this.data.docs.slice(Math.max(offset - 1, 0), this.pageSize),
  39. count: this.data.docs.length,
  40. next: offset + this.pageSize <= this.data.docs.length
  41. ? config.url.replace(`offset=${offset}`, `offset=${offset + this.pageSize}`)
  42. : null,
  43. previous: offset - this.pageSize >= 0
  44. ? config.url.replace(`offset=${offset}`, `offset=${offset - this.pageSize}`)
  45. : null,
  46. }];
  47. }
  48. getProject() {
  49. return [200, this.data.project];
  50. }
  51. postAnnotations(config) {
  52. const docId = parseDocId(config.url);
  53. const body = JSON.parse(config.data);
  54. const doc = this.data.docs.find(_ => _.id === docId);
  55. if (!doc) {
  56. return [404, {}];
  57. }
  58. let annotation = doc.annotations.find(_ => _[this.labelField] === body[this.labelField]);
  59. if (!annotation) {
  60. annotation = { id: newId(), ...body };
  61. doc.annotations.push(annotation);
  62. }
  63. return [200, annotation];
  64. }
  65. deleteAnnotations(config) {
  66. const docId = parseDocId(config.url);
  67. const annotationId = parseAnnotationId(config.url);
  68. const doc = this.data.docs.find(_ => _.id === docId);
  69. if (!doc) {
  70. return [404, {}];
  71. }
  72. doc.annotations = doc.annotations.filter(el => el.id !== annotationId);
  73. return [200, {}];
  74. }
  75. start() {
  76. this.mocks.forEach((mock) => {
  77. mock.onGet(/\/v1\/.*/g).reply((config) => {
  78. if (config.url.endsWith('/me')) {
  79. return this.getMe();
  80. }
  81. if (config.url.endsWith('/labels')) {
  82. return this.getLabels();
  83. }
  84. if (config.url.endsWith('/statistics')) {
  85. return this.getStatistics();
  86. }
  87. if (config.url.indexOf('/docs') !== -1) {
  88. return this.getDocs(config);
  89. }
  90. return this.getProject();
  91. });
  92. mock.onPost(/\/v1\/.*/g).reply((config) => {
  93. if (config.url.endsWith('/annotations')) {
  94. return this.postAnnotations(config);
  95. }
  96. return [404, {}];
  97. });
  98. mock.onDelete(/\/v1\/.*/g).reply((config) => {
  99. if (config.url.indexOf('/annotations') !== -1) {
  100. return this.deleteAnnotations(config);
  101. }
  102. return [404, {}];
  103. });
  104. });
  105. }
  106. stop() {
  107. this.mocks.forEach(mock => mock.reset());
  108. }
  109. }