BREAKING: Refactor /api endpoint

The endpoint was renamed to `/api/share` and refactored to use ESM instead of CommonJS
This commit is contained in:
Nikita Karamov 2023-03-16 12:16:21 +01:00
parent aeda860af6
commit 03f49e4056
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD
4 changed files with 33 additions and 19 deletions

View file

@ -15,7 +15,6 @@
"browser": false
},
"rules": {
"unicorn/prefer-module": 0,
"unicorn/prefer-node-protocol": 0
}
}

View file

@ -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)

View file

@ -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;

View file

@ -52,7 +52,7 @@
</p>
</header>
<main>
<form id="js-s2f-form" action="/api/toot" method="POST">
<form id="js-s2f-form" action="/api/share" method="POST">
<section>
<label for="text">Post text</label>
<textarea