Migrate and unify URL tooling in twenty-shared. We now have: - isValidHostname which follows our own business rules - a zod schema that can be re-used in different context and leverages is isValidHostname - isValidUrl on top of the zod schema - a getAbsoluteURl and getHostname on top of the zod schema I have added a LOT of tests to cover all the cases I've found Also fixes: https://github.com/twentyhq/twenty/issues/10147
17 lines
378 B
TypeScript
17 lines
378 B
TypeScript
import { absoluteUrlSchema } from 'src/utils/url/absoluteUrlSchema';
|
|
|
|
export const getUrlHostnameOrThrow = (url: string): string => {
|
|
const result = absoluteUrlSchema.safeParse(url);
|
|
|
|
if (!result.success) {
|
|
throw new Error('Invalid URL');
|
|
}
|
|
|
|
try {
|
|
const url = new URL(result.data);
|
|
return url.hostname;
|
|
} catch {
|
|
throw new Error('Invalid URL');
|
|
}
|
|
};
|