Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: edge function deno types (#34281)

Greg Richardson6a506d39ea56
diff --git a/apps/studio/components/ui/AIEditor/index.tsx b/apps/studio/components/ui/AIEditor/index.tsx
index c6d804fbb0b..753eef57c5c 100644
--- a/apps/studio/components/ui/AIEditor/index.tsx
+++ b/apps/studio/components/ui/AIEditor/index.tsx
@@ -10,6 +10,9 @@ import { constructHeaders } from 'data/fetchers'
import { detectOS } from 'lib/helpers'
import ResizableAIWidget from './ResizableAIWidget'
+// Special asset/resource import that fetches deno types at build time
+import denoTypesUrl from 'https://github.com/denoland/deno/releases/latest/download/lib.deno.d.ts?resource'
+
interface AIEditorProps {
id?: string
language?: string
@@ -125,6 +128,20 @@ const AIEditor = ({
}
}
+ // Add deno types to the TS language service
+ fetch(denoTypesUrl)
+ .then((response) => response.text())
+ .then((code) => {
+ monaco.languages.typescript.typescriptDefaults.addExtraLib(code)
+ })
+
+ // Add edge runtime types to the TS language service
+ fetch('https://jsr.io/@supabase/functions-js/2.4.4/src/edge-runtime.d.ts')
+ .then((response) => response.text())
+ .then((code) => {
+ monaco.languages.typescript.typescriptDefaults.addExtraLib(code)
+ })
+
if (!!executeQueryRef.current) {
editor.addAction({
id: 'run-query',
diff --git a/apps/studio/next.config.js b/apps/studio/next.config.js
index 2980f24a8f2..82a5e850618 100644
--- a/apps/studio/next.config.js
+++ b/apps/studio/next.config.js
@@ -55,12 +55,13 @@ const SUPABASE_ASSETS_URL =
process.env.NEXT_PUBLIC_ENVIRONMENT === 'staging'
? 'https://frontend-assets.supabase.green'
: 'https://frontend-assets.supabase.com'
+const JSR_URL = 'https://jsr.io'
// used by vercel live preview
const PUSHER_URL = 'https://*.pusher.com'
const PUSHER_URL_WS = 'wss://*.pusher.com'
-const DEFAULT_SRC_URLS = `${API_URL} ${SUPABASE_URL} ${GOTRUE_URL} ${SUPABASE_LOCAL_PROJECTS_URL_WS} ${SUPABASE_PROJECTS_URL} ${SUPABASE_PROJECTS_URL_WS} ${HCAPTCHA_SUBDOMAINS_URL} ${CONFIGCAT_URL} ${STRIPE_SUBDOMAINS_URL} ${STRIPE_NETWORK_URL} ${CLOUDFLARE_URL} ${ONE_ONE_ONE_ONE_URL} ${VERCEL_INSIGHTS_URL} ${GITHUB_API_URL} ${GITHUB_USER_CONTENT_URL} ${SUPABASE_ASSETS_URL}`
+const DEFAULT_SRC_URLS = `${API_URL} ${SUPABASE_URL} ${GOTRUE_URL} ${SUPABASE_LOCAL_PROJECTS_URL_WS} ${SUPABASE_PROJECTS_URL} ${SUPABASE_PROJECTS_URL_WS} ${HCAPTCHA_SUBDOMAINS_URL} ${CONFIGCAT_URL} ${STRIPE_SUBDOMAINS_URL} ${STRIPE_NETWORK_URL} ${CLOUDFLARE_URL} ${ONE_ONE_ONE_ONE_URL} ${VERCEL_INSIGHTS_URL} ${GITHUB_API_URL} ${GITHUB_USER_CONTENT_URL} ${SUPABASE_ASSETS_URL} ${JSR_URL}`
const SCRIPT_SRC_URLS = `${CLOUDFLARE_CDN_URL} ${HCAPTCHA_JS_URL} ${STRIPE_JS_URL} ${SUPABASE_ASSETS_URL}`
const FRAME_SRC_URLS = `${HCAPTCHA_ASSET_URL} ${STRIPE_JS_URL}`
const IMG_SRC_URLS = `${SUPABASE_URL} ${SUPABASE_COM_URL} ${SUPABASE_PROJECTS_URL} ${GITHUB_USER_AVATAR_URL} ${GOOGLE_USER_AVATAR_URL} ${SUPABASE_ASSETS_URL}`
@@ -123,6 +124,10 @@ const nextConfig = {
output: 'standalone',
experimental: {
webpackBuildWorker: true,
+ urlImports: [
+ // Used to import the Deno types passed on Monaco's typescript language service
+ 'https://github.com/denoland/deno',
+ ],
},
async rewrites() {
return [
@@ -524,6 +529,10 @@ const nextConfig = {
source: '/favicon/:slug*',
headers: [{ key: 'cache-control', value: 'public, max-age=86400' }],
},
+ {
+ source: '/(.*).ts',
+ headers: [{ key: 'content-type', value: 'text/typescript' }],
+ },
]
},
images: {
@@ -576,6 +585,25 @@ const nextConfig = {
}
})
+ // Find the parent `oneOf` rule that contains next-swc-loader
+ const tsRules = config.module.rules.find(
+ (rule) =>
+ 'oneOf' in rule &&
+ rule.oneOf.some(
+ (r) =>
+ Array.isArray(r.use) &&
+ r.use.some((u) => 'loader' in u && u.loader === 'next-swc-loader')
+ )
+ )
+
+ // Add custom loader that treats `?resource` imports as an asset/resource without further processing
+ // This allows us to import `.ts` files as assets without further processing through the SWC loader
+ // Used to load raw Deno `.d.ts` types as source assets passed to Monaco's typescript language service
+ tsRules.oneOf.unshift({
+ resourceQuery: /resource/,
+ type: 'asset/resource',
+ })
+
// .md files to be loaded as raw text
config.module.rules.push({
test: /\.md$/,
diff --git a/apps/studio/next.lock/lock.json b/apps/studio/next.lock/lock.json
new file mode 100644
index 00000000000..96f9d1499b0
--- /dev/null
+++ b/apps/studio/next.lock/lock.json
@@ -0,0 +1,4 @@
+{
+ "https://github.com/denoland/deno/releases/latest/download/lib.deno.d.ts?resource": "no-cache",
+ "version": 1
+}
diff --git a/apps/studio/tests/loaders/asset-resource-loader.mjs b/apps/studio/tests/loaders/asset-resource-loader.mjs
new file mode 100644
index 00000000000..4fdad7f3bb7
--- /dev/null
+++ b/apps/studio/tests/loaders/asset-resource-loader.mjs
@@ -0,0 +1,16 @@
+/**
+ * Simulates a custom webpack asset/resource loader we use in Next.js/webpack
+ */
+export function load(url, context, nextLoad) {
+ if (url.endsWith('?resource')) {
+ // Simply return the same URL for testing purposes
+ return {
+ format: 'module',
+ shortCircuit: true,
+ source: `export default ${JSON.stringify(url)}`,
+ }
+ }
+
+ // Otherwise let Node.js handle the import
+ return nextLoad(url)
+}
diff --git a/apps/studio/tests/loaders/register.mjs b/apps/studio/tests/loaders/register.mjs
new file mode 100644
index 00000000000..79b3652bbfd
--- /dev/null
+++ b/apps/studio/tests/loaders/register.mjs
@@ -0,0 +1,4 @@
+import { register } from 'node:module'
+import { pathToFileURL } from 'node:url'
+
+register('./tests/loaders/asset-resource-loader.mjs', pathToFileURL('./'))
diff --git a/apps/studio/types/resource.d.ts b/apps/studio/types/resource.d.ts
new file mode 100644
index 00000000000..b027b0aa019
--- /dev/null
+++ b/apps/studio/types/resource.d.ts
@@ -0,0 +1,4 @@
+declare module '*?resource' {
+ const url: string
+ export default url
+}
diff --git a/apps/studio/vitest.config.mts b/apps/studio/vitest.config.mts
index 84a31f0c53f..515becd5191 100644
--- a/apps/studio/vitest.config.mts
+++ b/apps/studio/vitest.config.mts
@@ -9,6 +9,9 @@ import { defineConfig } from 'vitest/config'
// `setupFiles` live next to the test file itself. This forces them to always resolve correctly.
const dirname = fileURLToPath(new URL('.', import.meta.url))
+// Register custom loaders to emulate webpack's behavior
+const nodeArgs = ['--import=./tests/loaders/register.mjs']
+
export default defineConfig({
plugins: [
react(),
@@ -26,6 +29,12 @@ export default defineConfig({
environment: 'jsdom', // TODO(kamil): This should be set per test via header in .tsx files only
include: [resolve(dirname, './**/*.test.{ts,tsx}')],
restoreMocks: true,
+ poolOptions: {
+ threads: { execArgv: nodeArgs },
+ forks: { execArgv: nodeArgs },
+ vmThreads: { execArgv: nodeArgs },
+ vmForks: { execArgv: nodeArgs },
+ },
setupFiles: [
resolve(dirname, './tests/vitestSetup.ts'),
resolve(dirname, './tests/setup/polyfills.js'),