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

options.text validation #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 22 additions & 8 deletions warpcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,43 @@ 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 {
private static readonly COMPOSER_BASE_URL = 'https://warpcast.com/~/compose';

/**
* @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) {
Expand All @@ -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);
}
Expand Down