feat(core): API client, account registry, secure token store, security

- typed transport-only Gitea client (auth header, timeouts, pagination, ApiError)
- instance URL normalization/validation: HTTPS enforced, creds rejected
- pure account registry with deterministic active-account reassignment
- SecureStore token store (token.<id> under gitea-native.credentials)
- account-scoped TanStack query keys + purgeAccountCache
- Zustand accounts store, biometric helper, error/format utils
This commit is contained in:
JustZoe101 committed 2026-10-09 16:41:10 +02:00
1 parent 7b89c594ea
commit e0ca502cc9
21 files changed
+2874

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
/**
* Account-scoped query key factory.
*
* Every server-state key starts with `['account', accountId, ...]`, which gives us:
* - Automatic isolation between accounts (no shared cache entries).
* - One-call cache purge on account removal:
* `queryClient.removeQueries({ queryKey: queryKeys.account(id) })`.
*
* Never construct raw arrays in feature code — always go through this factory.
*/
import type { QueryClient } from '@tanstack/react-query';
export const queryKeys = {
/** Root of everything this app writes into the query cache. */
root: ['gitea-native'] as const,
/** Top-level scope for one account. */
account: (accountId: string) => ['account', accountId] as const,
// --- instance/account level -----------------------------------------------
version: (accountId: string) => [...queryKeys.account(accountId), 'version'] as const,
me: (accountId: string) => [...queryKeys.account(accountId), 'me'] as const,
unreadCount: (accountId: string) => [...queryKeys.account(accountId), 'unread-count'] as const,
activity: (accountId: string, username: string) =>
[...queryKeys.account(accountId), 'activity', username] as const,
// --- repositories ----------------------------------------------------------
repos: {
mine: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.account(accountId), 'repos', 'mine', params] as const,
starred: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.account(accountId), 'repos', 'starred', params] as const,
search: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.account(accountId), 'repos', 'search', params] as const,
},
repo: (accountId: string, owner: string, name: string) =>
[...queryKeys.account(accountId), 'repo', owner, name] as const,
repoDetail: (accountId: string, owner: string, name: string) =>
[...queryKeys.repo(accountId, owner, name), 'detail'] as const,
repoLanguages: (accountId: string, owner: string, name: string) =>
[...queryKeys.repo(accountId, owner, name), 'languages'] as const,
contents: (accountId: string, owner: string, name: string, path: string, ref?: string) =>
[...queryKeys.repo(accountId, owner, name), 'contents', { path, ref }] as const,
file: (accountId: string, owner: string, name: string, path: string, ref?: string) =>
[...queryKeys.repo(accountId, owner, name), 'file', { path, ref }] as const,
readme: (accountId: string, owner: string, name: string, ref?: string) =>
[...queryKeys.repo(accountId, owner, name), 'readme', { ref }] as const,
branches: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'branches', params] as const,
branch: (accountId: string, owner: string, name: string, branch: string) =>
[...queryKeys.repo(accountId, owner, name), 'branch', branch] as const,
tags: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'tags', params] as const,
commits: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'commits', params] as const,
commit: (accountId: string, owner: string, name: string, sha: string) =>
[...queryKeys.repo(accountId, owner, name), 'commit', sha] as const,
// --- issues & pull requests ------------------------------------------------
issueSearch: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.account(accountId), 'issues', 'search', params] as const,
repoIssues: (
accountId: string,
owner: string,
name: string,
params: Record<string, unknown>,
) => [...queryKeys.repo(accountId, owner, name), 'issues', params] as const,
issue: (accountId: string, owner: string, name: string, index: number) =>
[...queryKeys.repo(accountId, owner, name), 'issue', index] as const,
repoLabels: (accountId: string, owner: string, name: string) =>
[...queryKeys.repo(accountId, owner, name), 'labels'] as const,
pulls: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'pulls', params] as const,
pull: (accountId: string, owner: string, name: string, index: number) =>
[...queryKeys.repo(accountId, owner, name), 'pull', index] as const,
// --- notifications ---------------------------------------------------------
notificationsRoot: (accountId: string) =>
[...queryKeys.account(accountId), 'notifications'] as const,
notifications: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.notificationsRoot(accountId), params] as const,
} as const;
/**
* Removes every cached query belonging to an account.
* Called on account removal and when a token turns out to be invalid.
*/
export function purgeAccountCache(queryClient: QueryClient, accountId: string): void {
queryClient.removeQueries({ queryKey: queryKeys.account(accountId) });
}