kwicherbelliaken / bad-reviews-make-good-movies

0 stars 0 forks source link

[FEATURE]: get astro using new API changes #88

Closed slackermorris closed 7 months ago

slackermorris commented 7 months ago
Uh oh, encountered a validation error. Error in 'body': Expected object, received string. Expected object, received string. Expected object, received string.

This is the request being attempted:

const response = await fetch(
    "https://97ogx4wg9c.execute-api.ap-southeast-2.amazonaws.com/movies/qfJTV11_V5KERNH7V0qT3",
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        username: "trial-user",
        payload,
      }),
    }
  );
slackermorris commented 7 months ago

So, the error very much resides with the schema and not other things in the fullSchema. Definitely within the body. So, how can I test the schema in its limited scope?

slackermorris commented 7 months ago

So, just using the eventSchema worked when parsing.

export const eventSchema = z.object({
  body: z.object({
    username: movieSchema.shape.username,
    payload: movieSchema.shape.movieDetails,
  }),
  pathParameters: z.object({
    watchlistId: movieSchema.shape.watchlistId,
  }),
});
 const exampleEvent = {
          body: {
            username: "trial-user",
            payload: {
              title: "The Matrix",
              year: "1999",
              genre: "Action",
              director: "The Wachowskis",
              actors: [
                "Keanu Reeves",
                "Laurence Fishburne",
                "Carrie-Anne Moss",
              ],
              plot: "A computer hacker learns about the true nature of his reality and his role in the war against its controllers.",
              poster:
                "https://m.media-amazon.com/images/I/71c5x9zK0NL._AC_SY679_.jpg",
              imdbRating: "8.7",
              poster_path: "/hEpWvX6Bp79eLxY1kX5ZZJcme5U.jpg",
              release_date: "1999-03-30",
              overview: "unsure",
              cast: [
                {
                  name: "Keanu Reeves",
                  character: "Neo",
                },
                {
                  name: "Laurence Fishburne",
                  character: "Morpheus",
                },
                {
                  name: "Carrie-Anne Moss",
                  character: "Trinity",
                },
              ],
              genres: [
                {
                  name: "Action",
                },
                {
                  name: "Science Fiction",
                },
              ],
            },
          },
          pathParameters: {
            watchlistId: "qfJTV11_V5KERNH7V0qT3",
          },
        };

        console.log("beginning parsing example event");
        console.log(testThisSchema.parse(exampleEvent));
        console.log("finished parsing request event");

testThisSchema being eventSchema

slackermorris commented 7 months ago

I think that I solved it. The issue was that genre on the body did not match the expected type definition.

type Genre = {
  id: number;
  name: string;
};

genres: Array<Pick<Genre, "name">>

Does not mean that genres is going to be an array of strings. It instead means it will be an array like:

genres: [ { name: 'genreName' } ]

To achieve the mapping I was after I need to do something like:

genres: Array<Genre['name']>
slackermorris commented 7 months ago

Now, why isn't the watchlist loading? Probably the wrong endpoint.

slackermorris commented 7 months ago

OK I think we are closer to where we want to be. I just need to refactor the getImage endpoint.

Screen Shot 2024-02-03 at 11 07 30 AM
slackermorris commented 7 months ago

Well, damn. I feel like my work here is done.