-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): add matchPath function
SL-72
- Loading branch information
Chris Miaskowski
committed
Oct 17, 2018
1 parent
2dc3f8b
commit 7292957
Showing
6 changed files
with
81 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,48 @@ | ||
import { IHttpOperation } from "@stoplight/types/http"; | ||
function fragmentarize(path: string): string[] { | ||
return path.split('/').slice(1); | ||
} | ||
|
||
function getTemplateParamName(pathFragment: string) { | ||
const match = pathFragment.match(/^{(.*)}$/); | ||
return match && match[1]; | ||
} | ||
|
||
/** | ||
* @returns `true` if matched concrete, `false` if not matched, `path param values` if matched templated | ||
*/ | ||
export function matchPath(requestPath: string, operation: { path: string }): boolean | { name: string, value: string }[] { | ||
const operationPath = operation.path; | ||
if (!requestPath.startsWith('/')) { | ||
throw new Error('The request path must start with a slash.'); | ||
} | ||
if (!operationPath.startsWith('/')) { | ||
throw new Error('The operation path must start with a slash.'); | ||
} | ||
const operationPathFragments = fragmentarize(operationPath); | ||
const requestPathFragments = fragmentarize(requestPath); | ||
|
||
if (operationPathFragments.length < requestPathFragments.length | ||
|| operationPathFragments.length > requestPathFragments.length) { | ||
return false; | ||
} | ||
|
||
const params = []; | ||
while (requestPathFragments.length) { | ||
const requestPathFragment = requestPathFragments.shift(); | ||
const operationPathFragment = operationPathFragments.shift(); | ||
|
||
const paramName = getTemplateParamName(operationPathFragment as string); | ||
|
||
if (paramName === null && operationPathFragment !== requestPathFragment) { | ||
// if concrete fragment and fragments are different return false | ||
return false; | ||
} else if (paramName !== null) { | ||
params.push({ | ||
name: paramName as string, | ||
value: requestPathFragment as string, | ||
}); | ||
} | ||
} | ||
|
||
// returns true if matched concrete | ||
// returns false if not matched | ||
// returns path param values if matched templated | ||
export function matchPath(requestPath: string, operation: Partial<IHttpOperation>): boolean | [ { name: string, value: string } ] { | ||
throw new Error('not implemented'); | ||
return params; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,11 @@ | |
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-1.0.1.tgz#4a1c49c494f66e46efce4da30edcb03fd94617c8" | ||
integrity sha512-5eobPkXsxJDP3q3IUT+iucRwiKwnJlaIRQY7bA42p6O3IyV9URBPuzfuMQNlyrDj2tt6StNEuYo6ReQxWFCHXg== | ||
|
||
"@types/chance@^1.0.1": | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/@types/chance/-/chance-1.0.1.tgz#c10703020369602c40dd9428cc6e1437027116df" | ||
integrity sha512-jtV6Bv/j+xk4gcXeLlESwNc/m/I/dIZA0xrt29g0uKcjyPob8iisj/5z0ARE+Ldfx4MxjNFNECG0z++J7zJgqg== | ||
|
||
"@types/[email protected]": | ||
version "23.3.5" | ||
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.3.5.tgz#870a1434208b60603745bfd214fc3fc675142364" | ||
|
@@ -546,6 +551,11 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.3.0: | |
escape-string-regexp "^1.0.5" | ||
supports-color "^5.3.0" | ||
|
||
chance@^1.0.16: | ||
version "1.0.16" | ||
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.16.tgz#bd61912716b0010c3dca8e3948a960efcaa7bb1b" | ||
integrity sha512-2bgDHH5bVfAXH05SPtjqrsASzZ7h90yCuYT2z4mkYpxxYvJXiIydBFzVieVHZx7wLH1Ag2Azaaej2/zA1XUrNQ== | ||
|
||
chownr@^1.0.1: | ||
version "1.1.1" | ||
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.1.tgz#54726b8b8fff4df053c42187e801fb4412df1494" | ||
|