@prefecthq/prefect-ui-library
Version:
This library is the Vue and Typescript component library for [Prefect 2](https://github.com/PrefectHQ/prefect) and [Prefect Cloud 2](https://www.prefect.io/cloud/). _The components and utilities in this project are not meant to be used independently_.
71 lines (54 loc) • 2.25 kB
text/typescript
import { isAxiosError, AxiosError } from 'axios'
import { AutomationResponse } from '@/automations/types/api/automation'
import { Automation } from '@/automations/types/automation'
import { AutomationsFilter } from '@/automations/types/filter'
import { mapper } from '@/services/Mapper'
import { WorkspaceApi } from '@/services/WorkspaceApi'
import { Require } from '@/types/utilities'
import { httpStatus } from '@/utilities/httpStatus'
export class WorkspaceAutomationsApi extends WorkspaceApi {
protected override routePrefix = '/automations'
public async getAutomation(automationId: string): Promise<Automation> {
const { data } = await this.get<AutomationResponse>(`/${automationId}`)
return mapper.map('AutomationResponse', data, 'Automation')
}
public async getAutomations(filter: AutomationsFilter = {}): Promise<Automation[]> {
const { data } = await this.post<AutomationResponse[]>('/filter', filter)
return mapper.map('AutomationResponse', data, 'Automation')
}
public deleteAutomation(automationId: string): Promise<void> {
return this.delete(`/${automationId}`)
}
public enableAutomation(automationId: string, enabled: boolean = true): Promise<void> {
return this.patch(`/${automationId}`, { enabled })
}
public async validateTemplate(template: string): Promise<string | true> {
try {
await this.post('/templates/validate', template)
return true
} catch (error) {
if (isInvalidAutomationTemplateError(error)) {
const { line, message } = error.response.data.error
return `Error on line ${line}: ${message} `
}
throw error
}
}
public async getResourceAutomations(resourceId: string): Promise<Automation[]> {
const { data } = await this.get<AutomationResponse[]>(`related-to/${resourceId}`)
return mapper.map('AutomationResponse', data, 'Automation')
}
}
type InvalidAutomationTemplateError = {
error: {
line: number,
message: string,
source: string,
},
}
function isInvalidAutomationTemplateError(error: unknown): error is Require<AxiosError<InvalidAutomationTemplateError>, 'response'> {
if (!isAxiosError(error)) {
return false
}
return httpStatus(error).is('UnprocessableEntity')
}