import type { ParsedQuery } from 'query-string' ; import type { UrlObject } from 'url' ; import { format as formatUrl , parse as parseUrl } from 'url' ; export interface URLMeaningfulParts { auth : string | null ; hash : string | null ; hostname : string | null ; pathname : string | null ; protocol : string | null ; slashes : boolean | null ; port : string | null ; query : ParsedQuery | { } ; } param url param urlModifier returns export function modifyUrl ( url , urlModifier ) { const parsed = parseUrl ( url , true ) as URLMeaningfulParts ; const meaningfulParts = { auth : parsed . auth , hash : parsed . hash , hostname : parsed . hostname , pathname : parsed . pathname , port : parsed . port , protocol : parsed . protocol , query : parsed . query || { } , slashes : parsed . slashes , } ; const modifiedParts = urlModifier ( meaningfulParts ) || meaningfulParts ; return formatUrl ( { auth : modifiedParts . auth , hash : modifiedParts . hash , hostname : modifiedParts . hostname , pathname : modifiedParts . pathname , port : modifiedParts . port , protocol : modifiedParts . protocol , query : modifiedParts . query , slashes : modifiedParts . slashes , } as UrlObject ) ; }