From 54412a3364b1dc7f0703fe724a638ae17d7c4142 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 18 Sep 2021 01:49:17 +0200 Subject: :pencil: add default route description to all routes --- api/src/routes/users/@me/applications/#app_id/entitlements.ts | 5 +++-- api/src/routes/users/@me/billing/country-code.ts | 7 ++++--- api/src/routes/users/@me/billing/subscriptions.ts | 5 +++-- api/src/routes/users/@me/index.ts | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) (limited to 'api/src/routes/users') diff --git a/api/src/routes/users/@me/applications/#app_id/entitlements.ts b/api/src/routes/users/@me/applications/#app_id/entitlements.ts index e4fbe1e4..411e95bf 100644 --- a/api/src/routes/users/@me/applications/#app_id/entitlements.ts +++ b/api/src/routes/users/@me/applications/#app_id/entitlements.ts @@ -1,10 +1,11 @@ import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; const router: Router = Router(); -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({}), async (req: Request, res: Response) => { //TODO res.json([]).status(200); }); -export default router; \ No newline at end of file +export default router; diff --git a/api/src/routes/users/@me/billing/country-code.ts b/api/src/routes/users/@me/billing/country-code.ts index ac3653a2..33d40796 100644 --- a/api/src/routes/users/@me/billing/country-code.ts +++ b/api/src/routes/users/@me/billing/country-code.ts @@ -1,10 +1,11 @@ import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; const router: Router = Router(); -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({}), async (req: Request, res: Response) => { //TODO - res.json({ "country_code": "US" }).status(200); + res.json({ country_code: "US" }).status(200); }); -export default router; \ No newline at end of file +export default router; diff --git a/api/src/routes/users/@me/billing/subscriptions.ts b/api/src/routes/users/@me/billing/subscriptions.ts index e4fbe1e4..411e95bf 100644 --- a/api/src/routes/users/@me/billing/subscriptions.ts +++ b/api/src/routes/users/@me/billing/subscriptions.ts @@ -1,10 +1,11 @@ import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; const router: Router = Router(); -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({}), async (req: Request, res: Response) => { //TODO res.json([]).status(200); }); -export default router; \ No newline at end of file +export default router; diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index da2f3348..67b11ce0 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -23,7 +23,7 @@ export interface UserModifySchema { code?: string; } -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({}), async (req: Request, res: Response) => { res.json(await User.findOne({ select: PrivateUserProjection, where: { id: req.user_id } })); }); -- cgit 1.5.1 From a3484e64e45764775c814739efe73759d5bdeaeb Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 18 Sep 2021 11:56:06 +0200 Subject: :sparkles: route middleware test option --- api/jest/getRouteDescriptions.ts | 6 ++++-- api/src/routes/users/#id/profile.ts | 2 +- api/src/util/route.ts | 6 +++--- api/tests/routes.test.ts | 11 ++++------- 4 files changed, 12 insertions(+), 13 deletions(-) (limited to 'api/src/routes/users') diff --git a/api/jest/getRouteDescriptions.ts b/api/jest/getRouteDescriptions.ts index d7d6e0c6..33922899 100644 --- a/api/jest/getRouteDescriptions.ts +++ b/api/jest/getRouteDescriptions.ts @@ -2,6 +2,7 @@ import { traverseDirectory } from "lambert-server"; import path from "path"; import express from "express"; import * as RouteUtility from "../dist/util/route"; +import { RouteOptions } from "../dist/util/route"; const Router = express.Router; const routes = new Map(); @@ -12,9 +13,10 @@ const methods = ["get", "post", "put", "delete", "patch"]; function registerPath(file, method, prefix, path, ...args) { const urlPath = prefix + path; const sourceFile = file.replace("/dist/", "/src/").replace(".js", ".ts"); - const opts = args.find((x) => typeof x === "object"); + const opts: RouteOptions = args.find((x) => typeof x === "object"); if (opts) { - routes.set(urlPath + "|" + method, opts); + routes.set(urlPath + "|" + method, opts); // @ts-ignore + opts.file = sourceFile; // console.log(method, urlPath, opts); } else { console.log(`${sourceFile}\nrouter.${method}("${path}") is missing the "route()" description middleware\n`, args); diff --git a/api/src/routes/users/#id/profile.ts b/api/src/routes/users/#id/profile.ts index d60c4f86..d099bce7 100644 --- a/api/src/routes/users/#id/profile.ts +++ b/api/src/routes/users/#id/profile.ts @@ -11,7 +11,7 @@ export interface UserProfileResponse { premium_since?: Date; } -router.get("/", route({ response: { body: "UserProfileResponse" } }), async (req: Request, res: Response) => { +router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }), async (req: Request, res: Response) => { if (req.params.id === "@me") req.params.id = req.user_id; const user = await User.getPublicUser(req.params.id, { relations: ["connected_accounts"] }); diff --git a/api/src/util/route.ts b/api/src/util/route.ts index 35ea43ba..9ef92c3a 100644 --- a/api/src/util/route.ts +++ b/api/src/util/route.ts @@ -33,11 +33,11 @@ export type RouteResponse = { status?: number; body?: `${string}Response`; heade export interface RouteOptions { permission?: PermissionResolvable; body?: `${string}Schema`; // typescript interface name - response?: RouteResponse; - example?: { + test?: { + response?: RouteResponse; body?: any; path?: string; - event?: EventData; + event?: EventData | EventData[]; headers?: Record; }; } diff --git a/api/tests/routes.test.ts b/api/tests/routes.test.ts index e913e0dc..4cb7e6bc 100644 --- a/api/tests/routes.test.ts +++ b/api/tests/routes.test.ts @@ -3,13 +3,13 @@ import getRouteDescriptions from "../jest/getRouteDescriptions"; import supertest, { Response } from "supertest"; -import path from "path"; +import { join } from "path"; import fs from "fs"; import Ajv from "ajv"; import addFormats from "ajv-formats"; const request = supertest("http://localhost:3001/api"); -const SchemaPath = path.join(__dirname, "..", "assets", "responses.json"); +const SchemaPath = join(__dirname, "..", "assets", "responses.json"); const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" })); export const ajv = new Ajv({ allErrors: true, @@ -27,13 +27,10 @@ describe("Automatic unit tests with route description middleware", () => { routes.forEach((route, pathAndMethod) => { const [path, method] = pathAndMethod.split("|"); + test(path, (done) => { if (!route.example) { - console.log(`Route ${path} is missing the example property`); - return done(); - } - if (!route.response) { - console.log(`Route ${path} is missing the response property`); + console.log(`${(route as any).file}\nrouter.${method} is missing the test property`); return done(); } const urlPath = path || route.example?.path; -- cgit 1.5.1