refactor(auth-js): enable isolated declarations (#1210)

pull/1211/head
Jonathan Haines 2025-06-09 20:11:03 +10:00 committed by GitHub
parent d4cc24f754
commit 0758fd0af1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 36 additions and 12 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/auth-js': patch
---
Add explicit return types

View File

@ -134,7 +134,7 @@ export async function fetchData<T = any>(
}
}
export function useOnline() {
export function useOnline(): boolean {
const [isOnline, setIsOnline] = useState(
typeof navigator !== 'undefined' ? navigator.onLine : false
)
@ -157,11 +157,19 @@ export function useOnline() {
return isOnline
}
export function now() {
export function now(): number {
return Math.floor(Date.now() / 1000)
}
export function parseUrl(url?: string) {
interface ParsedUrl {
origin: string
host: string
path: string
base: string
toString: () => string
}
export function parseUrl(url?: string): ParsedUrl {
const defaultUrl = 'http://localhost:3000/api/auth'
const parsedUrl = new URL(url ? (url.startsWith('http') ? url : `https://${url}`) : defaultUrl)
const path = parsedUrl.pathname === '/' ? '/api/auth' : parsedUrl.pathname.replace(/\/$/, '')

View File

@ -31,12 +31,12 @@ export interface AuthConfig extends Omit<AuthConfigCore, 'raw'> {}
export type ConfigHandler = (c: Context) => AuthConfig
export function setEnvDefaults(env: AuthEnv, config: AuthConfig) {
export function setEnvDefaults(env: AuthEnv, config: AuthConfig): void {
config.secret ??= env.AUTH_SECRET
coreSetEnvDefaults(env, config)
}
export function reqWithEnvUrl(req: Request, authUrl?: string) {
export function reqWithEnvUrl(req: Request, authUrl?: string): Request {
if (authUrl) {
const reqUrlObj = new URL(req.url)
const authUrlObj = new URL(authUrl)

View File

@ -65,9 +65,11 @@ class AuthConfigManager {
}
}
export const authConfigManager = AuthConfigManager.getInstance()
export const authConfigManager: AuthConfigManager = AuthConfigManager.getInstance()
export const SessionContext = React.createContext<SessionContextValue | undefined>(undefined)
export const SessionContext: React.Context<SessionContextValue | undefined> = React.createContext<
SessionContextValue | undefined
>(undefined)
function useInitializeSession(hasInitialSession: boolean, initialSession: Session | null) {
const authConfig = authConfigManager.getConfig()
@ -148,7 +150,7 @@ function useRefetchInterval(
}, [refetchInterval, shouldRefetch])
}
export async function getSession(params?: GetSessionParams) {
export async function getSession(params?: GetSessionParams): Promise<Session | null> {
const { baseUrl, basePath, credentials } = authConfigManager.getConfig()
const session = await fetchData<Session>(
'session',
@ -163,7 +165,7 @@ export async function getSession(params?: GetSessionParams) {
return session
}
export async function getCsrfToken() {
export async function getCsrfToken(): Promise<string> {
const { baseUrl, basePath, credentials } = authConfigManager.getConfig()
const response = await fetchData<{ csrfToken: string }>(
'csrf',
@ -177,7 +179,7 @@ export async function getCsrfToken() {
return response?.csrfToken ?? ''
}
export function SessionProvider(props: SessionProviderProps) {
export function SessionProvider(props: SessionProviderProps): React.JSX.Element {
if (!SessionContext) {
throw new Error('React Context is unavailable in Server Components')
}
@ -274,7 +276,7 @@ export function useSession<R extends boolean>(
type ProvidersType = Record<LiteralUnion<BuiltInProviderType>, ClientSafeProvider>
export async function getProviders() {
export async function getProviders(): Promise<ProvidersType | null> {
return fetchData<ProvidersType>('providers', authConfigManager.getConfig(), logger)
}
@ -395,10 +397,14 @@ interface PopupLoginOptions extends Partial<Omit<WindowProps, 'url'>> {
callbackUrl?: string
}
interface LoginState extends AuthState {
popUpSignin: () => Promise<void>
}
export const useOauthPopupLogin = (
provider: Parameters<typeof signIn>[0],
options: PopupLoginOptions = {}
) => {
): LoginState => {
const { width = 500, height = 500, title = 'Signin', onSuccess, callbackUrl = '/' } = options
const [externalWindow, setExternalWindow] = useState<Window | null>()

View File

@ -1,9 +1,14 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
"emitDeclarationOnly": false,
"isolatedDeclarations": true,
"jsx": "react"
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["**/*.test.ts"],
"references": []
}