-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: splits templates API into list and fetch by id
(#481) --------- Co-authored-by: Serhii <[email protected]>
- Loading branch information
Showing
5 changed files
with
126 additions
and
7 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
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); | ||
}); |
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,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); | ||
}); |
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