Refactor: discriminated-union for SavedConnection / ConnectionForm
- Dominant language
- TypeScript
- Stars
- 1
- Forks
- 0
- Avg merge
- 3h 8m
- Merged PRs (30d)
- 6
Description
Follow-up to PR #124 review (https://github.com/appspace/helix/pull/124#discussion_r3170281391) — flagged by reviewer as out of scope for that PR.
## Context
`SavedConnection` (`src/savedConnections.ts`) and `ConnectionForm` (`src/components/ConnectionManager.tsx`) currently model MongoDB-only fields (`connectionString`, `mongoMode`) as optional properties on a single shape, with comments asserting the invariant that non-mongodb writers MUST omit them. The comment in `savedConnections.ts:13-15` already promised that #113 would tighten this with a discriminated union, but #113 ended up adding a second mongo-only field (`mongoMode`) without doing the refactor.
## Proposal
Replace the optional-field model with a TS discriminated union, e.g.:
```ts
type ConnectionFormCommon = {
name: string;
database: string;
ssl: boolean;
sslVerify: boolean;
savePassword: boolean;
};
type SqlConnectionForm = ConnectionFormCommon & {
type: 'mysql' | 'postgres';
host: string;
port: string;
user: string;
password: string;
};
type MongoConnectionForm = ConnectionFormCommon & {
type: 'mongodb';
mongoMode: 'fields' | 'uri';
// Either fields-mode credentials...
host?: string; port?: string; user?: string; password?: string;
// ...or URI mode.
connectionString?: string;
};
type ConnectionForm = SqlConnectionForm | MongoConnectionForm;
```
Same shape for `SavedConnection`. This lets TS catch:
- Reading `mongoMode` on a postgres form
- Forgetting `connectionString` on a mongodb URI form at the type level
- The `mongoMode` default in `formFromSaved` for non-mongodb saved entries (would no longer compile)
## Scope
Touches `src/savedConnections.ts`, `src/components/ConnectionManager.tsx`, `src/api.ts` (`ConnectFormInput`), and any callsites in `App.tsx` that pass a connection form around.
Deferred from #124 (commit 92837c2).
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.