@@ -16,26 +16,49 @@ import (
1616 "github.com/supabase/cli/internal/utils/flags"
1717)
1818
19+ type AuthMode string
20+
21+ const (
22+ AuthModeNone AuthMode = "none"
23+ AuthModeApiKey AuthMode = "apikey"
24+ AuthModeUser AuthMode = "user"
25+ )
26+
1927var (
20- //go:embed templates/index.ts
21- indexEmbed string
28+ //go:embed templates/index_auth_mode_none.ts
29+ indexAuthModeNoneEmbed string
30+ //go:embed templates/index_auth_mode_apikey.ts
31+ indexAuthModeApiKeyEmbed string
32+ //go:embed templates/index_auth_mode_user.ts
33+ indexAuthModeUserEmbed string
34+
2235 //go:embed templates/deno.json
2336 denoEmbed string
2437 //go:embed templates/.npmrc
2538 npmrcEmbed string
2639 //go:embed templates/config.toml
2740 configEmbed string
2841
29- indexTemplate = template .Must (template .New ("index" ).Parse (indexEmbed ))
42+ indexAuthTemplates = map [AuthMode ]* template.Template {
43+ AuthModeNone : template .Must (template .New ("index" ).Parse (indexAuthModeNoneEmbed )),
44+ AuthModeApiKey : template .Must (template .New ("index" ).Parse (indexAuthModeApiKeyEmbed )),
45+ AuthModeUser : template .Must (template .New ("index" ).Parse (indexAuthModeUserEmbed )),
46+ }
47+
3048 configTemplate = template .Must (template .New ("config" ).Parse (configEmbed ))
3149)
3250
3351type indexConfig struct {
34- URL string
35- Token string
52+ URL string
53+ PublishableKey string
54+ }
55+
56+ type functionConfig struct {
57+ Slug string
58+ VerifyJWT bool
3659}
3760
38- func Run (ctx context.Context , slug string , fsys afero.Fs ) error {
61+ func Run (ctx context.Context , slug string , authMode AuthMode , fsys afero.Fs ) error {
3962 // 1. Sanity checks.
4063 if err := utils .ValidateFunctionSlug (slug ); err != nil {
4164 return err
@@ -56,17 +79,18 @@ func Run(ctx context.Context, slug string, fsys afero.Fs) error {
5679 if err := flags .LoadConfig (fsys ); err != nil {
5780 fmt .Fprintln (utils .GetDebugLogger (), err )
5881 }
59- if err := createEntrypointFile (slug , fsys ); err != nil {
82+ if err := createEntrypointFile (slug , authMode , fsys ); err != nil {
6083 return err
6184 }
62- if err := appendConfigFile (slug , fsys ); err != nil {
85+ verifyJWT := authMode == AuthModeUser
86+ if err := appendConfigFile (slug , verifyJWT , fsys ); err != nil {
6387 return err
6488 }
6589 // 3. Create optional files
66- if err := afero .WriteFile (fsys , filepath .Join (funcDir , "deno.json" ), []byte (denoEmbed ), 0644 ); err != nil {
90+ if err := afero .WriteFile (fsys , filepath .Join (funcDir , "deno.json" ), []byte (denoEmbed ), 0o644 ); err != nil {
6791 return errors .Errorf ("failed to create deno.json config: %w" , err )
6892 }
69- if err := afero .WriteFile (fsys , filepath .Join (funcDir , ".npmrc" ), []byte (npmrcEmbed ), 0644 ); err != nil {
93+ if err := afero .WriteFile (fsys , filepath .Join (funcDir , ".npmrc" ), []byte (npmrcEmbed ), 0o644 ); err != nil {
7094 return errors .Errorf ("failed to create .npmrc config: %w" , err )
7195 }
7296 fmt .Println ("Created new Function at " + utils .Bold (funcDir ))
@@ -79,33 +103,40 @@ func Run(ctx context.Context, slug string, fsys afero.Fs) error {
79103 return nil
80104}
81105
82- func createEntrypointFile (slug string , fsys afero.Fs ) error {
106+ func createEntrypointFile (slug string , authMode AuthMode , fsys afero.Fs ) error {
83107 entrypointPath := filepath .Join (utils .FunctionsDir , slug , "index.ts" )
84- f , err := fsys .OpenFile (entrypointPath , os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0644 )
108+ f , err := fsys .OpenFile (entrypointPath , os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0o644 )
85109 if err != nil {
86110 return errors .Errorf ("failed to create entrypoint: %w" , err )
87111 }
88112 defer f .Close ()
113+ indexTemplate , hasTemplate := indexAuthTemplates [authMode ]
114+ if ! hasTemplate {
115+ return errors .Errorf ("failed to write entrypoint: '%v' is not a valid template" , authMode )
116+ }
89117 if err := indexTemplate .Option ("missingkey=error" ).Execute (f , indexConfig {
90- URL : utils .GetApiUrl ("/functions/v1/" + slug ),
91- Token : utils .Config .Auth .AnonKey .Value ,
118+ URL : utils .GetApiUrl ("/functions/v1/" + slug ),
119+ PublishableKey : utils .Config .Auth .PublishableKey .Value ,
92120 }); err != nil {
93121 return errors .Errorf ("failed to write entrypoint: %w" , err )
94122 }
95123 return nil
96124}
97125
98- func appendConfigFile (slug string , fsys afero.Fs ) error {
126+ func appendConfigFile (slug string , verifyJWT bool , fsys afero.Fs ) error {
99127 if _ , exists := utils .Config .Functions [slug ]; exists {
100128 fmt .Fprintf (os .Stderr , "[functions.%s] is already declared in %s\n " , slug , utils .Bold (utils .ConfigPath ))
101129 return nil
102130 }
103- f , err := fsys .OpenFile (utils .ConfigPath , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0644 )
131+ f , err := fsys .OpenFile (utils .ConfigPath , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0o644 )
104132 if err != nil {
105133 return errors .Errorf ("failed to append config: %w" , err )
106134 }
107135 defer f .Close ()
108- if err := configTemplate .Option ("missingkey=error" ).Execute (f , slug ); err != nil {
136+ if err := configTemplate .Option ("missingkey=error" ).Execute (f , functionConfig {
137+ Slug : slug ,
138+ VerifyJWT : verifyJWT ,
139+ }); err != nil {
109140 return errors .Errorf ("failed to append template: %w" , err )
110141 }
111142 return nil
0 commit comments