24 lines
732 B
TypeScript
24 lines
732 B
TypeScript
import { BadRequestException } from '@nestjs/common';
|
|
|
|
/** مسیر داخلی (`/…`) یا URL با http/https */
|
|
export function normalizeBannerLink(value?: string | null): string | null {
|
|
if (value == null || value === '') return null;
|
|
const link = value.trim();
|
|
if (!link) return null;
|
|
if (link.startsWith('/')) {
|
|
if (!/^\/[^\s]*$/.test(link)) {
|
|
throw new BadRequestException('لینک بنر نامعتبر است');
|
|
}
|
|
return link;
|
|
}
|
|
try {
|
|
const url = new URL(link);
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
throw new Error('invalid protocol');
|
|
}
|
|
return link;
|
|
} catch {
|
|
throw new BadRequestException('لینک بنر نامعتبر است');
|
|
}
|
|
}
|