@settlemint/sdk-utils
Version:
Shared utilities and helper functions for SettleMint SDK modules
28 lines (26 loc) • 951 B
JavaScript
//#region src/url.ts
/**
* Extracts the base URL before a specific segment in a URL.
*
* @param baseUrl - The base URL to extract the path from
* @param pathSegment - The path segment to start from
* @returns The base URL before the specified segment
* @example
* ```typescript
* import { extractBaseUrlBeforeSegment } from "@settlemint/sdk-utils/url";
*
* const baseUrl = extractBaseUrlBeforeSegment("https://example.com/api/v1/subgraphs/name/my-subgraph", "/subgraphs");
* // Returns: "https://example.com/api/v1"
* ```
*/
function extractBaseUrlBeforeSegment(baseUrl, pathSegment) {
const url = new URL(baseUrl);
if (pathSegment.trim() === "") {
return url.toString();
}
const segmentIndex = url.pathname.indexOf(pathSegment);
return url.origin + (segmentIndex >= 0 ? url.pathname.substring(0, segmentIndex) : url.pathname);
}
//#endregion
exports.extractBaseUrlBeforeSegment = extractBaseUrlBeforeSegment;
//# sourceMappingURL=url.cjs.map