refactor(custom-domain): simplify record mapping logic (#10685)

Refactored the mapping logic in custom-domain.service to improve
readability and ensure proper handling of undefined records. This change
introduces an early return for nullish records and maintains existing
validation behavior.
This commit is contained in:
Antoine Moreaux
2025-03-06 14:19:23 +01:00
committed by GitHub
parent 341439092a
commit 17b488dd3b
2 changed files with 66 additions and 36 deletions

View File

@ -123,6 +123,36 @@ describe('CustomDomainService', () => {
hostname: customDomain, hostname: customDomain,
}); });
}); });
it('should return even if no record found', async () => {
const customDomain = 'example.com';
const mockResult = {
id: 'custom-id',
hostname: customDomain,
ownership_verification: undefined,
verification_errors: [],
};
const cloudflareMock = {
customHostnames: {
list: jest.fn().mockResolvedValueOnce({ result: [mockResult] }),
},
};
jest.spyOn(environmentService, 'get').mockReturnValue('test-zone-id');
jest
.spyOn(domainManagerService, 'getFrontUrl')
.mockReturnValue(new URL('https://front.domain'));
(customDomainService as any).cloudflareClient = cloudflareMock;
const result =
await customDomainService.getCustomDomainDetails(customDomain);
expect(result).toEqual({
id: 'custom-id',
customDomain: customDomain,
records: expect.any(Array),
});
});
it('should return domain details if a single result is found', async () => { it('should return domain details if a single result is found', async () => {
const customDomain = 'example.com'; const customDomain = 'example.com';

View File

@ -79,8 +79,9 @@ export class CustomDomainService {
response.result[0].ownership_verification, response.result[0].ownership_verification,
...(response.result[0].ssl?.validation_records ?? []), ...(response.result[0].ssl?.validation_records ?? []),
] ]
.map<CustomDomainValidRecords['records'][0] | undefined>( .map<CustomDomainValidRecords['records'][0] | undefined>((record) => {
(record: Record<string, string>) => { if (!record) return;
if ( if (
'txt_name' in record && 'txt_name' in record &&
'txt_value' in record && 'txt_value' in record &&
@ -114,8 +115,7 @@ export class CustomDomainService {
value: record.value, value: record.value,
}; };
} }
}, })
)
.filter(isDefined) .filter(isDefined)
.concat([ .concat([
{ {