Skip to content

Commit

Permalink
feat: add option for toggling automatic number conversion behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
justinawrey committed Sep 5, 2022
1 parent 154ecf2 commit 1f12358
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions cli/fsrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (!import.meta.main) {
const defaultOptions: Required<RouterOptions> = {
debug: false,
bootMessage: true,
convertToNumber: true,
};

const args = flags.parse(Deno.args, {
Expand Down
19 changes: 16 additions & 3 deletions core/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export * from "./handler.ts";
// Given a map of routes to their respective handlers, returns a single
// handler that correctly forwards requests to the right handler.
// If a route is hit that doesn't exist, the returned handler will 404.
function handleRoutes(routes: Route[]): http.Handler {
function handleRoutes(routes: Route[], convertToNumber: boolean): http.Handler {
// Split routes into ones that are exact (don't have slugs) and ones that aren't
const exactRoutes = routes.filter((route) => !route.hasSlugs);
const slugRoutes = Route.sort(routes.filter((route) => route.hasSlugs));
Expand Down Expand Up @@ -44,7 +44,7 @@ function handleRoutes(routes: Route[]): http.Handler {

// Otherwise, try matching slug routes
for (const slugRoute of slugRoutes) {
const matches = slugRoute.matches(urlPath);
const matches = slugRoute.matches(urlPath, convertToNumber);
if (matches) {
log.debug(
`Url ${urlPath} matched file ${
Expand Down Expand Up @@ -109,6 +109,18 @@ export interface RouterOptions {
* Defaults to false.
*/
debug?: boolean;

/**
* Whether or not slugs of type :number should be automatically converted to numbers
* in the matches object.
*
* For example, given the slug [id:number]:
* - if convertToNumber === true, then (typeof slugs.id) === 'number'
* - if convertToNumber === false, then (typeof slugs.id) === 'string'
*
* Defaults to true.
*/
convertToNumber?: boolean;
}

/**
Expand Down Expand Up @@ -164,6 +176,7 @@ export async function fsRouter(
{
debug = false,
bootMessage = true,
convertToNumber = true,
}: RouterOptions = {},
): Promise<http.Handler> {
await setupLogger(debug);
Expand All @@ -181,5 +194,5 @@ export async function fsRouter(
_bootMessage(routes, rootDir);
}

return handleRoutes(routes);
return handleRoutes(routes, convertToNumber);
}
9 changes: 5 additions & 4 deletions core/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ export class Route {
);
}

matches(urlPath: string): Matches | null {
matches(urlPath: string, convertToNumber: boolean): Matches | null {
const matches = this.regEx.exec(urlPath);
if (!matches) {
return null;
}

const matchObj: Matches = {};
for (const [index, match] of matches.slice(1).entries()) {
matchObj[this.slugs[index].raw] = this.slugs[index].type === "number"
? parseInt(match)
: match;
const shouldConvert = this.slugs[index].type === "number" &&
convertToNumber;

matchObj[this.slugs[index].raw] = shouldConvert ? parseInt(match) : match;
}

return matchObj;
Expand Down

0 comments on commit 1f12358

Please sign in to comment.