-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
|
||
import { List } from "../../../models/table"; | ||
import { handler } from "../update"; | ||
|
||
describe("UPDATE List", () => { | ||
it("requires path parameter ID", async () => { | ||
const response = await handler({ | ||
headers: { "Content-Type": "application/json" }, | ||
body: null, | ||
}); | ||
|
||
expect(response).toStrictEqual({ | ||
statusCode: 400, | ||
headers: { "Content-Type": "application/json" }, | ||
body: '{"issues":[{"code":"invalid_type","expected":"object","received":"null","path":[],"message":"Expected object, received null"}],"name":"ZodError"}', | ||
}); | ||
}); | ||
|
||
it("returns found list by ID", async () => { | ||
const list = await List.create({ | ||
name: "<NAME>", | ||
description: "This is a test list", | ||
}); | ||
|
||
const response = await handler({ | ||
headers: { "Content-Type": "application/json" }, | ||
pathParameters: { id: list.id }, | ||
body: JSON.stringify({ name: "Name v2", description: "Description v2" }), | ||
}); | ||
|
||
expect(response.statusCode).toBe(200); | ||
|
||
const body = JSON.parse(response.body); | ||
expect(body.name).toBe("Name v2"); | ||
expect(body.description).toBe("Description v2"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,36 @@ | ||
export const handler = () => {}; | ||
import middy from "@middy/core"; | ||
import httpErrorHandler from "@middy/http-error-handler"; | ||
import httpJsonBodyParser from "@middy/http-json-body-parser"; | ||
import { APIGatewayProxyEventV2, Handler } from "aws-lambda"; | ||
import { z } from "zod"; | ||
|
||
import { List } from "../../models/table"; | ||
import { ValidationError } from "../../utils/validationError"; | ||
|
||
const updateHandler: Handler<APIGatewayProxyEventV2> = async (event) => { | ||
const listSchema = z.object({ | ||
name: z.string(), | ||
description: z.string(), | ||
}) satisfies z.ZodType<List>; | ||
|
||
const list = listSchema.safeParse(event.body); | ||
if (!list.success) { | ||
throw new ValidationError(400, list.error); | ||
} | ||
|
||
const updateListRaw = { | ||
id: event.pathParameters?.id, | ||
...list.data, | ||
}; | ||
const updatedList = await List.update(updateListRaw); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(updatedList), | ||
}; | ||
}; | ||
|
||
export const handler = middy() | ||
.use(httpErrorHandler({ logger: false })) | ||
.use(httpJsonBodyParser()) | ||
.handler(updateHandler); |