5

Hello I've got a small challenge to do where I have to display some data that I get from an api. The main page will display the first 20 results and clicking on a button will add 20 more results from the page.

The api call that I was given returns an array with around 1500 elements and the api doesn't have a parameter to limit the amount of elements in the array so my question is if I can limit it somehow with axios or should I just fetch all of these elements and display them?

This is the api: https://api.chucknorris.io/

2
  • can you shear the api with us?
    – Dan Starns
    Commented Oct 15, 2019 at 9:26
  • @DanStarns Added the link in the post. I can't find anything in the documentation that can limit the amount of data i get from a call.
    – captain
    Commented Oct 15, 2019 at 9:27

2 Answers 2

6

there are two answers for your question

the short answer is :

On your side, there's nothing you can do until pagination is implemented on API side

the second answer is :

you can handle it using http module like this

 http.request(opts, function(response) {
    var request = this;
    console.log("Content-length: ", response.headers['content-length']);
    var str = '';
    response.on('data', function (chunk) {
      str += chunk;
      if (str.length > 10000)
      {
        request.abort();
      }
    });
    response.on('end', function() {
      console.log('done', str.length);
      ...
    });
  }).end();

This will abort the request at around 10.000 bytes, since the data arrives in chunks of various sizes.

1
  • Nice, I'll take a look at it.
    – captain
    Commented Oct 15, 2019 at 9:41
3

Since the API has no parameter to limit the amount of results you are responsible for modifying the response.

Since you're using Axios you could do this with a response interceptor so that the response is modified before reaching your application.

You may want to consider where the best place to do this is though. If you allow the full response to come back to your application and then store it somewhere, it may be easier to return the next page of 20 results at the user's request rather than repeatedly calling the API.

3
  • Yes that is what I'm doing. I'm storing everything in an array when the app get's loaded and display just part of the array on the frontend. I guess the example with the response interceptor is probably out of the scope ofi this challenge but I will have a look. I was just worried that they might say that it is not performant to get all the data from the beginning even though the response size is around 100kb.
    – captain
    Commented Oct 15, 2019 at 9:39
  • It's an understandable concern, but given you are limited by the API you have no choice and it sounds like you are taking the appropriate action.
    – Tom
    Commented Oct 15, 2019 at 9:41
  • Yep, that's what I imagined, I will tell the CTO my concerns regarding this issue after submitting the challenge. Thanks for clearing this up for me.
    – captain
    Commented Oct 15, 2019 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.