Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reply Types #400

Merged
merged 1 commit into from
Jun 5, 2024
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
57 changes: 57 additions & 0 deletions example/src/types/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EncodedContent,
JSContentCodec,
ReactionCodec,
ReplyCodec,
TextCodec,
sendMessage,
} from 'xmtp-react-native-sdk'
Expand Down Expand Up @@ -177,4 +178,60 @@ export const typeTests = async () => {
// @ts-expect-error
test: 'test',
})
const supportedReplyCodecs = [...supportedCodecs, new ReplyCodec()]
const replyClient = await Client.createRandom<typeof supportedReplyCodecs>({
codecs: supportedReplyCodecs,
})

const replyConvo = (await replyClient.conversations.list())[0]
await replyConvo.send({
reaction: {
action: 'added',
content: '💖',
reference: '123',
schema: 'unicode',
},
})
await replyConvo.send({
reply: {
reference: '123',
content: {
reaction: {
action: 'added',
content: '💖',
reference: '123',
schema: 'unicode',
},
},
},
})
await replyConvo.send({
reply: {
reference: '123',
content: {
// @ts-expect-error
reaction: {
action: 'added',
content: '💖',
reference: '123',
// schema: 'unicode',
},
},
},
})
const replyMessages = await replyConvo.messages()
const replyContent = replyMessages[0].content()
if (typeof replyContent === 'string') {
//
} else {
const reply = replyContent
// Typecheck for reaction
if (typeof reply.content === 'string') {
} else {
// is a reply of some type
if (reply.content.text !== 'added') {
//
}
}
}
}
10 changes: 3 additions & 7 deletions src/lib/NativeCodecs/ReplyCodec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { TextCodec } from './TextCodec'
import {
ContentTypeId,
NativeContentCodec,
NativeMessageContent,
} from '../ContentCodec'
import { DefaultContentTypes } from '../types/DefaultContentType'

export type ReplyContent<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
> = {
export type ReplyContent = {
reference: string
content: [...ContentTypes, TextCodec][number] | string
contentType: string
// Right now this will assume any NativeMessageContent is valid, but really should only be the supported content types
content: NativeMessageContent
}

export class ReplyCodec implements NativeContentCodec<ReplyContent> {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/types/ContentCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export type ReadReceiptContent = object

export type ReplyContent = {
reference: string
content: any
contentType: string
content: NativeMessageContent
}

export type ReactionContent = {
Expand Down
Loading