feat: add Links field type (#5176)
Closes #5113 --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
import { getUrlHostName } from '~/utils/url/getUrlHostName';
|
||||
|
||||
describe('getUrlHostName', () => {
|
||||
it("returns the URL's hostname", () => {
|
||||
expect(getUrlHostName('https://www.example.com')).toBe('example.com');
|
||||
expect(getUrlHostName('http://subdomain.example.com')).toBe(
|
||||
'subdomain.example.com',
|
||||
);
|
||||
expect(getUrlHostName('https://www.example.com/path')).toBe('example.com');
|
||||
expect(getUrlHostName('https://www.example.com?query=123')).toBe(
|
||||
'example.com',
|
||||
);
|
||||
expect(getUrlHostName('http://localhost:3000')).toBe('localhost');
|
||||
expect(getUrlHostName('example.com')).toBe('example.com');
|
||||
expect(getUrlHostName('www.subdomain.example.com')).toBe(
|
||||
'subdomain.example.com',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns an empty string for invalid URLs', () => {
|
||||
expect(getUrlHostName('?o')).toBe('');
|
||||
expect(getUrlHostName('')).toBe('');
|
||||
expect(getUrlHostName('\\')).toBe('');
|
||||
});
|
||||
});
|
||||
10
packages/twenty-front/src/utils/url/getUrlHostName.ts
Normal file
10
packages/twenty-front/src/utils/url/getUrlHostName.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { absoluteUrlSchema } from '~/utils/validation-schemas/absoluteUrlSchema';
|
||||
|
||||
export const getUrlHostName = (url: string) => {
|
||||
try {
|
||||
const absoluteUrl = absoluteUrlSchema.parse(url);
|
||||
return new URL(absoluteUrl).hostname.replace(/^www\./i, '');
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,34 @@
|
||||
import { absoluteUrlSchema } from '~/utils/validation-schemas/absoluteUrlSchema';
|
||||
|
||||
describe('absoluteUrlSchema', () => {
|
||||
it('validates an absolute url', () => {
|
||||
expect(absoluteUrlSchema.parse('https://www.example.com')).toBe(
|
||||
'https://www.example.com',
|
||||
);
|
||||
expect(absoluteUrlSchema.parse('http://subdomain.example.com')).toBe(
|
||||
'http://subdomain.example.com',
|
||||
);
|
||||
expect(absoluteUrlSchema.parse('https://www.example.com/path')).toBe(
|
||||
'https://www.example.com/path',
|
||||
);
|
||||
expect(absoluteUrlSchema.parse('https://www.example.com?query=123')).toBe(
|
||||
'https://www.example.com?query=123',
|
||||
);
|
||||
expect(absoluteUrlSchema.parse('http://localhost:3000')).toBe(
|
||||
'http://localhost:3000',
|
||||
);
|
||||
});
|
||||
|
||||
it('transforms a non-absolute URL to an absolute URL', () => {
|
||||
expect(absoluteUrlSchema.parse('example.com')).toBe('https://example.com');
|
||||
expect(absoluteUrlSchema.parse('www.subdomain.example.com')).toBe(
|
||||
'https://www.subdomain.example.com',
|
||||
);
|
||||
});
|
||||
|
||||
it('fails for invalid urls', () => {
|
||||
expect(absoluteUrlSchema.safeParse('?o').success).toBe(false);
|
||||
expect(absoluteUrlSchema.safeParse('').success).toBe(false);
|
||||
expect(absoluteUrlSchema.safeParse('\\').success).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const absoluteUrlSchema = z
|
||||
.string()
|
||||
.url()
|
||||
.or(
|
||||
z
|
||||
.string()
|
||||
.transform((value) => `https://${value}`)
|
||||
.pipe(z.string().url()),
|
||||
);
|
||||
Reference in New Issue
Block a user