Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: export class crashes in 'use server' module #1689

Closed
2 tasks done
binajmen opened this issue Dec 10, 2024 · 2 comments
Closed
2 tasks done

[Bug?]: export class crashes in 'use server' module #1689

binajmen opened this issue Dec 10, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@binajmen
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

[h3] [unhandled] H3Error: undefined does not match field "params": [Pattern] of type FunctionExpression
    at addParam (/Users/binajmen/Developer/fbnb/cockpit/node_modules/ast-types/lib/types.js:455:27)
    ... 7 lines matching cause stack trace ...
    at async loadAndTransform (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:51929:27) {
  cause: Error: undefined does not match field "params": [Pattern] of type FunctionExpression
      at addParam (/Users/binajmen/Developer/fbnb/cockpit/node_modules/ast-types/lib/types.js:455:27)
      at /Users/binajmen/Developer/fbnb/cockpit/node_modules/ast-types/lib/types.js:505:25
      at Array.forEach (<anonymous>)
      at builder.from (/Users/binajmen/Developer/fbnb/cockpit/node_modules/ast-types/lib/types.js:503:46)
      at wrapExports (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/@vinxi/plugin-directives/plugins/wrap-exports.js:393:45)
      at Object.transform (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/@vinxi/plugin-directives/plugins/wrap-exports.js:44:24)
      at TransformPluginContext.transform (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/@vinxi/plugin-directives/plugin.js:52:31)
      at PluginContainer.transform (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:49097:19)
      at async loadAndTransform (file:///Users/binajmen/Developer/fbnb/cockpit/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:51929:27) {
    plugin: 'vite-server-references',
    id: '/Users/binajmen/Developer/fbnb/cockpit/src/application/auth/auth.service.ts',
    pluginCode: '"use server";\n' +
      'import assert from "node:assert";\n' +
      'import { redirect } from "@solidjs/router";\n' +
      'import { type SessionConfig, useSession } from "vinxi/http";\n' +
      'import type { UserRepositoryInterface } from "~/domain/user/user.interface";\n' +
      'import { UserRepository } from "~/domain/user/user.repository";\n' +
      'import type { AuthServiceInterface } from "./auth.interface";\n' +
      '\n' +
      'assert(process.env.SESSION_SECRET, "SESSION_SECRET is required");\n' +
      '\n' +
      'const sessionConfig = {\n' +
      '  name: "cockpit",\n' +
      '  maxAge: 60 * 60 * 24 * 14,\n' +
      '  password: process.env.SESSION_SECRET,\n' +
      '  cookie: {\n' +
      '    secure: process.env.NODE_ENV === "production",\n' +
      '    httpOnly: true,\n' +
      '  },\n' +
      '} satisfies SessionConfig;\n' +
      '\n' +
      'type Session = {\n' +
      '  user_id?: string;\n' +
      '};\n' +
      '\n' +
      'function getSession() {\n' +
      '  return useSession<Session>(sessionConfig);\n' +
      '}\n' +
      '\n' +
      'export class AuthService implements AuthServiceInterface {\n' +
      '  userRepo: UserRepositoryInterface;\n' +
      '\n' +
      '  constructor(userRepo?: UserRepositoryInterface) {\n' +
      '    this.userRepo = userRepo || new UserRepository();\n' +
      '  }\n' +
      '\n' +

Expected behavior 🤔

No response

Steps to reproduce 🕹

If I refactor and split my class AuthService in functions, I don't get an error.

Context 🔦

Most probably linked to #1226

Your environment 🌎

No response

@binajmen binajmen added the bug Something isn't working label Dec 10, 2024
@binajmen
Copy link
Author

To complete the issue.

This will work

"use server";
import { eq } from "drizzle-orm";
import { db } from "~/db/client";
import { users } from "~/db/schema";
import type { InsertUser } from "./user.entity";
import type { UserRepositoryInterface } from "./user.interface";

export class UserRepository implements UserRepositoryInterface {
  async create(values: InsertUser) {
    const [row] = await db.insert(users).values(values).returning();
    return row;
  }

  async findAll() {
    return db.select().from(users);
  }

  async findById(id: string) {
    const row = await db.select().from(users).where(eq(users.id, id));
    return row.length > 0 ? row[0] : null;
  }

  async findByEmail(email: string) {
    const row = await db.select().from(users).where(eq(users.email, email));
    return row.length > 0 ? row[0] : null;
  }

  async update(id: string, values: Partial<InsertUser>) {
    const [row] = await db
      .update(users)
      .set(values)
      .where(eq(users.id, id))
      .returning();
    return row;
  }

  async delete(id: string) {
    await db.delete(users).where(eq(users.id, id));
  }
}

This will work

import { eq } from "drizzle-orm";
import { db } from "~/db/client";
import { users } from "~/db/schema";
import type { InsertUser } from "./user.entity";
import type { UserRepositoryInterface } from "./user.interface";

export class UserRepository implements UserRepositoryInterface {
  async create(values: InsertUser) {
    "use server";
    const [row] = await db.insert(users).values(values).returning();
    return row;
  }

  async findAll() {
    "use server";
    return db.select().from(users);
  }

  async findById(id: string) {
    "use server";
    const row = await db.select().from(users).where(eq(users.id, id));
    return row.length > 0 ? row[0] : null;
  }

  async findByEmail(email: string) {
    "use server";
    const row = await db.select().from(users).where(eq(users.email, email));
    return row.length > 0 ? row[0] : null;
  }

  async update(id: string, values: Partial<InsertUser>) {
    "use server";
    const [row] = await db
      .update(users)
      .set(values)
      .where(eq(users.id, id))
      .returning();
    return row;
  }

  async delete(id: string) {
    "use server";
    await db.delete(users).where(eq(users.id, id));
  }
}

I don't know if this is expected or not 🤷‍♂️

@binajmen
Copy link
Author

Following a discussion with ryan on discord, use server should be used in the function as expected. It does not make sense to export a class in the use server context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant