Skip to content

Commit

Permalink
refactor: splits templates API into list and fetch by id
Browse files Browse the repository at this point in the history
(#481)
---------
Co-authored-by: Serhii <[email protected]>
  • Loading branch information
stalniy authored Nov 19, 2024
1 parent 2ccd2b6 commit e65d61d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 7 deletions.
8 changes: 6 additions & 2 deletions apps/api/src/routes/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import proposals from "./proposals/list";
import providerByAddress from "./providers/byAddress";
import providerDeployments from "./providers/deployments";
import providerList from "./providers/list";
import templateById from "./templates/byId";
import templateList from "./templates/list";
import templateListFull from "./templates/list-full";
import transactionByHash from "./transactions/byHash";
import transactions from "./transactions/list";
import validatorByAddress from "./validators/byAddress";
Expand All @@ -31,7 +34,6 @@ import providerActiveLeasesGraphData from "./providerActiveLeasesGraphData";
import providerAttributesSchema from "./providerAttributesSchema";
import providerGraphData from "./providerGraphData";
import providerRegions from "./providerRegions";
import templates from "./templates";
import trialProviders from "./trialProviders";

export default [
Expand All @@ -53,7 +55,9 @@ export default [
validatorByAddress,
proposals,
proposalById,
templates,
templateListFull,
templateList,
templateById,
networkCapacity,
marketData,
dashboardData,
Expand Down
55 changes: 55 additions & 0 deletions apps/api/src/routes/v1/templates/byId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import { getCachedTemplateById } from "@src/services/external/templateReposService";

const route = createRoute({
method: "get",
path: "/templates/{id}",
tags: ["Other"],
request: {
params: z.object({
id: z.string().openapi({
description: "Template ID",
example: "akash-network-cosmos-omnibus-agoric"
})
})
},
responses: {
200: {
description: "Return a template by id",
content: {
"application/json": {
schema: z.object({
id: z.string(),
name: z.string(),
path: z.string(),
logoUrl: z.string().nullable(),
summary: z.string(),
readme: z.string().nullable(),
deploy: z.string(),
persistentStorageEnabled: z.boolean(),
guide: z.string().nullable(),
githubUrl: z.string(),
config: z.object({
ssh: z.boolean().optional()
})
})
}
}
},
404: {
description: "Template not found"
}
}
});

export default new OpenAPIHono().openapi(route, async c => {
const templateId = c.req.valid("param").id;
const template = await getCachedTemplateById(templateId);

if (!template) {
return c.text("Template not found", 404);
}

return c.json(template);
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import { cacheKeys, cacheResponse } from "@src/caching/helpers";
import { getTemplateGallery } from "@src/services/external/templateReposService";
import { getCachedTemplatesGallery } from "@src/services/external/templateReposService";

const route = createRoute({
method: "get",
path: "/templates",
tags: ["Other"],
responses: {
200: {
description: "Returns a list of deployment templates grouped by cateogories",
description: "Returns a list of deployment templates grouped by categories",
content: {
"application/json": {
schema: z.array(
Expand Down Expand Up @@ -40,7 +39,10 @@ const route = createRoute({
}
});

/**
* @deprecated should stay for some time in order to let UI to migrate to shorten list version.
*/
export default new OpenAPIHono().openapi(route, async c => {
const response = await cacheResponse(60 * 5, cacheKeys.getTemplates, async () => await getTemplateGallery(), true);
return c.json(response);
const templatesPerCategory = await getCachedTemplatesGallery();
return c.json(templatesPerCategory);
});
42 changes: 42 additions & 0 deletions apps/api/src/routes/v1/templates/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import { getCachedTemplatesGallery } from "@src/services/external/templateReposService";

const responseSchema = z.array(
z.object({
title: z.string(),
templates: z.array(
z.object({
id: z.string(),
name: z.string(),
logoUrl: z.string().nullable(),
summary: z.string(),
})
)
})
);
const route = createRoute({
method: "get",
path: "/templates-list",
tags: ["Other"],
responses: {
200: {
description: "Returns a list of deployment templates grouped by categories",
content: {
"application/json": {
schema: responseSchema
}
}
}
}
});

export default new OpenAPIHono().openapi(route, async c => {
const templatesPerCategory = await getCachedTemplatesGallery();
// TODO: remove manual response filtering when https://github.com/honojs/middleware/issues/181 is done
const filteredTemplatesPerCategory = await responseSchema.safeParseAsync(templatesPerCategory);
const response = filteredTemplatesPerCategory.success
? filteredTemplatesPerCategory.data
: templatesPerCategory;
return c.json(response);
});
16 changes: 16 additions & 0 deletions apps/api/src/services/external/templateReposService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { markdownToTxt } from "markdown-to-txt";
import fetch from "node-fetch";
import path from "path";

import { cacheKeys, cacheResponse } from "@src/caching/helpers";
import { GithubChainRegistryChainResponse } from "@src/types";
import { GithubDirectoryItem } from "@src/types/github";
import { dataFolderPath } from "@src/utils/constants";
Expand Down Expand Up @@ -129,6 +130,21 @@ export const getTemplateGallery = async () => {
}
};

export const getCachedTemplatesGallery = (): Promise<FinalCategory[]> => cacheResponse(60 * 5, cacheKeys.getTemplates, () => getTemplateGallery(), true);

export const getTemplateById = async (id: Required<Template>["id"]): Promise<Template | null> => {
const templatesByCategory = await getCachedTemplatesGallery();
for (const category of templatesByCategory) {
const template = category.templates.find(template => template.id === id);
if (template) return template;
}

return null;
};

export const getCachedTemplateById = (id: Required<Template>["id"]) =>
cacheResponse(60 * 5, `${cacheKeys.getTemplates}.${id}`, () => getTemplateById(id), true);

// Fetch latest version of a repo
export const fetchRepoVersion = async (octokit: Octokit, owner: string, repo: string) => {
const response = await octokit.rest.repos.getBranch({
Expand Down

0 comments on commit e65d61d

Please sign in to comment.