Skip to content

Commit 7667906

Browse files
committed
refactor(P060): tighten BridgeResult.error.code to a typed union
Bridge-owned codes (bad_input, bridge_disposed, iframe_not_ready, missing_result, timeout) are now literal types; iframe-forwarded codes (bad_request:*, forbidden:*, etc.) pass through via `string & {}` so arbitrary strings still compile. The `& {}` idiom preserves IDE autocomplete for the bridge-owned literals — typos like `code: 'iframe_not_redy'` show up in suggestions; bridge-emitted code is now self-documenting at the type level. Narrowing on a specific iframe code stays the consumer's responsibility. Exports BridgeErrorCode from the embed-bridge barrel so future adapter / consumer code can reference the type if it ever needs to.
1 parent 735945a commit 7667906

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

copilot/src/lib/embed-bridge/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
SupportedFieldTypeSchema,
2222
} from './schemas'
2323
export type {
24+
BridgeErrorCode,
2425
BridgeRequestType,
2526
BridgeResult,
2627
BridgeState,

copilot/src/lib/embed-bridge/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
// Shared types for the SimplePDF embed bridge. Pure TypeScript, no
22
// framework dependencies.
33

4+
// Codes the bridge itself emits. Anything else (`bad_request:invalid_page`,
5+
// `forbidden:editing_not_allowed`, etc.) is forwarded verbatim from the
6+
// iframe handler and flows through as a plain string. The `(string & {})`
7+
// in the union preserves IDE autocomplete for the bridge-owned literals
8+
// while still accepting arbitrary forwarded codes — narrowing on a
9+
// specific iframe code stays the consumer's responsibility.
10+
type BridgeOwnedErrorCode = 'bad_input' | 'bridge_disposed' | 'iframe_not_ready' | 'missing_result' | 'timeout'
11+
12+
export type BridgeErrorCode = BridgeOwnedErrorCode | (string & {})
13+
414
export type BridgeResult<TData = null> =
515
| { success: true; data: TData }
6-
| { success: false; error: { code: string; message: string } }
16+
| { success: false; error: { code: BridgeErrorCode; message: string } }
717

818
// Runtime guard for BridgeResult shapes received from the iframe. The
919
// postMessage payload is JSON parsed from an untrusted source — same-origin

0 commit comments

Comments
 (0)