Skip to content

[typegen] FetchAdapter not receiving auth configuration #49

@HosakaKeigo

Description

@HosakaKeigo

Description

There is a bug(?) in the buildAdapter function in packages/typegen/src/buildLayoutClient.ts that prevents FetchAdapter from being properly initialized with auth configuration.

The Problem

In the buildAdapter function (lines 156-183), the condition check for FetchAdapter is incorrect:

// Current code (line 158-160)
.inlineBlock(() => {
  if (typeof envNames.auth !== "object" || !("apiKey" in envNames.auth))
    return;

This condition checks for apiKey, but FetchAdapter should use username and password authentication. Since FetchAdapter users don't have apiKey, this condition always returns early, preventing any configuration from being written.

Reproduction Steps

  1. Create a configuration with username/password authentication:
{
  "clientSuffix": "Layout",
  "path": "./src/schema",
  "clearOldFiles": false,
  "envNames": {
    "auth": {
      "username": "MY_FM_USERNAME",
      "password": "MY_FM_PASSWORD"
    },
    "server": "MY_FM_SERVER",
    "db": "MY_FM_DATABASE_AB"
  },
  "layouts": [
    {
      "schemaName": "MySchema",
      "layoutName": "MyLayout"
    }
  ],
  "validator": "zod/v3"
}
  1. Run codegen(pnpm dlx @proofkit/typegen@latest

Current Behavior

The generated code produces:

adapter: new FetchAdapter({}),

The adapter receives an empty configuration object, missing all required settings.

Expected Behavior

The generated code should produce:

adapter: new FetchAdapter({
  auth: {
    username: process.env.MY_FM_USERNAME,
    password: process.env.MY_FM_PASSWORD
  },
  db: process.env.MY_FM_DATABASE_AB,
  server: process.env.MY_FM_SERVER
}),

Proposed Solution

The fix requires correcting the condition check and ensuring the auth block is properly handled:

} else {
  // FetchAdapter
  writer
    .write(`new FetchAdapter(`)
    .inlineBlock(() => {
      // Check for username/password auth
      if (typeof envNames.auth === "object" && "username" in envNames.auth) {
        writer
          .writeLine(`auth:`)
          .inlineBlock(() => {
            writer
              .write(`username: process.env.${envNames.auth.username}`)
              .write(",")
              .newLine();
            writer.write(`password: process.env.${envNames.auth.password}`);
          })
          .write(",")
          .newLine();
      }
      // db and server are always required
      writer.writeLine(`db: process.env.${envNames.db},`);
      writer.writeLine(`server: process.env.${envNames.server}`);
    })
    .write(")");
}

I'd be happy to submit a PR with the fix if you'd like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions