├── .editorconfig ├── .github └── workflows │ └── main.yml ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | indent_style = tab 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if a URL exists. 3 | 4 | @param url The URL to check. 5 | 6 | @example 7 | ``` 8 | import urlExist from "url-exist" 9 | 10 | await urlExist("https://google.com") 11 | //=> true 12 | 13 | await urlExist("https://google.com/404ingURL") 14 | //=> false 15 | 16 | await urlExist("notaurl") 17 | //=> false 18 | ``` 19 | */ 20 | declare function urlExist(url: string): Promise 21 | 22 | export default urlExist 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import isUrl from "is-url-superb" 2 | import ky from "ky-universal" 3 | 4 | const urlExist = async url => { 5 | if (typeof url !== "string") { 6 | throw new TypeError(`Expected a string, got ${typeof url}`) 7 | } 8 | 9 | if (!isUrl(url)) { 10 | return false 11 | } 12 | 13 | const response = await ky.head(url, { 14 | throwHttpErrors: false 15 | }) 16 | 17 | return response !== undefined && (response.status < 400 || response.status >= 500) 18 | } 19 | 20 | export default urlExist 21 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import { expectType } from "tsd" 2 | import urlExist from "./index.js" 3 | 4 | expectType>(urlExist("https://example.com")) 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 - 2021 Richie Bendall 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-exist", 3 | "version": "3.0.1", 4 | "description": "Check if a URL exists.", 5 | "repository": "Richienb/url-exist", 6 | "author": { 7 | "name": "Richie Bendall", 8 | "email": "richiebendall@gmail.com" 9 | }, 10 | "license": "MIT", 11 | "type": "module", 12 | "exports": "./index.js", 13 | "files": [ 14 | "index.js", 15 | "index.d.ts" 16 | ], 17 | "engines": { 18 | "node": ">=14.8" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava && tsd" 22 | }, 23 | "keywords": [ 24 | "url", 25 | "exists", 26 | "check", 27 | "validation", 28 | "promise", 29 | "modern", 30 | "async" 31 | ], 32 | "dependencies": { 33 | "is-url-superb": "^6.1.0", 34 | "ky": "^0.27.0", 35 | "ky-universal": "^0.10.1" 36 | }, 37 | "devDependencies": { 38 | "ava": "^3.15.0", 39 | "eslint-config-richienb": "^0.6.1", 40 | "tsd": "^0.14.0", 41 | "xo": "^0.37.1" 42 | }, 43 | "xo": { 44 | "extends": "richienb" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # URL Exist 2 | 3 | Check if a URL exists. 4 | 5 | [![NPM Badge](https://nodei.co/npm/url-exist.png)](https://npmjs.com/package/url-exist) 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install url-exist 11 | ``` 12 | 13 | ## Improvements over [`url-exists`](https://github.com/boblauer/url-exists) 14 | 15 | - Promise interface. 16 | - Works cross-platform. 17 | - Smaller install size. 18 | - Typescript support included. 19 | - Catches invalid URLs. 20 | - Actively maintained. 21 | 22 | ## Usage 23 | 24 | ```js 25 | import urlExist from "url-exist" 26 | 27 | await urlExist("https://google.com") 28 | //=> true 29 | 30 | await urlExist("https://google.com/404ingURL") 31 | //=> false 32 | 33 | await urlExist("notaurl") 34 | //=> false 35 | ``` 36 | 37 | ## API 38 | 39 | ### urlExist(url) 40 | 41 | #### url 42 | 43 | Type: `string` 44 | 45 | The URL to check. 46 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from "ava" 2 | import urlExist from "./index.js" 3 | 4 | test("main", async t => { 5 | t.true(await urlExist("https://httpbin.org/status/200")) 6 | 7 | t.false(await urlExist("https://httpbin.org/status/404")) 8 | 9 | t.false(await urlExist("notaurl")) 10 | 11 | t.false(await urlExist("https://aurlthatprobablydoesnotexist.co")) 12 | }) 13 | --------------------------------------------------------------------------------