-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
127 lines (120 loc) · 4.04 KB
/
api.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import axios, {
AxiosError,
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
} from 'axios'
import { handleErrors } from './utils/errors'
/**
* A class representing the core functionality for making HTTP requests using Axios.
*/
export class HTTPCore {
/**
* Axios instance for making HTTP requests.
*/
public request: AxiosInstance
/**
* Creates an instance of HTTPCore.
* @param {string} apiKey - The API key used for authentication.
* @param {IEnvironment} [environment] - The environment configuration.
*/
constructor(public secretKey: string, public publicKey: string) {
// Initialize the Axios instance with base URL and headers.
this.request = axios.create({
baseURL: 'https://api.blochq.io/v1',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${secretKey}`,
},
})
}
/**
* Handles Axios requests and returns the data or throws a BaseError in case of an error.
* @template T - The type of the expected response data.
* @param {Promise<AxiosResponse<T>>} requestPromise - The Axios request promise.
* @returns {Promise<T>} - The response data.
* @throws {BaseError} - If an error occurs during the request.
* @private
*/
private async handleRequest<T>(
requestPromise: Promise<AxiosResponse<T>>
): Promise<T> {
try {
const response = await requestPromise
return response.data
} catch (error) {
// Throw an Error in case of an AxiosError.
throw handleErrors(error as AxiosError)
}
}
/**
* Performs an HTTP GET request.
* @template T - The type of the expected response data.
* @param {string} url - The URL for the GET request.
* @param {AxiosRequestConfig} [config] - The Axios request configuration.
* @returns {Promise<T>} - The response data.
*/
public async get<T>(url: string, config?: AxiosRequestConfig): Promise<T> {
return this.handleRequest<T>(this.request.get(url, config))
}
/**
* Performs an HTTP POST request.
* @template T - The type of the expected response data.
* @param {string} url - The URL for the POST request.
* @param {any} [data] - The data to be sent in the request body.
* @param {AxiosRequestConfig} [config] - The Axios request configuration.
* @returns {Promise<T>} - The response data.
*/
public async post<T>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T> {
return this.handleRequest<T>(this.request.post(url, data, config))
}
/**
* Performs an HTTP PUT request.
* @template T - The type of the expected response data.
* @param {string} url - The URL for the PUT request.
* @param {any} [data] - The data to be sent in the request body.
* @param {AxiosRequestConfig} [config] - The Axios request configuration.
* @returns {Promise<T>} - The response data.
*/
public async put<T>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T> {
return this.handleRequest<T>(this.request.put(url, data, config))
}
/**
* Performs an HTTP PATCH request.
* @template T - The type of the expected response data.
* @param {string} url - The URL for the PATCH request.
* @param {any} [data] - The data to be sent in the request body.
* @param {AxiosRequestConfig} [config] - The Axios request configuration.
* @returns {Promise<T>} - The response data.
*/
public async patch<T>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T> {
return this.handleRequest<T>(this.request.patch(url, data, config))
}
/**
* Performs an HTTP DELETE request.
* @template T - The type of the expected response data.
* @param {string} url - The URL for the DELETE request.
* @param {AxiosRequestConfig} [config] - The Axios request configuration.
* @returns {Promise<T>} - The response data.
*/
public async delete<T>(
url: string,
// data?: any,
config?: AxiosRequestConfig
): Promise<T> {
return this.handleRequest<T>(this.request.delete(url, config))
}
}