mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 11:50:42 +08:00
In case of a `ResponseParseError` instead of comparing the versions of httpd and the web client strictly, we should make sure to check if httpd fulfills the web clients version requirement (to be the same major version) and else fallback to a more verbose error about the different versions.
176 lines
4.8 KiB
TypeScript
176 lines
4.8 KiB
TypeScript
// This module provides low-level capabilities to interact with a typed
|
|
// JSON HTTP API.
|
|
|
|
import type { ZodIssue, ZodType, TypeOf } from "zod";
|
|
|
|
import config from "virtual:config";
|
|
import { satisfies } from "compare-versions";
|
|
|
|
export interface BaseUrl {
|
|
hostname: string;
|
|
port: number;
|
|
scheme: string;
|
|
}
|
|
|
|
// Error that is thrown by `Fetcher` methods.
|
|
export class ResponseError extends Error {
|
|
public method: string;
|
|
public url: string;
|
|
public status: number;
|
|
public body: unknown;
|
|
|
|
public constructor(method: string, response: Response, body_: unknown) {
|
|
const body: unknown = body_;
|
|
if (
|
|
typeof body === "object" &&
|
|
body !== null &&
|
|
"message" in body &&
|
|
typeof body.message === "string"
|
|
) {
|
|
super(body.message);
|
|
} else {
|
|
super("Response error");
|
|
}
|
|
|
|
this.method = method;
|
|
this.body = body_;
|
|
this.status = response.status;
|
|
this.url = response.url;
|
|
}
|
|
}
|
|
|
|
// Error that is thrown by `Fetcher` methods when parsing the response
|
|
// body fails.
|
|
export class ResponseParseError extends Error {
|
|
public method: string;
|
|
public body: unknown;
|
|
public description: string;
|
|
public apiVersion: string | undefined;
|
|
public zodIssues: ZodIssue[];
|
|
public path?: string;
|
|
|
|
public constructor(
|
|
method: string,
|
|
body: unknown,
|
|
apiVersion: string,
|
|
zodIssues: ZodIssue[],
|
|
path?: string,
|
|
) {
|
|
super("Failed to parse response body");
|
|
const explorerRequiredApiVersion = config.nodes.requiredApiVersion;
|
|
const nodeApiVersion = apiVersion;
|
|
|
|
let description: string;
|
|
if (!satisfies(nodeApiVersion, explorerRequiredApiVersion)) {
|
|
description = `The node you are fetching from (v${nodeApiVersion}) doesn't match the version requirements of the web client ${explorerRequiredApiVersion}.`;
|
|
} else {
|
|
description = `The node (v${nodeApiVersion}) matches the version requirement of the web client (${explorerRequiredApiVersion}), but the web client isn't able to parse the response.`;
|
|
}
|
|
this.apiVersion = apiVersion;
|
|
this.description =
|
|
"The response received from the seed does not match the expected schema.<br/>".concat(
|
|
description,
|
|
);
|
|
|
|
this.method = method;
|
|
this.path = path;
|
|
this.body = body;
|
|
this.zodIssues = zodIssues;
|
|
}
|
|
}
|
|
|
|
export interface RequestOptions {
|
|
abort?: AbortSignal;
|
|
}
|
|
|
|
export interface FetchParams {
|
|
method: Method;
|
|
// Path to append to the `Fetcher`s base URL to get the final URL.
|
|
path?: string;
|
|
// Object that is serialized into JSON and sent as the data.
|
|
body?: unknown;
|
|
// Query parameters to be serialized with URLSearchParams.
|
|
query?: Record<string, string | number | boolean>;
|
|
options?: RequestOptions;
|
|
headers?: Record<string, string>;
|
|
}
|
|
|
|
type Method = "DELETE" | "GET" | "PATCH" | "POST" | "PUT";
|
|
|
|
export class Fetcher {
|
|
#baseUrl: BaseUrl;
|
|
|
|
public constructor(baseUrl: BaseUrl) {
|
|
this.#baseUrl = baseUrl;
|
|
}
|
|
|
|
// Execute a fetch and parse the result with the provided schema.
|
|
// Return the parsed payload.
|
|
//
|
|
// Throws `ResponseError` if the response status code is not `200`.
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
public async fetchOk<T extends ZodType<any>>(
|
|
params: FetchParams,
|
|
schema: T,
|
|
): Promise<TypeOf<T>> {
|
|
const response = await this.fetch({
|
|
...params,
|
|
query: { ...params.query, v: config.nodes.requiredApiVersion },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
let responseBody = await response.text();
|
|
try {
|
|
responseBody = JSON.parse(responseBody);
|
|
} catch {
|
|
// We keep the original text response body.
|
|
}
|
|
throw new ResponseError(params.method, response, responseBody);
|
|
}
|
|
|
|
const responseBody = await response.json();
|
|
const result = schema.safeParse(responseBody);
|
|
if (result.success) {
|
|
return result.data;
|
|
} else {
|
|
const response = await this.fetch({ method: "GET" });
|
|
const info = await response.json();
|
|
throw new ResponseParseError(
|
|
params.method,
|
|
responseBody,
|
|
info.apiVersion,
|
|
result.error.errors,
|
|
params.path,
|
|
);
|
|
}
|
|
}
|
|
|
|
private async fetch({
|
|
method,
|
|
path,
|
|
body,
|
|
options = {},
|
|
query,
|
|
headers = {},
|
|
}: FetchParams): Promise<Response> {
|
|
if (body !== undefined && headers["content-type"] === undefined) {
|
|
headers["content-type"] = "application/json";
|
|
}
|
|
|
|
const pathSegment = path === undefined ? "" : `/${path}`;
|
|
|
|
let url = `${this.#baseUrl.scheme}://${this.#baseUrl.hostname}:${this.#baseUrl.port}/api/v1${pathSegment}`;
|
|
|
|
if (query) {
|
|
const searchparams = new URLSearchParams(query as Record<string, string>);
|
|
url = `${url}?${searchparams.toString()}`;
|
|
}
|
|
return globalThis.fetch(url, {
|
|
method,
|
|
headers,
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
signal: options.abort,
|
|
});
|
|
}
|
|
}
|