diff --git a/.changeset/fix-typegen-auth-detection.md b/.changeset/fix-typegen-auth-detection.md new file mode 100644 index 0000000..49f921d --- /dev/null +++ b/.changeset/fix-typegen-auth-detection.md @@ -0,0 +1,5 @@ +--- +"@proofkit/typegen": patch +--- + +Fix generated client authentication type detection to use OttoAdapter when OTTO_API_KEY environment variable is set with default names diff --git a/.claude/ai-builder.lock.json b/.claude/ai-builder.lock.json new file mode 100644 index 0000000..e67a4c7 --- /dev/null +++ b/.claude/ai-builder.lock.json @@ -0,0 +1,13 @@ +{ + "version": 1, + "artifacts": { + "skill:openameba/create-changeset": { + "type": "skill", + "slug": "create-changeset", + "author": "openameba", + "name": "create-changeset", + "installedAt": "2026-01-07T14:50:43.613Z", + "files": [".claude/skills/create-changeset/SKILL.md"] + } + } +} diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000..0cbf4db --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,5 @@ +{ + "enabledPlugins": { + "typescript-lsp@claude-plugins-official": true + } +} diff --git a/.claude/skills/create-changeset/SKILL.md b/.claude/skills/create-changeset/SKILL.md new file mode 100644 index 0000000..67027c6 --- /dev/null +++ b/.claude/skills/create-changeset/SKILL.md @@ -0,0 +1,140 @@ +--- +name: create-changeset +description: Analyze git changes and create changesets for package releases. Use when preparing pull requests, creating PRs, when branch has commits ready for review, or when user mentions changeset or version bump. +--- +# Create Changeset Skill + +## Purpose + +This skill automatically analyzes branch changes and creates appropriate changesets for package releases in this monorepo. It examines git history, determines version bump types based on Conventional Commits, and generates properly formatted changeset files. + +## When to Invoke + +Automatically invoke this skill when: +- User is preparing to create a pull request +- User mentions "PR", "pull request", or "ready for review" +- Branch has commits ready for review +- User explicitly mentions "changeset" or "version bump" + +Do NOT invoke when: +- User is only pushing changes without creating a PR +- Only documentation files have changed (README, .md files) +- Only CI/CD configuration has changed (.github/workflows/) +- Only development tool configuration has changed (eslint, prettier, etc.) +- Commits are only `docs:`, `chore:`, `ci:`, or `test:` types that don't affect packages + +## Pre-execution Validation + +Before creating a changeset, check if a changeset file already exists for the current changes: +- Look for `.changeset/*.md` files (excluding README.md) +- If exists, ask user: "A changeset already exists. Create another one?" + +## Implementation Steps + +### 1. Check Current State + +Execute `git log main..HEAD` (or `origin/main..HEAD`) to check for committed changes on the branch. If no commits exist, exit early without creating a changeset. + +### 2. Analyze Changes + +Use `git diff main...HEAD` (or `origin/main...HEAD`) to analyze **committed changes only**. + +Identify which packages are affected by checking files under `packages/*/`. Review commit messages using `git log main..HEAD --oneline` (or `origin/main..HEAD`). + +### 3. Determine Version Bump Type + +Analyze commit messages following Conventional Commits 1.0.0 format: + +- **major**: Contains `BREAKING CHANGE` in commit body, or breaking changes detected in code + - API signature changes + - Removed exports or features + - Incompatible behavior changes +- **minor**: Starts with `feat:` or `feat(scope):` - new features (backward compatible) + - New components or functionality + - New props or options (with defaults) + - New exports +- **patch**: Starts with `fix:` or `fix(scope):` - bug fixes and minor improvements + - Bug fixes + - Performance improvements + - Minor style updates +- **skip**: Other types (`chore:`, `docs:`, `ci:`, `test:`) typically don't require changesets unless they affect package functionality + +Review actual code changes to confirm the appropriate version bump. **When in doubt between minor and patch, prefer patch for safety.** + +If the version bump is ambiguous or unclear: +- Ask user for clarification +- Explain the reasoning behind the suggested bump type +- Allow user to override the suggestion + +If all commits are types that don't require changesets (`docs:`, `chore:`, `ci:`, `test:`), exit early without creating a changeset. + +### 4. Generate Changeset + +Create a changeset file with a descriptive filename in `.changeset/` directory. + +**Filename format:** +- Use kebab-case with `.md` extension +- Examples: `.changeset/add-new-button.md`, `.changeset/fix-layout-bug.md`, `.changeset/update-icon-props.md` + +**File content format:** +```markdown +--- +"@openameba/package-name": major|minor|patch +--- + +Clear description of the change +``` + +**Example for single package:** +```markdown +--- +"@openameba/spindle-ui": minor +--- + +Add new Button variant for secondary actions +``` + +**Example for multiple packages:** +```markdown +--- +"@openameba/spindle-ui": minor +"@openameba/spindle-tokens": patch +--- + +- spindle-ui: Add new Button variant for secondary actions +- spindle-tokens: Fix color token contrast ratio +``` + +**Important guidelines:** +- The description should be user-friendly as it will appear in CHANGELOG +- **Use the same language as the commit messages** (Japanese or English). If commit messages are mixed, prefer Japanese. +- Split changesets into separate files when the same package has changes with different purposes (e.g., new feature + bug fix, breaking change + internal refactoring) +- This creates individual top-level items in release notes, making it easier for readers to understand the intent of each change +- Example: Create `.changeset/add-secondary-button.md` for a new feature and `.changeset/fix-button-layout.md` for a bug fix, even if both target the same package + +### 5. Lint Changeset + +Execute `pnpm textlint .changeset/.md` to validate the changeset file. + +**Error handling:** +- If linting errors occur, attempt to auto-fix common issues: + - Spacing and punctuation + - Common grammar mistakes +- Re-run textlint after auto-fix +- If errors persist: + - Display error details to user + - Ask user for guidance on how to fix + - Do NOT proceed to commit until lint passes + +### 6. Verify and Commit + +Display the generated changeset for review: +- Show the file path +- Show the file content +- Confirm it accurately reflects the changes + +Once verified, commit the changeset file: +```bash +git add .changeset/.md +git commit -m "chore: add changeset" +``` \ No newline at end of file diff --git a/packages/typegen/src/typegen.ts b/packages/typegen/src/typegen.ts index 831fae6..a135d4d 100644 --- a/packages/typegen/src/typegen.ts +++ b/packages/typegen/src/typegen.ts @@ -181,23 +181,38 @@ const generateTypedClientsSingle = async ( strictNumbers: item.strictNumbers, webviewerScriptName: config?.type === "fmdapi" ? config.webviewerScriptName : undefined, envNames: (() => { - const hasApiKey = envNames?.auth?.apiKey !== undefined; - const hasUsername = envNames?.auth?.username !== undefined; + // Determine the intended auth type based on config AND runtime. + // Priority: + // 1. If user explicitly specified apiKey in config → use OttoAdapter + // 2. If user explicitly specified username in config → use FetchAdapter + // 3. If neither specified (defaults) → use what was actually used at runtime + // + // Note: We check for the VALUE being defined, not just the property existing, + // because the Zod schema defines both apiKey and username as optional properties, + // so both exist on the object but with undefined values when not specified. + const configHasApiKey = envNames?.auth?.apiKey !== undefined; + const configHasUsername = envNames?.auth?.username !== undefined; + const runtimeUsedApiKey = "apiKey" in auth; + + // Use apiKey if: explicitly specified in config, OR not explicitly set to username AND runtime used apiKey + const useApiKey = configHasApiKey || (!configHasUsername && runtimeUsedApiKey); + + // Determine the env var names to use in generated code + const apiKeyEnvName = envNames?.auth?.apiKey ?? defaultEnvNames.apiKey; + const usernameEnvName = envNames?.auth?.username ?? defaultEnvNames.username; + const passwordEnvName = envNames?.auth?.password ?? defaultEnvNames.password; return { - auth: hasApiKey + auth: useApiKey ? { - apiKey: envNames?.auth?.apiKey ?? defaultEnvNames.apiKey, + apiKey: apiKeyEnvName, username: undefined, password: undefined, } : { apiKey: undefined, - username: hasUsername && envNames?.auth ? envNames.auth.username : defaultEnvNames.username, - password: - hasUsername && envNames?.auth && envNames.auth.password !== undefined - ? envNames.auth.password - : defaultEnvNames.password, + username: usernameEnvName, + password: passwordEnvName, }, db: envNames?.db ?? defaultEnvNames.db, server: envNames?.server ?? defaultEnvNames.server, diff --git a/packages/typegen/tests/typegen.test.ts b/packages/typegen/tests/typegen.test.ts index 064bdc9..88a0b43 100644 --- a/packages/typegen/tests/typegen.test.ts +++ b/packages/typegen/tests/typegen.test.ts @@ -1,7 +1,7 @@ import { execSync } from "node:child_process"; import fs from "node:fs/promises"; import path from "node:path"; -import { beforeEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { z } from "zod/v4"; import type { OttoAPIKey } from "../../fmdapi/src"; import { generateTypedClients } from "../src/typegen"; @@ -114,12 +114,26 @@ describe("typegen", () => { // Define a base path for generated files relative to the test file directory const baseGenPath = getBaseGenPath(); + // Store original env values to restore after tests + const originalEnv: Record = {}; + // Clean up the base directory before each test beforeEach(async () => { await fs.rm(baseGenPath, { recursive: true, force: true }); console.log(`Cleaned base output directory: ${baseGenPath}`); }); + // Restore original environment after each test + afterEach(() => { + for (const key of Object.keys(originalEnv)) { + if (originalEnv[key] === undefined) { + delete process.env[key]; + } else { + process.env[key] = originalEnv[key]; + } + } + }); + it("basic typegen with zod", async () => { const config: Extract, { type: "fmdapi" }> = { type: "fmdapi", @@ -320,4 +334,56 @@ describe("typegen", () => { await cleanupGeneratedFiles(genPath); }, 30_000); + + it("should use OttoAdapter with default env var names when OTTO_API_KEY is set and no envNames config provided", async () => { + // Store original env values + originalEnv.OTTO_API_KEY = process.env.OTTO_API_KEY; + originalEnv.FM_SERVER = process.env.FM_SERVER; + originalEnv.FM_DATABASE = process.env.FM_DATABASE; + originalEnv.FM_USERNAME = process.env.FM_USERNAME; + originalEnv.FM_PASSWORD = process.env.FM_PASSWORD; + + // Set up environment with default env var names (API key auth) + process.env.OTTO_API_KEY = process.env.DIFFERENT_OTTO_API_KEY || "test-api-key"; + process.env.FM_SERVER = process.env.DIFFERENT_FM_SERVER || "test-server"; + process.env.FM_DATABASE = process.env.DIFFERENT_FM_DATABASE || "test-db"; + // Ensure username/password are NOT set to force API key usage + // biome-ignore lint/performance/noDelete: delete is required to unset environment variables + delete process.env.FM_USERNAME; + // biome-ignore lint/performance/noDelete: delete is required to unset environment variables + delete process.env.FM_PASSWORD; + + // Config without envNames - should use defaults + const config: Extract, { type: "fmdapi" }> = { + type: "fmdapi", + layouts: [ + { + layoutName: "layout", + schemaName: "testLayout", + generateClient: true, + }, + ], + path: "typegen-output/default-api-key", + generateClient: true, + // Note: envNames is undefined - should use defaults + envNames: undefined, + }; + + const genPath = await generateTypes(config); + + // Check that the generated client uses OttoAdapter with default env var names + const clientPath = path.join(genPath, "client", "testLayout.ts"); + const clientContent = await fs.readFile(clientPath, "utf-8"); + + // Should use OttoAdapter since OTTO_API_KEY was set + expect(clientContent).toContain("OttoAdapter"); + expect(clientContent).not.toContain("FetchAdapter"); + // Should use the default env var name + expect(clientContent).toContain("OTTO_API_KEY"); + // Should NOT have username/password env var references + expect(clientContent).not.toContain("FM_USERNAME"); + expect(clientContent).not.toContain("FM_PASSWORD"); + + await cleanupGeneratedFiles(genPath); + }, 30_000); });