hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.64k stars 65 forks source link

"Object.assign" in add function clear the previous stack middlewares #21

Closed GeinerGV closed 4 years ago

GeinerGV commented 4 years ago

The function doesn't always work, because stack property is an Array

function add(method, ...handle) {
    for (let i = 0; i < handle.length; i += 1) {
      if (handle[i].stack) Object.assign(this.stack, handle[i].stack);
      else this.stack.push({ handle: handle[i], method });
    }
  }

a temporal solution: Change it with a forEach and push

function add(method, ...handle) {
    for (let i = 0; i < handle.length; i += 1) {
      if (handle[i].stack) handle[i].stack.forEach(objMiddle){this.stack.push(objMiddle);}
      else this.stack.push({ handle: handle[i], method });
    }
  }
hoangvvo commented 4 years ago

Ooops, that is right. I was thinking that Array was also an Object (which is true), but doing Object.assign would replace the target Array since the target and source share the same indexes (0,1,2,...).

Could you do a PR?

hoangvvo commented 4 years ago

@GeinerGV Are you plannig to do a PR? If not, I will open one. Thanks!