Skip to content

Commit

Permalink
Reformat with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pklaschka committed Sep 8, 2024
1 parent c63b515 commit 0475a06
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
22 changes: 11 additions & 11 deletions lib/http/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { optionalQueryNamespaceValidator } from "$http/validators/query/optional
export const apiRoutes = new Hono();

apiRoutes.get(
"/asn",
optionalQueryNamespaceValidator,
async (c) =>
c.json(
await generateASN(
createMetadata(c),
c.req.valid("query").namespace,
),
),
"/asn",
optionalQueryNamespaceValidator,
async (c) =>
c.json(
await generateASN(
createMetadata(c),
c.req.valid("query").namespace,
),
),
);

apiRoutes.get(
"/asn/:asn",
(c) => c.text("Not implemented yet", 501), // TODO: Implement this route
"/asn/:asn",
(c) => c.text("Not implemented yet", 501), // TODO: Implement this route
);
64 changes: 32 additions & 32 deletions lib/http/routes/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ import { getLookupURL } from "$http/mod.ts";
export const lookupRoutes = new Hono();

lookupRoutes.post(
"/lookup",
validator("form", (value, c) => {
const parsed = z.object({
asn: z.string({ coerce: true }).min(1).regex(/^\d+$/),
}).safeParse(value);

if (!parsed.success) {
return c.text("Invalid ASN. " + parsed.error.message, 400);
}

return parsed.data;
}),
(c) => {
const asn = CONFIG.ASN_PREFIX + c.req.valid("form").asn;
return c.redirect("/go/" + asn);
},
"/lookup",
validator("form", (value, c) => {
const parsed = z.object({
asn: z.string({ coerce: true }).min(1).regex(/^\d+$/),
}).safeParse(value);

if (!parsed.success) {
return c.text("Invalid ASN. " + parsed.error.message, 400);
}

return parsed.data;
}),
(c) => {
const asn = CONFIG.ASN_PREFIX + c.req.valid("form").asn;
return c.redirect("/go/" + asn);
},
);

lookupRoutes.get(
"/go/:asn",
validator("param", (value, c) => {
if (!value || !isValidASN(value.asn)) {
return c.text("Invalid ASN", 400);
}
return value;
}),
(c) => {
const asn = c.req.valid("param").asn;

if (!CONFIG.ASN_LOOKUP_URL) {
return c.text("ASN Lookup is disabled", 400);
}

return c.redirect(getLookupURL(asn));
},
"/go/:asn",
validator("param", (value, c) => {
if (!value || !isValidASN(value.asn)) {
return c.text("Invalid ASN", 400);
}
return value;
}),
(c) => {
const asn = c.req.valid("param").asn;

if (!CONFIG.ASN_LOOKUP_URL) {
return c.text("ASN Lookup is disabled", 400);
}

return c.redirect(getLookupURL(asn));
},
);
38 changes: 19 additions & 19 deletions lib/http/routes/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ export const uiRoutes = new Hono();
uiRoutes.use("*", jsxRenderer(Wrapper));

uiRoutes.get(
"/",
async (c) =>
await c.render(
<IndexPage config={CONFIG} />,
),
"/",
async (c) =>
await c.render(
<IndexPage config={CONFIG} />,
),
);

uiRoutes.get(
"/asn",
optionalQueryNamespaceValidator,
async (c) => {
const namespace = c.req.valid("query").namespace;

return await c.render(
<ASNPage
asn={await generateASN(
createMetadata(c),
namespace,
)}
/>,
);
},
"/asn",
optionalQueryNamespaceValidator,
async (c) => {
const namespace = c.req.valid("query").namespace;

return await c.render(
<ASNPage
asn={await generateASN(
createMetadata(c),
namespace,
)}
/>,
);
},
);
16 changes: 8 additions & 8 deletions lib/http/validators/param/asn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { validator } from "@hono/hono/validator";
import { isValidASN } from "$common/mod.ts";

export const paramASNValidator = validator("param", (value, c) => {
const asn = value.asn;
const asn = value.asn;

if (!asn) {
return c.text("No ASN provided", 400);
}
if (!asn) {
return c.text("No ASN provided", 400);
}

if (!isValidASN(asn)) {
return c.text("Invalid ASN provided", 400);
}
if (!isValidASN(asn)) {
return c.text("Invalid ASN provided", 400);
}

return { asn };
return { asn };
});
33 changes: 18 additions & 15 deletions lib/http/validators/query/optional-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import { validator } from "@hono/hono/validator";
import { z } from "@collinhacks/zod";
import { isManagedNamespace } from "$common/mod.ts";

export const optionalQueryNamespaceValidator = validator("query", (value, c) => {
const res = z.object({
namespace: z.number({ coerce: true }).optional().refine((v) => {
if (v === undefined) return true;
return isManagedNamespace(v);
}, {
message:
"Unregistered namespace. Please add it to the configuration's `ADDITIONAL_MANAGED_NAMESPACES` parameter.",
}),
}).safeParse(value);
export const optionalQueryNamespaceValidator = validator(
"query",
(value, c) => {
const res = z.object({
namespace: z.number({ coerce: true }).optional().refine((v) => {
if (v === undefined) return true;
return isManagedNamespace(v);
}, {
message:
"Unregistered namespace. Please add it to the configuration's `ADDITIONAL_MANAGED_NAMESPACES` parameter.",
}),
}).safeParse(value);

if (!res.success) {
return c.text(res.error.message, 400);
}
if (!res.success) {
return c.text(res.error.message, 400);
}

return res.data;
});
return res.data;
},
);

0 comments on commit 0475a06

Please sign in to comment.