fix: --api-key flag takes precedence over WORKOS_API_KEY env var#137
Conversation
Hono's `c.req.param()` returns `string | undefined` but these params are always present because they're defined in the route path pattern.
Previously the env var always won, making `--api-key` and `env switch` silently ineffective when WORKOS_API_KEY was set in the shell. Now the precedence follows standard CLI conventions: flag > env var > stored config. Also warns on `env switch` when WORKOS_API_KEY is set, since the env var will still override the stored environment key for commands that don't pass --api-key.
Greptile SummaryThis PR fixes API key resolution priority so Confidence Score: 5/5Safe to merge; all changes are correct and well-tested, with only a minor spy-leak style issue in the new test helpers. Only P2 findings present — the vi.spyOn(console, 'error') leak doesn't cause any current test failures and doesn't affect production code. Core logic (priority reorder, warning emission, JSON shape) is correct and covered by tests. src/commands/env.spec.ts — spy teardown in the two new human-mode tests should call mockRestore(). Important Files Changed
|
- Remove unused `outputWarning` export (warnings are embedded in `outputSuccess` response instead) - Add missing `!` assertion on `orgId` in `listFilter` for consistency - Restore `WORKOS_API_KEY` env var in no-warning test to prevent leakage
Reflects the new flag > env var > stored config resolution order.

Summary
--api-keynow beatsWORKOS_API_KEYenv var, matching standard CLI conventions (gh,aws,kubectl). Previously the env var always won, making--api-keyandenv switchsilently ineffective.env switch: WhenWORKOS_API_KEYis set in the shell,env switchwarns that the env var will override the stored key. In JSON mode, warnings are embedded in the response object (not separate JSONL lines).outputSuccessgains optionalwarnings: Centralizes warning delivery so JSON consumers get a single parseable object.Reported via Slack by Fraser Langton --
env switchappeared to work but all API calls still hit the old environment becauseWORKOS_API_KEYwas set in the shell.Test plan
pnpm buildpassespnpm testpasses (1621 tests)pnpm typecheckpassesWORKOS_API_KEY=sk_test_fake workos env switch prod --jsonreturns single JSON withwarningsarray--api-keyflag overrides env varSummary by CodeRabbit
Release Notes
New Features
Bug Fixes
Tests