Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

fix: use type from the reply nested content directly #21

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-coins-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/content-type-reply": patch
---

Gets the nested type of a reply from the deserialized EncodedContent instead of inspecting the parameter map
25 changes: 8 additions & 17 deletions packages/content-type-reply/src/Reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,12 @@ export type Reply = {
contentType: ContentTypeId;
};

export type ReplyParameters = Pick<Reply, "reference"> & {
contentType: string;
};

export class ReplyCodec implements ContentCodec<Reply> {
get contentType(): ContentTypeId {
return ContentTypeReply;
}

encode(
content: Reply,
codecs: CodecRegistry,
): EncodedContent<ReplyParameters> {
encode(content: Reply, codecs: CodecRegistry): EncodedContent {
const codec = codecs.codecFor(content.contentType);
if (!codec) {
throw new Error(
Expand All @@ -54,26 +47,24 @@ export class ReplyCodec implements ContentCodec<Reply> {
return {
type: ContentTypeReply,
parameters: {
// TODO: cut when we're certain no one is looking for "contentType" here.
contentType: content.contentType.toString(),
daria-github marked this conversation as resolved.
Show resolved Hide resolved
reference: content.reference,
},
content: bytes,
};
}

decode(
content: EncodedContent<ReplyParameters>,
codecs: CodecRegistry,
): Reply {
decode(content: EncodedContent, codecs: CodecRegistry): Reply {
const decodedContent = proto.EncodedContent.decode(content.content);
const contentType = ContentTypeId.fromString(
content.parameters.contentType,
);

if (!decodedContent.type) {
throw new Error("missing content type");
}
daria-github marked this conversation as resolved.
Show resolved Hide resolved
const contentType = new ContentTypeId(decodedContent.type);
const codec = codecs.codecFor(contentType);
if (!codec) {
throw new Error(
`missing codec for content type "${content.parameters.contentType}"`,
`missing codec for content type "${contentType.toString()}"`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/content-type-reply/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ReplyCodec, ContentTypeReply } from "./Reply";
export type { Reply, ReplyParameters } from "./Reply";
export type { Reply } from "./Reply";
Loading