Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+7
-33
@@ -1,33 +1,13 @@
|
||||
/**
|
||||
* The REST half of the API client, beside graphql.js: a thin axios wrapper for
|
||||
* the Go backend's JSON endpoints under /api (file uploads, admin tooling,
|
||||
* OAuth starts — everything backend/README.md lists as "not GraphQL").
|
||||
*
|
||||
* It does two things a bare axios call does not:
|
||||
* - prefixes `/api`, so callers name the route (`api.get("/radio/songs")`);
|
||||
* - turns a failure into an ApiError whose `message` is the backend's
|
||||
* `{"error": "..."}` body when there is one. Every handler reports errors
|
||||
* in that shape, and each view used to dig it out of `err.response` itself.
|
||||
*
|
||||
* Resolves to the parsed response body, not the axios response.
|
||||
*
|
||||
* Auth is the same HTTP-only cookies graphql.js relies on; nothing here handles
|
||||
* tokens.
|
||||
*
|
||||
* NOT for: the Python service's NDJSON streams (views/hotels/hotelsStream.js
|
||||
* reads those incrementally with fetch) or the `/radio/stream` HEAD probe —
|
||||
* neither is under /api nor answers with this error shape.
|
||||
* REST client for /api: prefixes the path, resolves to the body, and turns
|
||||
* failures into an ApiError carrying the backend's `error` message. Not for
|
||||
* the NDJSON streams or the radio probe.
|
||||
*/
|
||||
|
||||
import axios from "axios";
|
||||
|
||||
export class ApiError extends Error {
|
||||
/**
|
||||
* @param {string} message
|
||||
* @param {number|undefined} status HTTP status; undefined when no response
|
||||
* arrived at all (network down, backend restarting)
|
||||
* @param {*} data the parsed error body, if any
|
||||
*/
|
||||
/** `status` is undefined when no response arrived at all. */
|
||||
constructor(message, status, data) {
|
||||
super(message);
|
||||
this.name = "ApiError";
|
||||
@@ -37,15 +17,9 @@ export class ApiError extends Error {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} method
|
||||
* @param {string} path route below /api, with its leading slash
|
||||
* @param {object} [options]
|
||||
* @param {*} [options.body] JSON-serialisable body, or a FormData for
|
||||
* multipart uploads (axios sets the boundary itself)
|
||||
* @param {string} [options.fallback] message to use when the backend sent no
|
||||
* `error` string — callers pass the sentence their UI shows
|
||||
* Any other option (`headers`, `params`, `onUploadProgress`, `signal`, …) is
|
||||
* handed to axios unchanged.
|
||||
* @param {string} path below /api, with its leading slash
|
||||
* @param {object} [options] `body` (JSON or FormData), `fallback` message
|
||||
* when the backend sent none; anything else goes to axios.
|
||||
*/
|
||||
async function request(method, path, { body, fallback, ...config } = {}) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user