Skip to content

Commit

Permalink
fix: Reply Types
Browse files Browse the repository at this point in the history
Fixed Reply types on response and send
  • Loading branch information
Alex Risch authored and Alex Risch committed May 31, 2024
1 parent 59505df commit 721359b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
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

0 comments on commit 721359b

Please sign in to comment.