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

Strongly typed client #455

Merged
merged 19 commits into from
Sep 13, 2023
Merged

Strongly typed client #455

merged 19 commits into from
Sep 13, 2023

Conversation

neekolas
Copy link
Contributor

@neekolas neekolas commented Sep 12, 2023

Summary

A long-standing source of frustration with our JS SDK is the use of any types for message content. Sending and receiving messages has the content field typed as any.

Message content is of a finite set of types, defined by the codecs that get passed in to Client creation. If you have a codec that returns number, it's possible to get messages with content of type number. If you don't a number codec, it's not possible.

We just need to do some Typescript wrangling to infer those types from the codecs and pass them around to all the conversation methods.

This PR does that.

CleanShot 2023-09-12 at 16 38 37@2x

Notes

I've structured this to be safe for existing implementers. All the existing types default to any. This will make the migration path smoother for developers, but also may lead devs to not take advantage of this feature.

// This will have strong typing
const goodClient = await Client.create(someWallet)
const goodConvo = await goodClient.conversations.newConversation(someAddress)
// Will cause a type error because the client is set to have a type of "string"
await goodConvo.send(1234)

// Coerce the type of Client to the default
let badClient: Client
badClient = await Client.create(someWallet)
const badConvo = await badClient.conversations.newConversation(someAddress)
// The type-checker will ignore this, because Client is set to the default of any
await badConvo.send(1234)

// This will also disable the strong typing
const otherBadConvo: Conversation = await goodClient.conversations.newConversation(someAddress)
// This will be allowed
await otherBadConvo.send(1234)

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Sep 12, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4d30160
Status: ✅  Deploy successful!
Preview URL: https://fd2495c8.xmtp-js.pages.dev
Branch Preview URL: https://nmolnar-strongly-typed-clien.xmtp-js.pages.dev

View logs

src/Client.ts Outdated Show resolved Hide resolved
@neekolas neekolas marked this pull request as ready for review September 13, 2023 00:13
@neekolas neekolas requested a review from a team September 13, 2023 00:13
src/Message.ts Outdated Show resolved Hide resolved
src/Message.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@rygine rygine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits, but nothing blocking. great work, LGTM!

@neekolas neekolas merged commit ad59a8b into beta Sep 13, 2023
7 checks passed
@neekolas neekolas deleted the nmolnar/strongly-typed-client branch September 13, 2023 23:41
@github-actions
Copy link
Contributor

🎉 This PR is included in version 11.0.0-beta.11 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants