File size: 1,336 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useQuery } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import type { UseQueryOptions } from '@tanstack/react-query';

export const getCacheKey = ( domain: string, mailboxName: string ) => [
	'emails',
	'titan',
	domain,
	'check-mailbox-availability',
	mailboxName,
];

// A list of error statuses that are seen as final and shouldn't be retried for this query
const finalErrorStatuses = [ 400, 401, 403, 409 ];

/**
 * Checks whether a mailbox name/domain i.e. email address is available for creation.
 * @param domainName The domain name of the mailbox
 * @param mailboxName The mailbox name
 * @param queryOptions Optional options to pass to the underlying query engine
 * @returns Returns the result of the `useQuery` call
 */
export const useGetTitanMailboxAvailability = (
	domainName: string,
	mailboxName: string,
	queryOptions: Omit< UseQueryOptions< any, any >, 'queryKey' > = {}
) => {
	return useQuery< any, any >( {
		queryKey: getCacheKey( domainName, mailboxName ),
		queryFn: () =>
			wpcom.req.get( {
				path: `/emails/titan/${ encodeURIComponent(
					domainName
				) }/check-mailbox-availability/${ encodeURIComponent( mailboxName ) }`,
				apiNamespace: 'wpcom/v2',
			} ),
		...queryOptions,
		retry: ( count, { message: status } ) => finalErrorStatuses.includes( status ),
	} );
};