MCPcopy
hub / github.com/AndrewWalsh/openapi-devtools / upsert

Function upsert

src/lib/store-helpers/upsert.ts:23–53  ·  view source on GitHub ↗
({
  harRequest,
  responseBody,
  store,
  options,
}: Params)

Source from the content-addressed store, hash-verified

21};
22
23export 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}

Callers 1

insertMethod · 0.90

Calls 4

pathToArrayFunction · 0.90
mergeLeavesFunction · 0.90
createLeafFunction · 0.85
insertMethod · 0.80

Tested by

no test coverage detected