-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.service.ts
62 lines (50 loc) · 1.88 KB
/
data.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { DataRequestQuery } from '@/controllers/data.controller'
import DataList from '@/models/dataList.model'
import axios from 'axios'
import { PaginatedDataDto } from '@/dtos/paginatedData.dto'
import CacheService from './cache.service'
import stringify from 'json-stable-stringify'
import { mapDataListApiResponseToDataListModel } from '@/mappers/data.mapper'
export const DATA_URL = 'https://amock.io/api/tomasmax/data'
class DataService {
private cache: CacheService
constructor(cache: CacheService = new CacheService({ stdTTL: 600 })) {
this.cache = cache
}
private generateCacheKey(params: DataRequestQuery): string {
return stringify(params)
}
private async fetchData(url: string) {
try {
const cachedData = this.cache.get<DataList>(url)
if (cachedData) {
return cachedData
}
const response = await axios.get(url)
this.cache.set(url, response.data)
return response.data
} catch (error) {
if (axios.isAxiosError(error)) {
// Handle Axios-specific errors
console.error('[DataService.fetchData]: Axios error:', error.message)
}
throw error
}
}
public async fetchAndTransformData({ search, status, page = '1', limit = '20', sort }: DataRequestQuery) {
const cacheKey = this.generateCacheKey({ search, status, page, limit, sort })
const cachedData = this.cache.get<PaginatedDataDto>(cacheKey)
if (cachedData) {
return cachedData
}
const response = await this.fetchData(DATA_URL)
const dataList = new DataList(mapDataListApiResponseToDataListModel(response.output))
const filteredDataList = dataList.filterBySearch(search).filterByStatus(status).paginate(parseInt(page), parseInt(limit))
this.cache.set(cacheKey, filteredDataList)
return filteredDataList
}
public clearCache(): void {
this.cache.flushAll()
}
}
export default DataService