-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
189 lines (182 loc) · 5.88 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { fetchWithErrorHandling } from '../../utils';
import {
ApiCreateDeviceGroupPayload,
ApiCreateDeviceGroupResponse,
ApiDeleteDeviceGroupResponse,
ApiGetDeviceGroupResponse,
ApiListDeviceGroupsParams,
ApiListDeviceGroupsResponse,
ApiUpdateDeviceGroupPayload,
ApiUpdateDeviceGroupResponse,
} from './types';
const BASE_URL = 'https://api.azionapi.net/edge_applications';
/**
* Lists all device groups for an edge application.
*
* @param {string} token - Authentication token for Azion API.
* @param {number} Id - ID of the edge application.
* @param {ApiListDeviceGroupsParams} [params] - Optional parameters for filtering and pagination.
* @param {boolean} [debug] - Enable debug mode for detailed logging.
* @returns {Promise<ApiListDeviceGroupsResponse>} Array of device groups or an error if retrieval failed.
*/
const getDeviceGroups = async (
token: string,
Id: number,
params?: ApiListDeviceGroupsParams,
debug?: boolean,
): Promise<ApiListDeviceGroupsResponse> => {
try {
const { page = 1, page_size = 10, sort = 'name', order = 'asc' } = params || {};
const queryParams = new URLSearchParams({
page: String(page),
page_size: String(page_size),
sort,
order,
});
const data = await fetchWithErrorHandling(
`${BASE_URL}/${Id}/device_groups?${queryParams.toString()}`,
{
method: 'GET',
headers: { Accept: 'application/json; version=3', Authorization: `Token ${token}` },
},
debug,
);
return data;
} catch (error) {
if (debug) console.error('Error getting device groups:', error);
throw error;
}
};
/**
* Retrieves a specific device group by ID.
*
* @param {string} token - Authentication token for Azion API.
* @param {number} Id - ID of the edge application.
* @param {number} deviceGroupId - ID of the device group.
* @param {boolean} [debug] - Enable debug mode for detailed logging.
* @returns {Promise<ApiGetDeviceGroupResponse>} Device group data or an error if retrieval failed.
*/
const getDeviceGroupById = async (
token: string,
Id: number,
deviceGroupId: number,
debug?: boolean,
): Promise<ApiGetDeviceGroupResponse> => {
try {
const data = await fetchWithErrorHandling(
`${BASE_URL}/${Id}/device_groups/${deviceGroupId}`,
{
method: 'GET',
headers: { Accept: 'application/json; version=3', Authorization: `Token ${token}` },
},
debug,
);
return data;
} catch (error) {
if (debug) console.error('Error getting device group by ID:', error);
throw error;
}
};
/**
* Creates a new device group for an edge application.
*
* @param {string} token - Authentication token for Azion API.
* @param {number} Id - ID of the edge application.
* @param {ApiCreateDeviceGroupPayload} deviceGroupData - Data of the device group to be created.
* @param {boolean} [debug] - Enable debug mode for detailed logging.
* @returns {Promise<ApiCreateDeviceGroupResponse>} The created device group or an error if creation failed.
*/
const createDeviceGroup = async (
token: string,
Id: number,
deviceGroupData: ApiCreateDeviceGroupPayload,
debug?: boolean,
): Promise<ApiCreateDeviceGroupResponse> => {
try {
const data = await fetchWithErrorHandling(
`${BASE_URL}/${Id}/device_groups`,
{
method: 'POST',
headers: {
Accept: 'application/json; version=3',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
body: JSON.stringify(deviceGroupData),
},
debug,
);
return data;
} catch (error) {
if (debug) console.error('Error creating device group:', error);
throw error;
}
};
/**
* Updates an existing device group.
*
* @param {string} token - Authentication token for Azion API.
* @param {number} Id - ID of the edge application.
* @param {number} deviceGroupId - ID of the device group to update.
* @param {ApiUpdateDeviceGroupPayload} deviceGroupData - New data for the device group.
* @param {boolean} [debug] - Enable debug mode for detailed logging.
* @returns {Promise<ApiUpdateDeviceGroupResponse>} The updated device group or an error if update failed.
*/
const updateDeviceGroup = async (
token: string,
Id: number,
deviceGroupId: number,
deviceGroupData: ApiUpdateDeviceGroupPayload,
debug?: boolean,
): Promise<ApiUpdateDeviceGroupResponse> => {
try {
const data = await fetchWithErrorHandling(
`${BASE_URL}/${Id}/device_groups/${deviceGroupId}`,
{
method: 'PATCH',
headers: {
Accept: 'application/json; version=3',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
body: JSON.stringify(deviceGroupData),
},
debug,
);
return data;
} catch (error) {
if (debug) console.error('Error updating device group:', error);
throw error;
}
};
/**
* Deletes a device group.
*
* @param {string} token - Authentication token for Azion API.
* @param {number} Id - ID of the edge application.
* @param {number} deviceGroupId - ID of the device group to delete.
* @param {boolean} [debug] - Enable debug mode for detailed logging.
* @returns {Promise<ApiDeleteDeviceGroupResponse>} Confirmation of deletion or an error if deletion failed.
*/
const deleteDeviceGroup = async (
token: string,
Id: number,
deviceGroupId: number,
debug?: boolean,
): Promise<ApiDeleteDeviceGroupResponse> => {
try {
const data = await fetchWithErrorHandling(
`${BASE_URL}/${Id}/device_groups/${deviceGroupId}`,
{
method: 'DELETE',
headers: { Accept: 'application/json; version=3', Authorization: `Token ${token}` },
},
debug,
);
return data;
} catch (error) {
if (debug) console.error('Error deleting device group:', error);
throw error;
}
};
export { createDeviceGroup, deleteDeviceGroup, getDeviceGroupById, getDeviceGroups, updateDeviceGroup };