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

Commit

Permalink
fix: use type from the reply nested content directly
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccartney committed Aug 5, 2023
1 parent 4f72afa commit 6cb5ed8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
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(),
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");
}
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";

0 comments on commit 6cb5ed8

Please sign in to comment.