I know (see my contribution). Maybe I made an error in the index.ts. I just pasted the code from Supabase at the beginning of the WeWeb index.ts code (and changed the path to cors.ts as well as the API key).
Here’s the complete index.ts, maybe you see the error. VS only says that name Deno is unknown.
// Set the API key for the Resend service
const RESEND_API_KEY = ‘xxxxx’
import { corsHeaders } from ‘d:/_shared/cors.ts’
console.log(Function "browser-with-cors" up and running!
)
Deno.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 { name } = await req.json()
const data = {
message: Hello ${name}!
,
}
return new Response(JSON.stringify(data), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
})
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
headers: { …corsHeaders, ‘Content-Type’: ‘application/json’ },
status: 400,
})
}
})
// Define an asynchronous function (handler) to handle requests with parameters
const handler = async (_request: Request, from: string, to: string, subject: string, html: string): Promise => {
// Send a POST request to the Resend API to send an email
const res = await fetch(‘https://api.resend.com/emails’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’, // Set the content type to JSON
Authorization: Bearer ${RESEND_API_KEY}
, // Include the API key in the Authorization header
},
body: JSON.stringify({
from: from,
to: to,
subject: subject,
html: html,
}),
})
// Parse the response as JSON
const data = await res.json()
// Return a new Response object with the JSON data, status 200, and appropriate headers
return new Response(JSON.stringify(data), {
status: 200,
headers: {
‘Content-Type’: ‘application/json’,
},
})
}
// Start a Deno HTTP server and pass the handler function with dynamic “from”, “to”, “subject”, and “html” values
Deno.serve(async (req) => {
const { searchParams } = new URL(req.url)
// Example: Extract “from” and “to” values from query parameters
const from = searchParams.get(‘from’) || ‘defaultfrom@example.com’
const to = searchParams.get(‘to’) || ‘defaultto@example.com’
const subject = searchParams.get(‘subject’) || ‘Best email ever
’
const html = searchParams.get(‘html’) || ‘It works!’
// Call the modified handler function with dynamic “from”,“to”, “subject”, and “html” values
return await handler(req, from, to, subject, html)
})