Skip to content

Commit

Permalink
allow dots in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Nov 20, 2023
1 parent 7fb39f4 commit 16903e5
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 33 deletions.
10 changes: 0 additions & 10 deletions packages/primate/src/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@
"fix": "use only latin letters and decimal digits in path parameters",
"level": "Error"
},
"InvalidRouteName": {
"message": "invalid route name {0}",
"fix": "do not use dots in route names",
"level": "Error"
},
"InvalidTypeExport": {
"message": "invalid type export at {0}",
"fix": "export object with a `base` string and a `validate` function",
Expand Down Expand Up @@ -96,11 +91,6 @@
"fix": "add configuration options or remove file",
"level": "Warn"
},
"NoFileForPath": {
"message": "no file for {0}",
"fix": "create a file at {1}{0}",
"level": "Info"
},
"NoHandlerForExtension": {
"message": "no handler for {0} extension",
"fix": "add handler module for {0} files or remove {1}",
Expand Down
11 changes: 3 additions & 8 deletions packages/primate/src/hooks/handle.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Response, Status, MediaType } from "rcompat/http";
import { cascade, tryreturn } from "rcompat/async";
import { respond } from "./respond/exports.js";
import { invalid } from "./route.js";
import { error as clientError } from "../handlers/exports.js";
import _errors from "../errors.js";
const { NoFileForPath } = _errors;

const guard_error = Symbol("guard_error");
const guard = (app, guards) => async (request, next) => {
Expand Down Expand Up @@ -40,16 +37,14 @@ const get_layouts = async (layouts, request) => {

export default app => {
const { config: { http: { static: { root } }, location } } = app;
const route = request => app.route(request);

const as_route = async request => {
const { pathname } = request.url;
// if NoFileForPath is thrown, this will remain undefined
// if tryreturn throws, this will default
let error_handler = app.error.default;

return tryreturn(async _ => {
const { path, guards, errors, layouts, handler } = invalid(pathname)
? NoFileForPath.throw(pathname, location.static)
: await app.route(request);
const { path, guards, errors, layouts, handler } = await route(request);
error_handler = errors?.at(-1);

const pathed = { ...request, path };
Expand Down
3 changes: 0 additions & 3 deletions packages/primate/src/hooks/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import validate from "../validate.js";
// insensitive-case equal
const ieq = (left, right) => left.toLowerCase() === right.toLowerCase();

/* routes may not contain dots */
export const invalid = route => /\./u.test(route);

const deroot = pathname => pathname.endsWith("/") && pathname !== "/"
? pathname.slice(0, -1) : pathname;

Expand Down
3 changes: 0 additions & 3 deletions packages/primate/src/loaders/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { from } from "rcompat/object";
import { default as fs, doubled } from "./common.js";
import * as get from "./routes/exports.js";
import errors from "../errors.js";
import { invalid } from "../hooks/route.js";

const make = path => {
const double = doubled(path.split("/")
Expand All @@ -18,8 +17,6 @@ const make = path => {
return `(?<${param}>[^/]{1,}?)`;
}).orelse(_ => errors.InvalidPathParameter.throw(named, path)));

invalid(route) && errors.InvalidRouteName.throw(path);

return new RegExp(`^/${route}$`, "u");
};

Expand Down
9 changes: 0 additions & 9 deletions packages/primate/src/loaders/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,4 @@ export default test => {
const err2 = mark("invalid path parameter {0} in route {1}", "", path2);
assert(() => routes([[path2, { get }]])).throws(err2);
});
test.case("errors.InvalidRouteName", async assert => {
const post = ["po.st", { get }];
const throws = mark("invalid route name {0}", "po.st");
try {
await routes([post]);
} catch (error) {
assert(error.message).equals(throws);
}
});
};

0 comments on commit 16903e5

Please sign in to comment.