Skip to content

Commit

Permalink
removed backwards compatibility check
Browse files Browse the repository at this point in the history
* removed backwards compatibility check
  • Loading branch information
routerino authored Oct 1, 2024
1 parent f91f88c commit a6e53ba
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 56 deletions.
5 changes: 1 addition & 4 deletions src/lib/common/Stores.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { onMount } from 'svelte';
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore, APIMachineOrNode } from '$lib/common/stores.js';
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore} from '$lib/common/stores.js';
import { URLStore } from '$lib/common/stores.js';
import { APIKeyStore } from '$lib/common/stores.js';
import { preAuthHideStore } from '$lib/common/stores.js';
Expand Down Expand Up @@ -28,9 +28,6 @@
URLStore.subscribe((val) => localStorage.setItem('headscaleURL', val.replace(/\/+$/, '')));
APIKeyStore.set(localStorage.getItem('headscaleAPIKey') || '');
APIKeyStore.subscribe((val) => localStorage.setItem('headscaleAPIKey', val));
// stores api version compatibility info
APIMachineOrNode.set(localStorage.getItem('headscaleAPIMachineOrNode') || 'machine');
APIMachineOrNode.subscribe((val) => localStorage.setItem('headscaleAPIMachineOrNode', val));
// stores whether preauthkeys get hidden when expired/used
preAuthHideStore.set((localStorage.getItem('headscalePreAuthHide') || 'false') == 'true');
Expand Down
55 changes: 8 additions & 47 deletions src/lib/common/apiFunctions.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script context="module" lang="ts">
import { APIKey, Device, PreAuthKey, User } from '$lib/common/classes';
import { deviceStore, userStore, apiTestStore, APIMachineOrNode } from '$lib/common/stores.js';
import { deviceStore, userStore, apiTestStore} from '$lib/common/stores.js';
import { sortDevices, sortUsers } from '$lib/common/sorting.svelte';
import { filterDevices, filterUsers } from './searching.svelte';
Expand Down Expand Up @@ -152,16 +152,14 @@
}
export async function updateTags(deviceID: string, tags: string[]): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/tags`;
let endpointURL = `/api/v1/node/${deviceID}/tags`;
await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand Down Expand Up @@ -248,43 +246,14 @@
});
}
export async function testMachineOrNode() {
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for getting devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
await fetch(headscaleURL + endpointURL, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${headscaleAPIKey}`
}
}).then((response) => {
if (!response.ok) {
// set APIMachineOrNode to the opposite value
if (headscaleAPIMachineOrNode == 'machine') {
APIMachineOrNode.set('node');
} else {
APIMachineOrNode.set('machine');
}
}
});
}
export async function getDevices(): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for getting devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
let endpointURL = `/api/v1/node`;
//returning variables
let headscaleDevices = [new Device()];
Expand Down Expand Up @@ -315,7 +284,7 @@
});
await headscaleDeviceResponse.json().then((data) => {
headscaleDevices = data[`${headscaleAPIMachineOrNode}s`];
headscaleDevices = data[`nodes`];
headscaleDevices = sortDevices(headscaleDevices);
});
// set the stores
Expand Down Expand Up @@ -467,16 +436,14 @@
}
export async function newDevice(key: string, userName: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/register`;
let endpointURL = `/api/v1/node/register`;
await fetch(headscaleURL + endpointURL + '?user=' + userName + '&key=' + key, {
method: 'POST',
Expand All @@ -500,16 +467,14 @@
}
export async function moveDevice(deviceID: string, user: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/user?user=${user}`;
let endpointURL = `/api/v1/node/${deviceID}/user?user=${user}`;
await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand All @@ -533,16 +498,14 @@
}
export async function renameDevice(deviceID: string, name: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/rename/${name}`;
let endpointURL = `/api/v1/node/${deviceID}/rename/${name}`;
await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand All @@ -566,16 +529,14 @@
}
export async function removeDevice(deviceID: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';
// endpoint url for removing devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}`;
let endpointURL = `/api/v1/node/${deviceID}`;
await fetch(headscaleURL + endpointURL, {
method: 'DELETE',
Expand Down
2 changes: 0 additions & 2 deletions src/lib/common/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const themeStore = writable('');
// stores URL and API Key
export const URLStore = writable('');
export const APIKeyStore = writable('');
// stores the type of device api call made for version compatibility
export const APIMachineOrNode = writable('machine');
// stores sorting preferences
export const deviceSortStore = writable('id');
export const deviceSortDirectionStore = writable('ascending');
Expand Down
3 changes: 0 additions & 3 deletions src/lib/devices/DeviceCard/DeviceRoutesAPI.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<script context="module" lang="ts">
import type { Route } from '$lib/common/classes';
import { testMachineOrNode } from '$lib/common/apiFunctions.svelte'
export async function getDeviceRoutes(deviceID: string): Promise<Route[]> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
Expand Down

0 comments on commit a6e53ba

Please sign in to comment.