Fix TypeScript compilation errors in adapters - add AuthService type parameter#592
Fix TypeScript compilation errors in adapters - add AuthService type parameter#592
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes TypeScript compilation issues in the Hono, Next.js, and NestJS adapters by adding a local AuthService interface and using it as the type parameter to kernel.getService<AuthService>('auth'), enabling type-safe access to handleRequest().
Changes:
- Added an
AuthServiceinterface (per adapter) defininghandleRequest(request: Request): Promise<Response>. - Updated auth service lookups to
getService<AuthService>('auth')in all three adapters.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/adapters/nextjs/src/index.ts | Adds local AuthService type and uses typed getService<AuthService>('auth') for auth handling. |
| packages/adapters/nestjs/src/index.ts | Adds local AuthService type and uses typed getService<AuthService>('auth') in the controller auth route. |
| packages/adapters/hono/src/index.ts | Adds local AuthService type and uses typed getService<AuthService>('auth') in the auth route. |
| // Try AuthPlugin service first (preferred path) | ||
| const authService = typeof options.kernel.getService === 'function' | ||
| ? options.kernel.getService('auth') | ||
| ? options.kernel.getService<AuthService>('auth') | ||
| : null; |
There was a problem hiding this comment.
ObjectKernel.getService() throws when a service is missing (it does not return null/undefined). As written, calling getService<AuthService>('auth') will throw when the auth plugin/service isn’t registered, and the code will jump to the outer catch and return a 500, never reaching the legacy dispatcher fallback. Consider wrapping the getService call in a try/catch that treats the specific "service not found" error as a null authService so the fallback path can run.
| const kernel = this.service.getKernel(); | ||
| const authService = typeof kernel.getService === 'function' | ||
| ? kernel.getService('auth') | ||
| ? kernel.getService<AuthService>('auth') | ||
| : null; |
There was a problem hiding this comment.
ObjectKernel.getService() throws when a service is missing. If the auth plugin/service isn’t registered, getService<AuthService>('auth') will throw and the handler will return a 500 from the catch block, never reaching the legacy dispatcher.handleAuth(...) fallback. Wrap the getService lookup in a try/catch and treat the "service not found" case as null so the fallback can execute.
| // Try AuthPlugin service first (preferred path) | ||
| const authService = typeof options.kernel.getService === 'function' | ||
| ? options.kernel.getService('auth') | ||
| ? options.kernel.getService<AuthService>('auth') | ||
| : null; | ||
|
|
There was a problem hiding this comment.
ObjectKernel.getService() throws when a service is missing. If the auth plugin/service isn’t registered, getService<AuthService>('auth') will throw and the route will return a 500 from the catch block, never reaching the legacy dispatcher.handleAuth(...) fallback. Wrap the getService call in a try/catch and treat the "service not found" case as null so fallback behavior works.
TypeScript compilation failed in Hono, Next.js, and NestJS adapters because
kernel.getService('auth')returned{}without a type parameter, preventing access tohandleRequest().Changes
AuthServiceinterface defininghandleRequest(request: Request): Promise<Response>getService()calls to use type parameter:getService<AuthService>('auth')Example
Note
The
AuthServiceinterface is currently duplicated across the three adapters. Consider extracting to a shared types package in a follow-up.Original prompt
Created from VS Code.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.