All files / src/tools/auth0/handlers customDomains.ts

86.66% Statements 26/30
60% Branches 6/10
100% Functions 8/8
89.65% Lines 26/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1001x     1x                                         1x       138x             1x           4x       12x 12x       12x   9x   9x   3x         3x             1x 5x   5x 5x 5x     2x 2x       2x     2x       2x 2x       5x     5x      
import DefaultAPIHandler, { order } from './default';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      custom_domain_id: { type: 'string' },
      custom_client_ip_header: {
        type: 'string',
        nullable: true,
        enum: ['true-client-ip', 'cf-connecting-ip', 'x-forwarded-for', null],
      },
      domain: { type: 'string' },
      primary: { type: 'boolean' },
      status: { type: 'string', enum: ['pending_verification', 'ready', 'disabled', 'pending'] },
      type: { type: 'string', enum: ['auth0_managed_certs', 'self_managed_certs'] },
      verification: { type: 'object' },
    },
    required: ['domain', 'type'],
  },
};
 
export default class CustomDomainsHadnler extends DefaultAPIHandler {
  existing: Asset[] | null;
 
  constructor(config: DefaultAPIHandler) {
    super({
      ...config,
      type: 'customDomains',
      id: 'custom_domain_id',
      identifiers: ['domain'],
      stripCreateFields: ['status', 'primary', 'verification'],
      functions: {
        delete: (args) => this.client.customDomains.delete({ id: args.custom_domain_id }),
      },
    });
  }
 
  objString(item: Asset): string {
    return super.objString(item.domain);
  }
 
  async getType(): Promise<Asset | null> {
    try {
      Iif (this.existing) {
        return this.existing;
      }
 
      const customDomains = await this.client.customDomains.getAll({ paginate: false });
 
      this.existing = customDomains;
 
      return customDomains;
    } catch (err) {
      Eif (
        err.statusCode === 403 &&
        err.message ===
          'The account is not allowed to perform this operation, please contact our support team'
      ) {
        return null;
      }
      throw err;
    }
  }
 
  @order('50')
  async processChanges(assets: Assets): Promise<void> {
    const { customDomains } = assets;
 
    Iif (!customDomains) return;
    const changes = await this.calcChanges(assets).then((changes) => {
      const changesWithoutUpdates = {
        ...changes,
        create: changes.create.map((customDomainToCreate) => {
          const newCustomDomain = { ...customDomainToCreate };
          Iif (customDomainToCreate.custom_client_ip_header === null) {
            delete newCustomDomain.custom_client_ip_header;
          }
 
          return newCustomDomain;
        }),
        delete: changes.del.map((deleteToMake) => {
          const deleteWithSDKCompatibleID = {
            ...deleteToMake,
            id: deleteToMake.custom_domain_id,
          };
          delete deleteWithSDKCompatibleID['custom_domain_id'];
          return deleteWithSDKCompatibleID;
        }),
        update: [], //Do not perform custom domain updates because not supported by SDK
      };
      return changesWithoutUpdates;
    });
 
    await super.processChanges(assets, changes);
  }
}