-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtoggles-updater.service.ts
47 lines (42 loc) · 1.46 KB
/
toggles-updater.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
import { HttpStatus, Inject, Injectable } from '@nestjs/common'
import { SchedulerRegistry } from '@nestjs/schedule'
import { AxiosError } from 'axios'
import { REFRESH_INTERVAL } from '..'
import { UnleashFeaturesClient } from '../../unleash-client'
import { ToggleEntity } from '../entity/toggle.entity'
import { ToggleRepository } from '../repository/toggle-repository'
import { BaseUpdater } from './base-updater'
@Injectable()
export class TogglesUpdaterService extends BaseUpdater {
constructor(
@Inject(REFRESH_INTERVAL) protected readonly interval: number,
private readonly features: UnleashFeaturesClient,
protected readonly scheduler: SchedulerRegistry,
private readonly toggles: ToggleRepository,
) {
super()
}
isAxiosError(error: unknown): error is AxiosError {
return (error as AxiosError).isAxiosError
}
async update(): Promise<void> {
try {
const features = await this.features.getFeatures()
features.features.forEach((feature) => {
this.toggles.updateOrCreate(feature.name, new ToggleEntity(feature))
})
} catch (error) {
if (
this.isAxiosError(error) &&
error.response?.status === HttpStatus.NOT_FOUND &&
error.config
) {
const { url, baseURL } = error.config
this.logger.warn(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`Could not retrieve ${baseURL!}${url!}: ${error.message}`,
)
}
}
}
}