subdomainValidation.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import type ApiRequestService from '~/services/data/apiRequestService'
  2. export default class SubdomainValidation {
  3. private apiRequestService: ApiRequestService
  4. public constructor(apiRequestService: ApiRequestService) {
  5. this.apiRequestService = apiRequestService
  6. }
  7. /**
  8. * Le sous-domaine est valide s'il contient entre 2 et 60 caractères, et pas de caractères spéciaux
  9. * @param subdomain
  10. */
  11. public static isValid(subdomain: string | null): boolean {
  12. return (
  13. subdomain !== null &&
  14. subdomain.match(/^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/) !== null
  15. )
  16. }
  17. /**
  18. * Returns true if the given subdomain has not been registered yet or is not reserved
  19. * @param subdomain
  20. */
  21. public async isAvailable(subdomain: string): Promise<boolean> {
  22. const subdomainAvailability: Response = await this.apiRequestService.get(
  23. '/api/subdomains/is_available',
  24. { subdomain },
  25. )
  26. // @ts-expect-error Si pas d'erreur, la réponse ici doit avoir une prop 'available'
  27. return subdomainAvailability && subdomainAvailability.available === true
  28. }
  29. }