Skip to content

Commit

Permalink
Merge pull request #694 from xmtp/rygine/node-sdk-updates
Browse files Browse the repository at this point in the history
Update Node SDK
  • Loading branch information
rygine authored Oct 28, 2024
2 parents 850f061 + 87457d6 commit b7d535d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 61 deletions.
8 changes: 8 additions & 0 deletions .changeset/chatty-snails-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@xmtp/node-sdk": patch
---

- Allowed for `undefined` content type and content in messages
- Filtered out messages without content when calling `Conversation.messages`
- Added generic typing for message content to `DecodedMessage` class and `Conversations.findMessageById`
- Replaced temporary group updated codec with official content type
1 change: 1 addition & 0 deletions sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"typecheck": "tsc"
},
"dependencies": {
"@xmtp/content-type-group-updated": "^1.0.0",
"@xmtp/content-type-primitives": "^1.0.2",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/node-bindings": "^0.0.14",
Expand Down
3 changes: 2 additions & 1 deletion sdks/node-sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import tsConfigPaths from "rollup-plugin-tsconfig-paths";
const external = [
"node:path",
"node:process",
"@xmtp/content-type-text",
"@xmtp/content-type-group-updated",
"@xmtp/content-type-primitives",
"@xmtp/content-type-text",
"@xmtp/node-bindings",
"@xmtp/proto",
"@xmtp/xmtp-js",
Expand Down
8 changes: 4 additions & 4 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { join } from "node:path";
import process from "node:process";
import {
ContentTypeGroupUpdated,
GroupUpdatedCodec,
} from "@xmtp/content-type-group-updated";
import type {
ContentCodec,
ContentTypeId,
Expand All @@ -15,10 +19,6 @@ import {
type NapiClient,
type NapiMessage,
} from "@xmtp/node-bindings";
import {
ContentTypeGroupUpdated,
GroupUpdatedCodec,
} from "@/codecs/GroupUpdatedCodec";
import { Conversations } from "@/Conversations";

export const ApiUrls = {
Expand Down
10 changes: 7 additions & 3 deletions sdks/node-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ export class Conversation {
}

messages(options?: NapiListMessagesOptions): DecodedMessage[] {
return this.#group
.findMessages(options)
.map((message) => new DecodedMessage(this.#client, message));
return (
this.#group
.findMessages(options)
.map((message) => new DecodedMessage(this.#client, message))
// filter out messages without content
.filter((message) => message.content !== undefined)
);
}
}
4 changes: 2 additions & 2 deletions sdks/node-sdk/src/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class Conversations {
}
}

getMessageById(id: string) {
getMessageById<T = any>(id: string) {
try {
// findMessageById will throw if message is not found
const message = this.#conversations.findMessageById(id);
return new DecodedMessage(this.#client, message);
return new DecodedMessage<T>(this.#client, message);
} catch {
return null;
}
Expand Down
15 changes: 9 additions & 6 deletions sdks/node-sdk/src/DecodedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { nsToDate } from "@/helpers/date";
export type MessageKind = "application" | "membership_change";
export type MessageDeliveryStatus = "unpublished" | "published" | "failed";

export class DecodedMessage {
export class DecodedMessage<T = any> {
#client: Client;
content: any;
contentType: ContentTypeId;
content: T;
contentType: ContentTypeId | undefined;
conversationId: string;
deliveryStatus: MessageDeliveryStatus;
fallback?: string;
Expand Down Expand Up @@ -56,12 +56,15 @@ export class DecodedMessage {
// no default
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.contentType = new ContentTypeId(message.content.type!);
this.contentType = message.content.type
? new ContentTypeId(message.content.type)
: undefined;
this.parameters = message.content.parameters;
this.fallback = message.content.fallback;
this.compression = message.content.compression;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.content = this.#client.decodeContent(message, this.contentType);
this.content = this.contentType
? this.#client.decodeContent(message, this.contentType)
: undefined;
}
}
41 changes: 0 additions & 41 deletions sdks/node-sdk/src/codecs/GroupUpdatedCodec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions sdks/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ export { Client, ApiUrls } from "./Client";
export { Conversation } from "./Conversation";
export { Conversations } from "./Conversations";
export { DecodedMessage } from "./DecodedMessage";
export {
ContentTypeGroupUpdated,
GroupUpdatedCodec,
} from "./codecs/GroupUpdatedCodec";
export type { StreamCallback } from "./AsyncStream";
export type * from "@xmtp/node-bindings";
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4847,6 +4847,7 @@ __metadata:
"@rollup/plugin-typescript": "npm:^12.1.1"
"@types/node": "npm:^20.16.12"
"@vitest/coverage-v8": "npm:^2.1.3"
"@xmtp/content-type-group-updated": "npm:^1.0.0"
"@xmtp/content-type-primitives": "npm:^1.0.2"
"@xmtp/content-type-text": "npm:^1.0.0"
"@xmtp/node-bindings": "npm:^0.0.14"
Expand Down

0 comments on commit b7d535d

Please sign in to comment.