import { PermissionAction } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { ExternalLink, Maximize2, Minimize2, Terminal } from 'lucide-react' import { useRouter } from 'next/router' import { ComponentPropsWithoutRef, ElementRef, forwardRef, useState } from 'react' import { Button, Collapsible, CollapsibleContent, CollapsibleTrigger } from 'ui' import type { Commands } from './Functions.types' import CommandRender from '@/components/interfaces/Functions/CommandRender' import { DocsButton } from '@/components/ui/DocsButton' import { useAccessTokensQuery } from '@/data/access-tokens/access-tokens-query' import { useAPIKeys } from '@/data/api-keys/api-keys-query' import { useProjectApiUrl } from '@/data/config/project-endpoint-query' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' import { DOCS_URL } from '@/lib/constants' interface TerminalInstructionsProps extends ComponentPropsWithoutRef { closable?: boolean removeBorder?: boolean } export const TerminalInstructions = forwardRef< ElementRef, TerminalInstructionsProps >(({ closable = false, removeBorder = false, ...props }, ref) => { const router = useRouter() const { ref: projectRef } = useParams() const [showInstructions, setShowInstructions] = useState(!closable) const { data: tokens } = useAccessTokensQuery() const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*') const { data: apiKeyData } = useAPIKeys({ projectRef }, { enabled: canReadAPIKeys }) const { anonKey, publishableKey } = apiKeyData ?? {} const { data: endpoint } = useProjectApiUrl({ projectRef }) const functionsEndpoint = `${endpoint}/functions/v1` const apiKey = publishableKey?.api_key ?? anonKey?.api_key ?? '[YOUR ANON KEY]' // get the .co or .net TLD from the restUrl const restUrl = `https://${endpoint}` const restUrlTld = !!endpoint ? new URL(restUrl).hostname.split('.').pop() : 'co' const commands: Commands[] = [ { command: 'supabase functions new hello-world', description: ' creates a function stub at ./functions/hello-world/index.ts', jsx: () => { return ( <> supabase functions new hello-world ) }, comment: 'Create a function', }, { command: `supabase functions deploy hello-world --project-ref ${projectRef}`, description: 'Deploys function at ./functions/hello-world/index.ts', jsx: () => { return ( <> supabase functions deploy hello-world --project-ref {projectRef} ) }, comment: 'Deploy your function', }, { command: `curl -L -X POST 'https://${projectRef}.supabase.${restUrlTld}/functions/v1/hello-world' -H 'Authorization: Bearer ${apiKey}'${anonKey?.type === 'publishable' ? ` -H 'apikey: ${apiKey}'` : ''} --data '{"name":"Functions"}'`, description: 'Invokes the hello-world function', jsx: () => { return ( <> curl -L -X POST '{functionsEndpoint} /hello-world' -H 'Authorization: Bearer [YOUR ANON KEY]' {anonKey?.type === 'publishable' ? " -H 'apikey: [YOUR ANON KEY]' " : ''} {`--data '{"name":"Functions"}'`} ) }, comment: 'Invoke your function', }, ] return ( setShowInstructions(!showInstructions)} {...props} >

Create your first Edge Function via the CLI

{closable && (
setShowInstructions(!showInstructions)}> {showInstructions ? ( ) : ( )}
)}
{tokens && tokens.length === 0 ? (

You may need to create an access token

You can create a secure access token in your account section

) : (

Need help?

Read the documentation, or browse some sample code.

)}
) }) TerminalInstructions.displayName = 'TerminalInstructions'