-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ruleset-migrator): use Content-Type header to detect ruleset format
- Loading branch information
Showing
4 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { fetch as defaultFetch } from '@stoplight/spectral-runtime'; | ||
import { isURL, extname } from '@stoplight/path'; | ||
import type { Fetch } from '../types'; | ||
|
||
function stripSearchFromUrl(url: string): string { | ||
try { | ||
const { href, search } = new URL(url); | ||
return href.slice(0, href.length - search.length); | ||
} catch { | ||
return url; | ||
} | ||
} | ||
|
||
const CONTENT_TYPE_REGEXP = /^(?:application|text)\/(?:yaml|json)(?:;|$)/i; | ||
const EXT_REGEXP = /\.(json|ya?ml)$/i; | ||
|
||
export async function isBasicRuleset(uri: string, fetch: Fetch = defaultFetch): Promise<boolean> { | ||
const ext = extname(isURL(uri) ? stripSearchFromUrl(uri) : uri); | ||
|
||
if (EXT_REGEXP.test(ext)) { | ||
return true; | ||
} | ||
|
||
if (!isURL(uri)) { | ||
return false; | ||
} | ||
|
||
try { | ||
const contentType = (await fetch(uri)).headers.get('Content-Type'); | ||
return contentType !== null && CONTENT_TYPE_REGEXP.test(contentType); | ||
} catch { | ||
return false; | ||
} | ||
} |