课程: Supabase Essential Training
Deploying edge functions
- [Instructor] Edge functions are the one major feature of Supabase running outside of PostgreSQL. Since they're not PostgreSQL driven, they must be deployed from the command line rather than through the Supabase dashboard. Let's write and deploy an edge function now. You'll notice here I'm on the Edge Functions control panel of my Supabase instance, and there are some instructions here for creating and running your first edge function. I'm going to follow this process, but I'm going to create a function that has a different name. I'm going to go to Visual Studio Code now and I'm going to create this new function. So that is, supabase functions new. And let's call this one promotion. So that created a function named promotion and it still has this Hello World setup. We're going to change this just a little bit so that it's more specific to our use case. We're going to say, sale being generated. And then instead of a name, I want an offerCode, and then the message will be, save with this offer code. And then finally, let's also just kick that offerCode back into the response as well on its own. All right, now let's go ahead and deploy this function. To deploy the edge function, it has to build locally. The build relies on running a Supabase Docker image and I already have that running. So let's go back to the control panel for a second and let's grab these instructions for deploying the function. We need the project ref, and so I'm going to copy that here. And then it's supabase functions deploy, and then the name of the function. So I'm going to type in supabase functions deploy promotion. And one thing that you need to do before doing the deploy is save the file. A lot of times we're using IDEs where the files are being saved automatically, and in this case it was not. So I needed to make sure to save that file before we deploy it. And now I'm going to paste in that project ref. And now I'm going to hit Enter. And that deployed it. I'm now going to clear the screen and let's go back to the control panel. Now we have our promotion. If I click through to it, we have this detail screen. And now let's go to Metrics. We haven't run this edge function yet, so none of these graphs are showing anything. Let's run this function and see how it turns out. I'm going to go scroll down to the bottom of this screen and there are some instructions here for how to make an HTTP request. You'll notice that this is pointing to a local development environment. I'd actually like to run it in the cloud since it is up there now and we want to see those metrics. So let's change this URL first. I'm going to go back to the Details panel of Edge Functions and copy this URL and then I'm going to paste it in here and replace the local URL. This Authorization Bearer is going to change because this is pointing to a local JSON web token. And JSON web tokens are a way of doing authentication on the web. So I'm going to grab that token from Project Settings, API, and Project API keys under anonymous public. I'm going to go back to Edge Functions and pull this back up to metrics again. And I'm going to paste this in here. And let's also change the data. We have offerCode, and that offerCode is going to be PANS20. Now I'm going to copy this entire command and paste it into the terminal. And it ran our code there in the cloud. Save with this offer code, PANS20. If we go back to the Edge Functions, this should update in just a moment. There we go, we've got our invocation here. I'm going to go back to my code and I'm going to run this again, but this time I'm going to remove the header. And if I run this without the header, it's going to error out on us because we are missing the authorization. That is because our edge function is enforcing JWT verification. If I change this and save that to not enforce JWT verification, I can run it again without authentication. And there it works. When you need to run code outside of the PostgreSQL environment with maximum flexibility, edge functions are ready for you. Remember, you can have Supabase automatically require the public anonymous JWT.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。