Files
supabase/apps/studio/data/edge-functions/edge-function-body-query.ts
Kalleby SantosandCharis Lam 997203cd64 feat(studio-local): functions management api - function blob artifacts (#42349)
## What kind of change does this PR introduce?

Feature

## What is the current behavior?

Functions page on self-hosted differs from Platform

## What is the new behavior?

> [!NOTE]  
> This PR only add readonly operations. Function edit and deploy should
be implemented in a future one.

Adds the possibility to download and see function code in Self-Host
version.

<details>

<img width="1465" height="944" alt="image"
src="https://github.com/user-attachments/assets/4bbf8f5c-3390-4de6-9e8b-8ec9cd59ebad"
/>

</details>

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* API endpoint to stream function files/artifacts as
multipart/form-data.
* New function file entry type and server-side file listing for
functions.

* **Improvements**
  * Edge Functions "Code" navigation item always visible.
* Download popover reworked: ZIP download always available; CLI section
shown only on supported platforms.
* Editor set to read-only and file actions disabled on unsupported
environments.

* **Editor**
* Added JavaScript, TypeScript, and Markdown language modules for the
embedded editor.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-02-06 20:06:37 +00:00

78 lines
2.3 KiB
TypeScript

import { getMultipartBoundary, parseMultipartStream } from '@mjackson/multipart-parser'
import { useQuery } from '@tanstack/react-query'
import { FileData } from '@/components/ui/FileExplorerAndEditor/FileExplorerAndEditor.types'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { edgeFunctionsKeys } from './keys'
type EdgeFunctionBodyVariables = {
projectRef?: string
slug?: string
}
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: [] }
let metadata: {
deno2_entrypoint_path?: string | null
} = {}
for await (let part of parseMultipartStream(data, {
boundary,
maxFileSize: 20 * 1024 * 1024,
})) {
if (part.isFile) {
files.push({
name: part.filename,
content: part.text,
})
} else {
// treat it as metadata
metadata = JSON.parse(part.text)
}
}
return {
metadata,
files: files as Omit<FileData, 'id' | 'selected' | 'state'>[],
}
}
export type EdgeFunctionBodyData = Awaited<ReturnType<typeof getEdgeFunctionBody>>
export type EdgeFunctionBodyError = ResponseError
export const useEdgeFunctionBodyQuery = <TData = EdgeFunctionBodyData>(
{ projectRef, slug }: EdgeFunctionBodyVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<EdgeFunctionBodyData, EdgeFunctionBodyError, TData> = {}
) =>
useQuery<EdgeFunctionBodyData, EdgeFunctionBodyError, TData>({
queryKey: edgeFunctionsKeys.body(projectRef, slug),
queryFn: ({ signal }) => getEdgeFunctionBody({ projectRef, slug }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof slug !== 'undefined',
...options,
})