[PR #8278] [MERGED] fix(api-key): infer additional fields in request body and response types #7868

Closed
opened 2026-03-13 13:51:41 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8278
Author: @himself65
Created: 3/2/2026
Status: Merged
Merged: 3/2/2026
Merged by: @himself65

Base: canaryHead: fix/api-key-additional-fields-type-inference


📝 Commits (5)

  • 3f9cc87 fix(api-key): infer additional fields types in createApiKey and updateApiKey body
  • 3fee28c fix(api-key): infer additional fields in all endpoint response types
  • 9c6cee3 test(api-key): add type inference tests for additional fields on all endpoints
  • 4c5c9f6 test(api-key): simplify type inference tests to use auth.api directly
  • 813efba chore(api-key): fix lint

📊 Changes

9 files changed (+207 additions, -31 deletions)

View changed files

📝 packages/api-key/src/api-key.test.ts (+143 -1)
📝 packages/api-key/src/index.ts (+8 -8)
📝 packages/api-key/src/routes/create-api-key.ts (+9 -3)
📝 packages/api-key/src/routes/get-api-key.ts (+3 -3)
📝 packages/api-key/src/routes/index.ts (+15 -7)
📝 packages/api-key/src/routes/list-api-keys.ts (+3 -3)
📝 packages/api-key/src/routes/update-api-key.ts (+9 -3)
📝 packages/api-key/src/routes/verify-api-key.ts (+3 -3)
📝 packages/api-key/src/types.ts (+14 -0)

📄 Description

Summary

The additionalFields feature added in #7744 was missing TypeScript type inference — callers had no type-safe access to the extra fields on request bodies or returned API key objects.

  • Make apiKey(), createApiKeyRoutes(), createApiKey(), updateApiKey(), getApiKey(), listApiKeys(), and verifyApiKey() generic on O extends ApiKeyOptions so the concrete additionalFields definition propagates through
  • Add InferApiKey<O> helper type (ApiKey & InferAdditionalFieldsFromPluginOptions<"apikey", O>) for response types
  • Use metadata.$Infer.body on createApiKey and updateApiKey so better-call resolves the correct body type (additional fields required on create, optional on update; input: false fields excluded)
  • Cast all ctx.json() returns with InferApiKey<O> so response types carry the additional fields

Test plan

  • Parameters<typeof auth.api.createApiKey>[0]["body"] includes required organizationId, optional projectId, and excludes serverOnlyField (input: false)
  • Parameters<typeof auth.api.updateApiKey>[0]["body"] has all additional fields optional
  • Awaited<ReturnType<typeof auth.api.getApiKey>>, listApiKeys, verifyApiKey, createApiKey, and updateApiKey all include the additional fields in their return types
  • All 173 existing tests continue to pass

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8278 **Author:** [@himself65](https://github.com/himself65) **Created:** 3/2/2026 **Status:** ✅ Merged **Merged:** 3/2/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/api-key-additional-fields-type-inference` --- ### 📝 Commits (5) - [`3f9cc87`](https://github.com/better-auth/better-auth/commit/3f9cc872109763ba01e52e67e03987af8ceebda8) fix(api-key): infer additional fields types in createApiKey and updateApiKey body - [`3fee28c`](https://github.com/better-auth/better-auth/commit/3fee28c335dde4f775e3cb75ce4e12a9ecd73fb4) fix(api-key): infer additional fields in all endpoint response types - [`9c6cee3`](https://github.com/better-auth/better-auth/commit/9c6cee3f0ed650e04d973f21ecf733a4466ecf8d) test(api-key): add type inference tests for additional fields on all endpoints - [`4c5c9f6`](https://github.com/better-auth/better-auth/commit/4c5c9f63ea24b28de430e319fa35a80f1f2b10ac) test(api-key): simplify type inference tests to use auth.api directly - [`813efba`](https://github.com/better-auth/better-auth/commit/813efba57a05699a2719c07eeca975a75336e6ce) chore(api-key): fix lint ### 📊 Changes **9 files changed** (+207 additions, -31 deletions) <details> <summary>View changed files</summary> 📝 `packages/api-key/src/api-key.test.ts` (+143 -1) 📝 `packages/api-key/src/index.ts` (+8 -8) 📝 `packages/api-key/src/routes/create-api-key.ts` (+9 -3) 📝 `packages/api-key/src/routes/get-api-key.ts` (+3 -3) 📝 `packages/api-key/src/routes/index.ts` (+15 -7) 📝 `packages/api-key/src/routes/list-api-keys.ts` (+3 -3) 📝 `packages/api-key/src/routes/update-api-key.ts` (+9 -3) 📝 `packages/api-key/src/routes/verify-api-key.ts` (+3 -3) 📝 `packages/api-key/src/types.ts` (+14 -0) </details> ### 📄 Description ## Summary The `additionalFields` feature added in #7744 was missing TypeScript type inference — callers had no type-safe access to the extra fields on request bodies or returned API key objects. - Make `apiKey()`, `createApiKeyRoutes()`, `createApiKey()`, `updateApiKey()`, `getApiKey()`, `listApiKeys()`, and `verifyApiKey()` generic on `O extends ApiKeyOptions` so the concrete `additionalFields` definition propagates through - Add `InferApiKey<O>` helper type (`ApiKey & InferAdditionalFieldsFromPluginOptions<"apikey", O>`) for response types - Use `metadata.$Infer.body` on `createApiKey` and `updateApiKey` so `better-call` resolves the correct body type (additional fields required on create, optional on update; `input: false` fields excluded) - Cast all `ctx.json()` returns with `InferApiKey<O>` so response types carry the additional fields ## Test plan - `Parameters<typeof auth.api.createApiKey>[0]["body"]` includes required `organizationId`, optional `projectId`, and excludes `serverOnlyField` (`input: false`) - `Parameters<typeof auth.api.updateApiKey>[0]["body"]` has all additional fields optional - `Awaited<ReturnType<typeof auth.api.getApiKey>>`, `listApiKeys`, `verifyApiKey`, `createApiKey`, and `updateApiKey` all include the additional fields in their return types - All 173 existing tests continue to pass --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:51:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#7868