* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0. - [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases) - [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0) --- updated-dependencies: - dependency-name: eslint-plugin-unicorn dependency-version: 68.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * style: fix no-useless-coercion * style: fix prefer-boolean-return * style: fix no-useless-logical-operand * style: fix prefer-single-replace * style: fix consistent-conditional-object-spread * style: fix no-useless-else * style: fix prefer-unicode-code-point-escapes * style: fix operator-assignment * style: fix prefer-continue * style: fix prefer-math-constants * style: fix prefer-hoisting-branch-code * style: fix prefer-array-slice * style: fix consistent-class-member-order * style: fix no-return-array-push * style: fix prefer-promise-with-resolvers * style: fix no-array-concat-in-loop * style: fix no-loop-iterable-mutation * style: fix no-invalid-argument-count * style: fix no-duplicate-if-branches * style: fix prefer-else-if * style: fix no-unreadable-for-of-expression * style: fix logical-assignment-operators * style: fix default-export-style * style: revert false positve in prefer-type-literal-last in #22274 * style: fix arrow-body-style * style: add v68 recommended preset * style: refactor GraphQL queries to use template literals for better readability --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
import type { RouteHandler } from '@hono/zod-openapi';
|
|
import { createRoute, z } from '@hono/zod-openapi';
|
|
|
|
import { namespaces } from '@/registry';
|
|
|
|
const categoryList: Record<string, typeof namespaces> = {};
|
|
|
|
for (const namespace in namespaces) {
|
|
for (const path in namespaces[namespace].routes) {
|
|
if (!namespaces[namespace].routes[path].categories?.length) {
|
|
continue;
|
|
}
|
|
const categories = namespaces[namespace].routes[path].categories!;
|
|
for (const category of categories) {
|
|
if (!Object.hasOwn(categoryList, category)) {
|
|
categoryList[category] = {};
|
|
}
|
|
if (!Object.hasOwn(categoryList[category], namespace)) {
|
|
categoryList[category][namespace] = {
|
|
...namespaces[namespace],
|
|
routes: {},
|
|
};
|
|
}
|
|
categoryList[category][namespace].routes[path] = namespaces[namespace].routes[path];
|
|
}
|
|
}
|
|
}
|
|
|
|
const ParamsSchema = z.object({
|
|
category: z.string().openapi({
|
|
param: {
|
|
name: 'category',
|
|
in: 'path',
|
|
},
|
|
example: 'popular',
|
|
}),
|
|
});
|
|
|
|
const QuerySchema = z.object({
|
|
categories: z
|
|
.string()
|
|
.transform((val) => val.split(','))
|
|
.optional(),
|
|
lang: z.string().optional(),
|
|
});
|
|
|
|
const route = createRoute({
|
|
method: 'get',
|
|
path: '/category/{category}',
|
|
description: 'Namespace list filtered by category',
|
|
tags: ['Category'],
|
|
request: {
|
|
query: QuerySchema,
|
|
params: ParamsSchema,
|
|
},
|
|
responses: {
|
|
200: {
|
|
description: 'Namespaces matching the requested category',
|
|
},
|
|
},
|
|
});
|
|
|
|
const handler: RouteHandler<typeof route> = (ctx) => {
|
|
const { categories, lang } = ctx.req.valid('query');
|
|
const { category } = ctx.req.valid('param');
|
|
|
|
let allCategories = [category];
|
|
if (categories && categories.length > 0) {
|
|
allCategories = [...allCategories, ...categories];
|
|
}
|
|
|
|
// Get namespaces that exist in all requested categories
|
|
const commonNamespaces = Object.keys(categoryList[category] || {}).filter((namespace) => allCategories.every((cat) => categoryList[cat]?.[namespace]));
|
|
|
|
// Create result directly from common namespaces
|
|
let result = Object.fromEntries(commonNamespaces.map((namespace) => [namespace, categoryList[category][namespace]]));
|
|
|
|
// Filter by language if provided
|
|
if (lang) {
|
|
result = Object.fromEntries(Object.entries(result).filter(([, value]) => value.lang === lang));
|
|
}
|
|
|
|
return ctx.json(result);
|
|
};
|
|
|
|
export { handler, route };
|