From d577ce9b983ca6ea157b93c530019daa4f58dca8 Mon Sep 17 00:00:00 2001 From: thorwebdev Date: Mon, 10 Oct 2022 11:31:35 +0800 Subject: [PATCH] feat: edge functions get tshirt competition --- .../functions/_shared/supabaseClient.ts | 7 ++ .../get-tshirt-competition/README.md | 25 +++++++ .../functions/get-tshirt-competition/index.ts | 65 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 examples/edge-functions/supabase/functions/get-tshirt-competition/README.md create mode 100644 examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts diff --git a/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts b/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts index 2205294d529..36811de9b4a 100644 --- a/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts +++ b/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts @@ -6,3 +6,10 @@ export const supabaseClient = createClient( // Supabase API ANON KEY - env var exported by default when deployed. Deno.env.get('SUPABASE_ANON_KEY') ?? '' ) + +export const supabaseAdminClient = createClient( + // Supabase API URL - env var exported by default when deployed. + Deno.env.get('SUPABASE_URL') ?? '', + // Supabase API ANON KEY - env var exported by default when deployed. + Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? '' +) diff --git a/examples/edge-functions/supabase/functions/get-tshirt-competition/README.md b/examples/edge-functions/supabase/functions/get-tshirt-competition/README.md new file mode 100644 index 00000000000..9f6033295a7 --- /dev/null +++ b/examples/edge-functions/supabase/functions/get-tshirt-competition/README.md @@ -0,0 +1,25 @@ +# GET T-Shirt competition + +To celebrate the support of GET requests in Supabase Edge Functions we're giving away some limited edition Functions Shirts! + +To win a Supabase Edge Functions T-Shirt, make a GET request to https://obuldanrptloktxcffvn.functions.supabase.co/get-tshirt-competition with your email address, your Twitter handle (optional), and the correct answer, e.g. + +```text +https://obuldanrptloktxcffvn.functions.supabase.co/get-tshirt-competition?email=testr@test.de&twitter=thorwebdev&answer=20 +``` + +You can read the functions source code to figure out the answer ;) + +Good luck trying to GET a T-Shirt! + +### Serve this function locally + +```bash +supabase functions serve --no-verify-jwt get-tshirt-competition +``` + +### Deploy this function + +```bash +supabase functions deploy --no-verify-jwt get-tshirt-competition +``` diff --git a/examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts b/examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts new file mode 100644 index 00000000000..9464b117aed --- /dev/null +++ b/examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts @@ -0,0 +1,65 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +import { serve } from 'https://deno.land/std@0.131.0/http/server.ts' +import { supabaseAdminClient } from '../_shared/supabaseClient.ts' +import { corsHeaders } from '../_shared/cors.ts' + +console.log(`Function "get-tshirt-competition" up and running!`) + +function positionInAlphabet(myChar: string): number { + const DIFFERENCE_CHARCODE_AND_LETTERS = 96 + // Convert the character into lowercase + const myCharLowercase = myChar.toLowerCase() + // Find the position of the char in the alphabet + const position = myCharLowercase.charCodeAt(0) - DIFFERENCE_CHARCODE_AND_LETTERS + return position +} + +serve(async (req) => { + // This is needed if you're planning to invoke your function from a browser. + if (req.method === 'OPTIONS') { + return new Response('ok', { headers: corsHeaders }) + } + + try { + const url = new URL(req.url) + const email = url.searchParams.get('email') + const twitter = url.searchParams.get('twitter') + const answer = url.searchParams.get('answer') + + if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email ?? '') || !answer) + throw new Error( + `You're missing either a valid 'email' and/or 'answer' param(s). HINT: https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts` + ) + + if (Number(answer) !== positionInAlphabet(email!)) + throw new Error( + `Sorry, that's wrong, please try again! HINT: https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts` + ) + + // Submit email to draw + const { error } = await supabaseAdminClient.from('get-tshirt-competition').insert({ + email, + twitter, + }) + if (error) { + console.log(error) + throw new Error(error.details) + } + + return new Response(`Thanks for playing! ${email} has been added to the draw \\o/`, { + headers: { ...corsHeaders, 'Content-Type': 'text/plain' }, + status: 200, + }) + } catch (error) { + return new Response(JSON.stringify({ error: error.message }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400, + }) + } +}) + +// To invoke: +// curl -i --location --request GET 'http://localhost:54321/functions/v1/get-tshirt-competition?email=testr@test.de&answer=20'