File size: 3,621 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { translate } from 'i18n-calypso';
import { stringify } from 'qs';
import validator from 'validator';
import { fetchAndParse, wpcomRequest } from '../wpcom-request-controls';
import {
	receiveCategories,
	receiveDomainSuggestionsSuccess,
	receiveDomainSuggestionsError,
	fetchDomainSuggestions,
	receiveDomainAvailability,
} from './actions';
import { getFormattedPrice } from './utils';
import type { DomainSuggestion, DomainSuggestionQuery } from './types';

function getAvailabilityURL( domainName: string ) {
	return `https://public-api.wordpress.com/rest/v1.3/domains/${ encodeURIComponent(
		domainName
	) }/is-available?is_cart_pre_check=true`;
}

function suggestionsLackThisFQDN( suggestions: DomainSuggestion[], domainName: string ) {
	return (
		validator.isFQDN( domainName ) &&
		! suggestions.some( ( s ) => s.domain_name.toLowerCase() === domainName )
	);
}

export const isAvailable = function* isAvailable( domainName: string ) {
	const url = getAvailabilityURL( domainName );

	try {
		const { body } = yield fetchAndParse( url );
		return receiveDomainAvailability( domainName, body );
	} catch {
		// the API returns a status of 'unknown' if it can not accurately determine
		// availability, we will return the same status if the API request fails.
		return receiveDomainAvailability( domainName, {
			domain_name: domainName,
			mappable: 'unknown',
			status: 'unknown',
			supports_privacy: false,
		} );
	}
};

export function* getCategories() {
	const { body } = yield fetchAndParse(
		'https://public-api.wordpress.com/wpcom/v2/onboarding/domains/categories'
	);
	return receiveCategories( body );
}

export function* __internalGetDomainSuggestions( queryObject: DomainSuggestionQuery ) {
	// If normalized search string (`query`) contains no alphanumerics, endpoint 404s
	if ( ! queryObject.query ) {
		return receiveDomainSuggestionsError( 'Empty query' );
	}

	yield fetchDomainSuggestions();

	try {
		const suggestions: DomainSuggestion[] = yield wpcomRequest( {
			apiVersion: '1.1',
			path: '/domains/suggestions',
			query: stringify( queryObject ),
		} );

		if ( ! Array.isArray( suggestions ) ) {
			// Other internal server errors
			return receiveDomainSuggestionsError(
				translate( 'Invalid response from the server' ) as string
			);
		}

		// if the query is a FQDN and the results don't have it,
		// this implies that the user is searching for an unavailable domain name
		// TODO: query the availability endpoint to find the exact reason why it's unavailable
		// all the possible responses can be found here https://github.com/Automattic/wp-calypso/blob/trunk/client/lib/domains/registration/availability-messages.js#L40-L390
		if ( suggestionsLackThisFQDN( suggestions, queryObject.query ) ) {
			const unavailableSuggestion: DomainSuggestion = {
				domain_name: queryObject.query,
				unavailable: true,
				cost: '',
				raw_price: 0,
				currency_code: '',
			};
			suggestions.unshift( unavailableSuggestion );
		}

		const processedSuggestions = suggestions.map( ( suggestion: DomainSuggestion ) => {
			if ( suggestion.unavailable ) {
				return suggestion;
			}
			return {
				...suggestion,
				...( suggestion.raw_price &&
					suggestion.currency_code && {
						cost: getFormattedPrice( suggestion.raw_price, suggestion.currency_code ),
					} ),
			};
		} );

		return receiveDomainSuggestionsSuccess( queryObject, processedSuggestions );
	} catch ( e ) {
		// e.g. no connection, or JSON parsing error
		return receiveDomainSuggestionsError(
			( e as Error ).message || ( translate( 'Error while fetching server response' ) as string )
		);
	}
}