refactor(core/caniuse): use new caniuse API

This commit is contained in:
Sid Vishnoi 2021-03-15 22:56:50 +05:30
parent 7534a2b325
commit eff2e120ed
3 changed files with 17 additions and 24 deletions

View File

@ -11,7 +11,7 @@ import { html } from "./import-maps.js";
export const name = "core/caniuse";
const API_URL = "https://respec.org/caniuse/";
const API_URL = "https://respec.org/";
const BROWSERS = new Set([
"and_chr",
@ -57,7 +57,7 @@ export async function prepare(conf) {
const apiUrl = options.apiURL || API_URL;
// Initiate a fetch, but do not wait. Try to fill the cache early instead.
conf.state[name] = {
fetchPromise: fetchStats(apiUrl, options),
fetchPromise: fetchStats(apiUrl, options.feature, options.browsers),
};
}
@ -102,16 +102,16 @@ export async function run(conf) {
/**
* returns normalized `conf.caniuse` configuration
* @param {Object} conf configuration settings
* @returns {{ feature: string, browsers?: string[], apiURL?: string }}
*/
function getNormalizedConf(conf) {
const DEFAULTS = { versions: 4 };
if (typeof conf.caniuse === "string") {
return { feature: conf.caniuse, ...DEFAULTS };
return { feature: conf.caniuse };
}
const caniuseConf = { ...DEFAULTS, ...conf.caniuse };
const { browsers } = caniuseConf;
if (Array.isArray(browsers)) {
const invalidBrowsers = browsers.filter(browser => !BROWSERS.has(browser));
if (Array.isArray(conf.caniuse.browsers)) {
const invalidBrowsers = conf.caniuse.browsers.filter(
browser => !BROWSERS.has(browser)
);
if (invalidBrowsers.length) {
const names = invalidBrowsers.map(b => `"\`${b}\`"`).join(", ");
const msg =
@ -120,24 +120,21 @@ function getNormalizedConf(conf) {
showWarning(msg, name);
}
}
return caniuseConf;
return conf.caniuse;
}
/**
* @param {string} apiURL
* @param {string} feature
* @param {string[]} [browsers]
* @typedef {Record<string, [string, string[]][]>} ApiResponse
* @throws {Error} on failure
*/
async function fetchStats(apiURL, options) {
const { feature, versions, browsers } = options;
const searchParams = new URLSearchParams();
searchParams.set("feature", feature);
searchParams.set("versions", versions);
async function fetchStats(apiURL, feature, browsers) {
const url = new URL(`caniuse/${feature}`, apiURL);
if (Array.isArray(browsers)) {
searchParams.set("browsers", browsers.join(","));
url.searchParams.set("browsers", browsers.join(","));
}
searchParams.set("format", "html");
const url = `${apiURL}?${searchParams.toString()}`;
const response = await fetch(url);
if (!response.ok) {
const { status, statusText } = response;

View File

@ -10,7 +10,7 @@ import {
describe("Core — Can I Use", () => {
afterAll(flushIframes);
const apiURL = `${window.location.origin}/tests/data/caniuse/FEATURE.html`;
const apiURL = `${window.location.origin}/tests/data/caniuse/`;
it("uses meaningful defaults", async () => {
const ops = makeStandardOps({
@ -24,7 +24,6 @@ describe("Core — Can I Use", () => {
const { caniuse } = doc.defaultView.respecConfig;
expect(caniuse.feature).toBe("FEATURE");
expect(caniuse.versions).toBe(4);
expect(caniuse.browsers).toBeUndefined(); // uses server default
});
@ -32,7 +31,6 @@ describe("Core — Can I Use", () => {
const ops = makeStandardOps({
caniuse: {
feature: "FEATURE",
versions: 10,
browsers: ["firefox", "chrome"],
apiURL,
},
@ -43,7 +41,6 @@ describe("Core — Can I Use", () => {
expect(caniuse.feature).toBe("FEATURE");
expect(caniuse.browsers).toEqual(["firefox", "chrome"]);
expect(caniuse.versions).toBe(10);
});
it("does nothing if caniuse is not enabled", async () => {
@ -76,9 +73,8 @@ describe("Core — Can I Use", () => {
const ops = makeStandardOps({
caniuse: {
feature: "FEATURE",
apiURL,
browsers: ["firefox", "chrome", "opera"],
versions: 5,
apiURL,
},
});
const doc = await makeRSDoc(ops);
@ -122,7 +118,7 @@ describe("Core — Can I Use", () => {
opsWithCaniuse.config.publishDate = "1999-12-11";
opsWithCaniuse.config.caniuse = {
feature: "FEATURE",
apiURL: `${window.location.origin}/tests/data/caniuse/{FEATURE}.json`,
apiURL,
};
const doc = await makeRSDoc(opsWithCaniuse);
await doc.respec.ready;