export type CreateApiV2DeviceIdScriptRunArgs = {id: number, body: {type?: ('ACTION' | 'SCRIPT') | undefined, id?: number | undefined, uid?: string | undefined, parameters?: string | undefined, runAs?: string | undefined}}; export type CreateApiV2DeviceIdScriptRunOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; /** * Run script or built-in action * * POST /v2/device/{id}/script/run * * Also accepts auth: sessionKey */ export const createApiV2DeviceIdScriptRun = async (args: CreateApiV2DeviceIdScriptRunArgs, options: CreateApiV2DeviceIdScriptRunOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "*/*") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/device/${encodeURIComponent(String(args.id))}/script/run${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.blob() };