Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/api/src/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import authorsRoutes from "./routes/content/authors.ts";
import collectionsRoutes from "./routes/content/collections.ts";
import postRoutes from "./routes/content/post.ts";
import profilesRoutes from "./routes/content/profiles.ts";
import schemaPostRoutes from "./routes/content/schema-post.ts";
import schemaCollectionRoutes from "./routes/content/schema-collection.ts";
import schemaAuthorRoutes from "./routes/content/schema-author.ts";
import fastify from "fastify";
import devRoutes from "./routes/dev/index.ts";

Expand All @@ -27,6 +30,9 @@ export const createApp = () => {
app.register(collectionsRoutes);
app.register(postRoutes);
app.register(profilesRoutes);
app.register(schemaPostRoutes);
app.register(schemaCollectionRoutes);
app.register(schemaAuthorRoutes);

if (env.ENVIRONMENT === "development") {
app.register(devRoutes);
Expand Down
42 changes: 42 additions & 0 deletions apps/api/src/routes/content/createSchemaRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { FastifyPluginAsync } from "fastify";
import { Type, type TSchema } from "typebox";

export const createSchemaRoute = (
path: string,
description: string,
schema: TSchema,
): FastifyPluginAsync => {
const responseSchema = Type.Object(
{},
{
additionalProperties: true,
description,
examples: [schema],
},
);

return async (fastify) => {
fastify.get(
path,
{
schema: {
description,
response: {
200: {
description: "Successful",
content: {
"application/json": {
schema: responseSchema,
},
},
},
},
},
},
async (_request, reply) => {
reply.code(200);
reply.send(schema);
},
);
};
};
27 changes: 27 additions & 0 deletions apps/api/src/routes/content/schema-author.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fastify, { type FastifyInstance } from "fastify";
import schemaAuthorRoutes from "./schema-author.ts";
import { AuthorMetaSchema } from "@playfulprogramming/common";

describe("Schema Author Routes Tests", () => {
let app: FastifyInstance;
beforeAll(async () => {
app = fastify();
await app.register(schemaAuthorRoutes);
});

afterAll(async () => {
await app.close();
});

describe("/content/schema/author", () => {
test("returns the author frontmatter JSON Schema", async () => {
const response = await app.inject({
method: "GET",
url: "/content/schema/author",
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual(AuthorMetaSchema);
});
});
});
10 changes: 10 additions & 0 deletions apps/api/src/routes/content/schema-author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AuthorMetaSchema } from "@playfulprogramming/common";
import { createSchemaRoute } from "./createSchemaRoute.ts";

const schemaAuthorRoutes = createSchemaRoute(
"/content/schema/author",
"Fetch the JSON Schema for author frontmatter, as validated by the sync worker.",
AuthorMetaSchema,
);

export default schemaAuthorRoutes;
27 changes: 27 additions & 0 deletions apps/api/src/routes/content/schema-collection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fastify, { type FastifyInstance } from "fastify";
import schemaCollectionRoutes from "./schema-collection.ts";
import { CollectionMetaSchema } from "@playfulprogramming/common";

describe("Schema Collection Routes Tests", () => {
let app: FastifyInstance;
beforeAll(async () => {
app = fastify();
await app.register(schemaCollectionRoutes);
});

afterAll(async () => {
await app.close();
});

describe("/content/schema/collection", () => {
test("returns the collection frontmatter JSON Schema", async () => {
const response = await app.inject({
method: "GET",
url: "/content/schema/collection",
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual(CollectionMetaSchema);
});
});
});
10 changes: 10 additions & 0 deletions apps/api/src/routes/content/schema-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CollectionMetaSchema } from "@playfulprogramming/common";
import { createSchemaRoute } from "./createSchemaRoute.ts";

const schemaCollectionRoutes = createSchemaRoute(
"/content/schema/collection",
"Fetch the JSON Schema for collection frontmatter, as validated by the sync worker.",
CollectionMetaSchema,
);

export default schemaCollectionRoutes;
27 changes: 27 additions & 0 deletions apps/api/src/routes/content/schema-post.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fastify, { type FastifyInstance } from "fastify";
import schemaPostRoutes from "./schema-post.ts";
import { PostMetaSchema } from "@playfulprogramming/common";

describe("Schema Post Routes Tests", () => {
let app: FastifyInstance;
beforeAll(async () => {
app = fastify();
await app.register(schemaPostRoutes);
});

afterAll(async () => {
await app.close();
});

describe("/content/schema/post", () => {
test("returns the post frontmatter JSON Schema", async () => {
const response = await app.inject({
method: "GET",
url: "/content/schema/post",
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual(PostMetaSchema);
});
});
});
10 changes: 10 additions & 0 deletions apps/api/src/routes/content/schema-post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PostMetaSchema } from "@playfulprogramming/common";
import { createSchemaRoute } from "./createSchemaRoute.ts";

const schemaPostRoutes = createSchemaRoute(
"/content/schema/post",
"Fetch the JSON Schema for post frontmatter, as validated by the sync worker.",
PostMetaSchema,
);

export default schemaPostRoutes;
3 changes: 1 addition & 2 deletions apps/worker/src/tasks/sync-author/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "@playfulprogramming/common";
import { env, AuthorMetaSchema } from "@playfulprogramming/common";
import { Tasks, createJob } from "@playfulprogramming/bullmq";
import {
db,
Expand All @@ -10,7 +10,6 @@ import * as github from "@playfulprogramming/github-api";
import { s3 } from "@playfulprogramming/s3";
import { createProcessor } from "../../createProcessor.ts";
import matter from "gray-matter";
import { AuthorMetaSchema } from "./types.ts";
import { Value } from "typebox/value";
import sharp from "sharp";
import { Readable } from "node:stream";
Expand Down
3 changes: 1 addition & 2 deletions apps/worker/src/tasks/sync-collection/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "@playfulprogramming/common";
import { env, CollectionMetaSchema } from "@playfulprogramming/common";
import { Tasks, createJob } from "@playfulprogramming/bullmq";
import {
collectionAuthors,
Expand All @@ -11,7 +11,6 @@ import * as github from "@playfulprogramming/github-api";
import { createProcessor } from "../../createProcessor.ts";
import { eq } from "drizzle-orm";
import matter from "gray-matter";
import { CollectionMetaSchema } from "./types.ts";
import { Value } from "typebox/value";
import sharp from "sharp";
import { Readable } from "node:stream";
Expand Down
3 changes: 1 addition & 2 deletions apps/worker/src/tasks/sync-post/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "@playfulprogramming/common";
import { env, PostMetaSchema } from "@playfulprogramming/common";
import { Tasks, createJob } from "@playfulprogramming/bullmq";
import {
db,
Expand All @@ -18,7 +18,6 @@ import sharp from "sharp";
import { Readable } from "node:stream";
import { extname } from "node:path/posix";
import { Response } from "undici";
import { PostMetaSchema } from "./types.ts";
import { extractLocale } from "../../utils/extractLocale.ts";
import { extractMarkdownExcerpt } from "../../utils/extractMarkdownExcerpt.ts";

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./env.ts";
export * from "./frontmatter/post.ts";
export * from "./frontmatter/collection.ts";
export * from "./frontmatter/author.ts";