diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index 0e9aa64d7..666df1631 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -11,3 +11,4 @@ export * from './post'; export * from './posts'; export * from './timeline'; export * from './transactions'; +export * from './username'; diff --git a/packages/client/src/actions/timeline.ts b/packages/client/src/actions/timeline.ts index e9d2243e0..41c709a2a 100644 --- a/packages/client/src/actions/timeline.ts +++ b/packages/client/src/actions/timeline.ts @@ -32,7 +32,7 @@ export function fetchTimeline( } /** - * Fetch fetchTimelineHighlights from an account. + * Fetch Timeline Highlights for an account. * * ```ts * const result = await fetchTimelineHighlights(anyClient, { diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts new file mode 100644 index 000000000..3ec1b3b8c --- /dev/null +++ b/packages/client/src/actions/username.ts @@ -0,0 +1,47 @@ +import type { Username, UsernameRequest, UsernamesRequest } from '@lens-protocol/graphql'; +import { UsernameQuery, UsernamesQuery } from '@lens-protocol/graphql'; +import type { ResultAsync } from '@lens-protocol/types'; + +import type { AnyClient } from '../clients'; +import type { UnexpectedError } from '../errors'; +import type { Paginated } from '../types'; + +/** + * Fetch username details. + * + * ```ts + * const result = await fetchUsername(anyClient, { + * username: { localName: 'wagmi' }, + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The username details. + */ +export function fetchUsername( + client: AnyClient, + request: UsernameRequest, +): ResultAsync { + return client.query(UsernameQuery, { request }); +} + +/** + * Fetch usernames owned by an address. + * + * ```ts + * const result = await fetchUsernames(anyClient, { + * owner: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of owned usernames. + */ +export function fetchUsernames( + client: AnyClient, + request: UsernamesRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(UsernamesQuery, { request }); +} diff --git a/packages/graphql/src/fragments/index.ts b/packages/graphql/src/fragments/index.ts index 42e7f483d..dd90d4266 100644 --- a/packages/graphql/src/fragments/index.ts +++ b/packages/graphql/src/fragments/index.ts @@ -7,3 +7,4 @@ export * from './SponsoredTransactionRequest'; export * from './SelfFundedTransactionRequest'; export * from './TransactionWillFail'; export * from './username'; +export * from './namespace'; diff --git a/packages/graphql/src/fragments/namespace.ts b/packages/graphql/src/fragments/namespace.ts new file mode 100644 index 000000000..368b03517 --- /dev/null +++ b/packages/graphql/src/fragments/namespace.ts @@ -0,0 +1,27 @@ +import type { FragmentOf } from 'gql.tada'; +import { graphql } from '../graphql'; + +export const UsernameNamespace = graphql( + `fragment UsernameNamespace on UsernameNamespace { + __typename + address + namespace + createdAt + metadata { + __typename + description + id + } + owner + # TODO: Implement rules + # rules { + # anyOf { + # ...RULE + # } + # required { + # ...RULE + # } + # } + }`, +); +export type UsernameNamespace = FragmentOf; diff --git a/packages/graphql/src/fragments/username.ts b/packages/graphql/src/fragments/username.ts index e99fde15b..3a7fcb89b 100644 --- a/packages/graphql/src/fragments/username.ts +++ b/packages/graphql/src/fragments/username.ts @@ -1,22 +1,20 @@ import type { FragmentOf } from 'gql.tada'; import { graphql } from '../graphql'; +import { UsernameNamespace } from './namespace'; -export const Username = graphql(` - fragment Username on Username { +export const Username = graphql( + `fragment Username on Username { __typename id value - namespace { - address - namespace - metadata { - description - id - } - } + localName linkedTo ownedBy timestamp - } - `); + namespace { + ...UsernameNamespace + } + }`, + [UsernameNamespace], +); export type Username = FragmentOf; diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts index 7d52922c5..6fe340df7 100644 --- a/packages/graphql/src/index.ts +++ b/packages/graphql/src/index.ts @@ -15,3 +15,4 @@ export * from './notifications'; export * from './post'; export * from './timeline'; export * from './transactions'; +export * from './username'; diff --git a/packages/graphql/src/username.ts b/packages/graphql/src/username.ts new file mode 100644 index 000000000..e5e57092a --- /dev/null +++ b/packages/graphql/src/username.ts @@ -0,0 +1,28 @@ +import { PaginatedResultInfo, Username } from './fragments'; +import { type RequestOf, graphql } from './graphql'; + +export const UsernameQuery = graphql( + `query Username($request: UsernameRequest!) { + value: username(request: $request) { + ...Username + } + }`, + [Username], +); +export type UsernameRequest = RequestOf; + +export const UsernamesQuery = graphql( + `query Usernames($request: UsernamesRequest!) { + value: usernames(request: $request) { + __typename + items { + ...Username + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [Username, PaginatedResultInfo], +); +export type UsernamesRequest = RequestOf;