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