Openstatus www.openstatus.dev
6
fork

Configure Feed

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

Fix api update (#1287)

* ๐Ÿ›

* ๐Ÿ›

* ๐Ÿš€

* ci: apply automated fixes

* ๐Ÿ˜‚ remove test

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Thibault Le Ouay
autofix-ci[bot]
and committed by
GitHub
b0058e30 5679c6c4

+101 -56
-15
apps/server/src/routes/v1/monitors/put.test.ts
··· 22 22 expect(result.data?.name).toBe("New Name"); 23 23 }); 24 24 25 - test("update the monitor with a different jobType should return 400", async () => { 26 - const res = await app.request("/v1/monitor/1", { 27 - method: "PUT", 28 - headers: { 29 - "x-openstatus-key": "1", 30 - "Content-Type": "application/json", 31 - }, 32 - body: JSON.stringify({ 33 - jobType: "tcp", 34 - }), 35 - }); 36 - 37 - expect(res.status).toBe(400); 38 - }); 39 - 40 25 test("invalid monitor id should return 404", async () => { 41 26 const res = await app.request("/v1/page/404", { 42 27 method: "PUT",
+80 -28
apps/server/src/routes/v1/monitors/put.ts
··· 8 8 import { Events } from "@openstatus/analytics"; 9 9 import { serialize } from "@openstatus/assertions"; 10 10 import type { monitorsApi } from "./index"; 11 - import { MonitorSchema, ParamsSchema } from "./schema"; 12 - import { getAssertions } from "./utils"; 11 + import { 12 + HTTPMonitorSchema, 13 + MonitorSchema, 14 + ParamsSchema, 15 + TCPMonitorSchema, 16 + } from "./schema"; 17 + import { getAssertionNew } from "./utils"; 13 18 14 19 const putRoute = createRoute({ 15 20 method: "put", ··· 23 28 description: "The monitor to update", 24 29 content: { 25 30 "application/json": { 26 - schema: MonitorSchema.omit({ id: true }).partial(), 31 + schema: HTTPMonitorSchema.partial().or(TCPMonitorSchema.partial()), 27 32 }, 28 33 }, 29 34 }, ··· 48 53 const { id } = c.req.valid("param"); 49 54 const input = c.req.valid("json"); 50 55 51 - if (input.periodicity && !limits.periodicity.includes(input.periodicity)) { 56 + if (input.frequency && !limits.periodicity.includes(input.frequency)) { 52 57 throw new OpenStatusApiError({ 53 58 code: "PAYMENT_REQUIRED", 54 59 message: "Upgrade for more periodicity", ··· 85 90 }); 86 91 } 87 92 88 - if (input.jobType && input.jobType !== _monitor.jobType) { 89 - throw new OpenStatusApiError({ 90 - code: "BAD_REQUEST", 91 - message: 92 - "Cannot change jobType. Please delete and create a new monitor instead.", 93 - }); 94 - } 93 + if (_monitor.jobType === "http") { 94 + const data = HTTPMonitorSchema.partial().parse(input); 95 + const { request, regions, assertions, otelHeaders, ...rest } = data; 95 96 96 - const { headers, regions, assertions, ...rest } = input; 97 + const headers = data?.request?.headers 98 + ? Object.entries(data?.request.headers) 99 + : undefined; 97 100 98 - const assert = assertions ? getAssertions(assertions) : []; 101 + const otelHeadersEntries = otelHeaders 102 + ? Object.entries(otelHeaders).map(([key, value]) => ({ 103 + key: key, 104 + value: value, 105 + })) 106 + : undefined; 107 + const headersEntries = headers 108 + ? headers.map(([key, value]) => ({ key: key, value: value })) 109 + : undefined; 99 110 100 - const _newMonitor = await db 101 - .update(monitor) 102 - .set({ 103 - ...rest, 104 - regions: regions ? regions.join(",") : undefined, 105 - headers: input.headers ? JSON.stringify(input.headers) : undefined, 106 - assertions: assert.length > 0 ? serialize(assert) : undefined, 107 - timeout: input.timeout || 45000, 108 - updatedAt: new Date(), 109 - }) 110 - .where(eq(monitor.id, Number(_monitor.id))) 111 - .returning() 112 - .get(); 111 + const assert = assertions ? getAssertionNew(assertions) : []; 113 112 114 - const data = MonitorSchema.parse(_newMonitor); 115 - return c.json(data, 200); 113 + const _newMonitor = await db 114 + .update(monitor) 115 + .set({ 116 + ...rest, 117 + regions: regions ? regions.join(",") : _monitor.regions, 118 + headers: headersEntries 119 + ? JSON.stringify(headersEntries) 120 + : _monitor.headers, 121 + otelHeaders: otelHeadersEntries 122 + ? JSON.stringify(otelHeadersEntries) 123 + : _monitor.otelHeaders, 124 + assertions: 125 + assert.length > 0 ? serialize(assert) : _monitor.assertions, 126 + timeout: input.timeout || 45000, 127 + updatedAt: new Date(), 128 + }) 129 + .where(eq(monitor.id, Number(_monitor.id))) 130 + .returning() 131 + .get(); 132 + const r = MonitorSchema.parse(_newMonitor); 133 + return c.json(r, 200); 134 + } 135 + if (_monitor.jobType === "tcp") { 136 + const data = TCPMonitorSchema.partial().parse(input); 137 + const { request, regions, otelHeaders, ...rest } = data; 138 + 139 + const otelHeadersEntries = otelHeaders 140 + ? Object.entries(otelHeaders).map(([key, value]) => ({ 141 + key: key, 142 + value: value, 143 + })) 144 + : undefined; 145 + 146 + const _newMonitor = await db 147 + .update(monitor) 148 + .set({ 149 + ...rest, 150 + regions: regions ? regions.join(",") : _monitor.regions, 151 + otelHeaders: otelHeadersEntries 152 + ? JSON.stringify(otelHeadersEntries) 153 + : _monitor.otelHeaders, 154 + timeout: input.timeout || 45000, 155 + updatedAt: new Date(), 156 + }) 157 + .where(eq(monitor.id, Number(_monitor.id))) 158 + .returning() 159 + .get(); 160 + const r = MonitorSchema.parse(_newMonitor); 161 + return c.json(r, 200); 162 + } 163 + 164 + throw new OpenStatusApiError({ 165 + code: "NOT_FOUND", 166 + message: "Something went wrong", 167 + }); 116 168 }); 117 169 }
+21 -13
apps/server/src/routes/v1/monitors/schema.ts
··· 444 444 textBodyAssertions, 445 445 ]); 446 446 447 - export const HTTPMonitorSchema = baseRequest.extend({ 448 - assertions: z.array(assertionsSchema).optional().openapi({ 449 - description: "Assertions to run on the response", 450 - }), 451 - request: httpRequestSchema.openapi({ 452 - description: "The HTTP Request we are sending", 453 - }), 454 - }); 447 + export const HTTPMonitorSchema = baseRequest 448 + .extend({ 449 + assertions: z.array(assertionsSchema).optional().openapi({ 450 + description: "Assertions to run on the response", 451 + }), 452 + request: httpRequestSchema.openapi({ 453 + description: "The HTTP Request we are sending", 454 + }), 455 + }) 456 + .openapi({ 457 + title: "HTTP Monitor Schema", 458 + }); 455 459 456 - export const TCPMonitorSchema = baseRequest.extend({ 457 - request: tcpRequestSchema.openapi({ 458 - description: "The TCP Request we are sending", 459 - }), 460 - }); 460 + export const TCPMonitorSchema = baseRequest 461 + .extend({ 462 + request: tcpRequestSchema.openapi({ 463 + description: "The TCP Request we are sending", 464 + }), 465 + }) 466 + .openapi({ 467 + title: "TCP Monitor Schema", 468 + });