Files
supabase/apps/studio/data/edge-functions/edge-function-body-query.ts
95eedb927e Updates to Edge Functions dashboard code editor (#39991)
* feat: use mgmt-api's function body endpoint

* skip json files from static patterns

* set the default content for deno.json

* feat: add drag and drop file support to FileExplorerAndEditor

- Add drag and drop functionality to accept files
- Dropped files are automatically read and added to the files list
- Visual feedback with drag overlay during drag operations
- Files maintain existing data format with id, name, content, and selected state
- Last dropped file is automatically selected for editing

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: add binary file handling to FileExplorerAndEditor

- Add binary file detection for common file extensions (.wasm, images, executables, etc.)
- Show "Cannot Edit Selected File" error message when trying to edit binary files
- Binary files dropped via drag-and-drop retain their original binary content
- Only show error in editor view, files remain accessible in file list
- Text files continue to work normally with full editing capabilities

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* do not ignore empty files

* exclude wasm files from static patterns

* Remove eszip parser test as dependency has been removed

* Fix TS issues

* Fix TS issues

* Fix pnpm-lock

* Fix

* Fix

* Nit

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-30 14:30:54 +08:00

93 lines
2.7 KiB
TypeScript

import { getMultipartBoundary, parseMultipartStream } from '@mjackson/multipart-parser'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
export type EdgeFunctionBodyVariables = {
projectRef?: string
slug?: string
}
export type EdgeFunctionFile = {
name: string
content: string
}
export type EdgeFunctionBodyResponse = {
files: EdgeFunctionFile[]
}
async function streamToString(stream: ReadableStream<Uint8Array>) {
const reader = stream.getReader()
const decoder = new TextDecoder()
let result = ''
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
result += decoder.decode(value, { stream: true })
}
// Final decode to handle any remaining bytes
result += decoder.decode()
return result
} catch (error) {
console.error('Error reading stream:', error)
throw error
}
}
export async function getEdgeFunctionBody(
{ projectRef, slug }: EdgeFunctionBodyVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
if (!slug) throw new Error('slug is required')
const { data, response, error } = await get('/v1/projects/{ref}/functions/{function_slug}/body', {
params: { path: { ref: projectRef, function_slug: slug } },
headers: { Accept: 'multipart/form-data' },
parseAs: 'stream',
signal,
})
if (error) handleError(error)
const contentTypeHeader = response.headers.get('content-type') ?? ''
const boundary = getMultipartBoundary(contentTypeHeader)
const files = []
if (!data || !boundary) return { files: [] }
for await (let part of parseMultipartStream(data, { boundary })) {
if (part.isFile) {
files.push({
name: part.filename,
content: part.text,
})
}
}
return { files: files as EdgeFunctionFile[] }
}
export type EdgeFunctionBodyData = Awaited<ReturnType<typeof getEdgeFunctionBody>>
export type EdgeFunctionBodyError = ResponseError
export const useEdgeFunctionBodyQuery = <TData = EdgeFunctionBodyData>(
{ projectRef, slug }: EdgeFunctionBodyVariables,
{
enabled = true,
...options
}: UseQueryOptions<EdgeFunctionBodyData, EdgeFunctionBodyError, TData> = {}
) =>
useQuery<EdgeFunctionBodyData, EdgeFunctionBodyError, TData>({
queryKey: edgeFunctionsKeys.body(projectRef, slug),
queryFn: ({ signal }) => getEdgeFunctionBody({ projectRef, slug }, signal),
enabled:
IS_PLATFORM && enabled && typeof projectRef !== 'undefined' && typeof slug !== 'undefined',
...options,
})