diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-27 07:26:27 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-27 07:26:27 +0300 |
| commit | b2474b3853baa1b44d94cc1e7344b1407cf15e08 (patch) | |
| tree | bbcd45baf44230eca6386dee11f1f05afe03b80d | |
| parent | 42e07956eff9a7137823fb6193c132712a9dc1a3 (diff) | |
| download | the-epube-b2474b3853baa1b44d94cc1e7344b1407cf15e08.tar.gz the-epube-b2474b3853baa1b44d94cc1e7344b1407cf15e08.tar.xz | |
convert http.ts to native TS types
| -rw-r--r-- | src/http.d.ts | 32 | ||||
| -rw-r--r-- | src/http.ts | 88 |
2 files changed, 50 insertions, 70 deletions
diff --git a/src/http.d.ts b/src/http.d.ts deleted file mode 100644 index 451b8a2..0000000 --- a/src/http.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -// @ts-check - -declare class HttpError extends Error { - status?: number; - responseText?: string; -} - -declare interface Http { - static setCsrfToken(token: string): void; - static get(url: string): Promise<HttpResponse>; - static post(url: string, body: Record<string, string>): Promise<HttpResponse>; - static blob(url: string): Promise<HttpBlobResponse>; - static text(url: string): Promise<HttpTextResponse>; -} - -declare interface HttpResponse { - data: any; - ok: boolean; - status: number; -} - -declare interface HttpBlobResponse { - blob: Blob; - ok: boolean; - status: number; -} - -declare interface HttpTextResponse { - text: string; - ok: boolean; - status: number; -} diff --git a/src/http.ts b/src/http.ts index 9c8b71a..6fd5c17 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,10 +1,38 @@ -// @ts-check - 'use strict'; +declare class HttpError extends Error { + status?: number; + responseText?: string; +} + +declare interface Http { + setCsrfToken(token: string): void; + get(url: string): Promise<HttpResponse>; + post(url: string, body: Record<string, string>): Promise<HttpResponse>; + blob(url: string): Promise<HttpBlobResponse>; + text(url: string): Promise<HttpTextResponse>; +} + +declare interface HttpResponse { + data: any; + ok: boolean; + status: number; +} + +declare interface HttpBlobResponse { + blob: Blob; + ok: boolean; + status: number; +} + +declare interface HttpTextResponse { + text: string; + ok: boolean; + status: number; +} + /** - * @class - * @description Static HTTP client wrapping fetch with CSRF support. + * Static HTTP client wrapping fetch with CSRF support. * * Usage: * const resp = await Http.get('backend.php?op=list'); @@ -14,58 +42,48 @@ * * const { blob } = await Http.blob('backend.php?op=download&id=42'); */ -// eslint-disable-next-line no-unused-vars class Http { - /** @private @type {string} */ - static _csrfToken = ''; + private static _csrfToken = ''; /** * Set the CSRF token for subsequent requests. - * @param {string} token + * @param token - The CSRF token string. */ - static setCsrfToken(token: any) { + static setCsrfToken(token: string): void { Http._csrfToken = token; } /** * Append query parameters to a URL string. * @private - * @param {string} url - * @param {Record<string, string> | null | undefined} params - * @returns {string} */ - static _appendParams(url: any, params: any) { + private static _appendParams(url: string, params: Record<string, string> | null | undefined): string { if (!params || Object.keys(params).length === 0) return url; const separator = url.includes('?') ? '&' : '?'; - const query = new URLSearchParams(params).toString(); + const query = new URLSearchParams(params as Record<string, string>).toString(); return `${url}${separator}${query}`; } /** * Throw a structured error for failed HTTP responses. * @private - * @param {Response} resp - * @returns {never} */ - static _throwHttpError(resp: any) { - /** @type {HttpError} */ - const err = new Error(`HTTP ${resp.status}: ${resp.statusText}`); + private static _throwHttpError(resp: Response): never { + const err = new Error(`HTTP ${resp.status}: ${resp.statusText}`) as HttpError; (err as any).status = resp.status; (err as any).responseText = ''; // Read error body asynchronously — don't block the throw -resp.text().then((t: any) => { (err as any).responseText = t; }).catch(() => { - /* ignore */ -}); + resp.text().then((t: string) => { (err as any).responseText = t; }).catch(() => { + /* ignore */ + }); throw err; } /** * Perform a GET request. Returns parsed JSON. - * @param {string} url - * @param {Record<string, string> | null} [params] - optional query parameters - * @returns {Promise<HttpResponse>} + * @param params - Optional query parameters. */ - static async get(url: any, params: any = null) { + static async get(url: string, params: Record<string, string> | null = null): Promise<HttpResponse> { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', @@ -82,11 +100,9 @@ resp.text().then((t: any) => { (err as any).responseText = t; }).catch(() => { /** * Perform a POST request with form-encoded body. Returns parsed JSON. - * @param {string} url - * @param {Record<string, string>} body - * @returns {Promise<HttpResponse>} + * @param body - Form-encoded body parameters. */ - static async post(url: any, body: any) { + static async post(url: string, body: Record<string, string>): Promise<HttpResponse> { const params = new URLSearchParams(body); if (Http._csrfToken) { params.append('csrf_token', Http._csrfToken); @@ -108,11 +124,9 @@ resp.text().then((t: any) => { (err as any).responseText = t; }).catch(() => { /** * Perform a GET request and return the body as a Blob. - * @param {string} url - * @param {Record<string, string> | null} [params] - optional query parameters - * @returns {Promise<HttpBlobResponse>} + * @param params - Optional query parameters. */ - static async blob(url: any, params: any = null) { + static async blob(url: string, params: Record<string, string> | null = null): Promise<HttpBlobResponse> { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', @@ -129,11 +143,9 @@ resp.text().then((t: any) => { (err as any).responseText = t; }).catch(() => { /** * Perform a GET request and return the body as plain text. - * @param {string} url - * @param {Record<string, string> | null} [params] - optional query parameters - * @returns {Promise<HttpTextResponse>} + * @param params - Optional query parameters. */ - static async text(url: any, params: any = null) { + static async text(url: string, params: Record<string, string> | null = null): Promise<HttpTextResponse> { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', |
