Skip to content

Commit

Permalink
Add specification to version service
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Sep 13, 2024
1 parent 22a85ad commit 3cb981b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
12 changes: 8 additions & 4 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ describe("sanity tests for app", () => {
it("should generate valid API specification", async () => {
await get("/docs/").expect(StatusCode.SuccessOK);
const response = await get("/docs/json/").expect(StatusCode.SuccessOK);
const parsed = JSON.parse(response.text);

expect(JSON.parse(response.text)).toHaveProperty(
"info",
expect.objectContaining({
expect(parsed).toMatchObject({
info: expect.objectContaining({
title: "adonix",
}),
);
paths: expect.objectContaining({}),
components: expect.objectContaining({}),
});
expect(Object.keys((parsed as { paths: Record<string, unknown> }).paths)).not.toHaveLength(0);
expect(Object.keys((parsed as { components: Record<string, unknown> }).components)).not.toHaveLength(0);
});
});
74 changes: 40 additions & 34 deletions src/services/version/version-router.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
import { Router } from "express";
import { Request, Response } from "express-serve-static-core";
import Metadata from "../../metadata";
import { StatusCode } from "status-code-enum";
import specification, { Tag } from "../../middleware/specification";
import { versionResponseSchema } from "./version-schema";

const versionRouter = Router();

/**
* @api {get} /version/android/ GET /version/android/
* @apiGroup Version
* @apiDescription Get the latest Android app version.
*
* @apiSuccess (200: Success) {Json} version The latest Android app version.
* @apiSuccessExample Example Success Response
* HTTP/1.1 200 OK
* {
* "version": "2024.1.1"
* }
*/
versionRouter.get(
"/android/",
specification({
method: "get",
path: "/version/android/",
tag: Tag.VERSION,
summary: "Gets the current android version",
responses: {
[StatusCode.SuccessOK]: {
description: "The current version",
schema: versionResponseSchema,
},
},
}),
async (_, res) => {
const androidVersion = await Metadata.load("androidVersion");
res.status(StatusCode.SuccessOK).send({ version: androidVersion });
},
);

versionRouter.get("/android/", async (_: Request, res: Response) => {
const androidVersion = await Metadata.load("androidVersion");
res.status(StatusCode.SuccessOK).send({ version: androidVersion });
});

/**
* @api {get} /version/ios/ GET /version/ios/
* @apiGroup Version
* @apiDescription Get the latest iOS app version.
*
* @apiSuccess (200: Success) {Json} version The latest iOS app version.
* @apiSuccessExample Example Success Response
* HTTP/1.1 200 OK
* {
* "version": "2024.1.1"
* }
*/
versionRouter.get("/ios/", async (_: Request, res: Response) => {
const iosVersion = await Metadata.load("iosVersion");
res.status(StatusCode.SuccessOK).send({ version: iosVersion });
});
versionRouter.get(
"/ios/",
specification({
method: "get",
path: "/version/ios/",
tag: Tag.VERSION,
summary: "Gets the current ios version",
responses: {
[StatusCode.SuccessOK]: {
description: "The current version",
schema: versionResponseSchema,
},
},
}),
async (_, res) => {
const iosVersion = await Metadata.load("iosVersion");
res.status(StatusCode.SuccessOK).send({ version: iosVersion });
},
);

export default versionRouter;
7 changes: 7 additions & 0 deletions src/services/version/version-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from "zod";

export const versionResponseSchema = z
.object({
version: z.string().openapi({ description: "The version", example: "2024.2.4" }),
})
.openapi("VersionResponse");

0 comments on commit 3cb981b

Please sign in to comment.