Skip to content

Build your personal knowledge base with Trilium Notes

docs(etapi): document the week-first-day calendar route

Elian Doran1c32a2470fe0
diff --git a/apps/server/spec/etapi/openapi-spec.spec.ts b/apps/server/spec/etapi/openapi-spec.spec.ts
index 63c3b0db09..cef5776cb1 100644
--- a/apps/server/spec/etapi/openapi-spec.spec.ts
+++ b/apps/server/spec/etapi/openapi-spec.spec.ts
@@ -1,11 +1,28 @@
-import { Application } from "express";
+import { Application, Router } from "express";
+import { load } from "js-yaml";
import { beforeAll, describe, expect, it } from "vitest";
import supertest from "supertest";
import config from "../../src/services/config.js";
+import etapiAppInfoRoutes from "../../src/etapi/app_info.js";
+import etapiAttachmentRoutes from "../../src/etapi/attachments.js";
+import etapiAttributeRoutes from "../../src/etapi/attributes.js";
+import etapiAuthRoutes from "../../src/etapi/auth.js";
+import etapiBackupRoute from "../../src/etapi/backup.js";
+import etapiBranchRoutes from "../../src/etapi/branches.js";
+import etapiNoteRoutes from "../../src/etapi/notes.js";
+import etapiRevisionsRoutes from "../../src/etapi/revisions.js";
+import etapiSpecialNoteRoutes from "../../src/etapi/special_notes.js";
let app: Application;
+/**
+ * Endpoints served under /etapi that the specification deliberately leaves out: it cannot
+ * usefully describe itself, and /etapi/metrics is an operational endpoint rather than part
+ * of the note API. Everything else must be documented.
+ */
+const UNDOCUMENTED_ROUTES = new Set(["/etapi.openapi.yaml", "/metrics"]);
+
describe("etapi/etapi.openapi.yaml", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
@@ -22,4 +39,57 @@ describe("etapi/etapi.openapi.yaml", () => {
const cached = await supertest(app).get("/etapi/etapi.openapi.yaml").expect(200);
expect(cached.text).toStrictEqual(response.text);
});
+
+ it("documents every route registered under /etapi", async () => {
+ const response = await supertest(app).get("/etapi/etapi.openapi.yaml").expect(200);
+ const spec = load(response.text) as { paths: Record<string, Record<string, unknown>> };
+
+ const undocumented = collectEtapiRoutes().filter(({ method, path }) => {
+ if (UNDOCUMENTED_ROUTES.has(path)) {
+ return false;
+ }
+ return !spec.paths[path]?.[method];
+ });
+
+ expect(undocumented).toStrictEqual([]);
+ });
});
+
+/**
+ * Registers the ETAPI route modules onto a bare router and reads back what they mounted, as
+ * `{ method, path }` pairs addressed the way the specification addresses them: without the
+ * /etapi prefix and with `:date` rewritten to `{date}`.
+ */
+function collectEtapiRoutes() {
+ const router = Router();
+
+ etapiAuthRoutes.register(router, []);
+ etapiAppInfoRoutes.register(router);
+ etapiAttachmentRoutes.register(router);
+ etapiAttributeRoutes.register(router);
+ etapiBackupRoute.register(router);
+ etapiBranchRoutes.register(router);
+ etapiNoteRoutes.register(router);
+ etapiRevisionsRoutes.register(router);
+ etapiSpecialNoteRoutes.register(router);
+
+ const routes: { method: string; path: string }[] = [];
+
+ for (const layer of router.stack) {
+ // Express exposes the verbs a route was mounted with, but its types don't declare them.
+ const route = layer.route as (typeof layer.route & { methods: Record<string, boolean> }) | undefined;
+ if (!route?.path.startsWith("/etapi/")) {
+ continue;
+ }
+
+ const path = route.path.replace("/etapi", "").replace(/:(\w+)/g, "{$1}");
+ for (const [method, enabled] of Object.entries(route.methods)) {
+ if (enabled && method !== "_all") {
+ routes.push({ method, path });
+ }
+ }
+ }
+
+ expect(routes.length).toBeGreaterThan(0);
+ return routes;
+}
diff --git a/apps/server/src/assets/etapi.openapi.yaml b/apps/server/src/assets/etapi.openapi.yaml
index 6cee42d7e7..486a901e7a 100644
--- a/apps/server/src/assets/etapi.openapi.yaml
+++ b/apps/server/src/assets/etapi.openapi.yaml
@@ -843,6 +843,37 @@ paths:
application/json; charset=utf-8:
schema:
$ref: "#/components/schemas/Error"
+ /calendar/week-first-day/{date}:
+ get:
+ summary: Get the day note starting the week
+ description: |
+ Returns the day note for the first day of the week containing the given date, honouring
+ the configured first day of week. The note is created if it doesn't exist. This returns a
+ day note, not a week note, so unlike /calendar/weeks/{week} it does not require the
+ enableWeekNote label on the calendar root.
+ operationId: getWeekFirstDayNote
+ parameters:
+ - name: date
+ in: path
+ required: true
+ description: Any date within the week, in the format YYYY-MM-DD.
+ schema:
+ type: string
+ format: date
+ example: 2022-02-22
+ responses:
+ "200":
+ description: day note starting the week
+ content:
+ application/json; charset=utf-8:
+ schema:
+ $ref: "#/components/schemas/Note"
+ default:
+ description: unexpected error
+ content:
+ application/json; charset=utf-8:
+ schema:
+ $ref: "#/components/schemas/Error"
/calendar/weeks/{week}:
get:
summary: Get a week note