Skip to content

Commit 0620911

Browse files
Cherrymaheshwarip
authored andcommitted
[Docs Site] Convert schema function to TS (#16835)
1 parent be1a864 commit 0620911

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

functions/schema.js renamed to functions/schema.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
export async function onRequestGet(context) {
2-
const cachedSchema = await context.env.API_DOCS_KV.get("schema", "json");
1+
interface Environment {
2+
API_DOCS_KV: KVNamespace;
3+
}
4+
5+
export const onRequestGet: PagesFunction<Environment> = async (context) => {
6+
const cachedSchema = await context.env.API_DOCS_KV?.get("schema", "json");
37
if (cachedSchema) {
48
return new Response(JSON.stringify(cachedSchema), {
59
headers: { "Content-type": "application/json" },
@@ -17,16 +21,16 @@ export async function onRequestGet(context) {
1721
try {
1822
if (!response) {
1923
response = await fetch(req);
20-
let schema = await response.json();
24+
const schema = await response.json<any>();
2125

22-
const pathsByTag = {};
26+
const pathsByTag: Record<string, any> = {};
2327

2428
Object.keys(schema.paths).forEach((key) => {
2529
const path = schema.paths[key];
26-
const tag = Object.values(path).find((endpoint) => {
30+
const tag = Object.values(path).find((endpoint: any) => {
2731
const tags = endpoint.tags;
2832
return tags && tags.length && tags[0];
29-
});
33+
}) as string;
3034
if (tag) {
3135
if (!pathsByTag[tag]) pathsByTag[tag] = [];
3236
pathsByTag[tag].push({ path, key });
@@ -67,17 +71,17 @@ export async function onRequestGet(context) {
6771
return obj;
6872
}, {});
6973

70-
let sortedSchema = Object.assign({}, schema, { paths: sortedPaths });
74+
const sortedSchema = Object.assign({}, schema, { paths: sortedPaths });
7175

7276
response = new Response(JSON.stringify(sortedSchema), {
7377
headers: { "Content-type": "application/json" },
7478
});
7579

7680
const expirationTtl = 60 * 60;
77-
await context.env.API_DOCS_KV.put(
78-
"schema",
79-
JSON.stringify(sortedSchema),
80-
{ expirationTtl },
81+
context.waitUntil(
82+
context.env.API_DOCS_KV.put("schema", JSON.stringify(sortedSchema), {
83+
expirationTtl,
84+
}),
8185
);
8286
}
8387

@@ -86,4 +90,4 @@ export async function onRequestGet(context) {
8690
console.log(err);
8791
return fetch(req);
8892
}
89-
}
93+
};

0 commit comments

Comments
 (0)