encore icon indicating copy to clipboard operation
encore copied to clipboard

Can't import Supabase types

Open Jayllyz opened this issue 11 months ago • 1 comments

Environments :

macos: 15.2 "encore.dev": "^1.46.7" @supabase/supabase-js: "^2.49.1" node: 22.14

Having issue to setup Supabase with encore.dev :

Errors :

encore run
  ❌ Building Encore application graph... Failed: parse error
  ⠙ Analyzing service topology... 
error: object not found: User
 --> /Users/<>/code/test-encore/auth/routes.ts:1:19
  |
1 | import { Session, User } from "@supabase/supabase-js";
  |                   ^^^^


error: unknown identifier
  --> /Users/<>/code/test-encore/auth/routes.ts:11:9
   |
11 |   user: User | null;
   |         ^^^^


error: object not found: Session
 --> /Users/<>/code/test-encore/auth/routes.ts:1:10
  |
1 | import { Session, User } from "@supabase/supabase-js";
  |          ^^^^^^^


error: unknown identifier
  --> /Users/<>/code/test-encore/auth/routes.ts:12:12
   |
12 |   session: Session | null;
   |            ^^^^^^^

To reproduce :

import { Session, User } from "@supabase/supabase-js";
import { getSupabaseClient } from "./db/supabaseClient";
import { api, APIError, ErrCode } from "encore.dev/api";

interface LoginRequest {
  email: string;
  password: string;
}

interface LoginResponse {
  user: User | null;
  session: Session | null;
}

type SignupRequest = LoginRequest;
type SignupResponse = LoginResponse;

export const login = api({expose: true, auth: false, method: "POST", path: "/auth/login"}, 
    async ({ email, password }: LoginRequest): Promise<LoginResponse> => {
      const { supabaseAnon } = getSupabaseClient();
      if (!email || !password) {
        throw APIError.invalidArgument("Email and password are required");
      }
  
      const { data, error } = await supabaseAnon.auth.signInWithPassword({
        email: email,
        password: password,
      });
  
      if (error) {
        throw APIError.invalidArgument(error.message);
      }
  
      return {
        user: data?.user || null,
        session: data?.session || null,
      };
    });
  
  export const register = api({expose: true, auth: false, method: "POST", path: "/auth/signup"},
    async ({ email, password }: SignupRequest): Promise<SignupResponse> => {
      const { supabaseAnon } = getSupabaseClient();
      if (!email || !password) {
        throw APIError.invalidArgument("Email and password are required");
      }
  
      const { data, error } = await supabaseAnon.auth.signUp({
        email: email,
        password: password,
      });
  
      if (error) {
        throw APIError.invalidArgument(error.message);
      }
  
      return {
        user: data?.user || null,
        session: data?.session || null,
      };
    });

Jayllyz avatar Mar 10 '25 18:03 Jayllyz

Supabase may use complex types not yet supported in Encore.ts when defining APIs. You can solve this by designing your own API interface rather than just exposing your database schema as the API (which is generally not a good idea anyway).

marcuskohlberg avatar Mar 18 '25 09:03 marcuskohlberg