Work

Guest Comments API

Role: Author

Duration: A few days

Tools Used/Explored: Express.js, TypeScript, GitHub REST API

Repository: Link

Stargazers on GitHub ⭐:


A sim­ple, self-hosted Express.js API writ­ten in TypeScript for guest com­ments on my sta­tic site. Yes, this very one!

Idealogy

Comments should be a part of my sta­tic site repos­i­tory and han­dled at the sta­tic site level. This is most per­for­mant and keeps data in one place. There is no API end­point to fetch com­ments as the app does not store any data by it­self, for ex­am­ple, in a data­base.

All op­er­a­tions (well, just the three) in­ter­act with my GitHub repos­i­tory. Currently only GitHub is sup­ported but I may be open to adding sup­port for Gitea or Codeberg should I switch to such op­tions in the fu­ture.

Environment

The fol­low­ing en­vi­ron­ment vari­ables are nec­es­sary:

  • NODE_ENV (optional) - de­faults to production
  • SENDGRID_API_KEY - SendGrid API key used for send­ing no­ti­fi­ca­tion emails
  • GITHUB_PAT - GitHub per­sonal ac­cess to­ken

API Endpoints

v1

POST /api/v1/comment/new

Accepts URL en­coded re­quests. Redirects to ${origin}/thank-you/ or ${origin}/error. If the ori­gin is not avail­able, it sim­ply re­sponds with a 200/500 HTTP re­sponse.

Form en­coded data re­quired
  • name

  • email

  • site

  • comment

  • age (honeypot)

  • fallback-referer: Some browsers do not add ref­erer head­ers to form sub­mis­sion re­quests. We rely on this as a fall­back. Requires JavaScript client-side.

    const target = document.querySelector(
      "form > input[name='fallback-referer']"
    );
    if (target) target.setAttribute("value", window.location.href);
    

GET /api/v1/comment/moderate

Accepts URL en­coded re­quests. Responds with a plain-text mes­sage upon suc­cess.

Query pa­ra­me­ters
  • comment_id
  • action: approve or delete