Skip to content

Commit afc39a5

Browse files
committed
refactor(api): extract createSchemaRoute factory for schema endpoints
Addresses CodeRabbit's suggestion on PR #180 to de-duplicate the identical route/response-wrapper boilerplate across schema-post.ts, schema-collection.ts, and schema-author.ts.
1 parent 09d6595 commit afc39a5

4 files changed

Lines changed: 57 additions & 106 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { FastifyPluginAsync } from "fastify";
2+
import { Type, type TSchema } from "typebox";
3+
4+
export const createSchemaRoute = (
5+
path: string,
6+
description: string,
7+
schema: TSchema,
8+
): FastifyPluginAsync => {
9+
const responseSchema = Type.Object(
10+
{},
11+
{
12+
additionalProperties: true,
13+
description,
14+
examples: [schema],
15+
},
16+
);
17+
18+
return async (fastify) => {
19+
fastify.get(
20+
path,
21+
{
22+
schema: {
23+
description,
24+
response: {
25+
200: {
26+
description: "Successful",
27+
content: {
28+
"application/json": {
29+
schema: responseSchema,
30+
},
31+
},
32+
},
33+
},
34+
},
35+
},
36+
async (_request, reply) => {
37+
reply.code(200);
38+
reply.send(schema);
39+
},
40+
);
41+
};
42+
};
Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
1-
import type { FastifyPluginAsync } from "fastify";
2-
import { Type } from "typebox";
31
import { AuthorMetaSchema } from "../../../../worker/src/tasks/sync-author/types.ts";
2+
import { createSchemaRoute } from "./createSchemaRoute.ts";
43

5-
const SchemaAuthorResponseSchema = Type.Object(
6-
{},
7-
{
8-
additionalProperties: true,
9-
description: "A JSON Schema document describing valid author frontmatter.",
10-
examples: [AuthorMetaSchema],
11-
},
4+
const schemaAuthorRoutes = createSchemaRoute(
5+
"/content/schema/author",
6+
"Fetch the JSON Schema for author frontmatter, as validated by the sync worker.",
7+
AuthorMetaSchema,
128
);
139

14-
const schemaAuthorRoutes: FastifyPluginAsync = async (fastify) => {
15-
fastify.get(
16-
"/content/schema/author",
17-
{
18-
schema: {
19-
description:
20-
"Fetch the JSON Schema for author frontmatter, as validated by the sync worker.",
21-
response: {
22-
200: {
23-
description: "Successful",
24-
content: {
25-
"application/json": {
26-
schema: SchemaAuthorResponseSchema,
27-
},
28-
},
29-
},
30-
},
31-
},
32-
},
33-
async (_request, reply) => {
34-
reply.code(200);
35-
reply.send(AuthorMetaSchema);
36-
},
37-
);
38-
};
39-
4010
export default schemaAuthorRoutes;
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
1-
import type { FastifyPluginAsync } from "fastify";
2-
import { Type } from "typebox";
31
import { CollectionMetaSchema } from "../../../../worker/src/tasks/sync-collection/types.ts";
2+
import { createSchemaRoute } from "./createSchemaRoute.ts";
43

5-
const SchemaCollectionResponseSchema = Type.Object(
6-
{},
7-
{
8-
additionalProperties: true,
9-
description:
10-
"A JSON Schema document describing valid collection frontmatter.",
11-
examples: [CollectionMetaSchema],
12-
},
4+
const schemaCollectionRoutes = createSchemaRoute(
5+
"/content/schema/collection",
6+
"Fetch the JSON Schema for collection frontmatter, as validated by the sync worker.",
7+
CollectionMetaSchema,
138
);
149

15-
const schemaCollectionRoutes: FastifyPluginAsync = async (fastify) => {
16-
fastify.get(
17-
"/content/schema/collection",
18-
{
19-
schema: {
20-
description:
21-
"Fetch the JSON Schema for collection frontmatter, as validated by the sync worker.",
22-
response: {
23-
200: {
24-
description: "Successful",
25-
content: {
26-
"application/json": {
27-
schema: SchemaCollectionResponseSchema,
28-
},
29-
},
30-
},
31-
},
32-
},
33-
},
34-
async (_request, reply) => {
35-
reply.code(200);
36-
reply.send(CollectionMetaSchema);
37-
},
38-
);
39-
};
40-
4110
export default schemaCollectionRoutes;
Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
1-
import type { FastifyPluginAsync } from "fastify";
2-
import { Type } from "typebox";
31
import { PostMetaSchema } from "../../../../worker/src/tasks/sync-post/types.ts";
2+
import { createSchemaRoute } from "./createSchemaRoute.ts";
43

5-
const SchemaPostResponseSchema = Type.Object(
6-
{},
7-
{
8-
additionalProperties: true,
9-
description: "A JSON Schema document describing valid post frontmatter.",
10-
examples: [PostMetaSchema],
11-
},
4+
const schemaPostRoutes = createSchemaRoute(
5+
"/content/schema/post",
6+
"Fetch the JSON Schema for post frontmatter, as validated by the sync worker.",
7+
PostMetaSchema,
128
);
139

14-
const schemaPostRoutes: FastifyPluginAsync = async (fastify) => {
15-
fastify.get(
16-
"/content/schema/post",
17-
{
18-
schema: {
19-
description:
20-
"Fetch the JSON Schema for post frontmatter, as validated by the sync worker.",
21-
response: {
22-
200: {
23-
description: "Successful",
24-
content: {
25-
"application/json": {
26-
schema: SchemaPostResponseSchema,
27-
},
28-
},
29-
},
30-
},
31-
},
32-
},
33-
async (_request, reply) => {
34-
reply.code(200);
35-
reply.send(PostMetaSchema);
36-
},
37-
);
38-
};
39-
4010
export default schemaPostRoutes;

0 commit comments

Comments
 (0)