mirror of
https://github.com/cloudflare/cloudflare-docs.git
synced 2026-01-16 23:11:06 +00:00
[Docs Site] Remove old API docs functions (#18789)
* [Docs Site] Remove old API docs functions * remove tests * remove fixtures * fixup package.json scripts * remove fixture import
This commit is contained in:
parent
fe18bb1856
commit
564a1c80f8
8 changed files with 3 additions and 223 deletions
|
|
@ -1,47 +0,0 @@
|
|||
import redirects from "./redirects";
|
||||
|
||||
const apiBase = "https://cloudflare-api-docs-frontend.pages.dev";
|
||||
|
||||
const rewriteStaticAssets = {
|
||||
element: (element: Element) => {
|
||||
const prefixAttribute = (attr: string) => {
|
||||
const value = element.getAttribute(attr);
|
||||
|
||||
if (value.startsWith("http")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedValue = `/api/${value.startsWith("/") ? value.slice(1) : value}`;
|
||||
element.setAttribute(attr, updatedValue);
|
||||
};
|
||||
|
||||
const attrs = ["href", "src"];
|
||||
attrs.forEach((attr) => {
|
||||
if (element.getAttribute(attr)) prefixAttribute(attr);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export const onRequestGet: PagesFunction<{}> = async ({ request }) => {
|
||||
const apiPath = "/api";
|
||||
|
||||
const url = new URL(request.url);
|
||||
|
||||
let subpath = url.pathname.replace(apiPath, "");
|
||||
let normalizedSubpath =
|
||||
subpath.slice(-1) === "/"
|
||||
? subpath.substring(0, subpath.length - 1)
|
||||
: subpath;
|
||||
if (normalizedSubpath in redirects) {
|
||||
url.pathname = redirects[normalizedSubpath];
|
||||
return Response.redirect(url.toString(), 301);
|
||||
}
|
||||
const proxyUrl = `${apiBase}/${subpath}`;
|
||||
const proxyResponse = await fetch(proxyUrl);
|
||||
|
||||
return new HTMLRewriter()
|
||||
.on("script", rewriteStaticAssets)
|
||||
.on("link", rewriteStaticAssets)
|
||||
.on("img", rewriteStaticAssets)
|
||||
.transform(proxyResponse);
|
||||
};
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
export default {
|
||||
"/v4docs": "/fundamentals/api/how-to/make-api-calls/",
|
||||
"/tokens": "/fundamentals/api/get-started/",
|
||||
"/tokens/create": "/fundamentals/api/get-started/create-token/",
|
||||
"/tokens/create/template": "/fundamentals/api/reference/template/",
|
||||
"/tokens/create/permissions": "/fundamentals/api/reference/permissions/",
|
||||
"/tokens/advanced": "/fundamentals/api/how-to/",
|
||||
"/tokens/advanced/restrictions": "/fundamentals/api/how-to/restrict-tokens/",
|
||||
"/tokens/advanced/api": "/fundamentals/api/how-to/create-via-api/",
|
||||
"/keys": "/fundamentals/api/get-started/keys/",
|
||||
"/limits": "/fundamentals/api/reference/limits/",
|
||||
"/how-to/make-api-calls": "/fundamentals/api/how-to/make-api-calls/",
|
||||
"/get-started": "/fundamentals/api/get-started/",
|
||||
"/get-started/create-token": "/fundamentals/api/get-started/create-token/",
|
||||
"/reference/template": "/fundamentals/api/reference/template/",
|
||||
"/reference/permissions": "/fundamentals/api/reference/permissions/",
|
||||
"/how-to": "/fundamentals/api/how-to/",
|
||||
"/how-to/restrict-tokens": "/fundamentals/api/how-to/restrict-tokens/",
|
||||
"/how-to/create-via-api": "/fundamentals/api/how-to/create-via-api/",
|
||||
"/get-started/keys": "/fundamentals/api/get-started/keys/",
|
||||
"/reference/limits": "/fundamentals/api/reference/limits",
|
||||
};
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
interface Environment {
|
||||
API_DOCS_KV: KVNamespace;
|
||||
}
|
||||
|
||||
export const onRequestGet: PagesFunction<Environment> = async (context) => {
|
||||
const cachedSchema = await context.env.API_DOCS_KV?.get("schema", "json");
|
||||
if (cachedSchema) {
|
||||
return new Response(JSON.stringify(cachedSchema), {
|
||||
headers: { "Content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const schemaUrl =
|
||||
"https://raw.githubusercontent.com/cloudflare/api-schemas/main/openapi.json";
|
||||
|
||||
const req = new Request(schemaUrl);
|
||||
|
||||
const cache = caches.default;
|
||||
let response = await cache.match(req);
|
||||
|
||||
try {
|
||||
if (!response) {
|
||||
response = await fetch(req);
|
||||
const schema = await response.json<any>();
|
||||
|
||||
const pathsByTag: Record<string, any> = {};
|
||||
|
||||
Object.keys(schema.paths).forEach((key) => {
|
||||
const path = schema.paths[key];
|
||||
const tag = Object.values(path).find((endpoint: any) => {
|
||||
const tags = endpoint.tags;
|
||||
return tags && tags.length && tags[0];
|
||||
}) as string;
|
||||
if (tag) {
|
||||
if (!pathsByTag[tag]) pathsByTag[tag] = [];
|
||||
pathsByTag[tag].push({ path, key });
|
||||
}
|
||||
});
|
||||
|
||||
let sortedPaths = {};
|
||||
const sortedTags = Object.keys(pathsByTag).sort();
|
||||
sortedTags.forEach((tag) => {
|
||||
const tagArray = pathsByTag[tag];
|
||||
tagArray.forEach(({ key, path }) => {
|
||||
if (sortedPaths[key]) console.log("key already exists");
|
||||
sortedPaths[key] = path;
|
||||
});
|
||||
});
|
||||
|
||||
// sort sortedPaths by tag
|
||||
sortedPaths = Object.entries(sortedPaths)
|
||||
.sort((a, b) => {
|
||||
const aVal = a[1];
|
||||
const bVal = b[1];
|
||||
const firstAVal = Object.values(aVal).find((endpoint) => {
|
||||
const tags = endpoint.tags;
|
||||
return tags && tags.length && tags[0];
|
||||
});
|
||||
const aTag = (firstAVal && firstAVal.tags[0]) || "";
|
||||
const firstBVal = Object.values(bVal).find((endpoint) => {
|
||||
const tags = endpoint.tags;
|
||||
return tags && tags.length && tags[0];
|
||||
});
|
||||
const bTag = (firstBVal && firstBVal.tags[0]) || "";
|
||||
if (aTag < bTag) return -1;
|
||||
if (aTag > bTag) return 1;
|
||||
return 0;
|
||||
})
|
||||
.reduce((obj, [key, val]) => {
|
||||
obj[key] = val;
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
const sortedSchema = Object.assign({}, schema, { paths: sortedPaths });
|
||||
|
||||
response = new Response(JSON.stringify(sortedSchema), {
|
||||
headers: { "Content-type": "application/json" },
|
||||
});
|
||||
|
||||
const expirationTtl = 60 * 60;
|
||||
context.waitUntil(
|
||||
context.env.API_DOCS_KV.put("schema", JSON.stringify(sortedSchema), {
|
||||
expirationTtl,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return fetch(req);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "CommonJS",
|
||||
"lib": ["ES2020"],
|
||||
"types": ["@cloudflare/workers-types"]
|
||||
},
|
||||
"include": ["./**/*.ts"]
|
||||
}
|
||||
|
|
@ -6,11 +6,10 @@
|
|||
"astro": "npx astro",
|
||||
"build": "npx astro build",
|
||||
"postbuild": "npm run build:worker",
|
||||
"build:worker": "npx wrangler types -c wrangler-workers.toml --experimental-include-runtime && npx wrangler pages functions build --outdir worker/functions",
|
||||
"check": "npm run check:functions && npm run check:astro",
|
||||
"build:worker": "npx wrangler types -c wrangler-workers.toml --experimental-include-runtime",
|
||||
"check": "npm run check:astro && npm run check:worker",
|
||||
"check:astro": "npm run sync && astro check",
|
||||
"check:functions": "npx tsc --noEmit -p ./functions/tsconfig.json",
|
||||
"check:worker": "npx tsc --noEmit -p ./worker/tsconfig.json",
|
||||
"check:worker": "npm run build:worker && npx tsc --noEmit -p ./worker/tsconfig.json",
|
||||
"dev": "npx astro dev",
|
||||
"format": "npm run format:core && npm run format:data",
|
||||
"format:core": "npx prettier --write \"**/*.{js,jsx,ts,tsx,mjs,css}\"",
|
||||
|
|
|
|||
1
tests/fixtures/openapi.json
vendored
1
tests/fixtures/openapi.json
vendored
File diff suppressed because one or more lines are too long
|
|
@ -1,6 +1,5 @@
|
|||
import { fetchMock, SELF } from "cloudflare:test";
|
||||
import { describe, it, expect, beforeAll, afterEach } from "vitest";
|
||||
import openAPISchema from "./fixtures/openapi.json";
|
||||
import puppeteer, { Browser, HTTPRequest } from "@cloudflare/puppeteer";
|
||||
import { inject } from "vitest";
|
||||
|
||||
|
|
@ -83,43 +82,4 @@ describe("Cloudflare Docs", () => {
|
|||
const fullTitle = await textSelector?.evaluate((el) => el.textContent);
|
||||
expect(fullTitle).toContain("Cloudflare Docs");
|
||||
});
|
||||
|
||||
it("responds with API schema at `/schema`", async () => {
|
||||
fetchMock
|
||||
.get("https://raw.githubusercontent.com")
|
||||
.intercept({ path: "/cloudflare/api-schemas/main/openapi.json" })
|
||||
.reply(200, JSON.stringify(openAPISchema));
|
||||
|
||||
const request = new Request("http://fakehost/schema");
|
||||
const response = await SELF.fetch(request);
|
||||
expect(response.headers.get("Content-Type")).toBe("application/json");
|
||||
const data = (await response.json()) as any;
|
||||
expect(Object.keys(data)).toMatchInlineSnapshot(`
|
||||
[
|
||||
"components",
|
||||
"info",
|
||||
"openapi",
|
||||
"paths",
|
||||
"security",
|
||||
"servers",
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
it("responds with API docs files at `/api/*`", async () => {
|
||||
const mockContents = `const some = 'js';`;
|
||||
|
||||
fetchMock
|
||||
.get("https://cloudflare-api-docs-frontend.pages.dev")
|
||||
.intercept({
|
||||
path: (p) => {
|
||||
return p === "//static/js/file.js";
|
||||
},
|
||||
})
|
||||
.reply(200, mockContents);
|
||||
|
||||
const request = new Request("http://fakehost/api/static/js/file.js");
|
||||
const response = await SELF.fetch(request);
|
||||
expect(await response.text()).toEqual(mockContents);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import { WorkerEntrypoint } from "cloudflare:workers";
|
||||
import { generateRedirectsEvaluator } from "redirects-in-workers";
|
||||
import redirectsFileContents from "../dist/_redirects";
|
||||
import functions from "./functions";
|
||||
|
||||
const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents);
|
||||
|
||||
|
|
@ -51,12 +50,6 @@ export default class extends WorkerEntrypoint<Env> {
|
|||
error,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return await functions.fetch(request, this.env, this.ctx);
|
||||
} catch (error) {
|
||||
console.error("Could not evaluate functions", error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Unknown error", error);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue