Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
Improve api (#427)
Thibault Le Ouaycf439ed9d010
diff --git a/apps/server/package.json b/apps/server/package.jsonindex e6c82ee37..642e6b390 100644--- a/apps/server/package.json+++ b/apps/server/package.json@@ -7,11 +7,12 @@"scripts": {"dev": "bun run --hot src/index.ts","start": "NODE_ENV=production bun run src/index.ts",- "test": "vitest run"+ "test": "vitest run",+ "lint": "eslint \"**/*.ts*\""},"dependencies": {- "@hono/sentry": "^1.0.0",- "@hono/zod-openapi": "0.7.1",+ "@hono/sentry": "1.0.0",+ "@hono/zod-openapi": "0.8.3","@openstatus/db": "workspace:*","@openstatus/notification-discord": "workspace:*","@openstatus/notification-emails": "workspace:*",@@ -19,15 +20,16 @@"@openstatus/plans": "workspace:*","@openstatus/tinybird": "workspace:*","@openstatus/upstash": "workspace:*",- "@t3-oss/env-core": "0.7.0",- "@unkey/api": "0.10.0",- "hono": "3.7.6",- "nanoid": "5.0.1",+ "@t3-oss/env-core": "0.7.1",+ "@unkey/api": "0.11.0",+ "hono": "3.9.1",+ "nanoid": "5.0.2","zod": "3.22.2"},"devDependencies": {"@openstatus/tsconfig": "workspace:*","dotenv": "16.3.1",- "vitest": "0.34.6"+ "vitest": "0.34.6",+ "eslint": "8.50.0"}}diff --git a/apps/server/src/checker/checker.test.ts b/apps/server/src/checker/checker.test.tsindex 3f173955d..547acdf76 100644--- a/apps/server/src/checker/checker.test.ts+++ b/apps/server/src/checker/checker.test.ts@@ -1,3 +1,5 @@+/* eslint-disable @typescript-eslint/no-unused-vars */+/* eslint-disable @typescript-eslint/ban-ts-comment */import { expect, it, vi } from "vitest";// REMINDER: keep it here for the mockdiff --git a/apps/server/src/v1/incident.ts b/apps/server/src/v1/incident.tsindex 1f410b110..6b861ee8c 100644--- a/apps/server/src/v1/incident.ts+++ b/apps/server/src/v1/incident.ts@@ -50,6 +50,12 @@ const incidentSchema = z.object({const incidentExtendedSchema = incidentSchema.extend({id: z.number().openapi({ description: "The id of the incident" }),+ incident_updates: z+ .array(z.number())+ .openapi({+ description: "The ids of the incident updates",+ })+ .default([]),});const getAllRoute = createRoute({method: "get",@@ -75,18 +81,26 @@ const getAllRoute = createRoute({},},});+incidentApi.openapi(getAllRoute, async (c) => {const workspaceId = Number(c.get("workspaceId"));-- const _incidents = await db- .select()- .from(incident)- .where(eq(incident.workspaceId, workspaceId))- .all();+ const _incidents = await db.query.incident.findMany({+ with: {+ incidentUpdates: true,+ },+ where: eq(incident.workspaceId, workspaceId),+ });if (!_incidents) return c.jsonT({ code: 404, message: "Not Found" });- const data = z.array(incidentExtendedSchema).parse(_incidents);+ const data = z.array(incidentExtendedSchema).parse(+ _incidents.map((incident) => ({+ ...incident,+ incident_updates: incident.incidentUpdates.map((incidentUpdate) => {+ return incidentUpdate.id;+ }),+ })),+ );return c.jsonT(data);});@@ -117,23 +131,30 @@ const getRoute = createRoute({},},});+incidentApi.openapi(getRoute, async (c) => {const workspaceId = Number(c.get("workspaceId"));const { id } = c.req.valid("param");const incidentId = Number(id);- const _incident = await db- .select()- .from(incident)- .where(eq(incident.id, incidentId))- .get();+ const _incident = await db.query.incident.findFirst({+ with: {+ incidentUpdates: true,+ },+ where: and(+ eq(incident.workspaceId, workspaceId),+ eq(incident.id, incidentId),+ ),+ });if (!_incident) return c.jsonT({ code: 404, message: "Not Found" });-- if (workspaceId !== _incident.workspaceId)- return c.jsonT({ code: 401, message: "Unauthorized" });-- const data = incidentExtendedSchema.parse(_incident);+ console.log(_incident);+ const data = incidentExtendedSchema.parse({+ ..._incident,+ incident_updates: _incident.incidentUpdates.map(+ (incidentUpdate) => incidentUpdate.id,+ ),+ });return c.jsonT(data);});diff --git a/apps/server/src/v1/incidentUpdate.ts b/apps/server/src/v1/incidentUpdate.tsnew file mode 100644index 000000000..99dc32af3--- /dev/null+++ b/apps/server/src/v1/incidentUpdate.ts@@ -0,0 +1,102 @@+import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";++import { and, db, eq } from "@openstatus/db";+import {+ incident,+ incidentStatus,+ incidentUpdate,+} from "@openstatus/db/src/schema";++import type { Variables } from ".";+import { ErrorSchema } from "./shared";++const incidenUpdateApi = new OpenAPIHono<{ Variables: Variables }>();++const ParamsSelectSchema = z.object({+ id: z+ .string()+ .min(1)+ .openapi({+ param: {+ name: "incidentId",+ in: "path",+ },+ description: "The id of the update",+ example: "1",+ }),+});++const incidentUpdateSchema = z.object({+ status: z.enum(incidentStatus).openapi({+ description: "The status of the update",+ }),+ id: z.coerce.string().openapi({ description: "The id of the update" }),+ date: z+ .preprocess((val) => String(val), z.string())+ .openapi({+ description: "The date of the update in ISO 8601 format",+ }),+ message: z.string().openapi({+ description: "The message of the update",+ }),+});++// const incidentUpdateApi = new OpenAPIHono<{ Variables: Variables }>();++const getUpdateRoute = createRoute({+ method: "get",+ tags: ["incident_update"],+ path: "/:id",+ request: {+ params: ParamsSelectSchema,+ },+ responses: {+ 200: {+ content: {+ "application/json": {+ schema: z.object({}),+ },+ },+ description: "Get all incidents",+ },+ 400: {+ content: {+ "application/json": {+ schema: ErrorSchema,+ },+ },+ description: "Returns an error",+ },+ },+});++incidenUpdateApi.openapi(getUpdateRoute, async (c) => {+ const workspaceId = Number(c.get("workspaceId"));+ const { id } = c.req.valid("param");++ const update = await db+ .select()+ .from(incidentUpdate)+ .where(eq(incidentUpdate.id, Number(id)))+ .get();++ if (!update) return c.jsonT({ code: 404, message: "Not Found" });++ const currentIncident = await db+ .select()+ .from(incident)+ .where(+ and(+ eq(incident.id, update.incidentId),+ eq(incident.workspaceId, workspaceId),+ ),+ )+ .get();+ if (!currentIncident)+ return c.jsonT({ code: 401, message: "Not Authorized" });++ const data = incidentUpdateSchema.parse(update);+ return c.jsonT(data);+});++export { incidenUpdateApi as incidenUpdatetApi };diff --git a/apps/server/src/v1/index.ts b/apps/server/src/v1/index.tsindex 3922c0be0..800426324 100644--- a/apps/server/src/v1/index.ts+++ b/apps/server/src/v1/index.ts@@ -3,6 +3,7 @@ import { OpenAPIHono } from "@hono/zod-openapi";import type { Plan } from "@openstatus/plans";import { incidentApi } from "./incident";+import { incidenUpdatetApi } from "./incidentUpdate";import { middleware } from "./middleware";import { monitorApi } from "./monitor";@@ -27,4 +28,6 @@ api.doc("/openapi", {api.use("/*", middleware);api.route("/monitor", monitorApi);+api.route("/incident_update", incidenUpdatetApi);+api.route("/incident", incidentApi);diff --git a/apps/server/src/v1/middleware.ts b/apps/server/src/v1/middleware.tsindex 98032f77b..4664512ac 100644--- a/apps/server/src/v1/middleware.ts+++ b/apps/server/src/v1/middleware.ts@@ -10,7 +10,6 @@ export async function middleware() {const key = c.req.header("x-openstatus-key");if (!key) return c.text("Unauthorized", 401);-if (process.env.NODE_ENV === "production") {const { error, result } = await verifyKey(key);diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d6f097b2d..6ace0c9f6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -79,11 +79,11 @@ importers:apps/server:dependencies:'@hono/sentry':- specifier: ^1.0.0- version: 1.0.0([email protected])+ specifier: 1.0.0+ version: 1.0.0([email protected])'@hono/zod-openapi':- specifier: 0.7.1- version: 0.7.1([email protected])([email protected])+ specifier: 0.8.3+ version: 0.8.3([email protected])([email protected])'@openstatus/db':specifier: workspace:*version: link:../../packages/db@@ -106,17 +106,17 @@ importers:specifier: workspace:*version: link:../../packages/upstash'@t3-oss/env-core':- specifier: 0.7.0- version: 0.7.0([email protected])([email protected])+ specifier: 0.7.1+ version: 0.7.1([email protected])([email protected])'@unkey/api':- specifier: 0.10.0- version: 0.10.0+ specifier: 0.11.0+ version: 0.11.0hono:- specifier: 3.7.6- version: 3.7.6+ specifier: 3.9.1+ version: 3.9.1nanoid:- specifier: 5.0.1- version: 5.0.1+ specifier: 5.0.2+ version: 5.0.2zod:specifier: 3.22.2version: 3.22.2@@ -127,6 +127,9 @@ importers:dotenv:specifier: 16.3.1version: 16.3.1+ eslint:+ specifier: 8.50.0+ version: 8.50.0vitest:specifier: 0.34.6version: 0.34.6@@ -2258,35 +2261,35 @@ packages:tailwindcss: 3.3.2dev: false- /@hono/[email protected]([email protected]):+ /@hono/[email protected]([email protected]):resolution: {integrity: sha512-GbPxgpGuasM2zRCSaA77MPWu4KDcuk/EMf7JJykjCvnOTbjmtr7FovNxsvg7xlXCIjZDgLmqBaoJMi3AxbeIAA==}peerDependencies:hono: 3.*dependencies:- hono: 3.7.6+ hono: 3.9.1toucan-js: 3.3.0dev: false- resolution: {integrity: sha512-zs6AURALRF0A1gkuY5iKmRBT7d0JsLC3xVPDjBdRbJqNV3XQv4i2kprZTRAcI9AjjGApTsZbLZv1pZRCB2viwQ==}+ resolution: {integrity: sha512-TwjSf63miIF3VBqNy4V6EOqZYdTQPeIQQ9Q2HjRWFYH9f2QtBFXziuqIg2Y76gNZ3WDal67NQXmbSErHiFR44g==}engines: {node: '>=16.0.0'}peerDependencies:- hono: '*'+ hono: '>=3.9.0'zod: 3.*dependencies:'@asteasolutions/zod-to-openapi': 5.5.0([email protected])- '@hono/zod-validator': 0.1.9([email protected])([email protected])- hono: 3.7.6+ '@hono/zod-validator': 0.1.9([email protected])([email protected])+ hono: 3.9.1zod: 3.22.2dev: falseresolution: {integrity: sha512-qEG5xagKzyif283ldCKzp+aF9Aebclg0sfrgyRQQNAizmXpicZ3UGduST/Jp+a9bjt3mI+VyEXMftb4rogLxQA==}peerDependencies:hono: 3.*zod: ^3.19.1dependencies:- hono: 3.7.6+ hono: 3.9.1zod: 3.22.2dev: false@@ -5262,6 +5265,19 @@ packages:zod: 3.22.2dev: false+ resolution: {integrity: sha512-3+SQt39OlmSaRLqYVFv8uRm1BpFepM5TIiMytRqO9cjH+wB77o6BIJdeyM5h5U4qLBMEzOJWCY4MBaU/rLwbYw==}+ peerDependencies:+ typescript: '>=4.7.2'+ zod: ^3.0.0+ peerDependenciesMeta:+ typescript:+ optional: true+ dependencies:+ typescript: 5.2.2+ zod: 3.22.2+ dev: false+resolution: {integrity: sha512-rjQIt6P3tac2eRx4BNQLNaJ+AIb2P8wXw4uFvYbEekqMGShikkUALnX3hUn1twYiGVGHXRm6UbU+LqtjIktuGg==}peerDependencies:@@ -5993,6 +6009,10 @@ packages:resolution: {integrity: sha512-ix1XQqZPuBlNkimQniqNjwpqduGb/hBm5LAFRgPfotY97IGUcsKGV3YGl7GAnqFB9wa31OeOT/yeSbEgXiQJWw==}dev: false+ /@unkey/[email protected]:+ resolution: {integrity: sha512-pJRuvRWFAnp6qGryvtI5+J95rtIHUCnTOw0bQTewr2+qFNLv/UimEIlR8Ef6kln/5evc3C94yPjFy59nuCMoWQ==}+ dev: false+/@upstash/[email protected]:resolution: {integrity: sha512-cpPSR0XJAJs4Ddz9nq3tINlPS5aLfWVCqhhtHnXt4p7qr5+/Znlt1Es736poB/9rnl1hAHrOsOvVj46NEXcVqA==}engines: {node: '>=16.0.0'}@@ -9531,8 +9551,8 @@ packages:react-is: 16.13.1dev: false- resolution: {integrity: sha512-nuLNH9+nV6ojXK6b9I0RGZgdMuLTOXeQPs6xsIw/G5iyT8j8m7Nnx8pTxQprmSmCaEYIPv91rCcq45PQNfW68A==}+ resolution: {integrity: sha512-z3nM9CjNZ8PRAH6NNntk4ESKW2POEbGanhK1QpYdQ1MOYRzZPSEE8B5mqw8bYEPa7qIQxX0vtlv7XOxtwFbosg==}engines: {node: '>=16.0.0'}dev: false@@ -11688,6 +11708,12 @@ packages:hasBin: truedev: false+ resolution: {integrity: sha512-2ustYUX1R2rL/Br5B/FMhi8d5/QzvkJ912rBYxskcpu0myTHzSZfTr1LAS2Sm7jxRUObRrSBFoyzwAhL49aVSg==}+ engines: {node: ^18 || >=20}+ hasBin: true+ dev: false+resolution: {integrity: sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg==}dev: falsediff --git a/utils/api-bruno/OpenApi.bru b/utils/api-bruno/OpenApi.brunew file mode 100644index 000000000..69246b5be--- /dev/null+++ b/utils/api-bruno/OpenApi.bru@@ -0,0 +1,11 @@+meta {+ name: OpenApi+ type: http+ seq: 1+}++get {+ url: {{url}}/v1/openapi+ body: none+ auth: none+}diff --git a/utils/api-bruno/bruno.json b/utils/api-bruno/bruno.jsonnew file mode 100644index 000000000..8dfd8c7aa--- /dev/null+++ b/utils/api-bruno/bruno.json@@ -0,0 +1,5 @@+{+ "version": "1",+ "name": "OpenStatus",+ "type": "collection"+}\ No newline at end of filediff --git a/utils/api-bruno/environments/local.bru b/utils/api-bruno/environments/local.brunew file mode 100644index 000000000..5c0f373dc--- /dev/null+++ b/utils/api-bruno/environments/local.bru@@ -0,0 +1,3 @@+vars {+ url: http://localhost:3000+}diff --git a/utils/api-bruno/environments/prod.bru b/utils/api-bruno/environments/prod.brunew file mode 100644index 000000000..896e47370--- /dev/null+++ b/utils/api-bruno/environments/prod.bru@@ -0,0 +1,3 @@+vars {+ url: https://api.openstatus.dev+}diff --git a/utils/api-bruno/incident_update/Get Incident Update.bru b/utils/api-bruno/incident_update/Get Incident Update.brunew file mode 100644index 000000000..2a25d654b--- /dev/null+++ b/utils/api-bruno/incident_update/Get Incident Update.bru@@ -0,0 +1,19 @@+meta {+ name: Get Incident Update+ type: http+ seq: 1+}++get {+ url: {{url}}/v1/incident_update/{{updateId}}+ body: none+ auth: none+}++headers {+ x-openstatus-key: api-keu+}++vars:pre-request {+ updateId: 32+}diff --git a/utils/api-bruno/incidents/All incidents.bru b/utils/api-bruno/incidents/All incidents.brunew file mode 100644index 000000000..a1024ddcf--- /dev/null+++ b/utils/api-bruno/incidents/All incidents.bru@@ -0,0 +1,15 @@+meta {+ name: All incidents+ type: http+ seq: 1+}++get {+ url: {{url}}/v1/incident+ body: none+ auth: none+}++headers {+ x-openstatus-key: api-key+}diff --git a/utils/api-bruno/incidents/Get on incident.bru b/utils/api-bruno/incidents/Get on incident.brunew file mode 100644index 000000000..1bbc7d626--- /dev/null+++ b/utils/api-bruno/incidents/Get on incident.bru@@ -0,0 +1,19 @@+meta {+ name: Get on incident+ type: http+ seq: 2+}++get {+ url: {{url}}/v1/incident/{{id}}+ body: none+ auth: none+}++headers {+ x-openstatus-key: api-key+}++vars:pre-request {+ id: 31+}