import { useParams } from 'common/hooks' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { Button, IconBookOpen, IconCode, IconMaximize2, IconMinimize2, IconTerminal } from 'ui' import CommandRender from 'components/interfaces/Functions/CommandRender' import { useAccessTokensQuery } from 'data/access-tokens/access-tokens-query' import { useProjectApiQuery } from 'data/config/project-api-query' import { useCustomDomainsQuery } from 'data/custom-domains/custom-domains-query' import { Commands } from './Functions.types' interface TerminalInstructionsProps { closable?: boolean removeBorder?: boolean } const TerminalInstructions = ({ closable = false, removeBorder = false, }: TerminalInstructionsProps) => { const router = useRouter() const { ref: projectRef } = useParams() const [showInstructions, setShowInstructions] = useState(!closable) const { data: tokens } = useAccessTokensQuery() const { data: settings } = useProjectApiQuery({ projectRef }) const { data: customDomainData } = useCustomDomainsQuery({ projectRef }) const apiService = settings?.autoApiService const anonKey = apiService?.service_api_keys.find((x) => x.name === 'anon key') ? apiService.defaultApiKey : '[YOUR ANON KEY]' const endpoint = settings?.autoApiService.app_config.endpoint ?? '' const functionsEndpoint = customDomainData?.customDomain?.status === 'active' ? `https://${customDomainData.customDomain.hostname}/functions/v1` : `https://${endpoint}/functions/v1` // get the .co or .net TLD from the restUrl const restUrl = settings?.autoApiService.restUrl const restUrlTld = restUrl ? new URL(restUrl).hostname.split('.').pop() : '' 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 ${ anonKey ?? '[YOUR ANON KEY]' }' --data '{"name":"Functions"}'`, description: 'Invokes the hello-world function', jsx: () => { return ( <> curl -L -X POST '{functionsEndpoint} /hello-world' -H 'Authorization: Bearer [YOUR ANON KEY]'{' '} {`--data '{"name":"Functions"}'`} ) }, comment: 'Invoke your function', }, ] return (

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.

)}
) } export default TerminalInstructions