Refactor fetch method for HEAD request

This commit is contained in:
patrickkfkan 2023-10-30 02:05:03 +08:00
parent 38de8be573
commit 07b324f394
3 changed files with 8 additions and 9 deletions

View File

@ -1,4 +1,3 @@
import { Response } from 'node-fetch';
import Cache from '../utils/Cache.js';
import Fetcher, { FetchMethod } from '../utils/Fetcher.js';
@ -17,7 +16,7 @@ export default abstract class BaseAPI {
this.#cache = params.cache;
}
protected fetch(url: string, jsonResponse: false, method: FetchMethod.HEAD, payload?: undefined): Promise<Response>;
protected fetch(url: string, jsonResponse: false, method: FetchMethod.HEAD, payload?: undefined): Promise<{ ok: boolean, status: number }>;
protected fetch(url: string, jsonResponse: true, method?: FetchMethod, payload?: Record<string, any>): Promise<any>;
protected fetch(url: string, jsonResponse?: boolean, method?: FetchMethod, payload?: Record<string, any>): Promise<string>;
protected fetch(url: string, jsonResponse?: boolean, method?: FetchMethod, payload?: Record<string, any>): Promise<any> {

View File

@ -11,11 +11,7 @@ export interface StreamTestResult {
export default class StreamAPI extends BaseAPI {
async test(url: string): Promise<StreamTestResult> {
const res = await this.fetch(url, false, FetchMethod.HEAD);
return {
ok: res.ok,
status: res.status
};
return this.fetch(url, false, FetchMethod.HEAD);
}
async refresh(url: string): Promise<string | null> {

View File

@ -35,7 +35,7 @@ export default class Fetcher {
return this.#cookie;
}
fetch(url: string, jsonResponse: false, method: FetchMethod.HEAD, payload?: undefined): Promise<Response>;
fetch(url: string, jsonResponse: false, method: FetchMethod.HEAD, payload?: undefined): Promise<{ ok: boolean, status: number }>;
fetch(url: string, jsonResponse: true, method?: FetchMethod, payload?: Record<string, any>): Promise<any>;
fetch(url: string, jsonResponse?: boolean, method?: FetchMethod, payload?: Record<string, any>): Promise<string>;
fetch(url: string, jsonResponse?: boolean, method?: FetchMethod, payload?: Record<string, any>) {
@ -48,7 +48,11 @@ export default class Fetcher {
}
if (method === FetchMethod.HEAD) {
return fetch(url, { method: 'HEAD' });
const response = await fetch(url, { method: 'HEAD' });
return {
ok: response.ok,
status: response.status
};
}
let response;