From 03f49e40566146cb61d78fc51d97d50b0181fb35 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 16 Mar 2023 12:16:21 +0100 Subject: [PATCH] BREAKING: Refactor /api endpoint The endpoint was renamed to `/api/share` and refactored to use ESM instead of CommonJS --- .eslintrc.json | 1 - CHANGELOG.md | 11 +++++++++++ api/{toot.js => share.js} | 38 +++++++++++++++++++++----------------- index.html | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) rename api/{toot.js => share.js} (52%) 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 @@

-
+