({
harRequest,
responseBody,
store,
options,
}: Params)
| 21 | }; |
| 22 | |
| 23 | export default function upsert({ |
| 24 | harRequest, |
| 25 | responseBody, |
| 26 | store, |
| 27 | options, |
| 28 | }: Params): Returns | null { |
| 29 | const url = new URL(harRequest.request.url); |
| 30 | const { host } = url; |
| 31 | const pathname = decodeUriComponent(url.pathname); |
| 32 | const parts = pathToArray(pathname); |
| 33 | if (parts.length === 0) return null; |
| 34 | // Set the host on first visit |
| 35 | if (!store[host]) { |
| 36 | store[host] = createRouter(); |
| 37 | } |
| 38 | // Create or update the leaf |
| 39 | const insertLeaf = createLeaf({ harRequest, responseBody, options }); |
| 40 | const router = store[host]; |
| 41 | const matchedRoute = router.lookup(pathname); |
| 42 | const nextLeaf = matchedRoute |
| 43 | ? mergeLeaves(matchedRoute.data, insertLeaf) |
| 44 | : insertLeaf; |
| 45 | const parameterisedPath = matchedRoute?.data.pathname || pathname; |
| 46 | nextLeaf.pathname = parameterisedPath; |
| 47 | router.insert(parameterisedPath, { data: nextLeaf }); |
| 48 | return { |
| 49 | insertedPath: parameterisedPath, |
| 50 | insertedLeaf: nextLeaf, |
| 51 | insertedHost: host, |
| 52 | }; |
| 53 | } |
no test coverage detected