feat: update more primitives to use the new api

This commit is contained in:
Aarnav Tale
2025-11-04 23:06:56 -05:00
parent d68737e410
commit c84e9ca4a8
9 changed files with 109 additions and 116 deletions
+5 -10
View File
@@ -1,14 +1,12 @@
import { eq, isNotNull } from 'drizzle-orm';
import { LoaderFunctionArgs } from 'react-router';
import { Machine } from '~/types';
import log from '~/utils/log';
import { LoadContext } from '..';
import type { Route } from '../../layouts/+types/dashboard';
import { ephemeralNodes } from './schema';
export async function pruneEphemeralNodes({
context,
request,
}: LoaderFunctionArgs<LoadContext>) {
}: Route.LoaderArgs) {
const session = await context.sessions.auth(request);
const ephemerals = await context.db
.select()
@@ -20,11 +18,8 @@ export async function pruneEphemeralNodes({
return;
}
const { nodes } = await context.client.get<{ nodes: Machine[] }>(
'v1/node',
session.api_key,
);
const api = context.hsApi.getRuntimeClient(session.api_key);
const nodes = await api.getNodes();
const toPrune = nodes.filter((node) => {
if (node.online) {
return false;
@@ -42,7 +37,7 @@ export async function pruneEphemeralNodes({
const promises = toPrune.map((node) => {
return async () => {
log.debug('api', `Pruning node ${node.name}`);
await context.client.delete(`v1/node/${node.id}`, session.api_key);
await api.deleteNode(node.id);
await context.db
.delete(ephemeralNodes)
@@ -1,5 +1,6 @@
import {
composeEndpoints,
defineApiEndpoints,
type ExtractApiEndpoints,
type UnionToIntersection,
} from '../factory';
@@ -10,12 +11,42 @@ import policyEndpoints from './policy';
import preAuthKeyEndpoints from './pre-auth-keys';
import userEndpoints from './users';
interface HealthcheckEndpoint {
/**
* Checks if the Headscale instance is healthy.
*
* @returns A boolean indicating if the instance is healthy.
*/
isHealthy(): Promise<boolean>;
}
const healthcheckEndpoint = defineApiEndpoints<HealthcheckEndpoint>(
(client, apiKey) => ({
isHealthy: async () => {
try {
const res = await client.rawFetch('/health', {
method: 'GET',
headers: {
// This doesn't really matter
Authorization: `Bearer ${apiKey}`,
},
});
return res.statusCode === 200;
} catch {
return false;
}
},
}),
);
/**
* A constant list of all endpoint groups.
* Add new endpoint groups here.
*/
export const endpointSets = [
apiKeyEndpoints,
healthcheckEndpoint,
nodeEndpoints,
policyEndpoints,
preAuthKeyEndpoints,
@@ -15,7 +15,7 @@ export interface PreAuthKeyEndpoints {
*
* @param user The user to create the pre-authentication key for.
* @param ephemeral Whether the key is ephemeral.
* @param uses The number of uses for the key.
* @param reusable Whether the key is reusable.
* @param expiration The expiration date of the key, or `null` for no expiration.
* @param aclTags An array of ACL tags to associate with the key, or `null` for none.
* @returns A `PreAuthKey` object representing the newly created pre-authentication key.
@@ -23,7 +23,7 @@ export interface PreAuthKeyEndpoints {
createPreAuthKey(
user: string,
ephemeral: boolean,
uses: number,
reusable: boolean,
expiration: Date | null,
aclTags: string[] | null,
): Promise<PreAuthKey>;
@@ -46,13 +46,13 @@ export default defineApiEndpoints<PreAuthKeyEndpoints>((client, apiKey) => ({
return preAuthKeys;
},
createPreAuthKey: async (user, ephemeral, uses, expiration, aclTags) => {
createPreAuthKey: async (user, ephemeral, reusable, expiration, aclTags) => {
const { preAuthKey } = await client.apiFetch<{
preAuthKey: PreAuthKey;
}>('POST', 'v1/preauthkey', apiKey, {
user,
ephemeral,
uses,
reusable,
expiration: expiration ? expiration.toISOString() : null,
aclTags,
});