lukePeavey / quotable

Random Quotes API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request for Limit Parameter in /random Endpoint

eljubec opened this issue · comments

Hello, @lukePeavey,

In my opinion, it would be beneficial to include a "limit" parameter for the /random endpoint, which would allow users to retrieve multiple random quotes. Adding this feature would enhance the user experience by providing increased randomness and would make for a valuable addition to the service.

Sincerely,
Elias

I agree that it would be ideal if the /random endpoint returned an array of one or more random quotes, with one being the default.

The Problem

The response schema for the /random endpoint is a single Quote object. For this method to return multiple quotes, we would need to change the response type to an array of quote objects. That would be a breaking change.

I think the only way to do this would be to add a separate endpoint.

Implementation

This would be easy to implement.

The $sample operator that we use to select random quotes from the database supports a size parameter, which specifies the number of random documents to retrieve. All we would need to do is add a limit or count parameter, and pass that value to the size option.

// Select an array of random quotes from the database
const results = await Quotes.aggregate([
  // Apply filters (if any)
  { $match: filter },
  // Select n random quotes from the database
  // where n = limit
  { $sample: { size: limit } },
  { $project: { __v: 0, authorId: 0 } },
]);

I agree with you that it would be ideal for the /random endpoint to return an array of one or more random quotes, with one being the default.

However as you mentioned, changing the response schema to an array of quote objects would be a breaking change.

If a new endpoint were to be introduced, I would like to suggest using a "limit" parameter to specify the number of random quotes to retrieve.

Overall, this would provide greater flexibility to users who want to retrieve more than one random quote, while avoiding any disruption to existing implementations that rely on the current response schema of the /random endpoint.

Will submit PR shortly

Thanks for the effort, looking forward