Files
supabase/apps/studio/data/sql/format-sql-query.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

46 lines
1.2 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
export type FormatQueryVariables = {
projectRef: string
sql: string
connectionString?: string
}
export async function formatQuery(
{ projectRef, connectionString, sql }: FormatQueryVariables,
signal?: AbortSignal
) {
let headers = new Headers()
if (connectionString) {
headers.set('x-connection-encrypted', connectionString)
}
const response = await post(
`${API_URL}/pg-meta/${projectRef}/query/format`,
{ query: sql },
{ headers: Object.fromEntries(headers), signal }
)
if (response.error) {
throw response.error
}
return { result: response }
}
type FormatQueryData = Awaited<ReturnType<typeof formatQuery>>
export const useFormatQueryMutation = ({
onSuccess,
...options
}: Omit<UseMutationOptions<FormatQueryData, unknown, FormatQueryVariables>, 'mutationFn'> = {}) => {
return useMutation<FormatQueryData, unknown, FormatQueryVariables>((args) => formatQuery(args), {
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
...options,
})
}