Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

๐Ÿš€ Update incident and monitor endpoints (#443)

* ๐Ÿš€ Update incident and monitor endpoints

* ๐Ÿงช improve api and test

* ๐Ÿงชfix tests

* ๐Ÿงน

authored by

Thibault Le Ouay and committed by
GitHub
0a811248 030bbfdb

+85 -56
+1 -1
apps/docs/api-reference/incident/post-incident:idupdate.mdx
··· 1 1 --- 2 - title: Update Incident 2 + title: Create Incident Update 3 3 openapi: post /incident/:id/update 4 4 ---
+1 -1
apps/server/src/v1/incidentUpdate.ts
··· 54 54 200: { 55 55 content: { 56 56 "application/json": { 57 - schema: z.object({}), 57 + schema: incidentUpdateSchema, 58 58 }, 59 59 }, 60 60 description: "Get all incidents",
+36 -26
apps/server/src/v1/monitor.test.ts
··· 14 14 id: 1, 15 15 periodicity: "1m", 16 16 url: "https://www.openstatus.dev", 17 - regions: "ams", 17 + regions: ["ams"], 18 18 name: "OpenStatus", 19 19 description: "OpenStatus website", 20 20 method: "POST", ··· 31 31 }, 32 32 }); 33 33 expect(res.status).toBe(200); 34 + }); 34 35 35 - expect(await res.json()).toMatchObject([ 36 - { 37 - id: 1, 38 - periodicity: "1m", 39 - url: "https://www.openstatus.dev", 40 - regions: "ams", 41 - name: "OpenStatus", 42 - description: "OpenStatus website", 43 - method: "POST", 44 - body: '{"hello":"world"}', 45 - headers: [{ key: "key", value: "value" }], 46 - active: true, 47 - }, 48 - { 49 - active: false, 50 - body: "", 51 - description: "", 52 - headers: [], 53 - id: 2, 54 - method: "GET", 55 - name: "", 56 - periodicity: "10m", 57 - regions: "gru", 58 - url: "https://www.google.com", 36 + test("Create a monitor", async () => { 37 + const data = { 38 + periodicity: "10m", 39 + url: "https://www.openstatus.dev", 40 + name: "OpenStatus", 41 + description: "OpenStatus website", 42 + regions: ["ams", "gru"], 43 + method: "POST", 44 + body: '{"hello":"world"}', 45 + headers: [{ key: "key", value: "value" }], 46 + active: true, 47 + }; 48 + const res = await api.request("/monitor", { 49 + method: "POST", 50 + headers: { 51 + "x-openstatus-key": "1", 52 + "content-type": "application/json", 59 53 }, 60 - ]); 54 + body: JSON.stringify(data), 55 + }); 56 + // console.log(await res.text()); 57 + expect(res.status).toBe(200); 58 + 59 + expect(await res.json()).toMatchObject({ 60 + id: expect.any(Number), 61 + periodicity: "10m", 62 + url: "https://www.openstatus.dev", 63 + regions: ["ams", "gru"], 64 + name: "OpenStatus", 65 + description: "OpenStatus website", 66 + method: "POST", 67 + body: '{"hello":"world"}', 68 + headers: [{ key: "key", value: "value" }], 69 + active: true, 70 + }); 61 71 });
+19 -12
apps/server/src/v1/monitor.ts
··· 26 26 }); 27 27 28 28 export const periodicityEnum = z.enum(monitorPeriodicity); 29 - export const regionEnum = z 30 - .enum(flyRegions) 31 - .or(z.literal("")) 32 - .transform((val) => (val === "" ? "" : val)); 29 + 30 + const regionInput = z.array(z.enum(flyRegions)).transform((val) => String(val)); 33 31 34 32 const MonitorSchema = z 35 33 .object({ ··· 45 43 example: "https://www.documenso.co", 46 44 description: "The url to monitor", 47 45 }), 48 - regions: regionEnum.openapi({ 49 - example: "ams", 50 - description: "The regions to use", 51 - }), 46 + regions: z 47 + .preprocess( 48 + (val) => { 49 + if (String(val).length > 0) { 50 + return String(val).split(","); 51 + } else { 52 + return []; 53 + } 54 + }, 55 + z.array(z.enum(flyRegions)), 56 + ) 57 + .default([]) 58 + .openapi({ 59 + example: ["ams"], 60 + description: "The regions to use", 61 + }), 52 62 name: z 53 63 .string() 54 64 .openapi({ ··· 108 118 example: "https://www.documenso.co", 109 119 description: "The url to monitor", 110 120 }), 111 - regions: regionEnum.openapi({ 121 + regions: regionInput.openapi({ 112 122 example: "ams", 113 123 description: "The regions to use", 114 124 }), ··· 240 250 const { id } = c.req.valid("param"); 241 251 242 252 const monitorId = Number(id); 243 - console.log({ monitorId, workspaceId }); 244 253 const _monitor = await db 245 254 .select() 246 255 .from(monitor) ··· 303 312 monitorApi.openapi(postRoute, async (c) => { 304 313 const workspaceId = Number(c.get("workspaceId")); 305 314 const workspacePlan = c.get("workspacePlan"); 306 - console.log({ workspaceId }); 307 315 const input = c.req.valid("json"); 308 316 309 317 const count = ( ··· 312 320 313 321 if (count >= workspacePlan.limits.monitors) 314 322 return c.jsonT({ code: 403, message: "Forbidden" }); 315 - 316 323 if (!workspacePlan.limits.periodicity.includes(input.periodicity)) 317 324 return c.jsonT({ code: 403, message: "Forbidden" }); 318 325
+6 -6
packages/api/src/router/workspace.test.ts
··· 24 24 id: 1, 25 25 slug: "test", 26 26 name: "test", 27 - plan: "free", 27 + plan: "pro", 28 28 paidUntil: null, 29 - stripeId: "stripeId", 29 + stripeId: "stripeId1", 30 30 subscriptionId: "subscriptionId", 31 31 updatedAt: expect.any(Date), 32 32 createdAt: expect.any(Date), ··· 52 52 id: 1, 53 53 slug: "test", 54 54 name: "test", 55 - plan: "free", 55 + plan: "pro", 56 56 paidUntil: null, 57 - stripeId: "stripeId", 57 + stripeId: "stripeId1", 58 58 subscriptionId: "subscriptionId", 59 59 updatedAt: expect.any(Date), 60 60 createdAt: expect.any(Date), ··· 93 93 id: 1, 94 94 name: "test", 95 95 paidUntil: null, 96 - plan: "free", 96 + plan: "pro", 97 97 slug: "test", 98 - stripeId: "stripeId", 98 + stripeId: "stripeId1", 99 99 subscriptionId: "subscriptionId", 100 100 updatedAt: expect.any(Date), 101 101 },
+22 -10
packages/db/src/seed.mts
··· 22 22 console.log("Seeding database "); 23 23 await db 24 24 .insert(workspace) 25 - .values({ 26 - id: 1, 27 - slug: "test", 28 - stripeId: "stripeId", 29 - name: "test", 30 - subscriptionId: "subscriptionId", 31 - plan: "free", 32 - endsAt: null, 33 - paidUntil: null, 34 - }) 25 + .values([ 26 + { 27 + id: 1, 28 + slug: "test", 29 + stripeId: "stripeId1", 30 + name: "test", 31 + subscriptionId: "subscriptionId", 32 + plan: "pro", 33 + endsAt: null, 34 + paidUntil: null, 35 + }, 36 + { 37 + id: 2, 38 + slug: "test2", 39 + stripeId: "stripeId2", 40 + name: "test2", 41 + subscriptionId: "subscriptionId2", 42 + plan: "free", 43 + endsAt: null, 44 + paidUntil: null, 45 + }, 46 + ]) 35 47 .run(); 36 48 await db 37 49 .insert(monitor)