Refactor fetch method for HEAD request
This commit is contained in:
parent
38de8be573
commit
07b324f394
|
@ -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> {
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user