[SDK] Migrate the account surface to the pds and service clients (#11367)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,39 +1,37 @@
|
||||
import {type ComAtprotoServerCreateAppPassword} from '@atproto/api'
|
||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {STALE} from '#/state/queries'
|
||||
import {useAgent} from '../session'
|
||||
import {com} from '#/lexicons'
|
||||
import {usePdsClient} from '../session'
|
||||
|
||||
const RQKEY_ROOT = 'app-passwords'
|
||||
export const RQKEY = () => [RQKEY_ROOT]
|
||||
|
||||
export function useAppPasswordsQuery() {
|
||||
const agent = useAgent()
|
||||
const client = usePdsClient()
|
||||
return useQuery({
|
||||
staleTime: STALE.MINUTES.FIVE,
|
||||
queryKey: RQKEY(),
|
||||
queryFn: async () => {
|
||||
const res = await agent.com.atproto.server.listAppPasswords({})
|
||||
return res.data.passwords
|
||||
const data = await client.call(com.atproto.server.listAppPasswords)
|
||||
return data.passwords
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useAppPasswordCreateMutation() {
|
||||
const queryClient = useQueryClient()
|
||||
const agent = useAgent()
|
||||
const client = usePdsClient()
|
||||
return useMutation<
|
||||
ComAtprotoServerCreateAppPassword.OutputSchema,
|
||||
com.atproto.server.createAppPassword.$OutputBody,
|
||||
Error,
|
||||
{name: string; privileged: boolean}
|
||||
>({
|
||||
mutationFn: async ({name, privileged}) => {
|
||||
return (
|
||||
await agent.com.atproto.server.createAppPassword({
|
||||
name,
|
||||
privileged,
|
||||
})
|
||||
).data
|
||||
return await client.call(com.atproto.server.createAppPassword, {
|
||||
name,
|
||||
privileged,
|
||||
})
|
||||
},
|
||||
onSuccess() {
|
||||
queryClient.invalidateQueries({
|
||||
@@ -45,10 +43,10 @@ export function useAppPasswordCreateMutation() {
|
||||
|
||||
export function useAppPasswordDeleteMutation() {
|
||||
const queryClient = useQueryClient()
|
||||
const agent = useAgent()
|
||||
const client = usePdsClient()
|
||||
return useMutation<void, Error, {name: string}>({
|
||||
mutationFn: async ({name}) => {
|
||||
await agent.com.atproto.server.revokeAppPassword({
|
||||
await client.call(com.atproto.server.revokeAppPassword, {
|
||||
name,
|
||||
})
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {ComAtprotoTempCheckHandleAvailability} from '@atproto/api'
|
||||
import {type DatetimeString, type HandleString} from '@atproto/syntax'
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {
|
||||
@@ -7,10 +7,11 @@ import {
|
||||
PUBLIC_BSKY_SERVICE,
|
||||
} from '#/lib/constants'
|
||||
import {useDebouncedValue} from '#/lib/hooks/useDebouncedValue'
|
||||
import {createServiceClient} from '#/lib/lexClient'
|
||||
import {createFullHandle} from '#/lib/strings/handles'
|
||||
import {useAnalytics} from '#/analytics'
|
||||
import {com} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
import {Agent} from '../session/agent'
|
||||
|
||||
export const RQKEY_handleAvailability = (
|
||||
handle: string,
|
||||
@@ -79,25 +80,33 @@ export async function checkHandleAvailability(
|
||||
},
|
||||
) {
|
||||
if (serviceDid === BSKY_SERVICE_DID) {
|
||||
const agent = new Agent(null, {service: BSKY_SERVICE})
|
||||
/*
|
||||
* Runs pre-auth during signup, so it goes through a one-off service client
|
||||
* rather than a session-scoped one. The target is the fixed entryway rather
|
||||
* than a user-supplied host, but there is still no session to hang a client
|
||||
* off.
|
||||
*/
|
||||
const client = createServiceClient(BSKY_SERVICE)
|
||||
// entryway has a special API for handle availability
|
||||
const {data} = await agent.com.atproto.temp.checkHandleAvailability({
|
||||
handle,
|
||||
birthDate,
|
||||
const data = await client.call(com.atproto.temp.checkHandleAvailability, {
|
||||
// the caller assembles this from a validated username and domain
|
||||
handle: handle as HandleString,
|
||||
// callers pass an ISO date string built from the birth-date picker
|
||||
birthDate: birthDate as DatetimeString | undefined,
|
||||
email,
|
||||
})
|
||||
|
||||
if (
|
||||
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultAvailable>(
|
||||
bsky.isType(
|
||||
com.atproto.temp.checkHandleAvailability.resultAvailable,
|
||||
data.result,
|
||||
ComAtprotoTempCheckHandleAvailability.isResultAvailable,
|
||||
)
|
||||
) {
|
||||
return {available: true} as const
|
||||
} else if (
|
||||
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultUnavailable>(
|
||||
bsky.isType(
|
||||
com.atproto.temp.checkHandleAvailability.resultUnavailable,
|
||||
data.result,
|
||||
ComAtprotoTempCheckHandleAvailability.isResultUnavailable,
|
||||
)
|
||||
) {
|
||||
return {
|
||||
@@ -110,14 +119,18 @@ export async function checkHandleAvailability(
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// 3rd party PDSes won't have this API so just try and resolve the handle
|
||||
const agent = new Agent(null, {service: PUBLIC_BSKY_SERVICE})
|
||||
/*
|
||||
* 3rd party PDSes won't have this API so just try and resolve the handle.
|
||||
* This is an unauthenticated public-appview read, not a call against the
|
||||
* user's chosen host.
|
||||
*/
|
||||
const client = createServiceClient(PUBLIC_BSKY_SERVICE)
|
||||
try {
|
||||
const res = await agent.resolveHandle({
|
||||
handle,
|
||||
const data = await client.call(com.atproto.identity.resolveHandle, {
|
||||
handle: handle as HandleString,
|
||||
})
|
||||
|
||||
if (res.data.did) {
|
||||
if (data.did) {
|
||||
return {available: false} as const
|
||||
}
|
||||
} catch {}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import {useCallback} from 'react'
|
||||
import {type HandleString} from '@atproto/syntax'
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {STALE} from '#/state/queries'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAgent, usePdsClient} from '#/state/session'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
const handleQueryKeyRoot = 'handle'
|
||||
const fetchHandleQueryKey = (handleOrDid: string) => [
|
||||
@@ -36,11 +38,15 @@ export function useUpdateHandleMutation(opts?: {
|
||||
onSuccess?: (handle: string) => void
|
||||
}) {
|
||||
const queryClient = useQueryClient()
|
||||
const agent = useAgent()
|
||||
const client = usePdsClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({handle}: {handle: string}) => {
|
||||
await agent.updateHandle({handle})
|
||||
// `agent.updateHandle` was a pure alias for this method
|
||||
await client.call(com.atproto.identity.updateHandle, {
|
||||
// callers validate the handle before submitting
|
||||
handle: handle as HandleString,
|
||||
})
|
||||
},
|
||||
onSuccess(_data, variables) {
|
||||
opts?.onSuccess?.(variables.handle)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {Agent} from '../session/agent'
|
||||
import {createServiceClient} from '#/lib/lexClient'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
const RQKEY_ROOT = 'service'
|
||||
export const RQKEY = (serviceUrl: string) => [RQKEY_ROOT, serviceUrl]
|
||||
@@ -9,9 +10,12 @@ export function useServiceQuery(serviceUrl: string) {
|
||||
return useQuery({
|
||||
queryKey: RQKEY(serviceUrl),
|
||||
queryFn: async () => {
|
||||
const agent = new Agent(null, {service: serviceUrl})
|
||||
const res = await agent.com.atproto.server.describeServer()
|
||||
return res.data
|
||||
/*
|
||||
* The host is whatever the user typed or picked, so this describes it
|
||||
* through a one-off service client rather than a session-scoped one.
|
||||
*/
|
||||
const client = createServiceClient(serviceUrl)
|
||||
return await client.call(com.atproto.server.describeServer)
|
||||
},
|
||||
enabled: isValidUrl(serviceUrl),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user