import type { HeadscaleApiInterface } from '../api'; /** * Creates a strongly-typed group factory for a given endpoint interface. * * Example: * export const apiKeyGroup = defineGroup({...}) */ export interface EndpointFactory { __type?: T; (client: HeadscaleApiInterface['clientHelpers'], apiKey: string): T; } export function defineApiEndpoints( factories: ( client: HeadscaleApiInterface['clientHelpers'], apiKey: string, ) => T, ): EndpointFactory { return factories; } export type ExtractApiEndpoints = F extends EndpointFactory ? T : never; export type UnionToIntersection = ( U extends any ? (k: U) => void : never ) extends (k: infer I) => void ? I : never; /** * Compose multiple endpoint sets into a single typed runtime client */ export function composeEndpoints[]>( factories: T, clientHelpers: any, apiKey: string, ): UnionToIntersection> { const instances = factories.map((f) => f(clientHelpers, apiKey)); return Object.assign({}, ...instances) as any; }