Utilities Library
Utility functions for interacting with Lens Social Protocol, including formatting and data manipulation.
Installation
Usage
The utilities provided in this module can be imported and used in your project as follows:
Constants
| Name | Description |
|---|---|
LensChainId | The chain ID for the Lens Chain mainnet |
LensChainNativeToken | The native token address in Lens Chain |
LensChainTestnetId | The chain ID for the Lens Chain testnet |
ZeroAddress | A zero (or empty) address in EVM-compatible blockchains |
Functions
arweaveUrlToGatewayUrl string
Convert an Arweave URL to a gateway URL
Props
| Prop | Description | Type |
|---|---|---|
arUrl | The Arweave URL to convert | string |
gatewayDomain | The gateway domain to use (default: https://arweave.net/) | string |
formatFollowerCount string
Format a follower count as a string, using "k" for thousands and "m" for millions
Props
| Prop | Description | Type |
|---|---|---|
count | The follower count to format | number |
getAudioExtension string
Get the file extension for a given MediaAudioType
Props
| Prop | Description | Type |
|---|---|---|
mediaAudioType | The MediaAudioType to get the extension for | MediaAudioType |
getCidFromIpfsUrl string
Extract the CID from an IPFS URL
Props
| Prop | Description | Type |
|---|---|---|
ipfsUrl | The IPFS URL to extract the CID from | string |
getDisplayName string
Get the display name for an account, prioritizing the metadata name, then the username, and finally truncating the address.
Props
| Prop | Description | Type |
|---|---|---|
account | The account to get the display name for | Account |
getMetadataContent string | undefined
Safely extracts a textual content field from a post's metadata, if present.
This is useful for metadata types that include a content property (e.g., text or article types).
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
getUsernamePath string
Get the path for a Lens username, optionally including the namespace if it's not the default "lens" namespace.
Props
| Prop | Description | Type |
|---|---|---|
username |
| string |
namespace |
| EvmAddress |
getVideoExtension string
Get the file extension for a given MediaVideoType
Props
| Prop | Description | Type |
|---|---|---|
mediaVideoType | The MediaVideoType to get the extension for | MediaVideoType |
hasMetadata post is (Post & { metadata: PostMetadata }) | (ReferencedPost & { metadata: PostMetadata })
Type guard that checks whether a post has a metadata field.
Narrows AnyPost to a Post/ReferencedPost with a non-undefined metadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
ipfsUrlToGatewayUrl string
Convert an IPFS URL to a gateway URL
Props
| Prop | Description | Type |
|---|---|---|
ipfsUrl | The IPFS URL to convert | string |
gatewayDomain | The gateway domain to use (default: https://ipfs.io/ipfs/) | string |
isArticlePost post is ArticlePost
Type guard for posts whose metadata is ArticleMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isAudioPost post is AudioPost
Type guard for posts whose metadata is AudioMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isImagePost post is ImagePost
Type guard for posts whose metadata is ImageMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isLinkPost post is LinkPost
Type guard for posts whose metadata is LinkMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isLiveStreamPost post is LiveStreamPost
Type guard for posts whose metadata is LiveStreamMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isMintPost post is MintPost
Type guard for posts whose metadata is MintMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isSpacePost post is SpacePost
Type guard for posts whose metadata is SpaceMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isStoryPost post is StoryPost
Type guard for posts whose metadata is StoryMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isTextOnlyPost post is TextOnlyPost
Type guard for posts whose metadata is TextOnlyMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isThreeDPost post is ThreeDPost
Type guard for posts whose metadata is ThreeDMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isTransactionPost post is TransactionPost
Type guard for posts whose metadata is TransactionMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
isVideoPost post is VideoPost
Type guard for posts whose metadata is VideoMetadata.
Props
| Prop | Description | Type |
|---|---|---|
post |
| AnyPost |
lensUrlToGatewayUrl string
Convert a Lens URL to a gateway URL
Props
| Prop | Description | Type |
|---|---|---|
lensUrl | The Lens URL to convert | string |
gatewayDomain | The gateway domain to use (default: https://api.grove.storage/) | string |
parseUri string | undefined
Parse a URI and convert it to a gateway URL if it is an IPFS, Arweave, or Lens URL
Props
| Prop | Description | Type |
|---|---|---|
uri | The URI to parse | string | undefined |
toApiError SigningError | TransactionIndexingError | UnauthenticatedError | UnexpectedError | ValidationError
Converts an unknown error to a Lens API UnexpectedError, or returns the original error if it's a known error.'
Props
| Prop | Description | Type |
|---|---|---|
e | unknown |
truncateAddress string
Truncate a string to a maximum length, adding an ellipsis in the middle
Props
| Prop | Description | Type |
|---|---|---|
address | The string to truncate | string |
maxLength | The maximum length of the truncated string, excluding the ellipsis and 0x prefix | number |
truncateUrl string
Truncates a given URL to a specified maximum length and removes common prefixes/suffixes.
- Strips out
http://,https://, and leadingwww.from the URL. - Removes trailing
/or\characters. - Truncates the cleaned URL to the given
maxLength. - Appends an ellipsis (
…) if the original URL exceedsmaxLength.
Props
| Prop | Description | Type |
|---|---|---|
url |
| string |
maxLength |
| number |