Skip to content

Commit eead985

Browse files
authored
change api to dynamically check for breaking change (#132)
1 parent a9db179 commit eead985

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

src/lib/common/Stores.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import { onMount } from 'svelte';
3-
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore } from '$lib/common/stores.js';
3+
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore, APIMachineOrNode } from '$lib/common/stores.js';
44
import { URLStore } from '$lib/common/stores.js';
55
import { APIKeyStore } from '$lib/common/stores.js';
66
import { preAuthHideStore } from '$lib/common/stores.js';
@@ -28,6 +28,9 @@
2828
URLStore.subscribe((val) => localStorage.setItem('headscaleURL', val.replace(/\/+$/, '')));
2929
APIKeyStore.set(localStorage.getItem('headscaleAPIKey') || '');
3030
APIKeyStore.subscribe((val) => localStorage.setItem('headscaleAPIKey', val));
31+
// stores api version compatibility info
32+
APIMachineOrNode.set(localStorage.getItem('headscaleAPIMachineOrNode') || 'machine');
33+
APIMachineOrNode.subscribe((val) => localStorage.setItem('headscaleAPIMachineOrNode', val));
3134
3235
// stores whether preauthkeys get hidden when expired/used
3336
preAuthHideStore.set((localStorage.getItem('headscalePreAuthHide') || 'false') == 'true');

src/lib/common/apiFunctions.svelte

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script context="module" lang="ts">
22
import { APIKey, Device, PreAuthKey, User } from '$lib/common/classes';
3-
import { deviceStore, userStore, apiTestStore } from '$lib/common/stores.js';
3+
import { deviceStore, userStore, apiTestStore, APIMachineOrNode } from '$lib/common/stores.js';
44
import { sortDevices, sortUsers } from '$lib/common/sorting.svelte';
55
import { filterDevices, filterUsers } from './searching.svelte';
66
@@ -40,7 +40,7 @@
4040
});
4141
4242
await headscaleUsersResponse.json().then((data) => {
43-
headscaleUsers = data.users
43+
headscaleUsers = data.users;
4444
// sort the users
4545
headscaleUsers = sortUsers(headscaleUsers);
4646
});
@@ -152,12 +152,16 @@
152152
}
153153
154154
export async function updateTags(deviceID: string, tags: string[]): Promise<any> {
155+
// test the API routes whether we should try to use 'machines' or 'nodes'
156+
await testMachineOrNode();
157+
155158
// variables in local storage
156159
let headscaleURL = localStorage.getItem('headscaleURL') || '';
157160
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
161+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
158162
159163
// endpoint url for editing users
160-
let endpointURL = '/api/v1/machine/' + deviceID + '/tags';
164+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/tags`;
161165
162166
await fetch(headscaleURL + endpointURL, {
163167
method: 'POST',
@@ -244,13 +248,43 @@
244248
});
245249
}
246250
251+
export async function testMachineOrNode() {
252+
// variables in local storage
253+
let headscaleURL = localStorage.getItem('headscaleURL') || '';
254+
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
255+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
256+
257+
// endpoint url for getting devices
258+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
259+
await fetch(headscaleURL + endpointURL, {
260+
method: 'GET',
261+
headers: {
262+
Accept: 'application/json',
263+
Authorization: `Bearer ${headscaleAPIKey}`
264+
}
265+
}).then((response) => {
266+
if (!response.ok) {
267+
// set APIMachineOrNode to the opposite value
268+
if (headscaleAPIMachineOrNode == 'machine') {
269+
APIMachineOrNode.set('node');
270+
} else {
271+
APIMachineOrNode.set('machine');
272+
}
273+
}
274+
});
275+
}
276+
247277
export async function getDevices(): Promise<any> {
278+
// test the API routes whether we should try to use 'machines' or 'nodes'
279+
await testMachineOrNode();
280+
248281
// variables in local storage
249282
let headscaleURL = localStorage.getItem('headscaleURL') || '';
250283
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
284+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
251285
252-
// endpoint url for getting users
253-
let endpointURL = '/api/v1/machine';
286+
// endpoint url for getting devices
287+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
254288
255289
//returning variables
256290
let headscaleDevices = [new Device()];
@@ -281,7 +315,7 @@
281315
});
282316
283317
await headscaleDeviceResponse.json().then((data) => {
284-
headscaleDevices = data.machines;
318+
headscaleDevices = data[`${headscaleAPIMachineOrNode}s`];
285319
headscaleDevices = sortDevices(headscaleDevices);
286320
});
287321
// set the stores
@@ -291,8 +325,6 @@
291325
filterDevices();
292326
}
293327
294-
295-
296328
export async function getAPIKeys(): Promise<APIKey[]> {
297329
// variables in local storage
298330
let headscaleURL = localStorage.getItem('headscaleURL') || '';
@@ -435,12 +467,16 @@
435467
}
436468
437469
export async function newDevice(key: string, userName: string): Promise<any> {
470+
// test the API routes whether we should try to use 'machines' or 'nodes'
471+
await testMachineOrNode();
472+
438473
// variables in local storage
439474
let headscaleURL = localStorage.getItem('headscaleURL') || '';
440475
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
476+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
441477
442478
// endpoint url for editing users
443-
let endpointURL = '/api/v1/machine/register';
479+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/register`;
444480
445481
await fetch(headscaleURL + endpointURL + '?user=' + userName + '&key=' + key, {
446482
method: 'POST',
@@ -464,12 +500,16 @@
464500
}
465501
466502
export async function moveDevice(deviceID: string, user: string): Promise<any> {
503+
// test the API routes whether we should try to use 'machines' or 'nodes'
504+
await testMachineOrNode();
505+
467506
// variables in local storage
468507
let headscaleURL = localStorage.getItem('headscaleURL') || '';
469508
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
509+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
470510
471511
// endpoint url for editing users
472-
let endpointURL = '/api/v1/machine/' + deviceID + '/user?user=' + user;
512+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/user?user=${user}`;
473513
474514
await fetch(headscaleURL + endpointURL, {
475515
method: 'POST',
@@ -493,12 +533,16 @@
493533
}
494534
495535
export async function renameDevice(deviceID: string, name: string): Promise<any> {
536+
// test the API routes whether we should try to use 'machines' or 'nodes'
537+
await testMachineOrNode();
538+
496539
// variables in local storage
497540
let headscaleURL = localStorage.getItem('headscaleURL') || '';
498541
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
542+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
499543
500544
// endpoint url for editing users
501-
let endpointURL = '/api/v1/machine/' + deviceID + '/rename/' + name;
545+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/rename/${name}`;
502546
503547
await fetch(headscaleURL + endpointURL, {
504548
method: 'POST',
@@ -522,12 +566,16 @@
522566
}
523567
524568
export async function removeDevice(deviceID: string): Promise<any> {
569+
// test the API routes whether we should try to use 'machines' or 'nodes'
570+
await testMachineOrNode();
571+
525572
// variables in local storage
526573
let headscaleURL = localStorage.getItem('headscaleURL') || '';
527574
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
575+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
528576
529577
// endpoint url for removing devices
530-
let endpointURL = '/api/v1/machine/' + deviceID;
578+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}`;
531579
532580
await fetch(headscaleURL + endpointURL, {
533581
method: 'DELETE',

src/lib/common/stores.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const themeStore = writable('');
1010
// stores URL and API Key
1111
export const URLStore = writable('');
1212
export const APIKeyStore = writable('');
13+
// stores the type of device api call made for version compatibility
14+
export const APIMachineOrNode = writable('machine');
1315
// stores sorting preferences
1416
export const deviceSortStore = writable('id');
1517
export const deviceSortDirectionStore = writable('ascending');

src/lib/devices/DeviceCard/DeviceRoutesAPI.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<script context="module" lang="ts">
22
import type { Route } from '$lib/common/classes';
3+
import { testMachineOrNode } from '$lib/common/apiFunctions.svelte'
34
45
export async function getDeviceRoutes(deviceID: string): Promise<Route[]> {
6+
// test the API routes whether we should try to use 'machines' or 'nodes'
7+
await testMachineOrNode();
8+
59
// variables in local storage
610
let headscaleURL = localStorage.getItem('headscaleURL') || '';
711
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
12+
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
813
914
// endpoint url for getting users
10-
let endpointURL = '/api/v1/machine/' + deviceID + '/routes';
15+
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/routes`;
1116
1217
//returning variables
1318
let headscaleRouteList: Route[] = [];

0 commit comments

Comments
 (0)