Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 1x 1x 6x 6x 6x 6x 5x 5x 1x 5x | import { Hono } from "hono";
import type { Dal } from "../../dal";
import { logger } from "../../lib/logger";
import { success } from "../../lib/response";
/**
* Resolving an RRM short link for `go.interioring.com/l/:slug` (§1.6).
*
* Called by `apps/go`, which has no database and never will. It sits under the
* internal router, so the `X-Internal-API-Key` guard and `contextMiddleware`
* have already run — nothing here re-applies either.
*
* The point of the whole thing is attribution: one slug per posting place, so
* a sign-up can be traced back to the WhatsApp group or the 1:1 chat it came
* from instead of to "the link worked". The Go worker turns the destination
* into a 302 and the partner page's existing `?src=` capture does the rest.
*/
type Env = {
Bindings: CloudflareBindings;
Variables: { dal: Dal };
};
const rrmLinks = new Hono<Env>();
// GET /api/internal/rrm/links/:slug
//
// 404 for unknown AND for deactivated — the DAL deliberately answers both the
// same way, because the caller's response to either is the same: send them to
// the partner page rather than an error. Someone following a retired link from
// a group post six weeks ago is still a prospect.
rrmLinks.get("/links/:slug", async (c) => {
const dal = c.get("dal");
const slug = c.req.param("slug").trim().toLowerCase();
const destination = await dal.rrmLinks.resolve(slug);
if (!destination) return c.json({ success: false, error: "not_found" }, 404);
// Under `waitUntil` and never awaited: the click count is a reporting
// number, and a redirect must not wait on it — nor fail with it. Reading
// `c.executionCtx` throws when unset (some test harnesses), not just
// calling it, so both live inside the same try — same guard as
// rrm-submissions.routes.ts.
try {
c.executionCtx.waitUntil(
dal.rrmLinks
.recordClick(slug)
.catch((err) =>
logger.error("[rrm-links] failed to record click", err),
),
);
} catch {
/* executionCtx unavailable in tests */
}
return success(c, { destination });
});
export default rrmLinks;
|