diff --git a/.eslintrc.json b/.eslintrc.json index aff8587..15694bd 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,7 +15,6 @@ "browser": false }, "rules": { - "unicorn/prefer-module": 0, "unicorn/prefer-node-protocol": 0 } } diff --git a/CHANGELOG.md b/CHANGELOG.md index ae79fe7..d8cd8d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ## [v3 (unreleased)][Unreleased] + +### ⚠️ BREAKING CHANGES + +- **new API endpoint path**: ~~`/api/toot`~~ → `/api/share` +- API endpoint **is now ESM-based** instead of CommonJS + +### Added + +- when developing, the API endpoint can now be tested locally thanks to + [`vite-plugin-node`](https://github.com/axe-me/vite-plugin-node) + ### Changed - **new project name**: Share₂Fedi (see #1) diff --git a/api/toot.js b/api/share.js similarity index 52% rename from api/toot.js rename to api/share.js index 1f26b43..2b075df 100644 --- a/api/toot.js +++ b/api/share.js @@ -18,24 +18,28 @@ SPDX-License-Identifier: AGPL-3.0-or-later */ -const http = require("http"); +import http from "http"; -http - .createServer(async (request, response) => { - const buffers = []; - for await (const chunk of request) { - buffers.push(chunk); - } - const data = Buffer.concat(buffers).toString(); - const searchParameters = new URLSearchParams(data); +const requestListener = async (request, response) => { + const buffers = []; + for await (const chunk of request) { + buffers.push(chunk); + } + const data = Buffer.concat(buffers).toString(); + const searchParameters = new URLSearchParams(data); - const text = searchParameters.get("text") || ""; - const instanceURL = - searchParameters.get("instance") || "https://mastodon.social/"; + const text = searchParameters.get("text") || ""; + const instanceURL = + searchParameters.get("instance") || "https://mastodon.social/"; - const finalURL = new URL("share", instanceURL); - finalURL.search = new URLSearchParams({ text }).toString(); + const finalURL = new URL("share", instanceURL); + finalURL.search = new URLSearchParams({ text }).toString(); - response.writeHead(303, { Location: finalURL.toString() }).end(); - }) - .listen(8000); + response.writeHead(303, { Location: finalURL.toString() }).end(); +}; + +if (!import.meta.env || import.meta.env.PROD) { + http.createServer(requestListener).listen(8080); +} + +export const viteNodeApp = requestListener; diff --git a/index.html b/index.html index f8ef5b9..427dee7 100644 --- a/index.html +++ b/index.html @@ -52,7 +52,7 @@

-
+