jonschlinkert / paged-request

Simplified requests for paged (paginated) content.
https://github.com/jonschlinkert
MIT License
7 stars 3 forks source link

Allow to receive options to request from next function #2

Closed 7odri9o closed 4 years ago

7odri9o commented 4 years ago

I made this pull request to allow to use paged-request better. I am using paged-request npm package on professional project to make a list of request. I need to pass a JWT authentication on header. It is important to verify if JWT has expired before use it and this PR will allow to verify it or reauthenticate if it is needed.

I made a fix on tests before to refactor the main funcionality. The tests were breaking because regex to get next page number from html wasn't working.

These was the regex:

const regex = /href=".*?\/page\/(\d+)\/"/;

And this is the piece of html that is commig on response:

<nav class="pagination col col-12">
  <ul>
    <li class=pagination__current>1</li>
    <li><a href=/categories/css/page/2/>2 </a> </li> <li><a href=/categories/css/page/3/>3 </a> </li> <li
          class=pagination__ellipsis>...</li>
    <li class=pagination__next><a href=/categories/css/page/2/> <svg viewBox="0 0 100 100" width="20" height="20"
        tabindex="-1" role="img" aria-labelledby="title">
        <title>Next</title>
        <path d="M10 50l50 50L70 90 30 50 70 10 60 0z" transform="translate(0 0) rotate(180 50 50)" /></svg>
      </a></li>
  </ul>
</nav>

As you don't have double quotes after href. Regex exec was returning nothing. The regex on test was changed to this:

const regex = /href=.*?\/page\/(\d+)/;

Next. I started to refactor.

I changed opts to let so I can modify it after inside while:

let opts = Object.assign({}, options);

And then the while loop is now as below:

  while (url && typeof url === 'string' && !acc.urls.includes(url)) {
    acc.urls.push(url);
    res = await axios.get(url, opts);
    const result = await next(url, res, acc);
    if (result) {
      url = result.nextUrl
      opts = result.nextOptions
    } else {
      url = null
    }
    acc.pages.push(res);
  }

Now you can authenticate before every request and make other important things on your project as needed.

I hope you can approve my work.

OBS: 7odri9o and rrodrigohenrique are the same person.