From 8f4831c7873ccb9f19025d184970d6049fe777ab Mon Sep 17 00:00:00 2001 From: iSpeakNerd <96841384+iSpeakNerd@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:43:25 -0800 Subject: [PATCH] options.text validation --- warpcast.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/warpcast.ts b/warpcast.ts index 520e58c..87155ee 100644 --- a/warpcast.ts +++ b/warpcast.ts @@ -11,8 +11,8 @@ interface WarpcastComposeOptions { const hashPattern = /^0x[a-fA-F0-9]{40}$/; // regex pattern for hash validation /** - * Methods to generate a Warpcast composer URL with the given options, - * docs at https://docs.farcaster.xyz/reference/warpcast/cast-composer-intents + * Methods to generate a Warpcast composer intent URL with options, + * as described in the docs at https://docs.farcaster.xyz/reference/warpcast/cast-composer-intents * */ export class WarpcastUrlBuilder { @@ -20,20 +20,34 @@ export class WarpcastUrlBuilder { /** * @param options.text - The text of the cast + * - max 320 characters for short casts + * - max 1024 characters for long casts * - * @param options.embeds - The embeds to include in the cast (max 2), must be valid URLs + * @param options.embeds - The embed URLs to include in the cast + * - max 2 + * - must be valid URLs * * @param options.parentCastHash - The parent cast hash for replies, - * `/^0x[a-fA-F0-9]{40}$/`, - * overrides `options.channelKey` + * - must be in valid hash format `/^0x[a-fA-F0-9]{40}$/` + * - overrides `options.channelKey` * - * @param options.channelKey - The name of the channel to post to + * @param options.channelKey - The name of the channel to cast into * - * @returns The composer URL + * @returns The Warpcast intent URL */ static composerUrl(options: WarpcastComposeOptions): string { const params = new URLSearchParams(); + if (options.text.length > 1024) { + throw new Error( + 'Farcaster does not support cast text longer than 1024 characters' + ); + } + if (options.text.length > 320) { + console.warn( + 'This is a long cast. It will show only the first 320 characters on timeline' + ); + } params.append('text', options.text); if (options.embeds?.length) { @@ -59,7 +73,7 @@ export class WarpcastUrlBuilder { if (options.parentCastHash) { if (!hashPattern.test(options.parentCastHash)) { - throw new Error('Invalid parent cast hash'); + throw new Error('Invalid parent cast hash format'); } params.append('parentCastHash', options.parentCastHash); }