bitip/src/clients/node/src/utils/url-normalizer.ts
Tudor Stanciu dbb821fe92 Merged PR 110: feat: Implement Node BitipClient for GeoIP service integration
feat: Implement BitipClient for GeoIP service integration

- Add BitipClient class for interacting with the Bitip GeoIP Service.
- Implement methods for health check, version info, IP location lookup, detailed IP location, and batch IP lookup.
- Introduce validation for IP addresses with IpValidator utility.
- Normalize URLs with UrlNormalizer utility.
- Create constants for API keys and routes.
- Add TypeScript types for client options, responses, and errors.
- Set up ESLint and Prettier configurations for code quality.
- Add unit tests for BitipClient and IpValidator.
- Configure TypeScript and build settings with tsup.
- Set up Vitest for testing framework and coverage reporting.
2025-10-12 11:54:44 +00:00

52 lines
1.4 KiB
TypeScript

// Copyright (c) 2025 Tudor Stanciu
/**
* Normalizes URLs for API client usage
*/
export class UrlNormalizer {
/**
* Ensures a URL ends with a trailing slash and appends a suffix if provided
* @param baseUrl - Base URL to normalize
* @param suffix - Optional suffix to append (default: '/api')
* @returns Normalized URL with trailing slash
*/
static ensureTrailingSlash(baseUrl: string, suffix: string = '/api'): string {
if (!baseUrl) {
throw new Error('Base URL cannot be null or empty');
}
let normalized = baseUrl.trim();
// Remove trailing slash if present
if (normalized.endsWith('/')) {
normalized = normalized.slice(0, -1);
}
// Add suffix if not already present
if (suffix && !normalized.endsWith(suffix)) {
// Remove leading slash from suffix if present
const cleanSuffix = suffix.startsWith('/') ? suffix : `/${suffix}`;
normalized += cleanSuffix;
}
// Ensure trailing slash
if (!normalized.endsWith('/')) {
normalized += '/';
}
return normalized;
}
/**
* Combines base URL with path segments
* @param baseUrl - Base URL
* @param path - Path to append
* @returns Combined URL
*/
static combine(baseUrl: string, path: string): string {
const normalizedBase = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
const normalizedPath = path.startsWith('/') ? path.slice(1) : path;
return normalizedBase + normalizedPath;
}
}