Skip to content

Commit

Permalink
fix: issue with validating path
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Apr 10, 2024
1 parent 460890f commit 36d5efd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
26 changes: 13 additions & 13 deletions docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ with a status code for the redirection.

#### Type declaration

| Name | Type | Description |
| :-------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `from` | `string` | A string specifying the pattern a URL's path must match for this redirect to apply to it. The pattern must be a path (no host, query params, fragment, etc.) that follows `path-to-regexp`'s syntax: https://github.com/pillarjs/path-to-regexp#parameters Matching is case-insensitive and ignores trailing slashes. |
| `status?` | `number` | The status code to use for the redirection. Defaults to [RedirectUrlOptions#defaultStatus](modules.md#defaultstatus) if not specified. |
| `to` | `string` | A string specifying the URL to redirect to. The string may use parameters of the [ObjectRedirect#from](modules.md#from) path pattern according to `path-to-regexp`'s syntax: https://github.com/pillarjs/path-to-regexp#match The string may be a path or a full URL. For the former, the URL returned by [RedirectUrl](modules.md#redirecturl) will use the host of the matching input URL. In both cases, any query params or fragments present on an input URL matching the [ObjectRedirect#from](modules.md#from) pattern are carried over to the redirect URL. Trailing slashes are always stripped. Query params are always appended, even for duplicate query param names, while the fragments always overwrite. |
| Name | Type | Description |
| :-------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `from` | `string` | A string specifying the pattern a URL's path must match for this redirect to apply to it. The pattern must be a path (no host, query params, fragment, etc.) that follows `path-to-regexp`'s syntax: https://github.com/pillarjs/path-to-regexp#parameters Matching is case-insensitive and ignores trailing slashes. |
| `status?` | `number` | The status code to use for the redirection. Defaults to [RedirectUrlOptions#defaultStatus](modules.md#defaultstatus) if not specified. |
| `to` | `string` | A string specifying the URL to redirect to. The string may use parameters of the [ObjectRedirect#from](modules.md#from) path pattern according to `path-to-regexp`'s syntax: https://github.com/pillarjs/path-to-regexp#match The string may be a path or a full URL. For the former, the URL returned by [RedirectUrl](modules.md#redirecturl) will use the host of the matching input URL. In both cases, any query params or fragments present on an input URL matching the [ObjectRedirect#from](modules.md#from) pattern are carried over to the redirect URL. Trailing slashes are always stripped. Query params are always appended, even for duplicate query param names, while the fragments always overwrite. Although, note that servers don't receive fragments so that behavior only matters when using this package on the client. |

#### Defined in

[index.ts:214](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L214)
[index.ts:208](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L208)

---

Expand All @@ -53,7 +53,7 @@ See `ObjectRedirect` for more information.

#### Defined in

[index.ts:200](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L200)
[index.ts:194](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L194)

---

Expand Down Expand Up @@ -81,7 +81,7 @@ status code for the redirection, or null if no mapping exists for the input URL.

#### Defined in

[index.ts:296](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L296)
[index.ts:292](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L292)

---

Expand All @@ -104,7 +104,7 @@ All properties are optional.

#### Defined in

[index.ts:257](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L257)
[index.ts:253](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L253)

---

Expand All @@ -123,7 +123,7 @@ The result of computing a redirect for a URL.

#### Defined in

[index.ts:283](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L283)
[index.ts:279](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L279)

---

Expand All @@ -138,7 +138,7 @@ See `ObjectRedirect` for more information.

#### Defined in

[index.ts:208](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L208)
[index.ts:202](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L202)

## Functions

Expand Down Expand Up @@ -172,7 +172,7 @@ See [Redirect](modules.md#redirect) for how to specify redirects and

#### Defined in

[index.ts:106](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L106)
[index.ts:106](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L106)

---

Expand Down Expand Up @@ -228,4 +228,4 @@ See [Redirect](modules.md#redirect) for how to specify redirects and

#### Defined in

[index.ts:56](https://github.com/TomerAberbach/redirect-url/blob/15c0825b6681fc47ca26b6b9b7996b91f1caa403/src/index.ts#L56)
[index.ts:56](https://github.com/TomerAberbach/redirect-url/blob/460890ffa6c04c161f01e284f52b41e435fc4830/src/index.ts#L56)
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ const compileRedirect = (
}: ReadonlyDeep<RequiredDeep<RedirectUrlOptions>>,
): CompiledRedirect => {
const { from, to, status = defaultStatus } = normalizeRedirect(redirect)
if (!isPath(from)) {
throw new Error(`Bad from path: ${from}`)
}

const pathToParams = createPathToParamsFunction(getPath(from), { decode })
const pathToParams = createPathToParamsFunction(removeTrailingSlash(from), {
decode,
})
const paramsToPath = createParamsToPathFunction(getPath(to), { encode })

return {
Expand Down Expand Up @@ -180,13 +179,8 @@ const normalizeRedirect = (
return { from, to }
}

const isPath = (url: string): boolean => createUrl(url).pathname === url

const getPath = (url: string | URL): string =>
removeTrailingSlash(createUrl(url).pathname)

const createUrl = (url: string | URL): URL =>
new URL(url, `https://tomeraberba.ch`)
removeTrailingSlash(new URL(url, `https://tomeraberba.ch`).pathname)

const removeTrailingSlash = (url: string): string =>
url.endsWith(`/`) ? url.slice(0, -1) : url
Expand Down Expand Up @@ -238,6 +232,8 @@ export type ObjectRedirect = {
*
* Trailing slashes are always stripped. Query params are always appended,
* even for duplicate query param names, while the fragments always overwrite.
* Although, note that servers don't receive fragments so that behavior only
* matters when using this package on the client.
*/
to: string

Expand Down
9 changes: 0 additions & 9 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@
import { createRedirectUrl, parseRedirectUrl } from '../src/index.ts'
import type { RedirectUrlResult } from '../src/index.ts'

test.each([`https://tomeraberba.ch`, `/a?`, `/a?x=2`, `/a#`, `/a#x`])(
`createRedirectUrl - bad from path %p`,
from => {
expect(() => createRedirectUrl([[from, `/`]])).toThrow(
new Error(`Bad from path: ${from}`),
)
},
)

test.each([
[`https://tomeraberba.ch`, [`https://tomeraberba.ch/0`, 301]],
[`https://tomeraberba.ch/`, [`https://tomeraberba.ch/0`, 301]],
Expand Down

0 comments on commit 36d5efd

Please sign in to comment.