Refactor ParsedMetadata as generic#324
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
| const serverTransport = getServerTransport(); | ||
| const handler = | ||
| vi.fn<(ctx: ProcedureHandlerContext<object, object>) => void>(); | ||
| vi.fn<(ctx: ProcedureHandlerContext<object, object, unknown>) => void>(); |
There was a problem hiding this comment.
weird that we do unknown in some places but object in others as the default value for this
There was a problem hiding this comment.
That's right. It should be object here.
There was a problem hiding this comment.
unknown where possible makes sense, object is basically any
|
|
||
| // invariant: must pass custom validation if defined | ||
| let parsedMetadata: ParsedMetadata = {}; | ||
| let parsedMetadata: ParsedMetadata = {} as ParsedMetadata; |
There was a problem hiding this comment.
why do we need an additional assertion here when we didnt before? same question for L330
There was a problem hiding this comment.
because ParsedMetadata extends object now. It extends Record<string, unknown> before
| // MetadataSchema and ParsedMetadata are not used in this test, | ||
| // so we can safely use any here | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| serverTransport: ServerTransport<Connection, any, any>; |
There was a problem hiding this comment.
still weird, feels like we should be using <Connection, TSchema, object> here which should work?
There was a problem hiding this comment.
No, it doesn't work. The ServerTransport class has a method extendHandshake that takes ServerHandshakeOptions<MetadataSchema, ParsedMetadata>. These options include a validate function that expects the previous metadata to match the ParsedMetadata type. When we use any for ParsedMetadata, TypeScript allows any type to be passed. But when we try to use a specific type like { kept: string }, TypeScript enforces that the previous metadata must match that exact type 😢
| const transports: Array< | ||
| WebSocketClientTransport | WebSocketServerTransport | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| WebSocketClientTransport | WebSocketServerTransport<any, any> |
|
lgtm to unblock but i'd like to do a future pass at somepoint to see if we can figure out if we can thread less generics through everything :') |
6d9b233 to
1be6607
Compare
Merge activity
|
1be6607 to
301dfe1
Compare

Why
This PR makes the
ParsedMetadatatype a generic parameter rather than a global interface that requires declaration merging. This allows for having multipleParsedMetadatafor different projects in a monorepoWhat changed
ParsedMetadatafrom a global interface to a generic parameter in relevant typesProcedureHandlerContextto accept a genericParsedMetadataparameterServerHandshakeOptionsto accept a genericParsedMetadataparameterServerTransportto accept generic parameters for metadata schema and parsed metadataExample:
Versioning