-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunleash-client.ts
45 lines (39 loc) · 1.21 KB
/
unleash-client.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
import { HttpService } from '@nestjs/axios'
import { Injectable, Logger } from '@nestjs/common'
import { AxiosRequestConfig } from 'axios'
import { firstValueFrom } from 'rxjs'
@Injectable()
export class UnleashClient {
private readonly logger = new Logger(UnleashClient.name)
constructor(private readonly http: HttpService) {}
async request<T = unknown>(config: AxiosRequestConfig): Promise<T> {
const method = config.method ?? '(unknown method)'
const baseUrl = this.http.axiosRef.defaults.baseURL ?? '(unknown base url)'
const url = config.url ?? '(unknown url)'
this.logger.debug(`Request: ${method} ${baseUrl}${url}`)
const response = await firstValueFrom(this.http.request<T>(config))
return response.data
}
async get<TResponse = unknown>(
url: string,
config?: AxiosRequestConfig,
): Promise<TResponse> {
return this.request<TResponse>({
...config,
method: 'GET',
url,
})
}
async post<TResponse = unknown, TRequest = unknown>(
url: string,
data?: TRequest,
config?: AxiosRequestConfig,
): Promise<TResponse> {
return this.request<TResponse>({
...config,
data: data ?? {},
method: 'POST',
url,
})
}
}