Lens SDK

repository·main·Indexed 42 days ago

https://github.com/lens-protocol/lens-sdk

The official JavaScript SDK for interacting with the Lens Protocol. It provides a suite of packages, including a framework-agnostic client, React hooks, and GraphQL bindings, to build decentralized social applications. The SDK includes tools for managing account managers, creating and configuring Apps, handling authentication sessions, and performing social actions like posting and following.

Tokens
29.8K
Snippets
143
Records
180
Agent score
54%

What's inside Lens SDK

  1. Use @lens-protocol/graphql with caution

    main

    The @lens-protocol/graphql package provides gql.tada bindings for the Lens API.

    Warning: This package is not intended for direct use by end-users. Its interface is unstable and may change without notice. Developers should use higher-level SDK packages (like @lens-protocol/client) instead of interacting with this package directly.

  2. Usage warning for @lens-protocol/types

    main
    The @lens-protocol/types package contains low-level types and language extensions. It is not intended to be used directly by end-users. Because its interface is subject to change without notice, using it directly carries the risk of breaking changes in your application.
  3. Understand Post validation outcomes with OperationValidationOutcomeFragment

    main

    When performing operations on a post, the SDK provides validation results via OperationValidationOutcomeFragment. This fragment returns a union of three possible states:

    • PostOperationValidationPassed: The operation is valid.
    • PostOperationValidationUnknown: The operation requires extra checks (returns extraChecksRequired).
    • PostOperationValidationFailed: The operation is invalid (returns unsatisfiedRules and a reason).
  4. Understand AccountFollowOperationValidationOutcome

    main

    The AccountFollowOperationValidationOutcome is a union type used to determine if a follow or unfollow operation is permitted. It can result in one of three states:

    1. AccountFollowOperationValidationPassed: The operation is valid.
    2. AccountFollowOperationValidationUnknown: Extra checks are required (returns AccountFollowOperationValidationUnknown).
    3. AccountFollowOperationValidationFailed: The operation is invalid (returns AccountFollowOperationValidationFailed containing unsatisfiedRules).
  5. Understand the structure of a Storage Item

    main

    In the Lens SDK, data stored in external storage is wrapped in an IStorageItem<Data> structure. This ensures that every piece of data is accompanied by versioning and timestamp metadata, which is critical for data migration and integrity.

    A storage item consists of:

    • data: The actual payload of type Data.
    • metadata: A StorageMetadata object containing:
      • version: A positive integer representing the schema version.
      • createdAt: A positive integer timestamp.
      • updatedAt: A positive integer timestamp.
    interface IStorageItem<Data> {
      data: Data;
      metadata: {
        version: number;
        createdAt: number;
        updatedAt: number;
      };
    }
  6. Use SuspenseResult for React Suspense integration

    main

    The @lens-protocol/react package supports React Suspense. When a hook is configured to use Suspense, it may return a SuspenseResult<T>, which simply contains the data property.

    In a SuspendableResult<T> (which is ReadResult<T> | SuspenseResult<T>), the presence of data without a loading flag typically indicates that the component has successfully suspended and resumed with the required data.

    export type SuspenseResult<T> = { data: T };
    export type SuspendableResult<T> = ReadResult<T> | SuspenseResult<T>;
  7. Understand Feed, Namespace, and Group operation validation outcomes

    main

    Lens uses a standardized validation pattern for operations (like posting to a feed, creating a namespace, or joining a group). The OperationValidationOutcome is a union type representing three possible states:

    1. Passed: The operation is allowed.
    2. Unknown: The operation requires extra checks (returns extraChecksRequired rules).
    3. Failed: The operation is denied (returns unsatisfiedRules and a reason).

    Specific fragments for these outcomes include:

    • FeedOperationValidationOutcomeFragment (for Feeds)
    • NamespaceOperationValidationOutcomeFragment (for Namespaces)
    • GroupOperationValidationOutcomeFragment (for Groups)
  8. Handle read hook results with ReadResult

    main

    When using read hooks in @lens-protocol/react, the returned data follows the ReadResult<T, E> discriminated union pattern. You should use the loading boolean to determine the state of the operation:

    1. If loading is true, both data and error are undefined.
    2. If loading is false and error is undefined, the data property contains the successful result of type T.
    3. If loading is false and error is defined, the error property contains the error of type E.

    This pattern ensures type safety when checking for data availability.

    type ReadResult<T, E = never> =
      | { data: undefined; error: undefined; loading: true; }
      | { data: T; error: undefined; loading: false; }
      | { data: undefined; error: E; loading: false; };
  9. Identify specific JWT types: AccessToken, RefreshToken, and IdToken

    main

    The SDK uses branded types (via type-fest's Tagged) to distinguish between different types of JWTs at the type level. This prevents developers from accidentally using a RefreshToken where an AccessToken is required.

    Available branded types:

    • AccessToken: A JWT used for authenticating requests.
    • RefreshToken: A JWT used to obtain new access tokens.
    • IdToken: A JWT containing user identity information.

    Note: While the type definitions are public, the specific casting functions (like accessToken()) are marked as @internal and should generally be used via the generic compactJwt<T> utility if you need to manually cast a string to one of these specific types.

    import { compactJwt, type AccessToken, type RefreshToken, type IdToken } from '@lens-protocol/types';
    
    // Manually casting a string to a specific branded JWT type
    const myAccessToken = compactJwt<AccessToken>("header.payload.signature");
    const myRefreshToken = compactJwt<RefreshToken>("header.payload.signature");
    const myIdToken = compactJwt<IdToken>("header.payload.signature");