fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 47 lines 889 B view raw
1const parseUrlRegExp = 2 /^(([^:/?#]+):)?((\/\/)?([^:/?#]*)(:?([^/?#]*)))?([^?#]*)(\?([^#]*))?(#(.*))?/; 3 4interface Url { 5 host: string; 6 path: string; 7 port: string; 8 protocol: string; 9} 10 11export function parseUrl(value: string): Url { 12 const errorResponse: Url = { 13 host: '', 14 path: '', 15 port: '', 16 protocol: '', 17 }; 18 19 parseUrlRegExp.lastIndex = 0; 20 const match = value.match(parseUrlRegExp); 21 22 if (!match) { 23 return errorResponse; 24 } 25 26 const host = match[5] || ''; 27 28 // value is a relative file system path 29 if (host === '.' || host === '..') { 30 return errorResponse; 31 } 32 33 const path = match[8] || ''; 34 const protocol = match[2] || ''; 35 36 // value is probably a Windows file system path 37 if (protocol.length === 1) { 38 return errorResponse; 39 } 40 41 return { 42 host, 43 path: path === '/' ? '' : path, 44 port: match[7] || '', 45 protocol, 46 }; 47}