( matches: Action[], slug: string[], )
| 44 | } |
| 45 | |
| 46 | export 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 | |
| 76 | export function getMatchingAction( |
| 77 | org_id: number, |
no test coverage detected