link-exists
Version:
A super lightweight JavaScript / TypeScript library to check whether a given url is valid and exists or not.
35 lines (34 loc) • 1.24 kB
TypeScript
import type { LinkValidatorConfig } from './config';
export interface LinkCheckResult {
exists: boolean;
status: number;
url: string;
}
type Opts = LinkValidatorConfig;
type WithDetailsConfig = Opts & {
details: true;
};
/**
* Checks if a URL responds on the network. Returns true when a response is received.
* Returns false for bad URLs, timeouts, and network errors.
* Throws only if `link` is not a string or if a config field has the wrong type.
*
* If `config.details` is true, you get an object with `exists`, `status`, and `url`.
* If `details` is false or missing, you get a boolean like before.
*
* @param link - The URL string to check.
* @param config - Optional settings. See `LinkValidatorConfig`.
* @returns A promise with true or false, or with a result object when `details` is true.
*
* @example
* await linkExists('https://example.com');
*
* @example
* await linkExists('example.com', { ignoreProtocol: true });
*
* @example
* await linkExists('https://example.com', { details: true });
*/
export declare function linkExists(link: string, config?: Opts): Promise<boolean>;
export declare function linkExists(link: string, config: WithDetailsConfig): Promise<LinkCheckResult>;
export {};