2 * We use a slightly altered data model than what protobuf would produce
3 * internally, because the generics and polymorphism are easier to work with
4 * this way, or better said, protobuf doesn't produce very easy-to-consume
7 import type { ExtraField, ExtraIdentitySection, PlatformSpecific } from './item-v1';
12 ItemAlias as ProtobufItemAlias,
13 ItemCreditCard as ProtobufItemCreditCard,
14 ItemIdentity as ProtobufItemIdentity,
15 ItemLogin as ProtobufItemLogin,
16 ItemNote as ProtobufItemNote,
18 import { Vault } from './vault-v1';
27 ProtobufItemCreditCard,
35 * Excludes the undefined oneofKind type generated by protoc-gen-ts
36 * in ProtobufItem['content']
38 type OneOfKindKeys<T extends { content: any }> = Extract<T['content'], { oneofKind: string }>;
39 type OneOfKindMap<U extends { oneofKind: string }> = {
40 [T in U as T['oneofKind']]: Omit<T, 'oneofKind'> extends infer ContentType ? ContentType[keyof ContentType] : never;
44 * Intermediary utility type that will flatten the union type resulting
45 * from SafeProtobufItemContent to a single mapped object type containing
46 * all possible key/type pairs for the underlying content types
48 type ItemContentUnion = OneOfKindKeys<Content>;
49 export type ItemContentMap = OneOfKindMap<ItemContentUnion>;
50 export type ItemType = keyof ItemContentMap;
52 type ExtraFieldContentUnion = OneOfKindKeys<ExtraField>;
53 export type ExtraFieldContentMap = OneOfKindMap<ExtraFieldContentUnion>;
54 export type ExtraFieldType = keyof ExtraFieldContentMap;
57 * Creates a generic "distributive object type" over all possible
58 * oneofKind keys - When decoding protobuf items, this will allow us
59 * to select the underlying content type by its oneofKind property
60 * while maintaining strong type safety
62 export type SafeProtobufItem<T extends ItemType = ItemType> = {
67 } & { [ItemKey in Key]: ItemContentMap[Key] };
71 extraFields: SafeProtobufExtraField[];
72 platformSpecific?: PlatformSpecific;
75 export type SafeProtobufExtraField<T extends ExtraFieldType = ExtraFieldType> = { fieldName: string } & {
77 content: { oneofKind: Key } & {
78 [ExtraFieldKey in Key]: ExtraFieldContentMap[Key];