import {z} from 'zod' export type GetApiV2DeviceIdWindowsServicesArgs = {id: number, name?: string | undefined, state?: ('UNKNOWN' | 'STOPPED' | 'START_PENDING' | 'RUNNING' | 'STOP_PENDING' | 'PAUSE_PENDING' | 'PAUSED' | 'CONTINUE_PENDING') | undefined}; export type GetApiV2DeviceIdWindowsServicesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2DeviceIdWindowsServicesResponse = z.array(z.object({name: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), startType: z.enum(["AUTO_START", "AUTO_START_DELAYED", "BOOT_START", "DEMAND_START", "DISABLED", "SYSTEM_START"]).optional(), userName: z.string().optional(), state: z.enum(["UNKNOWN", "STOPPED", "START_PENDING", "RUNNING", "STOP_PENDING", "PAUSE_PENDING", "PAUSED", "CONTINUE_PENDING"]).optional()})); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Windows services * * GET /v2/device/{id}/windows-services * * Also accepts auth: sessionKey */ export const getApiV2DeviceIdWindowsServices = async (args: GetApiV2DeviceIdWindowsServicesArgs, options: GetApiV2DeviceIdWindowsServicesOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.state != null) query.append("state", String(args.state)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/device/${encodeURIComponent(String(args.id))}/windows-services${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2DeviceIdWindowsServicesResponse, await res.json()) };