MCPcopy
hub / github.com/GitbookIO/gitbook / parseHostAndPath

Function parseHostAndPath

packages/react-openapi/src/code-samples.ts:206–226  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

204}
205
206export function parseHostAndPath(url: string) {
207 try {
208 const urlObj = new URL(url);
209 const path = urlObj.pathname || '/';
210 return { host: urlObj.host, path };
211 } catch (_e) {
212 // If the URL was invalid do our best to parse the URL.
213 // Check for the protocol part and pull it off to grab the host
214 const splitted = url.split('//');
215 const fullUrl = splitted[1] ? splitted[1] : url;
216
217 // separate paths from the first element (host)
218 const parts = fullUrl.split('/');
219 // pull off the host (mutates)
220 const host = parts.shift();
221 // add a leading slash and join the paths again
222 const path = `/${parts.join('/')}`;
223
224 return { host, path };
225 }
226}
227
228// Body Generators
229const BodyGenerators = {

Callers 1

generateCodeSamplesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected