diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index 666df1631..b2203d3ff 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -4,6 +4,7 @@ export * from './app'; export * from './authentication'; export * from './feed'; export * from './follow'; +export * from './health'; export * from './graph'; export * from './namespace'; export * from './group'; diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts index 3ec1b3b8c..78da131d1 100644 --- a/packages/client/src/actions/username.ts +++ b/packages/client/src/actions/username.ts @@ -1,11 +1,95 @@ -import type { Username, UsernameRequest, UsernamesRequest } from '@lens-protocol/graphql'; -import { UsernameQuery, UsernamesQuery } from '@lens-protocol/graphql'; +import type { + AssignUsernameToAccountRequest, + AssignUsernameToAccountResult, + CreateUsernameRequest, + CreateUsernameResult, + UnassignUsernameFromAccountRequest, + UnassignUsernameToAccountResult, + Username, + UsernameRequest, + UsernamesRequest, +} from '@lens-protocol/graphql'; +import { + AssignUsernameToAccountMutation, + CreateUsernameMutation, + UnassignUsernameFromAccountMutation, + UsernameQuery, + UsernamesQuery, +} from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; +import type { SessionClient } from '../clients'; +import type { UnauthenticatedError, UnexpectedError } from '../errors'; + import type { AnyClient } from '../clients'; -import type { UnexpectedError } from '../errors'; import type { Paginated } from '../types'; +/** + * Create a username + * + * ```ts + * const result = await createUsername(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function createUsername( + client: SessionClient, + request: CreateUsernameRequest, +): ResultAsync { + return client.mutation(CreateUsernameMutation, { request }); +} + +/** + * Assign a username to the account associated with the authenticated session. + * + * ```ts + * const result = await assignUsernameToAccount(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function assignUsernameToAccount( + client: SessionClient, + request: AssignUsernameToAccountRequest, +): ResultAsync { + return client.mutation(AssignUsernameToAccountMutation, { request }); +} + +/** + * Unassign a username to the account associated with the authenticated session. + * + * ```ts + * const result = await unassignUsernameFromAccount(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function unassignUsernameFromAccount( + client: SessionClient, + request: UnassignUsernameFromAccountRequest, +): ResultAsync { + return client.query(UnassignUsernameFromAccountMutation, { request }); +} + /** * Fetch username details. * diff --git a/packages/graphql/src/username.ts b/packages/graphql/src/username.ts index e5e57092a..2d0936f49 100644 --- a/packages/graphql/src/username.ts +++ b/packages/graphql/src/username.ts @@ -1,6 +1,141 @@ -import { PaginatedResultInfo, Username } from './fragments'; +import type { FragmentOf } from 'gql.tada'; +import { + PaginatedResultInfo, + SelfFundedTransactionRequest, + SponsoredTransactionRequest, + TransactionWillFail, + Username, +} from './fragments'; import { type RequestOf, graphql } from './graphql'; +const CreateUsernameResponse = graphql( + `fragment CreateUsernameResponse on CreateUsernameResponse { + __typename + hash + }`, +); +export type CreateUsernameResponse = FragmentOf; + +const CreateUsernameResult = graphql( + `fragment CreateUsernameResult on CreateUsernameResult { + ...on CreateUsernameResponse { + ...CreateUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + CreateUsernameResponse, + SelfFundedTransactionRequest, + TransactionWillFail, + SponsoredTransactionRequest, + ], +); +export type CreateUsernameResult = FragmentOf; + +export const CreateUsernameMutation = graphql( + `mutation CreateUsername($request: CreateUsernameRequest!) { + value: createUsername(request: $request) { + ...CreateUsernameResult + } + }`, + [CreateUsernameResult], +); +export type CreateUsernameRequest = RequestOf; + +const AssignUsernameResponse = graphql( + `fragment AssignUsernameResponse on AssignUsernameResponse { + __typename + hash + }`, +); +export type AssignUsernameResponse = FragmentOf; + +const AssignUsernameToAccountResult = graphql( + `fragment AssignUsernameToAccountResult on AssignUsernameToAccountResult { + ...on AssignUsernameResponse { + ...AssignUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + AssignUsernameResponse, + ], +); +export type AssignUsernameToAccountResult = FragmentOf; + +export const AssignUsernameToAccountMutation = graphql( + `mutation AssignUsernameToAccount($request: AssignUsernameToAccountRequest!) { + value: assignUsernameToAccount(request: $request) { + ...AssignUsernameToAccountResult + } + }`, + [AssignUsernameToAccountResult], +); +export type AssignUsernameToAccountRequest = RequestOf; + +const UnassignUsernameResponse = graphql( + `fragment UnassignUsernameResponse on UnassignUsernameResponse { + __typename + hash + }`, +); +export type UnassignUsernameResponse = FragmentOf; + +const UnassignUsernameToAccountResult = graphql( + `fragment UnassignUsernameToAccountResult on UnassignUsernameToAccountResult { + ...on UnassignUsernameResponse { + ...UnassignUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + UnassignUsernameResponse, + ], +); +export type UnassignUsernameToAccountResult = FragmentOf; + +export const UnassignUsernameFromAccountMutation = graphql( + `mutation LeaveGroup($request: UnassignUsernameFromAccountRequest!) { + value: unassignUsernameFromAccount(request: $request) { + ...UnassignUsernameToAccountResult + } + }`, + [UnassignUsernameToAccountResult], +); +export type UnassignUsernameFromAccountRequest = RequestOf< + typeof UnassignUsernameFromAccountMutation +>; + export const UsernameQuery = graphql( `query Username($request: UsernameRequest!) { value: username(request: $request) {