feat: edge functions get tshirt competition

This commit is contained in:
thorwebdev committed 2022-10-10 11:31:35 +08:00
1 parent 0aac625f18
commit d577ce9b98
3 files changed
+97

No files matched your search

@@ -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') ?? ''
)
@@ -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
```
@@ -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'