-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use optional or required literal instead of optional flag
- Loading branch information
1 parent
4ed4ffb
commit 8feb706
Showing
20 changed files
with
146 additions
and
107 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { string, createRouting, segment } from "../../src"; | ||
|
||
describe("arg segment", () => { | ||
it("creates route with an arg segment", () => { | ||
const routes = createRouting({ | ||
product: segment`/product/${string("productId")}`, | ||
} as const); | ||
|
||
const route = routes.product({ productId: "id" }); | ||
|
||
expect(route).toEqual("/product/id"); | ||
}); | ||
|
||
it("creates route with an optional arg segment", () => { | ||
const routes = createRouting({ | ||
product: segment`/product/${string("productId", "optional")}`, | ||
} as const); | ||
|
||
const route = routes.product(); | ||
|
||
expect(route).toEqual("/product"); | ||
}); | ||
|
||
it("returns the correct path pattern when required", () => { | ||
const routes = createRouting({ | ||
product: segment`/product/${string("productId")}`, | ||
} as const); | ||
|
||
const pattern = routes.product.pattern; | ||
|
||
expect(pattern).toEqual("/product/:productId"); | ||
}); | ||
|
||
it("returns the correct path pattern when optional", () => { | ||
const routes = createRouting({ | ||
product: segment`/product/${string("productId", "optional")}`, | ||
} as const); | ||
|
||
const pattern = routes.product.pattern; | ||
|
||
expect(pattern).toEqual("/product/:productId?"); | ||
}); | ||
}); |
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,14 +1,19 @@ | ||
export default class PathParamDescription<TName extends string = string, TOptional extends boolean = true> { | ||
import { Optionality } from "./helpers"; | ||
|
||
export default class PathParamDescription< | ||
TName extends string = string, | ||
TOptionality extends Optionality = "optional", | ||
> { | ||
public readonly pattern: string; | ||
public readonly name: TName; | ||
public readonly optional: TOptional; | ||
public readonly optionality: TOptionality; | ||
|
||
constructor({ name, optional, pattern }: { name: TName; optional: TOptional; pattern?: string }) { | ||
constructor({ name, optionality, pattern }: { name: TName; optionality: TOptionality; pattern?: string }) { | ||
const patternPart = pattern ? `(${pattern})` : ""; | ||
const requirementPart = optional ? "?" : ""; | ||
const requirementPart = optionality === "optional" ? "?" : ""; | ||
|
||
this.name = name; | ||
this.optional = optional ?? (false as any); | ||
this.optionality = optionality; | ||
this.pattern = `:${name}${patternPart}${requirementPart}`; | ||
} | ||
} |
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,5 +1,7 @@ | ||
export default class QueryParamDescription<TReturnType = string, TOptional extends boolean = false> { | ||
import { Optionality } from "./helpers"; | ||
|
||
export default class QueryParamDescription<TReturnType = string, TOptionality extends Optionality = "required"> { | ||
private readonly _?: TReturnType; | ||
|
||
constructor(public readonly optional: TOptional) {} | ||
constructor(public readonly optionality: TOptionality) {} | ||
} |
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,5 +1,6 @@ | ||
import { Optionality } from "./helpers"; | ||
import PathParamDescription from "./PathParamDescription"; | ||
|
||
export default class SegmentPattern<TPathParamsDescription extends PathParamDescription<string, boolean>[]> { | ||
export default class SegmentPattern<TPathParamsDescription extends PathParamDescription<string, Optionality>[]> { | ||
constructor(public readonly pattern: string, public readonly params: TPathParamsDescription) {} | ||
} |
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,14 +1,15 @@ | ||
import { Optionality } from "../helpers"; | ||
import PathParamDescription from "../PathParamDescription"; | ||
|
||
export default function arg<TName extends string = string, TOptional extends boolean = false>( | ||
export default function arg<TName extends string = string, TOptionality extends Optionality = "required">( | ||
name: TName, | ||
{ | ||
pattern, | ||
optional = false as TOptional, | ||
optionality = "required" as TOptionality, | ||
}: { | ||
pattern?: string; | ||
optional?: TOptional; | ||
optionality?: TOptionality; | ||
} = {}, | ||
) { | ||
return new PathParamDescription({ name, optional, pattern }); | ||
return new PathParamDescription({ name, optionality, pattern }); | ||
} |
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,14 +1,22 @@ | ||
import { Optionality } from "../helpers"; | ||
import PathParamDescription from "../PathParamDescription"; | ||
|
||
export default function uuid<TName extends string = string, TOptional extends boolean = false>( | ||
export default function uuid<TName extends string = string, TOptionality extends Optionality = "required">( | ||
name: TName, | ||
{ | ||
optional = false as TOptional, | ||
}: { | ||
optional?: TOptional; | ||
} = {}, | ||
optsOrOptionality?: | ||
| { | ||
optionality?: TOptionality; | ||
} | ||
| TOptionality, | ||
) { | ||
let optionality: TOptionality; | ||
if (typeof optsOrOptionality === "string") { | ||
optionality = optsOrOptionality; | ||
} else { | ||
optionality = optsOrOptionality?.optionality ?? ("required" as TOptionality); | ||
} | ||
|
||
const number = "[0-9]+"; | ||
|
||
return new PathParamDescription({ name, optional, pattern: number }); | ||
return new PathParamDescription({ name, optionality, pattern: number }); | ||
} |
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,12 +1,20 @@ | ||
import { Optionality } from "../helpers"; | ||
import PathParamDescription from "../PathParamDescription"; | ||
|
||
export default function string<TName extends string = string, TOptional extends boolean = false>( | ||
export default function string<TName extends string = string, TOptionality extends Optionality = "required">( | ||
name: TName, | ||
{ | ||
optional = false as TOptional, | ||
}: { | ||
optional?: TOptional; | ||
} = {}, | ||
optsOrOptionality?: | ||
| { | ||
optionality?: TOptionality; | ||
} | ||
| TOptionality, | ||
) { | ||
return new PathParamDescription({ name, optional }); | ||
let optionality: TOptionality; | ||
if (typeof optsOrOptionality === "string") { | ||
optionality = optsOrOptionality; | ||
} else { | ||
optionality = optsOrOptionality?.optionality ?? ("required" as TOptionality); | ||
} | ||
|
||
return new PathParamDescription({ name, optionality }); | ||
} |
Oops, something went wrong.