Files
supabase/apps/studio/data/workflow-runs/workflow-run-logs-query.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

54 lines
1.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { workflowRunKeys } from './keys'
export type WorkflowRunLogsVariables = {
workflowRunId?: string
}
export async function getWorkflowRunLogs(
{ workflowRunId }: WorkflowRunLogsVariables,
signal?: AbortSignal
) {
if (!workflowRunId) throw new Error('workflowRunId is required')
const { data, error } = await get(`/platform/workflow-runs/{workflow_run_id}/logs`, {
params: {
path: {
workflow_run_id: workflowRunId,
},
},
parseAs: 'text',
headers: {
Accept: 'text/plain',
},
signal,
})
if (error) handleError(error)
return data
.split('\n')
.flatMap((line) => line.split('\r'))
.join('\n')
.trim()
}
export type WorkflowRunLogsData = Awaited<ReturnType<typeof getWorkflowRunLogs>>
export type WorkflowRunLogsError = ResponseError
export const useWorkflowRunLogsQuery = <TData = WorkflowRunLogsData>(
{ workflowRunId }: WorkflowRunLogsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<WorkflowRunLogsData, WorkflowRunLogsError, TData> = {}
) =>
useQuery<WorkflowRunLogsData, WorkflowRunLogsError, TData>({
queryKey: workflowRunKeys.list(workflowRunId),
queryFn: ({ signal }) => getWorkflowRunLogs({ workflowRunId }, signal),
enabled: enabled && typeof workflowRunId !== 'undefined',
staleTime: 0,
...options,
})