MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / processMultipleMatches

Function processMultipleMatches

pages/api/mock/[...slug].ts:46–74  ·  view source on GitHub ↗
(
  matches: Action[],
  slug: string[],
)

Source from the content-addressed store, hash-verified

44}
45
46export function processMultipleMatches(
47 matches: Action[],
48 slug: string[],
49): Action[] {
50 // slug = 'api/v1/Customers/location'. matches = /api/v1/Customers/location and /api/v1/Customers/{id}.
51 const matchEnd = matches.filter((match) => {
52 const split = match.path?.split("/").filter((arr) => arr !== "") ?? [];
53 return split[split.length - 1] === slug[slug.length - 1];
54 });
55 if (matchEnd.length === 1) return matchEnd;
56 const paramMatcher = (slugSegment: string, pathSegment: string) =>
57 (pathSegment.includes("{") && pathSegment.includes("}")) ||
58 slugSegment === pathSegment;
59
60 // slug = 'api/v2/Coordinators/1234'. matches = "/api/v2/Coordinators/{id}". "/api/v2/Coordinators/location"
61 const matchWithParams = matches.filter((match) => {
62 const split = match.path?.split("/").filter((arr) => arr !== "") ?? [];
63 return slug.every((slugSegment, i) => paramMatcher(slugSegment, split[i]));
64 });
65
66 if (matchWithParams.length === 1) return matchWithParams;
67
68 console.error(
69 `Failed to match slug: "${slug}" to single action. Candidate paths: ${matches.map(
70 (match) => match.path,
71 )}. Returning first match.`,
72 );
73 return [matches[0]];
74}
75
76export function getMatchingAction(
77 org_id: number,

Callers 2

slug.test.tsFile · 0.90
getMatchingActionFunction · 0.85

Calls 1

paramMatcherFunction · 0.85

Tested by

no test coverage detected