Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/api-client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { createApiClient } = require("../web/api-client");

test("API client surfaces a backend error message", async () => {
const api = createApiClient(async () => ({
ok: false,
status: 400,
json: async () => ({ error: "TikTok login is already in progress" }),
}));

await assert.rejects(
api.post("/api/tiktok/login"),
/TikTok login is already in progress/
);
});

test("API client falls back to the HTTP status for non-JSON errors", async () => {
const api = createApiClient(async () => ({
ok: false,
status: 502,
json: async () => {
throw new SyntaxError("Unexpected token");
},
}));

await assert.rejects(api.get("/api/status"), /API Error: 502/);
});
35 changes: 35 additions & 0 deletions web/api-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(function exposeApiClient(root, factory) {
const createApiClient = factory();

if (typeof module === "object" && module.exports) {
module.exports = { createApiClient };
} else {
root.createApiClient = createApiClient;
}
})(typeof globalThis !== "undefined" ? globalThis : this, function apiClientFactory() {
return function createApiClient(fetchImpl = globalThis.fetch) {
async function parse(response) {
const data = await response.json().catch(() => null);
if (!response.ok) {
throw new Error(data?.error || `API Error: ${response.status}`);
}
return data;
}

return {
async get(endpoint) {
return parse(await fetchImpl(endpoint));
},
async post(endpoint, body) {
const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
};
if (body !== undefined) {
options.body = JSON.stringify(body);
}
return parse(await fetchImpl(endpoint, options));
},
};
};
});
20 changes: 1 addition & 19 deletions web/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
const API = {
async get(endpoint) {
const res = await fetch(endpoint);
if (!res.ok) throw new Error(`API Error: ${res.status}`);
return res.json();
},
async post(endpoint, body) {
const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
};
if (body !== undefined) {
options.body = JSON.stringify(body);
}
const res = await fetch(endpoint, options);
if (!res.ok) throw new Error(`API Error: ${res.status}`);
return res.json();
},
};
const API = createApiClient();

function escapeHtml(value) {
return String(value ?? "")
Expand Down
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ <h2 class="card-title">Live Logs</h2>
</section>
</main>
</div>
<script src="api-client.js"></script>
<script src="app.js"></script>
</body>

Expand Down